OSDN Git Service

ca89846ee0bb929c2b5b5070ad2651b4800eec56
[uclinux-h8/linux.git] / lib / cmdline.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/lib/cmdline.c
4  * Helper functions generally used for parsing kernel command line
5  * and module options.
6  *
7  * Code and copyrights come from init/main.c and arch/i386/kernel/setup.c.
8  *
9  * GNU Indent formatting options for this file: -kr -i8 -npsl -pcs
10  */
11
12 #include <linux/export.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/ctype.h>
16
17 /*
18  *      If a hyphen was found in get_option, this will handle the
19  *      range of numbers, M-N.  This will expand the range and insert
20  *      the values[M, M+1, ..., N] into the ints array in get_options.
21  */
22
23 static int get_range(char **str, int *pint, int n)
24 {
25         int x, inc_counter, upper_range;
26
27         (*str)++;
28         upper_range = simple_strtol((*str), NULL, 0);
29         inc_counter = upper_range - *pint;
30         for (x = *pint; n && x < upper_range; x++, n--)
31                 *pint++ = x;
32         return inc_counter;
33 }
34
35 /**
36  *      get_option - Parse integer from an option string
37  *      @str: option string
38  *      @pint: (output) integer value parsed from @str
39  *
40  *      Read an int from an option string; if available accept a subsequent
41  *      comma as well.
42  *
43  *      Return values:
44  *      0 - no int in string
45  *      1 - int found, no subsequent comma
46  *      2 - int found including a subsequent comma
47  *      3 - hyphen found to denote a range
48  *
49  *      Leading hyphen without integer is no integer case, but we consume it
50  *      for the sake of simplification.
51  */
52
53 int get_option(char **str, int *pint)
54 {
55         char *cur = *str;
56
57         if (!cur || !(*cur))
58                 return 0;
59         if (*cur == '-')
60                 *pint = -simple_strtoull(++cur, str, 0);
61         else
62                 *pint = simple_strtoull(cur, str, 0);
63         if (cur == *str)
64                 return 0;
65         if (**str == ',') {
66                 (*str)++;
67                 return 2;
68         }
69         if (**str == '-')
70                 return 3;
71
72         return 1;
73 }
74 EXPORT_SYMBOL(get_option);
75
76 /**
77  *      get_options - Parse a string into a list of integers
78  *      @str: String to be parsed
79  *      @nints: size of integer array
80  *      @ints: integer array
81  *
82  *      This function parses a string containing a comma-separated
83  *      list of integers, a hyphen-separated range of _positive_ integers,
84  *      or a combination of both.  The parse halts when the array is
85  *      full, or when no more numbers can be retrieved from the
86  *      string.
87  *
88  *      Return value is the character in the string which caused
89  *      the parse to end (typically a null terminator, if @str is
90  *      completely parseable).
91  */
92
93 char *get_options(const char *str, int nints, int *ints)
94 {
95         int res, i = 1;
96
97         while (i < nints) {
98                 res = get_option((char **)&str, ints + i);
99                 if (res == 0)
100                         break;
101                 if (res == 3) {
102                         int range_nums;
103                         range_nums = get_range((char **)&str, ints + i, nints - i);
104                         if (range_nums < 0)
105                                 break;
106                         /*
107                          * Decrement the result by one to leave out the
108                          * last number in the range.  The next iteration
109                          * will handle the upper number in the range
110                          */
111                         i += (range_nums - 1);
112                 }
113                 i++;
114                 if (res == 1)
115                         break;
116         }
117         ints[0] = i - 1;
118         return (char *)str;
119 }
120 EXPORT_SYMBOL(get_options);
121
122 /**
123  *      memparse - parse a string with mem suffixes into a number
124  *      @ptr: Where parse begins
125  *      @retptr: (output) Optional pointer to next char after parse completes
126  *
127  *      Parses a string into a number.  The number stored at @ptr is
128  *      potentially suffixed with K, M, G, T, P, E.
129  */
130
131 unsigned long long memparse(const char *ptr, char **retptr)
132 {
133         char *endptr;   /* local pointer to end of parsed string */
134
135         unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
136
137         switch (*endptr) {
138         case 'E':
139         case 'e':
140                 ret <<= 10;
141                 /* fall through */
142         case 'P':
143         case 'p':
144                 ret <<= 10;
145                 /* fall through */
146         case 'T':
147         case 't':
148                 ret <<= 10;
149                 /* fall through */
150         case 'G':
151         case 'g':
152                 ret <<= 10;
153                 /* fall through */
154         case 'M':
155         case 'm':
156                 ret <<= 10;
157                 /* fall through */
158         case 'K':
159         case 'k':
160                 ret <<= 10;
161                 endptr++;
162         default:
163                 break;
164         }
165
166         if (retptr)
167                 *retptr = endptr;
168
169         return ret;
170 }
171 EXPORT_SYMBOL(memparse);
172
173 /**
174  *      parse_option_str - Parse a string and check an option is set or not
175  *      @str: String to be parsed
176  *      @option: option name
177  *
178  *      This function parses a string containing a comma-separated list of
179  *      strings like a=b,c.
180  *
181  *      Return true if there's such option in the string, or return false.
182  */
183 bool parse_option_str(const char *str, const char *option)
184 {
185         while (*str) {
186                 if (!strncmp(str, option, strlen(option))) {
187                         str += strlen(option);
188                         if (!*str || *str == ',')
189                                 return true;
190                 }
191
192                 while (*str && *str != ',')
193                         str++;
194
195                 if (*str == ',')
196                         str++;
197         }
198
199         return false;
200 }
201
202 /*
203  * Parse a string to get a param value pair.
204  * You can use " around spaces, but can't escape ".
205  * Hyphens and underscores equivalent in parameter names.
206  */
207 char *next_arg(char *args, char **param, char **val)
208 {
209         unsigned int i, equals = 0;
210         int in_quote = 0, quoted = 0;
211         char *next;
212
213         if (*args == '"') {
214                 args++;
215                 in_quote = 1;
216                 quoted = 1;
217         }
218
219         for (i = 0; args[i]; i++) {
220                 if (isspace(args[i]) && !in_quote)
221                         break;
222                 if (equals == 0) {
223                         if (args[i] == '=')
224                                 equals = i;
225                 }
226                 if (args[i] == '"')
227                         in_quote = !in_quote;
228         }
229
230         *param = args;
231         if (!equals)
232                 *val = NULL;
233         else {
234                 args[equals] = '\0';
235                 *val = args + equals + 1;
236
237                 /* Don't include quotes in value. */
238                 if (**val == '"') {
239                         (*val)++;
240                         if (args[i-1] == '"')
241                                 args[i-1] = '\0';
242                 }
243         }
244         if (quoted && args[i-1] == '"')
245                 args[i-1] = '\0';
246
247         if (args[i]) {
248                 args[i] = '\0';
249                 next = args + i + 1;
250         } else
251                 next = args + i;
252
253         /* Chew up trailing spaces. */
254         return skip_spaces(next);
255 }