2 * This file contains the ini and command liner parser main.
16 static struct fio_option *fio_options;
17 extern unsigned int fio_get_kb_base(void *);
19 static int vp_cmp(const void *p1, const void *p2)
21 const struct value_pair *vp1 = p1;
22 const struct value_pair *vp2 = p2;
24 return strlen(vp2->ival) - strlen(vp1->ival);
27 static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
29 const struct value_pair *vp;
32 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
34 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
35 vp = &o->posval[entries];
36 if (!vp->ival || vp->ival[0] == '\0')
39 memcpy(&vpmap[entries], vp, sizeof(*vp));
42 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
45 static void show_option_range(struct fio_option *o, FILE *out)
47 if (!o->minval && !o->maxval)
50 fprintf(out, "%20s: min=%d", "range", o->minval);
52 fprintf(out, ", max=%d", o->maxval);
56 static void show_option_values(struct fio_option *o)
61 const struct value_pair *vp = &o->posval[i];
66 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
68 printf(" %s", vp->help);
71 } while (i < PARSE_MAX_VP);
77 static void show_option_help(struct fio_option *o, FILE *out)
79 const char *typehelp[] = {
81 "string with possible k/m/g postfix (opt=4k)",
82 "string with time postfix (opt=10s)",
84 "string with dual range (opt=1k-4k,4k-8k)",
85 "integer value (opt=100)",
86 "boolean value (opt=1)",
91 fprintf(out, "%20s: %s\n", "alias", o->alias);
93 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
94 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
95 show_option_range(o, stdout);
96 show_option_values(o);
99 static unsigned long get_mult_time(char c)
116 static unsigned long long get_mult_bytes(char c, void *data)
118 unsigned int kb_base = fio_get_kb_base(data);
119 unsigned long long ret = 1;
126 ret *= (unsigned long long) kb_base;
129 ret *= (unsigned long long) kb_base;
132 ret *= (unsigned long long) kb_base;
135 ret *= (unsigned long long) kb_base;
138 ret *= (unsigned long long) kb_base;
146 * convert string into decimal value, noting any size suffix
148 int str_to_decimal(const char *str, long long *val, int kilo, void *data)
156 if (strstr(str, "0x") || strstr(str, "0X"))
161 *val = strtoll(str, NULL, base);
162 if (*val == LONG_MAX && errno == ERANGE)
168 * if the last char is 'b' or 'B', the user likely used
169 * "1gb" instead of just "1g". If the second to last is also
173 if ((*p == 'b' || *p == 'B') && isalpha(*(p - 1)))
176 *val *= get_mult_bytes(*p, data);
178 *val *= get_mult_time(str[len - 1]);
183 static int check_str_bytes(const char *p, long long *val, void *data)
185 return str_to_decimal(p, val, 1, data);
188 static int check_str_time(const char *p, long long *val)
190 return str_to_decimal(p, val, 0, NULL);
193 void strip_blank_front(char **p)
203 void strip_blank_end(char *p)
217 while ((isspace(*s) || iscntrl(*s)) && (s > start))
223 static int check_range_bytes(const char *str, long *val, void *data)
230 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
231 *val *= get_mult_bytes(suffix, data);
235 if (sscanf(str, "%lu", val) == 1)
241 static int check_int(const char *p, int *val)
245 if (strstr(p, "0x") || strstr(p, "0X")) {
246 if (sscanf(p, "%x", val) == 1)
249 if (sscanf(p, "%u", val) == 1)
256 static struct fio_option *find_option(struct fio_option *options,
259 struct fio_option *o;
261 for (o = &options[0]; o->name; o++) {
262 if (!strcmp(o->name, opt))
264 else if (o->alias && !strcmp(o->alias, opt))
271 #define val_store(ptr, val, off, data) \
273 ptr = td_var((data), (off)); \
277 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
281 long long ull, *ullp;
284 int ret = 0, is_time = 0;
286 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
289 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
290 fprintf(stderr, "Option %s requires an argument\n", o->name);
296 fio_opt_str_fn *fn = o->cb;
297 const struct value_pair *vp;
298 struct value_pair posval[PARSE_MAX_VP];
301 posval_sort(o, posval);
303 for (i = 0; i < PARSE_MAX_VP; i++) {
305 if (!vp->ival || vp->ival[0] == '\0')
308 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
312 val_store(ilp, vp->oval, o->off1, data);
318 show_option_values(o);
323 case FIO_OPT_STR_VAL_TIME:
326 case FIO_OPT_STR_VAL: {
327 fio_opt_str_val_fn *fn = o->cb;
330 ret = check_str_time(ptr, &ull);
332 ret = check_str_bytes(ptr, &ull, data);
337 if (o->maxval && ull > o->maxval) {
338 fprintf(stderr, "max value out of range: %lld"
339 " (%d max)\n", ull, o->maxval);
342 if (o->minval && ull < o->minval) {
343 fprintf(stderr, "min value out of range: %lld"
344 " (%d min)\n", ull, o->minval);
349 ret = fn(data, &ull);
351 if (o->type == FIO_OPT_INT) {
353 val_store(ilp, ull, o->off1, data);
354 if (!more && o->off2)
355 val_store(ilp, ull, o->off2, data);
358 val_store(ullp, ull, o->off1, data);
359 if (!more && o->off2)
360 val_store(ullp, ull, o->off2, data);
365 case FIO_OPT_STR_STORE: {
366 fio_opt_str_fn *fn = o->cb;
368 cp = td_var(data, o->off1);
379 case FIO_OPT_RANGE: {
383 strncpy(tmp, ptr, sizeof(tmp) - 1);
385 p1 = strchr(tmp, '-');
387 p1 = strchr(tmp, ':');
399 if (!check_range_bytes(p1, &ul1, data) &&
400 !check_range_bytes(p2, &ul2, data)) {
403 unsigned long foo = ul1;
410 val_store(ilp, ul1, o->off1, data);
411 val_store(ilp, ul2, o->off2, data);
413 if (o->off3 && o->off4) {
414 val_store(ilp, ul1, o->off3, data);
415 val_store(ilp, ul2, o->off4, data);
422 fio_opt_int_fn *fn = o->cb;
424 ret = check_int(ptr, &il);
428 if (o->maxval && il > (int) o->maxval) {
429 fprintf(stderr, "max value out of range: %d (%d max)\n",
433 if (o->minval && il < o->minval) {
434 fprintf(stderr, "min value out of range: %d (%d min)\n",
446 val_store(ilp, il, o->off1, data);
447 if (!more && o->off2)
448 val_store(ilp, il, o->off2, data);
452 case FIO_OPT_STR_SET: {
453 fio_opt_str_set_fn *fn = o->cb;
459 val_store(ilp, 1, o->off1, data);
460 if (!more && o->off2)
461 val_store(ilp, 1, o->off2, data);
465 case FIO_OPT_DEPRECATED:
466 fprintf(stdout, "Option %s is deprecated\n", o->name);
469 fprintf(stderr, "Bad option type %u\n", o->type);
477 ret = o->verify(o, data);
479 fprintf(stderr,"Correct format for offending option\n");
480 fprintf(stderr, "%20s: %s\n", o->name, o->help);
481 show_option_help(o, stderr);
488 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
490 char *ptr, *ptr2 = NULL;
493 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
500 * See if we have a second set of parameters, hidden after a comma.
501 * Do this before parsing the first round, to check if we should
502 * copy set 1 options to set 2.
505 (o->type != FIO_OPT_STR_STORE) &&
506 (o->type != FIO_OPT_STR)) {
507 ptr2 = strchr(ptr, ',');
508 if (ptr2 && *(ptr2 + 1) == '\0')
511 ptr2 = strchr(ptr, ':');
513 ptr2 = strchr(ptr, '-');
517 * Don't return early if parsing the first option fails - if
518 * we are doing multiple arguments, we can allow the first one
521 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
530 r2 = __handle_option(o, ptr2, data, 0, 0);
537 static struct fio_option *get_option(const char *opt,
538 struct fio_option *options, char **post)
540 struct fio_option *o;
543 ret = strchr(opt, '=');
549 strip_blank_end(ret);
550 o = find_option(options, ret);
552 o = find_option(options, opt);
559 static int opt_cmp(const void *p1, const void *p2)
561 struct fio_option *o1, *o2;
565 s1 = strdup(*((char **) p1));
566 s2 = strdup(*((char **) p2));
568 o1 = get_option(s1, fio_options, &foo);
569 o2 = get_option(s2, fio_options, &foo);
579 return prio2 - prio1;
582 void sort_options(char **opts, struct fio_option *options, int num_opts)
584 fio_options = options;
585 qsort(opts, num_opts, sizeof(char *), opt_cmp);
589 int parse_cmd_option(const char *opt, const char *val,
590 struct fio_option *options, void *data)
592 struct fio_option *o;
594 o = find_option(options, opt);
596 fprintf(stderr, "Bad option <%s>\n", opt);
600 if (!handle_option(o, val, data))
603 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
608 * Return a copy of the input string with substrings of the form ${VARNAME}
609 * substituted with the value of the environment variable VARNAME. The
610 * substitution always occurs, even if VARNAME is empty or the corresponding
611 * environment variable undefined.
613 static char *option_dup_subs(const char *opt)
615 char out[OPT_LEN_MAX+1];
616 char in[OPT_LEN_MAX+1];
619 char *ch1, *ch2, *env;
620 ssize_t nchr = OPT_LEN_MAX;
623 in[OPT_LEN_MAX] = '\0';
624 strncpy(in, opt, OPT_LEN_MAX);
626 while (*inptr && nchr > 0) {
627 if (inptr[0] == '$' && inptr[1] == '{') {
628 ch2 = strchr(inptr, '}');
629 if (ch2 && inptr+1 < ch2) {
636 envlen = strlen(env);
637 if (envlen <= nchr) {
638 memcpy(outptr, env, envlen);
648 *outptr++ = *inptr++;
656 int parse_option(const char *opt, struct fio_option *options, void *data)
658 struct fio_option *o;
661 tmp = option_dup_subs(opt);
663 o = get_option(tmp, options, &post);
665 fprintf(stderr, "Bad option <%s>\n", tmp);
670 if (!handle_option(o, post, data)) {
675 fprintf(stderr, "fio: failed parsing %s\n", opt);
681 * Option match, levenshtein distance. Handy for not quite remembering what
682 * the option name is.
684 static int string_distance(const char *s1, const char *s2)
686 unsigned int s1_len = strlen(s1);
687 unsigned int s2_len = strlen(s2);
688 unsigned int *p, *q, *r;
691 p = malloc(sizeof(unsigned int) * (s2_len + 1));
692 q = malloc(sizeof(unsigned int) * (s2_len + 1));
695 for (i = 1; i <= s2_len; i++)
698 for (i = 1; i <= s1_len; i++) {
700 for (j = 1; j <= s2_len; j++) {
701 unsigned int sub = p[j - 1];
703 if (s1[i - 1] != s2[j - 1])
706 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
719 static struct fio_option *find_child(struct fio_option *options,
720 struct fio_option *o)
722 struct fio_option *__o;
724 for (__o = options + 1; __o->name; __o++)
725 if (__o->parent && !strcmp(__o->parent, o->name))
731 static void __print_option(struct fio_option *o, struct fio_option *org,
745 p += sprintf(p, "%s", " ");
747 sprintf(p, "%s", o->name);
749 printf("%-24s: %s\n", name, o->help);
752 static void print_option(struct fio_option *o)
754 struct fio_option *parent;
755 struct fio_option *__o;
756 unsigned int printed;
759 __print_option(o, NULL, 0);
766 while ((__o = find_child(o, parent)) != NULL) {
767 __print_option(__o, o, level);
776 int show_cmd_help(struct fio_option *options, const char *name)
778 struct fio_option *o, *closest;
779 unsigned int best_dist;
783 if (!name || !strcmp(name, "all"))
788 for (o = &options[0]; o->name; o++) {
791 if (o->type == FIO_OPT_DEPRECATED)
795 if (!strcmp(name, o->name) ||
796 (o->alias && !strcmp(name, o->alias)))
801 dist = string_distance(name, o->name);
802 if (dist < best_dist) {
809 if (show_all || match) {
812 printf("%24s: %s\n", o->name, o->help);
823 show_option_help(o, stdout);
829 printf("No such command: %s", name);
831 printf(" - showing closest match\n");
832 printf("%20s: %s\n", closest->name, closest->help);
833 show_option_help(closest, stdout);
841 * Handle parsing of default parameters.
843 void fill_default_options(void *data, struct fio_option *options)
845 struct fio_option *o;
847 dprint(FD_PARSE, "filling default options\n");
849 for (o = &options[0]; o->name; o++)
851 handle_option(o, o->def, data);
855 * Sanitize the options structure. For now it just sets min/max for bool
856 * values and whether both callback and offsets are given.
858 void options_init(struct fio_option *options)
860 struct fio_option *o;
862 dprint(FD_PARSE, "init options\n");
864 for (o = &options[0]; o->name; o++) {
865 if (o->type == FIO_OPT_DEPRECATED)
867 if (o->type == FIO_OPT_BOOL) {
871 if (o->type == FIO_OPT_STR_SET && o->def) {
872 fprintf(stderr, "Option %s: string set option with"
873 " default will always be true\n",
876 if (!o->cb && !o->off1) {
877 fprintf(stderr, "Option %s: neither cb nor offset"
878 " given\n", o->name);
880 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
882 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
883 fprintf(stderr, "Option %s: both cb and offset given\n",