2 * This file contains the ini and command liner parser main.
18 static struct fio_option *fio_options;
19 extern unsigned int fio_get_kb_base(void *);
21 static int vp_cmp(const void *p1, const void *p2)
23 const struct value_pair *vp1 = p1;
24 const struct value_pair *vp2 = p2;
26 return strlen(vp2->ival) - strlen(vp1->ival);
29 static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
31 const struct value_pair *vp;
34 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
36 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
37 vp = &o->posval[entries];
38 if (!vp->ival || vp->ival[0] == '\0')
41 memcpy(&vpmap[entries], vp, sizeof(*vp));
44 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
47 static void show_option_range(struct fio_option *o, FILE *out)
49 if (o->type == FIO_OPT_FLOAT_LIST){
50 if (isnan(o->minfp) && isnan(o->maxfp))
53 fprintf(out, "%20s: min=%f", "range", o->minfp);
55 fprintf(out, ", max=%f", o->maxfp);
58 if (!o->minval && !o->maxval)
61 fprintf(out, "%20s: min=%d", "range", o->minval);
63 fprintf(out, ", max=%d", o->maxval);
68 static void show_option_values(struct fio_option *o)
72 for (i = 0; i < PARSE_MAX_VP; i++) {
73 const struct value_pair *vp = &o->posval[i];
78 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
80 printf(" %s", vp->help);
88 static void show_option_help(struct fio_option *o, FILE *out)
90 const char *typehelp[] = {
94 "string with possible k/m/g postfix (opt=4k)",
95 "string with time postfix (opt=10s)",
97 "string with dual range (opt=1k-4k,4k-8k)",
98 "integer value (opt=100)",
99 "boolean value (opt=1)",
100 "list of floating point values separated by ':' (opt=5.9:7.8)",
106 fprintf(out, "%20s: %s\n", "alias", o->alias);
108 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
109 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
111 fprintf(out, "%20s: only for profile '%s'\n", "valid", o->prof_name);
112 show_option_range(o, stdout);
113 show_option_values(o);
116 static unsigned long get_mult_time(char c)
133 static unsigned long long __get_mult_bytes(const char *p, void *data,
136 unsigned int kb_base = fio_get_kb_base(data);
137 unsigned long long ret = 1;
138 unsigned int i, pow = 0, mult = kb_base;
146 for (i = 0; i < strlen(c); i++)
147 c[i] = tolower(c[i]);
149 if (!strcmp("pib", c)) {
152 } else if (!strcmp("tib", c)) {
155 } else if (!strcmp("gib", c)) {
158 } else if (!strcmp("mib", c)) {
161 } else if (!strcmp("kib", c)) {
164 } else if (!strcmp("p", c) || !strcmp("pb", c))
166 else if (!strcmp("t", c) || !strcmp("tb", c))
168 else if (!strcmp("g", c) || !strcmp("gb", c))
170 else if (!strcmp("m", c) || !strcmp("mb", c))
172 else if (!strcmp("k", c) || !strcmp("kb", c))
174 else if (!strcmp("%", c)) {
180 ret *= (unsigned long long) mult;
186 static unsigned long long get_mult_bytes(const char *str, int len, void *data,
193 return __get_mult_bytes(str, data, percent);
196 * Go forward until we hit a non-digit, or +/- sign
198 while ((p - str) <= len) {
199 if (!isdigit((int) *p) &&
200 (((*p != '+') && (*p != '-')) || digit_seen))
202 digit_seen |= isdigit((int) *p);
206 if (!isalpha((int) *p) && (*p != '%'))
209 return __get_mult_bytes(p, data, percent);
213 * Convert string into a floating number. Return 1 for success and 0 otherwise.
215 int str_to_float(const char *str, double *val)
217 return (1 == sscanf(str, "%lf", val));
221 * convert string into decimal value, noting any size suffix
223 int str_to_decimal(const char *str, long long *val, int kilo, void *data)
231 if (strstr(str, "0x") || strstr(str, "0X"))
236 *val = strtoll(str, NULL, base);
237 if (*val == LONG_MAX && errno == ERANGE)
241 unsigned long long mult;
244 mult = get_mult_bytes(str, len, data, &perc);
250 *val *= get_mult_time(str[len - 1]);
255 static int check_str_bytes(const char *p, long long *val, void *data)
257 return str_to_decimal(p, val, 1, data);
260 static int check_str_time(const char *p, long long *val)
262 return str_to_decimal(p, val, 0, NULL);
265 void strip_blank_front(char **p)
269 while (isspace((int) *s))
275 void strip_blank_end(char *p)
289 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
295 static int check_range_bytes(const char *str, long *val, void *data)
299 if (!str_to_decimal(str, &__val, 1, data)) {
307 static int check_int(const char *p, int *val)
311 if (strstr(p, "0x") || strstr(p, "0X")) {
312 if (sscanf(p, "%x", val) == 1)
315 if (sscanf(p, "%u", val) == 1)
322 static int opt_len(const char *str)
326 postfix = strchr(str, ':');
330 return (int)(postfix - str);
333 #define val_store(ptr, val, off, or, data) \
335 ptr = td_var((data), (off)); \
342 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
343 int first, int more, int curr)
347 long long ull, *ullp;
351 int ret = 0, is_time = 0;
352 const struct value_pair *vp;
353 struct value_pair posval[PARSE_MAX_VP];
354 int i, all_skipped = 1;
356 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
359 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
360 fprintf(stderr, "Option %s requires an argument\n", o->name);
366 case FIO_OPT_STR_MULTI: {
367 fio_opt_str_fn *fn = o->cb;
369 posval_sort(o, posval);
372 for (i = 0; i < PARSE_MAX_VP; i++) {
374 if (!vp->ival || vp->ival[0] == '\0')
377 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
381 *(unsigned int *) o->roff1 |= vp->oval;
383 *(unsigned int *) o->roff1 = vp->oval;
387 val_store(ilp, vp->oval, o->off1, vp->or, data);
393 if (ret && !all_skipped)
394 show_option_values(o);
399 case FIO_OPT_STR_VAL_TIME:
402 case FIO_OPT_STR_VAL: {
403 fio_opt_str_val_fn *fn = o->cb;
406 ret = check_str_time(ptr, &ull);
408 ret = check_str_bytes(ptr, &ull, data);
413 if (o->maxval && ull > o->maxval) {
414 fprintf(stderr, "max value out of range: %lld"
415 " (%d max)\n", ull, o->maxval);
418 if (o->minval && ull < o->minval) {
419 fprintf(stderr, "min value out of range: %lld"
420 " (%d min)\n", ull, o->minval);
425 ret = fn(data, &ull);
427 if (o->type == FIO_OPT_INT) {
430 *(unsigned int *) o->roff1 = ull;
432 val_store(ilp, ull, o->off1, 0, data);
436 *(unsigned int *) o->roff2 = ull;
438 val_store(ilp, ull, o->off2, 0, data);
443 *(unsigned long long *) o->roff1 = ull;
445 val_store(ullp, ull, o->off1, 0, data);
449 *(unsigned long long *) o->roff2 = ull;
451 val_store(ullp, ull, o->off2, 0, data);
457 case FIO_OPT_FLOAT_LIST: {
461 ilp = td_var(data, o->off2);
464 if (curr >= o->maxlen) {
465 fprintf(stderr, "the list exceeding max length %d\n",
469 if(!str_to_float(ptr, &uf)){
470 fprintf(stderr, "not a floating point value: %s\n",
474 if (!isnan(o->maxfp) && uf > o->maxfp) {
475 fprintf(stderr, "value out of range: %f"
476 " (range max: %f)\n", uf, o->maxfp);
479 if (!isnan(o->minfp) && uf < o->minfp) {
480 fprintf(stderr, "value out of range: %f"
481 " (range min: %f)\n", uf, o->minfp);
485 flp = td_var(data, o->off1);
490 case FIO_OPT_STR_STORE: {
491 fio_opt_str_fn *fn = o->cb;
493 posval_sort(o, posval);
495 if (!o->posval[0].ival) {
501 for (i = 0; i < PARSE_MAX_VP; i++) {
503 if (!vp->ival || vp->ival[0] == '\0')
506 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
514 cp = (char **) o->roff1;
516 cp = td_var(data, o->off1);
518 rest = strstr(*cp, ":");
522 } else if (vp && vp->cb)
528 if (ret && !all_skipped)
529 show_option_values(o);
535 case FIO_OPT_RANGE: {
539 strncpy(tmp, ptr, sizeof(tmp) - 1);
541 /* Handle bsrange with separate read,write values: */
542 p1 = strchr(tmp, ',');
546 p1 = strchr(tmp, '-');
548 p1 = strchr(tmp, ':');
560 if (!check_range_bytes(p1, &ul1, data) &&
561 !check_range_bytes(p2, &ul2, data)) {
564 unsigned long foo = ul1;
572 *(unsigned int *) o->roff1 = ul1;
574 val_store(ilp, ul1, o->off1, 0, data);
576 *(unsigned int *) o->roff2 = ul2;
578 val_store(ilp, ul2, o->off2, 0, data);
580 if (o->roff3 && o->roff4) {
581 *(unsigned int *) o->roff3 = ul1;
582 *(unsigned int *) o->roff4 = ul2;
583 } else if (o->off3 && o->off4) {
584 val_store(ilp, ul1, o->off3, 0, data);
585 val_store(ilp, ul2, o->off4, 0, data);
592 case FIO_OPT_STR_SET: {
593 fio_opt_int_fn *fn = o->cb;
596 ret = check_int(ptr, &il);
597 else if (o->type == FIO_OPT_BOOL)
605 if (o->maxval && il > (int) o->maxval) {
606 fprintf(stderr, "max value out of range: %d (%d max)\n",
610 if (o->minval && il < o->minval) {
611 fprintf(stderr, "min value out of range: %d (%d min)\n",
624 *(unsigned int *)o->roff1 = il;
626 val_store(ilp, il, o->off1, 0, data);
630 *(unsigned int *) o->roff2 = il;
632 val_store(ilp, il, o->off2, 0, data);
637 case FIO_OPT_DEPRECATED:
638 fprintf(stdout, "Option %s is deprecated\n", o->name);
641 fprintf(stderr, "Bad option type %u\n", o->type);
649 ret = o->verify(o, data);
651 fprintf(stderr,"Correct format for offending option\n");
652 fprintf(stderr, "%20s: %s\n", o->name, o->help);
653 show_option_help(o, stderr);
660 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
662 char *o_ptr, *ptr, *ptr2;
665 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
669 o_ptr = ptr = strdup(__ptr);
672 * See if we have another set of parameters, hidden after a comma.
673 * Do this before parsing this round, to check if we should
674 * copy set 1 options to set 2.
683 (o->type != FIO_OPT_STR_STORE) &&
684 (o->type != FIO_OPT_STR) &&
685 (o->type != FIO_OPT_FLOAT_LIST)) {
686 ptr2 = strchr(ptr, ',');
687 if (ptr2 && *(ptr2 + 1) == '\0')
689 if (o->type != FIO_OPT_STR_MULTI) {
691 ptr2 = strchr(ptr, ':');
693 ptr2 = strchr(ptr, '-');
695 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
696 ptr2 = strchr(ptr, ':');
700 * Don't return early if parsing the first option fails - if
701 * we are doing multiple arguments, we can allow the first one
704 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
720 static struct fio_option *get_option(const char *opt,
721 struct fio_option *options, char **post)
723 struct fio_option *o;
726 ret = strchr(opt, '=');
732 strip_blank_end(ret);
733 o = find_option(options, ret);
735 o = find_option(options, opt);
742 static int opt_cmp(const void *p1, const void *p2)
744 struct fio_option *o1, *o2;
748 s1 = strdup(*((char **) p1));
749 s2 = strdup(*((char **) p2));
751 o1 = get_option(s1, fio_options, &foo);
752 o2 = get_option(s2, fio_options, &foo);
762 return prio2 - prio1;
765 void sort_options(char **opts, struct fio_option *options, int num_opts)
767 fio_options = options;
768 qsort(opts, num_opts, sizeof(char *), opt_cmp);
772 int parse_cmd_option(const char *opt, const char *val,
773 struct fio_option *options, void *data)
775 struct fio_option *o;
777 o = find_option(options, opt);
779 fprintf(stderr, "Bad option <%s>\n", opt);
783 if (!handle_option(o, val, data))
786 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
791 * Return a copy of the input string with substrings of the form ${VARNAME}
792 * substituted with the value of the environment variable VARNAME. The
793 * substitution always occurs, even if VARNAME is empty or the corresponding
794 * environment variable undefined.
796 static char *option_dup_subs(const char *opt)
798 char out[OPT_LEN_MAX+1];
799 char in[OPT_LEN_MAX+1];
802 char *ch1, *ch2, *env;
803 ssize_t nchr = OPT_LEN_MAX;
806 if (strlen(opt) + 1 > OPT_LEN_MAX) {
807 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
811 in[OPT_LEN_MAX] = '\0';
812 strncpy(in, opt, OPT_LEN_MAX);
814 while (*inptr && nchr > 0) {
815 if (inptr[0] == '$' && inptr[1] == '{') {
816 ch2 = strchr(inptr, '}');
817 if (ch2 && inptr+1 < ch2) {
824 envlen = strlen(env);
825 if (envlen <= nchr) {
826 memcpy(outptr, env, envlen);
836 *outptr++ = *inptr++;
844 int parse_option(const char *opt, struct fio_option *options, void *data)
846 struct fio_option *o;
849 tmp = option_dup_subs(opt);
853 o = get_option(tmp, options, &post);
855 fprintf(stderr, "Bad option <%s>\n", tmp);
860 if (!handle_option(o, post, data)) {
865 fprintf(stderr, "fio: failed parsing %s\n", opt);
871 * Option match, levenshtein distance. Handy for not quite remembering what
872 * the option name is.
874 static int string_distance(const char *s1, const char *s2)
876 unsigned int s1_len = strlen(s1);
877 unsigned int s2_len = strlen(s2);
878 unsigned int *p, *q, *r;
881 p = malloc(sizeof(unsigned int) * (s2_len + 1));
882 q = malloc(sizeof(unsigned int) * (s2_len + 1));
885 for (i = 1; i <= s2_len; i++)
888 for (i = 1; i <= s1_len; i++) {
890 for (j = 1; j <= s2_len; j++) {
891 unsigned int sub = p[j - 1];
893 if (s1[i - 1] != s2[j - 1])
896 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
909 static struct fio_option *find_child(struct fio_option *options,
910 struct fio_option *o)
912 struct fio_option *__o;
914 for (__o = options + 1; __o->name; __o++)
915 if (__o->parent && !strcmp(__o->parent, o->name))
921 static void __print_option(struct fio_option *o, struct fio_option *org,
935 p += sprintf(p, "%s", " ");
937 sprintf(p, "%s", o->name);
939 printf("%-24s: %s\n", name, o->help);
942 static void print_option(struct fio_option *o)
944 struct fio_option *parent;
945 struct fio_option *__o;
946 unsigned int printed;
949 __print_option(o, NULL, 0);
956 while ((__o = find_child(o, parent)) != NULL) {
957 __print_option(__o, o, level);
966 int show_cmd_help(struct fio_option *options, const char *name)
968 struct fio_option *o, *closest;
969 unsigned int best_dist = -1U;
973 if (!name || !strcmp(name, "all"))
978 for (o = &options[0]; o->name; o++) {
981 if (o->type == FIO_OPT_DEPRECATED)
983 if (!exec_profile && o->prof_name)
987 if (!strcmp(name, o->name) ||
988 (o->alias && !strcmp(name, o->alias)))
993 dist = string_distance(name, o->name);
994 if (dist < best_dist) {
1001 if (show_all || match) {
1004 printf("%20s: %s\n", o->name, o->help);
1015 show_option_help(o, stdout);
1021 printf("No such command: %s", name);
1024 * Only print an appropriately close option, one where the edit
1025 * distance isn't too big. Otherwise we get crazy matches.
1027 if (closest && best_dist < 3) {
1028 printf(" - showing closest match\n");
1029 printf("%20s: %s\n", closest->name, closest->help);
1030 show_option_help(closest, stdout);
1038 * Handle parsing of default parameters.
1040 void fill_default_options(void *data, struct fio_option *options)
1042 struct fio_option *o;
1044 dprint(FD_PARSE, "filling default options\n");
1046 for (o = &options[0]; o->name; o++)
1048 handle_option(o, o->def, data);
1051 void option_init(struct fio_option *o)
1053 if (o->type == FIO_OPT_DEPRECATED)
1055 if (o->type == FIO_OPT_BOOL) {
1059 if (o->type == FIO_OPT_FLOAT_LIST) {
1063 if (o->type == FIO_OPT_STR_SET && o->def) {
1064 fprintf(stderr, "Option %s: string set option with"
1065 " default will always be true\n", o->name);
1067 if (!o->cb && (!o->off1 && !o->roff1)) {
1068 fprintf(stderr, "Option %s: neither cb nor offset given\n",
1071 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1072 o->type == FIO_OPT_STR_MULTI)
1074 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
1075 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
1076 fprintf(stderr, "Option %s: both cb and offset given\n",
1082 * Sanitize the options structure. For now it just sets min/max for bool
1083 * values and whether both callback and offsets are given.
1085 void options_init(struct fio_option *options)
1087 struct fio_option *o;
1089 dprint(FD_PARSE, "init options\n");
1091 for (o = &options[0]; o->name; o++)