2 * This file contains the ini and command liner parser main.
16 static struct fio_option *fio_options;
18 static int vp_cmp(const void *p1, const void *p2)
20 const struct value_pair *vp1 = p1;
21 const struct value_pair *vp2 = p2;
23 return strlen(vp2->ival) - strlen(vp1->ival);
26 static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
28 const struct value_pair *vp;
31 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
33 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
34 vp = &o->posval[entries];
35 if (!vp->ival || vp->ival[0] == '\0')
38 memcpy(&vpmap[entries], vp, sizeof(*vp));
41 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
44 static void show_option_range(struct fio_option *o)
46 if (!o->minval && !o->maxval)
49 printf("%20s: min=%d, max=%d\n", "range", o->minval, o->maxval);
52 static void show_option_values(struct fio_option *o)
57 const struct value_pair *vp = &o->posval[i];
62 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
64 printf(" %s", vp->help);
67 } while (i < PARSE_MAX_VP);
73 static unsigned long get_mult_time(char c)
90 static unsigned long get_mult_bytes(char c)
101 return 1024 * 1024 * 1024;
104 return 1024 * 1024 * 1024 * 1024UL;
111 * convert string into decimal value, noting any size suffix
113 int str_to_decimal(const char *str, long long *val, int kilo)
121 *val = strtoll(str, NULL, 10);
122 if (*val == LONG_MAX && errno == ERANGE)
126 *val *= get_mult_bytes(str[len - 1]);
128 *val *= get_mult_time(str[len - 1]);
133 static int check_str_bytes(const char *p, long long *val)
135 return str_to_decimal(p, val, 1);
138 static int check_str_time(const char *p, long long *val)
140 return str_to_decimal(p, val, 0);
143 void strip_blank_front(char **p)
153 void strip_blank_end(char *p)
167 while ((isspace(*s) || iscntrl(*s)) && (s > p))
173 static int check_range_bytes(const char *str, long *val)
180 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
181 *val *= get_mult_bytes(suffix);
185 if (sscanf(str, "%lu", val) == 1)
191 static int check_int(const char *p, int *val)
195 if (strstr(p, "0x") || strstr(p, "0X")) {
196 if (sscanf(p, "%x", val) == 1)
199 if (sscanf(p, "%u", val) == 1)
206 static struct fio_option *find_option(struct fio_option *options,
209 struct fio_option *o;
211 for (o = &options[0]; o->name; o++) {
212 if (!strcmp(o->name, opt))
214 else if (o->alias && !strcmp(o->alias, opt))
221 #define val_store(ptr, val, off, data) \
223 ptr = td_var((data), (off)); \
227 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
231 long long ull, *ullp;
234 int ret = 0, is_time = 0;
236 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
239 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
240 fprintf(stderr, "Option %s requires an argument\n", o->name);
246 fio_opt_str_fn *fn = o->cb;
247 const struct value_pair *vp;
248 struct value_pair posval[PARSE_MAX_VP];
251 posval_sort(o, posval);
253 for (i = 0; i < PARSE_MAX_VP; i++) {
255 if (!vp->ival || vp->ival[0] == '\0')
258 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
262 val_store(ilp, vp->oval, o->off1, data);
268 show_option_values(o);
273 case FIO_OPT_STR_VAL_TIME:
275 case FIO_OPT_STR_VAL:
276 case FIO_OPT_STR_VAL_INT: {
277 fio_opt_str_val_fn *fn = o->cb;
280 ret = check_str_time(ptr, &ull);
282 ret = check_str_bytes(ptr, &ull);
287 if (o->maxval && ull > o->maxval) {
288 fprintf(stderr, "max value out of range: %lld"
289 " (%d max)\n", ull, o->maxval);
292 if (o->minval && ull < o->minval) {
293 fprintf(stderr, "min value out of range: %lld"
294 " (%d min)\n", ull, o->minval);
299 ret = fn(data, &ull);
301 if (o->type == FIO_OPT_STR_VAL_INT) {
303 val_store(ilp, ull, o->off1, data);
304 if (!more && o->off2)
305 val_store(ilp, ull, o->off2, data);
308 val_store(ullp, ull, o->off1, data);
309 if (!more && o->off2)
310 val_store(ullp, ull, o->off2, data);
315 case FIO_OPT_STR_STORE: {
316 fio_opt_str_fn *fn = o->cb;
318 cp = td_var(data, o->off1);
329 case FIO_OPT_RANGE: {
333 strncpy(tmp, ptr, sizeof(tmp) - 1);
335 p1 = strchr(tmp, '-');
337 p1 = strchr(tmp, ':');
349 if (!check_range_bytes(p1, &ul1) &&
350 !check_range_bytes(p2, &ul2)) {
353 unsigned long foo = ul1;
360 val_store(ilp, ul1, o->off1, data);
361 val_store(ilp, ul2, o->off2, data);
363 if (o->off3 && o->off4) {
364 val_store(ilp, ul1, o->off3, data);
365 val_store(ilp, ul2, o->off4, data);
373 fio_opt_int_fn *fn = o->cb;
375 ret = check_int(ptr, &il);
379 if (o->maxval && il > (int) o->maxval) {
380 fprintf(stderr, "max value out of range: %d (%d max)\n",
384 if (o->minval && il < o->minval) {
385 fprintf(stderr, "min value out of range: %d (%d min)\n",
397 val_store(ilp, il, o->off1, data);
398 if (!more && o->off2)
399 val_store(ilp, il, o->off2, data);
403 case FIO_OPT_STR_SET: {
404 fio_opt_str_set_fn *fn = o->cb;
410 val_store(ilp, 1, o->off1, data);
411 if (!more && o->off2)
412 val_store(ilp, 1, o->off2, data);
416 case FIO_OPT_DEPRECATED:
417 fprintf(stdout, "Option %s is deprecated\n", o->name);
420 fprintf(stderr, "Bad option type %u\n", o->type);
427 static int handle_option(struct fio_option *o, const char *ptr, void *data)
429 const char *ptr2 = NULL;
432 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, ptr);
435 * See if we have a second set of parameters, hidden after a comma.
436 * Do this before parsing the first round, to check if we should
437 * copy set 1 options to set 2.
440 (o->type != FIO_OPT_STR_STORE) &&
441 (o->type != FIO_OPT_STR)) {
442 ptr2 = strchr(ptr, ',');
444 ptr2 = strchr(ptr, ':');
446 ptr2 = strchr(ptr, '-');
450 * Don't return early if parsing the first option fails - if
451 * we are doing multiple arguments, we can allow the first one
454 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
460 r2 = __handle_option(o, ptr2, data, 0, 0);
465 static struct fio_option *get_option(const char *opt,
466 struct fio_option *options, char **post)
468 struct fio_option *o;
471 ret = strchr(opt, '=');
477 o = find_option(options, ret);
479 o = find_option(options, opt);
486 static int opt_cmp(const void *p1, const void *p2)
488 struct fio_option *o1, *o2;
492 s1 = strdup(*((char **) p1));
493 s2 = strdup(*((char **) p2));
495 o1 = get_option(s1, fio_options, &foo);
496 o2 = get_option(s2, fio_options, &foo);
506 return prio2 - prio1;
509 void sort_options(char **opts, struct fio_option *options, int num_opts)
511 fio_options = options;
512 qsort(opts, num_opts, sizeof(char *), opt_cmp);
516 int parse_cmd_option(const char *opt, const char *val,
517 struct fio_option *options, void *data)
519 struct fio_option *o;
521 o = find_option(options, opt);
523 fprintf(stderr, "Bad option %s\n", opt);
527 if (!handle_option(o, val, data))
530 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
535 * Return a copy of the input string with substrings of the form ${VARNAME}
536 * substituted with the value of the environment variable VARNAME. The
537 * substitution always occurs, even if VARNAME is empty or the corresponding
538 * environment variable undefined.
540 static char *option_dup_subs(const char *opt)
542 char out[OPT_LEN_MAX+1];
543 char in[OPT_LEN_MAX+1];
546 char *ch1, *ch2, *env;
547 ssize_t nchr = OPT_LEN_MAX;
550 in[OPT_LEN_MAX] = '\0';
551 strncpy(in, opt, OPT_LEN_MAX);
553 while (*inptr && nchr > 0) {
554 if (inptr[0] == '$' && inptr[1] == '{') {
555 ch2 = strchr(inptr, '}');
556 if (ch2 && inptr+1 < ch2) {
563 envlen = strlen(env);
564 if (envlen <= nchr) {
565 memcpy(outptr, env, envlen);
575 *outptr++ = *inptr++;
583 int parse_option(const char *opt, struct fio_option *options, void *data)
585 struct fio_option *o;
588 tmp = option_dup_subs(opt);
590 o = get_option(tmp, options, &post);
592 fprintf(stderr, "Bad option %s\n", tmp);
597 if (!handle_option(o, post, data)) {
602 fprintf(stderr, "fio: failed parsing %s\n", opt);
608 * Option match, levenshtein distance. Handy for not quite remembering what
609 * the option name is.
611 static int string_distance(const char *s1, const char *s2)
613 unsigned int s1_len = strlen(s1);
614 unsigned int s2_len = strlen(s2);
615 unsigned int *p, *q, *r;
618 p = malloc(sizeof(unsigned int) * (s2_len + 1));
619 q = malloc(sizeof(unsigned int) * (s2_len + 1));
622 for (i = 1; i <= s2_len; i++)
625 for (i = 1; i <= s1_len; i++) {
627 for (j = 1; j <= s2_len; j++) {
628 unsigned int sub = p[j - 1];
630 if (s1[i - 1] != s2[j - 1])
633 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
646 static void show_option_help(struct fio_option *o)
648 const char *typehelp[] = {
650 "string with possible k/m/g postfix (opt=4k)",
651 "string with range and postfix (opt=1k-4k)",
652 "string with time postfix (opt=10s)",
654 "string with dual range (opt=1k-4k,4k-8k)",
655 "integer value (opt=100)",
656 "boolean value (opt=1)",
661 printf("%20s: %s\n", "alias", o->alias);
663 printf("%20s: %s\n", "type", typehelp[o->type]);
664 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
665 show_option_range(o);
666 show_option_values(o);
669 static struct fio_option *find_child(struct fio_option *options,
670 struct fio_option *o)
672 struct fio_option *__o;
674 for (__o = options + 1; __o->name; __o++)
675 if (__o->parent && !strcmp(__o->parent, o->name))
681 static void __print_option(struct fio_option *o, struct fio_option *org,
695 p += sprintf(p, "%s", " ");
697 sprintf(p, "%s", o->name);
699 printf("%-24s: %s\n", name, o->help);
702 static void print_option(struct fio_option *o)
704 struct fio_option *parent;
705 struct fio_option *__o;
706 unsigned int printed;
709 __print_option(o, NULL, 0);
716 while ((__o = find_child(o, parent)) != NULL) {
717 __print_option(__o, o, level);
726 int show_cmd_help(struct fio_option *options, const char *name)
728 struct fio_option *o, *closest;
729 unsigned int best_dist;
733 if (!name || !strcmp(name, "all"))
738 for (o = &options[0]; o->name; o++) {
741 if (o->type == FIO_OPT_DEPRECATED)
745 if (!strcmp(name, o->name) ||
746 (o->alias && !strcmp(name, o->alias)))
751 dist = string_distance(name, o->name);
752 if (dist < best_dist) {
759 if (show_all || match) {
762 printf("%24s: %s\n", o->name, o->help);
779 printf("No such command: %s", name);
781 printf(" - showing closest match\n");
782 printf("%20s: %s\n", closest->name, closest->help);
783 show_option_help(closest);
791 * Handle parsing of default parameters.
793 void fill_default_options(void *data, struct fio_option *options)
795 struct fio_option *o;
797 dprint(FD_PARSE, "filling default options\n");
799 for (o = &options[0]; o->name; o++)
801 handle_option(o, o->def, data);
805 * Sanitize the options structure. For now it just sets min/max for bool
806 * values and whether both callback and offsets are given.
808 void options_init(struct fio_option *options)
810 struct fio_option *o;
812 dprint(FD_PARSE, "init options\n");
814 for (o = &options[0]; o->name; o++) {
815 if (o->type == FIO_OPT_DEPRECATED)
817 if (o->type == FIO_OPT_BOOL) {
821 if (o->type == FIO_OPT_STR_SET && o->def) {
822 fprintf(stderr, "Option %s: string set option with"
823 " default will always be true\n",
826 if (!o->cb && !o->off1) {
827 fprintf(stderr, "Option %s: neither cb nor offset"
828 " given\n", o->name);
830 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
832 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
833 fprintf(stderr, "Option %s: both cb and offset given\n",