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