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, FILE *out)
46 if (!o->minval && !o->maxval)
49 fprintf(out, "%20s: min=%d", "range", o->minval);
51 fprintf(out, ", 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 void show_option_help(struct fio_option *o, FILE *out)
78 const char *typehelp[] = {
80 "string with possible k/m/g postfix (opt=4k)",
81 "string with time postfix (opt=10s)",
83 "string with dual range (opt=1k-4k,4k-8k)",
84 "integer value (opt=100)",
85 "boolean value (opt=1)",
90 fprintf(out, "%20s: %s\n", "alias", o->alias);
92 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
93 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
94 show_option_range(o, stdout);
95 show_option_values(o);
98 static unsigned long get_mult_time(char c)
115 static unsigned long get_mult_bytes(char c)
126 return 1024 * 1024 * 1024;
129 return 1024 * 1024 * 1024 * 1024UL;
136 * convert string into decimal value, noting any size suffix
138 int str_to_decimal(const char *str, long long *val, int kilo)
146 if (strstr(str, "0x") || strstr(str, "0X"))
151 *val = strtoll(str, NULL, base);
152 if (*val == LONG_MAX && errno == ERANGE)
156 *val *= get_mult_bytes(str[len - 1]);
158 *val *= get_mult_time(str[len - 1]);
163 static int check_str_bytes(const char *p, long long *val)
165 return str_to_decimal(p, val, 1);
168 static int check_str_time(const char *p, long long *val)
170 return str_to_decimal(p, val, 0);
173 void strip_blank_front(char **p)
183 void strip_blank_end(char *p)
197 while ((isspace(*s) || iscntrl(*s)) && (s > start))
203 static int check_range_bytes(const char *str, long *val)
210 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
211 *val *= get_mult_bytes(suffix);
215 if (sscanf(str, "%lu", val) == 1)
221 static int check_int(const char *p, int *val)
225 if (strstr(p, "0x") || strstr(p, "0X")) {
226 if (sscanf(p, "%x", val) == 1)
229 if (sscanf(p, "%u", val) == 1)
236 static struct fio_option *find_option(struct fio_option *options,
239 struct fio_option *o;
241 for (o = &options[0]; o->name; o++) {
242 if (!strcmp(o->name, opt))
244 else if (o->alias && !strcmp(o->alias, opt))
251 #define val_store(ptr, val, off, data) \
253 ptr = td_var((data), (off)); \
257 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
261 long long ull, *ullp;
264 int ret = 0, is_time = 0;
266 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
269 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
270 fprintf(stderr, "Option %s requires an argument\n", o->name);
276 fio_opt_str_fn *fn = o->cb;
277 const struct value_pair *vp;
278 struct value_pair posval[PARSE_MAX_VP];
281 posval_sort(o, posval);
283 for (i = 0; i < PARSE_MAX_VP; i++) {
285 if (!vp->ival || vp->ival[0] == '\0')
288 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
292 val_store(ilp, vp->oval, o->off1, data);
298 show_option_values(o);
303 case FIO_OPT_STR_VAL_TIME:
306 case FIO_OPT_STR_VAL: {
307 fio_opt_str_val_fn *fn = o->cb;
310 ret = check_str_time(ptr, &ull);
312 ret = check_str_bytes(ptr, &ull);
317 if (o->maxval && ull > o->maxval) {
318 fprintf(stderr, "max value out of range: %lld"
319 " (%d max)\n", ull, o->maxval);
322 if (o->minval && ull < o->minval) {
323 fprintf(stderr, "min value out of range: %lld"
324 " (%d min)\n", ull, o->minval);
329 ret = fn(data, &ull);
331 if (o->type == FIO_OPT_INT) {
333 val_store(ilp, ull, o->off1, data);
334 if (!more && o->off2)
335 val_store(ilp, ull, o->off2, data);
338 val_store(ullp, ull, o->off1, data);
339 if (!more && o->off2)
340 val_store(ullp, ull, o->off2, data);
345 case FIO_OPT_STR_STORE: {
346 fio_opt_str_fn *fn = o->cb;
348 cp = td_var(data, o->off1);
359 case FIO_OPT_RANGE: {
363 strncpy(tmp, ptr, sizeof(tmp) - 1);
365 p1 = strchr(tmp, '-');
367 p1 = strchr(tmp, ':');
379 if (!check_range_bytes(p1, &ul1) &&
380 !check_range_bytes(p2, &ul2)) {
383 unsigned long foo = ul1;
390 val_store(ilp, ul1, o->off1, data);
391 val_store(ilp, ul2, o->off2, data);
393 if (o->off3 && o->off4) {
394 val_store(ilp, ul1, o->off3, data);
395 val_store(ilp, ul2, o->off4, data);
402 fio_opt_int_fn *fn = o->cb;
404 ret = check_int(ptr, &il);
408 if (o->maxval && il > (int) o->maxval) {
409 fprintf(stderr, "max value out of range: %d (%d max)\n",
413 if (o->minval && il < o->minval) {
414 fprintf(stderr, "min value out of range: %d (%d min)\n",
426 val_store(ilp, il, o->off1, data);
427 if (!more && o->off2)
428 val_store(ilp, il, o->off2, data);
432 case FIO_OPT_STR_SET: {
433 fio_opt_str_set_fn *fn = o->cb;
439 val_store(ilp, 1, o->off1, data);
440 if (!more && o->off2)
441 val_store(ilp, 1, o->off2, data);
445 case FIO_OPT_DEPRECATED:
446 fprintf(stdout, "Option %s is deprecated\n", o->name);
449 fprintf(stderr, "Bad option type %u\n", o->type);
457 ret = o->verify(o, data);
459 fprintf(stderr,"Correct format for offending option\n");
460 fprintf(stderr, "%20s: %s\n", o->name, o->help);
461 show_option_help(o, stderr);
468 static int handle_option(struct fio_option *o, const char *__ptr, void *data)
470 char *ptr, *ptr2 = NULL;
473 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
480 * See if we have a second set of parameters, hidden after a comma.
481 * Do this before parsing the first round, to check if we should
482 * copy set 1 options to set 2.
485 (o->type != FIO_OPT_STR_STORE) &&
486 (o->type != FIO_OPT_STR)) {
487 ptr2 = strchr(ptr, ',');
488 if (ptr2 && *(ptr2 + 1) == '\0')
491 ptr2 = strchr(ptr, ':');
493 ptr2 = strchr(ptr, '-');
497 * Don't return early if parsing the first option fails - if
498 * we are doing multiple arguments, we can allow the first one
501 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
510 r2 = __handle_option(o, ptr2, data, 0, 0);
517 static struct fio_option *get_option(const char *opt,
518 struct fio_option *options, char **post)
520 struct fio_option *o;
523 ret = strchr(opt, '=');
529 strip_blank_end(ret);
530 o = find_option(options, ret);
532 o = find_option(options, opt);
539 static int opt_cmp(const void *p1, const void *p2)
541 struct fio_option *o1, *o2;
545 s1 = strdup(*((char **) p1));
546 s2 = strdup(*((char **) p2));
548 o1 = get_option(s1, fio_options, &foo);
549 o2 = get_option(s2, fio_options, &foo);
559 return prio2 - prio1;
562 void sort_options(char **opts, struct fio_option *options, int num_opts)
564 fio_options = options;
565 qsort(opts, num_opts, sizeof(char *), opt_cmp);
569 int parse_cmd_option(const char *opt, const char *val,
570 struct fio_option *options, void *data)
572 struct fio_option *o;
574 o = find_option(options, opt);
576 fprintf(stderr, "Bad option <%s>\n", opt);
580 if (!handle_option(o, val, data))
583 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
588 * Return a copy of the input string with substrings of the form ${VARNAME}
589 * substituted with the value of the environment variable VARNAME. The
590 * substitution always occurs, even if VARNAME is empty or the corresponding
591 * environment variable undefined.
593 static char *option_dup_subs(const char *opt)
595 char out[OPT_LEN_MAX+1];
596 char in[OPT_LEN_MAX+1];
599 char *ch1, *ch2, *env;
600 ssize_t nchr = OPT_LEN_MAX;
603 in[OPT_LEN_MAX] = '\0';
604 strncpy(in, opt, OPT_LEN_MAX);
606 while (*inptr && nchr > 0) {
607 if (inptr[0] == '$' && inptr[1] == '{') {
608 ch2 = strchr(inptr, '}');
609 if (ch2 && inptr+1 < ch2) {
616 envlen = strlen(env);
617 if (envlen <= nchr) {
618 memcpy(outptr, env, envlen);
628 *outptr++ = *inptr++;
636 int parse_option(const char *opt, struct fio_option *options, void *data)
638 struct fio_option *o;
641 tmp = option_dup_subs(opt);
643 o = get_option(tmp, options, &post);
645 fprintf(stderr, "Bad option <%s>\n", tmp);
650 if (!handle_option(o, post, data)) {
655 fprintf(stderr, "fio: failed parsing %s\n", opt);
661 * Option match, levenshtein distance. Handy for not quite remembering what
662 * the option name is.
664 static int string_distance(const char *s1, const char *s2)
666 unsigned int s1_len = strlen(s1);
667 unsigned int s2_len = strlen(s2);
668 unsigned int *p, *q, *r;
671 p = malloc(sizeof(unsigned int) * (s2_len + 1));
672 q = malloc(sizeof(unsigned int) * (s2_len + 1));
675 for (i = 1; i <= s2_len; i++)
678 for (i = 1; i <= s1_len; i++) {
680 for (j = 1; j <= s2_len; j++) {
681 unsigned int sub = p[j - 1];
683 if (s1[i - 1] != s2[j - 1])
686 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
699 static struct fio_option *find_child(struct fio_option *options,
700 struct fio_option *o)
702 struct fio_option *__o;
704 for (__o = options + 1; __o->name; __o++)
705 if (__o->parent && !strcmp(__o->parent, o->name))
711 static void __print_option(struct fio_option *o, struct fio_option *org,
725 p += sprintf(p, "%s", " ");
727 sprintf(p, "%s", o->name);
729 printf("%-24s: %s\n", name, o->help);
732 static void print_option(struct fio_option *o)
734 struct fio_option *parent;
735 struct fio_option *__o;
736 unsigned int printed;
739 __print_option(o, NULL, 0);
746 while ((__o = find_child(o, parent)) != NULL) {
747 __print_option(__o, o, level);
756 int show_cmd_help(struct fio_option *options, const char *name)
758 struct fio_option *o, *closest;
759 unsigned int best_dist;
763 if (!name || !strcmp(name, "all"))
768 for (o = &options[0]; o->name; o++) {
771 if (o->type == FIO_OPT_DEPRECATED)
775 if (!strcmp(name, o->name) ||
776 (o->alias && !strcmp(name, o->alias)))
781 dist = string_distance(name, o->name);
782 if (dist < best_dist) {
789 if (show_all || match) {
792 printf("%24s: %s\n", o->name, o->help);
803 show_option_help(o, stdout);
809 printf("No such command: %s", name);
811 printf(" - showing closest match\n");
812 printf("%20s: %s\n", closest->name, closest->help);
813 show_option_help(closest, stdout);
821 * Handle parsing of default parameters.
823 void fill_default_options(void *data, struct fio_option *options)
825 struct fio_option *o;
827 dprint(FD_PARSE, "filling default options\n");
829 for (o = &options[0]; o->name; o++)
831 handle_option(o, o->def, data);
835 * Sanitize the options structure. For now it just sets min/max for bool
836 * values and whether both callback and offsets are given.
838 void options_init(struct fio_option *options)
840 struct fio_option *o;
842 dprint(FD_PARSE, "init options\n");
844 for (o = &options[0]; o->name; o++) {
845 if (o->type == FIO_OPT_DEPRECATED)
847 if (o->type == FIO_OPT_BOOL) {
851 if (o->type == FIO_OPT_STR_SET && o->def) {
852 fprintf(stderr, "Option %s: string set option with"
853 " default will always be true\n",
856 if (!o->cb && !o->off1) {
857 fprintf(stderr, "Option %s: neither cb nor offset"
858 " given\n", o->name);
860 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
862 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
863 fprintf(stderr, "Option %s: both cb and offset given\n",