3 * Helper functions generally used for parsing kernel command line
6 * Code and copyrights come from init/main.c and arch/i386/kernel/setup.c.
8 * This source code is licensed under the GNU General Public License,
9 * Version 2. See the file COPYING for more details.
11 * GNU Indent formatting options for this file: -kr -i8 -npsl -pcs
15 #include <linux/export.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/ctype.h>
21 * If a hyphen was found in get_option, this will handle the
22 * range of numbers, M-N. This will expand the range and insert
23 * the values[M, M+1, ..., N] into the ints array in get_options.
26 static int get_range(char **str
, int *pint
, int n
)
28 int x
, inc_counter
, upper_range
;
31 upper_range
= simple_strtol((*str
), NULL
, 0);
32 inc_counter
= upper_range
- *pint
;
33 for (x
= *pint
; n
&& x
< upper_range
; x
++, n
--)
39 * get_option - Parse integer from an option string
41 * @pint: (output) integer value parsed from @str
43 * Read an int from an option string; if available accept a subsequent
47 * 0 - no int in string
48 * 1 - int found, no subsequent comma
49 * 2 - int found including a subsequent comma
50 * 3 - hyphen found to denote a range
53 int get_option(char **str
, int *pint
)
59 *pint
= simple_strtol(cur
, str
, 0);
71 EXPORT_SYMBOL(get_option
);
74 * get_options - Parse a string into a list of integers
75 * @str: String to be parsed
76 * @nints: size of integer array
77 * @ints: integer array
79 * This function parses a string containing a comma-separated
80 * list of integers, a hyphen-separated range of _positive_ integers,
81 * or a combination of both. The parse halts when the array is
82 * full, or when no more numbers can be retrieved from the
85 * Return value is the character in the string which caused
86 * the parse to end (typically a null terminator, if @str is
87 * completely parseable).
90 char *get_options(const char *str
, int nints
, int *ints
)
95 res
= get_option((char **)&str
, ints
+ i
);
100 range_nums
= get_range((char **)&str
, ints
+ i
, nints
- i
);
104 * Decrement the result by one to leave out the
105 * last number in the range. The next iteration
106 * will handle the upper number in the range
108 i
+= (range_nums
- 1);
117 EXPORT_SYMBOL(get_options
);
120 * memparse - parse a string with mem suffixes into a number
121 * @ptr: Where parse begins
122 * @retptr: (output) Optional pointer to next char after parse completes
124 * Parses a string into a number. The number stored at @ptr is
125 * potentially suffixed with K, M, G, T, P, E.
128 unsigned long long memparse(const char *ptr
, char **retptr
)
130 char *endptr
; /* local pointer to end of parsed string */
132 unsigned long long ret
= simple_strtoull(ptr
, &endptr
, 0);
163 EXPORT_SYMBOL(memparse
);
166 * parse_option_str - Parse a string and check an option is set or not
167 * @str: String to be parsed
168 * @option: option name
170 * This function parses a string containing a comma-separated list of
171 * strings like a=b,c.
173 * Return true if there's such option in the string, or return false.
175 bool parse_option_str(const char *str
, const char *option
)
178 if (!strncmp(str
, option
, strlen(option
))) {
179 str
+= strlen(option
);
180 if (!*str
|| *str
== ',')
184 while (*str
&& *str
!= ',')
195 * Parse a string to get a param value pair.
196 * You can use " around spaces, but can't escape ".
197 * Hyphens and underscores equivalent in parameter names.
199 char *next_arg(char *args
, char **param
, char **val
)
201 unsigned int i
, equals
= 0;
202 int in_quote
= 0, quoted
= 0;
211 for (i
= 0; args
[i
]; i
++) {
212 if (isspace(args
[i
]) && !in_quote
)
219 in_quote
= !in_quote
;
227 *val
= args
+ equals
+ 1;
229 /* Don't include quotes in value. */
232 if (args
[i
-1] == '"')
236 if (quoted
&& args
[i
-1] == '"')
245 /* Chew up trailing spaces. */
246 return skip_spaces(next
);