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