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 * if the last char is 'b' or 'B', the user likely used
178 * "1gb" instead of just "1g". If the second to last is also
182 while (isalpha(*(p - 1)))
187 return __get_mult_bytes(p, data);
191 * convert string into decimal value, noting any size suffix
193 int str_to_decimal(const char *str, long long *val, int kilo, void *data)
201 if (strstr(str, "0x") || strstr(str, "0X"))
206 *val = strtoll(str, NULL, base);
207 if (*val == LONG_MAX && errno == ERANGE)
211 *val *= get_mult_bytes(str, len, data);
213 *val *= get_mult_time(str[len - 1]);
218 static int check_str_bytes(const char *p, long long *val, void *data)
220 return str_to_decimal(p, val, 1, data);
223 static int check_str_time(const char *p, long long *val)
225 return str_to_decimal(p, val, 0, NULL);
228 void strip_blank_front(char **p)
238 void strip_blank_end(char *p)
252 while ((isspace(*s) || iscntrl(*s)) && (s > start))
258 static int check_range_bytes(const char *str, long *val, void *data)
262 if (!str_to_decimal(str, &__val, 1, data)) {
270 static int check_int(const char *p, int *val)
274 if (strstr(p, "0x") || strstr(p, "0X")) {
275 if (sscanf(p, "%x", val) == 1)
278 if (sscanf(p, "%u", val) == 1)
285 static int opt_len(const char *str)
289 postfix = strchr(str, ':');
293 return (int)(postfix - str);
296 #define val_store(ptr, val, off, or, data) \
298 ptr = td_var((data), (off)); \
305 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
309 long long ull, *ullp;
314 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
317 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
318 fprintf(stderr, "Option %s requires an argument\n", o->name);
324 case FIO_OPT_STR_MULTI: {
325 fio_opt_str_fn *fn = o->cb;
326 const struct value_pair *vp;
327 struct value_pair posval[PARSE_MAX_VP];
328 int i, all_skipped = 1;
330 posval_sort(o, posval);
333 for (i = 0; i < PARSE_MAX_VP; i++) {
335 if (!vp->ival || vp->ival[0] == '\0')
338 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
342 *(unsigned int *) o->roff1 |= vp->oval;
344 *(unsigned int *) o->roff1 = vp->oval;
348 val_store(ilp, vp->oval, o->off1, vp->or, data);
354 if (ret && !all_skipped)
355 show_option_values(o);
360 case FIO_OPT_STR_VAL_TIME: {
361 fio_opt_str_val_fn *fn;
363 ret = check_str_time(ptr, &ull);
365 case FIO_OPT_STR_VAL:
367 ret = check_str_bytes(ptr, &ull, data);
371 if (o->maxval && ull > o->maxval) {
372 fprintf(stderr, "max value out of range: %lld"
373 " (%d max)\n", ull, o->maxval);
376 if (o->minval && ull < o->minval) {
377 fprintf(stderr, "min value out of range: %lld"
378 " (%d min)\n", ull, o->minval);
384 ret = fn(data, &ull);
386 if (o->type == FIO_OPT_INT) {
389 *(unsigned int *) o->roff1 = ull;
391 val_store(ilp, ull, o->off1, 0, data);
395 *(unsigned int *) o->roff2 = ull;
397 val_store(ilp, ull, o->off2, 0, data);
402 *(unsigned long long *) o->roff1 = ull;
404 val_store(ullp, ull, o->off1, 0, data);
408 *(unsigned long long *) o->roff2 = ull;
410 val_store(ullp, ull, o->off2, 0, data);
416 case FIO_OPT_STR_STORE: {
417 fio_opt_str_fn *fn = o->cb;
420 cp = (char **) o->roff1;
422 cp = td_var(data, o->off1);
434 case FIO_OPT_RANGE: {
438 strncpy(tmp, ptr, sizeof(tmp) - 1);
440 p1 = strchr(tmp, '-');
442 p1 = strchr(tmp, ':');
454 if (!check_range_bytes(p1, &ul1, data) &&
455 !check_range_bytes(p2, &ul2, data)) {
458 unsigned long foo = ul1;
466 *(unsigned int *) o->roff1 = ul1;
468 val_store(ilp, ul1, o->off1, 0, data);
470 *(unsigned int *) o->roff2 = ul2;
472 val_store(ilp, ul2, o->off2, 0, data);
474 if (o->roff3 && o->roff4) {
475 *(unsigned int *) o->roff3 = ul1;
476 *(unsigned int *) o->roff4 = ul2;
477 } else if (o->off3 && o->off4) {
478 val_store(ilp, ul1, o->off3, 0, data);
479 val_store(ilp, ul2, o->off4, 0, data);
486 fio_opt_int_fn *fn = o->cb;
488 ret = check_int(ptr, &il);
492 if (o->maxval && il > (int) o->maxval) {
493 fprintf(stderr, "max value out of range: %d (%d max)\n",
497 if (o->minval && il < o->minval) {
498 fprintf(stderr, "min value out of range: %d (%d min)\n",
511 *(unsigned int *)o->roff1 = il;
513 val_store(ilp, il, o->off1, 0, data);
517 *(unsigned int *) o->roff2 = il;
519 val_store(ilp, il, o->off2, 0, data);
524 case FIO_OPT_STR_SET: {
525 fio_opt_str_set_fn *fn = o->cb;
532 *(unsigned int *) o->roff1 = 1;
534 val_store(ilp, 1, o->off1, 0, data);
538 *(unsigned int *) o->roff2 = 1;
540 val_store(ilp, 1, o->off2, 0, data);
545 case FIO_OPT_DEPRECATED:
546 fprintf(stdout, "Option %s is deprecated\n", o->name);
549 fprintf(stderr, "Bad option type %u\n", o->type);
557 ret = o->verify(o, data);
559 fprintf(stderr,"Correct format for offending option\n");
560 fprintf(stderr, "%20s: %s\n", o->name, o->help);
561 show_option_help(o, stderr);
568 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
570 char *o_ptr, *ptr, *ptr2;
573 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
577 o_ptr = ptr = strdup(__ptr);
580 * See if we have another set of parameters, hidden after a comma.
581 * Do this before parsing this round, to check if we should
582 * copy set 1 options to set 2.
591 (o->type != FIO_OPT_STR_STORE) &&
592 (o->type != FIO_OPT_STR)) {
593 ptr2 = strchr(ptr, ',');
594 if (ptr2 && *(ptr2 + 1) == '\0')
596 if (o->type != FIO_OPT_STR_MULTI) {
598 ptr2 = strchr(ptr, ':');
600 ptr2 = strchr(ptr, '-');
605 * Don't return early if parsing the first option fails - if
606 * we are doing multiple arguments, we can allow the first one
609 __ret = __handle_option(o, ptr, data, !done, !!ptr2);
625 static struct fio_option *get_option(const char *opt,
626 struct fio_option *options, char **post)
628 struct fio_option *o;
631 ret = strchr(opt, '=');
637 strip_blank_end(ret);
638 o = find_option(options, ret);
640 o = find_option(options, opt);
647 static int opt_cmp(const void *p1, const void *p2)
649 struct fio_option *o1, *o2;
653 s1 = strdup(*((char **) p1));
654 s2 = strdup(*((char **) p2));
656 o1 = get_option(s1, fio_options, &foo);
657 o2 = get_option(s2, fio_options, &foo);
667 return prio2 - prio1;
670 void sort_options(char **opts, struct fio_option *options, int num_opts)
672 fio_options = options;
673 qsort(opts, num_opts, sizeof(char *), opt_cmp);
677 int parse_cmd_option(const char *opt, const char *val,
678 struct fio_option *options, void *data)
680 struct fio_option *o;
682 o = find_option(options, opt);
684 fprintf(stderr, "Bad option <%s>\n", opt);
688 if (!handle_option(o, val, data))
691 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
696 * Return a copy of the input string with substrings of the form ${VARNAME}
697 * substituted with the value of the environment variable VARNAME. The
698 * substitution always occurs, even if VARNAME is empty or the corresponding
699 * environment variable undefined.
701 static char *option_dup_subs(const char *opt)
703 char out[OPT_LEN_MAX+1];
704 char in[OPT_LEN_MAX+1];
707 char *ch1, *ch2, *env;
708 ssize_t nchr = OPT_LEN_MAX;
711 if (strlen(opt) + 1 > OPT_LEN_MAX) {
712 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
716 in[OPT_LEN_MAX] = '\0';
717 strncpy(in, opt, OPT_LEN_MAX);
719 while (*inptr && nchr > 0) {
720 if (inptr[0] == '$' && inptr[1] == '{') {
721 ch2 = strchr(inptr, '}');
722 if (ch2 && inptr+1 < ch2) {
729 envlen = strlen(env);
730 if (envlen <= nchr) {
731 memcpy(outptr, env, envlen);
741 *outptr++ = *inptr++;
749 int parse_option(const char *opt, struct fio_option *options, void *data)
751 struct fio_option *o;
754 tmp = option_dup_subs(opt);
758 o = get_option(tmp, options, &post);
760 fprintf(stderr, "Bad option <%s>\n", tmp);
765 if (!handle_option(o, post, data)) {
770 fprintf(stderr, "fio: failed parsing %s\n", opt);
776 * Option match, levenshtein distance. Handy for not quite remembering what
777 * the option name is.
779 static int string_distance(const char *s1, const char *s2)
781 unsigned int s1_len = strlen(s1);
782 unsigned int s2_len = strlen(s2);
783 unsigned int *p, *q, *r;
786 p = malloc(sizeof(unsigned int) * (s2_len + 1));
787 q = malloc(sizeof(unsigned int) * (s2_len + 1));
790 for (i = 1; i <= s2_len; i++)
793 for (i = 1; i <= s1_len; i++) {
795 for (j = 1; j <= s2_len; j++) {
796 unsigned int sub = p[j - 1];
798 if (s1[i - 1] != s2[j - 1])
801 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
814 static struct fio_option *find_child(struct fio_option *options,
815 struct fio_option *o)
817 struct fio_option *__o;
819 for (__o = options + 1; __o->name; __o++)
820 if (__o->parent && !strcmp(__o->parent, o->name))
826 static void __print_option(struct fio_option *o, struct fio_option *org,
840 p += sprintf(p, "%s", " ");
842 sprintf(p, "%s", o->name);
844 printf("%-24s: %s\n", name, o->help);
847 static void print_option(struct fio_option *o)
849 struct fio_option *parent;
850 struct fio_option *__o;
851 unsigned int printed;
854 __print_option(o, NULL, 0);
861 while ((__o = find_child(o, parent)) != NULL) {
862 __print_option(__o, o, level);
871 int show_cmd_help(struct fio_option *options, const char *name)
873 struct fio_option *o, *closest;
874 unsigned int best_dist = -1U;
878 if (!name || !strcmp(name, "all"))
883 for (o = &options[0]; o->name; o++) {
886 if (o->type == FIO_OPT_DEPRECATED)
888 if (!exec_profile && o->prof_name)
892 if (!strcmp(name, o->name) ||
893 (o->alias && !strcmp(name, o->alias)))
898 dist = string_distance(name, o->name);
899 if (dist < best_dist) {
906 if (show_all || match) {
909 printf("%20s: %s\n", o->name, o->help);
920 show_option_help(o, stdout);
926 printf("No such command: %s", name);
929 * Only print an appropriately close option, one where the edit
930 * distance isn't too big. Otherwise we get crazy matches.
932 if (closest && best_dist < 3) {
933 printf(" - showing closest match\n");
934 printf("%20s: %s\n", closest->name, closest->help);
935 show_option_help(closest, stdout);
943 * Handle parsing of default parameters.
945 void fill_default_options(void *data, struct fio_option *options)
947 struct fio_option *o;
949 dprint(FD_PARSE, "filling default options\n");
951 for (o = &options[0]; o->name; o++)
953 handle_option(o, o->def, data);
956 void option_init(struct fio_option *o)
958 if (o->type == FIO_OPT_DEPRECATED)
960 if (o->type == FIO_OPT_BOOL) {
964 if (o->type == FIO_OPT_STR_SET && o->def) {
965 fprintf(stderr, "Option %s: string set option with"
966 " default will always be true\n", o->name);
968 if (!o->cb && (!o->off1 && !o->roff1)) {
969 fprintf(stderr, "Option %s: neither cb nor offset given\n",
972 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
973 o->type == FIO_OPT_STR_MULTI)
975 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
976 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
977 fprintf(stderr, "Option %s: both cb and offset given\n",
983 * Sanitize the options structure. For now it just sets min/max for bool
984 * values and whether both callback and offsets are given.
986 void options_init(struct fio_option *options)
988 struct fio_option *o;
990 dprint(FD_PARSE, "init options\n");
992 for (o = &options[0]; o->name; o++)