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)
166 *val *= get_mult_bytes(str[len - 1], data);
168 *val *= get_mult_time(str[len - 1]);
173 static int check_str_bytes(const char *p, long long *val, void *data)
175 return str_to_decimal(p, val, 1, data);
178 static int check_str_time(const char *p, long long *val)
180 return str_to_decimal(p, val, 0, NULL);
183 void strip_blank_front(char **p)
193 void strip_blank_end(char *p)
207 while ((isspace(*s) || iscntrl(*s)) && (s > start))
213 static int check_range_bytes(const char *str, long *val, void *data)
220 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
221 *val *= get_mult_bytes(suffix, data);
225 if (sscanf(str, "%lu", val) == 1)
231 static int check_int(const char *p, int *val)
235 if (strstr(p, "0x") || strstr(p, "0X")) {
236 if (sscanf(p, "%x", val) == 1)
239 if (sscanf(p, "%u", val) == 1)
246 static struct fio_option *find_option(struct fio_option *options,
249 struct fio_option *o;
251 for (o = &options[0]; o->name; o++) {
252 if (!strcmp(o->name, opt))
254 else if (o->alias && !strcmp(o->alias, opt))
261 #define val_store(ptr, val, off, data) \
263 ptr = td_var((data), (off)); \
267 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
271 long long ull, *ullp;
274 int ret = 0, is_time = 0;
276 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
279 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
280 fprintf(stderr, "Option %s requires an argument\n", o->name);
286 fio_opt_str_fn *fn = o->cb;
287 const struct value_pair *vp;
288 struct value_pair posval[PARSE_MAX_VP];
291 posval_sort(o, posval);
293 for (i = 0; i < PARSE_MAX_VP; i++) {
295 if (!vp->ival || vp->ival[0] == '\0')
298 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
302 val_store(ilp, vp->oval, o->off1, data);
308 show_option_values(o);
313 case FIO_OPT_STR_VAL_TIME:
316 case FIO_OPT_STR_VAL: {
317 fio_opt_str_val_fn *fn = o->cb;
320 ret = check_str_time(ptr, &ull);
322 ret = check_str_bytes(ptr, &ull, data);
327 if (o->maxval && ull > o->maxval) {
328 fprintf(stderr, "max value out of range: %lld"
329 " (%d max)\n", ull, o->maxval);
332 if (o->minval && ull < o->minval) {
333 fprintf(stderr, "min value out of range: %lld"
334 " (%d min)\n", ull, o->minval);
339 ret = fn(data, &ull);
341 if (o->type == FIO_OPT_INT) {
343 val_store(ilp, ull, o->off1, data);
344 if (!more && o->off2)
345 val_store(ilp, ull, o->off2, data);
348 val_store(ullp, ull, o->off1, data);
349 if (!more && o->off2)
350 val_store(ullp, ull, o->off2, data);
355 case FIO_OPT_STR_STORE: {
356 fio_opt_str_fn *fn = o->cb;
358 cp = td_var(data, o->off1);
369 case FIO_OPT_RANGE: {
373 strncpy(tmp, ptr, sizeof(tmp) - 1);
375 p1 = strchr(tmp, '-');
377 p1 = strchr(tmp, ':');
389 if (!check_range_bytes(p1, &ul1, data) &&
390 !check_range_bytes(p2, &ul2, data)) {
393 unsigned long foo = ul1;
400 val_store(ilp, ul1, o->off1, data);
401 val_store(ilp, ul2, o->off2, data);
403 if (o->off3 && o->off4) {
404 val_store(ilp, ul1, o->off3, data);
405 val_store(ilp, ul2, o->off4, data);
412 fio_opt_int_fn *fn = o->cb;
414 ret = check_int(ptr, &il);
418 if (o->maxval && il > (int) o->maxval) {
419 fprintf(stderr, "max value out of range: %d (%d max)\n",
423 if (o->minval && il < o->minval) {
424 fprintf(stderr, "min value out of range: %d (%d min)\n",
436 val_store(ilp, il, o->off1, data);
437 if (!more && o->off2)
438 val_store(ilp, il, o->off2, data);
442 case FIO_OPT_STR_SET: {
443 fio_opt_str_set_fn *fn = o->cb;
449 val_store(ilp, 1, o->off1, data);
450 if (!more && o->off2)
451 val_store(ilp, 1, o->off2, data);
455 case FIO_OPT_DEPRECATED:
456 fprintf(stdout, "Option %s is deprecated\n", o->name);
459 fprintf(stderr, "Bad option type %u\n", o->type);
467 ret = o->verify(o, data);
469 fprintf(stderr,"Correct format for offending option\n");
470 fprintf(stderr, "%20s: %s\n", o->name, o->help);
471 show_option_help(o, stderr);
478 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
480 char *ptr, *ptr2 = NULL;
483 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
490 * See if we have a second set of parameters, hidden after a comma.
491 * Do this before parsing the first round, to check if we should
492 * copy set 1 options to set 2.
495 (o->type != FIO_OPT_STR_STORE) &&
496 (o->type != FIO_OPT_STR)) {
497 ptr2 = strchr(ptr, ',');
498 if (ptr2 && *(ptr2 + 1) == '\0')
501 ptr2 = strchr(ptr, ':');
503 ptr2 = strchr(ptr, '-');
507 * Don't return early if parsing the first option fails - if
508 * we are doing multiple arguments, we can allow the first one
511 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
520 r2 = __handle_option(o, ptr2, data, 0, 0);
527 static struct fio_option *get_option(const char *opt,
528 struct fio_option *options, char **post)
530 struct fio_option *o;
533 ret = strchr(opt, '=');
539 strip_blank_end(ret);
540 o = find_option(options, ret);
542 o = find_option(options, opt);
549 static int opt_cmp(const void *p1, const void *p2)
551 struct fio_option *o1, *o2;
555 s1 = strdup(*((char **) p1));
556 s2 = strdup(*((char **) p2));
558 o1 = get_option(s1, fio_options, &foo);
559 o2 = get_option(s2, fio_options, &foo);
569 return prio2 - prio1;
572 void sort_options(char **opts, struct fio_option *options, int num_opts)
574 fio_options = options;
575 qsort(opts, num_opts, sizeof(char *), opt_cmp);
579 int parse_cmd_option(const char *opt, const char *val,
580 struct fio_option *options, void *data)
582 struct fio_option *o;
584 o = find_option(options, opt);
586 fprintf(stderr, "Bad option <%s>\n", opt);
590 if (!handle_option(o, val, data))
593 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
598 * Return a copy of the input string with substrings of the form ${VARNAME}
599 * substituted with the value of the environment variable VARNAME. The
600 * substitution always occurs, even if VARNAME is empty or the corresponding
601 * environment variable undefined.
603 static char *option_dup_subs(const char *opt)
605 char out[OPT_LEN_MAX+1];
606 char in[OPT_LEN_MAX+1];
609 char *ch1, *ch2, *env;
610 ssize_t nchr = OPT_LEN_MAX;
613 in[OPT_LEN_MAX] = '\0';
614 strncpy(in, opt, OPT_LEN_MAX);
616 while (*inptr && nchr > 0) {
617 if (inptr[0] == '$' && inptr[1] == '{') {
618 ch2 = strchr(inptr, '}');
619 if (ch2 && inptr+1 < ch2) {
626 envlen = strlen(env);
627 if (envlen <= nchr) {
628 memcpy(outptr, env, envlen);
638 *outptr++ = *inptr++;
646 int parse_option(const char *opt, struct fio_option *options, void *data)
648 struct fio_option *o;
651 tmp = option_dup_subs(opt);
653 o = get_option(tmp, options, &post);
655 fprintf(stderr, "Bad option <%s>\n", tmp);
660 if (!handle_option(o, post, data)) {
665 fprintf(stderr, "fio: failed parsing %s\n", opt);
671 * Option match, levenshtein distance. Handy for not quite remembering what
672 * the option name is.
674 static int string_distance(const char *s1, const char *s2)
676 unsigned int s1_len = strlen(s1);
677 unsigned int s2_len = strlen(s2);
678 unsigned int *p, *q, *r;
681 p = malloc(sizeof(unsigned int) * (s2_len + 1));
682 q = malloc(sizeof(unsigned int) * (s2_len + 1));
685 for (i = 1; i <= s2_len; i++)
688 for (i = 1; i <= s1_len; i++) {
690 for (j = 1; j <= s2_len; j++) {
691 unsigned int sub = p[j - 1];
693 if (s1[i - 1] != s2[j - 1])
696 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
709 static struct fio_option *find_child(struct fio_option *options,
710 struct fio_option *o)
712 struct fio_option *__o;
714 for (__o = options + 1; __o->name; __o++)
715 if (__o->parent && !strcmp(__o->parent, o->name))
721 static void __print_option(struct fio_option *o, struct fio_option *org,
735 p += sprintf(p, "%s", " ");
737 sprintf(p, "%s", o->name);
739 printf("%-24s: %s\n", name, o->help);
742 static void print_option(struct fio_option *o)
744 struct fio_option *parent;
745 struct fio_option *__o;
746 unsigned int printed;
749 __print_option(o, NULL, 0);
756 while ((__o = find_child(o, parent)) != NULL) {
757 __print_option(__o, o, level);
766 int show_cmd_help(struct fio_option *options, const char *name)
768 struct fio_option *o, *closest;
769 unsigned int best_dist;
773 if (!name || !strcmp(name, "all"))
778 for (o = &options[0]; o->name; o++) {
781 if (o->type == FIO_OPT_DEPRECATED)
785 if (!strcmp(name, o->name) ||
786 (o->alias && !strcmp(name, o->alias)))
791 dist = string_distance(name, o->name);
792 if (dist < best_dist) {
799 if (show_all || match) {
802 printf("%24s: %s\n", o->name, o->help);
813 show_option_help(o, stdout);
819 printf("No such command: %s", name);
821 printf(" - showing closest match\n");
822 printf("%20s: %s\n", closest->name, closest->help);
823 show_option_help(closest, stdout);
831 * Handle parsing of default parameters.
833 void fill_default_options(void *data, struct fio_option *options)
835 struct fio_option *o;
837 dprint(FD_PARSE, "filling default options\n");
839 for (o = &options[0]; o->name; o++)
841 handle_option(o, o->def, data);
845 * Sanitize the options structure. For now it just sets min/max for bool
846 * values and whether both callback and offsets are given.
848 void options_init(struct fio_option *options)
850 struct fio_option *o;
852 dprint(FD_PARSE, "init options\n");
854 for (o = &options[0]; o->name; o++) {
855 if (o->type == FIO_OPT_DEPRECATED)
857 if (o->type == FIO_OPT_BOOL) {
861 if (o->type == FIO_OPT_STR_SET && o->def) {
862 fprintf(stderr, "Option %s: string set option with"
863 " default will always be true\n",
866 if (!o->cb && !o->off1) {
867 fprintf(stderr, "Option %s: neither cb nor offset"
868 " given\n", o->name);
870 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
872 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
873 fprintf(stderr, "Option %s: both cb and offset given\n",