]> rtime.felk.cvut.cz Git - jailhouse.git/blob - inmates/lib/cmdline.c
inmates/lib: cmdline.c
[jailhouse.git] / inmates / lib / cmdline.c
1 /*
2  * Jailhouse, a Linux-based partitioning hypervisor
3  *
4  * Copyright (c) Siemens AG, 2016
5  *
6  * Authors:
7  *  Jan Kiszka <jan.kiszka@siemens.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  */
12
13 #include <inmate.h>
14
15 extern const char cmdline[];
16
17 static bool get_param(const char *param, char *value_buffer,
18                       unsigned long buffer_size)
19 {
20         unsigned long param_len = strlen(param);
21         const char *p = cmdline;
22
23         while (1) {
24                 /* read over leading blanks */
25                 while (*p == ' ')
26                         p++;
27
28                 if (strncmp(p, param, param_len) == 0) {
29                         p += param_len;
30
31                         /* check for boolean parameter */
32                         if ((buffer_size == 0 && (*p == ' ' || *p == 0)))
33                                 return true;
34
35                         /* extract parameter value */
36                         if (*p == '=') {
37                                 p++;
38                                 while (buffer_size > 1) {
39                                         if (*p == ' ' || *p == 0)
40                                                 break;
41                                         *value_buffer++ = *p++;
42                                         buffer_size--;
43                                 }
44                                 if (buffer_size > 0)
45                                         *value_buffer = 0;
46                                 return true;
47                         }
48                 }
49
50                 /* search for end of this parameter */
51                 while (*p != ' ') {
52                         if (*p == 0)
53                                 return false;
54                         p++;
55                 }
56         }
57 }
58
59 const char *cmdline_parse_str(const char *param, char *value_buffer,
60                               unsigned long buffer_size,
61                               const char *default_value)
62 {
63         if (get_param(param, value_buffer, buffer_size))
64                 return value_buffer;
65         else
66                 return default_value;
67 }
68
69 long long cmdline_parse_int(const char *param, long long default_value)
70 {
71         char value_buffer[32];
72         char *p = value_buffer;
73         bool negative = false;
74         long long value = 0;
75
76         if (!get_param(param, value_buffer, sizeof(value_buffer)))
77                 return default_value;
78
79         if (strncmp(p, "0x", 2) == 0) {
80                 p += 2;
81                 do {
82                         if (*p >= '0' && *p <= '9')
83                                 value = (value << 4) + *p - '0';
84                         else if (*p >= 'A' && *p <= 'F')
85                                 value = (value << 4) + *p - 'A' + 10;
86                         else if (*p >= 'a' && *p <= 'f')
87                                 value = (value << 4) + *p - 'a' + 10;
88                         else
89                                 return default_value;
90                         p++;
91                 } while (*p != 0);
92         } else {
93                 if (*p == '-' || *p == '+')
94                         negative = (*p++ == '-');
95
96                 do {
97                         if (*p >= '0' && *p <= '9')
98                                 value = (value * 10) + *p - '0';
99                         else
100                                 return default_value;
101                         p++;
102                 } while (*p != 0);
103         }
104
105         return negative ? -value : value;
106 }
107
108 bool cmdline_parse_bool(const char *param)
109 {
110         return get_param(param, NULL, 0);
111 }