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, unsigned long long *val, int kilo)
59 *val = strtoul(str, NULL, 10);
60 if (*val == ULONG_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, unsigned long long *val)
73 return str_to_decimal(p, val, 1);
76 static int check_str_time(const char *p, unsigned 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, unsigned 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, unsigned 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))
142 #define val_store(ptr, val, off, data) \
144 ptr = td_var((data), (off)); \
148 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
151 unsigned int il, *ilp;
152 unsigned long long ull, *ullp;
153 unsigned long ul1, ul2;
155 int ret = 0, is_time = 0;
157 if (!ptr && o->type != FIO_OPT_STR_SET) {
158 fprintf(stderr, "Option %s requires an argument\n", o->name);
164 fio_opt_str_fn *fn = o->cb;
169 case FIO_OPT_STR_VAL_TIME:
171 case FIO_OPT_STR_VAL:
172 case FIO_OPT_STR_VAL_INT: {
173 fio_opt_str_val_fn *fn = o->cb;
176 ret = check_str_time(ptr, &ull);
178 ret = check_str_bytes(ptr, &ull);
183 if (o->maxval && ull > o->maxval)
185 if (o->minval && ull < o->minval)
189 ret = fn(data, &ull);
191 if (o->type == FIO_OPT_STR_VAL_INT) {
193 val_store(ilp, ull, o->off1, data);
194 if (!more && o->off2)
195 val_store(ilp, ull, o->off2, data);
198 val_store(ullp, ull, o->off1, data);
199 if (!more && o->off2)
200 val_store(ullp, ull, o->off2, data);
205 case FIO_OPT_STR_STORE:
206 cp = td_var(data, o->off1);
209 case FIO_OPT_RANGE: {
213 strncpy(tmp, ptr, sizeof(tmp) - 1);
215 p1 = strchr(tmp, '-');
226 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
229 unsigned long foo = ul1;
236 val_store(ilp, ul1, o->off1, data);
237 val_store(ilp, ul2, o->off2, data);
239 if (!more && o->off3 && o->off4) {
240 val_store(ilp, ul1, o->off3, data);
241 val_store(ilp, ul2, o->off4, data);
249 fio_opt_int_fn *fn = o->cb;
251 ret = check_int(ptr, &il);
255 if (o->maxval && il > o->maxval)
257 if (o->minval && il < o->minval)
264 val_store(ilp, il, o->off1, data);
265 if (!more && o->off2)
266 val_store(ilp, il, o->off2, data);
270 case FIO_OPT_STR_SET: {
271 fio_opt_str_set_fn *fn = o->cb;
277 val_store(ilp, 1, o->off1, data);
278 if (!more && o->off2)
279 val_store(ilp, 1, o->off2, data);
284 fprintf(stderr, "Bad option type %u\n", o->type);
291 static int handle_option(struct fio_option *o, const char *ptr, void *data)
293 const char *ptr2 = NULL;
297 * See if we have a second set of parameters, hidden after a comma.
298 * Do this before parsing the first round, to check if we should
299 * copy set 1 options to set 2.
302 ptr2 = strchr(ptr, ',');
305 * Don't return early if parsing the first option fails - if
306 * we are doing multiple arguments, we can allow the first one
309 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
315 r2 = __handle_option(o, ptr2, data, 0, 0);
320 int parse_cmd_option(const char *opt, const char *val,
321 struct fio_option *options, void *data)
323 struct fio_option *o;
325 o = find_option(options, opt);
327 fprintf(stderr, "Bad option %s\n", opt);
331 if (!handle_option(o, val, data))
334 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
338 int parse_option(const char *opt, struct fio_option *options, void *data)
340 struct fio_option *o;
344 strncpy(tmp, opt, sizeof(tmp) - 1);
346 pre = strchr(tmp, '=');
352 o = find_option(options, pre);
354 o = find_option(options, tmp);
359 fprintf(stderr, "Bad option %s\n", tmp);
363 if (!handle_option(o, post, data))
366 fprintf(stderr, "fio: failed parsing %s\n", opt);
370 static void show_option_range(struct fio_option *o)
372 if (!o->minval && !o->maxval)
375 printf("%16s: min=%d, max=%d\n", "range", o->minval, o->maxval);
378 static void show_option_values(struct fio_option *o)
392 printf("%16s: ", "valid values");
402 int show_cmd_help(struct fio_option *options, const char *name)
404 int show_all = !strcmp(name, "all");
405 struct fio_option *o = &options[0];
406 const char *typehelp[] = {
408 "string with possible k/m/g postfix (opt=4k)",
409 "string with range and postfix (opt=1k-4k)",
410 "string with time postfix (opt=10s)",
412 "string with dual range (opt=1k-4k,4k-8k)",
413 "integer value (opt=100)",
414 "boolean value (opt=1)",
420 int match = !strcmp(name, o->name);
422 if (show_all || match) {
424 printf("%16s: %s\n", o->name, o->help);
426 printf("%16s: %s\n", "type", typehelp[o->type]);
427 printf("%16s: %s\n", "default", o->def ? o->def : "no default");
428 show_option_range(o);
429 show_option_values(o);
439 printf("No such command: %s\n", name);
444 * Handle parsing of default parameters.
446 void fill_default_options(void *data, struct fio_option *options)
448 struct fio_option *o = &options[0];
452 handle_option(o, o->def, data);
458 * Sanitize the options structure. For now it just sets min/max for bool
461 void options_init(struct fio_option *options)
463 struct fio_option *o = &options[0];
466 if (o->type == FIO_OPT_BOOL) {