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 *val = strtoll(str, NULL, 10);
122 if (*val == LONG_MAX && errno == ERANGE)
126 *val *= get_mult_bytes(str[len - 1]);
128 *val *= get_mult_time(str[len - 1]);
133 static int check_str_bytes(const char *p, long long *val)
135 return str_to_decimal(p, val, 1);
138 static int check_str_time(const char *p, long long *val)
140 return str_to_decimal(p, val, 0);
143 void strip_blank_front(char **p)
153 void strip_blank_end(char *p)
167 while ((isspace(*s) || iscntrl(*s)) && (s > start))
173 static int check_range_bytes(const char *str, long *val)
180 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
181 *val *= get_mult_bytes(suffix);
185 if (sscanf(str, "%lu", val) == 1)
191 static int check_int(const char *p, int *val)
195 if (strstr(p, "0x") || strstr(p, "0X")) {
196 if (sscanf(p, "%x", val) == 1)
199 if (sscanf(p, "%u", val) == 1)
206 static struct fio_option *find_option(struct fio_option *options,
209 struct fio_option *o;
211 for (o = &options[0]; o->name; o++) {
212 if (!strcmp(o->name, opt))
214 else if (o->alias && !strcmp(o->alias, opt))
221 #define val_store(ptr, val, off, data) \
223 ptr = td_var((data), (off)); \
227 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
231 long long ull, *ullp;
234 int ret = 0, is_time = 0;
236 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
239 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
240 fprintf(stderr, "Option %s requires an argument\n", o->name);
246 fio_opt_str_fn *fn = o->cb;
247 const struct value_pair *vp;
248 struct value_pair posval[PARSE_MAX_VP];
251 posval_sort(o, posval);
253 for (i = 0; i < PARSE_MAX_VP; i++) {
255 if (!vp->ival || vp->ival[0] == '\0')
258 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
262 val_store(ilp, vp->oval, o->off1, data);
268 show_option_values(o);
273 case FIO_OPT_STR_VAL_TIME:
275 case FIO_OPT_STR_VAL:
276 case FIO_OPT_STR_VAL_INT: {
277 fio_opt_str_val_fn *fn = o->cb;
280 ret = check_str_time(ptr, &ull);
282 ret = check_str_bytes(ptr, &ull);
287 if (o->maxval && ull > o->maxval) {
288 fprintf(stderr, "max value out of range: %lld"
289 " (%d max)\n", ull, o->maxval);
292 if (o->minval && ull < o->minval) {
293 fprintf(stderr, "min value out of range: %lld"
294 " (%d min)\n", ull, o->minval);
299 ret = fn(data, &ull);
301 if (o->type == FIO_OPT_STR_VAL_INT) {
303 val_store(ilp, ull, o->off1, data);
304 if (!more && o->off2)
305 val_store(ilp, ull, o->off2, data);
308 val_store(ullp, ull, o->off1, data);
309 if (!more && o->off2)
310 val_store(ullp, ull, o->off2, data);
315 case FIO_OPT_STR_STORE: {
316 fio_opt_str_fn *fn = o->cb;
318 cp = td_var(data, o->off1);
329 case FIO_OPT_RANGE: {
333 strncpy(tmp, ptr, sizeof(tmp) - 1);
335 p1 = strchr(tmp, '-');
337 p1 = strchr(tmp, ':');
349 if (!check_range_bytes(p1, &ul1) &&
350 !check_range_bytes(p2, &ul2)) {
353 unsigned long foo = ul1;
360 val_store(ilp, ul1, o->off1, data);
361 val_store(ilp, ul2, o->off2, data);
363 if (o->off3 && o->off4) {
364 val_store(ilp, ul1, o->off3, data);
365 val_store(ilp, ul2, o->off4, data);
373 fio_opt_int_fn *fn = o->cb;
375 ret = check_int(ptr, &il);
379 if (o->maxval && il > (int) o->maxval) {
380 fprintf(stderr, "max value out of range: %d (%d max)\n",
384 if (o->minval && il < o->minval) {
385 fprintf(stderr, "min value out of range: %d (%d min)\n",
397 val_store(ilp, il, o->off1, data);
398 if (!more && o->off2)
399 val_store(ilp, il, o->off2, data);
403 case FIO_OPT_STR_SET: {
404 fio_opt_str_set_fn *fn = o->cb;
410 val_store(ilp, 1, o->off1, data);
411 if (!more && o->off2)
412 val_store(ilp, 1, o->off2, data);
416 case FIO_OPT_DEPRECATED:
417 fprintf(stdout, "Option %s is deprecated\n", o->name);
420 fprintf(stderr, "Bad option type %u\n", o->type);
427 static int handle_option(struct fio_option *o, const char *ptr, void *data)
429 const char *ptr2 = NULL;
432 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, ptr);
435 * See if we have a second set of parameters, hidden after a comma.
436 * Do this before parsing the first round, to check if we should
437 * copy set 1 options to set 2.
440 (o->type != FIO_OPT_STR_STORE) &&
441 (o->type != FIO_OPT_STR)) {
442 ptr2 = strchr(ptr, ',');
444 ptr2 = strchr(ptr, ':');
446 ptr2 = strchr(ptr, '-');
450 * Don't return early if parsing the first option fails - if
451 * we are doing multiple arguments, we can allow the first one
454 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
460 r2 = __handle_option(o, ptr2, data, 0, 0);
465 static struct fio_option *get_option(const char *opt,
466 struct fio_option *options, char **post)
468 struct fio_option *o;
471 ret = strchr(opt, '=');
477 strip_blank_end(ret);
478 o = find_option(options, ret);
480 o = find_option(options, opt);
487 static int opt_cmp(const void *p1, const void *p2)
489 struct fio_option *o1, *o2;
493 s1 = strdup(*((char **) p1));
494 s2 = strdup(*((char **) p2));
496 o1 = get_option(s1, fio_options, &foo);
497 o2 = get_option(s2, fio_options, &foo);
507 return prio2 - prio1;
510 void sort_options(char **opts, struct fio_option *options, int num_opts)
512 fio_options = options;
513 qsort(opts, num_opts, sizeof(char *), opt_cmp);
517 int parse_cmd_option(const char *opt, const char *val,
518 struct fio_option *options, void *data)
520 struct fio_option *o;
522 o = find_option(options, opt);
524 fprintf(stderr, "Bad option <%s>\n", opt);
528 if (!handle_option(o, val, data))
531 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
536 * Return a copy of the input string with substrings of the form ${VARNAME}
537 * substituted with the value of the environment variable VARNAME. The
538 * substitution always occurs, even if VARNAME is empty or the corresponding
539 * environment variable undefined.
541 static char *option_dup_subs(const char *opt)
543 char out[OPT_LEN_MAX+1];
544 char in[OPT_LEN_MAX+1];
547 char *ch1, *ch2, *env;
548 ssize_t nchr = OPT_LEN_MAX;
551 in[OPT_LEN_MAX] = '\0';
552 strncpy(in, opt, OPT_LEN_MAX);
554 while (*inptr && nchr > 0) {
555 if (inptr[0] == '$' && inptr[1] == '{') {
556 ch2 = strchr(inptr, '}');
557 if (ch2 && inptr+1 < ch2) {
564 envlen = strlen(env);
565 if (envlen <= nchr) {
566 memcpy(outptr, env, envlen);
576 *outptr++ = *inptr++;
584 int parse_option(const char *opt, struct fio_option *options, void *data)
586 struct fio_option *o;
589 tmp = option_dup_subs(opt);
591 o = get_option(tmp, options, &post);
593 fprintf(stderr, "Bad option <%s>\n", tmp);
598 if (!handle_option(o, post, data)) {
603 fprintf(stderr, "fio: failed parsing %s\n", opt);
609 * Option match, levenshtein distance. Handy for not quite remembering what
610 * the option name is.
612 static int string_distance(const char *s1, const char *s2)
614 unsigned int s1_len = strlen(s1);
615 unsigned int s2_len = strlen(s2);
616 unsigned int *p, *q, *r;
619 p = malloc(sizeof(unsigned int) * (s2_len + 1));
620 q = malloc(sizeof(unsigned int) * (s2_len + 1));
623 for (i = 1; i <= s2_len; i++)
626 for (i = 1; i <= s1_len; i++) {
628 for (j = 1; j <= s2_len; j++) {
629 unsigned int sub = p[j - 1];
631 if (s1[i - 1] != s2[j - 1])
634 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
647 static void show_option_help(struct fio_option *o)
649 const char *typehelp[] = {
651 "string with possible k/m/g postfix (opt=4k)",
652 "string with range and postfix (opt=1k-4k)",
653 "string with time postfix (opt=10s)",
655 "string with dual range (opt=1k-4k,4k-8k)",
656 "integer value (opt=100)",
657 "boolean value (opt=1)",
662 printf("%20s: %s\n", "alias", o->alias);
664 printf("%20s: %s\n", "type", typehelp[o->type]);
665 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
666 show_option_range(o);
667 show_option_values(o);
670 static struct fio_option *find_child(struct fio_option *options,
671 struct fio_option *o)
673 struct fio_option *__o;
675 for (__o = options + 1; __o->name; __o++)
676 if (__o->parent && !strcmp(__o->parent, o->name))
682 static void __print_option(struct fio_option *o, struct fio_option *org,
696 p += sprintf(p, "%s", " ");
698 sprintf(p, "%s", o->name);
700 printf("%-24s: %s\n", name, o->help);
703 static void print_option(struct fio_option *o)
705 struct fio_option *parent;
706 struct fio_option *__o;
707 unsigned int printed;
710 __print_option(o, NULL, 0);
717 while ((__o = find_child(o, parent)) != NULL) {
718 __print_option(__o, o, level);
727 int show_cmd_help(struct fio_option *options, const char *name)
729 struct fio_option *o, *closest;
730 unsigned int best_dist;
734 if (!name || !strcmp(name, "all"))
739 for (o = &options[0]; o->name; o++) {
742 if (o->type == FIO_OPT_DEPRECATED)
746 if (!strcmp(name, o->name) ||
747 (o->alias && !strcmp(name, o->alias)))
752 dist = string_distance(name, o->name);
753 if (dist < best_dist) {
760 if (show_all || match) {
763 printf("%24s: %s\n", o->name, o->help);
780 printf("No such command: %s", name);
782 printf(" - showing closest match\n");
783 printf("%20s: %s\n", closest->name, closest->help);
784 show_option_help(closest);
792 * Handle parsing of default parameters.
794 void fill_default_options(void *data, struct fio_option *options)
796 struct fio_option *o;
798 dprint(FD_PARSE, "filling default options\n");
800 for (o = &options[0]; o->name; o++)
802 handle_option(o, o->def, data);
806 * Sanitize the options structure. For now it just sets min/max for bool
807 * values and whether both callback and offsets are given.
809 void options_init(struct fio_option *options)
811 struct fio_option *o;
813 dprint(FD_PARSE, "init options\n");
815 for (o = &options[0]; o->name; o++) {
816 if (o->type == FIO_OPT_DEPRECATED)
818 if (o->type == FIO_OPT_BOOL) {
822 if (o->type == FIO_OPT_STR_SET && o->def) {
823 fprintf(stderr, "Option %s: string set option with"
824 " default will always be true\n",
827 if (!o->cb && !o->off1) {
828 fprintf(stderr, "Option %s: neither cb nor offset"
829 " given\n", o->name);
831 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
833 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
834 fprintf(stderr, "Option %s: both cb and offset given\n",