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