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