client: check and error out on exceeding number of command line args to pass
[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 fprintf(stderr, "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 fprintf(stderr, "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 fprintf(stderr, "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 fprintf(stderr, "the list exceeding max length %d\n",
473 o->maxlen);
474 return 1;
475 }
476 if(!str_to_float(ptr, &uf)){
477 fprintf(stderr, "not a floating point value: %s\n",
478 ptr);
479 return 1;
480 }
481 if (!isnan(o->maxfp) && uf > o->maxfp) {
482 fprintf(stderr, "value out of range: %f"
483 " (range max: %f)\n", uf, o->maxfp);
484 return 1;
485 }
486 if (!isnan(o->minfp) && uf < o->minfp) {
487 fprintf(stderr, "value out of range: %f"
488 " (range min: %f)\n", uf, o->minfp);
489 return 1;
490 }
491
492 flp = td_var(data, o->off1);
493 flp[curr] = uf;
494
495 break;
496 }
497 case FIO_OPT_STR_STORE: {
498 fio_opt_str_fn *fn = o->cb;
499
500 posval_sort(o, posval);
501
502 if (!o->posval[0].ival) {
503 vp = NULL;
504 goto match;
505 }
506
507 ret = 1;
508 for (i = 0; i < PARSE_MAX_VP; i++) {
509 vp = &posval[i];
510 if (!vp->ival || vp->ival[0] == '\0')
511 continue;
512 all_skipped = 0;
513 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
514 char *rest;
515
516 ret = 0;
517 if (vp->cb)
518 fn = vp->cb;
519match:
520 if (o->roff1)
521 cp = (char **) o->roff1;
522 else
523 cp = td_var(data, o->off1);
524 *cp = strdup(ptr);
525 rest = strstr(*cp, ":");
526 if (rest) {
527 *rest = '\0';
528 ptr = rest + 1;
529 } else if (vp && vp->cb)
530 ptr = NULL;
531 break;
532 }
533 }
534
535 if (ret && !all_skipped)
536 show_option_values(o);
537 else if (fn && ptr)
538 ret = fn(data, ptr);
539
540 break;
541 }
542 case FIO_OPT_RANGE: {
543 char tmp[128];
544 char *p1, *p2;
545
546 strncpy(tmp, ptr, sizeof(tmp) - 1);
547
548 /* Handle bsrange with separate read,write values: */
549 p1 = strchr(tmp, ',');
550 if (p1)
551 *p1 = '\0';
552
553 p1 = strchr(tmp, '-');
554 if (!p1) {
555 p1 = strchr(tmp, ':');
556 if (!p1) {
557 ret = 1;
558 break;
559 }
560 }
561
562 p2 = p1 + 1;
563 *p1 = '\0';
564 p1 = tmp;
565
566 ret = 1;
567 if (!check_range_bytes(p1, &ul1, data) &&
568 !check_range_bytes(p2, &ul2, data)) {
569 ret = 0;
570 if (ul1 > ul2) {
571 unsigned long foo = ul1;
572
573 ul1 = ul2;
574 ul2 = foo;
575 }
576
577 if (first) {
578 if (o->roff1)
579 *(unsigned int *) o->roff1 = ul1;
580 else
581 val_store(ilp, ul1, o->off1, 0, data);
582 if (o->roff2)
583 *(unsigned int *) o->roff2 = ul2;
584 else
585 val_store(ilp, ul2, o->off2, 0, data);
586 }
587 if (o->roff3 && o->roff4) {
588 *(unsigned int *) o->roff3 = ul1;
589 *(unsigned int *) o->roff4 = ul2;
590 } else if (o->off3 && o->off4) {
591 val_store(ilp, ul1, o->off3, 0, data);
592 val_store(ilp, ul2, o->off4, 0, data);
593 }
594 }
595
596 break;
597 }
598 case FIO_OPT_BOOL:
599 case FIO_OPT_STR_SET: {
600 fio_opt_int_fn *fn = o->cb;
601
602 if (ptr)
603 ret = check_int(ptr, &il);
604 else if (o->type == FIO_OPT_BOOL)
605 ret = 1;
606 else
607 il = 1;
608
609 if (ret)
610 break;
611
612 if (o->maxval && il > (int) o->maxval) {
613 fprintf(stderr, "max value out of range: %d (%d max)\n",
614 il, o->maxval);
615 return 1;
616 }
617 if (o->minval && il < o->minval) {
618 fprintf(stderr, "min value out of range: %d (%d min)\n",
619 il, o->minval);
620 return 1;
621 }
622
623 if (o->neg)
624 il = !il;
625
626 if (fn)
627 ret = fn(data, &il);
628 else {
629 if (first) {
630 if (o->roff1)
631 *(unsigned int *)o->roff1 = il;
632 else
633 val_store(ilp, il, o->off1, 0, data);
634 }
635 if (!more) {
636 if (o->roff2)
637 *(unsigned int *) o->roff2 = il;
638 else if (o->off2)
639 val_store(ilp, il, o->off2, 0, data);
640 }
641 }
642 break;
643 }
644 case FIO_OPT_DEPRECATED:
645 log_info("Option %s is deprecated\n", o->name);
646 break;
647 default:
648 log_err("Bad option type %u\n", o->type);
649 ret = 1;
650 }
651
652 if (ret)
653 return ret;
654
655 if (o->verify) {
656 ret = o->verify(o, data);
657 if (ret) {
658 fprintf(stderr,"Correct format for offending option\n");
659 fprintf(stderr, "%20s: %s\n", o->name, o->help);
660 show_option_help(o, 1);
661 }
662 }
663
664 return ret;
665}
666
667static int handle_option(struct fio_option *o, const char *__ptr, void *data)
668{
669 char *o_ptr, *ptr, *ptr2;
670 int ret, done;
671
672 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
673
674 o_ptr = ptr = NULL;
675 if (__ptr)
676 o_ptr = ptr = strdup(__ptr);
677
678 /*
679 * See if we have another set of parameters, hidden after a comma.
680 * Do this before parsing this round, to check if we should
681 * copy set 1 options to set 2.
682 */
683 done = 0;
684 ret = 1;
685 do {
686 int __ret;
687
688 ptr2 = NULL;
689 if (ptr &&
690 (o->type != FIO_OPT_STR_STORE) &&
691 (o->type != FIO_OPT_STR) &&
692 (o->type != FIO_OPT_FLOAT_LIST)) {
693 ptr2 = strchr(ptr, ',');
694 if (ptr2 && *(ptr2 + 1) == '\0')
695 *ptr2 = '\0';
696 if (o->type != FIO_OPT_STR_MULTI) {
697 if (!ptr2)
698 ptr2 = strchr(ptr, ':');
699 if (!ptr2)
700 ptr2 = strchr(ptr, '-');
701 }
702 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
703 ptr2 = strchr(ptr, ':');
704 }
705
706 /*
707 * Don't return early if parsing the first option fails - if
708 * we are doing multiple arguments, we can allow the first one
709 * being empty.
710 */
711 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
712 if (ret)
713 ret = __ret;
714
715 if (!ptr2)
716 break;
717
718 ptr = ptr2 + 1;
719 done++;
720 } while (1);
721
722 if (o_ptr)
723 free(o_ptr);
724 return ret;
725}
726
727static struct fio_option *get_option(const char *opt,
728 struct fio_option *options, char **post)
729{
730 struct fio_option *o;
731 char *ret;
732
733 ret = strchr(opt, '=');
734 if (ret) {
735 *post = ret;
736 *ret = '\0';
737 ret = (char *) opt;
738 (*post)++;
739 strip_blank_end(ret);
740 o = find_option(options, ret);
741 } else {
742 o = find_option(options, opt);
743 *post = NULL;
744 }
745
746 return o;
747}
748
749static int opt_cmp(const void *p1, const void *p2)
750{
751 struct fio_option *o1, *o2;
752 char *s1, *s2, *foo;
753 int prio1, prio2;
754
755 s1 = strdup(*((char **) p1));
756 s2 = strdup(*((char **) p2));
757
758 o1 = get_option(s1, fio_options, &foo);
759 o2 = get_option(s2, fio_options, &foo);
760
761 prio1 = prio2 = 0;
762 if (o1)
763 prio1 = o1->prio;
764 if (o2)
765 prio2 = o2->prio;
766
767 free(s1);
768 free(s2);
769 return prio2 - prio1;
770}
771
772void sort_options(char **opts, struct fio_option *options, int num_opts)
773{
774 fio_options = options;
775 qsort(opts, num_opts, sizeof(char *), opt_cmp);
776 fio_options = NULL;
777}
778
779int parse_cmd_option(const char *opt, const char *val,
780 struct fio_option *options, void *data)
781{
782 struct fio_option *o;
783
784 o = find_option(options, opt);
785 if (!o) {
786 log_err("Bad option <%s>\n", opt);
787 return 1;
788 }
789
790 if (!handle_option(o, val, data))
791 return 0;
792
793 log_err("fio: failed parsing %s=%s\n", opt, val);
794 return 1;
795}
796
797/*
798 * Return a copy of the input string with substrings of the form ${VARNAME}
799 * substituted with the value of the environment variable VARNAME. The
800 * substitution always occurs, even if VARNAME is empty or the corresponding
801 * environment variable undefined.
802 */
803static char *option_dup_subs(const char *opt)
804{
805 char out[OPT_LEN_MAX+1];
806 char in[OPT_LEN_MAX+1];
807 char *outptr = out;
808 char *inptr = in;
809 char *ch1, *ch2, *env;
810 ssize_t nchr = OPT_LEN_MAX;
811 size_t envlen;
812
813 if (strlen(opt) + 1 > OPT_LEN_MAX) {
814 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
815 return NULL;
816 }
817
818 in[OPT_LEN_MAX] = '\0';
819 strncpy(in, opt, OPT_LEN_MAX);
820
821 while (*inptr && nchr > 0) {
822 if (inptr[0] == '$' && inptr[1] == '{') {
823 ch2 = strchr(inptr, '}');
824 if (ch2 && inptr+1 < ch2) {
825 ch1 = inptr+2;
826 inptr = ch2+1;
827 *ch2 = '\0';
828
829 env = getenv(ch1);
830 if (env) {
831 envlen = strlen(env);
832 if (envlen <= nchr) {
833 memcpy(outptr, env, envlen);
834 outptr += envlen;
835 nchr -= envlen;
836 }
837 }
838
839 continue;
840 }
841 }
842
843 *outptr++ = *inptr++;
844 --nchr;
845 }
846
847 *outptr = '\0';
848 return strdup(out);
849}
850
851int parse_option(const char *opt, struct fio_option *options, void *data)
852{
853 struct fio_option *o;
854 char *post, *tmp;
855
856 tmp = option_dup_subs(opt);
857 if (!tmp)
858 return 1;
859
860 o = get_option(tmp, options, &post);
861 if (!o) {
862 log_err("Bad option <%s>\n", tmp);
863 free(tmp);
864 return 1;
865 }
866
867 if (!handle_option(o, post, data)) {
868 free(tmp);
869 return 0;
870 }
871
872 log_err("fio: failed parsing %s\n", opt);
873 free(tmp);
874 return 1;
875}
876
877/*
878 * Option match, levenshtein distance. Handy for not quite remembering what
879 * the option name is.
880 */
881static int string_distance(const char *s1, const char *s2)
882{
883 unsigned int s1_len = strlen(s1);
884 unsigned int s2_len = strlen(s2);
885 unsigned int *p, *q, *r;
886 unsigned int i, j;
887
888 p = malloc(sizeof(unsigned int) * (s2_len + 1));
889 q = malloc(sizeof(unsigned int) * (s2_len + 1));
890
891 p[0] = 0;
892 for (i = 1; i <= s2_len; i++)
893 p[i] = p[i - 1] + 1;
894
895 for (i = 1; i <= s1_len; i++) {
896 q[0] = p[0] + 1;
897 for (j = 1; j <= s2_len; j++) {
898 unsigned int sub = p[j - 1];
899
900 if (s1[i - 1] != s2[j - 1])
901 sub++;
902
903 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
904 }
905 r = p;
906 p = q;
907 q = r;
908 }
909
910 i = p[s2_len];
911 free(p);
912 free(q);
913 return i;
914}
915
916static struct fio_option *find_child(struct fio_option *options,
917 struct fio_option *o)
918{
919 struct fio_option *__o;
920
921 for (__o = options + 1; __o->name; __o++)
922 if (__o->parent && !strcmp(__o->parent, o->name))
923 return __o;
924
925 return NULL;
926}
927
928static void __print_option(struct fio_option *o, struct fio_option *org,
929 int level)
930{
931 char name[256], *p;
932 int depth;
933
934 if (!o)
935 return;
936 if (!org)
937 org = o;
938
939 p = name;
940 depth = level;
941 while (depth--)
942 p += sprintf(p, "%s", " ");
943
944 sprintf(p, "%s", o->name);
945
946 printf("%-24s: %s\n", name, o->help);
947}
948
949static void print_option(struct fio_option *o)
950{
951 struct fio_option *parent;
952 struct fio_option *__o;
953 unsigned int printed;
954 unsigned int level;
955
956 __print_option(o, NULL, 0);
957 parent = o;
958 level = 0;
959 do {
960 level++;
961 printed = 0;
962
963 while ((__o = find_child(o, parent)) != NULL) {
964 __print_option(__o, o, level);
965 o = __o;
966 printed++;
967 }
968
969 parent = o;
970 } while (printed);
971}
972
973int show_cmd_help(struct fio_option *options, const char *name)
974{
975 struct fio_option *o, *closest;
976 unsigned int best_dist = -1U;
977 int found = 0;
978 int show_all = 0;
979
980 if (!name || !strcmp(name, "all"))
981 show_all = 1;
982
983 closest = NULL;
984 best_dist = -1;
985 for (o = &options[0]; o->name; o++) {
986 int match = 0;
987
988 if (o->type == FIO_OPT_DEPRECATED)
989 continue;
990 if (!exec_profile && o->prof_name)
991 continue;
992
993 if (name) {
994 if (!strcmp(name, o->name) ||
995 (o->alias && !strcmp(name, o->alias)))
996 match = 1;
997 else {
998 unsigned int dist;
999
1000 dist = string_distance(name, o->name);
1001 if (dist < best_dist) {
1002 best_dist = dist;
1003 closest = o;
1004 }
1005 }
1006 }
1007
1008 if (show_all || match) {
1009 found = 1;
1010 if (match)
1011 printf("%20s: %s\n", o->name, o->help);
1012 if (show_all) {
1013 if (!o->parent)
1014 print_option(o);
1015 continue;
1016 }
1017 }
1018
1019 if (!match)
1020 continue;
1021
1022 show_option_help(o, 0);
1023 }
1024
1025 if (found)
1026 return 0;
1027
1028 printf("No such command: %s", name);
1029
1030 /*
1031 * Only print an appropriately close option, one where the edit
1032 * distance isn't too big. Otherwise we get crazy matches.
1033 */
1034 if (closest && best_dist < 3) {
1035 printf(" - showing closest match\n");
1036 printf("%20s: %s\n", closest->name, closest->help);
1037 show_option_help(closest, 0);
1038 } else
1039 printf("\n");
1040
1041 return 1;
1042}
1043
1044/*
1045 * Handle parsing of default parameters.
1046 */
1047void fill_default_options(void *data, struct fio_option *options)
1048{
1049 struct fio_option *o;
1050
1051 dprint(FD_PARSE, "filling default options\n");
1052
1053 for (o = &options[0]; o->name; o++)
1054 if (o->def)
1055 handle_option(o, o->def, data);
1056}
1057
1058void option_init(struct fio_option *o)
1059{
1060 if (o->type == FIO_OPT_DEPRECATED)
1061 return;
1062 if (o->type == FIO_OPT_BOOL) {
1063 o->minval = 0;
1064 o->maxval = 1;
1065 }
1066 if (o->type == FIO_OPT_FLOAT_LIST) {
1067 o->minfp = NAN;
1068 o->maxfp = NAN;
1069 }
1070 if (o->type == FIO_OPT_STR_SET && o->def) {
1071 log_err("Option %s: string set option with"
1072 " default will always be true\n", o->name);
1073 }
1074 if (!o->cb && (!o->off1 && !o->roff1))
1075 log_err("Option %s: neither cb nor offset given\n", o->name);
1076 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1077 o->type == FIO_OPT_STR_MULTI)
1078 return;
1079 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
1080 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
1081 log_err("Option %s: both cb and offset given\n", o->name);
1082 }
1083}
1084
1085/*
1086 * Sanitize the options structure. For now it just sets min/max for bool
1087 * values and whether both callback and offsets are given.
1088 */
1089void options_init(struct fio_option *options)
1090{
1091 struct fio_option *o;
1092
1093 dprint(FD_PARSE, "init options\n");
1094
1095 for (o = &options[0]; o->name; o++)
1096 option_init(o);
1097}