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