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 | ||
b1ec1da6 JA |
14 | static void show_option_range(struct fio_option *o) |
15 | { | |
16 | if (!o->minval && !o->maxval) | |
17 | return; | |
18 | ||
ec1aee01 | 19 | printf("%20s: min=%d, max=%d\n", "range", o->minval, o->maxval); |
b1ec1da6 JA |
20 | } |
21 | ||
22 | static void show_option_values(struct fio_option *o) | |
23 | { | |
24 | const char *msg; | |
25 | int i = 0; | |
26 | ||
27 | do { | |
28 | msg = o->posval[i].ival; | |
29 | if (!msg) | |
30 | break; | |
31 | ||
32 | if (!i) | |
ec1aee01 | 33 | printf("%20s: ", "valid values"); |
b1ec1da6 JA |
34 | |
35 | printf("%s,", msg); | |
36 | i++; | |
37 | } while (i < PARSE_MAX_VP); | |
38 | ||
39 | if (i) | |
40 | printf("\n"); | |
41 | } | |
42 | ||
cb2c86fd JA |
43 | static unsigned long get_mult_time(char c) |
44 | { | |
45 | switch (c) { | |
46 | case 'm': | |
47 | case 'M': | |
48 | return 60; | |
49 | case 'h': | |
50 | case 'H': | |
51 | return 60 * 60; | |
52 | case 'd': | |
53 | case 'D': | |
54 | return 24 * 60 * 60; | |
55 | default: | |
56 | return 1; | |
57 | } | |
58 | } | |
59 | ||
60 | static unsigned long get_mult_bytes(char c) | |
61 | { | |
62 | switch (c) { | |
63 | case 'k': | |
64 | case 'K': | |
65 | return 1024; | |
66 | case 'm': | |
67 | case 'M': | |
68 | return 1024 * 1024; | |
69 | case 'g': | |
70 | case 'G': | |
71 | return 1024 * 1024 * 1024; | |
f3502ba2 JA |
72 | case 'e': |
73 | case 'E': | |
74 | return 1024 * 1024 * 1024 * 1024UL; | |
cb2c86fd JA |
75 | default: |
76 | return 1; | |
77 | } | |
78 | } | |
79 | ||
80 | /* | |
e1f36503 | 81 | * convert string into decimal value, noting any size suffix |
cb2c86fd | 82 | */ |
63f29372 | 83 | static int str_to_decimal(const char *str, long long *val, int kilo) |
cb2c86fd | 84 | { |
cb2c86fd JA |
85 | int len; |
86 | ||
cb2c86fd | 87 | len = strlen(str); |
f90eff5a JA |
88 | if (!len) |
89 | return 1; | |
cb2c86fd | 90 | |
675de85a | 91 | *val = strtoll(str, NULL, 10); |
cda866ca | 92 | if (*val == LONG_MAX && errno == ERANGE) |
cb2c86fd JA |
93 | return 1; |
94 | ||
95 | if (kilo) | |
96 | *val *= get_mult_bytes(str[len - 1]); | |
97 | else | |
98 | *val *= get_mult_time(str[len - 1]); | |
787f7e95 | 99 | |
cb2c86fd JA |
100 | return 0; |
101 | } | |
102 | ||
63f29372 | 103 | static int check_str_bytes(const char *p, long long *val) |
cb2c86fd | 104 | { |
cb2c86fd JA |
105 | return str_to_decimal(p, val, 1); |
106 | } | |
107 | ||
63f29372 | 108 | static int check_str_time(const char *p, long long *val) |
cb2c86fd | 109 | { |
cb2c86fd JA |
110 | return str_to_decimal(p, val, 0); |
111 | } | |
112 | ||
113 | void strip_blank_front(char **p) | |
114 | { | |
115 | char *s = *p; | |
116 | ||
117 | while (isspace(*s)) | |
118 | s++; | |
119 | } | |
120 | ||
121 | void strip_blank_end(char *p) | |
122 | { | |
123 | char *s = p + strlen(p) - 1; | |
124 | ||
125 | while (isspace(*s) || iscntrl(*s)) | |
126 | s--; | |
127 | ||
128 | *(s + 1) = '\0'; | |
129 | } | |
130 | ||
63f29372 | 131 | static int check_range_bytes(const char *str, long *val) |
cb2c86fd JA |
132 | { |
133 | char suffix; | |
134 | ||
787f7e95 JA |
135 | if (!strlen(str)) |
136 | return 1; | |
137 | ||
cb2c86fd JA |
138 | if (sscanf(str, "%lu%c", val, &suffix) == 2) { |
139 | *val *= get_mult_bytes(suffix); | |
140 | return 0; | |
141 | } | |
142 | ||
143 | if (sscanf(str, "%lu", val) == 1) | |
144 | return 0; | |
145 | ||
146 | return 1; | |
147 | } | |
148 | ||
63f29372 | 149 | static int check_int(const char *p, int *val) |
cb2c86fd | 150 | { |
787f7e95 JA |
151 | if (!strlen(p)) |
152 | return 1; | |
e1f36503 JA |
153 | if (sscanf(p, "%u", val) == 1) |
154 | return 0; | |
cb2c86fd | 155 | |
e1f36503 JA |
156 | return 1; |
157 | } | |
cb2c86fd | 158 | |
e1f36503 JA |
159 | static struct fio_option *find_option(struct fio_option *options, |
160 | const char *opt) | |
161 | { | |
4945ba12 | 162 | struct fio_option *o; |
cb2c86fd | 163 | |
4945ba12 | 164 | for (o = &options[0]; o->name; o++) { |
e1f36503 JA |
165 | if (!strcmp(o->name, opt)) |
166 | return o; | |
03b74b3e JA |
167 | else if (o->alias && !strcmp(o->alias, opt)) |
168 | return o; | |
33963c6c | 169 | } |
cb2c86fd | 170 | |
e1f36503 | 171 | return NULL; |
cb2c86fd JA |
172 | } |
173 | ||
17abbe89 JA |
174 | #define val_store(ptr, val, off, data) \ |
175 | do { \ | |
176 | ptr = td_var((data), (off)); \ | |
177 | *ptr = (val); \ | |
178 | } while (0) | |
179 | ||
f90eff5a | 180 | static int __handle_option(struct fio_option *o, const char *ptr, void *data, |
787f7e95 | 181 | int first, int more) |
cb2c86fd | 182 | { |
63f29372 JA |
183 | int il, *ilp; |
184 | long long ull, *ullp; | |
185 | long ul1, ul2; | |
b4692828 | 186 | char **cp; |
e1f36503 | 187 | int ret = 0, is_time = 0; |
cb2c86fd | 188 | |
08e26e35 JA |
189 | if (!ptr && o->type != FIO_OPT_STR_SET) { |
190 | fprintf(stderr, "Option %s requires an argument\n", o->name); | |
191 | return 1; | |
192 | } | |
193 | ||
e1f36503 JA |
194 | switch (o->type) { |
195 | case FIO_OPT_STR: { | |
196 | fio_opt_str_fn *fn = o->cb; | |
b1ec1da6 JA |
197 | const struct value_pair *vp; |
198 | int i; | |
199 | ||
71dc58b8 JB |
200 | if (!first) |
201 | break; | |
202 | ||
b1ec1da6 JA |
203 | ret = 1; |
204 | for (i = 0; i < PARSE_MAX_VP; i++) { | |
205 | vp = &o->posval[i]; | |
206 | if (!vp->ival || vp->ival[0] == '\0') | |
207 | break; | |
208 | if (!strncmp(vp->ival, ptr, strlen(vp->ival))) { | |
209 | ret = 0; | |
210 | if (!o->off1) | |
211 | break; | |
212 | val_store(ilp, vp->oval, o->off1, data); | |
213 | break; | |
214 | } | |
215 | } | |
cb2c86fd | 216 | |
b1ec1da6 JA |
217 | if (ret) |
218 | show_option_values(o); | |
219 | else if (fn) | |
220 | ret = fn(data, ptr); | |
e1f36503 JA |
221 | break; |
222 | } | |
223 | case FIO_OPT_STR_VAL_TIME: | |
224 | is_time = 1; | |
75e6f36f JA |
225 | case FIO_OPT_STR_VAL: |
226 | case FIO_OPT_STR_VAL_INT: { | |
e1f36503 JA |
227 | fio_opt_str_val_fn *fn = o->cb; |
228 | ||
229 | if (is_time) | |
230 | ret = check_str_time(ptr, &ull); | |
231 | else | |
232 | ret = check_str_bytes(ptr, &ull); | |
233 | ||
234 | if (ret) | |
235 | break; | |
236 | ||
db8e0165 JA |
237 | if (o->maxval && ull > o->maxval) { |
238 | fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval); | |
239 | return 1; | |
240 | } | |
241 | if (o->minval && ull < o->minval) { | |
242 | fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval); | |
243 | return 1; | |
244 | } | |
e1f36503 JA |
245 | |
246 | if (fn) | |
247 | ret = fn(data, &ull); | |
248 | else { | |
75e6f36f | 249 | if (o->type == FIO_OPT_STR_VAL_INT) { |
17abbe89 JA |
250 | if (first) |
251 | val_store(ilp, ull, o->off1, data); | |
787f7e95 | 252 | if (!more && o->off2) |
17abbe89 | 253 | val_store(ilp, ull, o->off2, data); |
75e6f36f | 254 | } else { |
17abbe89 JA |
255 | if (first) |
256 | val_store(ullp, ull, o->off1, data); | |
787f7e95 | 257 | if (!more && o->off2) |
17abbe89 | 258 | val_store(ullp, ull, o->off2, data); |
75e6f36f | 259 | } |
e1f36503 JA |
260 | } |
261 | break; | |
262 | } | |
263 | case FIO_OPT_STR_STORE: | |
264 | cp = td_var(data, o->off1); | |
265 | *cp = strdup(ptr); | |
266 | break; | |
267 | case FIO_OPT_RANGE: { | |
b765a372 | 268 | char tmp[128]; |
e1f36503 JA |
269 | char *p1, *p2; |
270 | ||
0bbab0e7 | 271 | strncpy(tmp, ptr, sizeof(tmp) - 1); |
b765a372 JA |
272 | |
273 | p1 = strchr(tmp, '-'); | |
e1f36503 | 274 | if (!p1) { |
0c9baf91 JA |
275 | p1 = strchr(tmp, ':'); |
276 | if (!p1) { | |
277 | ret = 1; | |
278 | break; | |
279 | } | |
e1f36503 JA |
280 | } |
281 | ||
282 | p2 = p1 + 1; | |
283 | *p1 = '\0'; | |
b765a372 | 284 | p1 = tmp; |
e1f36503 JA |
285 | |
286 | ret = 1; | |
287 | if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) { | |
288 | ret = 0; | |
e1f36503 | 289 | if (ul1 > ul2) { |
f90eff5a JA |
290 | unsigned long foo = ul1; |
291 | ||
292 | ul1 = ul2; | |
293 | ul2 = foo; | |
294 | } | |
295 | ||
296 | if (first) { | |
17abbe89 JA |
297 | val_store(ilp, ul1, o->off1, data); |
298 | val_store(ilp, ul2, o->off2, data); | |
299 | } | |
787f7e95 | 300 | if (!more && o->off3 && o->off4) { |
17abbe89 JA |
301 | val_store(ilp, ul1, o->off3, data); |
302 | val_store(ilp, ul2, o->off4, data); | |
e1f36503 | 303 | } |
17abbe89 JA |
304 | } |
305 | ||
e1f36503 JA |
306 | break; |
307 | } | |
13335ddb JA |
308 | case FIO_OPT_INT: |
309 | case FIO_OPT_BOOL: { | |
e1f36503 JA |
310 | fio_opt_int_fn *fn = o->cb; |
311 | ||
312 | ret = check_int(ptr, &il); | |
313 | if (ret) | |
314 | break; | |
315 | ||
db8e0165 JA |
316 | if (o->maxval && il > (int) o->maxval) { |
317 | fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval); | |
318 | return 1; | |
319 | } | |
320 | if (o->minval && il < o->minval) { | |
321 | fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval); | |
322 | return 1; | |
323 | } | |
e1f36503 | 324 | |
76a43db4 JA |
325 | if (o->neg) |
326 | il = !il; | |
327 | ||
e1f36503 JA |
328 | if (fn) |
329 | ret = fn(data, &il); | |
330 | else { | |
17abbe89 JA |
331 | if (first) |
332 | val_store(ilp, il, o->off1, data); | |
787f7e95 | 333 | if (!more && o->off2) |
17abbe89 | 334 | val_store(ilp, il, o->off2, data); |
e1f36503 JA |
335 | } |
336 | break; | |
337 | } | |
338 | case FIO_OPT_STR_SET: { | |
339 | fio_opt_str_set_fn *fn = o->cb; | |
340 | ||
341 | if (fn) | |
342 | ret = fn(data); | |
343 | else { | |
17abbe89 JA |
344 | if (first) |
345 | val_store(ilp, 1, o->off1, data); | |
787f7e95 | 346 | if (!more && o->off2) |
17abbe89 | 347 | val_store(ilp, 1, o->off2, data); |
e1f36503 JA |
348 | } |
349 | break; | |
350 | } | |
351 | default: | |
1e97cce9 | 352 | fprintf(stderr, "Bad option type %u\n", o->type); |
e1f36503 JA |
353 | ret = 1; |
354 | } | |
cb2c86fd | 355 | |
e1f36503 | 356 | return ret; |
cb2c86fd JA |
357 | } |
358 | ||
f90eff5a JA |
359 | static int handle_option(struct fio_option *o, const char *ptr, void *data) |
360 | { | |
92b586f8 | 361 | const char *ptr2 = NULL; |
787f7e95 | 362 | int r1, r2; |
f90eff5a JA |
363 | |
364 | /* | |
787f7e95 JA |
365 | * See if we have a second set of parameters, hidden after a comma. |
366 | * Do this before parsing the first round, to check if we should | |
367 | * copy set 1 options to set 2. | |
f90eff5a | 368 | */ |
ed92ac0c | 369 | if (ptr && (o->type != FIO_OPT_STR_STORE)) { |
92b586f8 | 370 | ptr2 = strchr(ptr, ','); |
0c9baf91 JA |
371 | if (!ptr2) |
372 | ptr2 = strchr(ptr, ':'); | |
373 | } | |
787f7e95 JA |
374 | |
375 | /* | |
376 | * Don't return early if parsing the first option fails - if | |
377 | * we are doing multiple arguments, we can allow the first one | |
378 | * being empty. | |
379 | */ | |
380 | r1 = __handle_option(o, ptr, data, 1, !!ptr2); | |
381 | ||
f90eff5a | 382 | if (!ptr2) |
787f7e95 | 383 | return r1; |
f90eff5a JA |
384 | |
385 | ptr2++; | |
787f7e95 JA |
386 | r2 = __handle_option(o, ptr2, data, 0, 0); |
387 | ||
388 | return r1 && r2; | |
f90eff5a JA |
389 | } |
390 | ||
b4692828 JA |
391 | int parse_cmd_option(const char *opt, const char *val, |
392 | struct fio_option *options, void *data) | |
393 | { | |
394 | struct fio_option *o; | |
395 | ||
396 | o = find_option(options, opt); | |
397 | if (!o) { | |
398 | fprintf(stderr, "Bad option %s\n", opt); | |
399 | return 1; | |
400 | } | |
401 | ||
b1508cf9 JA |
402 | if (!handle_option(o, val, data)) |
403 | return 0; | |
404 | ||
405 | fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val); | |
406 | return 1; | |
b4692828 JA |
407 | } |
408 | ||
e1f36503 | 409 | int parse_option(const char *opt, struct fio_option *options, void *data) |
cb2c86fd | 410 | { |
b4692828 | 411 | struct fio_option *o; |
e1f36503 JA |
412 | char *pre, *post; |
413 | char tmp[64]; | |
414 | ||
0bbab0e7 | 415 | strncpy(tmp, opt, sizeof(tmp) - 1); |
e1f36503 JA |
416 | |
417 | pre = strchr(tmp, '='); | |
418 | if (pre) { | |
419 | post = pre; | |
420 | *pre = '\0'; | |
421 | pre = tmp; | |
422 | post++; | |
423 | o = find_option(options, pre); | |
424 | } else { | |
425 | o = find_option(options, tmp); | |
426 | post = NULL; | |
427 | } | |
cb2c86fd | 428 | |
e1f36503 JA |
429 | if (!o) { |
430 | fprintf(stderr, "Bad option %s\n", tmp); | |
431 | return 1; | |
432 | } | |
433 | ||
b1508cf9 JA |
434 | if (!handle_option(o, post, data)) |
435 | return 0; | |
436 | ||
437 | fprintf(stderr, "fio: failed parsing %s\n", opt); | |
438 | return 1; | |
e1f36503 | 439 | } |
fd28ca49 JA |
440 | |
441 | int show_cmd_help(struct fio_option *options, const char *name) | |
442 | { | |
443 | int show_all = !strcmp(name, "all"); | |
fd28ca49 JA |
444 | const char *typehelp[] = { |
445 | "string (opt=bla)", | |
446 | "string with possible k/m/g postfix (opt=4k)", | |
447 | "string with range and postfix (opt=1k-4k)", | |
448 | "string with time postfix (opt=10s)", | |
449 | "string (opt=bla)", | |
450 | "string with dual range (opt=1k-4k,4k-8k)", | |
451 | "integer value (opt=100)", | |
13335ddb | 452 | "boolean value (opt=1)", |
fd28ca49 JA |
453 | "no argument (opt)", |
454 | }; | |
4945ba12 | 455 | struct fio_option *o; |
29fc6afe | 456 | int found = 0; |
fd28ca49 | 457 | |
4945ba12 | 458 | for (o = &options[0]; o->name; o++) { |
fd28ca49 JA |
459 | int match = !strcmp(name, o->name); |
460 | ||
461 | if (show_all || match) { | |
29fc6afe | 462 | found = 1; |
ec1aee01 | 463 | printf("%20s: %s\n", o->name, o->help); |
4945ba12 JA |
464 | if (show_all) |
465 | continue; | |
fd28ca49 JA |
466 | } |
467 | ||
70df2f19 JA |
468 | if (!match) |
469 | continue; | |
470 | ||
ec1aee01 JA |
471 | printf("%20s: %s\n", "type", typehelp[o->type]); |
472 | printf("%20s: %s\n", "default", o->def ? o->def : "no default"); | |
4945ba12 JA |
473 | show_option_range(o); |
474 | show_option_values(o); | |
fd28ca49 JA |
475 | } |
476 | ||
29fc6afe JA |
477 | if (found) |
478 | return 0; | |
fd28ca49 | 479 | |
29fc6afe JA |
480 | printf("No such command: %s\n", name); |
481 | return 1; | |
fd28ca49 | 482 | } |
ee738499 | 483 | |
13335ddb JA |
484 | /* |
485 | * Handle parsing of default parameters. | |
486 | */ | |
ee738499 JA |
487 | void fill_default_options(void *data, struct fio_option *options) |
488 | { | |
4945ba12 | 489 | struct fio_option *o; |
ee738499 | 490 | |
4945ba12 | 491 | for (o = &options[0]; o->name; o++) |
ee738499 JA |
492 | if (o->def) |
493 | handle_option(o, o->def, data); | |
ee738499 | 494 | } |
13335ddb JA |
495 | |
496 | /* | |
497 | * Sanitize the options structure. For now it just sets min/max for bool | |
5b0a8880 | 498 | * values and whether both callback and offsets are given. |
13335ddb JA |
499 | */ |
500 | void options_init(struct fio_option *options) | |
501 | { | |
4945ba12 | 502 | struct fio_option *o; |
13335ddb | 503 | |
4945ba12 | 504 | for (o = &options[0]; o->name; o++) { |
13335ddb JA |
505 | if (o->type == FIO_OPT_BOOL) { |
506 | o->minval = 0; | |
507 | o->maxval = 1; | |
508 | } | |
b1ec1da6 JA |
509 | if (!o->cb && !o->off1) |
510 | fprintf(stderr, "Option %s: neither cb nor offset given\n", o->name); | |
511 | if (o->type == FIO_OPT_STR) | |
512 | continue; | |
5b0a8880 JA |
513 | if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) |
514 | fprintf(stderr, "Option %s: both cb and offset given\n", o->name); | |
13335ddb JA |
515 | } |
516 | } |