2 * This file contains the ini and command liner parser main.
17 static struct fio_option *fio_options;
18 extern unsigned int fio_get_kb_base(void *);
20 static int vp_cmp(const void *p1, const void *p2)
22 const struct value_pair *vp1 = p1;
23 const struct value_pair *vp2 = p2;
25 return strlen(vp2->ival) - strlen(vp1->ival);
28 static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
30 const struct value_pair *vp;
33 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
35 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
36 vp = &o->posval[entries];
37 if (!vp->ival || vp->ival[0] == '\0')
40 memcpy(&vpmap[entries], vp, sizeof(*vp));
43 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
46 static void show_option_range(struct fio_option *o, FILE *out)
48 if (!o->minval && !o->maxval)
51 fprintf(out, "%20s: min=%d", "range", o->minval);
53 fprintf(out, ", max=%d", o->maxval);
57 static void show_option_values(struct fio_option *o)
61 for (i = 0; i < PARSE_MAX_VP; i++) {
62 const struct value_pair *vp = &o->posval[i];
67 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
69 printf(" %s", vp->help);
77 static void show_option_help(struct fio_option *o, FILE *out)
79 const char *typehelp[] = {
82 "string with possible k/m/g postfix (opt=4k)",
83 "string with time postfix (opt=10s)",
85 "string with dual range (opt=1k-4k,4k-8k)",
86 "integer value (opt=100)",
87 "boolean value (opt=1)",
93 fprintf(out, "%20s: %s\n", "alias", o->alias);
95 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
96 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
98 fprintf(out, "%20s: only for profile '%s'\n", "valid", o->prof_name);
99 show_option_range(o, stdout);
100 show_option_values(o);
103 static unsigned long get_mult_time(char c)
120 static unsigned long long get_mult_bytes(char c, void *data)
122 unsigned int kb_base = fio_get_kb_base(data);
123 unsigned long long ret = 1;
130 ret *= (unsigned long long) kb_base;
133 ret *= (unsigned long long) kb_base;
136 ret *= (unsigned long long) kb_base;
139 ret *= (unsigned long long) kb_base;
142 ret *= (unsigned long long) kb_base;
150 * convert string into decimal value, noting any size suffix
152 int str_to_decimal(const char *str, long long *val, int kilo, void *data)
160 if (strstr(str, "0x") || strstr(str, "0X"))
165 *val = strtoll(str, NULL, base);
166 if (*val == LONG_MAX && errno == ERANGE)
172 * if the last char is 'b' or 'B', the user likely used
173 * "1gb" instead of just "1g". If the second to last is also
177 if ((*p == 'b' || *p == 'B') && isalpha(*(p - 1)))
180 *val *= get_mult_bytes(*p, data);
182 *val *= get_mult_time(str[len - 1]);
187 static int check_str_bytes(const char *p, long long *val, void *data)
189 return str_to_decimal(p, val, 1, data);
192 static int check_str_time(const char *p, long long *val)
194 return str_to_decimal(p, val, 0, NULL);
197 void strip_blank_front(char **p)
207 void strip_blank_end(char *p)
221 while ((isspace(*s) || iscntrl(*s)) && (s > start))
227 static int check_range_bytes(const char *str, long *val, void *data)
234 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
235 *val *= get_mult_bytes(suffix, data);
239 if (sscanf(str, "%lu", val) == 1)
245 static int check_int(const char *p, int *val)
249 if (strstr(p, "0x") || strstr(p, "0X")) {
250 if (sscanf(p, "%x", val) == 1)
253 if (sscanf(p, "%u", val) == 1)
260 static inline int o_match(struct fio_option *o, const char *opt)
262 if (!strcmp(o->name, opt))
264 else if (o->alias && !strcmp(o->alias, opt))
270 static struct fio_option *find_option(struct fio_option *options,
273 struct fio_option *o;
275 for (o = &options[0]; o->name; o++)
282 #define val_store(ptr, val, off, data) \
284 ptr = td_var((data), (off)); \
288 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
292 long long ull, *ullp;
295 int ret = 0, is_time = 0;
297 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
300 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
301 fprintf(stderr, "Option %s requires an argument\n", o->name);
307 fio_opt_str_fn *fn = o->cb;
308 const struct value_pair *vp;
309 struct value_pair posval[PARSE_MAX_VP];
312 posval_sort(o, posval);
314 for (i = 0; i < PARSE_MAX_VP; i++) {
316 if (!vp->ival || vp->ival[0] == '\0')
319 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
322 *(unsigned int *) o->roff1 = vp->oval;
326 val_store(ilp, vp->oval, o->off1, data);
333 show_option_values(o);
338 case FIO_OPT_STR_VAL_TIME:
341 case FIO_OPT_STR_VAL: {
342 fio_opt_str_val_fn *fn = o->cb;
345 ret = check_str_time(ptr, &ull);
347 ret = check_str_bytes(ptr, &ull, data);
352 if (o->maxval && ull > o->maxval) {
353 fprintf(stderr, "max value out of range: %lld"
354 " (%d max)\n", ull, o->maxval);
357 if (o->minval && ull < o->minval) {
358 fprintf(stderr, "min value out of range: %lld"
359 " (%d min)\n", ull, o->minval);
364 ret = fn(data, &ull);
366 if (o->type == FIO_OPT_INT) {
369 *(unsigned long long *) o->roff1 = ull;
371 val_store(ilp, ull, o->off1, data);
375 *(unsigned long long *) o->roff2 = ull;
377 val_store(ilp, ull, o->off2, data);
382 *(unsigned long long *) o->roff1 = ull;
384 val_store(ullp, ull, o->off1, data);
388 *(unsigned long long *) o->roff2 = ull;
390 val_store(ullp, ull, o->off2, data);
396 case FIO_OPT_STR_STORE: {
397 fio_opt_str_fn *fn = o->cb;
400 cp = (char **) o->roff1;
402 cp = td_var(data, o->off1);
414 case FIO_OPT_RANGE: {
418 strncpy(tmp, ptr, sizeof(tmp) - 1);
420 p1 = strchr(tmp, '-');
422 p1 = strchr(tmp, ':');
434 if (!check_range_bytes(p1, &ul1, data) &&
435 !check_range_bytes(p2, &ul2, data)) {
438 unsigned long foo = ul1;
446 *(unsigned long *) o->roff1 = ul1;
448 val_store(ilp, ul1, o->off1, data);
450 *(unsigned long *) o->roff2 = ul2;
452 val_store(ilp, ul2, o->off2, data);
454 if (o->roff3 && o->roff4) {
455 *(unsigned long *) o->roff3 = ul1;
456 *(unsigned long *) o->roff4 = ul2;
457 } else if (o->off3 && o->off4) {
458 val_store(ilp, ul1, o->off3, data);
459 val_store(ilp, ul2, o->off4, data);
466 fio_opt_int_fn *fn = o->cb;
468 ret = check_int(ptr, &il);
472 if (o->maxval && il > (int) o->maxval) {
473 fprintf(stderr, "max value out of range: %d (%d max)\n",
477 if (o->minval && il < o->minval) {
478 fprintf(stderr, "min value out of range: %d (%d min)\n",
491 *(unsigned int *)o->roff1 = il;
493 val_store(ilp, il, o->off1, data);
497 *(unsigned int *) o->roff2 = il;
499 val_store(ilp, il, o->off2, data);
504 case FIO_OPT_STR_SET: {
505 fio_opt_str_set_fn *fn = o->cb;
512 *(unsigned int *) o->roff1 = 1;
514 val_store(ilp, 1, o->off1, data);
518 *(unsigned int *) o->roff2 = 1;
520 val_store(ilp, 1, o->off2, data);
525 case FIO_OPT_DEPRECATED:
526 fprintf(stdout, "Option %s is deprecated\n", o->name);
529 fprintf(stderr, "Bad option type %u\n", o->type);
537 ret = o->verify(o, data);
539 fprintf(stderr,"Correct format for offending option\n");
540 fprintf(stderr, "%20s: %s\n", o->name, o->help);
541 show_option_help(o, stderr);
548 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
550 char *ptr, *ptr2 = NULL;
553 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
560 * See if we have a second set of parameters, hidden after a comma.
561 * Do this before parsing the first round, to check if we should
562 * copy set 1 options to set 2.
565 (o->type != FIO_OPT_STR_STORE) &&
566 (o->type != FIO_OPT_STR)) {
567 ptr2 = strchr(ptr, ',');
568 if (ptr2 && *(ptr2 + 1) == '\0')
571 ptr2 = strchr(ptr, ':');
573 ptr2 = strchr(ptr, '-');
577 * Don't return early if parsing the first option fails - if
578 * we are doing multiple arguments, we can allow the first one
581 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
590 r2 = __handle_option(o, ptr2, data, 0, 0);
597 static struct fio_option *get_option(const char *opt,
598 struct fio_option *options, char **post)
600 struct fio_option *o;
603 ret = strchr(opt, '=');
609 strip_blank_end(ret);
610 o = find_option(options, ret);
612 o = find_option(options, opt);
619 static int opt_cmp(const void *p1, const void *p2)
621 struct fio_option *o1, *o2;
625 s1 = strdup(*((char **) p1));
626 s2 = strdup(*((char **) p2));
628 o1 = get_option(s1, fio_options, &foo);
629 o2 = get_option(s2, fio_options, &foo);
639 return prio2 - prio1;
642 void sort_options(char **opts, struct fio_option *options, int num_opts)
644 fio_options = options;
645 qsort(opts, num_opts, sizeof(char *), opt_cmp);
649 int parse_cmd_option(const char *opt, const char *val,
650 struct fio_option *options, void *data)
652 struct fio_option *o;
654 o = find_option(options, opt);
656 fprintf(stderr, "Bad option <%s>\n", opt);
660 if (!handle_option(o, val, data))
663 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
668 * Return a copy of the input string with substrings of the form ${VARNAME}
669 * substituted with the value of the environment variable VARNAME. The
670 * substitution always occurs, even if VARNAME is empty or the corresponding
671 * environment variable undefined.
673 static char *option_dup_subs(const char *opt)
675 char out[OPT_LEN_MAX+1];
676 char in[OPT_LEN_MAX+1];
679 char *ch1, *ch2, *env;
680 ssize_t nchr = OPT_LEN_MAX;
683 if (strlen(in) + 1 > OPT_LEN_MAX) {
684 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
688 in[OPT_LEN_MAX] = '\0';
689 strncpy(in, opt, OPT_LEN_MAX);
691 while (*inptr && nchr > 0) {
692 if (inptr[0] == '$' && inptr[1] == '{') {
693 ch2 = strchr(inptr, '}');
694 if (ch2 && inptr+1 < ch2) {
701 envlen = strlen(env);
702 if (envlen <= nchr) {
703 memcpy(outptr, env, envlen);
713 *outptr++ = *inptr++;
721 int parse_option(const char *opt, struct fio_option *options, void *data)
723 struct fio_option *o;
726 tmp = option_dup_subs(opt);
730 o = get_option(tmp, options, &post);
732 fprintf(stderr, "Bad option <%s>\n", tmp);
737 if (!handle_option(o, post, data)) {
742 fprintf(stderr, "fio: failed parsing %s\n", opt);
748 * Option match, levenshtein distance. Handy for not quite remembering what
749 * the option name is.
751 static int string_distance(const char *s1, const char *s2)
753 unsigned int s1_len = strlen(s1);
754 unsigned int s2_len = strlen(s2);
755 unsigned int *p, *q, *r;
758 p = malloc(sizeof(unsigned int) * (s2_len + 1));
759 q = malloc(sizeof(unsigned int) * (s2_len + 1));
762 for (i = 1; i <= s2_len; i++)
765 for (i = 1; i <= s1_len; i++) {
767 for (j = 1; j <= s2_len; j++) {
768 unsigned int sub = p[j - 1];
770 if (s1[i - 1] != s2[j - 1])
773 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
786 static struct fio_option *find_child(struct fio_option *options,
787 struct fio_option *o)
789 struct fio_option *__o;
791 for (__o = options + 1; __o->name; __o++)
792 if (__o->parent && !strcmp(__o->parent, o->name))
798 static void __print_option(struct fio_option *o, struct fio_option *org,
812 p += sprintf(p, "%s", " ");
814 sprintf(p, "%s", o->name);
816 printf("%-24s: %s\n", name, o->help);
819 static void print_option(struct fio_option *o)
821 struct fio_option *parent;
822 struct fio_option *__o;
823 unsigned int printed;
826 __print_option(o, NULL, 0);
833 while ((__o = find_child(o, parent)) != NULL) {
834 __print_option(__o, o, level);
843 int show_cmd_help(struct fio_option *options, const char *name)
845 struct fio_option *o, *closest;
846 unsigned int best_dist;
850 if (!name || !strcmp(name, "all"))
855 for (o = &options[0]; o->name; o++) {
858 if (o->type == FIO_OPT_DEPRECATED)
860 if (!exec_profile && o->prof_name)
864 if (!strcmp(name, o->name) ||
865 (o->alias && !strcmp(name, o->alias)))
870 dist = string_distance(name, o->name);
871 if (dist < best_dist) {
878 if (show_all || match) {
881 printf("%20s: %s\n", o->name, o->help);
892 show_option_help(o, stdout);
898 printf("No such command: %s", name);
900 printf(" - showing closest match\n");
901 printf("%20s: %s\n", closest->name, closest->help);
902 show_option_help(closest, stdout);
910 * Handle parsing of default parameters.
912 void fill_default_options(void *data, struct fio_option *options)
914 struct fio_option *o;
916 dprint(FD_PARSE, "filling default options\n");
918 for (o = &options[0]; o->name; o++)
920 handle_option(o, o->def, data);
923 void option_init(struct fio_option *o)
925 if (o->type == FIO_OPT_DEPRECATED)
927 if (o->type == FIO_OPT_BOOL) {
931 if (o->type == FIO_OPT_STR_SET && o->def) {
932 fprintf(stderr, "Option %s: string set option with"
933 " default will always be true\n", o->name);
935 if (!o->cb && (!o->off1 && !o->roff1)) {
936 fprintf(stderr, "Option %s: neither cb nor offset given\n",
939 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
941 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
942 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
943 fprintf(stderr, "Option %s: both cb and offset given\n",
949 * Sanitize the options structure. For now it just sets min/max for bool
950 * values and whether both callback and offsets are given.
952 void options_init(struct fio_option *options)
954 struct fio_option *o;
956 dprint(FD_PARSE, "init options\n");
958 for (o = &options[0]; o->name; o++)