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