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;
159 fio_opt_str_fn *fn = o->cb;
164 case FIO_OPT_STR_VAL_TIME:
166 case FIO_OPT_STR_VAL:
167 case FIO_OPT_STR_VAL_INT: {
168 fio_opt_str_val_fn *fn = o->cb;
171 ret = check_str_time(ptr, &ull);
173 ret = check_str_bytes(ptr, &ull);
178 if (o->max_val && ull > o->max_val)
182 ret = fn(data, &ull);
184 if (o->type == FIO_OPT_STR_VAL_INT) {
186 val_store(ilp, ull, o->off1, data);
187 if (!more && o->off2)
188 val_store(ilp, ull, o->off2, data);
191 val_store(ullp, ull, o->off1, data);
192 if (!more && o->off2)
193 val_store(ullp, ull, o->off2, data);
198 case FIO_OPT_STR_STORE:
199 cp = td_var(data, o->off1);
202 case FIO_OPT_RANGE: {
206 strncpy(tmp, ptr, sizeof(tmp) - 1);
208 p1 = strchr(tmp, '-');
219 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
222 unsigned long foo = ul1;
229 val_store(ilp, ul1, o->off1, data);
230 val_store(ilp, ul2, o->off2, data);
232 if (!more && o->off3 && o->off4) {
233 val_store(ilp, ul1, o->off3, data);
234 val_store(ilp, ul2, o->off4, data);
241 fio_opt_int_fn *fn = o->cb;
243 ret = check_int(ptr, &il);
247 if (o->max_val && il > o->max_val)
254 val_store(ilp, il, o->off1, data);
255 if (!more && o->off2)
256 val_store(ilp, il, o->off2, data);
260 case FIO_OPT_STR_SET: {
261 fio_opt_str_set_fn *fn = o->cb;
267 val_store(ilp, 1, o->off1, data);
268 if (!more && o->off2)
269 val_store(ilp, 1, o->off2, data);
274 fprintf(stderr, "Bad option type %d\n", o->type);
281 static int handle_option(struct fio_option *o, const char *ptr, void *data)
287 * See if we have a second set of parameters, hidden after a comma.
288 * Do this before parsing the first round, to check if we should
289 * copy set 1 options to set 2.
291 ptr2 = strchr(ptr, ',');
294 * Don't return early if parsing the first option fails - if
295 * we are doing multiple arguments, we can allow the first one
298 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
304 r2 = __handle_option(o, ptr2, data, 0, 0);
309 int parse_cmd_option(const char *opt, const char *val,
310 struct fio_option *options, void *data)
312 struct fio_option *o;
314 o = find_option(options, opt);
316 fprintf(stderr, "Bad option %s\n", opt);
320 if (!handle_option(o, val, data))
323 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
327 int parse_option(const char *opt, struct fio_option *options, void *data)
329 struct fio_option *o;
333 strncpy(tmp, opt, sizeof(tmp) - 1);
335 pre = strchr(tmp, '=');
341 o = find_option(options, pre);
343 o = find_option(options, tmp);
348 fprintf(stderr, "Bad option %s\n", tmp);
352 if (!handle_option(o, post, data))
355 fprintf(stderr, "fio: failed parsing %s\n", opt);