2 * This file contains the ini and command liner parser main.
14 static int vp_cmp(const void *p1, const void *p2)
16 const struct value_pair *vp1 = p1;
17 const struct value_pair *vp2 = p2;
19 return strlen(vp2->ival) - strlen(vp1->ival);
22 static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
24 const struct value_pair *vp;
27 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
29 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
30 vp = &o->posval[entries];
31 if (!vp->ival || vp->ival[0] == '\0')
34 memcpy(&vpmap[entries], vp, sizeof(*vp));
37 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
40 static void show_option_range(struct fio_option *o)
42 if (!o->minval && !o->maxval)
45 printf("%20s: min=%d, max=%d\n", "range", o->minval, o->maxval);
48 static void show_option_values(struct fio_option *o)
53 const struct value_pair *vp = &o->posval[i];
58 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
60 printf(" %s", vp->help);
63 } while (i < PARSE_MAX_VP);
69 static unsigned long get_mult_time(char c)
86 static unsigned long get_mult_bytes(char c)
97 return 1024 * 1024 * 1024;
100 return 1024 * 1024 * 1024 * 1024UL;
107 * convert string into decimal value, noting any size suffix
109 int str_to_decimal(const char *str, long long *val, int kilo)
117 *val = strtoll(str, NULL, 10);
118 if (*val == LONG_MAX && errno == ERANGE)
122 *val *= get_mult_bytes(str[len - 1]);
124 *val *= get_mult_time(str[len - 1]);
129 static int check_str_bytes(const char *p, long long *val)
131 return str_to_decimal(p, val, 1);
134 static int check_str_time(const char *p, long long *val)
136 return str_to_decimal(p, val, 0);
139 void strip_blank_front(char **p)
149 void strip_blank_end(char *p)
163 while ((isspace(*s) || iscntrl(*s)) && (s > p))
169 static int check_range_bytes(const char *str, long *val)
176 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
177 *val *= get_mult_bytes(suffix);
181 if (sscanf(str, "%lu", val) == 1)
187 static int check_int(const char *p, int *val)
191 if (strstr(p, "0x") || strstr(p, "0X")) {
192 if (sscanf(p, "%x", val) == 1)
195 if (sscanf(p, "%u", val) == 1)
202 static struct fio_option *find_option(struct fio_option *options,
205 struct fio_option *o;
207 for (o = &options[0]; o->name; o++) {
208 if (!strcmp(o->name, opt))
210 else if (o->alias && !strcmp(o->alias, opt))
217 #define val_store(ptr, val, off, data) \
219 ptr = td_var((data), (off)); \
223 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
227 long long ull, *ullp;
230 int ret = 0, is_time = 0;
232 if (!ptr && o->type != FIO_OPT_STR_SET) {
233 fprintf(stderr, "Option %s requires an argument\n", o->name);
239 fio_opt_str_fn *fn = o->cb;
240 const struct value_pair *vp;
241 struct value_pair posval[PARSE_MAX_VP];
244 posval_sort(o, posval);
246 for (i = 0; i < PARSE_MAX_VP; i++) {
248 if (!vp->ival || vp->ival[0] == '\0')
251 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
255 val_store(ilp, vp->oval, o->off1, data);
261 show_option_values(o);
266 case FIO_OPT_STR_VAL_TIME:
268 case FIO_OPT_STR_VAL:
269 case FIO_OPT_STR_VAL_INT: {
270 fio_opt_str_val_fn *fn = o->cb;
273 ret = check_str_time(ptr, &ull);
275 ret = check_str_bytes(ptr, &ull);
280 if (o->maxval && ull > o->maxval) {
281 fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval);
284 if (o->minval && ull < o->minval) {
285 fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval);
290 ret = fn(data, &ull);
292 if (o->type == FIO_OPT_STR_VAL_INT) {
294 val_store(ilp, ull, o->off1, data);
295 if (!more && o->off2)
296 val_store(ilp, ull, o->off2, data);
299 val_store(ullp, ull, o->off1, data);
300 if (!more && o->off2)
301 val_store(ullp, ull, o->off2, data);
306 case FIO_OPT_STR_STORE: {
307 fio_opt_str_fn *fn = o->cb;
309 cp = td_var(data, o->off1);
320 case FIO_OPT_RANGE: {
324 strncpy(tmp, ptr, sizeof(tmp) - 1);
326 p1 = strchr(tmp, '-');
328 p1 = strchr(tmp, ':');
340 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
343 unsigned long foo = ul1;
350 val_store(ilp, ul1, o->off1, data);
351 val_store(ilp, ul2, o->off2, data);
353 if (o->off3 && o->off4) {
354 val_store(ilp, ul1, o->off3, data);
355 val_store(ilp, ul2, o->off4, data);
363 fio_opt_int_fn *fn = o->cb;
365 ret = check_int(ptr, &il);
369 if (o->maxval && il > (int) o->maxval) {
370 fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval);
373 if (o->minval && il < o->minval) {
374 fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval);
385 val_store(ilp, il, o->off1, data);
386 if (!more && o->off2)
387 val_store(ilp, il, o->off2, data);
391 case FIO_OPT_STR_SET: {
392 fio_opt_str_set_fn *fn = o->cb;
398 val_store(ilp, 1, o->off1, data);
399 if (!more && o->off2)
400 val_store(ilp, 1, o->off2, data);
405 fprintf(stderr, "Bad option type %u\n", o->type);
412 static int handle_option(struct fio_option *o, const char *ptr, void *data)
414 const char *ptr2 = NULL;
418 * See if we have a second set of parameters, hidden after a comma.
419 * Do this before parsing the first round, to check if we should
420 * copy set 1 options to set 2.
423 (o->type != FIO_OPT_STR_STORE) &&
424 (o->type != FIO_OPT_STR)) {
425 ptr2 = strchr(ptr, ',');
427 ptr2 = strchr(ptr, ':');
429 ptr2 = strchr(ptr, '-');
433 * Don't return early if parsing the first option fails - if
434 * we are doing multiple arguments, we can allow the first one
437 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
443 r2 = __handle_option(o, ptr2, data, 0, 0);
448 int parse_cmd_option(const char *opt, const char *val,
449 struct fio_option *options, void *data)
451 struct fio_option *o;
453 o = find_option(options, opt);
455 fprintf(stderr, "Bad option %s\n", opt);
459 if (!handle_option(o, val, data))
462 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
466 int parse_option(const char *opt, struct fio_option *options, void *data)
468 struct fio_option *o;
474 pre = strchr(tmp, '=');
480 o = find_option(options, pre);
482 o = find_option(options, tmp);
487 fprintf(stderr, "Bad option %s\n", tmp);
492 if (!handle_option(o, post, data)) {
497 fprintf(stderr, "fio: failed parsing %s\n", opt);
503 * Option match, levenshtein distance. Handy for not quite remembering what
504 * the option name is.
506 static int string_distance(const char *s1, const char *s2)
508 unsigned int s1_len = strlen(s1);
509 unsigned int s2_len = strlen(s2);
510 unsigned int *p, *q, *r;
513 p = malloc(sizeof(unsigned int) * (s2_len + 1));
514 q = malloc(sizeof(unsigned int) * (s2_len + 1));
517 for (i = 1; i <= s2_len; i++)
520 for (i = 1; i <= s1_len; i++) {
522 for (j = 1; j <= s2_len; j++) {
523 unsigned int sub = p[j - 1];
525 if (s1[i - 1] != s2[j - 1])
528 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
541 static void show_option_help(struct fio_option *o)
543 const char *typehelp[] = {
545 "string with possible k/m/g postfix (opt=4k)",
546 "string with range and postfix (opt=1k-4k)",
547 "string with time postfix (opt=10s)",
549 "string with dual range (opt=1k-4k,4k-8k)",
550 "integer value (opt=100)",
551 "boolean value (opt=1)",
556 printf("%20s: %s\n", "alias", o->alias);
558 printf("%20s: %s\n", "type", typehelp[o->type]);
559 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
560 show_option_range(o);
561 show_option_values(o);
564 static struct fio_option *find_child(struct fio_option *options,
565 struct fio_option *o)
567 struct fio_option *__o;
569 for (__o = options + 1; __o->name; __o++)
570 if (__o->parent && !strcmp(__o->parent, o->name))
576 static void print_option(struct fio_option *o, struct fio_option *org,
588 p += sprintf(p, "%s", " ");
590 sprintf(p, "%s", o->name);
592 printf("%-24s: %s\n", name, o->help);
593 print_option(find_child(o, org), org, level + 1);
596 int show_cmd_help(struct fio_option *options, const char *name)
598 struct fio_option *o, *closest;
599 unsigned int best_dist;
603 if (!name || !strcmp(name, "all"))
608 for (o = &options[0]; o->name; o++) {
612 if (!strcmp(name, o->name) ||
613 (o->alias && !strcmp(name, o->alias)))
618 dist = string_distance(name, o->name);
619 if (dist < best_dist) {
626 if (show_all || match) {
629 printf("%24s: %s\n", o->name, o->help);
632 print_option(o, NULL, 0);
646 printf("No such command: %s", name);
648 printf(" - showing closest match\n");
649 printf("%20s: %s\n", closest->name, closest->help);
650 show_option_help(closest);
658 * Handle parsing of default parameters.
660 void fill_default_options(void *data, struct fio_option *options)
662 struct fio_option *o;
664 for (o = &options[0]; o->name; o++)
666 handle_option(o, o->def, data);
670 * Sanitize the options structure. For now it just sets min/max for bool
671 * values and whether both callback and offsets are given.
673 void options_init(struct fio_option *options)
675 struct fio_option *o;
677 for (o = &options[0]; o->name; o++) {
678 if (o->type == FIO_OPT_BOOL) {
682 if (o->type == FIO_OPT_STR_SET && o->def)
683 fprintf(stderr, "Option %s: string set option with default will always be true\n", o->name);
684 if (!o->cb && !o->off1)
685 fprintf(stderr, "Option %s: neither cb nor offset given\n", o->name);
686 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
688 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
689 fprintf(stderr, "Option %s: both cb and offset given\n", o->name);