]> rtime.felk.cvut.cz Git - jailhouse.git/blob - inmates/lib/cmdline.c
inmates: Add support for command line parameters
[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                         if (*p == 0)
27                                 return false;
28                         p++;
29                 }
30
31                 if (strncmp(p, param, param_len) == 0) {
32                         p += param_len;
33
34                         /* check for boolean parameter */
35                         if ((buffer_size == 0 && (*p == ' ' || *p == 0)))
36                                 return true;
37
38                         /* extract parameter value */
39                         if (*p == '=') {
40                                 p++;
41                                 while (buffer_size > 1) {
42                                         if (*p == ' ' || *p == 0)
43                                                 break;
44                                         *value_buffer++ = *p++;
45                                         buffer_size--;
46                                 }
47                                 if (buffer_size > 0)
48                                         *value_buffer = 0;
49                                 return true;
50                         }
51                 }
52
53                 /* search for end of this parameter */
54                 while (*p != ' ') {
55                         if (*p == 0)
56                                 return false;
57                         p++;
58                 }
59         }
60 }
61
62 const char *cmdline_parse_str(const char *param, char *value_buffer,
63                               unsigned long buffer_size,
64                               const char *default_value)
65 {
66         if (get_param(param, value_buffer, buffer_size))
67                 return value_buffer;
68         else
69                 return default_value;
70 }
71
72 long long cmdline_parse_int(const char *param, long long default_value)
73 {
74         char value_buffer[32];
75         char *p = value_buffer;
76         bool negative = false;
77         long long value = 0;
78
79         if (!get_param(param, value_buffer, sizeof(value_buffer)))
80                 return default_value;
81
82         if (strncmp(p, "0x", 2) == 0) {
83                 p += 2;
84                 do {
85                         if (*p >= '0' && *p <= '9')
86                                 value = (value << 4) + *p - '0';
87                         else if (*p >= 'A' && *p <= 'F')
88                                 value = (value << 4) + *p - 'A';
89                         else if (*p >= 'a' && *p <= 'f')
90                                 value = (value << 4) + *p - 'a';
91                         else
92                                 return default_value;
93                         p++;
94                 } while (*p != 0);
95         } else {
96                 if (*p == '-' || *p == '+')
97                         negative = (*p++ == '-');
98
99                 do {
100                         if (*p >= '0' && *p <= '9')
101                                 value = (value * 10) + *p - '0';
102                         else
103                                 return default_value;
104                         p++;
105                 } while (*p != 0);
106         }
107
108         return negative ? -value : value;
109 }
110
111 bool cmdline_parse_bool(const char *param)
112 {
113         return get_param(param, NULL, 0);
114 }