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 fio_opt_str_val_fn *fn = o->cb;
285 ret = check_str_time(ptr, &ull);
287 ret = check_str_bytes(ptr, &ull);
292 if (o->maxval && ull > o->maxval) {
293 fprintf(stderr, "max value out of range: %lld"
294 " (%d max)\n", ull, o->maxval);
297 if (o->minval && ull < o->minval) {
298 fprintf(stderr, "min value out of range: %lld"
299 " (%d min)\n", ull, o->minval);
304 ret = fn(data, &ull);
306 if (o->type == FIO_OPT_INT) {
308 val_store(ilp, ull, o->off1, data);
309 if (!more && o->off2)
310 val_store(ilp, ull, o->off2, data);
313 val_store(ullp, ull, o->off1, data);
314 if (!more && o->off2)
315 val_store(ullp, ull, o->off2, data);
320 case FIO_OPT_STR_STORE: {
321 fio_opt_str_fn *fn = o->cb;
323 cp = td_var(data, o->off1);
334 case FIO_OPT_RANGE: {
338 strncpy(tmp, ptr, sizeof(tmp) - 1);
340 p1 = strchr(tmp, '-');
342 p1 = strchr(tmp, ':');
354 if (!check_range_bytes(p1, &ul1) &&
355 !check_range_bytes(p2, &ul2)) {
358 unsigned long foo = ul1;
365 val_store(ilp, ul1, o->off1, data);
366 val_store(ilp, ul2, o->off2, data);
368 if (o->off3 && o->off4) {
369 val_store(ilp, ul1, o->off3, data);
370 val_store(ilp, ul2, o->off4, data);
377 fio_opt_int_fn *fn = o->cb;
379 ret = check_int(ptr, &il);
383 if (o->maxval && il > (int) o->maxval) {
384 fprintf(stderr, "max value out of range: %d (%d max)\n",
388 if (o->minval && il < o->minval) {
389 fprintf(stderr, "min value out of range: %d (%d min)\n",
401 val_store(ilp, il, o->off1, data);
402 if (!more && o->off2)
403 val_store(ilp, il, o->off2, data);
407 case FIO_OPT_STR_SET: {
408 fio_opt_str_set_fn *fn = o->cb;
414 val_store(ilp, 1, o->off1, data);
415 if (!more && o->off2)
416 val_store(ilp, 1, o->off2, data);
420 case FIO_OPT_DEPRECATED:
421 fprintf(stdout, "Option %s is deprecated\n", o->name);
424 fprintf(stderr, "Bad option type %u\n", o->type);
431 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
433 char *ptr, *ptr2 = NULL;
436 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
443 * See if we have a second set of parameters, hidden after a comma.
444 * Do this before parsing the first round, to check if we should
445 * copy set 1 options to set 2.
448 (o->type != FIO_OPT_STR_STORE) &&
449 (o->type != FIO_OPT_STR)) {
450 ptr2 = strchr(ptr, ',');
451 if (ptr2 && *(ptr2 + 1) == '\0')
454 ptr2 = strchr(ptr, ':');
456 ptr2 = strchr(ptr, '-');
460 * Don't return early if parsing the first option fails - if
461 * we are doing multiple arguments, we can allow the first one
464 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
473 r2 = __handle_option(o, ptr2, data, 0, 0);
480 static struct fio_option *get_option(const char *opt,
481 struct fio_option *options, char **post)
483 struct fio_option *o;
486 ret = strchr(opt, '=');
492 strip_blank_end(ret);
493 o = find_option(options, ret);
495 o = find_option(options, opt);
502 static int opt_cmp(const void *p1, const void *p2)
504 struct fio_option *o1, *o2;
508 s1 = strdup(*((char **) p1));
509 s2 = strdup(*((char **) p2));
511 o1 = get_option(s1, fio_options, &foo);
512 o2 = get_option(s2, fio_options, &foo);
522 return prio2 - prio1;
525 void sort_options(char **opts, struct fio_option *options, int num_opts)
527 fio_options = options;
528 qsort(opts, num_opts, sizeof(char *), opt_cmp);
532 int parse_cmd_option(const char *opt, const char *val,
533 struct fio_option *options, void *data)
535 struct fio_option *o;
537 o = find_option(options, opt);
539 fprintf(stderr, "Bad option <%s>\n", opt);
543 if (!handle_option(o, val, data))
546 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
551 * Return a copy of the input string with substrings of the form ${VARNAME}
552 * substituted with the value of the environment variable VARNAME. The
553 * substitution always occurs, even if VARNAME is empty or the corresponding
554 * environment variable undefined.
556 static char *option_dup_subs(const char *opt)
558 char out[OPT_LEN_MAX+1];
559 char in[OPT_LEN_MAX+1];
562 char *ch1, *ch2, *env;
563 ssize_t nchr = OPT_LEN_MAX;
566 in[OPT_LEN_MAX] = '\0';
567 strncpy(in, opt, OPT_LEN_MAX);
569 while (*inptr && nchr > 0) {
570 if (inptr[0] == '$' && inptr[1] == '{') {
571 ch2 = strchr(inptr, '}');
572 if (ch2 && inptr+1 < ch2) {
579 envlen = strlen(env);
580 if (envlen <= nchr) {
581 memcpy(outptr, env, envlen);
591 *outptr++ = *inptr++;
599 int parse_option(const char *opt, struct fio_option *options, void *data)
601 struct fio_option *o;
604 tmp = option_dup_subs(opt);
606 o = get_option(tmp, options, &post);
608 fprintf(stderr, "Bad option <%s>\n", tmp);
613 if (!handle_option(o, post, data)) {
618 fprintf(stderr, "fio: failed parsing %s\n", opt);
624 * Option match, levenshtein distance. Handy for not quite remembering what
625 * the option name is.
627 static int string_distance(const char *s1, const char *s2)
629 unsigned int s1_len = strlen(s1);
630 unsigned int s2_len = strlen(s2);
631 unsigned int *p, *q, *r;
634 p = malloc(sizeof(unsigned int) * (s2_len + 1));
635 q = malloc(sizeof(unsigned int) * (s2_len + 1));
638 for (i = 1; i <= s2_len; i++)
641 for (i = 1; i <= s1_len; i++) {
643 for (j = 1; j <= s2_len; j++) {
644 unsigned int sub = p[j - 1];
646 if (s1[i - 1] != s2[j - 1])
649 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
662 static void show_option_help(struct fio_option *o)
664 const char *typehelp[] = {
666 "string with possible k/m/g postfix (opt=4k)",
667 "string with time postfix (opt=10s)",
669 "string with dual range (opt=1k-4k,4k-8k)",
670 "integer value (opt=100)",
671 "boolean value (opt=1)",
676 printf("%20s: %s\n", "alias", o->alias);
678 printf("%20s: %s\n", "type", typehelp[o->type]);
679 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
680 show_option_range(o);
681 show_option_values(o);
684 static struct fio_option *find_child(struct fio_option *options,
685 struct fio_option *o)
687 struct fio_option *__o;
689 for (__o = options + 1; __o->name; __o++)
690 if (__o->parent && !strcmp(__o->parent, o->name))
696 static void __print_option(struct fio_option *o, struct fio_option *org,
710 p += sprintf(p, "%s", " ");
712 sprintf(p, "%s", o->name);
714 printf("%-24s: %s\n", name, o->help);
717 static void print_option(struct fio_option *o)
719 struct fio_option *parent;
720 struct fio_option *__o;
721 unsigned int printed;
724 __print_option(o, NULL, 0);
731 while ((__o = find_child(o, parent)) != NULL) {
732 __print_option(__o, o, level);
741 int show_cmd_help(struct fio_option *options, const char *name)
743 struct fio_option *o, *closest;
744 unsigned int best_dist;
748 if (!name || !strcmp(name, "all"))
753 for (o = &options[0]; o->name; o++) {
756 if (o->type == FIO_OPT_DEPRECATED)
760 if (!strcmp(name, o->name) ||
761 (o->alias && !strcmp(name, o->alias)))
766 dist = string_distance(name, o->name);
767 if (dist < best_dist) {
774 if (show_all || match) {
777 printf("%24s: %s\n", o->name, o->help);
794 printf("No such command: %s", name);
796 printf(" - showing closest match\n");
797 printf("%20s: %s\n", closest->name, closest->help);
798 show_option_help(closest);
806 * Handle parsing of default parameters.
808 void fill_default_options(void *data, struct fio_option *options)
810 struct fio_option *o;
812 dprint(FD_PARSE, "filling default options\n");
814 for (o = &options[0]; o->name; o++)
816 handle_option(o, o->def, data);
820 * Sanitize the options structure. For now it just sets min/max for bool
821 * values and whether both callback and offsets are given.
823 void options_init(struct fio_option *options)
825 struct fio_option *o;
827 dprint(FD_PARSE, "init options\n");
829 for (o = &options[0]; o->name; o++) {
830 if (o->type == FIO_OPT_DEPRECATED)
832 if (o->type == FIO_OPT_BOOL) {
836 if (o->type == FIO_OPT_STR_SET && o->def) {
837 fprintf(stderr, "Option %s: string set option with"
838 " default will always be true\n",
841 if (!o->cb && !o->off1) {
842 fprintf(stderr, "Option %s: neither cb nor offset"
843 " given\n", o->name);
845 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
847 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
848 fprintf(stderr, "Option %s: both cb and offset given\n",