2 * This file contains the ini and command liner parser main.
14 static void show_option_range(struct fio_option *o)
16 if (!o->minval && !o->maxval)
19 printf("%20s: min=%d, max=%d\n", "range", o->minval, o->maxval);
22 static void show_option_values(struct fio_option *o)
27 const struct value_pair *vp = &o->posval[i];
32 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
34 printf(" %s", vp->help);
37 } while (i < PARSE_MAX_VP);
43 static unsigned long get_mult_time(char c)
60 static unsigned long get_mult_bytes(char c)
71 return 1024 * 1024 * 1024;
74 return 1024 * 1024 * 1024 * 1024UL;
81 * convert string into decimal value, noting any size suffix
83 static int str_to_decimal(const char *str, long long *val, int kilo)
91 *val = strtoll(str, NULL, 10);
92 if (*val == LONG_MAX && errno == ERANGE)
96 *val *= get_mult_bytes(str[len - 1]);
98 *val *= get_mult_time(str[len - 1]);
103 static int check_str_bytes(const char *p, long long *val)
105 return str_to_decimal(p, val, 1);
108 static int check_str_time(const char *p, long long *val)
110 return str_to_decimal(p, val, 0);
113 void strip_blank_front(char **p)
121 void strip_blank_end(char *p)
123 char *s = p + strlen(p) - 1;
125 while (isspace(*s) || iscntrl(*s))
131 static int check_range_bytes(const char *str, long *val)
138 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
139 *val *= get_mult_bytes(suffix);
143 if (sscanf(str, "%lu", val) == 1)
149 static int check_int(const char *p, int *val)
153 if (sscanf(p, "%u", val) == 1)
159 static struct fio_option *find_option(struct fio_option *options,
162 struct fio_option *o;
164 for (o = &options[0]; o->name; o++) {
165 if (!strcmp(o->name, opt))
167 else if (o->alias && !strcmp(o->alias, opt))
174 #define val_store(ptr, val, off, data) \
176 ptr = td_var((data), (off)); \
180 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
184 long long ull, *ullp;
187 int ret = 0, is_time = 0;
189 if (!ptr && o->type != FIO_OPT_STR_SET) {
190 fprintf(stderr, "Option %s requires an argument\n", o->name);
196 fio_opt_str_fn *fn = o->cb;
197 const struct value_pair *vp;
200 for (i = 0; i < PARSE_MAX_VP; i++) {
202 if (!vp->ival || vp->ival[0] == '\0')
205 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
209 val_store(ilp, vp->oval, o->off1, data);
215 show_option_values(o);
220 case FIO_OPT_STR_VAL_TIME:
222 case FIO_OPT_STR_VAL:
223 case FIO_OPT_STR_VAL_INT: {
224 fio_opt_str_val_fn *fn = o->cb;
227 ret = check_str_time(ptr, &ull);
229 ret = check_str_bytes(ptr, &ull);
234 if (o->maxval && ull > o->maxval) {
235 fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval);
238 if (o->minval && ull < o->minval) {
239 fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval);
244 ret = fn(data, &ull);
246 if (o->type == FIO_OPT_STR_VAL_INT) {
248 val_store(ilp, ull, o->off1, data);
249 if (!more && o->off2)
250 val_store(ilp, ull, o->off2, data);
253 val_store(ullp, ull, o->off1, data);
254 if (!more && o->off2)
255 val_store(ullp, ull, o->off2, data);
260 case FIO_OPT_STR_STORE: {
261 fio_opt_str_fn *fn = o->cb;
263 cp = td_var(data, o->off1);
274 case FIO_OPT_RANGE: {
278 strncpy(tmp, ptr, sizeof(tmp) - 1);
280 p1 = strchr(tmp, '-');
282 p1 = strchr(tmp, ':');
294 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
297 unsigned long foo = ul1;
304 val_store(ilp, ul1, o->off1, data);
305 val_store(ilp, ul2, o->off2, data);
307 if (!more && o->off3 && o->off4) {
308 val_store(ilp, ul1, o->off3, data);
309 val_store(ilp, ul2, o->off4, data);
317 fio_opt_int_fn *fn = o->cb;
319 ret = check_int(ptr, &il);
323 if (o->maxval && il > (int) o->maxval) {
324 fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval);
327 if (o->minval && il < o->minval) {
328 fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval);
339 val_store(ilp, il, o->off1, data);
340 if (!more && o->off2)
341 val_store(ilp, il, o->off2, data);
345 case FIO_OPT_STR_SET: {
346 fio_opt_str_set_fn *fn = o->cb;
352 val_store(ilp, 1, o->off1, data);
353 if (!more && o->off2)
354 val_store(ilp, 1, o->off2, data);
359 fprintf(stderr, "Bad option type %u\n", o->type);
366 static int handle_option(struct fio_option *o, const char *ptr, void *data)
368 const char *ptr2 = NULL;
372 * See if we have a second set of parameters, hidden after a comma.
373 * Do this before parsing the first round, to check if we should
374 * copy set 1 options to set 2.
377 (o->type != FIO_OPT_STR_STORE) &&
378 (o->type != FIO_OPT_STR)) {
379 ptr2 = strchr(ptr, ',');
381 ptr2 = strchr(ptr, ':');
383 ptr2 = strchr(ptr, '-');
387 * Don't return early if parsing the first option fails - if
388 * we are doing multiple arguments, we can allow the first one
391 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
397 r2 = __handle_option(o, ptr2, data, 0, 0);
402 int parse_cmd_option(const char *opt, const char *val,
403 struct fio_option *options, void *data)
405 struct fio_option *o;
407 o = find_option(options, opt);
409 fprintf(stderr, "Bad option %s\n", opt);
413 if (!handle_option(o, val, data))
416 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
420 int parse_option(const char *opt, struct fio_option *options, void *data)
422 struct fio_option *o;
426 strncpy(tmp, opt, sizeof(tmp) - 1);
428 pre = strchr(tmp, '=');
434 o = find_option(options, pre);
436 o = find_option(options, tmp);
441 fprintf(stderr, "Bad option %s\n", tmp);
445 if (!handle_option(o, post, data))
448 fprintf(stderr, "fio: failed parsing %s\n", opt);
453 * Option match, levenshtein distance. Handy for not quite remembering what
454 * the option name is.
456 static int string_distance(const char *s1, const char *s2)
458 unsigned int s1_len = strlen(s1);
459 unsigned int s2_len = strlen(s2);
460 unsigned int *p, *q, *r;
463 p = malloc(sizeof(unsigned int) * (s2_len + 1));
464 q = malloc(sizeof(unsigned int) * (s2_len + 1));
467 for (i = 1; i <= s2_len; i++)
470 for (i = 1; i <= s1_len; i++) {
472 for (j = 1; j <= s2_len; j++) {
473 unsigned int sub = p[j - 1];
475 if (s1[i - 1] != s2[j - 1])
478 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
491 static void show_option_help(struct fio_option *o)
493 const char *typehelp[] = {
495 "string with possible k/m/g postfix (opt=4k)",
496 "string with range and postfix (opt=1k-4k)",
497 "string with time postfix (opt=10s)",
499 "string with dual range (opt=1k-4k,4k-8k)",
500 "integer value (opt=100)",
501 "boolean value (opt=1)",
505 printf("%20s: %s\n", "type", typehelp[o->type]);
506 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
507 show_option_range(o);
508 show_option_values(o);
511 int show_cmd_help(struct fio_option *options, const char *name)
513 struct fio_option *o, *closest;
514 unsigned int best_dist;
518 if (!name || !strcmp(name, "all"))
523 for (o = &options[0]; o->name; o++) {
527 if (!strcmp(name, o->name))
532 dist = string_distance(name, o->name);
533 if (dist < best_dist) {
540 if (show_all || match) {
543 printf("%20s: %s\n", o->name, o->help);
545 printf("%-20s: %s\n", o->name, o->help);
559 printf("No such command: %s", name);
561 printf(" - showing closest match\n");
562 printf("%20s: %s\n", closest->name, closest->help);
563 show_option_help(closest);
571 * Handle parsing of default parameters.
573 void fill_default_options(void *data, struct fio_option *options)
575 struct fio_option *o;
577 for (o = &options[0]; o->name; o++)
579 handle_option(o, o->def, data);
583 * Sanitize the options structure. For now it just sets min/max for bool
584 * values and whether both callback and offsets are given.
586 void options_init(struct fio_option *options)
588 struct fio_option *o;
590 for (o = &options[0]; o->name; o++) {
591 if (o->type == FIO_OPT_BOOL) {
595 if (!o->cb && !o->off1)
596 fprintf(stderr, "Option %s: neither cb nor offset given\n", o->name);
597 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
599 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
600 fprintf(stderr, "Option %s: both cb and offset given\n", o->name);