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