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[] = {
82 "string with possible k/m/g postfix (opt=4k)",
83 "string with time postfix (opt=10s)",
85 "string with dual range (opt=1k-4k,4k-8k)",
86 "integer value (opt=100)",
87 "boolean value (opt=1)",
93 fprintf(out, "%20s: %s\n", "alias", o->alias);
95 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
96 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
98 fprintf(out, "%20s: only for profile '%s'\n", "valid", o->prof_name);
99 show_option_range(o, stdout);
100 show_option_values(o);
103 static unsigned long get_mult_time(char c)
120 static unsigned long long __get_mult_bytes(const char *p, void *data)
122 unsigned int kb_base = fio_get_kb_base(data);
123 unsigned long long ret = 1;
124 unsigned int i, pow = 0, mult = kb_base;
132 for (i = 0; i < strlen(c); i++)
133 c[i] = tolower(c[i]);
135 if (!strcmp("pib", c)) {
138 } else if (!strcmp("tib", c)) {
141 } else if (!strcmp("gib", c)) {
144 } else if (!strcmp("mib", c)) {
147 } else if (!strcmp("kib", c)) {
150 } else if (!strcmp("p", c) || !strcmp("pb", c))
152 else if (!strcmp("t", c) || !strcmp("tb", c))
154 else if (!strcmp("g", c) || !strcmp("gb", c))
156 else if (!strcmp("m", c) || !strcmp("mb", c))
158 else if (!strcmp("k", c) || !strcmp("kb", c))
162 ret *= (unsigned long long) mult;
168 static unsigned long long get_mult_bytes(const char *str, int len, void *data)
173 * if the last char is 'b' or 'B', the user likely used
174 * "1gb" instead of just "1g". If the second to last is also
178 while (isalpha(*(p - 1)))
183 return __get_mult_bytes(p, data);
187 * convert string into decimal value, noting any size suffix
189 int str_to_decimal(const char *str, long long *val, int kilo, void *data)
197 if (strstr(str, "0x") || strstr(str, "0X"))
202 *val = strtoll(str, NULL, base);
203 if (*val == LONG_MAX && errno == ERANGE)
207 *val *= get_mult_bytes(str, len, data);
209 *val *= get_mult_time(str[len - 1]);
214 static int check_str_bytes(const char *p, long long *val, void *data)
216 return str_to_decimal(p, val, 1, data);
219 static int check_str_time(const char *p, long long *val)
221 return str_to_decimal(p, val, 0, NULL);
224 void strip_blank_front(char **p)
234 void strip_blank_end(char *p)
248 while ((isspace(*s) || iscntrl(*s)) && (s > start))
254 static int check_range_bytes(const char *str, long *val, void *data)
258 if (!str_to_decimal(str, &__val, 1, data)) {
266 static int check_int(const char *p, int *val)
270 if (strstr(p, "0x") || strstr(p, "0X")) {
271 if (sscanf(p, "%x", val) == 1)
274 if (sscanf(p, "%u", val) == 1)
281 static int opt_len(const char *str)
285 postfix = strchr(str, ':');
289 return (int)(postfix - str);
292 #define val_store(ptr, val, off, or, data) \
294 ptr = td_var((data), (off)); \
301 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
305 long long ull, *ullp;
308 int ret = 0, is_time = 0;
310 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
313 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
314 fprintf(stderr, "Option %s requires an argument\n", o->name);
320 case FIO_OPT_STR_MULTI: {
321 fio_opt_str_fn *fn = o->cb;
322 const struct value_pair *vp;
323 struct value_pair posval[PARSE_MAX_VP];
324 int i, all_skipped = 1;
326 posval_sort(o, posval);
329 for (i = 0; i < PARSE_MAX_VP; i++) {
331 if (!vp->ival || vp->ival[0] == '\0')
334 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
338 *(unsigned int *) o->roff1 |= vp->oval;
340 *(unsigned int *) o->roff1 = vp->oval;
344 val_store(ilp, vp->oval, o->off1, vp->or, data);
350 if (ret && !all_skipped)
351 show_option_values(o);
356 case FIO_OPT_STR_VAL_TIME:
359 case FIO_OPT_STR_VAL: {
360 fio_opt_str_val_fn *fn = o->cb;
363 ret = check_str_time(ptr, &ull);
365 ret = check_str_bytes(ptr, &ull, data);
370 if (o->maxval && ull > o->maxval) {
371 fprintf(stderr, "max value out of range: %lld"
372 " (%d max)\n", ull, o->maxval);
375 if (o->minval && ull < o->minval) {
376 fprintf(stderr, "min value out of range: %lld"
377 " (%d min)\n", ull, o->minval);
382 ret = fn(data, &ull);
384 if (o->type == FIO_OPT_INT) {
387 *(unsigned int *) o->roff1 = ull;
389 val_store(ilp, ull, o->off1, 0, data);
393 *(unsigned int *) o->roff2 = ull;
395 val_store(ilp, ull, o->off2, 0, data);
400 *(unsigned long long *) o->roff1 = ull;
402 val_store(ullp, ull, o->off1, 0, data);
406 *(unsigned long long *) o->roff2 = ull;
408 val_store(ullp, ull, o->off2, 0, data);
414 case FIO_OPT_STR_STORE: {
415 fio_opt_str_fn *fn = o->cb;
418 cp = (char **) o->roff1;
420 cp = td_var(data, o->off1);
432 case FIO_OPT_RANGE: {
436 strncpy(tmp, ptr, sizeof(tmp) - 1);
438 p1 = strchr(tmp, '-');
440 p1 = strchr(tmp, ':');
452 if (!check_range_bytes(p1, &ul1, data) &&
453 !check_range_bytes(p2, &ul2, data)) {
456 unsigned long foo = ul1;
464 *(unsigned int *) o->roff1 = ul1;
466 val_store(ilp, ul1, o->off1, 0, data);
468 *(unsigned int *) o->roff2 = ul2;
470 val_store(ilp, ul2, o->off2, 0, data);
472 if (o->roff3 && o->roff4) {
473 *(unsigned int *) o->roff3 = ul1;
474 *(unsigned int *) o->roff4 = ul2;
475 } else if (o->off3 && o->off4) {
476 val_store(ilp, ul1, o->off3, 0, data);
477 val_store(ilp, ul2, o->off4, 0, data);
484 fio_opt_int_fn *fn = o->cb;
486 ret = check_int(ptr, &il);
490 if (o->maxval && il > (int) o->maxval) {
491 fprintf(stderr, "max value out of range: %d (%d max)\n",
495 if (o->minval && il < o->minval) {
496 fprintf(stderr, "min value out of range: %d (%d min)\n",
509 *(unsigned int *)o->roff1 = il;
511 val_store(ilp, il, o->off1, 0, data);
515 *(unsigned int *) o->roff2 = il;
517 val_store(ilp, il, o->off2, 0, data);
522 case FIO_OPT_STR_SET: {
523 fio_opt_str_set_fn *fn = o->cb;
530 *(unsigned int *) o->roff1 = 1;
532 val_store(ilp, 1, o->off1, 0, data);
536 *(unsigned int *) o->roff2 = 1;
538 val_store(ilp, 1, o->off2, 0, data);
543 case FIO_OPT_DEPRECATED:
544 fprintf(stdout, "Option %s is deprecated\n", o->name);
547 fprintf(stderr, "Bad option type %u\n", o->type);
555 ret = o->verify(o, data);
557 fprintf(stderr,"Correct format for offending option\n");
558 fprintf(stderr, "%20s: %s\n", o->name, o->help);
559 show_option_help(o, stderr);
566 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
568 char *o_ptr, *ptr, *ptr2;
571 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
575 o_ptr = ptr = strdup(__ptr);
578 * See if we have another set of parameters, hidden after a comma.
579 * Do this before parsing this round, to check if we should
580 * copy set 1 options to set 2.
589 (o->type != FIO_OPT_STR_STORE) &&
590 (o->type != FIO_OPT_STR)) {
591 ptr2 = strchr(ptr, ',');
592 if (ptr2 && *(ptr2 + 1) == '\0')
594 if (o->type != FIO_OPT_STR_MULTI) {
596 ptr2 = strchr(ptr, ':');
598 ptr2 = strchr(ptr, '-');
603 * Don't return early if parsing the first option fails - if
604 * we are doing multiple arguments, we can allow the first one
607 __ret = __handle_option(o, ptr, data, !done, !!ptr2);
623 static struct fio_option *get_option(const char *opt,
624 struct fio_option *options, char **post)
626 struct fio_option *o;
629 ret = strchr(opt, '=');
635 strip_blank_end(ret);
636 o = find_option(options, ret);
638 o = find_option(options, opt);
645 static int opt_cmp(const void *p1, const void *p2)
647 struct fio_option *o1, *o2;
651 s1 = strdup(*((char **) p1));
652 s2 = strdup(*((char **) p2));
654 o1 = get_option(s1, fio_options, &foo);
655 o2 = get_option(s2, fio_options, &foo);
665 return prio2 - prio1;
668 void sort_options(char **opts, struct fio_option *options, int num_opts)
670 fio_options = options;
671 qsort(opts, num_opts, sizeof(char *), opt_cmp);
675 int parse_cmd_option(const char *opt, const char *val,
676 struct fio_option *options, void *data)
678 struct fio_option *o;
680 o = find_option(options, opt);
682 fprintf(stderr, "Bad option <%s>\n", opt);
686 if (!handle_option(o, val, data))
689 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
694 * Return a copy of the input string with substrings of the form ${VARNAME}
695 * substituted with the value of the environment variable VARNAME. The
696 * substitution always occurs, even if VARNAME is empty or the corresponding
697 * environment variable undefined.
699 static char *option_dup_subs(const char *opt)
701 char out[OPT_LEN_MAX+1];
702 char in[OPT_LEN_MAX+1];
705 char *ch1, *ch2, *env;
706 ssize_t nchr = OPT_LEN_MAX;
709 if (strlen(opt) + 1 > OPT_LEN_MAX) {
710 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
714 in[OPT_LEN_MAX] = '\0';
715 strncpy(in, opt, OPT_LEN_MAX);
717 while (*inptr && nchr > 0) {
718 if (inptr[0] == '$' && inptr[1] == '{') {
719 ch2 = strchr(inptr, '}');
720 if (ch2 && inptr+1 < ch2) {
727 envlen = strlen(env);
728 if (envlen <= nchr) {
729 memcpy(outptr, env, envlen);
739 *outptr++ = *inptr++;
747 int parse_option(const char *opt, struct fio_option *options, void *data)
749 struct fio_option *o;
752 tmp = option_dup_subs(opt);
756 o = get_option(tmp, options, &post);
758 fprintf(stderr, "Bad option <%s>\n", tmp);
763 if (!handle_option(o, post, data)) {
768 fprintf(stderr, "fio: failed parsing %s\n", opt);
774 * Option match, levenshtein distance. Handy for not quite remembering what
775 * the option name is.
777 static int string_distance(const char *s1, const char *s2)
779 unsigned int s1_len = strlen(s1);
780 unsigned int s2_len = strlen(s2);
781 unsigned int *p, *q, *r;
784 p = malloc(sizeof(unsigned int) * (s2_len + 1));
785 q = malloc(sizeof(unsigned int) * (s2_len + 1));
788 for (i = 1; i <= s2_len; i++)
791 for (i = 1; i <= s1_len; i++) {
793 for (j = 1; j <= s2_len; j++) {
794 unsigned int sub = p[j - 1];
796 if (s1[i - 1] != s2[j - 1])
799 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
812 static struct fio_option *find_child(struct fio_option *options,
813 struct fio_option *o)
815 struct fio_option *__o;
817 for (__o = options + 1; __o->name; __o++)
818 if (__o->parent && !strcmp(__o->parent, o->name))
824 static void __print_option(struct fio_option *o, struct fio_option *org,
838 p += sprintf(p, "%s", " ");
840 sprintf(p, "%s", o->name);
842 printf("%-24s: %s\n", name, o->help);
845 static void print_option(struct fio_option *o)
847 struct fio_option *parent;
848 struct fio_option *__o;
849 unsigned int printed;
852 __print_option(o, NULL, 0);
859 while ((__o = find_child(o, parent)) != NULL) {
860 __print_option(__o, o, level);
869 int show_cmd_help(struct fio_option *options, const char *name)
871 struct fio_option *o, *closest;
872 unsigned int best_dist = -1U;
876 if (!name || !strcmp(name, "all"))
881 for (o = &options[0]; o->name; o++) {
884 if (o->type == FIO_OPT_DEPRECATED)
886 if (!exec_profile && o->prof_name)
890 if (!strcmp(name, o->name) ||
891 (o->alias && !strcmp(name, o->alias)))
896 dist = string_distance(name, o->name);
897 if (dist < best_dist) {
904 if (show_all || match) {
907 printf("%20s: %s\n", o->name, o->help);
918 show_option_help(o, stdout);
924 printf("No such command: %s", name);
927 * Only print an appropriately close option, one where the edit
928 * distance isn't too big. Otherwise we get crazy matches.
930 if (closest && best_dist < 3) {
931 printf(" - showing closest match\n");
932 printf("%20s: %s\n", closest->name, closest->help);
933 show_option_help(closest, stdout);
941 * Handle parsing of default parameters.
943 void fill_default_options(void *data, struct fio_option *options)
945 struct fio_option *o;
947 dprint(FD_PARSE, "filling default options\n");
949 for (o = &options[0]; o->name; o++)
951 handle_option(o, o->def, data);
954 void option_init(struct fio_option *o)
956 if (o->type == FIO_OPT_DEPRECATED)
958 if (o->type == FIO_OPT_BOOL) {
962 if (o->type == FIO_OPT_STR_SET && o->def) {
963 fprintf(stderr, "Option %s: string set option with"
964 " default will always be true\n", o->name);
966 if (!o->cb && (!o->off1 && !o->roff1)) {
967 fprintf(stderr, "Option %s: neither cb nor offset given\n",
970 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
971 o->type == FIO_OPT_STR_MULTI)
973 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
974 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
975 fprintf(stderr, "Option %s: both cb and offset given\n",
981 * Sanitize the options structure. For now it just sets min/max for bool
982 * values and whether both callback and offsets are given.
984 void options_init(struct fio_option *options)
986 struct fio_option *o;
988 dprint(FD_PARSE, "init options\n");
990 for (o = &options[0]; o->name; o++)