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