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