This source file includes following definitions.
- get_range
- get_option
- get_options
- memparse
- parse_option_str
- next_arg
1
2
3
4
5
6
7
8
9
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
19
20
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50 int get_option(char **str, int *pint)
51 {
52 char *cur = *str;
53
54 if (!cur || !(*cur))
55 return 0;
56 *pint = simple_strtol(cur, str, 0);
57 if (cur == *str)
58 return 0;
59 if (**str == ',') {
60 (*str)++;
61 return 2;
62 }
63 if (**str == '-')
64 return 3;
65
66 return 1;
67 }
68 EXPORT_SYMBOL(get_option);
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 char *get_options(const char *str, int nints, int *ints)
88 {
89 int res, i = 1;
90
91 while (i < nints) {
92 res = get_option((char **)&str, ints + i);
93 if (res == 0)
94 break;
95 if (res == 3) {
96 int range_nums;
97 range_nums = get_range((char **)&str, ints + i, nints - i);
98 if (range_nums < 0)
99 break;
100
101
102
103
104
105 i += (range_nums - 1);
106 }
107 i++;
108 if (res == 1)
109 break;
110 }
111 ints[0] = i - 1;
112 return (char *)str;
113 }
114 EXPORT_SYMBOL(get_options);
115
116
117
118
119
120
121
122
123
124
125 unsigned long long memparse(const char *ptr, char **retptr)
126 {
127 char *endptr;
128
129 unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
130
131 switch (*endptr) {
132 case 'E':
133 case 'e':
134 ret <<= 10;
135
136 case 'P':
137 case 'p':
138 ret <<= 10;
139
140 case 'T':
141 case 't':
142 ret <<= 10;
143
144 case 'G':
145 case 'g':
146 ret <<= 10;
147
148 case 'M':
149 case 'm':
150 ret <<= 10;
151
152 case 'K':
153 case 'k':
154 ret <<= 10;
155 endptr++;
156 default:
157 break;
158 }
159
160 if (retptr)
161 *retptr = endptr;
162
163 return ret;
164 }
165 EXPORT_SYMBOL(memparse);
166
167
168
169
170
171
172
173
174
175
176
177 bool parse_option_str(const char *str, const char *option)
178 {
179 while (*str) {
180 if (!strncmp(str, option, strlen(option))) {
181 str += strlen(option);
182 if (!*str || *str == ',')
183 return true;
184 }
185
186 while (*str && *str != ',')
187 str++;
188
189 if (*str == ',')
190 str++;
191 }
192
193 return false;
194 }
195
196
197
198
199
200
201 char *next_arg(char *args, char **param, char **val)
202 {
203 unsigned int i, equals = 0;
204 int in_quote = 0, quoted = 0;
205 char *next;
206
207 if (*args == '"') {
208 args++;
209 in_quote = 1;
210 quoted = 1;
211 }
212
213 for (i = 0; args[i]; i++) {
214 if (isspace(args[i]) && !in_quote)
215 break;
216 if (equals == 0) {
217 if (args[i] == '=')
218 equals = i;
219 }
220 if (args[i] == '"')
221 in_quote = !in_quote;
222 }
223
224 *param = args;
225 if (!equals)
226 *val = NULL;
227 else {
228 args[equals] = '\0';
229 *val = args + equals + 1;
230
231
232 if (**val == '"') {
233 (*val)++;
234 if (args[i-1] == '"')
235 args[i-1] = '\0';
236 }
237 }
238 if (quoted && args[i-1] == '"')
239 args[i-1] = '\0';
240
241 if (args[i]) {
242 args[i] = '\0';
243 next = args + i + 1;
244 } else
245 next = args + i;
246
247
248 return skip_spaces(next);
249 }