1 // SPDX-License-Identifier: GPL-2.0-only
4 * Helper functions generally used for parsing kernel command line
7 * Code and copyrights come from init/main.c and arch/i386/kernel/setup.c.
9 * GNU Indent formatting options for this file: -kr -i8 -npsl -pcs
12 #include <linux/export.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/ctype.h>
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.
23 static int get_range(char **str, int *pint, int n)
25 int x, inc_counter, upper_range;
28 upper_range = simple_strtol((*str), NULL, 0);
29 inc_counter = upper_range - *pint;
30 for (x = *pint; n && x < upper_range; x++, n--)
36 * get_option - Parse integer from an option string
38 * @pint: (output) integer value parsed from @str
40 * Read an int from an option string; if available accept a subsequent
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
50 int get_option(char **str, int *pint)
56 *pint = simple_strtol(cur, str, 0);
68 EXPORT_SYMBOL(get_option);
71 * get_options - Parse a string into a list of integers
72 * @str: String to be parsed
73 * @nints: size of integer array
74 * @ints: integer array
76 * This function parses a string containing a comma-separated
77 * list of integers, a hyphen-separated range of _positive_ integers,
78 * or a combination of both. The parse halts when the array is
79 * full, or when no more numbers can be retrieved from the
82 * Return value is the character in the string which caused
83 * the parse to end (typically a null terminator, if @str is
84 * completely parseable).
87 char *get_options(const char *str, int nints, int *ints)
92 res = get_option((char **)&str, ints + i);
97 range_nums = get_range((char **)&str, ints + i, nints - i);
101 * Decrement the result by one to leave out the
102 * last number in the range. The next iteration
103 * will handle the upper number in the range
105 i += (range_nums - 1);
114 EXPORT_SYMBOL(get_options);
117 * memparse - parse a string with mem suffixes into a number
118 * @ptr: Where parse begins
119 * @retptr: (output) Optional pointer to next char after parse completes
121 * Parses a string into a number. The number stored at @ptr is
122 * potentially suffixed with K, M, G, T, P, E.
125 unsigned long long memparse(const char *ptr, char **retptr)
127 char *endptr; /* local pointer to end of parsed string */
129 unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
165 EXPORT_SYMBOL(memparse);
168 * parse_option_str - Parse a string and check an option is set or not
169 * @str: String to be parsed
170 * @option: option name
172 * This function parses a string containing a comma-separated list of
173 * strings like a=b,c.
175 * Return true if there's such option in the string, or return false.
177 bool parse_option_str(const char *str, const char *option)
180 if (!strncmp(str, option, strlen(option))) {
181 str += strlen(option);
182 if (!*str || *str == ',')
186 while (*str && *str != ',')
197 * Parse a string to get a param value pair.
198 * You can use " around spaces, but can't escape ".
199 * Hyphens and underscores equivalent in parameter names.
201 char *next_arg(char *args, char **param, char **val)
203 unsigned int i, equals = 0;
204 int in_quote = 0, quoted = 0;
213 for (i = 0; args[i]; i++) {
214 if (isspace(args[i]) && !in_quote)
221 in_quote = !in_quote;
229 *val = args + equals + 1;
231 /* Don't include quotes in value. */
234 if (args[i-1] == '"')
238 if (quoted && args[i-1] == '"')
247 /* Chew up trailing spaces. */
248 return skip_spaces(next);