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;
204 for (i = 0; i < PARSE_MAX_VP; i++) {
206 if (!vp->ival || vp->ival[0] == '\0')
208 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
212 val_store(ilp, vp->oval, o->off1, data);
218 show_option_values(o);
223 case FIO_OPT_STR_VAL_TIME:
225 case FIO_OPT_STR_VAL:
226 case FIO_OPT_STR_VAL_INT: {
227 fio_opt_str_val_fn *fn = o->cb;
230 ret = check_str_time(ptr, &ull);
232 ret = check_str_bytes(ptr, &ull);
237 if (o->maxval && ull > o->maxval) {
238 fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval);
241 if (o->minval && ull < o->minval) {
242 fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval);
247 ret = fn(data, &ull);
249 if (o->type == FIO_OPT_STR_VAL_INT) {
251 val_store(ilp, ull, o->off1, data);
252 if (!more && o->off2)
253 val_store(ilp, ull, o->off2, data);
256 val_store(ullp, ull, o->off1, data);
257 if (!more && o->off2)
258 val_store(ullp, ull, o->off2, data);
263 case FIO_OPT_STR_STORE:
264 cp = td_var(data, o->off1);
267 case FIO_OPT_RANGE: {
271 strncpy(tmp, ptr, sizeof(tmp) - 1);
273 p1 = strchr(tmp, '-');
275 p1 = strchr(tmp, ':');
287 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
290 unsigned long foo = ul1;
297 val_store(ilp, ul1, o->off1, data);
298 val_store(ilp, ul2, o->off2, data);
300 if (!more && o->off3 && o->off4) {
301 val_store(ilp, ul1, o->off3, data);
302 val_store(ilp, ul2, o->off4, data);
310 fio_opt_int_fn *fn = o->cb;
312 ret = check_int(ptr, &il);
316 if (o->maxval && il > (int) o->maxval) {
317 fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval);
320 if (o->minval && il < o->minval) {
321 fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval);
332 val_store(ilp, il, o->off1, data);
333 if (!more && o->off2)
334 val_store(ilp, il, o->off2, data);
338 case FIO_OPT_STR_SET: {
339 fio_opt_str_set_fn *fn = o->cb;
345 val_store(ilp, 1, o->off1, data);
346 if (!more && o->off2)
347 val_store(ilp, 1, o->off2, data);
352 fprintf(stderr, "Bad option type %u\n", o->type);
359 static int handle_option(struct fio_option *o, const char *ptr, void *data)
361 const char *ptr2 = NULL;
365 * See if we have a second set of parameters, hidden after a comma.
366 * Do this before parsing the first round, to check if we should
367 * copy set 1 options to set 2.
369 if (ptr && (o->type != FIO_OPT_STR_STORE)) {
370 ptr2 = strchr(ptr, ',');
372 ptr2 = strchr(ptr, ':');
376 * Don't return early if parsing the first option fails - if
377 * we are doing multiple arguments, we can allow the first one
380 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
386 r2 = __handle_option(o, ptr2, data, 0, 0);
391 int parse_cmd_option(const char *opt, const char *val,
392 struct fio_option *options, void *data)
394 struct fio_option *o;
396 o = find_option(options, opt);
398 fprintf(stderr, "Bad option %s\n", opt);
402 if (!handle_option(o, val, data))
405 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
409 int parse_option(const char *opt, struct fio_option *options, void *data)
411 struct fio_option *o;
415 strncpy(tmp, opt, sizeof(tmp) - 1);
417 pre = strchr(tmp, '=');
423 o = find_option(options, pre);
425 o = find_option(options, tmp);
430 fprintf(stderr, "Bad option %s\n", tmp);
434 if (!handle_option(o, post, data))
437 fprintf(stderr, "fio: failed parsing %s\n", opt);
441 int show_cmd_help(struct fio_option *options, const char *name)
443 int show_all = !strcmp(name, "all");
444 const char *typehelp[] = {
446 "string with possible k/m/g postfix (opt=4k)",
447 "string with range and postfix (opt=1k-4k)",
448 "string with time postfix (opt=10s)",
450 "string with dual range (opt=1k-4k,4k-8k)",
451 "integer value (opt=100)",
452 "boolean value (opt=1)",
455 struct fio_option *o;
458 for (o = &options[0]; o->name; o++) {
459 int match = !strcmp(name, o->name);
461 if (show_all || match) {
463 printf("%20s: %s\n", o->name, o->help);
471 printf("%20s: %s\n", "type", typehelp[o->type]);
472 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
473 show_option_range(o);
474 show_option_values(o);
480 printf("No such command: %s\n", name);
485 * Handle parsing of default parameters.
487 void fill_default_options(void *data, struct fio_option *options)
489 struct fio_option *o;
491 for (o = &options[0]; o->name; o++)
493 handle_option(o, o->def, data);
497 * Sanitize the options structure. For now it just sets min/max for bool
498 * values and whether both callback and offsets are given.
500 void options_init(struct fio_option *options)
502 struct fio_option *o;
504 for (o = &options[0]; o->name; o++) {
505 if (o->type == FIO_OPT_BOOL) {
509 if (!o->cb && !o->off1)
510 fprintf(stderr, "Option %s: neither cb nor offset given\n", o->name);
511 if (o->type == FIO_OPT_STR)
513 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
514 fprintf(stderr, "Option %s: both cb and offset given\n", o->name);