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 if (strstr(str, "0x") || strstr(str, "0X"))
126 *val = strtoll(str, NULL, base);
127 if (*val == LONG_MAX && errno == ERANGE)
131 *val *= get_mult_bytes(str[len - 1]);
133 *val *= get_mult_time(str[len - 1]);
138 static int check_str_bytes(const char *p, long long *val)
140 return str_to_decimal(p, val, 1);
143 static int check_str_time(const char *p, long long *val)
145 return str_to_decimal(p, val, 0);
148 void strip_blank_front(char **p)
158 void strip_blank_end(char *p)
172 while ((isspace(*s) || iscntrl(*s)) && (s > start))
178 static int check_range_bytes(const char *str, long *val)
185 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
186 *val *= get_mult_bytes(suffix);
190 if (sscanf(str, "%lu", val) == 1)
196 static int check_int(const char *p, int *val)
200 if (strstr(p, "0x") || strstr(p, "0X")) {
201 if (sscanf(p, "%x", val) == 1)
204 if (sscanf(p, "%u", val) == 1)
211 static struct fio_option *find_option(struct fio_option *options,
214 struct fio_option *o;
216 for (o = &options[0]; o->name; o++) {
217 if (!strcmp(o->name, opt))
219 else if (o->alias && !strcmp(o->alias, opt))
226 #define val_store(ptr, val, off, data) \
228 ptr = td_var((data), (off)); \
232 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
236 long long ull, *ullp;
239 int ret = 0, is_time = 0;
241 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
244 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
245 fprintf(stderr, "Option %s requires an argument\n", o->name);
251 fio_opt_str_fn *fn = o->cb;
252 const struct value_pair *vp;
253 struct value_pair posval[PARSE_MAX_VP];
256 posval_sort(o, posval);
258 for (i = 0; i < PARSE_MAX_VP; i++) {
260 if (!vp->ival || vp->ival[0] == '\0')
263 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
267 val_store(ilp, vp->oval, o->off1, data);
273 show_option_values(o);
278 case FIO_OPT_STR_VAL_TIME:
281 case FIO_OPT_STR_VAL:
282 case FIO_OPT_STR_VAL_INT: {
283 fio_opt_str_val_fn *fn = o->cb;
286 ret = check_str_time(ptr, &ull);
288 ret = check_str_bytes(ptr, &ull);
293 if (o->maxval && ull > o->maxval) {
294 fprintf(stderr, "max value out of range: %lld"
295 " (%d max)\n", ull, o->maxval);
298 if (o->minval && ull < o->minval) {
299 fprintf(stderr, "min value out of range: %lld"
300 " (%d min)\n", ull, o->minval);
305 ret = fn(data, &ull);
307 if (o->type == FIO_OPT_STR_VAL_INT) {
309 val_store(ilp, ull, o->off1, data);
310 if (!more && o->off2)
311 val_store(ilp, ull, o->off2, data);
314 val_store(ullp, ull, o->off1, data);
315 if (!more && o->off2)
316 val_store(ullp, ull, o->off2, data);
321 case FIO_OPT_STR_STORE: {
322 fio_opt_str_fn *fn = o->cb;
324 cp = td_var(data, o->off1);
335 case FIO_OPT_RANGE: {
339 strncpy(tmp, ptr, sizeof(tmp) - 1);
341 p1 = strchr(tmp, '-');
343 p1 = strchr(tmp, ':');
355 if (!check_range_bytes(p1, &ul1) &&
356 !check_range_bytes(p2, &ul2)) {
359 unsigned long foo = ul1;
366 val_store(ilp, ul1, o->off1, data);
367 val_store(ilp, ul2, o->off2, data);
369 if (o->off3 && o->off4) {
370 val_store(ilp, ul1, o->off3, data);
371 val_store(ilp, ul2, o->off4, data);
378 fio_opt_int_fn *fn = o->cb;
380 ret = check_int(ptr, &il);
384 if (o->maxval && il > (int) o->maxval) {
385 fprintf(stderr, "max value out of range: %d (%d max)\n",
389 if (o->minval && il < o->minval) {
390 fprintf(stderr, "min value out of range: %d (%d min)\n",
402 val_store(ilp, il, o->off1, data);
403 if (!more && o->off2)
404 val_store(ilp, il, o->off2, data);
408 case FIO_OPT_STR_SET: {
409 fio_opt_str_set_fn *fn = o->cb;
415 val_store(ilp, 1, o->off1, data);
416 if (!more && o->off2)
417 val_store(ilp, 1, o->off2, data);
421 case FIO_OPT_DEPRECATED:
422 fprintf(stdout, "Option %s is deprecated\n", o->name);
425 fprintf(stderr, "Bad option type %u\n", o->type);
432 static int handle_option(struct fio_option *o, const char *ptr, void *data)
434 const char *ptr2 = NULL;
437 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, ptr);
440 * See if we have a second set of parameters, hidden after a comma.
441 * Do this before parsing the first round, to check if we should
442 * copy set 1 options to set 2.
445 (o->type != FIO_OPT_STR_STORE) &&
446 (o->type != FIO_OPT_STR)) {
447 ptr2 = strchr(ptr, ',');
449 ptr2 = strchr(ptr, ':');
451 ptr2 = strchr(ptr, '-');
455 * Don't return early if parsing the first option fails - if
456 * we are doing multiple arguments, we can allow the first one
459 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
465 r2 = __handle_option(o, ptr2, data, 0, 0);
470 static struct fio_option *get_option(const char *opt,
471 struct fio_option *options, char **post)
473 struct fio_option *o;
476 ret = strchr(opt, '=');
482 strip_blank_end(ret);
483 o = find_option(options, ret);
485 o = find_option(options, opt);
492 static int opt_cmp(const void *p1, const void *p2)
494 struct fio_option *o1, *o2;
498 s1 = strdup(*((char **) p1));
499 s2 = strdup(*((char **) p2));
501 o1 = get_option(s1, fio_options, &foo);
502 o2 = get_option(s2, fio_options, &foo);
512 return prio2 - prio1;
515 void sort_options(char **opts, struct fio_option *options, int num_opts)
517 fio_options = options;
518 qsort(opts, num_opts, sizeof(char *), opt_cmp);
522 int parse_cmd_option(const char *opt, const char *val,
523 struct fio_option *options, void *data)
525 struct fio_option *o;
527 o = find_option(options, opt);
529 fprintf(stderr, "Bad option <%s>\n", opt);
533 if (!handle_option(o, val, data))
536 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
541 * Return a copy of the input string with substrings of the form ${VARNAME}
542 * substituted with the value of the environment variable VARNAME. The
543 * substitution always occurs, even if VARNAME is empty or the corresponding
544 * environment variable undefined.
546 static char *option_dup_subs(const char *opt)
548 char out[OPT_LEN_MAX+1];
549 char in[OPT_LEN_MAX+1];
552 char *ch1, *ch2, *env;
553 ssize_t nchr = OPT_LEN_MAX;
556 in[OPT_LEN_MAX] = '\0';
557 strncpy(in, opt, OPT_LEN_MAX);
559 while (*inptr && nchr > 0) {
560 if (inptr[0] == '$' && inptr[1] == '{') {
561 ch2 = strchr(inptr, '}');
562 if (ch2 && inptr+1 < ch2) {
569 envlen = strlen(env);
570 if (envlen <= nchr) {
571 memcpy(outptr, env, envlen);
581 *outptr++ = *inptr++;
589 int parse_option(const char *opt, struct fio_option *options, void *data)
591 struct fio_option *o;
594 tmp = option_dup_subs(opt);
596 o = get_option(tmp, options, &post);
598 fprintf(stderr, "Bad option <%s>\n", tmp);
603 if (!handle_option(o, post, data)) {
608 fprintf(stderr, "fio: failed parsing %s\n", opt);
614 * Option match, levenshtein distance. Handy for not quite remembering what
615 * the option name is.
617 static int string_distance(const char *s1, const char *s2)
619 unsigned int s1_len = strlen(s1);
620 unsigned int s2_len = strlen(s2);
621 unsigned int *p, *q, *r;
624 p = malloc(sizeof(unsigned int) * (s2_len + 1));
625 q = malloc(sizeof(unsigned int) * (s2_len + 1));
628 for (i = 1; i <= s2_len; i++)
631 for (i = 1; i <= s1_len; i++) {
633 for (j = 1; j <= s2_len; j++) {
634 unsigned int sub = p[j - 1];
636 if (s1[i - 1] != s2[j - 1])
639 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
652 static void show_option_help(struct fio_option *o)
654 const char *typehelp[] = {
656 "string with possible k/m/g postfix (opt=4k)",
657 "string with range and postfix (opt=1k-4k)",
658 "string with time postfix (opt=10s)",
660 "string with dual range (opt=1k-4k,4k-8k)",
661 "integer value (opt=100)",
662 "boolean value (opt=1)",
667 printf("%20s: %s\n", "alias", o->alias);
669 printf("%20s: %s\n", "type", typehelp[o->type]);
670 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
671 show_option_range(o);
672 show_option_values(o);
675 static struct fio_option *find_child(struct fio_option *options,
676 struct fio_option *o)
678 struct fio_option *__o;
680 for (__o = options + 1; __o->name; __o++)
681 if (__o->parent && !strcmp(__o->parent, o->name))
687 static void __print_option(struct fio_option *o, struct fio_option *org,
701 p += sprintf(p, "%s", " ");
703 sprintf(p, "%s", o->name);
705 printf("%-24s: %s\n", name, o->help);
708 static void print_option(struct fio_option *o)
710 struct fio_option *parent;
711 struct fio_option *__o;
712 unsigned int printed;
715 __print_option(o, NULL, 0);
722 while ((__o = find_child(o, parent)) != NULL) {
723 __print_option(__o, o, level);
732 int show_cmd_help(struct fio_option *options, const char *name)
734 struct fio_option *o, *closest;
735 unsigned int best_dist;
739 if (!name || !strcmp(name, "all"))
744 for (o = &options[0]; o->name; o++) {
747 if (o->type == FIO_OPT_DEPRECATED)
751 if (!strcmp(name, o->name) ||
752 (o->alias && !strcmp(name, o->alias)))
757 dist = string_distance(name, o->name);
758 if (dist < best_dist) {
765 if (show_all || match) {
768 printf("%24s: %s\n", o->name, o->help);
785 printf("No such command: %s", name);
787 printf(" - showing closest match\n");
788 printf("%20s: %s\n", closest->name, closest->help);
789 show_option_help(closest);
797 * Handle parsing of default parameters.
799 void fill_default_options(void *data, struct fio_option *options)
801 struct fio_option *o;
803 dprint(FD_PARSE, "filling default options\n");
805 for (o = &options[0]; o->name; o++)
807 handle_option(o, o->def, data);
811 * Sanitize the options structure. For now it just sets min/max for bool
812 * values and whether both callback and offsets are given.
814 void options_init(struct fio_option *options)
816 struct fio_option *o;
818 dprint(FD_PARSE, "init options\n");
820 for (o = &options[0]; o->name; o++) {
821 if (o->type == FIO_OPT_DEPRECATED)
823 if (o->type == FIO_OPT_BOOL) {
827 if (o->type == FIO_OPT_STR_SET && o->def) {
828 fprintf(stderr, "Option %s: string set option with"
829 " default will always be true\n",
832 if (!o->cb && !o->off1) {
833 fprintf(stderr, "Option %s: neither cb nor offset"
834 " given\n", o->name);
836 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
838 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
839 fprintf(stderr, "Option %s: both cb and offset given\n",