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