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,
124 unsigned int kb_base = fio_get_kb_base(data);
125 unsigned long long ret = 1;
126 unsigned int i, pow = 0, mult = kb_base;
134 for (i = 0; i < strlen(c); i++)
135 c[i] = tolower(c[i]);
137 if (!strcmp("pib", c)) {
140 } else if (!strcmp("tib", c)) {
143 } else if (!strcmp("gib", c)) {
146 } else if (!strcmp("mib", c)) {
149 } else if (!strcmp("kib", c)) {
152 } else if (!strcmp("p", c) || !strcmp("pb", c))
154 else if (!strcmp("t", c) || !strcmp("tb", c))
156 else if (!strcmp("g", c) || !strcmp("gb", c))
158 else if (!strcmp("m", c) || !strcmp("mb", c))
160 else if (!strcmp("k", c) || !strcmp("kb", c))
162 else if (!strcmp("%", c)) {
168 ret *= (unsigned long long) mult;
174 static unsigned long long get_mult_bytes(const char *str, int len, void *data,
180 return __get_mult_bytes(str, data, percent);
183 * Go forward until we hit a non-digit
185 while ((p - str) <= len) {
186 if (!isdigit((int) *p))
191 if (!isalpha((int) *p) && (*p != '%'))
194 return __get_mult_bytes(p, data, percent);
198 * convert string into decimal value, noting any size suffix
200 int str_to_decimal(const char *str, long long *val, int kilo, void *data)
208 if (strstr(str, "0x") || strstr(str, "0X"))
213 *val = strtoll(str, NULL, base);
214 if (*val == LONG_MAX && errno == ERANGE)
218 unsigned long long mult;
221 mult = get_mult_bytes(str, len, data, &perc);
227 *val *= get_mult_time(str[len - 1]);
232 static int check_str_bytes(const char *p, long long *val, void *data)
234 return str_to_decimal(p, val, 1, data);
237 static int check_str_time(const char *p, long long *val)
239 return str_to_decimal(p, val, 0, NULL);
242 void strip_blank_front(char **p)
246 while (isspace((int) *s))
252 void strip_blank_end(char *p)
266 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
272 static int check_range_bytes(const char *str, long *val, void *data)
276 if (!str_to_decimal(str, &__val, 1, data)) {
284 static int check_int(const char *p, int *val)
288 if (strstr(p, "0x") || strstr(p, "0X")) {
289 if (sscanf(p, "%x", val) == 1)
292 if (sscanf(p, "%u", val) == 1)
299 static int opt_len(const char *str)
303 postfix = strchr(str, ':');
307 return (int)(postfix - str);
310 #define val_store(ptr, val, off, or, data) \
312 ptr = td_var((data), (off)); \
319 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
323 long long ull, *ullp;
326 int ret = 0, is_time = 0;
328 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
331 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
332 fprintf(stderr, "Option %s requires an argument\n", o->name);
338 case FIO_OPT_STR_MULTI: {
339 fio_opt_str_fn *fn = o->cb;
340 const struct value_pair *vp;
341 struct value_pair posval[PARSE_MAX_VP];
342 int i, all_skipped = 1;
344 posval_sort(o, posval);
347 for (i = 0; i < PARSE_MAX_VP; i++) {
349 if (!vp->ival || vp->ival[0] == '\0')
352 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
356 *(unsigned int *) o->roff1 |= vp->oval;
358 *(unsigned int *) o->roff1 = vp->oval;
362 val_store(ilp, vp->oval, o->off1, vp->or, data);
368 if (ret && !all_skipped)
369 show_option_values(o);
374 case FIO_OPT_STR_VAL_TIME:
377 case FIO_OPT_STR_VAL: {
378 fio_opt_str_val_fn *fn = o->cb;
381 ret = check_str_time(ptr, &ull);
383 ret = check_str_bytes(ptr, &ull, data);
388 if (o->maxval && ull > o->maxval) {
389 fprintf(stderr, "max value out of range: %lld"
390 " (%d max)\n", ull, o->maxval);
393 if (o->minval && ull < o->minval) {
394 fprintf(stderr, "min value out of range: %lld"
395 " (%d min)\n", ull, o->minval);
400 ret = fn(data, &ull);
402 if (o->type == FIO_OPT_INT) {
405 *(unsigned int *) o->roff1 = ull;
407 val_store(ilp, ull, o->off1, 0, data);
411 *(unsigned int *) o->roff2 = ull;
413 val_store(ilp, ull, o->off2, 0, data);
418 *(unsigned long long *) o->roff1 = ull;
420 val_store(ullp, ull, o->off1, 0, data);
424 *(unsigned long long *) o->roff2 = ull;
426 val_store(ullp, ull, o->off2, 0, data);
432 case FIO_OPT_STR_STORE: {
433 fio_opt_str_fn *fn = o->cb;
436 cp = (char **) o->roff1;
438 cp = td_var(data, o->off1);
450 case FIO_OPT_RANGE: {
454 strncpy(tmp, ptr, sizeof(tmp) - 1);
456 p1 = strchr(tmp, '-');
458 p1 = strchr(tmp, ':');
470 if (!check_range_bytes(p1, &ul1, data) &&
471 !check_range_bytes(p2, &ul2, data)) {
474 unsigned long foo = ul1;
482 *(unsigned int *) o->roff1 = ul1;
484 val_store(ilp, ul1, o->off1, 0, data);
486 *(unsigned int *) o->roff2 = ul2;
488 val_store(ilp, ul2, o->off2, 0, data);
490 if (o->roff3 && o->roff4) {
491 *(unsigned int *) o->roff3 = ul1;
492 *(unsigned int *) o->roff4 = ul2;
493 } else if (o->off3 && o->off4) {
494 val_store(ilp, ul1, o->off3, 0, data);
495 val_store(ilp, ul2, o->off4, 0, data);
502 case FIO_OPT_STR_SET: {
503 fio_opt_int_fn *fn = o->cb;
506 ret = check_int(ptr, &il);
507 else if (o->type == FIO_OPT_BOOL)
515 if (o->maxval && il > (int) o->maxval) {
516 fprintf(stderr, "max value out of range: %d (%d max)\n",
520 if (o->minval && il < o->minval) {
521 fprintf(stderr, "min value out of range: %d (%d min)\n",
534 *(unsigned int *)o->roff1 = il;
536 val_store(ilp, il, o->off1, 0, data);
540 *(unsigned int *) o->roff2 = il;
542 val_store(ilp, il, o->off2, 0, data);
547 case FIO_OPT_DEPRECATED:
548 fprintf(stdout, "Option %s is deprecated\n", o->name);
551 fprintf(stderr, "Bad option type %u\n", o->type);
559 ret = o->verify(o, data);
561 fprintf(stderr,"Correct format for offending option\n");
562 fprintf(stderr, "%20s: %s\n", o->name, o->help);
563 show_option_help(o, stderr);
570 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
572 char *o_ptr, *ptr, *ptr2;
575 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
579 o_ptr = ptr = strdup(__ptr);
582 * See if we have another set of parameters, hidden after a comma.
583 * Do this before parsing this round, to check if we should
584 * copy set 1 options to set 2.
593 (o->type != FIO_OPT_STR_STORE) &&
594 (o->type != FIO_OPT_STR)) {
595 ptr2 = strchr(ptr, ',');
596 if (ptr2 && *(ptr2 + 1) == '\0')
598 if (o->type != FIO_OPT_STR_MULTI) {
600 ptr2 = strchr(ptr, ':');
602 ptr2 = strchr(ptr, '-');
607 * Don't return early if parsing the first option fails - if
608 * we are doing multiple arguments, we can allow the first one
611 __ret = __handle_option(o, ptr, data, !done, !!ptr2);
627 static struct fio_option *get_option(const char *opt,
628 struct fio_option *options, char **post)
630 struct fio_option *o;
633 ret = strchr(opt, '=');
639 strip_blank_end(ret);
640 o = find_option(options, ret);
642 o = find_option(options, opt);
649 static int opt_cmp(const void *p1, const void *p2)
651 struct fio_option *o1, *o2;
655 s1 = strdup(*((char **) p1));
656 s2 = strdup(*((char **) p2));
658 o1 = get_option(s1, fio_options, &foo);
659 o2 = get_option(s2, fio_options, &foo);
669 return prio2 - prio1;
672 void sort_options(char **opts, struct fio_option *options, int num_opts)
674 fio_options = options;
675 qsort(opts, num_opts, sizeof(char *), opt_cmp);
679 int parse_cmd_option(const char *opt, const char *val,
680 struct fio_option *options, void *data)
682 struct fio_option *o;
684 o = find_option(options, opt);
686 fprintf(stderr, "Bad option <%s>\n", opt);
690 if (!handle_option(o, val, data))
693 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
698 * Return a copy of the input string with substrings of the form ${VARNAME}
699 * substituted with the value of the environment variable VARNAME. The
700 * substitution always occurs, even if VARNAME is empty or the corresponding
701 * environment variable undefined.
703 static char *option_dup_subs(const char *opt)
705 char out[OPT_LEN_MAX+1];
706 char in[OPT_LEN_MAX+1];
709 char *ch1, *ch2, *env;
710 ssize_t nchr = OPT_LEN_MAX;
713 if (strlen(opt) + 1 > OPT_LEN_MAX) {
714 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
718 in[OPT_LEN_MAX] = '\0';
719 strncpy(in, opt, OPT_LEN_MAX);
721 while (*inptr && nchr > 0) {
722 if (inptr[0] == '$' && inptr[1] == '{') {
723 ch2 = strchr(inptr, '}');
724 if (ch2 && inptr+1 < ch2) {
731 envlen = strlen(env);
732 if (envlen <= nchr) {
733 memcpy(outptr, env, envlen);
743 *outptr++ = *inptr++;
751 int parse_option(const char *opt, struct fio_option *options, void *data)
753 struct fio_option *o;
756 tmp = option_dup_subs(opt);
760 o = get_option(tmp, options, &post);
762 fprintf(stderr, "Bad option <%s>\n", tmp);
767 if (!handle_option(o, post, data)) {
772 fprintf(stderr, "fio: failed parsing %s\n", opt);
778 * Option match, levenshtein distance. Handy for not quite remembering what
779 * the option name is.
781 static int string_distance(const char *s1, const char *s2)
783 unsigned int s1_len = strlen(s1);
784 unsigned int s2_len = strlen(s2);
785 unsigned int *p, *q, *r;
788 p = malloc(sizeof(unsigned int) * (s2_len + 1));
789 q = malloc(sizeof(unsigned int) * (s2_len + 1));
792 for (i = 1; i <= s2_len; i++)
795 for (i = 1; i <= s1_len; i++) {
797 for (j = 1; j <= s2_len; j++) {
798 unsigned int sub = p[j - 1];
800 if (s1[i - 1] != s2[j - 1])
803 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
816 static struct fio_option *find_child(struct fio_option *options,
817 struct fio_option *o)
819 struct fio_option *__o;
821 for (__o = options + 1; __o->name; __o++)
822 if (__o->parent && !strcmp(__o->parent, o->name))
828 static void __print_option(struct fio_option *o, struct fio_option *org,
842 p += sprintf(p, "%s", " ");
844 sprintf(p, "%s", o->name);
846 printf("%-24s: %s\n", name, o->help);
849 static void print_option(struct fio_option *o)
851 struct fio_option *parent;
852 struct fio_option *__o;
853 unsigned int printed;
856 __print_option(o, NULL, 0);
863 while ((__o = find_child(o, parent)) != NULL) {
864 __print_option(__o, o, level);
873 int show_cmd_help(struct fio_option *options, const char *name)
875 struct fio_option *o, *closest;
876 unsigned int best_dist = -1U;
880 if (!name || !strcmp(name, "all"))
885 for (o = &options[0]; o->name; o++) {
888 if (o->type == FIO_OPT_DEPRECATED)
890 if (!exec_profile && o->prof_name)
894 if (!strcmp(name, o->name) ||
895 (o->alias && !strcmp(name, o->alias)))
900 dist = string_distance(name, o->name);
901 if (dist < best_dist) {
908 if (show_all || match) {
911 printf("%20s: %s\n", o->name, o->help);
922 show_option_help(o, stdout);
928 printf("No such command: %s", name);
931 * Only print an appropriately close option, one where the edit
932 * distance isn't too big. Otherwise we get crazy matches.
934 if (closest && best_dist < 3) {
935 printf(" - showing closest match\n");
936 printf("%20s: %s\n", closest->name, closest->help);
937 show_option_help(closest, stdout);
945 * Handle parsing of default parameters.
947 void fill_default_options(void *data, struct fio_option *options)
949 struct fio_option *o;
951 dprint(FD_PARSE, "filling default options\n");
953 for (o = &options[0]; o->name; o++)
955 handle_option(o, o->def, data);
958 void option_init(struct fio_option *o)
960 if (o->type == FIO_OPT_DEPRECATED)
962 if (o->type == FIO_OPT_BOOL) {
966 if (o->type == FIO_OPT_STR_SET && o->def) {
967 fprintf(stderr, "Option %s: string set option with"
968 " default will always be true\n", o->name);
970 if (!o->cb && (!o->off1 && !o->roff1)) {
971 fprintf(stderr, "Option %s: neither cb nor offset given\n",
974 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
975 o->type == FIO_OPT_STR_MULTI)
977 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
978 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
979 fprintf(stderr, "Option %s: both cb and offset given\n",
985 * Sanitize the options structure. For now it just sets min/max for bool
986 * values and whether both callback and offsets are given.
988 void options_init(struct fio_option *options)
990 struct fio_option *o;
992 dprint(FD_PARSE, "init options\n");
994 for (o = &options[0]; o->name; o++)