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