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