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