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