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)
62 const struct value_pair *vp = &o->posval[i];
67 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
69 printf(" %s", vp->help);
72 } while (i < PARSE_MAX_VP);
78 static void show_option_help(struct fio_option *o, FILE *out)
80 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(char c, void *data)
123 unsigned int kb_base = fio_get_kb_base(data);
124 unsigned long long ret = 1;
131 ret *= (unsigned long long) kb_base;
134 ret *= (unsigned long long) kb_base;
137 ret *= (unsigned long long) kb_base;
140 ret *= (unsigned long long) kb_base;
143 ret *= (unsigned long long) kb_base;
151 * convert string into decimal value, noting any size suffix
153 int str_to_decimal(const char *str, long long *val, int kilo, void *data)
161 if (strstr(str, "0x") || strstr(str, "0X"))
166 *val = strtoll(str, NULL, base);
167 if (*val == LONG_MAX && errno == ERANGE)
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 if ((*p == 'b' || *p == 'B') && isalpha(*(p - 1)))
181 *val *= get_mult_bytes(*p, data);
183 *val *= get_mult_time(str[len - 1]);
188 static int check_str_bytes(const char *p, long long *val, void *data)
190 return str_to_decimal(p, val, 1, data);
193 static int check_str_time(const char *p, long long *val)
195 return str_to_decimal(p, val, 0, NULL);
198 void strip_blank_front(char **p)
208 void strip_blank_end(char *p)
222 while ((isspace(*s) || iscntrl(*s)) && (s > start))
228 static int check_range_bytes(const char *str, long *val, void *data)
235 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
236 *val *= get_mult_bytes(suffix, data);
240 if (sscanf(str, "%lu", val) == 1)
246 static int check_int(const char *p, int *val)
250 if (strstr(p, "0x") || strstr(p, "0X")) {
251 if (sscanf(p, "%x", val) == 1)
254 if (sscanf(p, "%u", val) == 1)
261 static inline int o_match(struct fio_option *o, const char *opt)
263 if (!strcmp(o->name, opt))
265 else if (o->alias && !strcmp(o->alias, opt))
271 static struct fio_option *find_option(struct fio_option *options,
274 struct fio_option *o;
276 for (o = &options[0]; o->name; o++)
283 #define val_store(ptr, val, off, data) \
285 ptr = td_var((data), (off)); \
289 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
293 long long ull, *ullp;
296 int ret = 0, is_time = 0;
298 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
301 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
302 fprintf(stderr, "Option %s requires an argument\n", o->name);
308 fio_opt_str_fn *fn = o->cb;
309 const struct value_pair *vp;
310 struct value_pair posval[PARSE_MAX_VP];
313 posval_sort(o, posval);
315 for (i = 0; i < PARSE_MAX_VP; i++) {
317 if (!vp->ival || vp->ival[0] == '\0')
320 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
323 *(unsigned int *) o->roff1 = vp->oval;
327 val_store(ilp, vp->oval, o->off1, data);
334 show_option_values(o);
339 case FIO_OPT_STR_VAL_TIME:
342 case FIO_OPT_STR_VAL: {
343 fio_opt_str_val_fn *fn = o->cb;
346 ret = check_str_time(ptr, &ull);
348 ret = check_str_bytes(ptr, &ull, data);
353 if (o->maxval && ull > o->maxval) {
354 fprintf(stderr, "max value out of range: %lld"
355 " (%d max)\n", ull, o->maxval);
358 if (o->minval && ull < o->minval) {
359 fprintf(stderr, "min value out of range: %lld"
360 " (%d min)\n", ull, o->minval);
365 ret = fn(data, &ull);
367 if (o->type == FIO_OPT_INT) {
370 *(unsigned long long *) o->roff1 = ull;
372 val_store(ilp, ull, o->off1, data);
376 *(unsigned long long *) o->roff2 = ull;
378 val_store(ilp, ull, o->off2, data);
383 *(unsigned long long *) o->roff1 = ull;
385 val_store(ullp, ull, o->off1, data);
389 *(unsigned long long *) o->roff2 = ull;
391 val_store(ullp, ull, o->off2, data);
397 case FIO_OPT_STR_STORE: {
398 fio_opt_str_fn *fn = o->cb;
401 cp = (char **) o->roff1;
403 cp = td_var(data, o->off1);
415 case FIO_OPT_RANGE: {
419 strncpy(tmp, ptr, sizeof(tmp) - 1);
421 p1 = strchr(tmp, '-');
423 p1 = strchr(tmp, ':');
435 if (!check_range_bytes(p1, &ul1, data) &&
436 !check_range_bytes(p2, &ul2, data)) {
439 unsigned long foo = ul1;
447 *(unsigned long *) o->roff1 = ul1;
449 val_store(ilp, ul1, o->off1, data);
451 *(unsigned long *) o->roff2 = ul2;
453 val_store(ilp, ul2, o->off2, data);
455 if (o->roff3 && o->roff4) {
456 *(unsigned long *) o->roff3 = ul1;
457 *(unsigned long *) o->roff4 = ul2;
458 } else if (o->off3 && o->off4) {
459 val_store(ilp, ul1, o->off3, data);
460 val_store(ilp, ul2, o->off4, data);
467 fio_opt_int_fn *fn = o->cb;
469 ret = check_int(ptr, &il);
473 if (o->maxval && il > (int) o->maxval) {
474 fprintf(stderr, "max value out of range: %d (%d max)\n",
478 if (o->minval && il < o->minval) {
479 fprintf(stderr, "min value out of range: %d (%d min)\n",
492 *(unsigned int *)o->roff1 = il;
494 val_store(ilp, il, o->off1, data);
498 *(unsigned int *) o->roff2 = il;
500 val_store(ilp, il, o->off2, data);
505 case FIO_OPT_STR_SET: {
506 fio_opt_str_set_fn *fn = o->cb;
513 *(unsigned int *) o->roff1 = 1;
515 val_store(ilp, 1, o->off1, data);
519 *(unsigned int *) o->roff2 = 1;
521 val_store(ilp, 1, o->off2, data);
526 case FIO_OPT_DEPRECATED:
527 fprintf(stdout, "Option %s is deprecated\n", o->name);
530 fprintf(stderr, "Bad option type %u\n", o->type);
538 ret = o->verify(o, data);
540 fprintf(stderr,"Correct format for offending option\n");
541 fprintf(stderr, "%20s: %s\n", o->name, o->help);
542 show_option_help(o, stderr);
549 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
551 char *ptr, *ptr2 = NULL;
554 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
561 * See if we have a second set of parameters, hidden after a comma.
562 * Do this before parsing the first round, to check if we should
563 * copy set 1 options to set 2.
566 (o->type != FIO_OPT_STR_STORE) &&
567 (o->type != FIO_OPT_STR)) {
568 ptr2 = strchr(ptr, ',');
569 if (ptr2 && *(ptr2 + 1) == '\0')
572 ptr2 = strchr(ptr, ':');
574 ptr2 = strchr(ptr, '-');
578 * Don't return early if parsing the first option fails - if
579 * we are doing multiple arguments, we can allow the first one
582 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
591 r2 = __handle_option(o, ptr2, data, 0, 0);
598 static struct fio_option *get_option(const char *opt,
599 struct fio_option *options, char **post)
601 struct fio_option *o;
604 ret = strchr(opt, '=');
610 strip_blank_end(ret);
611 o = find_option(options, ret);
613 o = find_option(options, opt);
620 static int opt_cmp(const void *p1, const void *p2)
622 struct fio_option *o1, *o2;
626 s1 = strdup(*((char **) p1));
627 s2 = strdup(*((char **) p2));
629 o1 = get_option(s1, fio_options, &foo);
630 o2 = get_option(s2, fio_options, &foo);
640 return prio2 - prio1;
643 void sort_options(char **opts, struct fio_option *options, int num_opts)
645 fio_options = options;
646 qsort(opts, num_opts, sizeof(char *), opt_cmp);
650 int parse_cmd_option(const char *opt, const char *val,
651 struct fio_option *options, void *data)
653 struct fio_option *o;
655 o = find_option(options, opt);
657 fprintf(stderr, "Bad option <%s>\n", opt);
661 if (!handle_option(o, val, data))
664 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
669 * Return a copy of the input string with substrings of the form ${VARNAME}
670 * substituted with the value of the environment variable VARNAME. The
671 * substitution always occurs, even if VARNAME is empty or the corresponding
672 * environment variable undefined.
674 static char *option_dup_subs(const char *opt)
676 char out[OPT_LEN_MAX+1];
677 char in[OPT_LEN_MAX+1];
680 char *ch1, *ch2, *env;
681 ssize_t nchr = OPT_LEN_MAX;
684 if (strlen(in) + 1 > OPT_LEN_MAX) {
685 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
689 in[OPT_LEN_MAX] = '\0';
690 strncpy(in, opt, OPT_LEN_MAX);
692 while (*inptr && nchr > 0) {
693 if (inptr[0] == '$' && inptr[1] == '{') {
694 ch2 = strchr(inptr, '}');
695 if (ch2 && inptr+1 < ch2) {
702 envlen = strlen(env);
703 if (envlen <= nchr) {
704 memcpy(outptr, env, envlen);
714 *outptr++ = *inptr++;
722 int parse_option(const char *opt, struct fio_option *options, void *data)
724 struct fio_option *o;
727 tmp = option_dup_subs(opt);
731 o = get_option(tmp, options, &post);
733 fprintf(stderr, "Bad option <%s>\n", tmp);
738 if (!handle_option(o, post, data)) {
743 fprintf(stderr, "fio: failed parsing %s\n", opt);
749 * Option match, levenshtein distance. Handy for not quite remembering what
750 * the option name is.
752 static int string_distance(const char *s1, const char *s2)
754 unsigned int s1_len = strlen(s1);
755 unsigned int s2_len = strlen(s2);
756 unsigned int *p, *q, *r;
759 p = malloc(sizeof(unsigned int) * (s2_len + 1));
760 q = malloc(sizeof(unsigned int) * (s2_len + 1));
763 for (i = 1; i <= s2_len; i++)
766 for (i = 1; i <= s1_len; i++) {
768 for (j = 1; j <= s2_len; j++) {
769 unsigned int sub = p[j - 1];
771 if (s1[i - 1] != s2[j - 1])
774 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
787 static struct fio_option *find_child(struct fio_option *options,
788 struct fio_option *o)
790 struct fio_option *__o;
792 for (__o = options + 1; __o->name; __o++)
793 if (__o->parent && !strcmp(__o->parent, o->name))
799 static void __print_option(struct fio_option *o, struct fio_option *org,
813 p += sprintf(p, "%s", " ");
815 sprintf(p, "%s", o->name);
817 printf("%-24s: %s\n", name, o->help);
820 static void print_option(struct fio_option *o)
822 struct fio_option *parent;
823 struct fio_option *__o;
824 unsigned int printed;
827 __print_option(o, NULL, 0);
834 while ((__o = find_child(o, parent)) != NULL) {
835 __print_option(__o, o, level);
844 int show_cmd_help(struct fio_option *options, const char *name)
846 struct fio_option *o, *closest;
847 unsigned int best_dist;
851 if (!name || !strcmp(name, "all"))
856 for (o = &options[0]; o->name; o++) {
859 if (o->type == FIO_OPT_DEPRECATED)
861 if (!exec_profile && o->prof_name)
865 if (!strcmp(name, o->name) ||
866 (o->alias && !strcmp(name, o->alias)))
871 dist = string_distance(name, o->name);
872 if (dist < best_dist) {
879 if (show_all || match) {
882 printf("%20s: %s\n", o->name, o->help);
893 show_option_help(o, stdout);
899 printf("No such command: %s", name);
901 printf(" - showing closest match\n");
902 printf("%20s: %s\n", closest->name, closest->help);
903 show_option_help(closest, stdout);
911 * Handle parsing of default parameters.
913 void fill_default_options(void *data, struct fio_option *options)
915 struct fio_option *o;
917 dprint(FD_PARSE, "filling default options\n");
919 for (o = &options[0]; o->name; o++)
921 handle_option(o, o->def, data);
924 void option_init(struct fio_option *o)
926 if (o->type == FIO_OPT_DEPRECATED)
928 if (o->type == FIO_OPT_BOOL) {
932 if (o->type == FIO_OPT_STR_SET && o->def) {
933 fprintf(stderr, "Option %s: string set option with"
934 " default will always be true\n", o->name);
936 if (!o->cb && (!o->off1 && !o->roff1)) {
937 fprintf(stderr, "Option %s: neither cb nor offset given\n",
940 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
942 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
943 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
944 fprintf(stderr, "Option %s: both cb and offset given\n",
950 * Sanitize the options structure. For now it just sets min/max for bool
951 * values and whether both callback and offsets are given.
953 void options_init(struct fio_option *options)
955 struct fio_option *o;
957 dprint(FD_PARSE, "init options\n");
959 for (o = &options[0]; o->name; o++)