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 static 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)
147 void strip_blank_end(char *p)
149 char *s = p + strlen(p) - 1;
151 while (isspace(*s) || iscntrl(*s))
157 static int check_range_bytes(const char *str, long *val)
164 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
165 *val *= get_mult_bytes(suffix);
169 if (sscanf(str, "%lu", val) == 1)
175 static int check_int(const char *p, int *val)
179 if (sscanf(p, "%u", val) == 1)
185 static struct fio_option *find_option(struct fio_option *options,
188 struct fio_option *o;
190 for (o = &options[0]; o->name; o++) {
191 if (!strcmp(o->name, opt))
193 else if (o->alias && !strcmp(o->alias, opt))
200 #define val_store(ptr, val, off, data) \
202 ptr = td_var((data), (off)); \
206 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
210 long long ull, *ullp;
213 int ret = 0, is_time = 0;
215 if (!ptr && o->type != FIO_OPT_STR_SET) {
216 fprintf(stderr, "Option %s requires an argument\n", o->name);
222 fio_opt_str_fn *fn = o->cb;
223 const struct value_pair *vp;
224 struct value_pair posval[PARSE_MAX_VP];
227 posval_sort(o, posval);
229 for (i = 0; i < PARSE_MAX_VP; i++) {
231 if (!vp->ival || vp->ival[0] == '\0')
234 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
238 val_store(ilp, vp->oval, o->off1, data);
244 show_option_values(o);
249 case FIO_OPT_STR_VAL_TIME:
251 case FIO_OPT_STR_VAL:
252 case FIO_OPT_STR_VAL_INT: {
253 fio_opt_str_val_fn *fn = o->cb;
256 ret = check_str_time(ptr, &ull);
258 ret = check_str_bytes(ptr, &ull);
263 if (o->maxval && ull > o->maxval) {
264 fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval);
267 if (o->minval && ull < o->minval) {
268 fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval);
273 ret = fn(data, &ull);
275 if (o->type == FIO_OPT_STR_VAL_INT) {
277 val_store(ilp, ull, o->off1, data);
278 if (!more && o->off2)
279 val_store(ilp, ull, o->off2, data);
282 val_store(ullp, ull, o->off1, data);
283 if (!more && o->off2)
284 val_store(ullp, ull, o->off2, data);
289 case FIO_OPT_STR_STORE: {
290 fio_opt_str_fn *fn = o->cb;
292 cp = td_var(data, o->off1);
303 case FIO_OPT_RANGE: {
307 strncpy(tmp, ptr, sizeof(tmp) - 1);
309 p1 = strchr(tmp, '-');
311 p1 = strchr(tmp, ':');
323 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
326 unsigned long foo = ul1;
333 val_store(ilp, ul1, o->off1, data);
334 val_store(ilp, ul2, o->off2, data);
336 if (!more && o->off3 && o->off4) {
337 val_store(ilp, ul1, o->off3, data);
338 val_store(ilp, ul2, o->off4, data);
346 fio_opt_int_fn *fn = o->cb;
348 ret = check_int(ptr, &il);
352 if (o->maxval && il > (int) o->maxval) {
353 fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval);
356 if (o->minval && il < o->minval) {
357 fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval);
368 val_store(ilp, il, o->off1, data);
369 if (!more && o->off2)
370 val_store(ilp, il, o->off2, data);
374 case FIO_OPT_STR_SET: {
375 fio_opt_str_set_fn *fn = o->cb;
381 val_store(ilp, 1, o->off1, data);
382 if (!more && o->off2)
383 val_store(ilp, 1, o->off2, data);
388 fprintf(stderr, "Bad option type %u\n", o->type);
395 static int handle_option(struct fio_option *o, const char *ptr, void *data)
397 const char *ptr2 = NULL;
401 * See if we have a second set of parameters, hidden after a comma.
402 * Do this before parsing the first round, to check if we should
403 * copy set 1 options to set 2.
406 (o->type != FIO_OPT_STR_STORE) &&
407 (o->type != FIO_OPT_STR)) {
408 ptr2 = strchr(ptr, ',');
410 ptr2 = strchr(ptr, ':');
412 ptr2 = strchr(ptr, '-');
416 * Don't return early if parsing the first option fails - if
417 * we are doing multiple arguments, we can allow the first one
420 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
426 r2 = __handle_option(o, ptr2, data, 0, 0);
431 int parse_cmd_option(const char *opt, const char *val,
432 struct fio_option *options, void *data)
434 struct fio_option *o;
436 o = find_option(options, opt);
438 fprintf(stderr, "Bad option %s\n", opt);
442 if (!handle_option(o, val, data))
445 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
449 int parse_option(const char *opt, struct fio_option *options, void *data)
451 struct fio_option *o;
455 strncpy(tmp, opt, sizeof(tmp) - 1);
457 pre = strchr(tmp, '=');
463 o = find_option(options, pre);
465 o = find_option(options, tmp);
470 fprintf(stderr, "Bad option %s\n", tmp);
474 if (!handle_option(o, post, data))
477 fprintf(stderr, "fio: failed parsing %s\n", opt);
482 * Option match, levenshtein distance. Handy for not quite remembering what
483 * the option name is.
485 static int string_distance(const char *s1, const char *s2)
487 unsigned int s1_len = strlen(s1);
488 unsigned int s2_len = strlen(s2);
489 unsigned int *p, *q, *r;
492 p = malloc(sizeof(unsigned int) * (s2_len + 1));
493 q = malloc(sizeof(unsigned int) * (s2_len + 1));
496 for (i = 1; i <= s2_len; i++)
499 for (i = 1; i <= s1_len; i++) {
501 for (j = 1; j <= s2_len; j++) {
502 unsigned int sub = p[j - 1];
504 if (s1[i - 1] != s2[j - 1])
507 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
520 static void show_option_help(struct fio_option *o)
522 const char *typehelp[] = {
524 "string with possible k/m/g postfix (opt=4k)",
525 "string with range and postfix (opt=1k-4k)",
526 "string with time postfix (opt=10s)",
528 "string with dual range (opt=1k-4k,4k-8k)",
529 "integer value (opt=100)",
530 "boolean value (opt=1)",
535 printf("%20s: %s\n", "alias", o->alias);
537 printf("%20s: %s\n", "type", typehelp[o->type]);
538 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
539 show_option_range(o);
540 show_option_values(o);
543 int show_cmd_help(struct fio_option *options, const char *name)
545 struct fio_option *o, *closest;
546 unsigned int best_dist;
550 if (!name || !strcmp(name, "all"))
555 for (o = &options[0]; o->name; o++) {
559 if (!strcmp(name, o->name) ||
560 (o->alias && !strcmp(name, o->alias)))
565 dist = string_distance(name, o->name);
566 if (dist < best_dist) {
573 if (show_all || match) {
576 printf("%20s: %s\n", o->name, o->help);
578 printf("%-20s: %s\n", o->name, o->help);
592 printf("No such command: %s", name);
594 printf(" - showing closest match\n");
595 printf("%20s: %s\n", closest->name, closest->help);
596 show_option_help(closest);
604 * Handle parsing of default parameters.
606 void fill_default_options(void *data, struct fio_option *options)
608 struct fio_option *o;
610 for (o = &options[0]; o->name; o++)
612 handle_option(o, o->def, data);
616 * Sanitize the options structure. For now it just sets min/max for bool
617 * values and whether both callback and offsets are given.
619 void options_init(struct fio_option *options)
621 struct fio_option *o;
623 for (o = &options[0]; o->name; o++) {
624 if (o->type == FIO_OPT_BOOL) {
628 if (o->type == FIO_OPT_STR_SET && o->def)
629 fprintf(stderr, "Option %s: string set option with default will always be true\n", o->name);
630 if (!o->cb && !o->off1)
631 fprintf(stderr, "Option %s: neither cb nor offset given\n", o->name);
632 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
634 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
635 fprintf(stderr, "Option %s: both cb and offset given\n", o->name);