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(const char *p, void *data)
122 unsigned int kb_base = fio_get_kb_base(data);
123 unsigned long long ret = 1;
124 unsigned int i, pow = 0, mult = kb_base;
132 for (i = 0; i < strlen(c); i++)
133 c[i] = tolower(c[i]);
135 if (!strcmp("pib", c)) {
138 } else if (!strcmp("tib", c)) {
141 } else if (!strcmp("gib", c)) {
144 } else if (!strcmp("mib", c)) {
147 } else if (!strcmp("kib", c)) {
150 } else if (!strcmp("p", c) || !strcmp("pb", c))
152 else if (!strcmp("t", c) || !strcmp("tb", c))
154 else if (!strcmp("g", c) || !strcmp("gb", c))
156 else if (!strcmp("m", c) || !strcmp("mb", c))
158 else if (!strcmp("k", c) || !strcmp("kb", c))
162 ret *= (unsigned long long) mult;
168 static unsigned long long get_mult_bytes(const char *str, int len, void *data)
173 * if the last char is 'b' or 'B', the user likely used
174 * "1gb" instead of just "1g". If the second to last is also
178 while (isalpha(*(p - 1)))
183 return __get_mult_bytes(p, data);
187 * convert string into decimal value, noting any size suffix
189 int str_to_decimal(const char *str, long long *val, int kilo, void *data)
197 if (strstr(str, "0x") || strstr(str, "0X"))
202 *val = strtoll(str, NULL, base);
203 if (*val == LONG_MAX && errno == ERANGE)
207 *val *= get_mult_bytes(str, len, data);
209 *val *= get_mult_time(str[len - 1]);
214 static int check_str_bytes(const char *p, long long *val, void *data)
216 return str_to_decimal(p, val, 1, data);
219 static int check_str_time(const char *p, long long *val)
221 return str_to_decimal(p, val, 0, NULL);
224 void strip_blank_front(char **p)
234 void strip_blank_end(char *p)
248 while ((isspace(*s) || iscntrl(*s)) && (s > start))
254 static int check_range_bytes(const char *str, long *val, void *data)
258 if (!str_to_decimal(str, &__val, 1, data)) {
266 static int check_int(const char *p, int *val)
270 if (strstr(p, "0x") || strstr(p, "0X")) {
271 if (sscanf(p, "%x", val) == 1)
274 if (sscanf(p, "%u", val) == 1)
281 #define val_store(ptr, val, off, or, data) \
283 ptr = td_var((data), (off)); \
290 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
294 long long ull, *ullp;
297 int ret = 0, is_time = 0;
299 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
302 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
303 fprintf(stderr, "Option %s requires an argument\n", o->name);
309 case FIO_OPT_STR_MULTI: {
310 fio_opt_str_fn *fn = o->cb;
311 const struct value_pair *vp;
312 struct value_pair posval[PARSE_MAX_VP];
313 int i, all_skipped = 1;
315 posval_sort(o, posval);
318 for (i = 0; i < PARSE_MAX_VP; i++) {
320 if (!vp->ival || vp->ival[0] == '\0')
323 if (!strncmp(vp->ival, ptr, strlen(ptr))) {
327 *(unsigned int *) o->roff1 |= vp->oval;
329 *(unsigned int *) o->roff1 = vp->oval;
333 val_store(ilp, vp->oval, o->off1, vp->or, data);
339 if (ret && !all_skipped)
340 show_option_values(o);
345 case FIO_OPT_STR_VAL_TIME:
348 case FIO_OPT_STR_VAL: {
349 fio_opt_str_val_fn *fn = o->cb;
352 ret = check_str_time(ptr, &ull);
354 ret = check_str_bytes(ptr, &ull, data);
359 if (o->maxval && ull > o->maxval) {
360 fprintf(stderr, "max value out of range: %lld"
361 " (%d max)\n", ull, o->maxval);
364 if (o->minval && ull < o->minval) {
365 fprintf(stderr, "min value out of range: %lld"
366 " (%d min)\n", ull, o->minval);
371 ret = fn(data, &ull);
373 if (o->type == FIO_OPT_INT) {
376 *(unsigned int *) o->roff1 = ull;
378 val_store(ilp, ull, o->off1, 0, data);
382 *(unsigned int *) o->roff2 = ull;
384 val_store(ilp, ull, o->off2, 0, data);
389 *(unsigned long long *) o->roff1 = ull;
391 val_store(ullp, ull, o->off1, 0, data);
395 *(unsigned long long *) o->roff2 = ull;
397 val_store(ullp, ull, o->off2, 0, data);
403 case FIO_OPT_STR_STORE: {
404 fio_opt_str_fn *fn = o->cb;
407 cp = (char **) o->roff1;
409 cp = td_var(data, o->off1);
421 case FIO_OPT_RANGE: {
425 strncpy(tmp, ptr, sizeof(tmp) - 1);
427 p1 = strchr(tmp, '-');
429 p1 = strchr(tmp, ':');
441 if (!check_range_bytes(p1, &ul1, data) &&
442 !check_range_bytes(p2, &ul2, data)) {
445 unsigned long foo = ul1;
453 *(unsigned int *) o->roff1 = ul1;
455 val_store(ilp, ul1, o->off1, 0, data);
457 *(unsigned int *) o->roff2 = ul2;
459 val_store(ilp, ul2, o->off2, 0, data);
461 if (o->roff3 && o->roff4) {
462 *(unsigned int *) o->roff3 = ul1;
463 *(unsigned int *) o->roff4 = ul2;
464 } else if (o->off3 && o->off4) {
465 val_store(ilp, ul1, o->off3, 0, data);
466 val_store(ilp, ul2, o->off4, 0, data);
473 fio_opt_int_fn *fn = o->cb;
475 ret = check_int(ptr, &il);
479 if (o->maxval && il > (int) o->maxval) {
480 fprintf(stderr, "max value out of range: %d (%d max)\n",
484 if (o->minval && il < o->minval) {
485 fprintf(stderr, "min value out of range: %d (%d min)\n",
498 *(unsigned int *)o->roff1 = il;
500 val_store(ilp, il, o->off1, 0, data);
504 *(unsigned int *) o->roff2 = il;
506 val_store(ilp, il, o->off2, 0, data);
511 case FIO_OPT_STR_SET: {
512 fio_opt_str_set_fn *fn = o->cb;
519 *(unsigned int *) o->roff1 = 1;
521 val_store(ilp, 1, o->off1, 0, data);
525 *(unsigned int *) o->roff2 = 1;
527 val_store(ilp, 1, o->off2, 0, data);
532 case FIO_OPT_DEPRECATED:
533 fprintf(stdout, "Option %s is deprecated\n", o->name);
536 fprintf(stderr, "Bad option type %u\n", o->type);
544 ret = o->verify(o, data);
546 fprintf(stderr,"Correct format for offending option\n");
547 fprintf(stderr, "%20s: %s\n", o->name, o->help);
548 show_option_help(o, stderr);
555 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
557 char *o_ptr, *ptr, *ptr2;
560 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
564 o_ptr = ptr = strdup(__ptr);
567 * See if we have another set of parameters, hidden after a comma.
568 * Do this before parsing this round, to check if we should
569 * copy set 1 options to set 2.
578 (o->type != FIO_OPT_STR_STORE) &&
579 (o->type != FIO_OPT_STR)) {
580 ptr2 = strchr(ptr, ',');
581 if (ptr2 && *(ptr2 + 1) == '\0')
583 if (o->type != FIO_OPT_STR_MULTI) {
585 ptr2 = strchr(ptr, ':');
587 ptr2 = strchr(ptr, '-');
592 * Don't return early if parsing the first option fails - if
593 * we are doing multiple arguments, we can allow the first one
596 __ret = __handle_option(o, ptr, data, !done, !!ptr2);
612 static struct fio_option *get_option(const char *opt,
613 struct fio_option *options, char **post)
615 struct fio_option *o;
618 ret = strchr(opt, '=');
624 strip_blank_end(ret);
625 o = find_option(options, ret);
627 o = find_option(options, opt);
634 static int opt_cmp(const void *p1, const void *p2)
636 struct fio_option *o1, *o2;
640 s1 = strdup(*((char **) p1));
641 s2 = strdup(*((char **) p2));
643 o1 = get_option(s1, fio_options, &foo);
644 o2 = get_option(s2, fio_options, &foo);
654 return prio2 - prio1;
657 void sort_options(char **opts, struct fio_option *options, int num_opts)
659 fio_options = options;
660 qsort(opts, num_opts, sizeof(char *), opt_cmp);
664 int parse_cmd_option(const char *opt, const char *val,
665 struct fio_option *options, void *data)
667 struct fio_option *o;
669 o = find_option(options, opt);
671 fprintf(stderr, "Bad option <%s>\n", opt);
675 if (!handle_option(o, val, data))
678 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
683 * Return a copy of the input string with substrings of the form ${VARNAME}
684 * substituted with the value of the environment variable VARNAME. The
685 * substitution always occurs, even if VARNAME is empty or the corresponding
686 * environment variable undefined.
688 static char *option_dup_subs(const char *opt)
690 char out[OPT_LEN_MAX+1];
691 char in[OPT_LEN_MAX+1];
694 char *ch1, *ch2, *env;
695 ssize_t nchr = OPT_LEN_MAX;
698 if (strlen(opt) + 1 > OPT_LEN_MAX) {
699 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
703 in[OPT_LEN_MAX] = '\0';
704 strncpy(in, opt, OPT_LEN_MAX);
706 while (*inptr && nchr > 0) {
707 if (inptr[0] == '$' && inptr[1] == '{') {
708 ch2 = strchr(inptr, '}');
709 if (ch2 && inptr+1 < ch2) {
716 envlen = strlen(env);
717 if (envlen <= nchr) {
718 memcpy(outptr, env, envlen);
728 *outptr++ = *inptr++;
736 int parse_option(const char *opt, struct fio_option *options, void *data)
738 struct fio_option *o;
741 tmp = option_dup_subs(opt);
745 o = get_option(tmp, options, &post);
747 fprintf(stderr, "Bad option <%s>\n", tmp);
752 if (!handle_option(o, post, data)) {
757 fprintf(stderr, "fio: failed parsing %s\n", opt);
763 * Option match, levenshtein distance. Handy for not quite remembering what
764 * the option name is.
766 static int string_distance(const char *s1, const char *s2)
768 unsigned int s1_len = strlen(s1);
769 unsigned int s2_len = strlen(s2);
770 unsigned int *p, *q, *r;
773 p = malloc(sizeof(unsigned int) * (s2_len + 1));
774 q = malloc(sizeof(unsigned int) * (s2_len + 1));
777 for (i = 1; i <= s2_len; i++)
780 for (i = 1; i <= s1_len; i++) {
782 for (j = 1; j <= s2_len; j++) {
783 unsigned int sub = p[j - 1];
785 if (s1[i - 1] != s2[j - 1])
788 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
801 static struct fio_option *find_child(struct fio_option *options,
802 struct fio_option *o)
804 struct fio_option *__o;
806 for (__o = options + 1; __o->name; __o++)
807 if (__o->parent && !strcmp(__o->parent, o->name))
813 static void __print_option(struct fio_option *o, struct fio_option *org,
827 p += sprintf(p, "%s", " ");
829 sprintf(p, "%s", o->name);
831 printf("%-24s: %s\n", name, o->help);
834 static void print_option(struct fio_option *o)
836 struct fio_option *parent;
837 struct fio_option *__o;
838 unsigned int printed;
841 __print_option(o, NULL, 0);
848 while ((__o = find_child(o, parent)) != NULL) {
849 __print_option(__o, o, level);
858 int show_cmd_help(struct fio_option *options, const char *name)
860 struct fio_option *o, *closest;
861 unsigned int best_dist = -1U;
865 if (!name || !strcmp(name, "all"))
870 for (o = &options[0]; o->name; o++) {
873 if (o->type == FIO_OPT_DEPRECATED)
875 if (!exec_profile && o->prof_name)
879 if (!strcmp(name, o->name) ||
880 (o->alias && !strcmp(name, o->alias)))
885 dist = string_distance(name, o->name);
886 if (dist < best_dist) {
893 if (show_all || match) {
896 printf("%20s: %s\n", o->name, o->help);
907 show_option_help(o, stdout);
913 printf("No such command: %s", name);
916 * Only print an appropriately close option, one where the edit
917 * distance isn't too big. Otherwise we get crazy matches.
919 if (closest && best_dist < 3) {
920 printf(" - showing closest match\n");
921 printf("%20s: %s\n", closest->name, closest->help);
922 show_option_help(closest, stdout);
930 * Handle parsing of default parameters.
932 void fill_default_options(void *data, struct fio_option *options)
934 struct fio_option *o;
936 dprint(FD_PARSE, "filling default options\n");
938 for (o = &options[0]; o->name; o++)
940 handle_option(o, o->def, data);
943 void option_init(struct fio_option *o)
945 if (o->type == FIO_OPT_DEPRECATED)
947 if (o->type == FIO_OPT_BOOL) {
951 if (o->type == FIO_OPT_STR_SET && o->def) {
952 fprintf(stderr, "Option %s: string set option with"
953 " default will always be true\n", o->name);
955 if (!o->cb && (!o->off1 && !o->roff1)) {
956 fprintf(stderr, "Option %s: neither cb nor offset given\n",
959 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
960 o->type == FIO_OPT_STR_MULTI)
962 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
963 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
964 fprintf(stderr, "Option %s: both cb and offset given\n",
970 * Sanitize the options structure. For now it just sets min/max for bool
971 * values and whether both callback and offsets are given.
973 void options_init(struct fio_option *options)
975 struct fio_option *o;
977 dprint(FD_PARSE, "init options\n");
979 for (o = &options[0]; o->name; o++)