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", "range", o->minval);
51 printf(", 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 unsigned long get_mult_time(char c)
93 static unsigned long get_mult_bytes(char c)
104 return 1024 * 1024 * 1024;
107 return 1024 * 1024 * 1024 * 1024UL;
114 * convert string into decimal value, noting any size suffix
116 int str_to_decimal(const char *str, long long *val, int kilo)
124 if (strstr(str, "0x") || strstr(str, "0X"))
129 *val = strtoll(str, NULL, base);
130 if (*val == LONG_MAX && errno == ERANGE)
134 *val *= get_mult_bytes(str[len - 1]);
136 *val *= get_mult_time(str[len - 1]);
141 static int check_str_bytes(const char *p, long long *val)
143 return str_to_decimal(p, val, 1);
146 static int check_str_time(const char *p, long long *val)
148 return str_to_decimal(p, val, 0);
151 void strip_blank_front(char **p)
161 void strip_blank_end(char *p)
175 while ((isspace(*s) || iscntrl(*s)) && (s > start))
181 static int check_range_bytes(const char *str, long *val)
188 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
189 *val *= get_mult_bytes(suffix);
193 if (sscanf(str, "%lu", val) == 1)
199 static int check_int(const char *p, int *val)
203 if (strstr(p, "0x") || strstr(p, "0X")) {
204 if (sscanf(p, "%x", val) == 1)
207 if (sscanf(p, "%u", val) == 1)
214 static struct fio_option *find_option(struct fio_option *options,
217 struct fio_option *o;
219 for (o = &options[0]; o->name; o++) {
220 if (!strcmp(o->name, opt))
222 else if (o->alias && !strcmp(o->alias, opt))
229 #define val_store(ptr, val, off, data) \
231 ptr = td_var((data), (off)); \
235 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
239 long long ull, *ullp;
242 int ret = 0, is_time = 0;
244 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
247 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
248 fprintf(stderr, "Option %s requires an argument\n", o->name);
254 fio_opt_str_fn *fn = o->cb;
255 const struct value_pair *vp;
256 struct value_pair posval[PARSE_MAX_VP];
259 posval_sort(o, posval);
261 for (i = 0; i < PARSE_MAX_VP; i++) {
263 if (!vp->ival || vp->ival[0] == '\0')
266 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
270 val_store(ilp, vp->oval, o->off1, data);
276 show_option_values(o);
281 case FIO_OPT_STR_VAL_TIME:
284 case FIO_OPT_STR_VAL: {
285 fio_opt_str_val_fn *fn = o->cb;
288 ret = check_str_time(ptr, &ull);
290 ret = check_str_bytes(ptr, &ull);
295 if (o->maxval && ull > o->maxval) {
296 fprintf(stderr, "max value out of range: %lld"
297 " (%d max)\n", ull, o->maxval);
300 if (o->minval && ull < o->minval) {
301 fprintf(stderr, "min value out of range: %lld"
302 " (%d min)\n", ull, o->minval);
307 ret = fn(data, &ull);
309 if (o->type == FIO_OPT_INT) {
311 val_store(ilp, ull, o->off1, data);
312 if (!more && o->off2)
313 val_store(ilp, ull, o->off2, data);
316 val_store(ullp, ull, o->off1, data);
317 if (!more && o->off2)
318 val_store(ullp, ull, o->off2, data);
323 case FIO_OPT_STR_STORE: {
324 fio_opt_str_fn *fn = o->cb;
326 cp = td_var(data, o->off1);
337 case FIO_OPT_RANGE: {
341 strncpy(tmp, ptr, sizeof(tmp) - 1);
343 p1 = strchr(tmp, '-');
345 p1 = strchr(tmp, ':');
357 if (!check_range_bytes(p1, &ul1) &&
358 !check_range_bytes(p2, &ul2)) {
361 unsigned long foo = ul1;
368 val_store(ilp, ul1, o->off1, data);
369 val_store(ilp, ul2, o->off2, data);
371 if (o->off3 && o->off4) {
372 val_store(ilp, ul1, o->off3, data);
373 val_store(ilp, ul2, o->off4, data);
380 fio_opt_int_fn *fn = o->cb;
382 ret = check_int(ptr, &il);
386 if (o->maxval && il > (int) o->maxval) {
387 fprintf(stderr, "max value out of range: %d (%d max)\n",
391 if (o->minval && il < o->minval) {
392 fprintf(stderr, "min value out of range: %d (%d min)\n",
404 val_store(ilp, il, o->off1, data);
405 if (!more && o->off2)
406 val_store(ilp, il, o->off2, data);
410 case FIO_OPT_STR_SET: {
411 fio_opt_str_set_fn *fn = o->cb;
417 val_store(ilp, 1, o->off1, data);
418 if (!more && o->off2)
419 val_store(ilp, 1, o->off2, data);
423 case FIO_OPT_DEPRECATED:
424 fprintf(stdout, "Option %s is deprecated\n", o->name);
427 fprintf(stderr, "Bad option type %u\n", o->type);
434 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
436 char *ptr, *ptr2 = NULL;
439 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
446 * See if we have a second set of parameters, hidden after a comma.
447 * Do this before parsing the first round, to check if we should
448 * copy set 1 options to set 2.
451 (o->type != FIO_OPT_STR_STORE) &&
452 (o->type != FIO_OPT_STR)) {
453 ptr2 = strchr(ptr, ',');
454 if (ptr2 && *(ptr2 + 1) == '\0')
457 ptr2 = strchr(ptr, ':');
459 ptr2 = strchr(ptr, '-');
463 * Don't return early if parsing the first option fails - if
464 * we are doing multiple arguments, we can allow the first one
467 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
476 r2 = __handle_option(o, ptr2, data, 0, 0);
483 static struct fio_option *get_option(const char *opt,
484 struct fio_option *options, char **post)
486 struct fio_option *o;
489 ret = strchr(opt, '=');
495 strip_blank_end(ret);
496 o = find_option(options, ret);
498 o = find_option(options, opt);
505 static int opt_cmp(const void *p1, const void *p2)
507 struct fio_option *o1, *o2;
511 s1 = strdup(*((char **) p1));
512 s2 = strdup(*((char **) p2));
514 o1 = get_option(s1, fio_options, &foo);
515 o2 = get_option(s2, fio_options, &foo);
525 return prio2 - prio1;
528 void sort_options(char **opts, struct fio_option *options, int num_opts)
530 fio_options = options;
531 qsort(opts, num_opts, sizeof(char *), opt_cmp);
535 int parse_cmd_option(const char *opt, const char *val,
536 struct fio_option *options, void *data)
538 struct fio_option *o;
540 o = find_option(options, opt);
542 fprintf(stderr, "Bad option <%s>\n", opt);
546 if (!handle_option(o, val, data))
549 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
554 * Return a copy of the input string with substrings of the form ${VARNAME}
555 * substituted with the value of the environment variable VARNAME. The
556 * substitution always occurs, even if VARNAME is empty or the corresponding
557 * environment variable undefined.
559 static char *option_dup_subs(const char *opt)
561 char out[OPT_LEN_MAX+1];
562 char in[OPT_LEN_MAX+1];
565 char *ch1, *ch2, *env;
566 ssize_t nchr = OPT_LEN_MAX;
569 in[OPT_LEN_MAX] = '\0';
570 strncpy(in, opt, OPT_LEN_MAX);
572 while (*inptr && nchr > 0) {
573 if (inptr[0] == '$' && inptr[1] == '{') {
574 ch2 = strchr(inptr, '}');
575 if (ch2 && inptr+1 < ch2) {
582 envlen = strlen(env);
583 if (envlen <= nchr) {
584 memcpy(outptr, env, envlen);
594 *outptr++ = *inptr++;
602 int parse_option(const char *opt, struct fio_option *options, void *data)
604 struct fio_option *o;
607 tmp = option_dup_subs(opt);
609 o = get_option(tmp, options, &post);
611 fprintf(stderr, "Bad option <%s>\n", tmp);
616 if (!handle_option(o, post, data)) {
621 fprintf(stderr, "fio: failed parsing %s\n", opt);
627 * Option match, levenshtein distance. Handy for not quite remembering what
628 * the option name is.
630 static int string_distance(const char *s1, const char *s2)
632 unsigned int s1_len = strlen(s1);
633 unsigned int s2_len = strlen(s2);
634 unsigned int *p, *q, *r;
637 p = malloc(sizeof(unsigned int) * (s2_len + 1));
638 q = malloc(sizeof(unsigned int) * (s2_len + 1));
641 for (i = 1; i <= s2_len; i++)
644 for (i = 1; i <= s1_len; i++) {
646 for (j = 1; j <= s2_len; j++) {
647 unsigned int sub = p[j - 1];
649 if (s1[i - 1] != s2[j - 1])
652 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
665 static void show_option_help(struct fio_option *o)
667 const char *typehelp[] = {
669 "string with possible k/m/g postfix (opt=4k)",
670 "string with time postfix (opt=10s)",
672 "string with dual range (opt=1k-4k,4k-8k)",
673 "integer value (opt=100)",
674 "boolean value (opt=1)",
679 printf("%20s: %s\n", "alias", o->alias);
681 printf("%20s: %s\n", "type", typehelp[o->type]);
682 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
683 show_option_range(o);
684 show_option_values(o);
687 static struct fio_option *find_child(struct fio_option *options,
688 struct fio_option *o)
690 struct fio_option *__o;
692 for (__o = options + 1; __o->name; __o++)
693 if (__o->parent && !strcmp(__o->parent, o->name))
699 static void __print_option(struct fio_option *o, struct fio_option *org,
713 p += sprintf(p, "%s", " ");
715 sprintf(p, "%s", o->name);
717 printf("%-24s: %s\n", name, o->help);
720 static void print_option(struct fio_option *o)
722 struct fio_option *parent;
723 struct fio_option *__o;
724 unsigned int printed;
727 __print_option(o, NULL, 0);
734 while ((__o = find_child(o, parent)) != NULL) {
735 __print_option(__o, o, level);
744 int show_cmd_help(struct fio_option *options, const char *name)
746 struct fio_option *o, *closest;
747 unsigned int best_dist;
751 if (!name || !strcmp(name, "all"))
756 for (o = &options[0]; o->name; o++) {
759 if (o->type == FIO_OPT_DEPRECATED)
763 if (!strcmp(name, o->name) ||
764 (o->alias && !strcmp(name, o->alias)))
769 dist = string_distance(name, o->name);
770 if (dist < best_dist) {
777 if (show_all || match) {
780 printf("%24s: %s\n", o->name, o->help);
797 printf("No such command: %s", name);
799 printf(" - showing closest match\n");
800 printf("%20s: %s\n", closest->name, closest->help);
801 show_option_help(closest);
809 * Handle parsing of default parameters.
811 void fill_default_options(void *data, struct fio_option *options)
813 struct fio_option *o;
815 dprint(FD_PARSE, "filling default options\n");
817 for (o = &options[0]; o->name; o++)
819 handle_option(o, o->def, data);
823 * Sanitize the options structure. For now it just sets min/max for bool
824 * values and whether both callback and offsets are given.
826 void options_init(struct fio_option *options)
828 struct fio_option *o;
830 dprint(FD_PARSE, "init options\n");
832 for (o = &options[0]; o->name; o++) {
833 if (o->type == FIO_OPT_DEPRECATED)
835 if (o->type == FIO_OPT_BOOL) {
839 if (o->type == FIO_OPT_STR_SET && o->def) {
840 fprintf(stderr, "Option %s: string set option with"
841 " default will always be true\n",
844 if (!o->cb && !o->off1) {
845 fprintf(stderr, "Option %s: neither cb nor offset"
846 " given\n", o->name);
848 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
850 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
851 fprintf(stderr, "Option %s: both cb and offset given\n",