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