2 * This file contains the ini and command liner parser main.
15 static int vp_cmp(const void *p1, const void *p2)
17 const struct value_pair *vp1 = p1;
18 const struct value_pair *vp2 = p2;
20 return strlen(vp2->ival) - strlen(vp1->ival);
23 static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
25 const struct value_pair *vp;
28 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
30 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
31 vp = &o->posval[entries];
32 if (!vp->ival || vp->ival[0] == '\0')
35 memcpy(&vpmap[entries], vp, sizeof(*vp));
38 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
41 static void show_option_range(struct fio_option *o)
43 if (!o->minval && !o->maxval)
46 printf("%20s: min=%d, max=%d\n", "range", o->minval, o->maxval);
49 static void show_option_values(struct fio_option *o)
54 const struct value_pair *vp = &o->posval[i];
59 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
61 printf(" %s", vp->help);
64 } while (i < PARSE_MAX_VP);
70 static unsigned long get_mult_time(char c)
87 static unsigned long get_mult_bytes(char c)
98 return 1024 * 1024 * 1024;
101 return 1024 * 1024 * 1024 * 1024UL;
108 * convert string into decimal value, noting any size suffix
110 int str_to_decimal(const char *str, long long *val, int kilo)
118 *val = strtoll(str, NULL, 10);
119 if (*val == LONG_MAX && errno == ERANGE)
123 *val *= get_mult_bytes(str[len - 1]);
125 *val *= get_mult_time(str[len - 1]);
130 static int check_str_bytes(const char *p, long long *val)
132 return str_to_decimal(p, val, 1);
135 static int check_str_time(const char *p, long long *val)
137 return str_to_decimal(p, val, 0);
140 void strip_blank_front(char **p)
150 void strip_blank_end(char *p)
164 while ((isspace(*s) || iscntrl(*s)) && (s > p))
170 static int check_range_bytes(const char *str, long *val)
177 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
178 *val *= get_mult_bytes(suffix);
182 if (sscanf(str, "%lu", val) == 1)
188 static int check_int(const char *p, int *val)
192 if (strstr(p, "0x") || strstr(p, "0X")) {
193 if (sscanf(p, "%x", val) == 1)
196 if (sscanf(p, "%u", val) == 1)
203 static struct fio_option *find_option(struct fio_option *options,
206 struct fio_option *o;
208 for (o = &options[0]; o->name; o++) {
209 if (!strcmp(o->name, opt))
211 else if (o->alias && !strcmp(o->alias, opt))
218 #define val_store(ptr, val, off, data) \
220 ptr = td_var((data), (off)); \
224 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
228 long long ull, *ullp;
231 int ret = 0, is_time = 0;
233 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
236 if (!ptr && o->type != FIO_OPT_STR_SET) {
237 fprintf(stderr, "Option %s requires an argument\n", o->name);
243 fio_opt_str_fn *fn = o->cb;
244 const struct value_pair *vp;
245 struct value_pair posval[PARSE_MAX_VP];
248 posval_sort(o, posval);
250 for (i = 0; i < PARSE_MAX_VP; i++) {
252 if (!vp->ival || vp->ival[0] == '\0')
255 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
259 val_store(ilp, vp->oval, o->off1, data);
265 show_option_values(o);
270 case FIO_OPT_STR_VAL_TIME:
272 case FIO_OPT_STR_VAL:
273 case FIO_OPT_STR_VAL_INT: {
274 fio_opt_str_val_fn *fn = o->cb;
277 ret = check_str_time(ptr, &ull);
279 ret = check_str_bytes(ptr, &ull);
284 if (o->maxval && ull > o->maxval) {
285 fprintf(stderr, "max value out of range: %lld"
286 " (%d max)\n", ull, o->maxval);
289 if (o->minval && ull < o->minval) {
290 fprintf(stderr, "min value out of range: %lld"
291 " (%d min)\n", ull, o->minval);
296 ret = fn(data, &ull);
298 if (o->type == FIO_OPT_STR_VAL_INT) {
300 val_store(ilp, ull, o->off1, data);
301 if (!more && o->off2)
302 val_store(ilp, ull, o->off2, data);
305 val_store(ullp, ull, o->off1, data);
306 if (!more && o->off2)
307 val_store(ullp, ull, o->off2, data);
312 case FIO_OPT_STR_STORE: {
313 fio_opt_str_fn *fn = o->cb;
315 cp = td_var(data, o->off1);
326 case FIO_OPT_RANGE: {
330 strncpy(tmp, ptr, sizeof(tmp) - 1);
332 p1 = strchr(tmp, '-');
334 p1 = strchr(tmp, ':');
346 if (!check_range_bytes(p1, &ul1) &&
347 !check_range_bytes(p2, &ul2)) {
350 unsigned long foo = ul1;
357 val_store(ilp, ul1, o->off1, data);
358 val_store(ilp, ul2, o->off2, data);
360 if (o->off3 && o->off4) {
361 val_store(ilp, ul1, o->off3, data);
362 val_store(ilp, ul2, o->off4, data);
370 fio_opt_int_fn *fn = o->cb;
372 ret = check_int(ptr, &il);
376 if (o->maxval && il > (int) o->maxval) {
377 fprintf(stderr, "max value out of range: %d (%d max)\n",
381 if (o->minval && il < o->minval) {
382 fprintf(stderr, "min value out of range: %d (%d min)\n",
394 val_store(ilp, il, o->off1, data);
395 if (!more && o->off2)
396 val_store(ilp, il, o->off2, data);
400 case FIO_OPT_STR_SET: {
401 fio_opt_str_set_fn *fn = o->cb;
407 val_store(ilp, 1, o->off1, data);
408 if (!more && o->off2)
409 val_store(ilp, 1, o->off2, data);
413 case FIO_OPT_DEPRECATED:
414 fprintf(stdout, "Option %s is deprecated\n", o->name);
417 fprintf(stderr, "Bad option type %u\n", o->type);
424 static int handle_option(struct fio_option *o, const char *ptr, void *data)
426 const char *ptr2 = NULL;
429 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, ptr);
432 * See if we have a second set of parameters, hidden after a comma.
433 * Do this before parsing the first round, to check if we should
434 * copy set 1 options to set 2.
437 (o->type != FIO_OPT_STR_STORE) &&
438 (o->type != FIO_OPT_STR)) {
439 ptr2 = strchr(ptr, ',');
441 ptr2 = strchr(ptr, ':');
443 ptr2 = strchr(ptr, '-');
447 * Don't return early if parsing the first option fails - if
448 * we are doing multiple arguments, we can allow the first one
451 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
457 r2 = __handle_option(o, ptr2, data, 0, 0);
462 int parse_cmd_option(const char *opt, const char *val,
463 struct fio_option *options, void *data)
465 struct fio_option *o;
467 o = find_option(options, opt);
469 fprintf(stderr, "Bad option %s\n", opt);
473 if (!handle_option(o, val, data))
476 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
480 int parse_option(const char *opt, struct fio_option *options, void *data)
482 struct fio_option *o;
488 pre = strchr(tmp, '=');
494 o = find_option(options, pre);
496 o = find_option(options, tmp);
501 fprintf(stderr, "Bad option %s\n", tmp);
506 if (!handle_option(o, post, data)) {
511 fprintf(stderr, "fio: failed parsing %s\n", opt);
517 * Option match, levenshtein distance. Handy for not quite remembering what
518 * the option name is.
520 static int string_distance(const char *s1, const char *s2)
522 unsigned int s1_len = strlen(s1);
523 unsigned int s2_len = strlen(s2);
524 unsigned int *p, *q, *r;
527 p = malloc(sizeof(unsigned int) * (s2_len + 1));
528 q = malloc(sizeof(unsigned int) * (s2_len + 1));
531 for (i = 1; i <= s2_len; i++)
534 for (i = 1; i <= s1_len; i++) {
536 for (j = 1; j <= s2_len; j++) {
537 unsigned int sub = p[j - 1];
539 if (s1[i - 1] != s2[j - 1])
542 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
555 static void show_option_help(struct fio_option *o)
557 const char *typehelp[] = {
559 "string with possible k/m/g postfix (opt=4k)",
560 "string with range and postfix (opt=1k-4k)",
561 "string with time postfix (opt=10s)",
563 "string with dual range (opt=1k-4k,4k-8k)",
564 "integer value (opt=100)",
565 "boolean value (opt=1)",
570 printf("%20s: %s\n", "alias", o->alias);
572 printf("%20s: %s\n", "type", typehelp[o->type]);
573 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
574 show_option_range(o);
575 show_option_values(o);
578 static struct fio_option *find_child(struct fio_option *options,
579 struct fio_option *o)
581 struct fio_option *__o;
583 for (__o = options + 1; __o->name; __o++)
584 if (__o->parent && !strcmp(__o->parent, o->name))
590 static void __print_option(struct fio_option *o, struct fio_option *org,
604 p += sprintf(p, "%s", " ");
606 sprintf(p, "%s", o->name);
608 printf("%-24s: %s\n", name, o->help);
611 static void print_option(struct fio_option *o)
613 struct fio_option *parent;
614 struct fio_option *__o;
615 unsigned int printed;
618 __print_option(o, NULL, 0);
625 while ((__o = find_child(o, parent)) != NULL) {
626 __print_option(__o, o, level);
635 int show_cmd_help(struct fio_option *options, const char *name)
637 struct fio_option *o, *closest;
638 unsigned int best_dist;
642 if (!name || !strcmp(name, "all"))
647 for (o = &options[0]; o->name; o++) {
650 if (o->type == FIO_OPT_DEPRECATED)
654 if (!strcmp(name, o->name) ||
655 (o->alias && !strcmp(name, o->alias)))
660 dist = string_distance(name, o->name);
661 if (dist < best_dist) {
668 if (show_all || match) {
671 printf("%24s: %s\n", o->name, o->help);
688 printf("No such command: %s", name);
690 printf(" - showing closest match\n");
691 printf("%20s: %s\n", closest->name, closest->help);
692 show_option_help(closest);
700 * Handle parsing of default parameters.
702 void fill_default_options(void *data, struct fio_option *options)
704 struct fio_option *o;
706 dprint(FD_PARSE, "filling default options\n");
708 for (o = &options[0]; o->name; o++)
710 handle_option(o, o->def, data);
714 * Sanitize the options structure. For now it just sets min/max for bool
715 * values and whether both callback and offsets are given.
717 void options_init(struct fio_option *options)
719 struct fio_option *o;
721 dprint(FD_PARSE, "init options\n");
723 for (o = &options[0]; o->name; o++) {
724 if (o->type == FIO_OPT_DEPRECATED)
726 if (o->type == FIO_OPT_BOOL) {
730 if (o->type == FIO_OPT_STR_SET && o->def) {
731 fprintf(stderr, "Option %s: string set option with"
732 " default will always be true\n",
735 if (!o->cb && !o->off1) {
736 fprintf(stderr, "Option %s: neither cb nor offset"
737 " given\n", o->name);
739 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
741 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
742 fprintf(stderr, "Option %s: both cb and offset given\n",