Commit | Line | Data |
---|---|---|
cb2c86fd JA |
1 | /* |
2 | * This file contains the ini and command liner parser main. | |
3 | */ | |
4 | #include <stdio.h> | |
5 | #include <stdlib.h> | |
6 | #include <unistd.h> | |
7 | #include <ctype.h> | |
8 | #include <string.h> | |
9 | #include <errno.h> | |
10 | #include <limits.h> | |
11 | ||
12 | #include "parse.h" | |
13 | ||
14 | static unsigned long get_mult_time(char c) | |
15 | { | |
16 | switch (c) { | |
17 | case 'm': | |
18 | case 'M': | |
19 | return 60; | |
20 | case 'h': | |
21 | case 'H': | |
22 | return 60 * 60; | |
23 | case 'd': | |
24 | case 'D': | |
25 | return 24 * 60 * 60; | |
26 | default: | |
27 | return 1; | |
28 | } | |
29 | } | |
30 | ||
31 | static unsigned long get_mult_bytes(char c) | |
32 | { | |
33 | switch (c) { | |
34 | case 'k': | |
35 | case 'K': | |
36 | return 1024; | |
37 | case 'm': | |
38 | case 'M': | |
39 | return 1024 * 1024; | |
40 | case 'g': | |
41 | case 'G': | |
42 | return 1024 * 1024 * 1024; | |
43 | default: | |
44 | return 1; | |
45 | } | |
46 | } | |
47 | ||
48 | /* | |
e1f36503 | 49 | * convert string into decimal value, noting any size suffix |
cb2c86fd | 50 | */ |
b4692828 | 51 | static int str_to_decimal(const char *str, unsigned long long *val, int kilo) |
cb2c86fd | 52 | { |
cb2c86fd JA |
53 | int len; |
54 | ||
cb2c86fd | 55 | len = strlen(str); |
f90eff5a JA |
56 | if (!len) |
57 | return 1; | |
cb2c86fd JA |
58 | |
59 | *val = strtoul(str, NULL, 10); | |
60 | if (*val == ULONG_MAX && errno == ERANGE) | |
61 | return 1; | |
62 | ||
63 | if (kilo) | |
64 | *val *= get_mult_bytes(str[len - 1]); | |
65 | else | |
66 | *val *= get_mult_time(str[len - 1]); | |
67 | return 0; | |
68 | } | |
69 | ||
b4692828 | 70 | static int check_str_bytes(const char *p, unsigned long long *val) |
cb2c86fd | 71 | { |
cb2c86fd JA |
72 | return str_to_decimal(p, val, 1); |
73 | } | |
74 | ||
b4692828 | 75 | static int check_str_time(const char *p, unsigned long long *val) |
cb2c86fd | 76 | { |
cb2c86fd JA |
77 | return str_to_decimal(p, val, 0); |
78 | } | |
79 | ||
80 | void strip_blank_front(char **p) | |
81 | { | |
82 | char *s = *p; | |
83 | ||
84 | while (isspace(*s)) | |
85 | s++; | |
86 | } | |
87 | ||
88 | void strip_blank_end(char *p) | |
89 | { | |
90 | char *s = p + strlen(p) - 1; | |
91 | ||
92 | while (isspace(*s) || iscntrl(*s)) | |
93 | s--; | |
94 | ||
95 | *(s + 1) = '\0'; | |
96 | } | |
97 | ||
b765a372 | 98 | static int check_range_bytes(const char *str, unsigned long *val) |
cb2c86fd JA |
99 | { |
100 | char suffix; | |
101 | ||
102 | if (sscanf(str, "%lu%c", val, &suffix) == 2) { | |
103 | *val *= get_mult_bytes(suffix); | |
104 | return 0; | |
105 | } | |
106 | ||
107 | if (sscanf(str, "%lu", val) == 1) | |
108 | return 0; | |
109 | ||
110 | return 1; | |
111 | } | |
112 | ||
b4692828 | 113 | static int check_int(const char *p, unsigned int *val) |
cb2c86fd | 114 | { |
e1f36503 JA |
115 | if (sscanf(p, "%u", val) == 1) |
116 | return 0; | |
cb2c86fd | 117 | |
e1f36503 JA |
118 | return 1; |
119 | } | |
cb2c86fd | 120 | |
e1f36503 JA |
121 | static struct fio_option *find_option(struct fio_option *options, |
122 | const char *opt) | |
123 | { | |
33963c6c | 124 | struct fio_option *o = &options[0]; |
cb2c86fd | 125 | |
33963c6c | 126 | while (o->name) { |
e1f36503 JA |
127 | if (!strcmp(o->name, opt)) |
128 | return o; | |
cb2c86fd | 129 | |
33963c6c JA |
130 | o++; |
131 | } | |
cb2c86fd | 132 | |
e1f36503 | 133 | return NULL; |
cb2c86fd JA |
134 | } |
135 | ||
f90eff5a JA |
136 | static int __handle_option(struct fio_option *o, const char *ptr, void *data, |
137 | int first) | |
cb2c86fd | 138 | { |
75e6f36f | 139 | unsigned int il, *ilp1, *ilp2; |
e1f36503 | 140 | unsigned long long ull, *ullp; |
75e6f36f | 141 | unsigned long ul1, ul2; |
b4692828 | 142 | char **cp; |
e1f36503 | 143 | int ret = 0, is_time = 0; |
cb2c86fd | 144 | |
e1f36503 JA |
145 | switch (o->type) { |
146 | case FIO_OPT_STR: { | |
147 | fio_opt_str_fn *fn = o->cb; | |
cb2c86fd | 148 | |
e1f36503 JA |
149 | ret = fn(data, ptr); |
150 | break; | |
151 | } | |
152 | case FIO_OPT_STR_VAL_TIME: | |
153 | is_time = 1; | |
75e6f36f JA |
154 | case FIO_OPT_STR_VAL: |
155 | case FIO_OPT_STR_VAL_INT: { | |
e1f36503 JA |
156 | fio_opt_str_val_fn *fn = o->cb; |
157 | ||
158 | if (is_time) | |
159 | ret = check_str_time(ptr, &ull); | |
160 | else | |
161 | ret = check_str_bytes(ptr, &ull); | |
162 | ||
163 | if (ret) | |
164 | break; | |
165 | ||
166 | if (o->max_val && ull > o->max_val) | |
167 | ull = o->max_val; | |
168 | ||
169 | if (fn) | |
170 | ret = fn(data, &ull); | |
171 | else { | |
75e6f36f | 172 | if (o->type == FIO_OPT_STR_VAL_INT) { |
f90eff5a JA |
173 | if (first) { |
174 | ilp1 = td_var(data, o->off1); | |
175 | *ilp1 = ull; | |
176 | if (o->off2) { | |
177 | ilp1 = td_var(data, o->off2); | |
178 | *ilp1 = ull; | |
179 | } | |
180 | } else if (o->off2) { | |
181 | ilp1 = td_var(data, o->off2); | |
182 | *ilp1 = ull; | |
183 | } | |
75e6f36f | 184 | } else { |
f90eff5a JA |
185 | if (first) { |
186 | ullp = td_var(data, o->off1); | |
187 | *ullp = ull; | |
188 | if (o->off2) { | |
189 | ullp = td_var(data, o->off2); | |
190 | *ullp = ull; | |
191 | } | |
192 | } else if (o->off2) { | |
193 | ullp = td_var(data, o->off2); | |
194 | *ullp = ull; | |
195 | } | |
75e6f36f | 196 | } |
e1f36503 JA |
197 | } |
198 | break; | |
199 | } | |
200 | case FIO_OPT_STR_STORE: | |
201 | cp = td_var(data, o->off1); | |
202 | *cp = strdup(ptr); | |
203 | break; | |
204 | case FIO_OPT_RANGE: { | |
b765a372 | 205 | char tmp[128]; |
e1f36503 JA |
206 | char *p1, *p2; |
207 | ||
0bbab0e7 | 208 | strncpy(tmp, ptr, sizeof(tmp) - 1); |
b765a372 JA |
209 | |
210 | p1 = strchr(tmp, '-'); | |
e1f36503 JA |
211 | if (!p1) { |
212 | ret = 1; | |
213 | break; | |
214 | } | |
215 | ||
216 | p2 = p1 + 1; | |
217 | *p1 = '\0'; | |
b765a372 | 218 | p1 = tmp; |
e1f36503 JA |
219 | |
220 | ret = 1; | |
221 | if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) { | |
222 | ret = 0; | |
e1f36503 | 223 | if (ul1 > ul2) { |
f90eff5a JA |
224 | unsigned long foo = ul1; |
225 | ||
226 | ul1 = ul2; | |
227 | ul2 = foo; | |
228 | } | |
229 | ||
230 | if (first) { | |
231 | ilp1 = td_var(data, o->off1); | |
232 | ilp2 = td_var(data, o->off2); | |
233 | *ilp1 = ul1; | |
75e6f36f | 234 | *ilp2 = ul2; |
f90eff5a JA |
235 | if (o->off3 && o->off4) { |
236 | ilp1 = td_var(data, o->off3); | |
237 | ilp2 = td_var(data, o->off4); | |
238 | *ilp1 = ul1; | |
239 | *ilp2 = ul2; | |
240 | } | |
241 | } else if (o->off3 && o->off4) { | |
242 | ilp1 = td_var(data, o->off3); | |
243 | ilp2 = td_var(data, o->off4); | |
75e6f36f | 244 | *ilp1 = ul1; |
f90eff5a | 245 | *ilp2 = ul2; |
e1f36503 JA |
246 | } |
247 | } | |
248 | ||
249 | break; | |
250 | } | |
251 | case FIO_OPT_INT: { | |
252 | fio_opt_int_fn *fn = o->cb; | |
253 | ||
254 | ret = check_int(ptr, &il); | |
255 | if (ret) | |
256 | break; | |
257 | ||
258 | if (o->max_val && il > o->max_val) | |
259 | il = o->max_val; | |
260 | ||
261 | if (fn) | |
262 | ret = fn(data, &il); | |
263 | else { | |
f90eff5a JA |
264 | if (first || !o->off2) |
265 | ilp1 = td_var(data, o->off1); | |
266 | else | |
267 | ilp1 = td_var(data, o->off2); | |
268 | ||
75e6f36f | 269 | *ilp1 = il; |
e1f36503 JA |
270 | } |
271 | break; | |
272 | } | |
273 | case FIO_OPT_STR_SET: { | |
274 | fio_opt_str_set_fn *fn = o->cb; | |
275 | ||
276 | if (fn) | |
277 | ret = fn(data); | |
278 | else { | |
f90eff5a JA |
279 | if (first || !o->off2) |
280 | ilp1 = td_var(data, o->off1); | |
281 | else | |
282 | ilp1 = td_var(data, o->off2); | |
283 | ||
75e6f36f | 284 | *ilp1 = 1; |
e1f36503 JA |
285 | } |
286 | break; | |
287 | } | |
288 | default: | |
289 | fprintf(stderr, "Bad option type %d\n", o->type); | |
290 | ret = 1; | |
291 | } | |
cb2c86fd | 292 | |
e1f36503 | 293 | return ret; |
cb2c86fd JA |
294 | } |
295 | ||
f90eff5a JA |
296 | static int handle_option(struct fio_option *o, const char *ptr, void *data) |
297 | { | |
298 | const char *ptr2; | |
299 | int ret; | |
300 | ||
301 | ret = __handle_option(o, ptr, data, 1); | |
302 | if (ret) | |
303 | return ret; | |
304 | ||
305 | /* | |
306 | * See if we have a second set of parameters, hidden after a comma | |
307 | */ | |
308 | ptr2 = strchr(ptr, ','); | |
309 | if (!ptr2) | |
310 | return 0; | |
311 | ||
312 | ptr2++; | |
313 | return __handle_option(o, ptr2, data, 0); | |
314 | } | |
315 | ||
b4692828 JA |
316 | int parse_cmd_option(const char *opt, const char *val, |
317 | struct fio_option *options, void *data) | |
318 | { | |
319 | struct fio_option *o; | |
320 | ||
321 | o = find_option(options, opt); | |
322 | if (!o) { | |
323 | fprintf(stderr, "Bad option %s\n", opt); | |
324 | return 1; | |
325 | } | |
326 | ||
b1508cf9 JA |
327 | if (!handle_option(o, val, data)) |
328 | return 0; | |
329 | ||
330 | fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val); | |
331 | return 1; | |
b4692828 JA |
332 | } |
333 | ||
e1f36503 | 334 | int parse_option(const char *opt, struct fio_option *options, void *data) |
cb2c86fd | 335 | { |
b4692828 | 336 | struct fio_option *o; |
e1f36503 JA |
337 | char *pre, *post; |
338 | char tmp[64]; | |
339 | ||
0bbab0e7 | 340 | strncpy(tmp, opt, sizeof(tmp) - 1); |
e1f36503 JA |
341 | |
342 | pre = strchr(tmp, '='); | |
343 | if (pre) { | |
344 | post = pre; | |
345 | *pre = '\0'; | |
346 | pre = tmp; | |
347 | post++; | |
348 | o = find_option(options, pre); | |
349 | } else { | |
350 | o = find_option(options, tmp); | |
351 | post = NULL; | |
352 | } | |
cb2c86fd | 353 | |
e1f36503 JA |
354 | if (!o) { |
355 | fprintf(stderr, "Bad option %s\n", tmp); | |
356 | return 1; | |
357 | } | |
358 | ||
b1508cf9 JA |
359 | if (!handle_option(o, post, data)) |
360 | return 0; | |
361 | ||
362 | fprintf(stderr, "fio: failed parsing %s\n", opt); | |
363 | return 1; | |
e1f36503 | 364 | } |