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