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