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