2 * This file contains the ini and command liner parser main.
16 static struct fio_option *fio_options;
17 extern unsigned int fio_kb_base;
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)
118 unsigned long long ret = 1;
125 ret *= (unsigned long long) fio_kb_base;
128 ret *= (unsigned long long) fio_kb_base;
131 ret *= (unsigned long long) fio_kb_base;
134 ret *= (unsigned long long) fio_kb_base;
137 ret *= (unsigned long long) fio_kb_base;
145 * convert string into decimal value, noting any size suffix
147 int str_to_decimal(const char *str, long long *val, int kilo)
155 if (strstr(str, "0x") || strstr(str, "0X"))
160 *val = strtoll(str, NULL, base);
161 if (*val == LONG_MAX && errno == ERANGE)
165 *val *= get_mult_bytes(str[len - 1]);
167 *val *= get_mult_time(str[len - 1]);
172 static int check_str_bytes(const char *p, long long *val)
174 return str_to_decimal(p, val, 1);
177 static int check_str_time(const char *p, long long *val)
179 return str_to_decimal(p, val, 0);
182 void strip_blank_front(char **p)
192 void strip_blank_end(char *p)
206 while ((isspace(*s) || iscntrl(*s)) && (s > start))
212 static int check_range_bytes(const char *str, long *val)
219 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
220 *val *= get_mult_bytes(suffix);
224 if (sscanf(str, "%lu", val) == 1)
230 static int check_int(const char *p, int *val)
234 if (strstr(p, "0x") || strstr(p, "0X")) {
235 if (sscanf(p, "%x", val) == 1)
238 if (sscanf(p, "%u", val) == 1)
245 static struct fio_option *find_option(struct fio_option *options,
248 struct fio_option *o;
250 for (o = &options[0]; o->name; o++) {
251 if (!strcmp(o->name, opt))
253 else if (o->alias && !strcmp(o->alias, opt))
260 #define val_store(ptr, val, off, data) \
262 ptr = td_var((data), (off)); \
266 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
270 long long ull, *ullp;
273 int ret = 0, is_time = 0;
275 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
278 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
279 fprintf(stderr, "Option %s requires an argument\n", o->name);
285 fio_opt_str_fn *fn = o->cb;
286 const struct value_pair *vp;
287 struct value_pair posval[PARSE_MAX_VP];
290 posval_sort(o, posval);
292 for (i = 0; i < PARSE_MAX_VP; i++) {
294 if (!vp->ival || vp->ival[0] == '\0')
297 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
301 val_store(ilp, vp->oval, o->off1, data);
307 show_option_values(o);
312 case FIO_OPT_STR_VAL_TIME:
315 case FIO_OPT_STR_VAL: {
316 fio_opt_str_val_fn *fn = o->cb;
319 ret = check_str_time(ptr, &ull);
321 ret = check_str_bytes(ptr, &ull);
326 if (o->maxval && ull > o->maxval) {
327 fprintf(stderr, "max value out of range: %lld"
328 " (%d max)\n", ull, o->maxval);
331 if (o->minval && ull < o->minval) {
332 fprintf(stderr, "min value out of range: %lld"
333 " (%d min)\n", ull, o->minval);
338 ret = fn(data, &ull);
340 if (o->type == FIO_OPT_INT) {
342 val_store(ilp, ull, o->off1, data);
343 if (!more && o->off2)
344 val_store(ilp, ull, o->off2, data);
347 val_store(ullp, ull, o->off1, data);
348 if (!more && o->off2)
349 val_store(ullp, ull, o->off2, data);
354 case FIO_OPT_STR_STORE: {
355 fio_opt_str_fn *fn = o->cb;
357 cp = td_var(data, o->off1);
368 case FIO_OPT_RANGE: {
372 strncpy(tmp, ptr, sizeof(tmp) - 1);
374 p1 = strchr(tmp, '-');
376 p1 = strchr(tmp, ':');
388 if (!check_range_bytes(p1, &ul1) &&
389 !check_range_bytes(p2, &ul2)) {
392 unsigned long foo = ul1;
399 val_store(ilp, ul1, o->off1, data);
400 val_store(ilp, ul2, o->off2, data);
402 if (o->off3 && o->off4) {
403 val_store(ilp, ul1, o->off3, data);
404 val_store(ilp, ul2, o->off4, data);
411 fio_opt_int_fn *fn = o->cb;
413 ret = check_int(ptr, &il);
417 if (o->maxval && il > (int) o->maxval) {
418 fprintf(stderr, "max value out of range: %d (%d max)\n",
422 if (o->minval && il < o->minval) {
423 fprintf(stderr, "min value out of range: %d (%d min)\n",
435 val_store(ilp, il, o->off1, data);
436 if (!more && o->off2)
437 val_store(ilp, il, o->off2, data);
441 case FIO_OPT_STR_SET: {
442 fio_opt_str_set_fn *fn = o->cb;
448 val_store(ilp, 1, o->off1, data);
449 if (!more && o->off2)
450 val_store(ilp, 1, o->off2, data);
454 case FIO_OPT_DEPRECATED:
455 fprintf(stdout, "Option %s is deprecated\n", o->name);
458 fprintf(stderr, "Bad option type %u\n", o->type);
466 ret = o->verify(o, data);
468 fprintf(stderr,"Correct format for offending option\n");
469 fprintf(stderr, "%20s: %s\n", o->name, o->help);
470 show_option_help(o, stderr);
477 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
479 char *ptr, *ptr2 = NULL;
482 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
489 * See if we have a second set of parameters, hidden after a comma.
490 * Do this before parsing the first round, to check if we should
491 * copy set 1 options to set 2.
494 (o->type != FIO_OPT_STR_STORE) &&
495 (o->type != FIO_OPT_STR)) {
496 ptr2 = strchr(ptr, ',');
497 if (ptr2 && *(ptr2 + 1) == '\0')
500 ptr2 = strchr(ptr, ':');
502 ptr2 = strchr(ptr, '-');
506 * Don't return early if parsing the first option fails - if
507 * we are doing multiple arguments, we can allow the first one
510 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
519 r2 = __handle_option(o, ptr2, data, 0, 0);
526 static struct fio_option *get_option(const char *opt,
527 struct fio_option *options, char **post)
529 struct fio_option *o;
532 ret = strchr(opt, '=');
538 strip_blank_end(ret);
539 o = find_option(options, ret);
541 o = find_option(options, opt);
548 static int opt_cmp(const void *p1, const void *p2)
550 struct fio_option *o1, *o2;
554 s1 = strdup(*((char **) p1));
555 s2 = strdup(*((char **) p2));
557 o1 = get_option(s1, fio_options, &foo);
558 o2 = get_option(s2, fio_options, &foo);
568 return prio2 - prio1;
571 void sort_options(char **opts, struct fio_option *options, int num_opts)
573 fio_options = options;
574 qsort(opts, num_opts, sizeof(char *), opt_cmp);
578 int parse_cmd_option(const char *opt, const char *val,
579 struct fio_option *options, void *data)
581 struct fio_option *o;
583 o = find_option(options, opt);
585 fprintf(stderr, "Bad option <%s>\n", opt);
589 if (!handle_option(o, val, data))
592 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
597 * Return a copy of the input string with substrings of the form ${VARNAME}
598 * substituted with the value of the environment variable VARNAME. The
599 * substitution always occurs, even if VARNAME is empty or the corresponding
600 * environment variable undefined.
602 static char *option_dup_subs(const char *opt)
604 char out[OPT_LEN_MAX+1];
605 char in[OPT_LEN_MAX+1];
608 char *ch1, *ch2, *env;
609 ssize_t nchr = OPT_LEN_MAX;
612 in[OPT_LEN_MAX] = '\0';
613 strncpy(in, opt, OPT_LEN_MAX);
615 while (*inptr && nchr > 0) {
616 if (inptr[0] == '$' && inptr[1] == '{') {
617 ch2 = strchr(inptr, '}');
618 if (ch2 && inptr+1 < ch2) {
625 envlen = strlen(env);
626 if (envlen <= nchr) {
627 memcpy(outptr, env, envlen);
637 *outptr++ = *inptr++;
645 int parse_option(const char *opt, struct fio_option *options, void *data)
647 struct fio_option *o;
650 tmp = option_dup_subs(opt);
652 o = get_option(tmp, options, &post);
654 fprintf(stderr, "Bad option <%s>\n", tmp);
659 if (!handle_option(o, post, data)) {
664 fprintf(stderr, "fio: failed parsing %s\n", opt);
670 * Option match, levenshtein distance. Handy for not quite remembering what
671 * the option name is.
673 static int string_distance(const char *s1, const char *s2)
675 unsigned int s1_len = strlen(s1);
676 unsigned int s2_len = strlen(s2);
677 unsigned int *p, *q, *r;
680 p = malloc(sizeof(unsigned int) * (s2_len + 1));
681 q = malloc(sizeof(unsigned int) * (s2_len + 1));
684 for (i = 1; i <= s2_len; i++)
687 for (i = 1; i <= s1_len; i++) {
689 for (j = 1; j <= s2_len; j++) {
690 unsigned int sub = p[j - 1];
692 if (s1[i - 1] != s2[j - 1])
695 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
708 static struct fio_option *find_child(struct fio_option *options,
709 struct fio_option *o)
711 struct fio_option *__o;
713 for (__o = options + 1; __o->name; __o++)
714 if (__o->parent && !strcmp(__o->parent, o->name))
720 static void __print_option(struct fio_option *o, struct fio_option *org,
734 p += sprintf(p, "%s", " ");
736 sprintf(p, "%s", o->name);
738 printf("%-24s: %s\n", name, o->help);
741 static void print_option(struct fio_option *o)
743 struct fio_option *parent;
744 struct fio_option *__o;
745 unsigned int printed;
748 __print_option(o, NULL, 0);
755 while ((__o = find_child(o, parent)) != NULL) {
756 __print_option(__o, o, level);
765 int show_cmd_help(struct fio_option *options, const char *name)
767 struct fio_option *o, *closest;
768 unsigned int best_dist;
772 if (!name || !strcmp(name, "all"))
777 for (o = &options[0]; o->name; o++) {
780 if (o->type == FIO_OPT_DEPRECATED)
784 if (!strcmp(name, o->name) ||
785 (o->alias && !strcmp(name, o->alias)))
790 dist = string_distance(name, o->name);
791 if (dist < best_dist) {
798 if (show_all || match) {
801 printf("%24s: %s\n", o->name, o->help);
812 show_option_help(o, stdout);
818 printf("No such command: %s", name);
820 printf(" - showing closest match\n");
821 printf("%20s: %s\n", closest->name, closest->help);
822 show_option_help(closest, stdout);
830 * Handle parsing of default parameters.
832 void fill_default_options(void *data, struct fio_option *options)
834 struct fio_option *o;
836 dprint(FD_PARSE, "filling default options\n");
838 for (o = &options[0]; o->name; o++)
840 handle_option(o, o->def, data);
844 * Sanitize the options structure. For now it just sets min/max for bool
845 * values and whether both callback and offsets are given.
847 void options_init(struct fio_option *options)
849 struct fio_option *o;
851 dprint(FD_PARSE, "init options\n");
853 for (o = &options[0]; o->name; o++) {
854 if (o->type == FIO_OPT_DEPRECATED)
856 if (o->type == FIO_OPT_BOOL) {
860 if (o->type == FIO_OPT_STR_SET && o->def) {
861 fprintf(stderr, "Option %s: string set option with"
862 " default will always be true\n",
865 if (!o->cb && !o->off1) {
866 fprintf(stderr, "Option %s: neither cb nor offset"
867 " given\n", o->name);
869 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
871 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
872 fprintf(stderr, "Option %s: both cb and offset given\n",