option parsing: Mark arguments that are not modified as 'const'
[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 == 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(const 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(const 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(const 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(const struct fio_option *o, const char *ptr,
499                            void *data, 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(const struct fio_option *o, const char *__ptr,
896                          void *data)
897 {
898         char *o_ptr, *ptr, *ptr2;
899         int ret, done;
900
901         dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
902
903         o_ptr = ptr = NULL;
904         if (__ptr)
905                 o_ptr = ptr = strdup(__ptr);
906
907         /*
908          * See if we have another set of parameters, hidden after a comma.
909          * Do this before parsing this round, to check if we should
910          * copy set 1 options to set 2.
911          */
912         done = 0;
913         ret = 1;
914         do {
915                 int __ret;
916
917                 ptr2 = NULL;
918                 if (ptr &&
919                     (o->type != FIO_OPT_STR_STORE) &&
920                     (o->type != FIO_OPT_STR) &&
921                     (o->type != FIO_OPT_FLOAT_LIST)) {
922                         ptr2 = strchr(ptr, ',');
923                         if (ptr2 && *(ptr2 + 1) == '\0')
924                                 *ptr2 = '\0';
925                         if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
926                                 if (!ptr2)
927                                         ptr2 = strchr(ptr, ':');
928                                 if (!ptr2)
929                                         ptr2 = strchr(ptr, '-');
930                         }
931                 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
932                         ptr2 = strchr(ptr, ':');
933                 }
934
935                 /*
936                  * Don't return early if parsing the first option fails - if
937                  * we are doing multiple arguments, we can allow the first one
938                  * being empty.
939                  */
940                 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
941                 if (ret)
942                         ret = __ret;
943
944                 if (!ptr2)
945                         break;
946
947                 ptr = ptr2 + 1;
948                 done++;
949         } while (1);
950
951         if (o_ptr)
952                 free(o_ptr);
953         return ret;
954 }
955
956 struct fio_option *find_option(struct fio_option *options, const char *opt)
957 {
958         struct fio_option *o;
959
960         for (o = &options[0]; o->name; o++) {
961                 if (!o_match(o, opt))
962                         continue;
963                 if (o->type == FIO_OPT_UNSUPPORTED) {
964                         log_err("Option <%s>: %s\n", o->name, o->help);
965                         continue;
966                 }
967
968                 return o;
969         }
970
971         return NULL;
972 }
973
974 const struct fio_option *
975 find_option_c(const struct fio_option *options, const char *opt)
976 {
977         return find_option((struct fio_option *)options, opt);
978 }
979
980 static const struct fio_option *
981 get_option(char *opt, const struct fio_option *options, char **post)
982 {
983         const struct fio_option *o;
984         char *ret;
985
986         ret = strchr(opt, '=');
987         if (ret) {
988                 *post = ret;
989                 *ret = '\0';
990                 ret = opt;
991                 (*post)++;
992                 strip_blank_end(ret);
993                 o = find_option_c(options, ret);
994         } else {
995                 o = find_option_c(options, opt);
996                 *post = NULL;
997         }
998
999         return o;
1000 }
1001
1002 static int opt_cmp(const void *p1, const void *p2)
1003 {
1004         const struct fio_option *o;
1005         char *s, *foo;
1006         int prio1, prio2;
1007
1008         prio1 = prio2 = 0;
1009
1010         if (*(char **)p1) {
1011                 s = strdup(*((char **) p1));
1012                 o = get_option(s, __fio_options, &foo);
1013                 if (o)
1014                         prio1 = o->prio;
1015                 free(s);
1016         }
1017         if (*(char **)p2) {
1018                 s = strdup(*((char **) p2));
1019                 o = get_option(s, __fio_options, &foo);
1020                 if (o)
1021                         prio2 = o->prio;
1022                 free(s);
1023         }
1024
1025         return prio2 - prio1;
1026 }
1027
1028 void sort_options(char **opts, const struct fio_option *options, int num_opts)
1029 {
1030         __fio_options = options;
1031         qsort(opts, num_opts, sizeof(char *), opt_cmp);
1032         __fio_options = NULL;
1033 }
1034
1035 static void add_to_dump_list(const struct fio_option *o,
1036                              struct flist_head *dump_list, const char *post)
1037 {
1038         struct print_option *p;
1039
1040         if (!dump_list)
1041                 return;
1042
1043         p = malloc(sizeof(*p));
1044         p->name = strdup(o->name);
1045         if (post)
1046                 p->value = strdup(post);
1047         else
1048                 p->value = NULL;
1049
1050         flist_add_tail(&p->list, dump_list);
1051 }
1052
1053 int parse_cmd_option(const char *opt, const char *val,
1054                      const struct fio_option *options, void *data,
1055                      struct flist_head *dump_list)
1056 {
1057         const struct fio_option *o;
1058
1059         o = find_option_c(options, opt);
1060         if (!o) {
1061                 log_err("Bad option <%s>\n", opt);
1062                 return 1;
1063         }
1064
1065         if (handle_option(o, val, data)) {
1066                 log_err("fio: failed parsing %s=%s\n", opt, val);
1067                 return 1;
1068         }
1069
1070         add_to_dump_list(o, dump_list, val);
1071         return 0;
1072 }
1073
1074 int parse_option(char *opt, const char *input, const struct fio_option *options,
1075                  const struct fio_option **o, void *data,
1076                  struct flist_head *dump_list)
1077 {
1078         char *post;
1079
1080         if (!opt) {
1081                 log_err("fio: failed parsing %s\n", input);
1082                 *o = NULL;
1083                 return 1;
1084         }
1085
1086         *o = get_option(opt, options, &post);
1087         if (!*o) {
1088                 if (post) {
1089                         int len = strlen(opt);
1090                         if (opt + len + 1 != post)
1091                                 memmove(opt + len + 1, post, strlen(post));
1092                         opt[len] = '=';
1093                 }
1094                 return 1;
1095         }
1096
1097         if (handle_option(*o, post, data)) {
1098                 log_err("fio: failed parsing %s\n", input);
1099                 return 1;
1100         }
1101
1102         add_to_dump_list(*o, dump_list, post);
1103         return 0;
1104 }
1105
1106 /*
1107  * Option match, levenshtein distance. Handy for not quite remembering what
1108  * the option name is.
1109  */
1110 int string_distance(const char *s1, const char *s2)
1111 {
1112         unsigned int s1_len = strlen(s1);
1113         unsigned int s2_len = strlen(s2);
1114         unsigned int *p, *q, *r;
1115         unsigned int i, j;
1116
1117         p = malloc(sizeof(unsigned int) * (s2_len + 1));
1118         q = malloc(sizeof(unsigned int) * (s2_len + 1));
1119
1120         p[0] = 0;
1121         for (i = 1; i <= s2_len; i++)
1122                 p[i] = p[i - 1] + 1;
1123
1124         for (i = 1; i <= s1_len; i++) {
1125                 q[0] = p[0] + 1;
1126                 for (j = 1; j <= s2_len; j++) {
1127                         unsigned int sub = p[j - 1];
1128                         unsigned int pmin;
1129
1130                         if (s1[i - 1] != s2[j - 1])
1131                                 sub++;
1132
1133                         pmin = min(q[j - 1] + 1, sub);
1134                         q[j] = min(p[j] + 1, pmin);
1135                 }
1136                 r = p;
1137                 p = q;
1138                 q = r;
1139         }
1140
1141         i = p[s2_len];
1142         free(p);
1143         free(q);
1144         return i;
1145 }
1146
1147 /*
1148  * Make a guess of whether the distance from 's1' is significant enough
1149  * to warrant printing the guess. We set this to a 1/2 match.
1150  */
1151 int string_distance_ok(const char *opt, int distance)
1152 {
1153         size_t len;
1154
1155         len = strlen(opt);
1156         len = (len + 1) / 2;
1157         return distance <= len;
1158 }
1159
1160 static const struct fio_option *find_child(const struct fio_option *options,
1161                                            const struct fio_option *o)
1162 {
1163         const struct fio_option *__o;
1164
1165         for (__o = options + 1; __o->name; __o++)
1166                 if (__o->parent && !strcmp(__o->parent, o->name))
1167                         return __o;
1168
1169         return NULL;
1170 }
1171
1172 static void __print_option(const struct fio_option *o,
1173                            const struct fio_option *org,
1174                            int level)
1175 {
1176         char name[256], *p;
1177         int depth;
1178
1179         if (!o)
1180                 return;
1181         if (!org)
1182                 org = o;
1183
1184         p = name;
1185         depth = level;
1186         while (depth--)
1187                 p += sprintf(p, "%s", "  ");
1188
1189         sprintf(p, "%s", o->name);
1190
1191         log_info("%-24s: %s\n", name, o->help);
1192 }
1193
1194 static void print_option(const struct fio_option *o)
1195 {
1196         const struct fio_option *parent;
1197         const struct fio_option *__o;
1198         unsigned int printed;
1199         unsigned int level;
1200
1201         __print_option(o, NULL, 0);
1202         parent = o;
1203         level = 0;
1204         do {
1205                 level++;
1206                 printed = 0;
1207
1208                 while ((__o = find_child(o, parent)) != NULL) {
1209                         __print_option(__o, o, level);
1210                         o = __o;
1211                         printed++;
1212                 }
1213
1214                 parent = o;
1215         } while (printed);
1216 }
1217
1218 int show_cmd_help(const struct fio_option *options, const char *name)
1219 {
1220         const struct fio_option *o, *closest;
1221         unsigned int best_dist = -1U;
1222         int found = 0;
1223         int show_all = 0;
1224
1225         if (!name || !strcmp(name, "all"))
1226                 show_all = 1;
1227
1228         closest = NULL;
1229         best_dist = -1;
1230         for (o = &options[0]; o->name; o++) {
1231                 int match = 0;
1232
1233                 if (o->type == FIO_OPT_DEPRECATED)
1234                         continue;
1235                 if (!exec_profile && o->prof_name)
1236                         continue;
1237                 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1238                         continue;
1239
1240                 if (name) {
1241                         if (!strcmp(name, o->name) ||
1242                             (o->alias && !strcmp(name, o->alias)))
1243                                 match = 1;
1244                         else {
1245                                 unsigned int dist;
1246
1247                                 dist = string_distance(name, o->name);
1248                                 if (dist < best_dist) {
1249                                         best_dist = dist;
1250                                         closest = o;
1251                                 }
1252                         }
1253                 }
1254
1255                 if (show_all || match) {
1256                         found = 1;
1257                         if (match)
1258                                 log_info("%20s: %s\n", o->name, o->help);
1259                         if (show_all) {
1260                                 if (!o->parent)
1261                                         print_option(o);
1262                                 continue;
1263                         }
1264                 }
1265
1266                 if (!match)
1267                         continue;
1268
1269                 show_option_help(o, 0);
1270         }
1271
1272         if (found)
1273                 return 0;
1274
1275         log_err("No such command: %s", name);
1276
1277         /*
1278          * Only print an appropriately close option, one where the edit
1279          * distance isn't too big. Otherwise we get crazy matches.
1280          */
1281         if (closest && best_dist < 3) {
1282                 log_info(" - showing closest match\n");
1283                 log_info("%20s: %s\n", closest->name, closest->help);
1284                 show_option_help(closest, 0);
1285         } else
1286                 log_info("\n");
1287
1288         return 1;
1289 }
1290
1291 /*
1292  * Handle parsing of default parameters.
1293  */
1294 void fill_default_options(void *data, const struct fio_option *options)
1295 {
1296         const struct fio_option *o;
1297
1298         dprint(FD_PARSE, "filling default options\n");
1299
1300         for (o = &options[0]; o->name; o++)
1301                 if (o->def)
1302                         handle_option(o, o->def, data);
1303 }
1304
1305 static void option_init(struct fio_option *o)
1306 {
1307         if (o->type == FIO_OPT_DEPRECATED || o->type == FIO_OPT_UNSUPPORTED)
1308                 return;
1309         if (o->name && !o->lname)
1310                 log_err("Option %s: missing long option name\n", o->name);
1311         if (o->type == FIO_OPT_BOOL) {
1312                 o->minval = 0;
1313                 o->maxval = 1;
1314         }
1315         if (o->type == FIO_OPT_INT) {
1316                 if (!o->maxval)
1317                         o->maxval = UINT_MAX;
1318         }
1319         if (o->type == FIO_OPT_FLOAT_LIST) {
1320                 o->minfp = DBL_MIN;
1321                 o->maxfp = DBL_MAX;
1322         }
1323         if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
1324                 log_err("Option %s: string set option with"
1325                                 " default will always be true\n", o->name);
1326         }
1327         if (!o->cb && !o->off1)
1328                 log_err("Option %s: neither cb nor offset given\n", o->name);
1329         if (!o->category) {
1330                 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1331                 o->category = FIO_OPT_C_GENERAL;
1332                 o->group = FIO_OPT_G_INVALID;
1333         }
1334         if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1335             o->type == FIO_OPT_STR_MULTI)
1336                 return;
1337 }
1338
1339 /*
1340  * Sanitize the options structure. For now it just sets min/max for bool
1341  * values and whether both callback and offsets are given.
1342  */
1343 void options_init(struct fio_option *options)
1344 {
1345         struct fio_option *o;
1346
1347         dprint(FD_PARSE, "init options\n");
1348
1349         for (o = &options[0]; o->name; o++) {
1350                 option_init(o);
1351                 if (o->inverse)
1352                         o->inv_opt = find_option(options, o->inverse);
1353         }
1354 }
1355
1356 void options_mem_dupe(const struct fio_option *options, void *data)
1357 {
1358         const struct fio_option *o;
1359         char **ptr;
1360
1361         dprint(FD_PARSE, "dup options\n");
1362
1363         for (o = &options[0]; o->name; o++) {
1364                 if (o->type != FIO_OPT_STR_STORE)
1365                         continue;
1366
1367                 ptr = td_var(data, o, o->off1);
1368                 if (*ptr)
1369                         *ptr = strdup(*ptr);
1370         }
1371 }
1372
1373 void options_free(const struct fio_option *options, void *data)
1374 {
1375         const struct fio_option *o;
1376         char **ptr;
1377
1378         dprint(FD_PARSE, "free options\n");
1379
1380         for (o = &options[0]; o->name; o++) {
1381                 if (o->type != FIO_OPT_STR_STORE || !o->off1 || o->no_free)
1382                         continue;
1383
1384                 ptr = td_var(data, o, o->off1);
1385                 if (*ptr) {
1386                         free(*ptr);
1387                         *ptr = NULL;
1388                 }
1389         }
1390 }