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]);
70 static int check_str_bytes(const char *p, unsigned long long *val)
72 return str_to_decimal(p, val, 1);
75 static int check_str_time(const char *p, unsigned long long *val)
77 return str_to_decimal(p, val, 0);
80 void strip_blank_front(char **p)
88 void strip_blank_end(char *p)
90 char *s = p + strlen(p) - 1;
92 while (isspace(*s) || iscntrl(*s))
98 static int check_range_bytes(const char *str, unsigned long *val)
102 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
103 *val *= get_mult_bytes(suffix);
107 if (sscanf(str, "%lu", val) == 1)
113 static int check_int(const char *p, unsigned int *val)
115 if (sscanf(p, "%u", val) == 1)
121 static struct fio_option *find_option(struct fio_option *options,
124 struct fio_option *o = &options[0];
127 if (!strcmp(o->name, opt))
136 #define val_store(ptr, val, off, data) \
138 ptr = td_var((data), (off)); \
142 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
145 unsigned int il, *ilp;
146 unsigned long long ull, *ullp;
147 unsigned long ul1, ul2;
149 int ret = 0, is_time = 0;
153 fio_opt_str_fn *fn = o->cb;
158 case FIO_OPT_STR_VAL_TIME:
160 case FIO_OPT_STR_VAL:
161 case FIO_OPT_STR_VAL_INT: {
162 fio_opt_str_val_fn *fn = o->cb;
165 ret = check_str_time(ptr, &ull);
167 ret = check_str_bytes(ptr, &ull);
172 if (o->max_val && ull > o->max_val)
176 ret = fn(data, &ull);
178 if (o->type == FIO_OPT_STR_VAL_INT) {
180 val_store(ilp, ull, o->off1, data);
182 val_store(ilp, ull, o->off2, data);
185 val_store(ullp, ull, o->off1, data);
187 val_store(ullp, ull, o->off2, data);
192 case FIO_OPT_STR_STORE:
193 cp = td_var(data, o->off1);
196 case FIO_OPT_RANGE: {
200 strncpy(tmp, ptr, sizeof(tmp) - 1);
202 p1 = strchr(tmp, '-');
213 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
216 unsigned long foo = ul1;
223 val_store(ilp, ul1, o->off1, data);
224 val_store(ilp, ul2, o->off2, data);
226 if (o->off3 && o->off4) {
227 val_store(ilp, ul1, o->off3, data);
228 val_store(ilp, ul2, o->off4, data);
235 fio_opt_int_fn *fn = o->cb;
237 ret = check_int(ptr, &il);
241 if (o->max_val && il > o->max_val)
248 val_store(ilp, il, o->off1, data);
250 val_store(ilp, il, o->off2, data);
254 case FIO_OPT_STR_SET: {
255 fio_opt_str_set_fn *fn = o->cb;
261 val_store(ilp, 1, o->off1, data);
263 val_store(ilp, 1, o->off2, data);
268 fprintf(stderr, "Bad option type %d\n", o->type);
275 static int handle_option(struct fio_option *o, const char *ptr, void *data)
280 ret = __handle_option(o, ptr, data, 1);
285 * See if we have a second set of parameters, hidden after a comma
287 ptr2 = strchr(ptr, ',');
292 return __handle_option(o, ptr2, data, 0);
295 int parse_cmd_option(const char *opt, const char *val,
296 struct fio_option *options, void *data)
298 struct fio_option *o;
300 o = find_option(options, opt);
302 fprintf(stderr, "Bad option %s\n", opt);
306 if (!handle_option(o, val, data))
309 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
313 int parse_option(const char *opt, struct fio_option *options, void *data)
315 struct fio_option *o;
319 strncpy(tmp, opt, sizeof(tmp) - 1);
321 pre = strchr(tmp, '=');
327 o = find_option(options, pre);
329 o = find_option(options, tmp);
334 fprintf(stderr, "Bad option %s\n", tmp);
338 if (!handle_option(o, post, data))
341 fprintf(stderr, "fio: failed parsing %s\n", opt);