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