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[] = {
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)",
92 fprintf(out, "%20s: %s\n", "alias", o->alias);
94 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
95 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
96 show_option_range(o, stdout);
97 show_option_values(o);
100 static unsigned long get_mult_time(char c)
117 static unsigned long long get_mult_bytes(char c, void *data)
119 unsigned int kb_base = fio_get_kb_base(data);
120 unsigned long long ret = 1;
127 ret *= (unsigned long long) kb_base;
130 ret *= (unsigned long long) kb_base;
133 ret *= (unsigned long long) kb_base;
136 ret *= (unsigned long long) kb_base;
139 ret *= (unsigned long long) kb_base;
147 * convert string into decimal value, noting any size suffix
149 int str_to_decimal(const char *str, long long *val, int kilo, void *data)
157 if (strstr(str, "0x") || strstr(str, "0X"))
162 *val = strtoll(str, NULL, base);
163 if (*val == LONG_MAX && errno == ERANGE)
169 * if the last char is 'b' or 'B', the user likely used
170 * "1gb" instead of just "1g". If the second to last is also
174 if ((*p == 'b' || *p == 'B') && isalpha(*(p - 1)))
177 *val *= get_mult_bytes(*p, data);
179 *val *= get_mult_time(str[len - 1]);
184 static int check_str_bytes(const char *p, long long *val, void *data)
186 return str_to_decimal(p, val, 1, data);
189 static int check_str_time(const char *p, long long *val)
191 return str_to_decimal(p, val, 0, NULL);
194 void strip_blank_front(char **p)
204 void strip_blank_end(char *p)
218 while ((isspace(*s) || iscntrl(*s)) && (s > start))
224 static int check_range_bytes(const char *str, long *val, void *data)
231 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
232 *val *= get_mult_bytes(suffix, data);
236 if (sscanf(str, "%lu", val) == 1)
242 static int check_int(const char *p, int *val)
246 if (strstr(p, "0x") || strstr(p, "0X")) {
247 if (sscanf(p, "%x", val) == 1)
250 if (sscanf(p, "%u", val) == 1)
257 static inline int o_match(struct fio_option *o, const char *opt)
259 if (!strcmp(o->name, opt))
261 else if (o->alias && !strcmp(o->alias, opt))
267 static struct fio_option *find_option(struct fio_option *options,
268 struct flist_head *eops, const char *opt)
270 struct flist_head *n;
271 struct fio_option *o;
273 for (o = &options[0]; o->name; o++)
280 flist_for_each(n, eops) {
281 struct ext_option *eopt;
283 eopt = flist_entry(n, struct ext_option, list);
284 if (o_match(&eopt->o, opt))
291 #define val_store(ptr, val, off, data) \
293 ptr = td_var((data), (off)); \
297 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
301 long long ull, *ullp;
304 int ret = 0, is_time = 0;
306 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
309 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
310 fprintf(stderr, "Option %s requires an argument\n", o->name);
316 fio_opt_str_fn *fn = o->cb;
317 const struct value_pair *vp;
318 struct value_pair posval[PARSE_MAX_VP];
321 posval_sort(o, posval);
323 for (i = 0; i < PARSE_MAX_VP; i++) {
325 if (!vp->ival || vp->ival[0] == '\0')
328 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
332 val_store(ilp, vp->oval, o->off1, data);
338 show_option_values(o);
343 case FIO_OPT_STR_VAL_TIME:
346 case FIO_OPT_STR_VAL: {
347 fio_opt_str_val_fn *fn = o->cb;
350 ret = check_str_time(ptr, &ull);
352 ret = check_str_bytes(ptr, &ull, data);
357 if (o->maxval && ull > o->maxval) {
358 fprintf(stderr, "max value out of range: %lld"
359 " (%d max)\n", ull, o->maxval);
362 if (o->minval && ull < o->minval) {
363 fprintf(stderr, "min value out of range: %lld"
364 " (%d min)\n", ull, o->minval);
369 ret = fn(data, &ull);
371 if (o->type == FIO_OPT_INT) {
373 val_store(ilp, ull, o->off1, data);
374 if (!more && o->off2)
375 val_store(ilp, ull, o->off2, data);
378 val_store(ullp, ull, o->off1, data);
379 if (!more && o->off2)
380 val_store(ullp, ull, o->off2, data);
385 case FIO_OPT_STR_STORE: {
386 fio_opt_str_fn *fn = o->cb;
388 cp = td_var(data, o->off1);
399 case FIO_OPT_RANGE: {
403 strncpy(tmp, ptr, sizeof(tmp) - 1);
405 p1 = strchr(tmp, '-');
407 p1 = strchr(tmp, ':');
419 if (!check_range_bytes(p1, &ul1, data) &&
420 !check_range_bytes(p2, &ul2, data)) {
423 unsigned long foo = ul1;
430 val_store(ilp, ul1, o->off1, data);
431 val_store(ilp, ul2, o->off2, data);
433 if (o->off3 && o->off4) {
434 val_store(ilp, ul1, o->off3, data);
435 val_store(ilp, ul2, o->off4, data);
442 fio_opt_int_fn *fn = o->cb;
444 ret = check_int(ptr, &il);
448 if (o->maxval && il > (int) o->maxval) {
449 fprintf(stderr, "max value out of range: %d (%d max)\n",
453 if (o->minval && il < o->minval) {
454 fprintf(stderr, "min value out of range: %d (%d min)\n",
466 val_store(ilp, il, o->off1, data);
467 if (!more && o->off2)
468 val_store(ilp, il, o->off2, data);
472 case FIO_OPT_STR_SET: {
473 fio_opt_str_set_fn *fn = o->cb;
479 val_store(ilp, 1, o->off1, data);
480 if (!more && o->off2)
481 val_store(ilp, 1, o->off2, data);
485 case FIO_OPT_DEPRECATED:
486 fprintf(stdout, "Option %s is deprecated\n", o->name);
489 fprintf(stderr, "Bad option type %u\n", o->type);
497 ret = o->verify(o, data);
499 fprintf(stderr,"Correct format for offending option\n");
500 fprintf(stderr, "%20s: %s\n", o->name, o->help);
501 show_option_help(o, stderr);
508 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
510 char *ptr, *ptr2 = NULL;
513 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
520 * See if we have a second set of parameters, hidden after a comma.
521 * Do this before parsing the first round, to check if we should
522 * copy set 1 options to set 2.
525 (o->type != FIO_OPT_STR_STORE) &&
526 (o->type != FIO_OPT_STR)) {
527 ptr2 = strchr(ptr, ',');
528 if (ptr2 && *(ptr2 + 1) == '\0')
531 ptr2 = strchr(ptr, ':');
533 ptr2 = strchr(ptr, '-');
537 * Don't return early if parsing the first option fails - if
538 * we are doing multiple arguments, we can allow the first one
541 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
550 r2 = __handle_option(o, ptr2, data, 0, 0);
557 static struct fio_option *get_option(const char *opt,
558 struct fio_option *options,
559 struct flist_head *eops, char **post)
561 struct fio_option *o;
564 ret = strchr(opt, '=');
570 strip_blank_end(ret);
571 o = find_option(options, eops, ret);
573 o = find_option(options, eops, opt);
580 static int opt_cmp(const void *p1, const void *p2)
582 struct fio_option *o1, *o2;
586 s1 = strdup(*((char **) p1));
587 s2 = strdup(*((char **) p2));
589 o1 = get_option(s1, fio_options, NULL, &foo);
590 o2 = get_option(s2, fio_options, NULL, &foo);
600 return prio2 - prio1;
603 void sort_options(char **opts, struct fio_option *options, int num_opts)
605 fio_options = options;
606 qsort(opts, num_opts, sizeof(char *), opt_cmp);
610 int parse_cmd_option(const char *opt, const char *val,
611 struct fio_option *options, struct flist_head *eops,
614 struct fio_option *o;
616 o = find_option(options, eops, opt);
618 fprintf(stderr, "Bad option <%s>\n", opt);
622 if (!handle_option(o, val, data))
625 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
630 * Return a copy of the input string with substrings of the form ${VARNAME}
631 * substituted with the value of the environment variable VARNAME. The
632 * substitution always occurs, even if VARNAME is empty or the corresponding
633 * environment variable undefined.
635 static char *option_dup_subs(const char *opt)
637 char out[OPT_LEN_MAX+1];
638 char in[OPT_LEN_MAX+1];
641 char *ch1, *ch2, *env;
642 ssize_t nchr = OPT_LEN_MAX;
645 if (strlen(in) + 1 > OPT_LEN_MAX) {
646 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
650 in[OPT_LEN_MAX] = '\0';
651 strncpy(in, opt, OPT_LEN_MAX);
653 while (*inptr && nchr > 0) {
654 if (inptr[0] == '$' && inptr[1] == '{') {
655 ch2 = strchr(inptr, '}');
656 if (ch2 && inptr+1 < ch2) {
663 envlen = strlen(env);
664 if (envlen <= nchr) {
665 memcpy(outptr, env, envlen);
675 *outptr++ = *inptr++;
683 int parse_option(const char *opt, struct fio_option *options,
684 struct flist_head *ext_opt_list, void *data)
686 struct fio_option *o;
689 tmp = option_dup_subs(opt);
693 o = get_option(tmp, options, ext_opt_list, &post);
695 fprintf(stderr, "Bad option <%s>\n", tmp);
700 if (!handle_option(o, post, data)) {
705 fprintf(stderr, "fio: failed parsing %s\n", opt);
711 * Option match, levenshtein distance. Handy for not quite remembering what
712 * the option name is.
714 static int string_distance(const char *s1, const char *s2)
716 unsigned int s1_len = strlen(s1);
717 unsigned int s2_len = strlen(s2);
718 unsigned int *p, *q, *r;
721 p = malloc(sizeof(unsigned int) * (s2_len + 1));
722 q = malloc(sizeof(unsigned int) * (s2_len + 1));
725 for (i = 1; i <= s2_len; i++)
728 for (i = 1; i <= s1_len; i++) {
730 for (j = 1; j <= s2_len; j++) {
731 unsigned int sub = p[j - 1];
733 if (s1[i - 1] != s2[j - 1])
736 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
749 static struct fio_option *find_child(struct fio_option *options,
750 struct fio_option *o)
752 struct fio_option *__o;
754 for (__o = options + 1; __o->name; __o++)
755 if (__o->parent && !strcmp(__o->parent, o->name))
761 static void __print_option(struct fio_option *o, struct fio_option *org,
775 p += sprintf(p, "%s", " ");
777 sprintf(p, "%s", o->name);
779 printf("%-24s: %s\n", name, o->help);
782 static void print_option(struct fio_option *o)
784 struct fio_option *parent;
785 struct fio_option *__o;
786 unsigned int printed;
789 __print_option(o, NULL, 0);
796 while ((__o = find_child(o, parent)) != NULL) {
797 __print_option(__o, o, level);
806 int show_cmd_help(struct fio_option *options, const char *name)
808 struct fio_option *o, *closest;
809 unsigned int best_dist;
813 if (!name || !strcmp(name, "all"))
818 for (o = &options[0]; o->name; o++) {
821 if (o->type == FIO_OPT_DEPRECATED)
825 if (!strcmp(name, o->name) ||
826 (o->alias && !strcmp(name, o->alias)))
831 dist = string_distance(name, o->name);
832 if (dist < best_dist) {
839 if (show_all || match) {
842 printf("%24s: %s\n", o->name, o->help);
853 show_option_help(o, stdout);
859 printf("No such command: %s", name);
861 printf(" - showing closest match\n");
862 printf("%20s: %s\n", closest->name, closest->help);
863 show_option_help(closest, stdout);
871 * Handle parsing of default parameters.
873 void fill_default_options(void *data, struct fio_option *options)
875 struct fio_option *o;
877 dprint(FD_PARSE, "filling default options\n");
879 for (o = &options[0]; o->name; o++)
881 handle_option(o, o->def, data);
884 void option_init(struct fio_option *o)
886 if (o->type == FIO_OPT_DEPRECATED)
888 if (o->type == FIO_OPT_BOOL) {
892 if (o->type == FIO_OPT_STR_SET && o->def) {
893 fprintf(stderr, "Option %s: string set option with"
894 " default will always be true\n", o->name);
896 if (!o->cb && !o->off1) {
897 fprintf(stderr, "Option %s: neither cb nor offset given\n",
900 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
902 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
903 fprintf(stderr, "Option %s: both cb and offset given\n",
909 * Sanitize the options structure. For now it just sets min/max for bool
910 * values and whether both callback and offsets are given.
912 void options_init(struct fio_option *options)
914 struct fio_option *o;
916 dprint(FD_PARSE, "init options\n");
918 for (o = &options[0]; o->name; o++)