2 * This file contains the ini and command liner parser main.
15 static struct fio_option *fio_options;
17 static int vp_cmp(const void *p1, const void *p2)
19 const struct value_pair *vp1 = p1;
20 const struct value_pair *vp2 = p2;
22 return strlen(vp2->ival) - strlen(vp1->ival);
25 static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
27 const struct value_pair *vp;
30 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
32 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
33 vp = &o->posval[entries];
34 if (!vp->ival || vp->ival[0] == '\0')
37 memcpy(&vpmap[entries], vp, sizeof(*vp));
40 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
43 static void show_option_range(struct fio_option *o)
45 if (!o->minval && !o->maxval)
48 printf("%20s: min=%d, max=%d\n", "range", o->minval, o->maxval);
51 static void show_option_values(struct fio_option *o)
56 const struct value_pair *vp = &o->posval[i];
61 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
63 printf(" %s", vp->help);
66 } while (i < PARSE_MAX_VP);
72 static unsigned long get_mult_time(char c)
89 static unsigned long get_mult_bytes(char c)
100 return 1024 * 1024 * 1024;
103 return 1024 * 1024 * 1024 * 1024UL;
110 * convert string into decimal value, noting any size suffix
112 int str_to_decimal(const char *str, long long *val, int kilo)
120 *val = strtoll(str, NULL, 10);
121 if (*val == LONG_MAX && errno == ERANGE)
125 *val *= get_mult_bytes(str[len - 1]);
127 *val *= get_mult_time(str[len - 1]);
132 static int check_str_bytes(const char *p, long long *val)
134 return str_to_decimal(p, val, 1);
137 static int check_str_time(const char *p, long long *val)
139 return str_to_decimal(p, val, 0);
142 void strip_blank_front(char **p)
152 void strip_blank_end(char *p)
166 while ((isspace(*s) || iscntrl(*s)) && (s > p))
172 static int check_range_bytes(const char *str, long *val)
179 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
180 *val *= get_mult_bytes(suffix);
184 if (sscanf(str, "%lu", val) == 1)
190 static int check_int(const char *p, int *val)
194 if (strstr(p, "0x") || strstr(p, "0X")) {
195 if (sscanf(p, "%x", val) == 1)
198 if (sscanf(p, "%u", val) == 1)
205 static struct fio_option *find_option(struct fio_option *options,
208 struct fio_option *o;
210 for (o = &options[0]; o->name; o++) {
211 if (!strcmp(o->name, opt))
213 else if (o->alias && !strcmp(o->alias, opt))
220 #define val_store(ptr, val, off, data) \
222 ptr = td_var((data), (off)); \
226 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
230 long long ull, *ullp;
233 int ret = 0, is_time = 0;
235 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
238 if (!ptr && o->type != FIO_OPT_STR_SET) {
239 fprintf(stderr, "Option %s requires an argument\n", o->name);
245 fio_opt_str_fn *fn = o->cb;
246 const struct value_pair *vp;
247 struct value_pair posval[PARSE_MAX_VP];
250 posval_sort(o, posval);
252 for (i = 0; i < PARSE_MAX_VP; i++) {
254 if (!vp->ival || vp->ival[0] == '\0')
257 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
261 val_store(ilp, vp->oval, o->off1, data);
267 show_option_values(o);
272 case FIO_OPT_STR_VAL_TIME:
274 case FIO_OPT_STR_VAL:
275 case FIO_OPT_STR_VAL_INT: {
276 fio_opt_str_val_fn *fn = o->cb;
279 ret = check_str_time(ptr, &ull);
281 ret = check_str_bytes(ptr, &ull);
286 if (o->maxval && ull > o->maxval) {
287 fprintf(stderr, "max value out of range: %lld"
288 " (%d max)\n", ull, o->maxval);
291 if (o->minval && ull < o->minval) {
292 fprintf(stderr, "min value out of range: %lld"
293 " (%d min)\n", ull, o->minval);
298 ret = fn(data, &ull);
300 if (o->type == FIO_OPT_STR_VAL_INT) {
302 val_store(ilp, ull, o->off1, data);
303 if (!more && o->off2)
304 val_store(ilp, ull, o->off2, data);
307 val_store(ullp, ull, o->off1, data);
308 if (!more && o->off2)
309 val_store(ullp, ull, o->off2, data);
314 case FIO_OPT_STR_STORE: {
315 fio_opt_str_fn *fn = o->cb;
317 cp = td_var(data, o->off1);
328 case FIO_OPT_RANGE: {
332 strncpy(tmp, ptr, sizeof(tmp) - 1);
334 p1 = strchr(tmp, '-');
336 p1 = strchr(tmp, ':');
348 if (!check_range_bytes(p1, &ul1) &&
349 !check_range_bytes(p2, &ul2)) {
352 unsigned long foo = ul1;
359 val_store(ilp, ul1, o->off1, data);
360 val_store(ilp, ul2, o->off2, data);
362 if (o->off3 && o->off4) {
363 val_store(ilp, ul1, o->off3, data);
364 val_store(ilp, ul2, o->off4, data);
372 fio_opt_int_fn *fn = o->cb;
374 ret = check_int(ptr, &il);
378 if (o->maxval && il > (int) o->maxval) {
379 fprintf(stderr, "max value out of range: %d (%d max)\n",
383 if (o->minval && il < o->minval) {
384 fprintf(stderr, "min value out of range: %d (%d min)\n",
396 val_store(ilp, il, o->off1, data);
397 if (!more && o->off2)
398 val_store(ilp, il, o->off2, data);
402 case FIO_OPT_STR_SET: {
403 fio_opt_str_set_fn *fn = o->cb;
409 val_store(ilp, 1, o->off1, data);
410 if (!more && o->off2)
411 val_store(ilp, 1, o->off2, data);
415 case FIO_OPT_DEPRECATED:
416 fprintf(stdout, "Option %s is deprecated\n", o->name);
419 fprintf(stderr, "Bad option type %u\n", o->type);
426 static int handle_option(struct fio_option *o, const char *ptr, void *data)
428 const char *ptr2 = NULL;
431 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, ptr);
434 * See if we have a second set of parameters, hidden after a comma.
435 * Do this before parsing the first round, to check if we should
436 * copy set 1 options to set 2.
439 (o->type != FIO_OPT_STR_STORE) &&
440 (o->type != FIO_OPT_STR)) {
441 ptr2 = strchr(ptr, ',');
443 ptr2 = strchr(ptr, ':');
445 ptr2 = strchr(ptr, '-');
449 * Don't return early if parsing the first option fails - if
450 * we are doing multiple arguments, we can allow the first one
453 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
459 r2 = __handle_option(o, ptr2, data, 0, 0);
464 static struct fio_option *get_option(const char *opt,
465 struct fio_option *options, char **post)
467 struct fio_option *o;
470 ret = strchr(opt, '=');
476 o = find_option(options, ret);
478 o = find_option(options, opt);
485 static int opt_cmp(const void *p1, const void *p2)
487 struct fio_option *o1, *o2;
491 s1 = strdup(*((char **) p1));
492 s2 = strdup(*((char **) p2));
494 o1 = get_option(s1, fio_options, &foo);
495 o2 = get_option(s2, fio_options, &foo);
497 if ((!o1 && o2) || (o1 && !o2))
500 ret = o2->prio - o1->prio;
507 void sort_options(char **opts, struct fio_option *options, int num_opts)
509 fio_options = options;
510 qsort(opts, num_opts, sizeof(char *), opt_cmp);
514 int parse_cmd_option(const char *opt, const char *val,
515 struct fio_option *options, void *data)
517 struct fio_option *o;
519 o = find_option(options, opt);
521 fprintf(stderr, "Bad option %s\n", opt);
525 if (!handle_option(o, val, data))
528 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
532 int parse_option(const char *opt, struct fio_option *options, void *data)
534 struct fio_option *o;
539 o = get_option(tmp, options, &post);
541 fprintf(stderr, "Bad option %s\n", tmp);
546 if (!handle_option(o, post, data)) {
551 fprintf(stderr, "fio: failed parsing %s\n", opt);
557 * Option match, levenshtein distance. Handy for not quite remembering what
558 * the option name is.
560 static int string_distance(const char *s1, const char *s2)
562 unsigned int s1_len = strlen(s1);
563 unsigned int s2_len = strlen(s2);
564 unsigned int *p, *q, *r;
567 p = malloc(sizeof(unsigned int) * (s2_len + 1));
568 q = malloc(sizeof(unsigned int) * (s2_len + 1));
571 for (i = 1; i <= s2_len; i++)
574 for (i = 1; i <= s1_len; i++) {
576 for (j = 1; j <= s2_len; j++) {
577 unsigned int sub = p[j - 1];
579 if (s1[i - 1] != s2[j - 1])
582 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
595 static void show_option_help(struct fio_option *o)
597 const char *typehelp[] = {
599 "string with possible k/m/g postfix (opt=4k)",
600 "string with range and postfix (opt=1k-4k)",
601 "string with time postfix (opt=10s)",
603 "string with dual range (opt=1k-4k,4k-8k)",
604 "integer value (opt=100)",
605 "boolean value (opt=1)",
610 printf("%20s: %s\n", "alias", o->alias);
612 printf("%20s: %s\n", "type", typehelp[o->type]);
613 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
614 show_option_range(o);
615 show_option_values(o);
618 static struct fio_option *find_child(struct fio_option *options,
619 struct fio_option *o)
621 struct fio_option *__o;
623 for (__o = options + 1; __o->name; __o++)
624 if (__o->parent && !strcmp(__o->parent, o->name))
630 static void __print_option(struct fio_option *o, struct fio_option *org,
644 p += sprintf(p, "%s", " ");
646 sprintf(p, "%s", o->name);
648 printf("%-24s: %s\n", name, o->help);
651 static void print_option(struct fio_option *o)
653 struct fio_option *parent;
654 struct fio_option *__o;
655 unsigned int printed;
658 __print_option(o, NULL, 0);
665 while ((__o = find_child(o, parent)) != NULL) {
666 __print_option(__o, o, level);
675 int show_cmd_help(struct fio_option *options, const char *name)
677 struct fio_option *o, *closest;
678 unsigned int best_dist;
682 if (!name || !strcmp(name, "all"))
687 for (o = &options[0]; o->name; o++) {
690 if (o->type == FIO_OPT_DEPRECATED)
694 if (!strcmp(name, o->name) ||
695 (o->alias && !strcmp(name, o->alias)))
700 dist = string_distance(name, o->name);
701 if (dist < best_dist) {
708 if (show_all || match) {
711 printf("%24s: %s\n", o->name, o->help);
728 printf("No such command: %s", name);
730 printf(" - showing closest match\n");
731 printf("%20s: %s\n", closest->name, closest->help);
732 show_option_help(closest);
740 * Handle parsing of default parameters.
742 void fill_default_options(void *data, struct fio_option *options)
744 struct fio_option *o;
746 dprint(FD_PARSE, "filling default options\n");
748 for (o = &options[0]; o->name; o++)
750 handle_option(o, o->def, data);
754 * Sanitize the options structure. For now it just sets min/max for bool
755 * values and whether both callback and offsets are given.
757 void options_init(struct fio_option *options)
759 struct fio_option *o;
761 dprint(FD_PARSE, "init options\n");
763 for (o = &options[0]; o->name; o++) {
764 if (o->type == FIO_OPT_DEPRECATED)
766 if (o->type == FIO_OPT_BOOL) {
770 if (o->type == FIO_OPT_STR_SET && o->def) {
771 fprintf(stderr, "Option %s: string set option with"
772 " default will always be true\n",
775 if (!o->cb && !o->off1) {
776 fprintf(stderr, "Option %s: neither cb nor offset"
777 " given\n", o->name);
779 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
781 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
782 fprintf(stderr, "Option %s: both cb and offset given\n",