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);
435 ret = o->verify(o, data);
440 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
442 char *ptr, *ptr2 = NULL;
445 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
452 * See if we have a second set of parameters, hidden after a comma.
453 * Do this before parsing the first round, to check if we should
454 * copy set 1 options to set 2.
457 (o->type != FIO_OPT_STR_STORE) &&
458 (o->type != FIO_OPT_STR)) {
459 ptr2 = strchr(ptr, ',');
460 if (ptr2 && *(ptr2 + 1) == '\0')
463 ptr2 = strchr(ptr, ':');
465 ptr2 = strchr(ptr, '-');
469 * Don't return early if parsing the first option fails - if
470 * we are doing multiple arguments, we can allow the first one
473 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
482 r2 = __handle_option(o, ptr2, data, 0, 0);
489 static struct fio_option *get_option(const char *opt,
490 struct fio_option *options, char **post)
492 struct fio_option *o;
495 ret = strchr(opt, '=');
501 strip_blank_end(ret);
502 o = find_option(options, ret);
504 o = find_option(options, opt);
511 static int opt_cmp(const void *p1, const void *p2)
513 struct fio_option *o1, *o2;
517 s1 = strdup(*((char **) p1));
518 s2 = strdup(*((char **) p2));
520 o1 = get_option(s1, fio_options, &foo);
521 o2 = get_option(s2, fio_options, &foo);
531 return prio2 - prio1;
534 void sort_options(char **opts, struct fio_option *options, int num_opts)
536 fio_options = options;
537 qsort(opts, num_opts, sizeof(char *), opt_cmp);
541 int parse_cmd_option(const char *opt, const char *val,
542 struct fio_option *options, void *data)
544 struct fio_option *o;
546 o = find_option(options, opt);
548 fprintf(stderr, "Bad option <%s>\n", opt);
552 if (!handle_option(o, val, data))
555 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
560 * Return a copy of the input string with substrings of the form ${VARNAME}
561 * substituted with the value of the environment variable VARNAME. The
562 * substitution always occurs, even if VARNAME is empty or the corresponding
563 * environment variable undefined.
565 static char *option_dup_subs(const char *opt)
567 char out[OPT_LEN_MAX+1];
568 char in[OPT_LEN_MAX+1];
571 char *ch1, *ch2, *env;
572 ssize_t nchr = OPT_LEN_MAX;
575 in[OPT_LEN_MAX] = '\0';
576 strncpy(in, opt, OPT_LEN_MAX);
578 while (*inptr && nchr > 0) {
579 if (inptr[0] == '$' && inptr[1] == '{') {
580 ch2 = strchr(inptr, '}');
581 if (ch2 && inptr+1 < ch2) {
588 envlen = strlen(env);
589 if (envlen <= nchr) {
590 memcpy(outptr, env, envlen);
600 *outptr++ = *inptr++;
608 int parse_option(const char *opt, struct fio_option *options, void *data)
610 struct fio_option *o;
613 tmp = option_dup_subs(opt);
615 o = get_option(tmp, options, &post);
617 fprintf(stderr, "Bad option <%s>\n", tmp);
622 if (!handle_option(o, post, data)) {
627 fprintf(stderr, "fio: failed parsing %s\n", opt);
633 * Option match, levenshtein distance. Handy for not quite remembering what
634 * the option name is.
636 static int string_distance(const char *s1, const char *s2)
638 unsigned int s1_len = strlen(s1);
639 unsigned int s2_len = strlen(s2);
640 unsigned int *p, *q, *r;
643 p = malloc(sizeof(unsigned int) * (s2_len + 1));
644 q = malloc(sizeof(unsigned int) * (s2_len + 1));
647 for (i = 1; i <= s2_len; i++)
650 for (i = 1; i <= s1_len; i++) {
652 for (j = 1; j <= s2_len; j++) {
653 unsigned int sub = p[j - 1];
655 if (s1[i - 1] != s2[j - 1])
658 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
671 static void show_option_help(struct fio_option *o)
673 const char *typehelp[] = {
675 "string with possible k/m/g postfix (opt=4k)",
676 "string with time postfix (opt=10s)",
678 "string with dual range (opt=1k-4k,4k-8k)",
679 "integer value (opt=100)",
680 "boolean value (opt=1)",
685 printf("%20s: %s\n", "alias", o->alias);
687 printf("%20s: %s\n", "type", typehelp[o->type]);
688 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
689 show_option_range(o);
690 show_option_values(o);
693 static struct fio_option *find_child(struct fio_option *options,
694 struct fio_option *o)
696 struct fio_option *__o;
698 for (__o = options + 1; __o->name; __o++)
699 if (__o->parent && !strcmp(__o->parent, o->name))
705 static void __print_option(struct fio_option *o, struct fio_option *org,
719 p += sprintf(p, "%s", " ");
721 sprintf(p, "%s", o->name);
723 printf("%-24s: %s\n", name, o->help);
726 static void print_option(struct fio_option *o)
728 struct fio_option *parent;
729 struct fio_option *__o;
730 unsigned int printed;
733 __print_option(o, NULL, 0);
740 while ((__o = find_child(o, parent)) != NULL) {
741 __print_option(__o, o, level);
750 int show_cmd_help(struct fio_option *options, const char *name)
752 struct fio_option *o, *closest;
753 unsigned int best_dist;
757 if (!name || !strcmp(name, "all"))
762 for (o = &options[0]; o->name; o++) {
765 if (o->type == FIO_OPT_DEPRECATED)
769 if (!strcmp(name, o->name) ||
770 (o->alias && !strcmp(name, o->alias)))
775 dist = string_distance(name, o->name);
776 if (dist < best_dist) {
783 if (show_all || match) {
786 printf("%24s: %s\n", o->name, o->help);
803 printf("No such command: %s", name);
805 printf(" - showing closest match\n");
806 printf("%20s: %s\n", closest->name, closest->help);
807 show_option_help(closest);
815 * Handle parsing of default parameters.
817 void fill_default_options(void *data, struct fio_option *options)
819 struct fio_option *o;
821 dprint(FD_PARSE, "filling default options\n");
823 for (o = &options[0]; o->name; o++)
825 handle_option(o, o->def, data);
829 * Sanitize the options structure. For now it just sets min/max for bool
830 * values and whether both callback and offsets are given.
832 void options_init(struct fio_option *options)
834 struct fio_option *o;
836 dprint(FD_PARSE, "init options\n");
838 for (o = &options[0]; o->name; o++) {
839 if (o->type == FIO_OPT_DEPRECATED)
841 if (o->type == FIO_OPT_BOOL) {
845 if (o->type == FIO_OPT_STR_SET && o->def) {
846 fprintf(stderr, "Option %s: string set option with"
847 " default will always be true\n",
850 if (!o->cb && !o->off1) {
851 fprintf(stderr, "Option %s: neither cb nor offset"
852 " given\n", o->name);
854 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
856 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
857 fprintf(stderr, "Option %s: both cb and offset given\n",