Use stdlib.h instead of malloc.h
[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 case FIO_OPT_ULL:
566 case FIO_OPT_INT:
567 case FIO_OPT_STR_VAL: {
568 fio_opt_str_val_fn *fn = o->cb;
569 char tmp[128], *p;
570
571 if (!is_time && o->is_time)
572 is_time = o->is_time;
573
574 tmp[sizeof(tmp) - 1] = '\0';
575 strncpy(tmp, ptr, sizeof(tmp) - 1);
576 p = strchr(tmp, ',');
577 if (p)
578 *p = '\0';
579
580 if (is_time)
581 ret = check_str_time(tmp, &ull, o->is_seconds);
582 else
583 ret = check_str_bytes(tmp, &ull, data);
584
585 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
586
587 if (ret)
588 break;
589 if (o->pow2 && !is_power_of_2(ull)) {
590 log_err("%s: must be a power-of-2\n", o->name);
591 return 1;
592 }
593
594 if (o->maxval && ull > o->maxval) {
595 log_err("max value out of range: %llu"
596 " (%llu max)\n", ull, o->maxval);
597 return 1;
598 }
599 if (o->minval && ull < o->minval) {
600 log_err("min value out of range: %lld"
601 " (%d min)\n", ull, o->minval);
602 return 1;
603 }
604 if (o->posval[0].ival) {
605 posval_sort(o, posval);
606
607 ret = 1;
608 for (i = 0; i < PARSE_MAX_VP; i++) {
609 vp = &posval[i];
610 if (!vp->ival || vp->ival[0] == '\0')
611 continue;
612 if (vp->oval == ull) {
613 ret = 0;
614 break;
615 }
616 }
617 if (ret) {
618 log_err("fio: value %llu not allowed:\n", ull);
619 show_option_values(o);
620 return 1;
621 }
622 }
623
624 if (fn)
625 ret = fn(data, &ull);
626 else {
627 if (o->type == FIO_OPT_INT) {
628 if (first)
629 val_store(ilp, ull, o->off1, 0, data, o);
630 if (curr == 1) {
631 if (o->off2)
632 val_store(ilp, ull, o->off2, 0, data, o);
633 }
634 if (curr == 2) {
635 if (o->off3)
636 val_store(ilp, ull, o->off3, 0, data, o);
637 }
638 if (!more) {
639 if (curr < 1) {
640 if (o->off2)
641 val_store(ilp, ull, o->off2, 0, data, o);
642 }
643 if (curr < 2) {
644 if (o->off3)
645 val_store(ilp, ull, o->off3, 0, data, o);
646 }
647 }
648 } else if (o->type == FIO_OPT_ULL) {
649 if (first)
650 val_store(ullp, ull, o->off1, 0, data, o);
651 if (curr == 1) {
652 if (o->off2)
653 val_store(ullp, ull, o->off2, 0, data, o);
654 }
655 if (curr == 2) {
656 if (o->off3)
657 val_store(ullp, ull, o->off3, 0, data, o);
658 }
659 if (!more) {
660 if (curr < 1) {
661 if (o->off2)
662 val_store(ullp, ull, o->off2, 0, data, o);
663 }
664 if (curr < 2) {
665 if (o->off3)
666 val_store(ullp, ull, o->off3, 0, data, o);
667 }
668 }
669 } else {
670 if (first)
671 val_store(ullp, ull, o->off1, 0, data, o);
672 if (!more) {
673 if (o->off2)
674 val_store(ullp, ull, o->off2, 0, data, o);
675 }
676 }
677 }
678 break;
679 }
680 case FIO_OPT_FLOAT_LIST: {
681 char *cp2;
682
683 if (first) {
684 /*
685 ** Initialize precision to 0 and zero out list
686 ** in case specified list is shorter than default
687 */
688 if (o->off2) {
689 ul2 = 0;
690 ilp = td_var(data, o, o->off2);
691 *ilp = ul2;
692 }
693
694 flp = td_var(data, o, o->off1);
695 for(i = 0; i < o->maxlen; i++)
696 flp[i].u.f = 0.0;
697 }
698 if (curr >= o->maxlen) {
699 log_err("the list exceeding max length %d\n",
700 o->maxlen);
701 return 1;
702 }
703 if (!str_to_float(ptr, &uf, 0)) { /* this breaks if we ever have lists of times */
704 log_err("not a floating point value: %s\n", ptr);
705 return 1;
706 }
707 if (o->minfp || o->maxfp) {
708 if (uf > o->maxfp) {
709 log_err("value out of range: %f"
710 " (range max: %f)\n", uf, o->maxfp);
711 return 1;
712 }
713 if (uf < o->minfp) {
714 log_err("value out of range: %f"
715 " (range min: %f)\n", uf, o->minfp);
716 return 1;
717 }
718 }
719
720 flp = td_var(data, o, o->off1);
721 flp[curr].u.f = uf;
722
723 dprint(FD_PARSE, " out=%f\n", uf);
724
725 /*
726 ** Calculate precision for output by counting
727 ** number of digits after period. Find first
728 ** period in entire remaining list each time
729 */
730 cp2 = strchr(ptr, '.');
731 if (cp2 != NULL) {
732 int len = 0;
733
734 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
735 len++;
736
737 if (o->off2) {
738 ilp = td_var(data, o, o->off2);
739 if (len > *ilp)
740 *ilp = len;
741 }
742 }
743
744 break;
745 }
746 case FIO_OPT_STR_STORE: {
747 fio_opt_str_fn *fn = o->cb;
748
749 if (!strlen(ptr))
750 return 1;
751
752 if (o->off1) {
753 cp = td_var(data, o, o->off1);
754 *cp = strdup(ptr);
755 }
756
757 if (fn)
758 ret = fn(data, ptr);
759 else if (o->posval[0].ival) {
760 posval_sort(o, posval);
761
762 ret = 1;
763 for (i = 0; i < PARSE_MAX_VP; i++) {
764 vp = &posval[i];
765 if (!vp->ival || vp->ival[0] == '\0' || !cp)
766 continue;
767 all_skipped = 0;
768 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
769 char *rest;
770
771 ret = 0;
772 if (vp->cb)
773 fn = vp->cb;
774 rest = strstr(*cp ?: ptr, ":");
775 if (rest) {
776 if (*cp)
777 *rest = '\0';
778 ptr = rest + 1;
779 } else
780 ptr = NULL;
781 break;
782 }
783 }
784 }
785
786 if (!all_skipped) {
787 if (ret && !*cp)
788 show_option_values(o);
789 else if (ret && *cp)
790 ret = 0;
791 else if (fn && ptr)
792 ret = fn(data, ptr);
793 }
794
795 break;
796 }
797 case FIO_OPT_RANGE: {
798 char tmp[128];
799 char *p1, *p2;
800
801 tmp[sizeof(tmp) - 1] = '\0';
802 strncpy(tmp, ptr, sizeof(tmp) - 1);
803
804 /* Handle bsrange with separate read,write values: */
805 p1 = strchr(tmp, ',');
806 if (p1)
807 *p1 = '\0';
808
809 p1 = strchr(tmp, '-');
810 if (!p1) {
811 p1 = strchr(tmp, ':');
812 if (!p1) {
813 ret = 1;
814 break;
815 }
816 }
817
818 p2 = p1 + 1;
819 *p1 = '\0';
820 p1 = tmp;
821
822 ret = 1;
823 if (!check_range_bytes(p1, &ull1, data) &&
824 !check_range_bytes(p2, &ull2, data)) {
825 ret = 0;
826 if (ull1 > ull2) {
827 unsigned long long foo = ull1;
828
829 ull1 = ull2;
830 ull2 = foo;
831 }
832
833 if (first) {
834 val_store(ullp, ull1, o->off1, 0, data, o);
835 val_store(ullp, ull2, o->off2, 0, data, o);
836 }
837 if (curr == 1) {
838 if (o->off3 && o->off4) {
839 val_store(ullp, ull1, o->off3, 0, data, o);
840 val_store(ullp, ull2, o->off4, 0, data, o);
841 }
842 }
843 if (curr == 2) {
844 if (o->off5 && o->off6) {
845 val_store(ullp, ull1, o->off5, 0, data, o);
846 val_store(ullp, ull2, o->off6, 0, data, o);
847 }
848 }
849 if (!more) {
850 if (curr < 1) {
851 if (o->off3 && o->off4) {
852 val_store(ullp, ull1, o->off3, 0, data, o);
853 val_store(ullp, ull2, o->off4, 0, data, o);
854 }
855 }
856 if (curr < 2) {
857 if (o->off5 && o->off6) {
858 val_store(ullp, ull1, o->off5, 0, data, o);
859 val_store(ullp, ull2, o->off6, 0, data, o);
860 }
861 }
862 }
863 }
864
865 break;
866 }
867 case FIO_OPT_BOOL:
868 case FIO_OPT_STR_SET: {
869 fio_opt_int_fn *fn = o->cb;
870
871 if (ptr)
872 ret = check_int(ptr, &il);
873 else if (o->type == FIO_OPT_BOOL)
874 ret = 1;
875 else
876 il = 1;
877
878 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
879
880 if (ret)
881 break;
882
883 if (o->maxval && il > (int) o->maxval) {
884 log_err("max value out of range: %d (%llu max)\n",
885 il, o->maxval);
886 return 1;
887 }
888 if (o->minval && il < o->minval) {
889 log_err("min value out of range: %d (%d min)\n",
890 il, o->minval);
891 return 1;
892 }
893
894 if (o->neg)
895 il = !il;
896
897 if (fn)
898 ret = fn(data, &il);
899 else {
900 if (first)
901 val_store(ilp, il, o->off1, 0, data, o);
902 if (!more) {
903 if (o->off2)
904 val_store(ilp, il, o->off2, 0, data, o);
905 }
906 }
907 break;
908 }
909 case FIO_OPT_DEPRECATED:
910 ret = 1;
911 case FIO_OPT_SOFT_DEPRECATED:
912 log_info("Option %s is deprecated\n", o->name);
913 break;
914 default:
915 log_err("Bad option type %u\n", o->type);
916 ret = 1;
917 }
918
919 if (ret)
920 return ret;
921
922 if (o->verify) {
923 ret = o->verify(o, data);
924 if (ret) {
925 log_err("Correct format for offending option\n");
926 log_err("%20s: %s\n", o->name, o->help);
927 show_option_help(o, 1);
928 }
929 }
930
931 return ret;
932}
933
934static int handle_option(const struct fio_option *o, const char *__ptr,
935 void *data)
936{
937 char *o_ptr, *ptr, *ptr2;
938 int ret, done;
939
940 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
941
942 o_ptr = ptr = NULL;
943 if (__ptr)
944 o_ptr = ptr = strdup(__ptr);
945
946 /*
947 * See if we have another set of parameters, hidden after a comma.
948 * Do this before parsing this round, to check if we should
949 * copy set 1 options to set 2.
950 */
951 done = 0;
952 ret = 1;
953 do {
954 int __ret;
955
956 ptr2 = NULL;
957 if (ptr &&
958 (o->type != FIO_OPT_STR_STORE) &&
959 (o->type != FIO_OPT_STR) &&
960 (o->type != FIO_OPT_FLOAT_LIST)) {
961 ptr2 = strchr(ptr, ',');
962 if (ptr2 && *(ptr2 + 1) == '\0')
963 *ptr2 = '\0';
964 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
965 if (!ptr2)
966 ptr2 = strchr(ptr, ':');
967 if (!ptr2)
968 ptr2 = strchr(ptr, '-');
969 }
970 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
971 ptr2 = strchr(ptr, ':');
972 }
973
974 /*
975 * Don't return early if parsing the first option fails - if
976 * we are doing multiple arguments, we can allow the first one
977 * being empty.
978 */
979 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
980 if (ret)
981 ret = __ret;
982
983 if (!ptr2)
984 break;
985
986 ptr = ptr2 + 1;
987 done++;
988 } while (1);
989
990 if (o_ptr)
991 free(o_ptr);
992 return ret;
993}
994
995struct fio_option *find_option(struct fio_option *options, const char *opt)
996{
997 struct fio_option *o;
998
999 for (o = &options[0]; o->name; o++) {
1000 if (!o_match(o, opt))
1001 continue;
1002 if (o->type == FIO_OPT_UNSUPPORTED) {
1003 log_err("Option <%s>: %s\n", o->name, o->help);
1004 continue;
1005 }
1006
1007 return o;
1008 }
1009
1010 return NULL;
1011}
1012
1013const struct fio_option *
1014find_option_c(const struct fio_option *options, const char *opt)
1015{
1016 return find_option((struct fio_option *)options, opt);
1017}
1018
1019static const struct fio_option *
1020get_option(char *opt, const struct fio_option *options, char **post)
1021{
1022 const struct fio_option *o;
1023 char *ret;
1024
1025 ret = strchr(opt, '=');
1026 if (ret) {
1027 *post = ret;
1028 *ret = '\0';
1029 ret = opt;
1030 (*post)++;
1031 strip_blank_end(ret);
1032 o = find_option_c(options, ret);
1033 } else {
1034 o = find_option_c(options, opt);
1035 *post = NULL;
1036 }
1037
1038 return o;
1039}
1040
1041static int opt_cmp(const void *p1, const void *p2)
1042{
1043 const struct fio_option *o;
1044 char *s, *foo;
1045 int prio1, prio2;
1046
1047 prio1 = prio2 = 0;
1048
1049 if (*(char **)p1) {
1050 s = strdup(*((char **) p1));
1051 o = get_option(s, __fio_options, &foo);
1052 if (o)
1053 prio1 = o->prio;
1054 free(s);
1055 }
1056 if (*(char **)p2) {
1057 s = strdup(*((char **) p2));
1058 o = get_option(s, __fio_options, &foo);
1059 if (o)
1060 prio2 = o->prio;
1061 free(s);
1062 }
1063
1064 return prio2 - prio1;
1065}
1066
1067void sort_options(char **opts, const struct fio_option *options, int num_opts)
1068{
1069 __fio_options = options;
1070 qsort(opts, num_opts, sizeof(char *), opt_cmp);
1071 __fio_options = NULL;
1072}
1073
1074static void add_to_dump_list(const struct fio_option *o,
1075 struct flist_head *dump_list, const char *post)
1076{
1077 struct print_option *p;
1078
1079 if (!dump_list)
1080 return;
1081
1082 p = malloc(sizeof(*p));
1083 p->name = strdup(o->name);
1084 if (post)
1085 p->value = strdup(post);
1086 else
1087 p->value = NULL;
1088
1089 flist_add_tail(&p->list, dump_list);
1090}
1091
1092int parse_cmd_option(const char *opt, const char *val,
1093 const struct fio_option *options, void *data,
1094 struct flist_head *dump_list)
1095{
1096 const struct fio_option *o;
1097
1098 o = find_option_c(options, opt);
1099 if (!o) {
1100 log_err("Bad option <%s>\n", opt);
1101 return 1;
1102 }
1103
1104 if (handle_option(o, val, data)) {
1105 log_err("fio: failed parsing %s=%s\n", opt, val);
1106 return 1;
1107 }
1108
1109 add_to_dump_list(o, dump_list, val);
1110 return 0;
1111}
1112
1113int parse_option(char *opt, const char *input, const struct fio_option *options,
1114 const struct fio_option **o, void *data,
1115 struct flist_head *dump_list)
1116{
1117 char *post;
1118
1119 if (!opt) {
1120 log_err("fio: failed parsing %s\n", input);
1121 *o = NULL;
1122 return 1;
1123 }
1124
1125 *o = get_option(opt, options, &post);
1126 if (!*o) {
1127 if (post) {
1128 int len = strlen(opt);
1129 if (opt + len + 1 != post)
1130 memmove(opt + len + 1, post, strlen(post));
1131 opt[len] = '=';
1132 }
1133 return 1;
1134 }
1135
1136 if (handle_option(*o, post, data)) {
1137 log_err("fio: failed parsing %s\n", input);
1138 return 1;
1139 }
1140
1141 add_to_dump_list(*o, dump_list, post);
1142 return 0;
1143}
1144
1145/*
1146 * Option match, levenshtein distance. Handy for not quite remembering what
1147 * the option name is.
1148 */
1149int string_distance(const char *s1, const char *s2)
1150{
1151 unsigned int s1_len = strlen(s1);
1152 unsigned int s2_len = strlen(s2);
1153 unsigned int *p, *q, *r;
1154 unsigned int i, j;
1155
1156 p = malloc(sizeof(unsigned int) * (s2_len + 1));
1157 q = malloc(sizeof(unsigned int) * (s2_len + 1));
1158
1159 p[0] = 0;
1160 for (i = 1; i <= s2_len; i++)
1161 p[i] = p[i - 1] + 1;
1162
1163 for (i = 1; i <= s1_len; i++) {
1164 q[0] = p[0] + 1;
1165 for (j = 1; j <= s2_len; j++) {
1166 unsigned int sub = p[j - 1];
1167 unsigned int pmin;
1168
1169 if (s1[i - 1] != s2[j - 1])
1170 sub++;
1171
1172 pmin = min(q[j - 1] + 1, sub);
1173 q[j] = min(p[j] + 1, pmin);
1174 }
1175 r = p;
1176 p = q;
1177 q = r;
1178 }
1179
1180 i = p[s2_len];
1181 free(p);
1182 free(q);
1183 return i;
1184}
1185
1186/*
1187 * Make a guess of whether the distance from 's1' is significant enough
1188 * to warrant printing the guess. We set this to a 1/2 match.
1189 */
1190int string_distance_ok(const char *opt, int distance)
1191{
1192 size_t len;
1193
1194 len = strlen(opt);
1195 len = (len + 1) / 2;
1196 return distance <= len;
1197}
1198
1199static const struct fio_option *find_child(const struct fio_option *options,
1200 const struct fio_option *o)
1201{
1202 const struct fio_option *__o;
1203
1204 for (__o = options + 1; __o->name; __o++)
1205 if (__o->parent && !strcmp(__o->parent, o->name))
1206 return __o;
1207
1208 return NULL;
1209}
1210
1211static void __print_option(const struct fio_option *o,
1212 const struct fio_option *org,
1213 int level)
1214{
1215 char name[256], *p;
1216 int depth;
1217
1218 if (!o)
1219 return;
1220
1221 p = name;
1222 depth = level;
1223 while (depth--)
1224 p += sprintf(p, "%s", " ");
1225
1226 sprintf(p, "%s", o->name);
1227
1228 log_info("%-24s: %s\n", name, o->help);
1229}
1230
1231static void print_option(const struct fio_option *o)
1232{
1233 const struct fio_option *parent;
1234 const struct fio_option *__o;
1235 unsigned int printed;
1236 unsigned int level;
1237
1238 __print_option(o, NULL, 0);
1239 parent = o;
1240 level = 0;
1241 do {
1242 level++;
1243 printed = 0;
1244
1245 while ((__o = find_child(o, parent)) != NULL) {
1246 __print_option(__o, o, level);
1247 o = __o;
1248 printed++;
1249 }
1250
1251 parent = o;
1252 } while (printed);
1253}
1254
1255int show_cmd_help(const struct fio_option *options, const char *name)
1256{
1257 const struct fio_option *o, *closest;
1258 unsigned int best_dist = -1U;
1259 int found = 0;
1260 int show_all = 0;
1261
1262 if (!name || !strcmp(name, "all"))
1263 show_all = 1;
1264
1265 closest = NULL;
1266 best_dist = -1;
1267 for (o = &options[0]; o->name; o++) {
1268 int match = 0;
1269
1270 if (o->type == FIO_OPT_DEPRECATED ||
1271 o->type == FIO_OPT_SOFT_DEPRECATED)
1272 continue;
1273 if (!exec_profile && o->prof_name)
1274 continue;
1275 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1276 continue;
1277
1278 if (name) {
1279 if (!strcmp(name, o->name) ||
1280 (o->alias && !strcmp(name, o->alias)))
1281 match = 1;
1282 else {
1283 unsigned int dist;
1284
1285 dist = string_distance(name, o->name);
1286 if (dist < best_dist) {
1287 best_dist = dist;
1288 closest = o;
1289 }
1290 }
1291 }
1292
1293 if (show_all || match) {
1294 found = 1;
1295 if (match)
1296 log_info("%20s: %s\n", o->name, o->help);
1297 if (show_all) {
1298 if (!o->parent)
1299 print_option(o);
1300 continue;
1301 }
1302 }
1303
1304 if (!match)
1305 continue;
1306
1307 show_option_help(o, 0);
1308 }
1309
1310 if (found)
1311 return 0;
1312
1313 log_err("No such command: %s", name);
1314
1315 /*
1316 * Only print an appropriately close option, one where the edit
1317 * distance isn't too big. Otherwise we get crazy matches.
1318 */
1319 if (closest && best_dist < 3) {
1320 log_info(" - showing closest match\n");
1321 log_info("%20s: %s\n", closest->name, closest->help);
1322 show_option_help(closest, 0);
1323 } else
1324 log_info("\n");
1325
1326 return 1;
1327}
1328
1329/*
1330 * Handle parsing of default parameters.
1331 */
1332void fill_default_options(void *data, const struct fio_option *options)
1333{
1334 const struct fio_option *o;
1335
1336 dprint(FD_PARSE, "filling default options\n");
1337
1338 for (o = &options[0]; o->name; o++)
1339 if (o->def)
1340 handle_option(o, o->def, data);
1341}
1342
1343static void option_init(struct fio_option *o)
1344{
1345 if (o->type == FIO_OPT_DEPRECATED || o->type == FIO_OPT_UNSUPPORTED ||
1346 o->type == FIO_OPT_SOFT_DEPRECATED)
1347 return;
1348 if (o->name && !o->lname)
1349 log_err("Option %s: missing long option name\n", o->name);
1350 if (o->type == FIO_OPT_BOOL) {
1351 o->minval = 0;
1352 o->maxval = 1;
1353 }
1354 if (o->type == FIO_OPT_INT) {
1355 if (!o->maxval)
1356 o->maxval = UINT_MAX;
1357 }
1358 if (o->type == FIO_OPT_ULL) {
1359 if (!o->maxval)
1360 o->maxval = ULLONG_MAX;
1361 }
1362 if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
1363 log_err("Option %s: string set option with"
1364 " default will always be true\n", o->name);
1365 }
1366 if (!o->cb && !o->off1)
1367 log_err("Option %s: neither cb nor offset given\n", o->name);
1368 if (!o->category) {
1369 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1370 o->category = FIO_OPT_C_GENERAL;
1371 o->group = FIO_OPT_G_INVALID;
1372 }
1373 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1374 o->type == FIO_OPT_STR_MULTI)
1375 return;
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}