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