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