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.
366 if (ptr && (o->type != FIO_OPT_STR_STORE)) {
367 ptr2 = strchr(ptr, ',');
369 ptr2 = strchr(ptr, ':');
373 * Don't return early if parsing the first option fails - if
374 * we are doing multiple arguments, we can allow the first one
377 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
383 r2 = __handle_option(o, ptr2, data, 0, 0);
388 int parse_cmd_option(const char *opt, const char *val,
389 struct fio_option *options, void *data)
391 struct fio_option *o;
393 o = find_option(options, opt);
395 fprintf(stderr, "Bad option %s\n", opt);
399 if (!handle_option(o, val, data))
402 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
406 int parse_option(const char *opt, struct fio_option *options, void *data)
408 struct fio_option *o;
412 strncpy(tmp, opt, sizeof(tmp) - 1);
414 pre = strchr(tmp, '=');
420 o = find_option(options, pre);
422 o = find_option(options, tmp);
427 fprintf(stderr, "Bad option %s\n", tmp);
431 if (!handle_option(o, post, data))
434 fprintf(stderr, "fio: failed parsing %s\n", opt);
438 int show_cmd_help(struct fio_option *options, const char *name)
440 int show_all = !strcmp(name, "all");
441 const char *typehelp[] = {
443 "string with possible k/m/g postfix (opt=4k)",
444 "string with range and postfix (opt=1k-4k)",
445 "string with time postfix (opt=10s)",
447 "string with dual range (opt=1k-4k,4k-8k)",
448 "integer value (opt=100)",
449 "boolean value (opt=1)",
452 struct fio_option *o;
455 for (o = &options[0]; o->name; o++) {
456 int match = !strcmp(name, o->name);
458 if (show_all || match) {
460 printf("%20s: %s\n", o->name, o->help);
468 printf("%20s: %s\n", "type", typehelp[o->type]);
469 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
470 show_option_range(o);
471 show_option_values(o);
477 printf("No such command: %s\n", name);
482 * Handle parsing of default parameters.
484 void fill_default_options(void *data, struct fio_option *options)
486 struct fio_option *o;
488 for (o = &options[0]; o->name; o++)
490 handle_option(o, o->def, data);
494 * Sanitize the options structure. For now it just sets min/max for bool
495 * values and whether both callback and offsets are given.
497 void options_init(struct fio_option *options)
499 struct fio_option *o;
501 for (o = &options[0]; o->name; o++) {
502 if (o->type == FIO_OPT_BOOL) {
506 if (!o->cb && !o->off1)
507 fprintf(stderr, "Option %s: neither cb nor offset given\n", o->name);
508 if (o->type == FIO_OPT_STR)
510 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
511 fprintf(stderr, "Option %s: both cb and offset given\n", o->name);