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