2 * This file contains the ini and command liner parser main.
14 static unsigned long get_mult_time(char c)
31 static unsigned long get_mult_bytes(char c)
42 return 1024 * 1024 * 1024;
49 * convert string into decimal value, noting any size suffix
51 static int str_to_decimal(const char *str, long long *val, int kilo)
59 *val = strtol(str, NULL, 10);
60 if (*val == LONG_MAX && errno == ERANGE)
64 *val *= get_mult_bytes(str[len - 1]);
66 *val *= get_mult_time(str[len - 1]);
71 static int check_str_bytes(const char *p, long long *val)
73 return str_to_decimal(p, val, 1);
76 static int check_str_time(const char *p, long long *val)
78 return str_to_decimal(p, val, 0);
81 void strip_blank_front(char **p)
89 void strip_blank_end(char *p)
91 char *s = p + strlen(p) - 1;
93 while (isspace(*s) || iscntrl(*s))
99 static int check_range_bytes(const char *str, long *val)
106 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
107 *val *= get_mult_bytes(suffix);
111 if (sscanf(str, "%lu", val) == 1)
117 static int check_int(const char *p, int *val)
121 if (sscanf(p, "%u", val) == 1)
127 static struct fio_option *find_option(struct fio_option *options,
130 struct fio_option *o = &options[0];
133 if (!strcmp(o->name, opt))
135 else if (o->alias && !strcmp(o->alias, opt))
144 #define val_store(ptr, val, off, data) \
146 ptr = td_var((data), (off)); \
150 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
154 long long ull, *ullp;
157 int ret = 0, is_time = 0;
159 if (!ptr && o->type != FIO_OPT_STR_SET) {
160 fprintf(stderr, "Option %s requires an argument\n", o->name);
166 fio_opt_str_fn *fn = o->cb;
171 case FIO_OPT_STR_VAL_TIME:
173 case FIO_OPT_STR_VAL:
174 case FIO_OPT_STR_VAL_INT: {
175 fio_opt_str_val_fn *fn = o->cb;
178 ret = check_str_time(ptr, &ull);
180 ret = check_str_bytes(ptr, &ull);
185 if (o->maxval && ull > o->maxval) {
186 fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval);
189 if (o->minval && ull < o->minval) {
190 fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval);
195 ret = fn(data, &ull);
197 if (o->type == FIO_OPT_STR_VAL_INT) {
199 val_store(ilp, ull, o->off1, data);
200 if (!more && o->off2)
201 val_store(ilp, ull, o->off2, data);
204 val_store(ullp, ull, o->off1, data);
205 if (!more && o->off2)
206 val_store(ullp, ull, o->off2, data);
211 case FIO_OPT_STR_STORE:
212 cp = td_var(data, o->off1);
215 case FIO_OPT_RANGE: {
219 strncpy(tmp, ptr, sizeof(tmp) - 1);
221 p1 = strchr(tmp, '-');
232 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
235 unsigned long foo = ul1;
242 val_store(ilp, ul1, o->off1, data);
243 val_store(ilp, ul2, o->off2, data);
245 if (!more && o->off3 && o->off4) {
246 val_store(ilp, ul1, o->off3, data);
247 val_store(ilp, ul2, o->off4, data);
255 fio_opt_int_fn *fn = o->cb;
257 ret = check_int(ptr, &il);
261 if (o->maxval && il > (int) o->maxval) {
262 fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval);
265 if (o->minval && il < o->minval) {
266 fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval);
274 val_store(ilp, il, o->off1, data);
275 if (!more && o->off2)
276 val_store(ilp, il, o->off2, data);
280 case FIO_OPT_STR_SET: {
281 fio_opt_str_set_fn *fn = o->cb;
287 val_store(ilp, 1, o->off1, data);
288 if (!more && o->off2)
289 val_store(ilp, 1, o->off2, data);
294 fprintf(stderr, "Bad option type %u\n", o->type);
301 static int handle_option(struct fio_option *o, const char *ptr, void *data)
303 const char *ptr2 = NULL;
307 * See if we have a second set of parameters, hidden after a comma.
308 * Do this before parsing the first round, to check if we should
309 * copy set 1 options to set 2.
312 ptr2 = strchr(ptr, ',');
315 * Don't return early if parsing the first option fails - if
316 * we are doing multiple arguments, we can allow the first one
319 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
325 r2 = __handle_option(o, ptr2, data, 0, 0);
330 int parse_cmd_option(const char *opt, const char *val,
331 struct fio_option *options, void *data)
333 struct fio_option *o;
335 o = find_option(options, opt);
337 fprintf(stderr, "Bad option %s\n", opt);
341 if (!handle_option(o, val, data))
344 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
348 int parse_option(const char *opt, struct fio_option *options, void *data)
350 struct fio_option *o;
354 strncpy(tmp, opt, sizeof(tmp) - 1);
356 pre = strchr(tmp, '=');
362 o = find_option(options, pre);
364 o = find_option(options, tmp);
369 fprintf(stderr, "Bad option %s\n", tmp);
373 if (!handle_option(o, post, data))
376 fprintf(stderr, "fio: failed parsing %s\n", opt);
380 static void show_option_range(struct fio_option *o)
382 if (!o->minval && !o->maxval)
385 printf("%16s: min=%d, max=%d\n", "range", o->minval, o->maxval);
388 static void show_option_values(struct fio_option *o)
402 printf("%16s: ", "valid values");
412 int show_cmd_help(struct fio_option *options, const char *name)
414 int show_all = !strcmp(name, "all");
415 struct fio_option *o = &options[0];
416 const char *typehelp[] = {
418 "string with possible k/m/g postfix (opt=4k)",
419 "string with range and postfix (opt=1k-4k)",
420 "string with time postfix (opt=10s)",
422 "string with dual range (opt=1k-4k,4k-8k)",
423 "integer value (opt=100)",
424 "boolean value (opt=1)",
430 int match = !strcmp(name, o->name);
432 if (show_all || match) {
434 printf("%16s: %s\n", o->name, o->help);
436 printf("%16s: %s\n", "type", typehelp[o->type]);
437 printf("%16s: %s\n", "default", o->def ? o->def : "no default");
438 show_option_range(o);
439 show_option_values(o);
449 printf("No such command: %s\n", name);
454 * Handle parsing of default parameters.
456 void fill_default_options(void *data, struct fio_option *options)
458 struct fio_option *o = &options[0];
462 handle_option(o, o->def, data);
468 * Sanitize the options structure. For now it just sets min/max for bool
471 void options_init(struct fio_option *options)
473 struct fio_option *o = &options[0];
476 if (o->type == FIO_OPT_BOOL) {