blob: 869b3dbf8ba088d3147b0042c74d2618c278d815 [file] [log] [blame]
Jens Axboecb2c86f2006-10-26 13:48:34 +02001/*
2 * This file contains the ini and command liner parser main.
3 */
4#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <ctype.h>
8#include <string.h>
9#include <errno.h>
10#include <limits.h>
11
12#include "parse.h"
13
14static unsigned long get_mult_time(char c)
15{
16 switch (c) {
17 case 'm':
18 case 'M':
19 return 60;
20 case 'h':
21 case 'H':
22 return 60 * 60;
23 case 'd':
24 case 'D':
25 return 24 * 60 * 60;
26 default:
27 return 1;
28 }
29}
30
31static unsigned long get_mult_bytes(char c)
32{
33 switch (c) {
34 case 'k':
35 case 'K':
36 return 1024;
37 case 'm':
38 case 'M':
39 return 1024 * 1024;
40 case 'g':
41 case 'G':
42 return 1024 * 1024 * 1024;
43 default:
44 return 1;
45 }
46}
47
48/*
Jens Axboee1f36502006-10-27 10:54:08 +020049 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +020050 */
Jens Axboeb4692822006-10-27 13:43:22 +020051static int str_to_decimal(const char *str, unsigned long long *val, int kilo)
Jens Axboecb2c86f2006-10-26 13:48:34 +020052{
Jens Axboecb2c86f2006-10-26 13:48:34 +020053 int len;
54
Jens Axboecb2c86f2006-10-26 13:48:34 +020055 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +010056 if (!len)
57 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +020058
59 *val = strtoul(str, NULL, 10);
60 if (*val == ULONG_MAX && errno == ERANGE)
61 return 1;
62
63 if (kilo)
64 *val *= get_mult_bytes(str[len - 1]);
65 else
66 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +010067
Jens Axboecb2c86f2006-10-26 13:48:34 +020068 return 0;
69}
70
Jens Axboeb4692822006-10-27 13:43:22 +020071static int check_str_bytes(const char *p, unsigned long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +020072{
Jens Axboecb2c86f2006-10-26 13:48:34 +020073 return str_to_decimal(p, val, 1);
74}
75
Jens Axboeb4692822006-10-27 13:43:22 +020076static int check_str_time(const char *p, unsigned long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +020077{
Jens Axboecb2c86f2006-10-26 13:48:34 +020078 return str_to_decimal(p, val, 0);
79}
80
81void strip_blank_front(char **p)
82{
83 char *s = *p;
84
85 while (isspace(*s))
86 s++;
87}
88
89void strip_blank_end(char *p)
90{
91 char *s = p + strlen(p) - 1;
92
93 while (isspace(*s) || iscntrl(*s))
94 s--;
95
96 *(s + 1) = '\0';
97}
98
Jens Axboeb765a372006-10-27 15:00:16 +020099static int check_range_bytes(const char *str, unsigned long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200100{
101 char suffix;
102
Jens Axboe787f7e92006-11-06 13:26:29 +0100103 if (!strlen(str))
104 return 1;
105
Jens Axboecb2c86f2006-10-26 13:48:34 +0200106 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
107 *val *= get_mult_bytes(suffix);
108 return 0;
109 }
110
111 if (sscanf(str, "%lu", val) == 1)
112 return 0;
113
114 return 1;
115}
116
Jens Axboeb4692822006-10-27 13:43:22 +0200117static int check_int(const char *p, unsigned int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200118{
Jens Axboe787f7e92006-11-06 13:26:29 +0100119 if (!strlen(p))
120 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200121 if (sscanf(p, "%u", val) == 1)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200122 return 0;
123
124 return 1;
125}
126
Jens Axboee1f36502006-10-27 10:54:08 +0200127static struct fio_option *find_option(struct fio_option *options,
128 const char *opt)
129{
Jens Axboe33963c62006-10-27 11:33:01 +0200130 struct fio_option *o = &options[0];
Jens Axboee1f36502006-10-27 10:54:08 +0200131
Jens Axboe33963c62006-10-27 11:33:01 +0200132 while (o->name) {
Jens Axboee1f36502006-10-27 10:54:08 +0200133 if (!strcmp(o->name, opt))
134 return o;
135
Jens Axboe33963c62006-10-27 11:33:01 +0200136 o++;
137 }
Jens Axboee1f36502006-10-27 10:54:08 +0200138
139 return NULL;
140}
141
Jens Axboe17abbe82006-11-06 11:23:10 +0100142#define val_store(ptr, val, off, data) \
143 do { \
144 ptr = td_var((data), (off)); \
145 *ptr = (val); \
146 } while (0)
147
Jens Axboef90eff52006-11-06 11:08:21 +0100148static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100149 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200150{
Jens Axboe17abbe82006-11-06 11:23:10 +0100151 unsigned int il, *ilp;
Jens Axboee1f36502006-10-27 10:54:08 +0200152 unsigned long long ull, *ullp;
Jens Axboe75e6f362006-11-03 08:17:09 +0100153 unsigned long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200154 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200155 int ret = 0, is_time = 0;
156
Jens Axboee1f36502006-10-27 10:54:08 +0200157 switch (o->type) {
158 case FIO_OPT_STR: {
159 fio_opt_str_fn *fn = o->cb;
160
161 ret = fn(data, ptr);
162 break;
163 }
164 case FIO_OPT_STR_VAL_TIME:
165 is_time = 1;
Jens Axboe75e6f362006-11-03 08:17:09 +0100166 case FIO_OPT_STR_VAL:
167 case FIO_OPT_STR_VAL_INT: {
Jens Axboee1f36502006-10-27 10:54:08 +0200168 fio_opt_str_val_fn *fn = o->cb;
169
170 if (is_time)
171 ret = check_str_time(ptr, &ull);
172 else
173 ret = check_str_bytes(ptr, &ull);
174
175 if (ret)
176 break;
177
178 if (o->max_val && ull > o->max_val)
179 ull = o->max_val;
180
181 if (fn)
182 ret = fn(data, &ull);
183 else {
Jens Axboe75e6f362006-11-03 08:17:09 +0100184 if (o->type == FIO_OPT_STR_VAL_INT) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100185 if (first)
186 val_store(ilp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100187 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100188 val_store(ilp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100189 } else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100190 if (first)
191 val_store(ullp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100192 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100193 val_store(ullp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100194 }
Jens Axboee1f36502006-10-27 10:54:08 +0200195 }
196 break;
197 }
198 case FIO_OPT_STR_STORE:
199 cp = td_var(data, o->off1);
200 *cp = strdup(ptr);
201 break;
202 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200203 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200204 char *p1, *p2;
205
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100206 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200207
208 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200209 if (!p1) {
210 ret = 1;
211 break;
212 }
213
214 p2 = p1 + 1;
215 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200216 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200217
218 ret = 1;
219 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
220 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200221 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100222 unsigned long foo = ul1;
223
224 ul1 = ul2;
225 ul2 = foo;
226 }
227
228 if (first) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100229 val_store(ilp, ul1, o->off1, data);
230 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200231 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100232 if (!more && o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100233 val_store(ilp, ul1, o->off3, data);
234 val_store(ilp, ul2, o->off4, data);
235 }
236 }
237
Jens Axboee1f36502006-10-27 10:54:08 +0200238 break;
239 }
240 case FIO_OPT_INT: {
241 fio_opt_int_fn *fn = o->cb;
242
243 ret = check_int(ptr, &il);
244 if (ret)
245 break;
246
247 if (o->max_val && il > o->max_val)
248 il = o->max_val;
249
250 if (fn)
251 ret = fn(data, &il);
252 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100253 if (first)
254 val_store(ilp, il, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100255 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100256 val_store(ilp, il, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200257 }
258 break;
259 }
260 case FIO_OPT_STR_SET: {
261 fio_opt_str_set_fn *fn = o->cb;
262
263 if (fn)
264 ret = fn(data);
265 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100266 if (first)
267 val_store(ilp, 1, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100268 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100269 val_store(ilp, 1, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200270 }
271 break;
272 }
273 default:
274 fprintf(stderr, "Bad option type %d\n", o->type);
275 ret = 1;
276 }
277
Jens Axboee1f36502006-10-27 10:54:08 +0200278 return ret;
279}
280
Jens Axboef90eff52006-11-06 11:08:21 +0100281static int handle_option(struct fio_option *o, const char *ptr, void *data)
282{
Jens Axboe92b586f2006-11-07 14:29:17 +0100283 const char *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100284 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100285
286 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100287 * 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.
Jens Axboef90eff52006-11-06 11:08:21 +0100290 */
Jens Axboe92b586f2006-11-07 14:29:17 +0100291 if (ptr)
292 ptr2 = strchr(ptr, ',');
Jens Axboe787f7e92006-11-06 13:26:29 +0100293
294 /*
295 * Don't return early if parsing the first option fails - if
296 * we are doing multiple arguments, we can allow the first one
297 * being empty.
298 */
299 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
300
Jens Axboef90eff52006-11-06 11:08:21 +0100301 if (!ptr2)
Jens Axboe787f7e92006-11-06 13:26:29 +0100302 return r1;
Jens Axboef90eff52006-11-06 11:08:21 +0100303
304 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100305 r2 = __handle_option(o, ptr2, data, 0, 0);
306
307 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100308}
309
Jens Axboeb4692822006-10-27 13:43:22 +0200310int parse_cmd_option(const char *opt, const char *val,
311 struct fio_option *options, void *data)
312{
313 struct fio_option *o;
314
315 o = find_option(options, opt);
316 if (!o) {
317 fprintf(stderr, "Bad option %s\n", opt);
318 return 1;
319 }
320
Jens Axboeb1508cf2006-11-02 09:12:40 +0100321 if (!handle_option(o, val, data))
322 return 0;
323
324 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
325 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200326}
327
Jens Axboee1f36502006-10-27 10:54:08 +0200328int parse_option(const char *opt, struct fio_option *options, void *data)
329{
Jens Axboeb4692822006-10-27 13:43:22 +0200330 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200331 char *pre, *post;
332 char tmp[64];
333
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100334 strncpy(tmp, opt, sizeof(tmp) - 1);
Jens Axboee1f36502006-10-27 10:54:08 +0200335
336 pre = strchr(tmp, '=');
337 if (pre) {
338 post = pre;
339 *pre = '\0';
340 pre = tmp;
341 post++;
342 o = find_option(options, pre);
343 } else {
344 o = find_option(options, tmp);
345 post = NULL;
346 }
347
348 if (!o) {
349 fprintf(stderr, "Bad option %s\n", tmp);
350 return 1;
351 }
352
Jens Axboeb1508cf2006-11-02 09:12:40 +0100353 if (!handle_option(o, post, data))
354 return 0;
355
356 fprintf(stderr, "fio: failed parsing %s\n", opt);
357 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200358}