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;
45 return 1024 * 1024 * 1024 * 1024UL;
52 * convert string into decimal value, noting any size suffix
54 static int str_to_decimal(const char *str, long long *val, int kilo)
62 *val = strtoll(str, NULL, 10);
63 if (*val == LONG_MAX && errno == ERANGE)
67 *val *= get_mult_bytes(str[len - 1]);
69 *val *= get_mult_time(str[len - 1]);
74 static int check_str_bytes(const char *p, long long *val)
76 return str_to_decimal(p, val, 1);
79 static int check_str_time(const char *p, long long *val)
81 return str_to_decimal(p, val, 0);
84 void strip_blank_front(char **p)
92 void strip_blank_end(char *p)
94 char *s = p + strlen(p) - 1;
96 while (isspace(*s) || iscntrl(*s))
102 static int check_range_bytes(const char *str, long *val)
109 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
110 *val *= get_mult_bytes(suffix);
114 if (sscanf(str, "%lu", val) == 1)
120 static int check_int(const char *p, int *val)
124 if (sscanf(p, "%u", val) == 1)
130 static struct fio_option *find_option(struct fio_option *options,
133 struct fio_option *o;
135 for (o = &options[0]; o->name; o++) {
136 if (!strcmp(o->name, opt))
138 else if (o->alias && !strcmp(o->alias, opt))
145 #define val_store(ptr, val, off, data) \
147 ptr = td_var((data), (off)); \
151 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
155 long long ull, *ullp;
158 int ret = 0, is_time = 0;
160 if (!ptr && o->type != FIO_OPT_STR_SET) {
161 fprintf(stderr, "Option %s requires an argument\n", o->name);
167 fio_opt_str_fn *fn = o->cb;
172 case FIO_OPT_STR_VAL_TIME:
174 case FIO_OPT_STR_VAL:
175 case FIO_OPT_STR_VAL_INT: {
176 fio_opt_str_val_fn *fn = o->cb;
179 ret = check_str_time(ptr, &ull);
181 ret = check_str_bytes(ptr, &ull);
186 if (o->maxval && ull > o->maxval) {
187 fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval);
190 if (o->minval && ull < o->minval) {
191 fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval);
196 ret = fn(data, &ull);
198 if (o->type == FIO_OPT_STR_VAL_INT) {
200 val_store(ilp, ull, o->off1, data);
201 if (!more && o->off2)
202 val_store(ilp, ull, o->off2, data);
205 val_store(ullp, ull, o->off1, data);
206 if (!more && o->off2)
207 val_store(ullp, ull, o->off2, data);
212 case FIO_OPT_STR_STORE:
213 cp = td_var(data, o->off1);
216 case FIO_OPT_RANGE: {
220 strncpy(tmp, ptr, sizeof(tmp) - 1);
222 p1 = strchr(tmp, '-');
224 p1 = strchr(tmp, ':');
236 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
239 unsigned long foo = ul1;
246 val_store(ilp, ul1, o->off1, data);
247 val_store(ilp, ul2, o->off2, data);
249 if (!more && o->off3 && o->off4) {
250 val_store(ilp, ul1, o->off3, data);
251 val_store(ilp, ul2, o->off4, data);
259 fio_opt_int_fn *fn = o->cb;
261 ret = check_int(ptr, &il);
265 if (o->maxval && il > (int) o->maxval) {
266 fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval);
269 if (o->minval && il < o->minval) {
270 fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval);
281 val_store(ilp, il, o->off1, data);
282 if (!more && o->off2)
283 val_store(ilp, il, o->off2, data);
287 case FIO_OPT_STR_SET: {
288 fio_opt_str_set_fn *fn = o->cb;
294 val_store(ilp, 1, o->off1, data);
295 if (!more && o->off2)
296 val_store(ilp, 1, o->off2, data);
301 fprintf(stderr, "Bad option type %u\n", o->type);
308 static int handle_option(struct fio_option *o, const char *ptr, void *data)
310 const char *ptr2 = NULL;
314 * See if we have a second set of parameters, hidden after a comma.
315 * Do this before parsing the first round, to check if we should
316 * copy set 1 options to set 2.
318 if (ptr && (o->type != FIO_OPT_STR_STORE)) {
319 ptr2 = strchr(ptr, ',');
321 ptr2 = strchr(ptr, ':');
325 * Don't return early if parsing the first option fails - if
326 * we are doing multiple arguments, we can allow the first one
329 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
335 r2 = __handle_option(o, ptr2, data, 0, 0);
340 int parse_cmd_option(const char *opt, const char *val,
341 struct fio_option *options, void *data)
343 struct fio_option *o;
345 o = find_option(options, opt);
347 fprintf(stderr, "Bad option %s\n", opt);
351 if (!handle_option(o, val, data))
354 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
358 int parse_option(const char *opt, struct fio_option *options, void *data)
360 struct fio_option *o;
364 strncpy(tmp, opt, sizeof(tmp) - 1);
366 pre = strchr(tmp, '=');
372 o = find_option(options, pre);
374 o = find_option(options, tmp);
379 fprintf(stderr, "Bad option %s\n", tmp);
383 if (!handle_option(o, post, data))
386 fprintf(stderr, "fio: failed parsing %s\n", opt);
390 static void show_option_range(struct fio_option *o)
392 if (!o->minval && !o->maxval)
395 printf("%16s: min=%d, max=%d\n", "range", o->minval, o->maxval);
398 static void show_option_values(struct fio_option *o)
412 printf("%16s: ", "valid values");
422 int show_cmd_help(struct fio_option *options, const char *name)
424 int show_all = !strcmp(name, "all");
425 const char *typehelp[] = {
427 "string with possible k/m/g postfix (opt=4k)",
428 "string with range and postfix (opt=1k-4k)",
429 "string with time postfix (opt=10s)",
431 "string with dual range (opt=1k-4k,4k-8k)",
432 "integer value (opt=100)",
433 "boolean value (opt=1)",
436 struct fio_option *o;
439 for (o = &options[0]; o->name; o++) {
440 int match = !strcmp(name, o->name);
442 if (show_all || match) {
444 printf("%16s: %s\n", o->name, o->help);
452 printf("%16s: %s\n", "type", typehelp[o->type]);
453 printf("%16s: %s\n", "default", o->def ? o->def : "no default");
454 show_option_range(o);
455 show_option_values(o);
461 printf("No such command: %s\n", name);
466 * Handle parsing of default parameters.
468 void fill_default_options(void *data, struct fio_option *options)
470 struct fio_option *o;
472 for (o = &options[0]; o->name; o++)
474 handle_option(o, o->def, data);
478 * Sanitize the options structure. For now it just sets min/max for bool
479 * values and whether both callback and offsets are given.
481 void options_init(struct fio_option *options)
483 struct fio_option *o;
485 for (o = &options[0]; o->name; o++) {
486 if (o->type == FIO_OPT_BOOL) {
490 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
491 fprintf(stderr, "Option %s: both cb and offset given\n", o->name);