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