Add a new file to gitignore
[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                 char *endptr;
377
378                 if (strstr(str, "0x") || strstr(str, "0X"))
379                         base = 16;
380                 else
381                         base = 10;
382
383                 *val = strtoll(str, &endptr, base);
384                 if (*val == 0 && endptr == str)
385                         return 1;
386                 if (*val == LONG_MAX && errno == ERANGE)
387                         return 1;
388         }
389
390         if (kilo) {
391                 unsigned long long mult;
392                 int perc = 0;
393
394                 mult = get_mult_bytes(str, len, data, &perc);
395                 if (perc)
396                         *val = -1ULL - *val;
397                 else
398                         *val *= mult;
399         } else
400                 *val *= get_mult_time(str, len, is_seconds);
401
402         return 0;
403 }
404
405 int check_str_bytes(const char *p, long long *val, void *data)
406 {
407         return str_to_decimal(p, val, 1, data, 0, 0);
408 }
409
410 int check_str_time(const char *p, long long *val, int is_seconds)
411 {
412         return str_to_decimal(p, val, 0, NULL, is_seconds, 1);
413 }
414
415 void strip_blank_front(char **p)
416 {
417         char *s = *p;
418
419         if (!strlen(s))
420                 return;
421         while (isspace((int) *s))
422                 s++;
423
424         *p = s;
425 }
426
427 void strip_blank_end(char *p)
428 {
429         char *start = p, *s;
430
431         if (!strlen(p))
432                 return;
433
434         s = strchr(p, ';');
435         if (s)
436                 *s = '\0';
437         s = strchr(p, '#');
438         if (s)
439                 *s = '\0';
440         if (s)
441                 p = s;
442
443         s = p + strlen(p);
444         while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
445                 s--;
446
447         *(s + 1) = '\0';
448 }
449
450 static int check_range_bytes(const char *str, long long *val, void *data)
451 {
452         long long __val;
453
454         if (!str_to_decimal(str, &__val, 1, data, 0, 0)) {
455                 *val = __val;
456                 return 0;
457         }
458
459         return 1;
460 }
461
462 static int check_int(const char *p, int *val)
463 {
464         if (!strlen(p))
465                 return 1;
466         if (strstr(p, "0x") || strstr(p, "0X")) {
467                 if (sscanf(p, "%x", val) == 1)
468                         return 0;
469         } else {
470                 if (sscanf(p, "%u", val) == 1)
471                         return 0;
472         }
473
474         return 1;
475 }
476
477 static size_t opt_len(const char *str)
478 {
479         char *postfix;
480
481         postfix = strchr(str, ':');
482         if (!postfix)
483                 return strlen(str);
484
485         return (int)(postfix - str);
486 }
487
488 static int str_match_len(const struct value_pair *vp, const char *str)
489 {
490         return max(strlen(vp->ival), opt_len(str));
491 }
492
493 #define val_store(ptr, val, off, or, data, o)           \
494         do {                                            \
495                 ptr = td_var((data), (o), (off));       \
496                 if ((or))                               \
497                         *ptr |= (val);                  \
498                 else                                    \
499                         *ptr = (val);                   \
500         } while (0)
501
502 static const char *opt_type_name(const struct fio_option *o)
503 {
504         compiletime_assert(FIO_ARRAY_SIZE(opt_type_names) - 1 == FIO_OPT_UNSUPPORTED,
505                                 "opt_type_names[] index");
506
507         if (o->type <= FIO_OPT_UNSUPPORTED)
508                 return opt_type_names[o->type];
509
510         return "OPT_UNKNOWN?";
511 }
512
513 static bool val_too_large(const struct fio_option *o, unsigned long long val,
514                           bool is_uint)
515 {
516         if (!o->maxval)
517                 return false;
518
519         if (is_uint) {
520                 if ((int) val < 0)
521                         return (int) val > (int) o->maxval;
522                 return (unsigned int) val > o->maxval;
523         }
524
525         return val > o->maxval;
526 }
527
528 static bool val_too_small(const struct fio_option *o, unsigned long long val,
529                           bool is_uint)
530 {
531         if (!o->minval)
532                 return false;
533
534         if (is_uint)
535                 return (int) val < o->minval;
536
537         return val < o->minval;
538 }
539
540 static int __handle_option(const struct fio_option *o, const char *ptr,
541                            void *data, int first, int more, int curr)
542 {
543         int il=0, *ilp;
544         fio_fp64_t *flp;
545         long long ull, *ullp;
546         long ul2;
547         long long ull1, ull2;
548         double uf;
549         char **cp = NULL;
550         int ret = 0, is_time = 0;
551         const struct value_pair *vp;
552         struct value_pair posval[PARSE_MAX_VP];
553         int i, all_skipped = 1;
554
555         dprint(FD_PARSE, "__handle_option=%s, type=%s, ptr=%s\n", o->name,
556                                                         opt_type_name(o), ptr);
557
558         if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
559                 log_err("Option %s requires an argument\n", o->name);
560                 return 1;
561         }
562
563         switch (o->type) {
564         case FIO_OPT_STR:
565         case FIO_OPT_STR_ULL:
566         case FIO_OPT_STR_MULTI: {
567                 fio_opt_str_fn *fn = o->cb;
568
569                 posval_sort(o, posval);
570
571                 ret = 1;
572                 for (i = 0; i < PARSE_MAX_VP; i++) {
573                         vp = &posval[i];
574                         if (!vp->ival || vp->ival[0] == '\0')
575                                 continue;
576                         all_skipped = 0;
577                         if (!ptr)
578                                 break;
579                         if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
580                                 ret = 0;
581                                 if (!o->off1)
582                                         continue;
583                                 if (o->type == FIO_OPT_STR_ULL)
584                                         val_store(ullp, vp->oval, o->off1, vp->orval, data, o);
585                                 else
586                                         val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
587                                 continue;
588                         }
589                 }
590
591                 if (ret && !all_skipped)
592                         show_option_values(o);
593                 else if (fn)
594                         ret = fn(data, ptr);
595                 break;
596         }
597         case FIO_OPT_STR_VAL_TIME:
598                 is_time = 1;
599                 fallthrough;
600         case FIO_OPT_ULL:
601         case FIO_OPT_INT:
602         case FIO_OPT_STR_VAL: {
603                 fio_opt_str_val_fn *fn = o->cb;
604                 char tmp[128], *p;
605
606                 if (!is_time && o->is_time)
607                         is_time = o->is_time;
608
609                 snprintf(tmp, sizeof(tmp), "%s", ptr);
610                 p = strchr(tmp, ',');
611                 if (p)
612                         *p = '\0';
613
614                 if (is_time)
615                         ret = check_str_time(tmp, &ull, o->is_seconds);
616                 else
617                         ret = check_str_bytes(tmp, &ull, data);
618
619                 dprint(FD_PARSE, "  ret=%d, out=%llu\n", ret, ull);
620
621                 if (ret)
622                         break;
623                 if (o->pow2 && !is_power_of_2(ull)) {
624                         log_err("%s: must be a power-of-2\n", o->name);
625                         return 1;
626                 }
627
628                 if (val_too_large(o, ull, o->type == FIO_OPT_INT)) {
629                         log_err("%s: max value out of range: %llu"
630                                 " (%llu max)\n", o->name, ull, o->maxval);
631                         return 1;
632                 }
633                 if (val_too_small(o, ull, o->type == FIO_OPT_INT)) {
634                         log_err("%s: min value out of range: %lld"
635                                 " (%d min)\n", o->name, ull, o->minval);
636                         return 1;
637                 }
638                 if (o->posval[0].ival) {
639                         posval_sort(o, posval);
640
641                         ret = 1;
642                         for (i = 0; i < PARSE_MAX_VP; i++) {
643                                 vp = &posval[i];
644                                 if (!vp->ival || vp->ival[0] == '\0')
645                                         continue;
646                                 if (vp->oval == ull) {
647                                         ret = 0;
648                                         break;
649                                 }
650                         }
651                         if (ret) {
652                                 log_err("fio: value %llu not allowed:\n", ull);
653                                 show_option_values(o);
654                                 return 1;
655                         }
656                 }
657
658                 if (fn)
659                         ret = fn(data, &ull);
660                 else {
661                         if (o->type == FIO_OPT_INT) {
662                                 if (first)
663                                         val_store(ilp, ull, o->off1, 0, data, o);
664                                 if (curr == 1) {
665                                         if (o->off2)
666                                                 val_store(ilp, ull, o->off2, 0, data, o);
667                                 }
668                                 if (curr == 2) {
669                                         if (o->off3)
670                                                 val_store(ilp, ull, o->off3, 0, data, o);
671                                 }
672                                 if (!more) {
673                                         if (curr < 1) {
674                                                 if (o->off2)
675                                                         val_store(ilp, ull, o->off2, 0, data, o);
676                                         }
677                                         if (curr < 2) {
678                                                 if (o->off3)
679                                                         val_store(ilp, ull, o->off3, 0, data, o);
680                                         }
681                                 }
682                         } else if (o->type == FIO_OPT_ULL) {
683                                 if (first)
684                                         val_store(ullp, ull, o->off1, 0, data, o);
685                                 if (curr == 1) {
686                                         if (o->off2)
687                                                 val_store(ullp, ull, o->off2, 0, data, o);
688                                 }
689                                 if (curr == 2) {
690                                         if (o->off3)
691                                                 val_store(ullp, ull, o->off3, 0, data, o);
692                                 }
693                                 if (!more) {
694                                         if (curr < 1) {
695                                                 if (o->off2)
696                                                         val_store(ullp, ull, o->off2, 0, data, o);
697                                         }
698                                         if (curr < 2) {
699                                                 if (o->off3)
700                                                         val_store(ullp, ull, o->off3, 0, data, o);
701                                         }
702                                 }
703                         } else {
704                                 if (first)
705                                         val_store(ullp, ull, o->off1, 0, data, o);
706                                 if (!more) {
707                                         if (o->off2)
708                                                 val_store(ullp, ull, o->off2, 0, data, o);
709                                 }
710                         }
711                 }
712                 break;
713         }
714         case FIO_OPT_FLOAT_LIST: {
715                 char *cp2;
716
717                 if (first) {
718                         /*
719                         ** Initialize precision to 0 and zero out list
720                         ** in case specified list is shorter than default
721                         */
722                         if (o->off2) {
723                                 ul2 = 0;
724                                 ilp = td_var(data, o, o->off2);
725                                 *ilp = ul2;
726                         }
727
728                         flp = td_var(data, o, o->off1);
729                         for(i = 0; i < o->maxlen; i++)
730                                 flp[i].u.f = 0.0;
731                 }
732                 if (curr >= o->maxlen) {
733                         log_err("the list exceeding max length %d\n",
734                                         o->maxlen);
735                         return 1;
736                 }
737                 if (!str_to_float(ptr, &uf, 0)) { /* this breaks if we ever have lists of times */
738                         log_err("not a floating point value: %s\n", ptr);
739                         return 1;
740                 }
741                 if (o->minfp || o->maxfp) {
742                         if (uf > o->maxfp) {
743                                 log_err("value out of range: %f"
744                                         " (range max: %f)\n", uf, o->maxfp);
745                                 return 1;
746                         }
747                         if (uf < o->minfp) {
748                                 log_err("value out of range: %f"
749                                         " (range min: %f)\n", uf, o->minfp);
750                                 return 1;
751                         }
752                 }
753
754                 flp = td_var(data, o, o->off1);
755                 flp[curr].u.f = uf;
756
757                 dprint(FD_PARSE, "  out=%f\n", uf);
758
759                 /*
760                 ** Calculate precision for output by counting
761                 ** number of digits after period. Find first
762                 ** period in entire remaining list each time
763                 */
764                 cp2 = strchr(ptr, '.');
765                 if (cp2 != NULL) {
766                         int len = 0;
767
768                         while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
769                                 len++;
770
771                         if (o->off2) {
772                                 ilp = td_var(data, o, o->off2);
773                                 if (len > *ilp)
774                                         *ilp = len;
775                         }
776                 }
777
778                 break;
779         }
780         case FIO_OPT_STR_STORE: {
781                 fio_opt_str_fn *fn = o->cb;
782
783                 if (!strlen(ptr))
784                         return 1;
785
786                 if (o->off1) {
787                         cp = td_var(data, o, o->off1);
788                         *cp = strdup(ptr);
789                         if (strlen(ptr) > o->maxlen - 1) {
790                                 log_err("value exceeds max length of %d\n",
791                                         o->maxlen);
792                                 return 1;
793                         }
794                 }
795
796                 if (fn)
797                         ret = fn(data, ptr);
798                 else if (o->posval[0].ival) {
799                         posval_sort(o, posval);
800
801                         ret = 1;
802                         for (i = 0; i < PARSE_MAX_VP; i++) {
803                                 vp = &posval[i];
804                                 if (!vp->ival || vp->ival[0] == '\0' || !cp)
805                                         continue;
806                                 all_skipped = 0;
807                                 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
808                                         char *rest;
809
810                                         ret = 0;
811                                         if (vp->cb)
812                                                 fn = vp->cb;
813                                         rest = strstr(*cp ?: ptr, ":");
814                                         if (rest) {
815                                                 if (*cp)
816                                                         *rest = '\0';
817                                                 ptr = rest + 1;
818                                         } else
819                                                 ptr = NULL;
820                                         break;
821                                 }
822                         }
823                 }
824
825                 if (!all_skipped) {
826                         if (ret && !*cp)
827                                 show_option_values(o);
828                         else if (ret && *cp)
829                                 ret = 0;
830                         else if (fn && ptr)
831                                 ret = fn(data, ptr);
832                 }
833
834                 break;
835         }
836         case FIO_OPT_RANGE: {
837                 char tmp[128];
838                 char *p1, *p2;
839
840                 snprintf(tmp, sizeof(tmp), "%s", ptr);
841
842                 /* Handle bsrange with separate read,write values: */
843                 p1 = strchr(tmp, ',');
844                 if (p1)
845                         *p1 = '\0';
846
847                 p1 = strchr(tmp, '-');
848                 if (!p1) {
849                         p1 = strchr(tmp, ':');
850                         if (!p1) {
851                                 ret = 1;
852                                 break;
853                         }
854                 }
855
856                 p2 = p1 + 1;
857                 *p1 = '\0';
858                 p1 = tmp;
859
860                 ret = 1;
861                 if (!check_range_bytes(p1, &ull1, data) &&
862                         !check_range_bytes(p2, &ull2, data)) {
863                         ret = 0;
864                         if (ull1 > ull2) {
865                                 unsigned long long foo = ull1;
866
867                                 ull1 = ull2;
868                                 ull2 = foo;
869                         }
870
871                         if (first) {
872                                 val_store(ullp, ull1, o->off1, 0, data, o);
873                                 val_store(ullp, ull2, o->off2, 0, data, o);
874                         }
875                         if (curr == 1) {
876                                 if (o->off3 && o->off4) {
877                                         val_store(ullp, ull1, o->off3, 0, data, o);
878                                         val_store(ullp, ull2, o->off4, 0, data, o);
879                                 }
880                         }
881                         if (curr == 2) {
882                                 if (o->off5 && o->off6) {
883                                         val_store(ullp, ull1, o->off5, 0, data, o);
884                                         val_store(ullp, ull2, o->off6, 0, data, o);
885                                 }
886                         }
887                         if (!more) {
888                                 if (curr < 1) {
889                                         if (o->off3 && o->off4) {
890                                                 val_store(ullp, ull1, o->off3, 0, data, o);
891                                                 val_store(ullp, ull2, o->off4, 0, data, o);
892                                         }
893                                 }
894                                 if (curr < 2) {
895                                         if (o->off5 && o->off6) {
896                                                 val_store(ullp, ull1, o->off5, 0, data, o);
897                                                 val_store(ullp, ull2, o->off6, 0, data, o);
898                                         }
899                                 }
900                         }
901                 }
902
903                 break;
904         }
905         case FIO_OPT_BOOL:
906         case FIO_OPT_STR_SET: {
907                 fio_opt_int_fn *fn = o->cb;
908
909                 if (ptr)
910                         ret = check_int(ptr, &il);
911                 else if (o->type == FIO_OPT_BOOL)
912                         ret = 1;
913                 else
914                         il = 1;
915
916                 dprint(FD_PARSE, "  ret=%d, out=%d\n", ret, il);
917
918                 if (ret)
919                         break;
920
921                 if (o->maxval && il > (int) o->maxval) {
922                         log_err("max value out of range: %d (%llu max)\n",
923                                                                 il, o->maxval);
924                         return 1;
925                 }
926                 if (o->minval && il < o->minval) {
927                         log_err("min value out of range: %d (%d min)\n",
928                                                                 il, o->minval);
929                         return 1;
930                 }
931
932                 if (o->neg)
933                         il = !il;
934
935                 if (fn)
936                         ret = fn(data, &il);
937                 else {
938                         if (first)
939                                 val_store(ilp, il, o->off1, 0, data, o);
940                         if (!more) {
941                                 if (o->off2)
942                                         val_store(ilp, il, o->off2, 0, data, o);
943                         }
944                 }
945                 break;
946         }
947         case FIO_OPT_DEPRECATED:
948                 ret = 1;
949                 fallthrough;
950         case FIO_OPT_SOFT_DEPRECATED:
951                 log_info("Option %s is deprecated\n", o->name);
952                 break;
953         default:
954                 log_err("Bad option type %u\n", o->type);
955                 ret = 1;
956         }
957
958         if (ret)
959                 return ret;
960
961         if (o->verify) {
962                 ret = o->verify(o, data);
963                 if (ret) {
964                         log_err("Correct format for offending option\n");
965                         log_err("%20s: %s\n", o->name, o->help);
966                         show_option_help(o, 1);
967                 }
968         }
969
970         return ret;
971 }
972
973 static int handle_option(const struct fio_option *o, const char *__ptr,
974                          void *data)
975 {
976         char *o_ptr, *ptr, *ptr2;
977         int ret, done;
978
979         dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
980
981         o_ptr = ptr = NULL;
982         if (__ptr)
983                 o_ptr = ptr = strdup(__ptr);
984
985         /*
986          * See if we have another set of parameters, hidden after a comma.
987          * Do this before parsing this round, to check if we should
988          * copy set 1 options to set 2.
989          */
990         done = 0;
991         ret = 1;
992         do {
993                 int __ret;
994
995                 ptr2 = NULL;
996                 if (ptr &&
997                     (o->type != FIO_OPT_STR_STORE) &&
998                     (o->type != FIO_OPT_STR) &&
999                     (o->type != FIO_OPT_STR_ULL) &&
1000                     (o->type != FIO_OPT_FLOAT_LIST)) {
1001                         ptr2 = strchr(ptr, ',');
1002                         if (ptr2 && *(ptr2 + 1) == '\0')
1003                                 *ptr2 = '\0';
1004                         if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
1005                                 if (!ptr2)
1006                                         ptr2 = strchr(ptr, ':');
1007                                 if (!ptr2)
1008                                         ptr2 = strchr(ptr, '-');
1009                         }
1010                 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
1011                         ptr2 = strchr(ptr, ':');
1012                 }
1013
1014                 /*
1015                  * Don't return early if parsing the first option fails - if
1016                  * we are doing multiple arguments, we can allow the first one
1017                  * being empty.
1018                  */
1019                 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
1020                 if (ret)
1021                         ret = __ret;
1022
1023                 if (!ptr2)
1024                         break;
1025
1026                 ptr = ptr2 + 1;
1027                 done++;
1028         } while (1);
1029
1030         if (o_ptr)
1031                 free(o_ptr);
1032         return ret;
1033 }
1034
1035 struct fio_option *find_option(struct fio_option *options, const char *opt)
1036 {
1037         struct fio_option *o;
1038
1039         for (o = &options[0]; o->name; o++) {
1040                 if (!o_match(o, opt))
1041                         continue;
1042                 if (o->type == FIO_OPT_UNSUPPORTED) {
1043                         log_err("Option <%s>: %s\n", o->name, o->help);
1044                         continue;
1045                 }
1046
1047                 return o;
1048         }
1049
1050         return NULL;
1051 }
1052
1053 const struct fio_option *
1054 find_option_c(const struct fio_option *options, const char *opt)
1055 {
1056         const struct fio_option *o;
1057
1058         for (o = &options[0]; o->name; o++) {
1059                 if (!o_match(o, opt))
1060                         continue;
1061                 if (o->type == FIO_OPT_UNSUPPORTED) {
1062                         log_err("Option <%s>: %s\n", o->name, o->help);
1063                         continue;
1064                 }
1065
1066                 return o;
1067         }
1068
1069         return NULL;
1070 }
1071
1072 static const struct fio_option *
1073 get_option(char *opt, const struct fio_option *options, char **post)
1074 {
1075         const struct fio_option *o;
1076         char *ret;
1077
1078         ret = strchr(opt, '=');
1079         if (ret) {
1080                 *post = ret;
1081                 *ret = '\0';
1082                 ret = opt;
1083                 (*post)++;
1084                 strip_blank_end(ret);
1085                 o = find_option_c(options, ret);
1086         } else {
1087                 o = find_option_c(options, opt);
1088                 *post = NULL;
1089         }
1090
1091         return o;
1092 }
1093
1094 static int opt_cmp(const void *p1, const void *p2)
1095 {
1096         const struct fio_option *o;
1097         char *s, *foo;
1098         int prio1, prio2;
1099
1100         prio1 = prio2 = 0;
1101
1102         if (*(char **)p1) {
1103                 s = strdup(*((char **) p1));
1104                 o = get_option(s, __fio_options, &foo);
1105                 if (o)
1106                         prio1 = o->prio;
1107                 free(s);
1108         }
1109         if (*(char **)p2) {
1110                 s = strdup(*((char **) p2));
1111                 o = get_option(s, __fio_options, &foo);
1112                 if (o)
1113                         prio2 = o->prio;
1114                 free(s);
1115         }
1116
1117         return prio2 - prio1;
1118 }
1119
1120 void sort_options(char **opts, const struct fio_option *options, int num_opts)
1121 {
1122         __fio_options = options;
1123         qsort(opts, num_opts, sizeof(char *), opt_cmp);
1124         __fio_options = NULL;
1125 }
1126
1127 static void add_to_dump_list(const struct fio_option *o,
1128                              struct flist_head *dump_list, const char *post)
1129 {
1130         struct print_option *p;
1131
1132         if (!dump_list)
1133                 return;
1134
1135         p = malloc(sizeof(*p));
1136         p->name = strdup(o->name);
1137         if (post)
1138                 p->value = strdup(post);
1139         else
1140                 p->value = NULL;
1141
1142         flist_add_tail(&p->list, dump_list);
1143 }
1144
1145 int parse_cmd_option(const char *opt, const char *val,
1146                      const struct fio_option *options, void *data,
1147                      struct flist_head *dump_list)
1148 {
1149         const struct fio_option *o;
1150
1151         o = find_option_c(options, opt);
1152         if (!o) {
1153                 log_err("Bad option <%s>\n", opt);
1154                 return 1;
1155         }
1156
1157         if (handle_option(o, val, data)) {
1158                 log_err("fio: failed parsing %s=%s\n", opt, val);
1159                 return 1;
1160         }
1161
1162         add_to_dump_list(o, dump_list, val);
1163         return 0;
1164 }
1165
1166 int parse_option(char *opt, const char *input, const struct fio_option *options,
1167                  const struct fio_option **o, void *data,
1168                  struct flist_head *dump_list)
1169 {
1170         char *post;
1171
1172         if (!opt) {
1173                 log_err("fio: failed parsing %s\n", input);
1174                 *o = NULL;
1175                 return 1;
1176         }
1177
1178         *o = get_option(opt, options, &post);
1179         if (!*o) {
1180                 if (post) {
1181                         int len = strlen(opt);
1182                         if (opt + len + 1 != post)
1183                                 memmove(opt + len + 1, post, strlen(post));
1184                         opt[len] = '=';
1185                 }
1186                 return 1;
1187         }
1188
1189         if (handle_option(*o, post, data)) {
1190                 log_err("fio: failed parsing %s\n", input);
1191                 return 1;
1192         }
1193
1194         add_to_dump_list(*o, dump_list, post);
1195         return 0;
1196 }
1197
1198 /*
1199  * Option match, levenshtein distance. Handy for not quite remembering what
1200  * the option name is.
1201  */
1202 int string_distance(const char *s1, const char *s2)
1203 {
1204         unsigned int s1_len = strlen(s1);
1205         unsigned int s2_len = strlen(s2);
1206         unsigned int *p, *q, *r;
1207         unsigned int i, j;
1208
1209         p = malloc(sizeof(unsigned int) * (s2_len + 1));
1210         q = malloc(sizeof(unsigned int) * (s2_len + 1));
1211
1212         p[0] = 0;
1213         for (i = 1; i <= s2_len; i++)
1214                 p[i] = p[i - 1] + 1;
1215
1216         for (i = 1; i <= s1_len; i++) {
1217                 q[0] = p[0] + 1;
1218                 for (j = 1; j <= s2_len; j++) {
1219                         unsigned int sub = p[j - 1];
1220                         unsigned int pmin;
1221
1222                         if (s1[i - 1] != s2[j - 1])
1223                                 sub++;
1224
1225                         pmin = min(q[j - 1] + 1, sub);
1226                         q[j] = min(p[j] + 1, pmin);
1227                 }
1228                 r = p;
1229                 p = q;
1230                 q = r;
1231         }
1232
1233         i = p[s2_len];
1234         free(p);
1235         free(q);
1236         return i;
1237 }
1238
1239 /*
1240  * Make a guess of whether the distance from 's1' is significant enough
1241  * to warrant printing the guess. We set this to a 1/2 match.
1242  */
1243 int string_distance_ok(const char *opt, int distance)
1244 {
1245         size_t len;
1246
1247         len = strlen(opt);
1248         len = (len + 1) / 2;
1249         return distance <= len;
1250 }
1251
1252 static const struct fio_option *find_child(const struct fio_option *options,
1253                                            const struct fio_option *o)
1254 {
1255         const struct fio_option *__o;
1256
1257         for (__o = options + 1; __o->name; __o++)
1258                 if (__o->parent && !strcmp(__o->parent, o->name))
1259                         return __o;
1260
1261         return NULL;
1262 }
1263
1264 static void __print_option(const struct fio_option *o,
1265                            const struct fio_option *org,
1266                            int level)
1267 {
1268         char name[256], *p;
1269         int depth;
1270
1271         if (!o)
1272                 return;
1273
1274         p = name;
1275         depth = level;
1276         while (depth--)
1277                 p += sprintf(p, "%s", "  ");
1278
1279         sprintf(p, "%s", o->name);
1280
1281         log_info("%-24s: %s\n", name, o->help);
1282 }
1283
1284 static void print_option(const struct fio_option *o)
1285 {
1286         const struct fio_option *parent;
1287         const struct fio_option *__o;
1288         unsigned int printed;
1289         unsigned int level;
1290
1291         __print_option(o, NULL, 0);
1292         parent = o;
1293         level = 0;
1294         do {
1295                 level++;
1296                 printed = 0;
1297
1298                 while ((__o = find_child(o, parent)) != NULL) {
1299                         __print_option(__o, o, level);
1300                         o = __o;
1301                         printed++;
1302                 }
1303
1304                 parent = o;
1305         } while (printed);
1306 }
1307
1308 int show_cmd_help(const struct fio_option *options, const char *name)
1309 {
1310         const struct fio_option *o, *closest;
1311         unsigned int best_dist = -1U;
1312         int found = 0;
1313         int show_all = 0;
1314
1315         if (!name || !strcmp(name, "all"))
1316                 show_all = 1;
1317
1318         closest = NULL;
1319         best_dist = -1;
1320         for (o = &options[0]; o->name; o++) {
1321                 int match = 0;
1322
1323                 if (o->type == FIO_OPT_DEPRECATED ||
1324                     o->type == FIO_OPT_SOFT_DEPRECATED)
1325                         continue;
1326                 if (!exec_profile && o->prof_name)
1327                         continue;
1328                 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1329                         continue;
1330
1331                 if (name) {
1332                         if (!strcmp(name, o->name) ||
1333                             (o->alias && !strcmp(name, o->alias)))
1334                                 match = 1;
1335                         else {
1336                                 unsigned int dist;
1337
1338                                 dist = string_distance(name, o->name);
1339                                 if (dist < best_dist) {
1340                                         best_dist = dist;
1341                                         closest = o;
1342                                 }
1343                         }
1344                 }
1345
1346                 if (show_all || match) {
1347                         found = 1;
1348                         if (match)
1349                                 log_info("%20s: %s\n", o->name, o->help);
1350                         if (show_all) {
1351                                 if (!o->parent)
1352                                         print_option(o);
1353                                 continue;
1354                         }
1355                 }
1356
1357                 if (!match)
1358                         continue;
1359
1360                 show_option_help(o, 0);
1361         }
1362
1363         if (found)
1364                 return 0;
1365
1366         log_err("No such command: %s", name);
1367
1368         /*
1369          * Only print an appropriately close option, one where the edit
1370          * distance isn't too big. Otherwise we get crazy matches.
1371          */
1372         if (closest && best_dist < 3) {
1373                 log_info(" - showing closest match\n");
1374                 log_info("%20s: %s\n", closest->name, closest->help);
1375                 show_option_help(closest, 0);
1376         } else
1377                 log_info("\n");
1378
1379         return 1;
1380 }
1381
1382 /*
1383  * Handle parsing of default parameters.
1384  */
1385 void fill_default_options(void *data, const struct fio_option *options)
1386 {
1387         const struct fio_option *o;
1388
1389         dprint(FD_PARSE, "filling default options\n");
1390
1391         for (o = &options[0]; o->name; o++)
1392                 if (o->def)
1393                         handle_option(o, o->def, data);
1394 }
1395
1396 static void option_init(struct fio_option *o)
1397 {
1398         if (o->type == FIO_OPT_DEPRECATED || o->type == FIO_OPT_UNSUPPORTED ||
1399             o->type == FIO_OPT_SOFT_DEPRECATED)
1400                 return;
1401         if (o->name && !o->lname)
1402                 log_err("Option %s: missing long option name\n", o->name);
1403         if (o->type == FIO_OPT_BOOL) {
1404                 o->minval = 0;
1405                 o->maxval = 1;
1406         }
1407         if (o->type == FIO_OPT_INT) {
1408                 if (!o->maxval)
1409                         o->maxval = UINT_MAX;
1410         }
1411         if (o->type == FIO_OPT_ULL) {
1412                 if (!o->maxval)
1413                         o->maxval = ULLONG_MAX;
1414         }
1415         if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
1416                 log_err("Option %s: string set option with"
1417                                 " default will always be true\n", o->name);
1418         }
1419         if (!o->cb && !o->off1)
1420                 log_err("Option %s: neither cb nor offset given\n", o->name);
1421         if (!o->category) {
1422                 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1423                 o->category = FIO_OPT_C_GENERAL;
1424                 o->group = FIO_OPT_G_INVALID;
1425         }
1426 }
1427
1428 /*
1429  * Sanitize the options structure. For now it just sets min/max for bool
1430  * values and whether both callback and offsets are given.
1431  */
1432 void options_init(struct fio_option *options)
1433 {
1434         struct fio_option *o;
1435
1436         dprint(FD_PARSE, "init options\n");
1437
1438         for (o = &options[0]; o->name; o++) {
1439                 option_init(o);
1440                 if (o->inverse)
1441                         o->inv_opt = find_option(options, o->inverse);
1442         }
1443 }
1444
1445 void options_mem_dupe(const struct fio_option *options, void *data)
1446 {
1447         const struct fio_option *o;
1448         char **ptr;
1449
1450         dprint(FD_PARSE, "dup options\n");
1451
1452         for (o = &options[0]; o->name; o++) {
1453                 if (o->type != FIO_OPT_STR_STORE)
1454                         continue;
1455
1456                 ptr = td_var(data, o, o->off1);
1457                 if (*ptr)
1458                         *ptr = strdup(*ptr);
1459         }
1460 }
1461
1462 void options_free(const struct fio_option *options, void *data)
1463 {
1464         const struct fio_option *o;
1465         char **ptr;
1466
1467         dprint(FD_PARSE, "free options\n");
1468
1469         for (o = &options[0]; o->name; o++) {
1470                 if (o->type != FIO_OPT_STR_STORE || !o->off1 || o->no_free)
1471                         continue;
1472
1473                 ptr = td_var(data, o, o->off1);
1474                 if (*ptr) {
1475                         free(*ptr);
1476                         *ptr = NULL;
1477                 }
1478         }
1479 }