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 #define val_store(ptr, val, off, or, data) \
262 ptr = td_var((data), (off)); \
269 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
273 long long ull, *ullp;
276 int ret = 0, is_time = 0;
278 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
281 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
282 fprintf(stderr, "Option %s requires an argument\n", o->name);
288 case FIO_OPT_STR_MULTI: {
289 fio_opt_str_fn *fn = o->cb;
290 const struct value_pair *vp;
291 struct value_pair posval[PARSE_MAX_VP];
292 int i, all_skipped = 1;
294 posval_sort(o, posval);
297 for (i = 0; i < PARSE_MAX_VP; i++) {
299 if (!vp->ival || vp->ival[0] == '\0')
302 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
306 *(unsigned int *) o->roff1 |= vp->oval;
308 *(unsigned int *) o->roff1 = vp->oval;
312 val_store(ilp, vp->oval, o->off1, vp->or, data);
318 if (ret && !all_skipped)
319 show_option_values(o);
324 case FIO_OPT_STR_VAL_TIME:
327 case FIO_OPT_STR_VAL: {
328 fio_opt_str_val_fn *fn = o->cb;
331 ret = check_str_time(ptr, &ull);
333 ret = check_str_bytes(ptr, &ull, data);
338 if (o->maxval && ull > o->maxval) {
339 fprintf(stderr, "max value out of range: %lld"
340 " (%d max)\n", ull, o->maxval);
343 if (o->minval && ull < o->minval) {
344 fprintf(stderr, "min value out of range: %lld"
345 " (%d min)\n", ull, o->minval);
350 ret = fn(data, &ull);
352 if (o->type == FIO_OPT_INT) {
355 *(unsigned int *) o->roff1 = ull;
357 val_store(ilp, ull, o->off1, 0, data);
361 *(unsigned int *) o->roff2 = ull;
363 val_store(ilp, ull, o->off2, 0, data);
368 *(unsigned long long *) o->roff1 = ull;
370 val_store(ullp, ull, o->off1, 0, data);
374 *(unsigned long long *) o->roff2 = ull;
376 val_store(ullp, ull, o->off2, 0, data);
382 case FIO_OPT_STR_STORE: {
383 fio_opt_str_fn *fn = o->cb;
386 cp = (char **) o->roff1;
388 cp = td_var(data, o->off1);
400 case FIO_OPT_RANGE: {
404 strncpy(tmp, ptr, sizeof(tmp) - 1);
406 p1 = strchr(tmp, '-');
408 p1 = strchr(tmp, ':');
420 if (!check_range_bytes(p1, &ul1, data) &&
421 !check_range_bytes(p2, &ul2, data)) {
424 unsigned long foo = ul1;
432 *(unsigned int *) o->roff1 = ul1;
434 val_store(ilp, ul1, o->off1, 0, data);
436 *(unsigned int *) o->roff2 = ul2;
438 val_store(ilp, ul2, o->off2, 0, data);
440 if (o->roff3 && o->roff4) {
441 *(unsigned int *) o->roff3 = ul1;
442 *(unsigned int *) o->roff4 = ul2;
443 } else if (o->off3 && o->off4) {
444 val_store(ilp, ul1, o->off3, 0, data);
445 val_store(ilp, ul2, o->off4, 0, data);
452 fio_opt_int_fn *fn = o->cb;
454 ret = check_int(ptr, &il);
458 if (o->maxval && il > (int) o->maxval) {
459 fprintf(stderr, "max value out of range: %d (%d max)\n",
463 if (o->minval && il < o->minval) {
464 fprintf(stderr, "min value out of range: %d (%d min)\n",
477 *(unsigned int *)o->roff1 = il;
479 val_store(ilp, il, o->off1, 0, data);
483 *(unsigned int *) o->roff2 = il;
485 val_store(ilp, il, o->off2, 0, data);
490 case FIO_OPT_STR_SET: {
491 fio_opt_str_set_fn *fn = o->cb;
498 *(unsigned int *) o->roff1 = 1;
500 val_store(ilp, 1, o->off1, 0, data);
504 *(unsigned int *) o->roff2 = 1;
506 val_store(ilp, 1, o->off2, 0, data);
511 case FIO_OPT_DEPRECATED:
512 fprintf(stdout, "Option %s is deprecated\n", o->name);
515 fprintf(stderr, "Bad option type %u\n", o->type);
523 ret = o->verify(o, data);
525 fprintf(stderr,"Correct format for offending option\n");
526 fprintf(stderr, "%20s: %s\n", o->name, o->help);
527 show_option_help(o, stderr);
534 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
536 char *o_ptr, *ptr, *ptr2;
539 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
543 o_ptr = ptr = strdup(__ptr);
546 * See if we have another set of parameters, hidden after a comma.
547 * Do this before parsing this round, to check if we should
548 * copy set 1 options to set 2.
557 (o->type != FIO_OPT_STR_STORE) &&
558 (o->type != FIO_OPT_STR)) {
559 ptr2 = strchr(ptr, ',');
560 if (ptr2 && *(ptr2 + 1) == '\0')
562 if (o->type != FIO_OPT_STR_MULTI) {
564 ptr2 = strchr(ptr, ':');
566 ptr2 = strchr(ptr, '-');
571 * Don't return early if parsing the first option fails - if
572 * we are doing multiple arguments, we can allow the first one
575 __ret = __handle_option(o, ptr, data, !done, !!ptr2);
591 static struct fio_option *get_option(const char *opt,
592 struct fio_option *options, char **post)
594 struct fio_option *o;
597 ret = strchr(opt, '=');
603 strip_blank_end(ret);
604 o = find_option(options, ret);
606 o = find_option(options, opt);
613 static int opt_cmp(const void *p1, const void *p2)
615 struct fio_option *o1, *o2;
619 s1 = strdup(*((char **) p1));
620 s2 = strdup(*((char **) p2));
622 o1 = get_option(s1, fio_options, &foo);
623 o2 = get_option(s2, fio_options, &foo);
633 return prio2 - prio1;
636 void sort_options(char **opts, struct fio_option *options, int num_opts)
638 fio_options = options;
639 qsort(opts, num_opts, sizeof(char *), opt_cmp);
643 int parse_cmd_option(const char *opt, const char *val,
644 struct fio_option *options, void *data)
646 struct fio_option *o;
648 o = find_option(options, opt);
650 fprintf(stderr, "Bad option <%s>\n", opt);
654 if (!handle_option(o, val, data))
657 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
662 * Return a copy of the input string with substrings of the form ${VARNAME}
663 * substituted with the value of the environment variable VARNAME. The
664 * substitution always occurs, even if VARNAME is empty or the corresponding
665 * environment variable undefined.
667 static char *option_dup_subs(const char *opt)
669 char out[OPT_LEN_MAX+1];
670 char in[OPT_LEN_MAX+1];
673 char *ch1, *ch2, *env;
674 ssize_t nchr = OPT_LEN_MAX;
677 if (strlen(opt) + 1 > OPT_LEN_MAX) {
678 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
682 in[OPT_LEN_MAX] = '\0';
683 strncpy(in, opt, OPT_LEN_MAX);
685 while (*inptr && nchr > 0) {
686 if (inptr[0] == '$' && inptr[1] == '{') {
687 ch2 = strchr(inptr, '}');
688 if (ch2 && inptr+1 < ch2) {
695 envlen = strlen(env);
696 if (envlen <= nchr) {
697 memcpy(outptr, env, envlen);
707 *outptr++ = *inptr++;
715 int parse_option(const char *opt, struct fio_option *options, void *data)
717 struct fio_option *o;
720 tmp = option_dup_subs(opt);
724 o = get_option(tmp, options, &post);
726 fprintf(stderr, "Bad option <%s>\n", tmp);
731 if (!handle_option(o, post, data)) {
736 fprintf(stderr, "fio: failed parsing %s\n", opt);
742 * Option match, levenshtein distance. Handy for not quite remembering what
743 * the option name is.
745 static int string_distance(const char *s1, const char *s2)
747 unsigned int s1_len = strlen(s1);
748 unsigned int s2_len = strlen(s2);
749 unsigned int *p, *q, *r;
752 p = malloc(sizeof(unsigned int) * (s2_len + 1));
753 q = malloc(sizeof(unsigned int) * (s2_len + 1));
756 for (i = 1; i <= s2_len; i++)
759 for (i = 1; i <= s1_len; i++) {
761 for (j = 1; j <= s2_len; j++) {
762 unsigned int sub = p[j - 1];
764 if (s1[i - 1] != s2[j - 1])
767 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
780 static struct fio_option *find_child(struct fio_option *options,
781 struct fio_option *o)
783 struct fio_option *__o;
785 for (__o = options + 1; __o->name; __o++)
786 if (__o->parent && !strcmp(__o->parent, o->name))
792 static void __print_option(struct fio_option *o, struct fio_option *org,
806 p += sprintf(p, "%s", " ");
808 sprintf(p, "%s", o->name);
810 printf("%-24s: %s\n", name, o->help);
813 static void print_option(struct fio_option *o)
815 struct fio_option *parent;
816 struct fio_option *__o;
817 unsigned int printed;
820 __print_option(o, NULL, 0);
827 while ((__o = find_child(o, parent)) != NULL) {
828 __print_option(__o, o, level);
837 int show_cmd_help(struct fio_option *options, const char *name)
839 struct fio_option *o, *closest;
840 unsigned int best_dist = -1U;
844 if (!name || !strcmp(name, "all"))
849 for (o = &options[0]; o->name; o++) {
852 if (o->type == FIO_OPT_DEPRECATED)
854 if (!exec_profile && o->prof_name)
858 if (!strcmp(name, o->name) ||
859 (o->alias && !strcmp(name, o->alias)))
864 dist = string_distance(name, o->name);
865 if (dist < best_dist) {
872 if (show_all || match) {
875 printf("%20s: %s\n", o->name, o->help);
886 show_option_help(o, stdout);
892 printf("No such command: %s", name);
895 * Only print an appropriately close option, one where the edit
896 * distance isn't too big. Otherwise we get crazy matches.
898 if (closest && best_dist < 3) {
899 printf(" - showing closest match\n");
900 printf("%20s: %s\n", closest->name, closest->help);
901 show_option_help(closest, stdout);
909 * Handle parsing of default parameters.
911 void fill_default_options(void *data, struct fio_option *options)
913 struct fio_option *o;
915 dprint(FD_PARSE, "filling default options\n");
917 for (o = &options[0]; o->name; o++)
919 handle_option(o, o->def, data);
922 void option_init(struct fio_option *o)
924 if (o->type == FIO_OPT_DEPRECATED)
926 if (o->type == FIO_OPT_BOOL) {
930 if (o->type == FIO_OPT_STR_SET && o->def) {
931 fprintf(stderr, "Option %s: string set option with"
932 " default will always be true\n", o->name);
934 if (!o->cb && (!o->off1 && !o->roff1)) {
935 fprintf(stderr, "Option %s: neither cb nor offset given\n",
938 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
939 o->type == FIO_OPT_STR_MULTI)
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++)