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