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 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
139 unsigned int il, *ilp1, *ilp2;
140 unsigned long long ull, *ullp;
141 unsigned long ul1, ul2;
143 int ret = 0, is_time = 0;
147 fio_opt_str_fn *fn = o->cb;
152 case FIO_OPT_STR_VAL_TIME:
154 case FIO_OPT_STR_VAL:
155 case FIO_OPT_STR_VAL_INT: {
156 fio_opt_str_val_fn *fn = o->cb;
159 ret = check_str_time(ptr, &ull);
161 ret = check_str_bytes(ptr, &ull);
166 if (o->max_val && ull > o->max_val)
170 ret = fn(data, &ull);
172 if (o->type == FIO_OPT_STR_VAL_INT) {
174 ilp1 = td_var(data, o->off1);
177 ilp1 = td_var(data, o->off2);
180 } else if (o->off2) {
181 ilp1 = td_var(data, o->off2);
186 ullp = td_var(data, o->off1);
189 ullp = td_var(data, o->off2);
192 } else if (o->off2) {
193 ullp = td_var(data, o->off2);
200 case FIO_OPT_STR_STORE:
201 cp = td_var(data, o->off1);
204 case FIO_OPT_RANGE: {
208 strncpy(tmp, ptr, sizeof(tmp) - 1);
210 p1 = strchr(tmp, '-');
221 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
224 unsigned long foo = ul1;
231 ilp1 = td_var(data, o->off1);
232 ilp2 = td_var(data, o->off2);
235 if (o->off3 && o->off4) {
236 ilp1 = td_var(data, o->off3);
237 ilp2 = td_var(data, o->off4);
241 } else if (o->off3 && o->off4) {
242 ilp1 = td_var(data, o->off3);
243 ilp2 = td_var(data, o->off4);
252 fio_opt_int_fn *fn = o->cb;
254 ret = check_int(ptr, &il);
258 if (o->max_val && il > o->max_val)
264 if (first || !o->off2)
265 ilp1 = td_var(data, o->off1);
267 ilp1 = td_var(data, o->off2);
273 case FIO_OPT_STR_SET: {
274 fio_opt_str_set_fn *fn = o->cb;
279 if (first || !o->off2)
280 ilp1 = td_var(data, o->off1);
282 ilp1 = td_var(data, o->off2);
289 fprintf(stderr, "Bad option type %d\n", o->type);
296 static int handle_option(struct fio_option *o, const char *ptr, void *data)
301 ret = __handle_option(o, ptr, data, 1);
306 * See if we have a second set of parameters, hidden after a comma
308 ptr2 = strchr(ptr, ',');
313 return __handle_option(o, ptr2, data, 0);
316 int parse_cmd_option(const char *opt, const char *val,
317 struct fio_option *options, void *data)
319 struct fio_option *o;
321 o = find_option(options, opt);
323 fprintf(stderr, "Bad option %s\n", opt);
327 if (!handle_option(o, val, data))
330 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
334 int parse_option(const char *opt, struct fio_option *options, void *data)
336 struct fio_option *o;
340 strncpy(tmp, opt, sizeof(tmp) - 1);
342 pre = strchr(tmp, '=');
348 o = find_option(options, pre);
350 o = find_option(options, tmp);
355 fprintf(stderr, "Bad option %s\n", tmp);
359 if (!handle_option(o, post, data))
362 fprintf(stderr, "fio: failed parsing %s\n", opt);