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