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)
28 msg = o->posval[i].ival;
33 printf("%20s: ", "valid values");
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;
201 for (i = 0; i < PARSE_MAX_VP; i++) {
203 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 cp = td_var(data, o->off1);
264 case FIO_OPT_RANGE: {
268 strncpy(tmp, ptr, sizeof(tmp) - 1);
270 p1 = strchr(tmp, '-');
272 p1 = strchr(tmp, ':');
284 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
287 unsigned long foo = ul1;
294 val_store(ilp, ul1, o->off1, data);
295 val_store(ilp, ul2, o->off2, data);
297 if (!more && o->off3 && o->off4) {
298 val_store(ilp, ul1, o->off3, data);
299 val_store(ilp, ul2, o->off4, data);
307 fio_opt_int_fn *fn = o->cb;
309 ret = check_int(ptr, &il);
313 if (o->maxval && il > (int) o->maxval) {
314 fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval);
317 if (o->minval && il < o->minval) {
318 fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval);
329 val_store(ilp, il, o->off1, data);
330 if (!more && o->off2)
331 val_store(ilp, il, o->off2, data);
335 case FIO_OPT_STR_SET: {
336 fio_opt_str_set_fn *fn = o->cb;
342 val_store(ilp, 1, o->off1, data);
343 if (!more && o->off2)
344 val_store(ilp, 1, o->off2, data);
349 fprintf(stderr, "Bad option type %u\n", o->type);
356 static int handle_option(struct fio_option *o, const char *ptr, void *data)
358 const char *ptr2 = NULL;
362 * See if we have a second set of parameters, hidden after a comma.
363 * Do this before parsing the first round, to check if we should
364 * copy set 1 options to set 2.
367 (o->type != FIO_OPT_STR_STORE) &&
368 (o->type != FIO_OPT_STR)) {
369 ptr2 = strchr(ptr, ',');
371 ptr2 = strchr(ptr, ':');
375 * Don't return early if parsing the first option fails - if
376 * we are doing multiple arguments, we can allow the first one
379 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
385 r2 = __handle_option(o, ptr2, data, 0, 0);
390 int parse_cmd_option(const char *opt, const char *val,
391 struct fio_option *options, void *data)
393 struct fio_option *o;
395 o = find_option(options, opt);
397 fprintf(stderr, "Bad option %s\n", opt);
401 if (!handle_option(o, val, data))
404 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
408 int parse_option(const char *opt, struct fio_option *options, void *data)
410 struct fio_option *o;
414 strncpy(tmp, opt, sizeof(tmp) - 1);
416 pre = strchr(tmp, '=');
422 o = find_option(options, pre);
424 o = find_option(options, tmp);
429 fprintf(stderr, "Bad option %s\n", tmp);
433 if (!handle_option(o, post, data))
436 fprintf(stderr, "fio: failed parsing %s\n", opt);
441 * Option match, levenshtein distance. Handy for not quite remembering what
442 * the option name is.
444 static int string_distance(const char *s1, const char *s2)
446 unsigned int s1_len = strlen(s1);
447 unsigned int s2_len = strlen(s2);
448 unsigned int *p, *q, *r;
451 p = malloc(sizeof(unsigned int) * (s2_len + 1));
452 q = malloc(sizeof(unsigned int) * (s2_len + 1));
455 for (i = 1; i <= s2_len; i++)
458 for (i = 1; i <= s1_len; i++) {
460 for (j = 1; j <= s2_len; j++) {
461 unsigned int sub = p[j - 1];
463 if (s1[i - 1] != s2[j - 1])
466 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
479 static void show_option_help(struct fio_option *o)
481 const char *typehelp[] = {
483 "string with possible k/m/g postfix (opt=4k)",
484 "string with range and postfix (opt=1k-4k)",
485 "string with time postfix (opt=10s)",
487 "string with dual range (opt=1k-4k,4k-8k)",
488 "integer value (opt=100)",
489 "boolean value (opt=1)",
493 printf("%20s: %s\n", "type", typehelp[o->type]);
494 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
495 show_option_range(o);
496 show_option_values(o);
499 int show_cmd_help(struct fio_option *options, const char *name)
501 struct fio_option *o, *closest;
502 unsigned int best_dist;
506 if (!name || !strcmp(name, "all"))
511 for (o = &options[0]; o->name; o++) {
515 if (!strcmp(name, o->name))
520 dist = string_distance(name, o->name);
521 if (dist < best_dist) {
528 if (show_all || match) {
530 printf("%20s: %s\n", o->name, o->help);
544 printf("No such command: %s", name);
546 printf(" - showing closest match\n");
547 printf("%20s: %s\n", closest->name, closest->help);
548 show_option_help(closest);
556 * Handle parsing of default parameters.
558 void fill_default_options(void *data, struct fio_option *options)
560 struct fio_option *o;
562 for (o = &options[0]; o->name; o++)
564 handle_option(o, o->def, data);
568 * Sanitize the options structure. For now it just sets min/max for bool
569 * values and whether both callback and offsets are given.
571 void options_init(struct fio_option *options)
573 struct fio_option *o;
575 for (o = &options[0]; o->name; o++) {
576 if (o->type == FIO_OPT_BOOL) {
580 if (!o->cb && !o->off1)
581 fprintf(stderr, "Option %s: neither cb nor offset given\n", o->name);
582 if (o->type == FIO_OPT_STR)
584 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
585 fprintf(stderr, "Option %s: both cb and offset given\n", o->name);