'1' is minimum value for queue depth, don't allow 0
[fio.git] / parse.c
CommitLineData
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
f085737f
JA
14static int vp_cmp(const void *p1, const void *p2)
15{
16 const struct value_pair *vp1 = p1;
17 const struct value_pair *vp2 = p2;
18
19 return strlen(vp2->ival) - strlen(vp1->ival);
20}
21
22static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
23{
24 const struct value_pair *vp;
25 int entries;
26
27 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
28
29 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
30 vp = &o->posval[entries];
31 if (!vp->ival || vp->ival[0] == '\0')
32 break;
33
34 memcpy(&vpmap[entries], vp, sizeof(*vp));
35 }
36
37 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
38}
39
b1ec1da6
JA
40static void show_option_range(struct fio_option *o)
41{
42 if (!o->minval && !o->maxval)
43 return;
44
ec1aee01 45 printf("%20s: min=%d, max=%d\n", "range", o->minval, o->maxval);
b1ec1da6
JA
46}
47
48static void show_option_values(struct fio_option *o)
49{
b1ec1da6
JA
50 int i = 0;
51
52 do {
7837213b 53 const struct value_pair *vp = &o->posval[i];
b1ec1da6 54
7837213b
JA
55 if (!vp->ival)
56 break;
b1ec1da6 57
7837213b
JA
58 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
59 if (vp->help)
60 printf(" %s", vp->help);
61 printf("\n");
b1ec1da6
JA
62 i++;
63 } while (i < PARSE_MAX_VP);
64
65 if (i)
66 printf("\n");
67}
68
cb2c86fd
JA
69static unsigned long get_mult_time(char c)
70{
71 switch (c) {
72 case 'm':
73 case 'M':
74 return 60;
75 case 'h':
76 case 'H':
77 return 60 * 60;
78 case 'd':
79 case 'D':
80 return 24 * 60 * 60;
81 default:
82 return 1;
83 }
84}
85
86static unsigned long get_mult_bytes(char c)
87{
88 switch (c) {
89 case 'k':
90 case 'K':
91 return 1024;
92 case 'm':
93 case 'M':
94 return 1024 * 1024;
95 case 'g':
96 case 'G':
97 return 1024 * 1024 * 1024;
f3502ba2
JA
98 case 'e':
99 case 'E':
100 return 1024 * 1024 * 1024 * 1024UL;
cb2c86fd
JA
101 default:
102 return 1;
103 }
104}
105
106/*
e1f36503 107 * convert string into decimal value, noting any size suffix
cb2c86fd 108 */
63f29372 109static int str_to_decimal(const char *str, long long *val, int kilo)
cb2c86fd 110{
cb2c86fd
JA
111 int len;
112
cb2c86fd 113 len = strlen(str);
f90eff5a
JA
114 if (!len)
115 return 1;
cb2c86fd 116
675de85a 117 *val = strtoll(str, NULL, 10);
cda866ca 118 if (*val == LONG_MAX && errno == ERANGE)
cb2c86fd
JA
119 return 1;
120
121 if (kilo)
122 *val *= get_mult_bytes(str[len - 1]);
123 else
124 *val *= get_mult_time(str[len - 1]);
787f7e95 125
cb2c86fd
JA
126 return 0;
127}
128
63f29372 129static int check_str_bytes(const char *p, long long *val)
cb2c86fd 130{
cb2c86fd
JA
131 return str_to_decimal(p, val, 1);
132}
133
63f29372 134static int check_str_time(const char *p, long long *val)
cb2c86fd 135{
cb2c86fd
JA
136 return str_to_decimal(p, val, 0);
137}
138
139void strip_blank_front(char **p)
140{
141 char *s = *p;
142
143 while (isspace(*s))
144 s++;
4d651dad
JA
145
146 *p = s;
cb2c86fd
JA
147}
148
149void strip_blank_end(char *p)
150{
523bfadb
JA
151 char *s;
152
153 s = strchr(p, ';');
154 if (s)
155 *s = '\0';
156 s = strchr(p, '#');
157 if (s)
158 *s = '\0';
159 if (s)
160 p = s;
161
7f7e6e59
JA
162 s = p + strlen(p);
163 while ((isspace(*s) || iscntrl(*s)) && (s > p))
cb2c86fd
JA
164 s--;
165
166 *(s + 1) = '\0';
167}
168
63f29372 169static int check_range_bytes(const char *str, long *val)
cb2c86fd
JA
170{
171 char suffix;
172
787f7e95
JA
173 if (!strlen(str))
174 return 1;
175
cb2c86fd
JA
176 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
177 *val *= get_mult_bytes(suffix);
178 return 0;
179 }
180
181 if (sscanf(str, "%lu", val) == 1)
182 return 0;
183
184 return 1;
185}
186
63f29372 187static int check_int(const char *p, int *val)
cb2c86fd 188{
787f7e95
JA
189 if (!strlen(p))
190 return 1;
d78ee463 191 if (strstr(p, "0x") || strstr(p, "0X")) {
a61bdfd8
JA
192 if (sscanf(p, "%x", val) == 1)
193 return 0;
194 } else {
195 if (sscanf(p, "%u", val) == 1)
196 return 0;
197 }
cb2c86fd 198
e1f36503
JA
199 return 1;
200}
cb2c86fd 201
e1f36503
JA
202static struct fio_option *find_option(struct fio_option *options,
203 const char *opt)
204{
4945ba12 205 struct fio_option *o;
cb2c86fd 206
4945ba12 207 for (o = &options[0]; o->name; o++) {
e1f36503
JA
208 if (!strcmp(o->name, opt))
209 return o;
03b74b3e
JA
210 else if (o->alias && !strcmp(o->alias, opt))
211 return o;
33963c6c 212 }
cb2c86fd 213
e1f36503 214 return NULL;
cb2c86fd
JA
215}
216
17abbe89
JA
217#define val_store(ptr, val, off, data) \
218 do { \
219 ptr = td_var((data), (off)); \
220 *ptr = (val); \
221 } while (0)
222
f90eff5a 223static int __handle_option(struct fio_option *o, const char *ptr, void *data,
787f7e95 224 int first, int more)
cb2c86fd 225{
63f29372
JA
226 int il, *ilp;
227 long long ull, *ullp;
228 long ul1, ul2;
b4692828 229 char **cp;
e1f36503 230 int ret = 0, is_time = 0;
cb2c86fd 231
08e26e35
JA
232 if (!ptr && o->type != FIO_OPT_STR_SET) {
233 fprintf(stderr, "Option %s requires an argument\n", o->name);
234 return 1;
235 }
236
e1f36503
JA
237 switch (o->type) {
238 case FIO_OPT_STR: {
239 fio_opt_str_fn *fn = o->cb;
b1ec1da6 240 const struct value_pair *vp;
f085737f 241 struct value_pair posval[PARSE_MAX_VP];
b1ec1da6
JA
242 int i;
243
f085737f
JA
244 posval_sort(o, posval);
245
b1ec1da6 246 for (i = 0; i < PARSE_MAX_VP; i++) {
f085737f 247 vp = &posval[i];
b1ec1da6
JA
248 if (!vp->ival || vp->ival[0] == '\0')
249 break;
6612a27b 250 ret = 1;
b1ec1da6
JA
251 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
252 ret = 0;
253 if (!o->off1)
254 break;
255 val_store(ilp, vp->oval, o->off1, data);
256 break;
257 }
258 }
cb2c86fd 259
b1ec1da6
JA
260 if (ret)
261 show_option_values(o);
262 else if (fn)
263 ret = fn(data, ptr);
e1f36503
JA
264 break;
265 }
266 case FIO_OPT_STR_VAL_TIME:
267 is_time = 1;
75e6f36f
JA
268 case FIO_OPT_STR_VAL:
269 case FIO_OPT_STR_VAL_INT: {
e1f36503
JA
270 fio_opt_str_val_fn *fn = o->cb;
271
272 if (is_time)
273 ret = check_str_time(ptr, &ull);
274 else
275 ret = check_str_bytes(ptr, &ull);
276
277 if (ret)
278 break;
279
db8e0165
JA
280 if (o->maxval && ull > o->maxval) {
281 fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval);
282 return 1;
283 }
284 if (o->minval && ull < o->minval) {
285 fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval);
286 return 1;
287 }
e1f36503
JA
288
289 if (fn)
290 ret = fn(data, &ull);
291 else {
75e6f36f 292 if (o->type == FIO_OPT_STR_VAL_INT) {
17abbe89
JA
293 if (first)
294 val_store(ilp, ull, o->off1, data);
787f7e95 295 if (!more && o->off2)
17abbe89 296 val_store(ilp, ull, o->off2, data);
75e6f36f 297 } else {
17abbe89
JA
298 if (first)
299 val_store(ullp, ull, o->off1, data);
787f7e95 300 if (!more && o->off2)
17abbe89 301 val_store(ullp, ull, o->off2, data);
75e6f36f 302 }
e1f36503
JA
303 }
304 break;
305 }
af52b345
JA
306 case FIO_OPT_STR_STORE: {
307 fio_opt_str_fn *fn = o->cb;
308
e1f36503
JA
309 cp = td_var(data, o->off1);
310 *cp = strdup(ptr);
af52b345
JA
311 if (fn) {
312 ret = fn(data, ptr);
313 if (ret) {
314 free(*cp);
315 *cp = NULL;
316 }
317 }
e1f36503 318 break;
af52b345 319 }
e1f36503 320 case FIO_OPT_RANGE: {
b765a372 321 char tmp[128];
e1f36503
JA
322 char *p1, *p2;
323
0bbab0e7 324 strncpy(tmp, ptr, sizeof(tmp) - 1);
b765a372
JA
325
326 p1 = strchr(tmp, '-');
e1f36503 327 if (!p1) {
0c9baf91
JA
328 p1 = strchr(tmp, ':');
329 if (!p1) {
330 ret = 1;
331 break;
332 }
e1f36503
JA
333 }
334
335 p2 = p1 + 1;
336 *p1 = '\0';
b765a372 337 p1 = tmp;
e1f36503
JA
338
339 ret = 1;
340 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
341 ret = 0;
e1f36503 342 if (ul1 > ul2) {
f90eff5a
JA
343 unsigned long foo = ul1;
344
345 ul1 = ul2;
346 ul2 = foo;
347 }
348
349 if (first) {
17abbe89
JA
350 val_store(ilp, ul1, o->off1, data);
351 val_store(ilp, ul2, o->off2, data);
352 }
787f7e95 353 if (!more && o->off3 && o->off4) {
17abbe89
JA
354 val_store(ilp, ul1, o->off3, data);
355 val_store(ilp, ul2, o->off4, data);
e1f36503 356 }
17abbe89
JA
357 }
358
e1f36503
JA
359 break;
360 }
13335ddb
JA
361 case FIO_OPT_INT:
362 case FIO_OPT_BOOL: {
e1f36503
JA
363 fio_opt_int_fn *fn = o->cb;
364
365 ret = check_int(ptr, &il);
366 if (ret)
367 break;
368
db8e0165
JA
369 if (o->maxval && il > (int) o->maxval) {
370 fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval);
371 return 1;
372 }
373 if (o->minval && il < o->minval) {
374 fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval);
375 return 1;
376 }
e1f36503 377
76a43db4
JA
378 if (o->neg)
379 il = !il;
380
e1f36503
JA
381 if (fn)
382 ret = fn(data, &il);
383 else {
17abbe89
JA
384 if (first)
385 val_store(ilp, il, o->off1, data);
787f7e95 386 if (!more && o->off2)
17abbe89 387 val_store(ilp, il, o->off2, data);
e1f36503
JA
388 }
389 break;
390 }
391 case FIO_OPT_STR_SET: {
392 fio_opt_str_set_fn *fn = o->cb;
393
394 if (fn)
395 ret = fn(data);
396 else {
17abbe89
JA
397 if (first)
398 val_store(ilp, 1, o->off1, data);
787f7e95 399 if (!more && o->off2)
17abbe89 400 val_store(ilp, 1, o->off2, data);
e1f36503
JA
401 }
402 break;
403 }
404 default:
1e97cce9 405 fprintf(stderr, "Bad option type %u\n", o->type);
e1f36503
JA
406 ret = 1;
407 }
cb2c86fd 408
e1f36503 409 return ret;
cb2c86fd
JA
410}
411
f90eff5a
JA
412static int handle_option(struct fio_option *o, const char *ptr, void *data)
413{
92b586f8 414 const char *ptr2 = NULL;
787f7e95 415 int r1, r2;
f90eff5a
JA
416
417 /*
787f7e95
JA
418 * See if we have a second set of parameters, hidden after a comma.
419 * Do this before parsing the first round, to check if we should
420 * copy set 1 options to set 2.
f90eff5a 421 */
ad231bc4
JB
422 if (ptr &&
423 (o->type != FIO_OPT_STR_STORE) &&
424 (o->type != FIO_OPT_STR)) {
92b586f8 425 ptr2 = strchr(ptr, ',');
0c9baf91
JA
426 if (!ptr2)
427 ptr2 = strchr(ptr, ':');
b486f76a
JA
428 if (!ptr2)
429 ptr2 = strchr(ptr, '-');
0c9baf91 430 }
787f7e95
JA
431
432 /*
433 * Don't return early if parsing the first option fails - if
434 * we are doing multiple arguments, we can allow the first one
435 * being empty.
436 */
437 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
438
f90eff5a 439 if (!ptr2)
787f7e95 440 return r1;
f90eff5a
JA
441
442 ptr2++;
787f7e95
JA
443 r2 = __handle_option(o, ptr2, data, 0, 0);
444
445 return r1 && r2;
f90eff5a
JA
446}
447
b4692828
JA
448int parse_cmd_option(const char *opt, const char *val,
449 struct fio_option *options, void *data)
450{
451 struct fio_option *o;
452
453 o = find_option(options, opt);
454 if (!o) {
455 fprintf(stderr, "Bad option %s\n", opt);
456 return 1;
457 }
458
b1508cf9
JA
459 if (!handle_option(o, val, data))
460 return 0;
461
462 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
463 return 1;
b4692828
JA
464}
465
e1f36503 466int parse_option(const char *opt, struct fio_option *options, void *data)
cb2c86fd 467{
b4692828 468 struct fio_option *o;
e1f36503 469 char *pre, *post;
0401bca6 470 char *tmp;
e1f36503 471
0401bca6 472 tmp = strdup(opt);
e1f36503
JA
473
474 pre = strchr(tmp, '=');
475 if (pre) {
476 post = pre;
477 *pre = '\0';
478 pre = tmp;
479 post++;
480 o = find_option(options, pre);
481 } else {
482 o = find_option(options, tmp);
483 post = NULL;
484 }
cb2c86fd 485
e1f36503
JA
486 if (!o) {
487 fprintf(stderr, "Bad option %s\n", tmp);
0401bca6 488 free(tmp);
e1f36503
JA
489 return 1;
490 }
491
0401bca6
JA
492 if (!handle_option(o, post, data)) {
493 free(tmp);
b1508cf9 494 return 0;
0401bca6 495 }
b1508cf9
JA
496
497 fprintf(stderr, "fio: failed parsing %s\n", opt);
0401bca6 498 free(tmp);
b1508cf9 499 return 1;
e1f36503 500}
fd28ca49 501
0e9f7fac
JA
502/*
503 * Option match, levenshtein distance. Handy for not quite remembering what
504 * the option name is.
505 */
506static int string_distance(const char *s1, const char *s2)
507{
508 unsigned int s1_len = strlen(s1);
509 unsigned int s2_len = strlen(s2);
510 unsigned int *p, *q, *r;
511 unsigned int i, j;
512
513 p = malloc(sizeof(unsigned int) * (s2_len + 1));
514 q = malloc(sizeof(unsigned int) * (s2_len + 1));
515
516 p[0] = 0;
517 for (i = 1; i <= s2_len; i++)
518 p[i] = p[i - 1] + 1;
519
520 for (i = 1; i <= s1_len; i++) {
521 q[0] = p[0] + 1;
522 for (j = 1; j <= s2_len; j++) {
523 unsigned int sub = p[j - 1];
524
525 if (s1[i - 1] != s2[j - 1])
526 sub++;
527
528 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
529 }
530 r = p;
531 p = q;
532 q = r;
533 }
534
535 i = p[s2_len];
536 free(p);
537 free(q);
538 return i;
539}
540
541static void show_option_help(struct fio_option *o)
fd28ca49 542{
fd28ca49
JA
543 const char *typehelp[] = {
544 "string (opt=bla)",
545 "string with possible k/m/g postfix (opt=4k)",
546 "string with range and postfix (opt=1k-4k)",
547 "string with time postfix (opt=10s)",
548 "string (opt=bla)",
549 "string with dual range (opt=1k-4k,4k-8k)",
550 "integer value (opt=100)",
13335ddb 551 "boolean value (opt=1)",
fd28ca49
JA
552 "no argument (opt)",
553 };
0e9f7fac 554
d2bb7fea
JA
555 if (o->alias)
556 printf("%20s: %s\n", "alias", o->alias);
557
0e9f7fac
JA
558 printf("%20s: %s\n", "type", typehelp[o->type]);
559 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
560 show_option_range(o);
561 show_option_values(o);
562}
563
afdf9352
JA
564static struct fio_option *find_child(struct fio_option *options,
565 struct fio_option *o)
566{
567 struct fio_option *__o;
568
fdf28744
JA
569 for (__o = options + 1; __o->name; __o++)
570 if (__o->parent && !strcmp(__o->parent, o->name))
afdf9352
JA
571 return __o;
572
573 return NULL;
574}
575
fdf28744
JA
576static void print_option(struct fio_option *o, struct fio_option *org,
577 int level)
afdf9352
JA
578{
579 char name[256], *p;
afdf9352
JA
580
581 if (!o)
582 return;
ef9aff52
JA
583 if (!org)
584 org = o;
afdf9352
JA
585
586 p = name;
ef9aff52 587 if (level)
afdf9352
JA
588 p += sprintf(p, "%s", " ");
589
590 sprintf(p, "%s", o->name);
591
592 printf("%-24s: %s\n", name, o->help);
fdf28744 593 print_option(find_child(o, org), org, level + 1);
afdf9352
JA
594}
595
0e9f7fac
JA
596int show_cmd_help(struct fio_option *options, const char *name)
597{
598 struct fio_option *o, *closest;
599 unsigned int best_dist;
29fc6afe 600 int found = 0;
320beefe
JA
601 int show_all = 0;
602
603 if (!name || !strcmp(name, "all"))
604 show_all = 1;
fd28ca49 605
0e9f7fac
JA
606 closest = NULL;
607 best_dist = -1;
4945ba12 608 for (o = &options[0]; o->name; o++) {
320beefe
JA
609 int match = 0;
610
0e9f7fac 611 if (name) {
7f9348f8
JA
612 if (!strcmp(name, o->name) ||
613 (o->alias && !strcmp(name, o->alias)))
0e9f7fac
JA
614 match = 1;
615 else {
616 unsigned int dist;
617
618 dist = string_distance(name, o->name);
619 if (dist < best_dist) {
620 best_dist = dist;
621 closest = o;
622 }
623 }
624 }
fd28ca49
JA
625
626 if (show_all || match) {
29fc6afe 627 found = 1;
c167dedc 628 if (match)
afdf9352 629 printf("%24s: %s\n", o->name, o->help);
c167dedc 630 if (show_all) {
afdf9352 631 if (!o->parent)
fdf28744 632 print_option(o, NULL, 0);
4945ba12 633 continue;
c167dedc 634 }
fd28ca49
JA
635 }
636
70df2f19
JA
637 if (!match)
638 continue;
639
0e9f7fac 640 show_option_help(o);
fd28ca49
JA
641 }
642
29fc6afe
JA
643 if (found)
644 return 0;
fd28ca49 645
0e9f7fac
JA
646 printf("No such command: %s", name);
647 if (closest) {
648 printf(" - showing closest match\n");
649 printf("%20s: %s\n", closest->name, closest->help);
650 show_option_help(closest);
651 } else
652 printf("\n");
653
29fc6afe 654 return 1;
fd28ca49 655}
ee738499 656
13335ddb
JA
657/*
658 * Handle parsing of default parameters.
659 */
ee738499
JA
660void fill_default_options(void *data, struct fio_option *options)
661{
4945ba12 662 struct fio_option *o;
ee738499 663
4945ba12 664 for (o = &options[0]; o->name; o++)
ee738499
JA
665 if (o->def)
666 handle_option(o, o->def, data);
ee738499 667}
13335ddb
JA
668
669/*
670 * Sanitize the options structure. For now it just sets min/max for bool
5b0a8880 671 * values and whether both callback and offsets are given.
13335ddb
JA
672 */
673void options_init(struct fio_option *options)
674{
4945ba12 675 struct fio_option *o;
13335ddb 676
4945ba12 677 for (o = &options[0]; o->name; o++) {
13335ddb
JA
678 if (o->type == FIO_OPT_BOOL) {
679 o->minval = 0;
680 o->maxval = 1;
681 }
0f3e35ef
JA
682 if (o->type == FIO_OPT_STR_SET && o->def)
683 fprintf(stderr, "Option %s: string set option with default will always be true\n", o->name);
b1ec1da6
JA
684 if (!o->cb && !o->off1)
685 fprintf(stderr, "Option %s: neither cb nor offset given\n", o->name);
af52b345 686 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
b1ec1da6 687 continue;
5b0a8880
JA
688 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
689 fprintf(stderr, "Option %s: both cb and offset given\n", o->name);
13335ddb
JA
690 }
691}