X-Git-Url: https://git.kernel.dk/?p=fio.git;a=blobdiff_plain;f=parse.c;h=e9eb73852dbccbeb630b415db7220678054553ad;hp=cbe8e35f2bd7e9fc5c5785c5d2afe77c95f341ca;hb=1a1137d9ba2603e295aaac579777ab0d3524faa6;hpb=b62bdf2c3e8877c276796d1ed7909df194fc846c diff --git a/parse.c b/parse.c index cbe8e35f..e9eb7385 100644 --- a/parse.c +++ b/parse.c @@ -79,6 +79,7 @@ static void show_option_help(struct fio_option *o, FILE *out) const char *typehelp[] = { "invalid", "string (opt=bla)", + "string (opt=bla)", "string with possible k/m/g postfix (opt=4k)", "string with time postfix (opt=10s)", "string (opt=bla)", @@ -117,35 +118,75 @@ static unsigned long get_mult_time(char c) } } -static unsigned long long get_mult_bytes(char c, void *data) +static unsigned long long __get_mult_bytes(const char *p, void *data) { unsigned int kb_base = fio_get_kb_base(data); unsigned long long ret = 1; + unsigned int i, pow = 0, mult = kb_base; + char *c; - switch (c) { - default: - break; - case 'p': - case 'P': - ret *= (unsigned long long) kb_base; - case 't': - case 'T': - ret *= (unsigned long long) kb_base; - case 'g': - case 'G': - ret *= (unsigned long long) kb_base; - case 'm': - case 'M': - ret *= (unsigned long long) kb_base; - case 'k': - case 'K': - ret *= (unsigned long long) kb_base; - break; - } + if (!p) + return 1; + c = strdup(p); + + for (i = 0; i < strlen(c); i++) + c[i] = tolower(c[i]); + + if (!strcmp("pib", c)) { + pow = 5; + mult = 1000; + } else if (!strcmp("tib", c)) { + pow = 4; + mult = 1000; + } else if (!strcmp("gib", c)) { + pow = 3; + mult = 1000; + } else if (!strcmp("mib", c)) { + pow = 2; + mult = 1000; + } else if (!strcmp("kib", c)) { + pow = 1; + mult = 1000; + } else if (!strcmp("p", c) || !strcmp("pb", c)) + pow = 5; + else if (!strcmp("t", c) || !strcmp("tb", c)) + pow = 4; + else if (!strcmp("g", c) || !strcmp("gb", c)) + pow = 3; + else if (!strcmp("m", c) || !strcmp("mb", c)) + pow = 2; + else if (!strcmp("k", c) || !strcmp("kb", c)) + pow = 1; + + while (pow--) + ret *= (unsigned long long) mult; + + free(c); return ret; } +static unsigned long long get_mult_bytes(const char *str, int len, void *data) +{ + const char *p; + + if (len < 2) + return __get_mult_bytes(str, data); + + /* + * if the last char is 'b' or 'B', the user likely used + * "1gb" instead of just "1g". If the second to last is also + * a letter, adjust. + */ + p = str + len - 1; + while (isalpha(*(p - 1))) + p--; + if (!isalpha(*p)) + p = NULL; + + return __get_mult_bytes(p, data); +} + /* * convert string into decimal value, noting any size suffix */ @@ -166,19 +207,9 @@ int str_to_decimal(const char *str, long long *val, int kilo, void *data) if (*val == LONG_MAX && errno == ERANGE) return 1; - if (kilo) { - const char *p; - /* - * if the last char is 'b' or 'B', the user likely used - * "1gb" instead of just "1g". If the second to last is also - * a letter, adjust. - */ - p = str + len - 1; - if ((*p == 'b' || *p == 'B') && isalpha(*(p - 1))) - --p; - - *val *= get_mult_bytes(*p, data); - } else + if (kilo) + *val *= get_mult_bytes(str, len, data); + else *val *= get_mult_time(str[len - 1]); return 0; @@ -226,19 +257,13 @@ void strip_blank_end(char *p) static int check_range_bytes(const char *str, long *val, void *data) { - char suffix; + long long __val; - if (!strlen(str)) - return 1; - - if (sscanf(str, "%lu%c", val, &suffix) == 2) { - *val *= get_mult_bytes(suffix, data); + if (!str_to_decimal(str, &__val, 1, data)) { + *val = __val; return 0; } - if (sscanf(str, "%lu", val) == 1) - return 0; - return 1; } @@ -257,6 +282,17 @@ static int check_int(const char *p, int *val) return 1; } +static int opt_len(const char *str) +{ + char *postfix; + + postfix = strchr(str, ':'); + if (!postfix) + return strlen(str); + + return (int)(postfix - str); +} + #define val_store(ptr, val, off, or, data) \ do { \ ptr = td_var((data), (off)); \ @@ -273,7 +309,7 @@ static int __handle_option(struct fio_option *o, const char *ptr, void *data, long long ull, *ullp; long ul1, ul2; char **cp; - int ret = 0, is_time = 0; + int ret = 0; dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name, o->type, ptr); @@ -289,7 +325,7 @@ static int __handle_option(struct fio_option *o, const char *ptr, void *data, fio_opt_str_fn *fn = o->cb; const struct value_pair *vp; struct value_pair posval[PARSE_MAX_VP]; - int i; + int i, all_skipped = 1; posval_sort(o, posval); @@ -298,7 +334,8 @@ static int __handle_option(struct fio_option *o, const char *ptr, void *data, vp = &posval[i]; if (!vp->ival || vp->ival[0] == '\0') continue; - if (!strncmp(vp->ival, ptr, strlen(vp->ival))) { + all_skipped = 0; + if (!strncmp(vp->ival, ptr, opt_len(ptr))) { ret = 0; if (o->roff1) { if (vp->or) @@ -314,23 +351,20 @@ static int __handle_option(struct fio_option *o, const char *ptr, void *data, } } - if (ret) + if (ret && !all_skipped) show_option_values(o); else if (fn) ret = fn(data, ptr); break; } - case FIO_OPT_STR_VAL_TIME: - is_time = 1; - case FIO_OPT_INT: - case FIO_OPT_STR_VAL: { - fio_opt_str_val_fn *fn = o->cb; + case FIO_OPT_STR_VAL_TIME: { + fio_opt_str_val_fn *fn; - if (is_time) - ret = check_str_time(ptr, &ull); - else - ret = check_str_bytes(ptr, &ull, data); + ret = check_str_time(ptr, &ull); + case FIO_OPT_INT: + case FIO_OPT_STR_VAL: + ret = check_str_bytes(ptr, &ull, data); if (ret) break; @@ -345,19 +379,20 @@ static int __handle_option(struct fio_option *o, const char *ptr, void *data, return 1; } + fn = o->cb; if (fn) ret = fn(data, &ull); else { if (o->type == FIO_OPT_INT) { if (first) { if (o->roff1) - *(unsigned long long *) o->roff1 = ull; + *(unsigned int *) o->roff1 = ull; else val_store(ilp, ull, o->off1, 0, data); } if (!more) { if (o->roff2) - *(unsigned long long *) o->roff2 = ull; + *(unsigned int *) o->roff2 = ull; else if (o->off2) val_store(ilp, ull, o->off2, 0, data); } @@ -428,17 +463,17 @@ static int __handle_option(struct fio_option *o, const char *ptr, void *data, if (first) { if (o->roff1) - *(unsigned long *) o->roff1 = ul1; + *(unsigned int *) o->roff1 = ul1; else val_store(ilp, ul1, o->off1, 0, data); if (o->roff2) - *(unsigned long *) o->roff2 = ul2; + *(unsigned int *) o->roff2 = ul2; else val_store(ilp, ul2, o->off2, 0, data); } if (o->roff3 && o->roff4) { - *(unsigned long *) o->roff3 = ul1; - *(unsigned long *) o->roff4 = ul2; + *(unsigned int *) o->roff3 = ul1; + *(unsigned int *) o->roff4 = ul2; } else if (o->off3 && o->off4) { val_store(ilp, ul1, o->off3, 0, data); val_store(ilp, ul2, o->off4, 0, data); @@ -836,7 +871,7 @@ static void print_option(struct fio_option *o) int show_cmd_help(struct fio_option *options, const char *name) { struct fio_option *o, *closest; - unsigned int best_dist; + unsigned int best_dist = -1U; int found = 0; int show_all = 0; @@ -889,7 +924,12 @@ int show_cmd_help(struct fio_option *options, const char *name) return 0; printf("No such command: %s", name); - if (closest) { + + /* + * Only print an appropriately close option, one where the edit + * distance isn't too big. Otherwise we get crazy matches. + */ + if (closest && best_dist < 3) { printf(" - showing closest match\n"); printf("%20s: %s\n", closest->name, closest->help); show_option_help(closest, stdout);