2 * This file contains the ini and command liner parser main.
17 static struct fio_option *fio_options;
18 extern unsigned int fio_get_kb_base(void *);
20 static int vp_cmp(const void *p1, const void *p2)
22 const struct value_pair *vp1 = p1;
23 const struct value_pair *vp2 = p2;
25 return strlen(vp2->ival) - strlen(vp1->ival);
28 static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
30 const struct value_pair *vp;
33 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
35 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
36 vp = &o->posval[entries];
37 if (!vp->ival || vp->ival[0] == '\0')
40 memcpy(&vpmap[entries], vp, sizeof(*vp));
43 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
46 static void show_option_range(struct fio_option *o, FILE *out)
48 if (!o->minval && !o->maxval)
51 fprintf(out, "%20s: min=%d", "range", o->minval);
53 fprintf(out, ", max=%d", o->maxval);
57 static void show_option_values(struct fio_option *o)
61 for (i = 0; i < PARSE_MAX_VP; i++) {
62 const struct value_pair *vp = &o->posval[i];
67 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
69 printf(" %s", vp->help);
77 static void show_option_help(struct fio_option *o, FILE *out)
79 const char *typehelp[] = {
83 "string with possible k/m/g postfix (opt=4k)",
84 "string with time postfix (opt=10s)",
86 "string with dual range (opt=1k-4k,4k-8k)",
87 "integer value (opt=100)",
88 "boolean value (opt=1)",
94 fprintf(out, "%20s: %s\n", "alias", o->alias);
96 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
97 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
99 fprintf(out, "%20s: only for profile '%s'\n", "valid", o->prof_name);
100 show_option_range(o, stdout);
101 show_option_values(o);
104 static unsigned long get_mult_time(char c)
121 static unsigned long long __get_mult_bytes(const char *p, void *data)
123 unsigned int kb_base = fio_get_kb_base(data);
124 unsigned long long ret = 1;
125 unsigned int i, pow = 0, mult = kb_base;
133 for (i = 0; i < strlen(c); i++)
134 c[i] = tolower(c[i]);
136 if (!strcmp("pib", c)) {
139 } else if (!strcmp("tib", c)) {
142 } else if (!strcmp("gib", c)) {
145 } else if (!strcmp("mib", c)) {
148 } else if (!strcmp("kib", c)) {
151 } else if (!strcmp("p", c) || !strcmp("pb", c))
153 else if (!strcmp("t", c) || !strcmp("tb", c))
155 else if (!strcmp("g", c) || !strcmp("gb", c))
157 else if (!strcmp("m", c) || !strcmp("mb", c))
159 else if (!strcmp("k", c) || !strcmp("kb", c))
163 ret *= (unsigned long long) mult;
169 static unsigned long long get_mult_bytes(const char *str, int len, void *data)
174 return __get_mult_bytes(str, data);
177 * Go forward until we hit a non-digit
179 while ((p - str) <= len) {
188 return __get_mult_bytes(p, data);
192 * convert string into decimal value, noting any size suffix
194 int str_to_decimal(const char *str, long long *val, int kilo, void *data)
202 if (strstr(str, "0x") || strstr(str, "0X"))
207 *val = strtoll(str, NULL, base);
208 if (*val == LONG_MAX && errno == ERANGE)
212 *val *= get_mult_bytes(str, len, data);
214 *val *= get_mult_time(str[len - 1]);
219 static int check_str_bytes(const char *p, long long *val, void *data)
221 return str_to_decimal(p, val, 1, data);
224 static int check_str_time(const char *p, long long *val)
226 return str_to_decimal(p, val, 0, NULL);
229 void strip_blank_front(char **p)
239 void strip_blank_end(char *p)
253 while ((isspace(*s) || iscntrl(*s)) && (s > start))
259 static int check_range_bytes(const char *str, long *val, void *data)
263 if (!str_to_decimal(str, &__val, 1, data)) {
271 static int check_int(const char *p, int *val)
275 if (strstr(p, "0x") || strstr(p, "0X")) {
276 if (sscanf(p, "%x", val) == 1)
279 if (sscanf(p, "%u", val) == 1)
286 static int opt_len(const char *str)
290 postfix = strchr(str, ':');
294 return (int)(postfix - str);
297 #define val_store(ptr, val, off, or, data) \
299 ptr = td_var((data), (off)); \
306 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
310 long long ull, *ullp;
313 int ret = 0, is_time = 0;
315 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
318 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
319 fprintf(stderr, "Option %s requires an argument\n", o->name);
325 case FIO_OPT_STR_MULTI: {
326 fio_opt_str_fn *fn = o->cb;
327 const struct value_pair *vp;
328 struct value_pair posval[PARSE_MAX_VP];
329 int i, all_skipped = 1;
331 posval_sort(o, posval);
334 for (i = 0; i < PARSE_MAX_VP; i++) {
336 if (!vp->ival || vp->ival[0] == '\0')
339 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
343 *(unsigned int *) o->roff1 |= vp->oval;
345 *(unsigned int *) o->roff1 = vp->oval;
349 val_store(ilp, vp->oval, o->off1, vp->or, data);
355 if (ret && !all_skipped)
356 show_option_values(o);
361 case FIO_OPT_STR_VAL_TIME:
364 case FIO_OPT_STR_VAL: {
365 fio_opt_str_val_fn *fn = o->cb;
368 ret = check_str_time(ptr, &ull);
370 ret = check_str_bytes(ptr, &ull, data);
375 if (o->maxval && ull > o->maxval) {
376 fprintf(stderr, "max value out of range: %lld"
377 " (%d max)\n", ull, o->maxval);
380 if (o->minval && ull < o->minval) {
381 fprintf(stderr, "min value out of range: %lld"
382 " (%d min)\n", ull, o->minval);
387 ret = fn(data, &ull);
389 if (o->type == FIO_OPT_INT) {
392 *(unsigned int *) o->roff1 = ull;
394 val_store(ilp, ull, o->off1, 0, data);
398 *(unsigned int *) o->roff2 = ull;
400 val_store(ilp, ull, o->off2, 0, data);
405 *(unsigned long long *) o->roff1 = ull;
407 val_store(ullp, ull, o->off1, 0, data);
411 *(unsigned long long *) o->roff2 = ull;
413 val_store(ullp, ull, o->off2, 0, data);
419 case FIO_OPT_STR_STORE: {
420 fio_opt_str_fn *fn = o->cb;
423 cp = (char **) o->roff1;
425 cp = td_var(data, o->off1);
437 case FIO_OPT_RANGE: {
441 strncpy(tmp, ptr, sizeof(tmp) - 1);
443 p1 = strchr(tmp, '-');
445 p1 = strchr(tmp, ':');
457 if (!check_range_bytes(p1, &ul1, data) &&
458 !check_range_bytes(p2, &ul2, data)) {
461 unsigned long foo = ul1;
469 *(unsigned int *) o->roff1 = ul1;
471 val_store(ilp, ul1, o->off1, 0, data);
473 *(unsigned int *) o->roff2 = ul2;
475 val_store(ilp, ul2, o->off2, 0, data);
477 if (o->roff3 && o->roff4) {
478 *(unsigned int *) o->roff3 = ul1;
479 *(unsigned int *) o->roff4 = ul2;
480 } else if (o->off3 && o->off4) {
481 val_store(ilp, ul1, o->off3, 0, data);
482 val_store(ilp, ul2, o->off4, 0, data);
489 fio_opt_int_fn *fn = o->cb;
491 ret = check_int(ptr, &il);
495 if (o->maxval && il > (int) o->maxval) {
496 fprintf(stderr, "max value out of range: %d (%d max)\n",
500 if (o->minval && il < o->minval) {
501 fprintf(stderr, "min value out of range: %d (%d min)\n",
514 *(unsigned int *)o->roff1 = il;
516 val_store(ilp, il, o->off1, 0, data);
520 *(unsigned int *) o->roff2 = il;
522 val_store(ilp, il, o->off2, 0, data);
527 case FIO_OPT_STR_SET: {
528 fio_opt_str_set_fn *fn = o->cb;
535 *(unsigned int *) o->roff1 = 1;
537 val_store(ilp, 1, o->off1, 0, data);
541 *(unsigned int *) o->roff2 = 1;
543 val_store(ilp, 1, o->off2, 0, data);
548 case FIO_OPT_DEPRECATED:
549 fprintf(stdout, "Option %s is deprecated\n", o->name);
552 fprintf(stderr, "Bad option type %u\n", o->type);
560 ret = o->verify(o, data);
562 fprintf(stderr,"Correct format for offending option\n");
563 fprintf(stderr, "%20s: %s\n", o->name, o->help);
564 show_option_help(o, stderr);
571 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
573 char *o_ptr, *ptr, *ptr2;
576 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
580 o_ptr = ptr = strdup(__ptr);
583 * See if we have another set of parameters, hidden after a comma.
584 * Do this before parsing this round, to check if we should
585 * copy set 1 options to set 2.
594 (o->type != FIO_OPT_STR_STORE) &&
595 (o->type != FIO_OPT_STR)) {
596 ptr2 = strchr(ptr, ',');
597 if (ptr2 && *(ptr2 + 1) == '\0')
599 if (o->type != FIO_OPT_STR_MULTI) {
601 ptr2 = strchr(ptr, ':');
603 ptr2 = strchr(ptr, '-');
608 * Don't return early if parsing the first option fails - if
609 * we are doing multiple arguments, we can allow the first one
612 __ret = __handle_option(o, ptr, data, !done, !!ptr2);
628 static struct fio_option *get_option(const char *opt,
629 struct fio_option *options, char **post)
631 struct fio_option *o;
634 ret = strchr(opt, '=');
640 strip_blank_end(ret);
641 o = find_option(options, ret);
643 o = find_option(options, opt);
650 static int opt_cmp(const void *p1, const void *p2)
652 struct fio_option *o1, *o2;
656 s1 = strdup(*((char **) p1));
657 s2 = strdup(*((char **) p2));
659 o1 = get_option(s1, fio_options, &foo);
660 o2 = get_option(s2, fio_options, &foo);
670 return prio2 - prio1;
673 void sort_options(char **opts, struct fio_option *options, int num_opts)
675 fio_options = options;
676 qsort(opts, num_opts, sizeof(char *), opt_cmp);
680 int parse_cmd_option(const char *opt, const char *val,
681 struct fio_option *options, void *data)
683 struct fio_option *o;
685 o = find_option(options, opt);
687 fprintf(stderr, "Bad option <%s>\n", opt);
691 if (!handle_option(o, val, data))
694 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
699 * Return a copy of the input string with substrings of the form ${VARNAME}
700 * substituted with the value of the environment variable VARNAME. The
701 * substitution always occurs, even if VARNAME is empty or the corresponding
702 * environment variable undefined.
704 static char *option_dup_subs(const char *opt)
706 char out[OPT_LEN_MAX+1];
707 char in[OPT_LEN_MAX+1];
710 char *ch1, *ch2, *env;
711 ssize_t nchr = OPT_LEN_MAX;
714 if (strlen(opt) + 1 > OPT_LEN_MAX) {
715 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
719 in[OPT_LEN_MAX] = '\0';
720 strncpy(in, opt, OPT_LEN_MAX);
722 while (*inptr && nchr > 0) {
723 if (inptr[0] == '$' && inptr[1] == '{') {
724 ch2 = strchr(inptr, '}');
725 if (ch2 && inptr+1 < ch2) {
732 envlen = strlen(env);
733 if (envlen <= nchr) {
734 memcpy(outptr, env, envlen);
744 *outptr++ = *inptr++;
752 int parse_option(const char *opt, struct fio_option *options, void *data)
754 struct fio_option *o;
757 tmp = option_dup_subs(opt);
761 o = get_option(tmp, options, &post);
763 fprintf(stderr, "Bad option <%s>\n", tmp);
768 if (!handle_option(o, post, data)) {
773 fprintf(stderr, "fio: failed parsing %s\n", opt);
779 * Option match, levenshtein distance. Handy for not quite remembering what
780 * the option name is.
782 static int string_distance(const char *s1, const char *s2)
784 unsigned int s1_len = strlen(s1);
785 unsigned int s2_len = strlen(s2);
786 unsigned int *p, *q, *r;
789 p = malloc(sizeof(unsigned int) * (s2_len + 1));
790 q = malloc(sizeof(unsigned int) * (s2_len + 1));
793 for (i = 1; i <= s2_len; i++)
796 for (i = 1; i <= s1_len; i++) {
798 for (j = 1; j <= s2_len; j++) {
799 unsigned int sub = p[j - 1];
801 if (s1[i - 1] != s2[j - 1])
804 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
817 static struct fio_option *find_child(struct fio_option *options,
818 struct fio_option *o)
820 struct fio_option *__o;
822 for (__o = options + 1; __o->name; __o++)
823 if (__o->parent && !strcmp(__o->parent, o->name))
829 static void __print_option(struct fio_option *o, struct fio_option *org,
843 p += sprintf(p, "%s", " ");
845 sprintf(p, "%s", o->name);
847 printf("%-24s: %s\n", name, o->help);
850 static void print_option(struct fio_option *o)
852 struct fio_option *parent;
853 struct fio_option *__o;
854 unsigned int printed;
857 __print_option(o, NULL, 0);
864 while ((__o = find_child(o, parent)) != NULL) {
865 __print_option(__o, o, level);
874 int show_cmd_help(struct fio_option *options, const char *name)
876 struct fio_option *o, *closest;
877 unsigned int best_dist = -1U;
881 if (!name || !strcmp(name, "all"))
886 for (o = &options[0]; o->name; o++) {
889 if (o->type == FIO_OPT_DEPRECATED)
891 if (!exec_profile && o->prof_name)
895 if (!strcmp(name, o->name) ||
896 (o->alias && !strcmp(name, o->alias)))
901 dist = string_distance(name, o->name);
902 if (dist < best_dist) {
909 if (show_all || match) {
912 printf("%20s: %s\n", o->name, o->help);
923 show_option_help(o, stdout);
929 printf("No such command: %s", name);
932 * Only print an appropriately close option, one where the edit
933 * distance isn't too big. Otherwise we get crazy matches.
935 if (closest && best_dist < 3) {
936 printf(" - showing closest match\n");
937 printf("%20s: %s\n", closest->name, closest->help);
938 show_option_help(closest, stdout);
946 * Handle parsing of default parameters.
948 void fill_default_options(void *data, struct fio_option *options)
950 struct fio_option *o;
952 dprint(FD_PARSE, "filling default options\n");
954 for (o = &options[0]; o->name; o++)
956 handle_option(o, o->def, data);
959 void option_init(struct fio_option *o)
961 if (o->type == FIO_OPT_DEPRECATED)
963 if (o->type == FIO_OPT_BOOL) {
967 if (o->type == FIO_OPT_STR_SET && o->def) {
968 fprintf(stderr, "Option %s: string set option with"
969 " default will always be true\n", o->name);
971 if (!o->cb && (!o->off1 && !o->roff1)) {
972 fprintf(stderr, "Option %s: neither cb nor offset given\n",
975 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
976 o->type == FIO_OPT_STR_MULTI)
978 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
979 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
980 fprintf(stderr, "Option %s: both cb and offset given\n",
986 * Sanitize the options structure. For now it just sets min/max for bool
987 * values and whether both callback and offsets are given.
989 void options_init(struct fio_option *options)
991 struct fio_option *o;
993 dprint(FD_PARSE, "init options\n");
995 for (o = &options[0]; o->name; o++)