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))) {
331 *(unsigned int *) o->roff1 = vp->oval;
335 val_store(ilp, vp->oval, o->off1, data);
342 show_option_values(o);
347 case FIO_OPT_STR_VAL_TIME:
350 case FIO_OPT_STR_VAL: {
351 fio_opt_str_val_fn *fn = o->cb;
354 ret = check_str_time(ptr, &ull);
356 ret = check_str_bytes(ptr, &ull, data);
361 if (o->maxval && ull > o->maxval) {
362 fprintf(stderr, "max value out of range: %lld"
363 " (%d max)\n", ull, o->maxval);
366 if (o->minval && ull < o->minval) {
367 fprintf(stderr, "min value out of range: %lld"
368 " (%d min)\n", ull, o->minval);
373 ret = fn(data, &ull);
375 if (o->type == FIO_OPT_INT) {
378 *(unsigned long long *) o->roff1 = ull;
380 val_store(ilp, ull, o->off1, data);
384 *(unsigned long long *) o->roff2 = ull;
386 val_store(ilp, ull, o->off2, data);
391 *(unsigned long long *) o->roff1 = ull;
393 val_store(ullp, ull, o->off1, data);
397 *(unsigned long long *) o->roff2 = ull;
399 val_store(ullp, ull, o->off2, data);
405 case FIO_OPT_STR_STORE: {
406 fio_opt_str_fn *fn = o->cb;
409 cp = (char **) o->roff1;
411 cp = td_var(data, o->off1);
423 case FIO_OPT_RANGE: {
427 strncpy(tmp, ptr, sizeof(tmp) - 1);
429 p1 = strchr(tmp, '-');
431 p1 = strchr(tmp, ':');
443 if (!check_range_bytes(p1, &ul1, data) &&
444 !check_range_bytes(p2, &ul2, data)) {
447 unsigned long foo = ul1;
455 *(unsigned long *) o->roff1 = ul1;
457 val_store(ilp, ul1, o->off1, data);
459 *(unsigned long *) o->roff2 = ul2;
461 val_store(ilp, ul2, o->off2, data);
463 if (o->roff3 && o->roff4) {
464 *(unsigned long *) o->roff3 = ul1;
465 *(unsigned long *) o->roff4 = ul2;
466 } else if (o->off3 && o->off4) {
467 val_store(ilp, ul1, o->off3, data);
468 val_store(ilp, ul2, o->off4, data);
475 fio_opt_int_fn *fn = o->cb;
477 ret = check_int(ptr, &il);
481 if (o->maxval && il > (int) o->maxval) {
482 fprintf(stderr, "max value out of range: %d (%d max)\n",
486 if (o->minval && il < o->minval) {
487 fprintf(stderr, "min value out of range: %d (%d min)\n",
500 *(unsigned int *)o->roff1 = il;
502 val_store(ilp, il, o->off1, data);
506 *(unsigned int *) o->roff2 = il;
508 val_store(ilp, il, o->off2, data);
513 case FIO_OPT_STR_SET: {
514 fio_opt_str_set_fn *fn = o->cb;
521 *(unsigned int *) o->roff1 = 1;
523 val_store(ilp, 1, o->off1, data);
527 *(unsigned int *) o->roff2 = 1;
529 val_store(ilp, 1, o->off2, data);
534 case FIO_OPT_DEPRECATED:
535 fprintf(stdout, "Option %s is deprecated\n", o->name);
538 fprintf(stderr, "Bad option type %u\n", o->type);
546 ret = o->verify(o, data);
548 fprintf(stderr,"Correct format for offending option\n");
549 fprintf(stderr, "%20s: %s\n", o->name, o->help);
550 show_option_help(o, stderr);
557 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
559 char *ptr, *ptr2 = NULL;
562 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
569 * See if we have a second set of parameters, hidden after a comma.
570 * Do this before parsing the first round, to check if we should
571 * copy set 1 options to set 2.
574 (o->type != FIO_OPT_STR_STORE) &&
575 (o->type != FIO_OPT_STR)) {
576 ptr2 = strchr(ptr, ',');
577 if (ptr2 && *(ptr2 + 1) == '\0')
580 ptr2 = strchr(ptr, ':');
582 ptr2 = strchr(ptr, '-');
586 * Don't return early if parsing the first option fails - if
587 * we are doing multiple arguments, we can allow the first one
590 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
599 r2 = __handle_option(o, ptr2, data, 0, 0);
606 static struct fio_option *get_option(const char *opt,
607 struct fio_option *options,
608 struct flist_head *eops, char **post)
610 struct fio_option *o;
613 ret = strchr(opt, '=');
619 strip_blank_end(ret);
620 o = find_option(options, eops, ret);
622 o = find_option(options, eops, opt);
629 static int opt_cmp(const void *p1, const void *p2)
631 struct fio_option *o1, *o2;
635 s1 = strdup(*((char **) p1));
636 s2 = strdup(*((char **) p2));
638 o1 = get_option(s1, fio_options, NULL, &foo);
639 o2 = get_option(s2, fio_options, NULL, &foo);
649 return prio2 - prio1;
652 void sort_options(char **opts, struct fio_option *options, int num_opts)
654 fio_options = options;
655 qsort(opts, num_opts, sizeof(char *), opt_cmp);
659 int parse_cmd_option(const char *opt, const char *val,
660 struct fio_option *options, struct flist_head *eops,
663 struct fio_option *o;
665 o = find_option(options, eops, opt);
667 fprintf(stderr, "Bad option <%s>\n", opt);
671 if (!handle_option(o, val, data))
674 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
679 * Return a copy of the input string with substrings of the form ${VARNAME}
680 * substituted with the value of the environment variable VARNAME. The
681 * substitution always occurs, even if VARNAME is empty or the corresponding
682 * environment variable undefined.
684 static char *option_dup_subs(const char *opt)
686 char out[OPT_LEN_MAX+1];
687 char in[OPT_LEN_MAX+1];
690 char *ch1, *ch2, *env;
691 ssize_t nchr = OPT_LEN_MAX;
694 if (strlen(in) + 1 > OPT_LEN_MAX) {
695 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
699 in[OPT_LEN_MAX] = '\0';
700 strncpy(in, opt, OPT_LEN_MAX);
702 while (*inptr && nchr > 0) {
703 if (inptr[0] == '$' && inptr[1] == '{') {
704 ch2 = strchr(inptr, '}');
705 if (ch2 && inptr+1 < ch2) {
712 envlen = strlen(env);
713 if (envlen <= nchr) {
714 memcpy(outptr, env, envlen);
724 *outptr++ = *inptr++;
732 int parse_option(const char *opt, struct fio_option *options,
733 struct flist_head *ext_opt_list, void *data)
735 struct fio_option *o;
738 tmp = option_dup_subs(opt);
742 o = get_option(tmp, options, ext_opt_list, &post);
744 fprintf(stderr, "Bad option <%s>\n", tmp);
749 if (!handle_option(o, post, data)) {
754 fprintf(stderr, "fio: failed parsing %s\n", opt);
760 * Option match, levenshtein distance. Handy for not quite remembering what
761 * the option name is.
763 static int string_distance(const char *s1, const char *s2)
765 unsigned int s1_len = strlen(s1);
766 unsigned int s2_len = strlen(s2);
767 unsigned int *p, *q, *r;
770 p = malloc(sizeof(unsigned int) * (s2_len + 1));
771 q = malloc(sizeof(unsigned int) * (s2_len + 1));
774 for (i = 1; i <= s2_len; i++)
777 for (i = 1; i <= s1_len; i++) {
779 for (j = 1; j <= s2_len; j++) {
780 unsigned int sub = p[j - 1];
782 if (s1[i - 1] != s2[j - 1])
785 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
798 static struct fio_option *find_child(struct fio_option *options,
799 struct fio_option *o)
801 struct fio_option *__o;
803 for (__o = options + 1; __o->name; __o++)
804 if (__o->parent && !strcmp(__o->parent, o->name))
810 static void __print_option(struct fio_option *o, struct fio_option *org,
824 p += sprintf(p, "%s", " ");
826 sprintf(p, "%s", o->name);
828 printf("%-24s: %s\n", name, o->help);
831 static void print_option(struct fio_option *o)
833 struct fio_option *parent;
834 struct fio_option *__o;
835 unsigned int printed;
838 __print_option(o, NULL, 0);
845 while ((__o = find_child(o, parent)) != NULL) {
846 __print_option(__o, o, level);
855 int show_cmd_help(struct fio_option *options, const char *name)
857 struct fio_option *o, *closest;
858 unsigned int best_dist;
862 if (!name || !strcmp(name, "all"))
867 for (o = &options[0]; o->name; o++) {
870 if (o->type == FIO_OPT_DEPRECATED)
874 if (!strcmp(name, o->name) ||
875 (o->alias && !strcmp(name, o->alias)))
880 dist = string_distance(name, o->name);
881 if (dist < best_dist) {
888 if (show_all || match) {
891 printf("%24s: %s\n", o->name, o->help);
902 show_option_help(o, stdout);
908 printf("No such command: %s", name);
910 printf(" - showing closest match\n");
911 printf("%20s: %s\n", closest->name, closest->help);
912 show_option_help(closest, stdout);
920 * Handle parsing of default parameters.
922 void fill_default_options(void *data, struct fio_option *options)
924 struct fio_option *o;
926 dprint(FD_PARSE, "filling default options\n");
928 for (o = &options[0]; o->name; o++)
930 handle_option(o, o->def, data);
933 void option_init(struct fio_option *o)
935 if (o->type == FIO_OPT_DEPRECATED)
937 if (o->type == FIO_OPT_BOOL) {
941 if (o->type == FIO_OPT_STR_SET && o->def) {
942 fprintf(stderr, "Option %s: string set option with"
943 " default will always be true\n", o->name);
945 if (!o->cb && !o->off1) {
946 fprintf(stderr, "Option %s: neither cb nor offset given\n",
949 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
951 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
952 fprintf(stderr, "Option %s: both cb and offset given\n",
958 * Sanitize the options structure. For now it just sets min/max for bool
959 * values and whether both callback and offsets are given.
961 void options_init(struct fio_option *options)
963 struct fio_option *o;
965 dprint(FD_PARSE, "init options\n");
967 for (o = &options[0]; o->name; o++)