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, FILE *out)
46 if (!o->minval && !o->maxval)
49 fprintf(out, "%20s: min=%d", "range", o->minval);
51 fprintf(out, ", max=%d", o->maxval);
55 static void show_option_values(struct fio_option *o)
60 const struct value_pair *vp = &o->posval[i];
65 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
67 printf(" %s", vp->help);
70 } while (i < PARSE_MAX_VP);
76 static void show_option_help(struct fio_option *o, FILE *out)
78 const char *typehelp[] = {
80 "string with possible k/m/g postfix (opt=4k)",
81 "string with time postfix (opt=10s)",
83 "string with dual range (opt=1k-4k,4k-8k)",
84 "integer value (opt=100)",
85 "boolean value (opt=1)",
90 fprintf(out, "%20s: %s\n", "alias", o->alias);
92 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
93 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
94 show_option_range(o, stdout);
95 show_option_values(o);
98 static unsigned long get_mult_time(char c)
115 static unsigned long long get_mult_bytes(char c)
126 return 1024 * 1024 * 1024;
129 return 1024 * 1024 * 1024 * 1024UL;
132 return 1024 * 1024 * 1024 * 1024ULL * 1024ULL;
139 * convert string into decimal value, noting any size suffix
141 int str_to_decimal(const char *str, long long *val, int kilo)
149 if (strstr(str, "0x") || strstr(str, "0X"))
154 *val = strtoll(str, NULL, base);
155 if (*val == LONG_MAX && errno == ERANGE)
159 *val *= get_mult_bytes(str[len - 1]);
161 *val *= get_mult_time(str[len - 1]);
166 static int check_str_bytes(const char *p, long long *val)
168 return str_to_decimal(p, val, 1);
171 static int check_str_time(const char *p, long long *val)
173 return str_to_decimal(p, val, 0);
176 void strip_blank_front(char **p)
186 void strip_blank_end(char *p)
200 while ((isspace(*s) || iscntrl(*s)) && (s > start))
206 static int check_range_bytes(const char *str, long *val)
213 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
214 *val *= get_mult_bytes(suffix);
218 if (sscanf(str, "%lu", val) == 1)
224 static int check_int(const char *p, int *val)
228 if (strstr(p, "0x") || strstr(p, "0X")) {
229 if (sscanf(p, "%x", val) == 1)
232 if (sscanf(p, "%u", val) == 1)
239 static struct fio_option *find_option(struct fio_option *options,
242 struct fio_option *o;
244 for (o = &options[0]; o->name; o++) {
245 if (!strcmp(o->name, opt))
247 else if (o->alias && !strcmp(o->alias, opt))
254 #define val_store(ptr, val, off, data) \
256 ptr = td_var((data), (off)); \
260 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
264 long long ull, *ullp;
267 int ret = 0, is_time = 0;
269 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
272 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
273 fprintf(stderr, "Option %s requires an argument\n", o->name);
279 fio_opt_str_fn *fn = o->cb;
280 const struct value_pair *vp;
281 struct value_pair posval[PARSE_MAX_VP];
284 posval_sort(o, posval);
286 for (i = 0; i < PARSE_MAX_VP; i++) {
288 if (!vp->ival || vp->ival[0] == '\0')
291 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
295 val_store(ilp, vp->oval, o->off1, data);
301 show_option_values(o);
306 case FIO_OPT_STR_VAL_TIME:
309 case FIO_OPT_STR_VAL: {
310 fio_opt_str_val_fn *fn = o->cb;
313 ret = check_str_time(ptr, &ull);
315 ret = check_str_bytes(ptr, &ull);
320 if (o->maxval && ull > o->maxval) {
321 fprintf(stderr, "max value out of range: %lld"
322 " (%d max)\n", ull, o->maxval);
325 if (o->minval && ull < o->minval) {
326 fprintf(stderr, "min value out of range: %lld"
327 " (%d min)\n", ull, o->minval);
332 ret = fn(data, &ull);
334 if (o->type == FIO_OPT_INT) {
336 val_store(ilp, ull, o->off1, data);
337 if (!more && o->off2)
338 val_store(ilp, ull, o->off2, data);
341 val_store(ullp, ull, o->off1, data);
342 if (!more && o->off2)
343 val_store(ullp, ull, o->off2, data);
348 case FIO_OPT_STR_STORE: {
349 fio_opt_str_fn *fn = o->cb;
351 cp = td_var(data, o->off1);
362 case FIO_OPT_RANGE: {
366 strncpy(tmp, ptr, sizeof(tmp) - 1);
368 p1 = strchr(tmp, '-');
370 p1 = strchr(tmp, ':');
382 if (!check_range_bytes(p1, &ul1) &&
383 !check_range_bytes(p2, &ul2)) {
386 unsigned long foo = ul1;
393 val_store(ilp, ul1, o->off1, data);
394 val_store(ilp, ul2, o->off2, data);
396 if (o->off3 && o->off4) {
397 val_store(ilp, ul1, o->off3, data);
398 val_store(ilp, ul2, o->off4, data);
405 fio_opt_int_fn *fn = o->cb;
407 ret = check_int(ptr, &il);
411 if (o->maxval && il > (int) o->maxval) {
412 fprintf(stderr, "max value out of range: %d (%d max)\n",
416 if (o->minval && il < o->minval) {
417 fprintf(stderr, "min value out of range: %d (%d min)\n",
429 val_store(ilp, il, o->off1, data);
430 if (!more && o->off2)
431 val_store(ilp, il, o->off2, data);
435 case FIO_OPT_STR_SET: {
436 fio_opt_str_set_fn *fn = o->cb;
442 val_store(ilp, 1, o->off1, data);
443 if (!more && o->off2)
444 val_store(ilp, 1, o->off2, data);
448 case FIO_OPT_DEPRECATED:
449 fprintf(stdout, "Option %s is deprecated\n", o->name);
452 fprintf(stderr, "Bad option type %u\n", o->type);
460 ret = o->verify(o, data);
462 fprintf(stderr,"Correct format for offending option\n");
463 fprintf(stderr, "%20s: %s\n", o->name, o->help);
464 show_option_help(o, stderr);
471 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
473 char *ptr, *ptr2 = NULL;
476 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
483 * See if we have a second set of parameters, hidden after a comma.
484 * Do this before parsing the first round, to check if we should
485 * copy set 1 options to set 2.
488 (o->type != FIO_OPT_STR_STORE) &&
489 (o->type != FIO_OPT_STR)) {
490 ptr2 = strchr(ptr, ',');
491 if (ptr2 && *(ptr2 + 1) == '\0')
494 ptr2 = strchr(ptr, ':');
496 ptr2 = strchr(ptr, '-');
500 * Don't return early if parsing the first option fails - if
501 * we are doing multiple arguments, we can allow the first one
504 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
513 r2 = __handle_option(o, ptr2, data, 0, 0);
520 static struct fio_option *get_option(const char *opt,
521 struct fio_option *options, char **post)
523 struct fio_option *o;
526 ret = strchr(opt, '=');
532 strip_blank_end(ret);
533 o = find_option(options, ret);
535 o = find_option(options, opt);
542 static int opt_cmp(const void *p1, const void *p2)
544 struct fio_option *o1, *o2;
548 s1 = strdup(*((char **) p1));
549 s2 = strdup(*((char **) p2));
551 o1 = get_option(s1, fio_options, &foo);
552 o2 = get_option(s2, fio_options, &foo);
562 return prio2 - prio1;
565 void sort_options(char **opts, struct fio_option *options, int num_opts)
567 fio_options = options;
568 qsort(opts, num_opts, sizeof(char *), opt_cmp);
572 int parse_cmd_option(const char *opt, const char *val,
573 struct fio_option *options, void *data)
575 struct fio_option *o;
577 o = find_option(options, opt);
579 fprintf(stderr, "Bad option <%s>\n", opt);
583 if (!handle_option(o, val, data))
586 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
591 * Return a copy of the input string with substrings of the form ${VARNAME}
592 * substituted with the value of the environment variable VARNAME. The
593 * substitution always occurs, even if VARNAME is empty or the corresponding
594 * environment variable undefined.
596 static char *option_dup_subs(const char *opt)
598 char out[OPT_LEN_MAX+1];
599 char in[OPT_LEN_MAX+1];
602 char *ch1, *ch2, *env;
603 ssize_t nchr = OPT_LEN_MAX;
606 in[OPT_LEN_MAX] = '\0';
607 strncpy(in, opt, OPT_LEN_MAX);
609 while (*inptr && nchr > 0) {
610 if (inptr[0] == '$' && inptr[1] == '{') {
611 ch2 = strchr(inptr, '}');
612 if (ch2 && inptr+1 < ch2) {
619 envlen = strlen(env);
620 if (envlen <= nchr) {
621 memcpy(outptr, env, envlen);
631 *outptr++ = *inptr++;
639 int parse_option(const char *opt, struct fio_option *options, void *data)
641 struct fio_option *o;
644 tmp = option_dup_subs(opt);
646 o = get_option(tmp, options, &post);
648 fprintf(stderr, "Bad option <%s>\n", tmp);
653 if (!handle_option(o, post, data)) {
658 fprintf(stderr, "fio: failed parsing %s\n", opt);
664 * Option match, levenshtein distance. Handy for not quite remembering what
665 * the option name is.
667 static int string_distance(const char *s1, const char *s2)
669 unsigned int s1_len = strlen(s1);
670 unsigned int s2_len = strlen(s2);
671 unsigned int *p, *q, *r;
674 p = malloc(sizeof(unsigned int) * (s2_len + 1));
675 q = malloc(sizeof(unsigned int) * (s2_len + 1));
678 for (i = 1; i <= s2_len; i++)
681 for (i = 1; i <= s1_len; i++) {
683 for (j = 1; j <= s2_len; j++) {
684 unsigned int sub = p[j - 1];
686 if (s1[i - 1] != s2[j - 1])
689 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
702 static struct fio_option *find_child(struct fio_option *options,
703 struct fio_option *o)
705 struct fio_option *__o;
707 for (__o = options + 1; __o->name; __o++)
708 if (__o->parent && !strcmp(__o->parent, o->name))
714 static void __print_option(struct fio_option *o, struct fio_option *org,
728 p += sprintf(p, "%s", " ");
730 sprintf(p, "%s", o->name);
732 printf("%-24s: %s\n", name, o->help);
735 static void print_option(struct fio_option *o)
737 struct fio_option *parent;
738 struct fio_option *__o;
739 unsigned int printed;
742 __print_option(o, NULL, 0);
749 while ((__o = find_child(o, parent)) != NULL) {
750 __print_option(__o, o, level);
759 int show_cmd_help(struct fio_option *options, const char *name)
761 struct fio_option *o, *closest;
762 unsigned int best_dist;
766 if (!name || !strcmp(name, "all"))
771 for (o = &options[0]; o->name; o++) {
774 if (o->type == FIO_OPT_DEPRECATED)
778 if (!strcmp(name, o->name) ||
779 (o->alias && !strcmp(name, o->alias)))
784 dist = string_distance(name, o->name);
785 if (dist < best_dist) {
792 if (show_all || match) {
795 printf("%24s: %s\n", o->name, o->help);
806 show_option_help(o, stdout);
812 printf("No such command: %s", name);
814 printf(" - showing closest match\n");
815 printf("%20s: %s\n", closest->name, closest->help);
816 show_option_help(closest, stdout);
824 * Handle parsing of default parameters.
826 void fill_default_options(void *data, struct fio_option *options)
828 struct fio_option *o;
830 dprint(FD_PARSE, "filling default options\n");
832 for (o = &options[0]; o->name; o++)
834 handle_option(o, o->def, data);
838 * Sanitize the options structure. For now it just sets min/max for bool
839 * values and whether both callback and offsets are given.
841 void options_init(struct fio_option *options)
843 struct fio_option *o;
845 dprint(FD_PARSE, "init options\n");
847 for (o = &options[0]; o->name; o++) {
848 if (o->type == FIO_OPT_DEPRECATED)
850 if (o->type == FIO_OPT_BOOL) {
854 if (o->type == FIO_OPT_STR_SET && o->def) {
855 fprintf(stderr, "Option %s: string set option with"
856 " default will always be true\n",
859 if (!o->cb && !o->off1) {
860 fprintf(stderr, "Option %s: neither cb nor offset"
861 " given\n", o->name);
863 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
865 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
866 fprintf(stderr, "Option %s: both cb and offset given\n",