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