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