blob: 6e935f31069f3cf0076fb5806c8a595d82b6ff65 [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]);
67 return 0;
68}
69
Jens Axboeb4692822006-10-27 13:43:22 +020070static int check_str_bytes(const char *p, unsigned long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +020071{
Jens Axboecb2c86f2006-10-26 13:48:34 +020072 return str_to_decimal(p, val, 1);
73}
74
Jens Axboeb4692822006-10-27 13:43:22 +020075static int check_str_time(const char *p, unsigned long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +020076{
Jens Axboecb2c86f2006-10-26 13:48:34 +020077 return str_to_decimal(p, val, 0);
78}
79
80void strip_blank_front(char **p)
81{
82 char *s = *p;
83
84 while (isspace(*s))
85 s++;
86}
87
88void strip_blank_end(char *p)
89{
90 char *s = p + strlen(p) - 1;
91
92 while (isspace(*s) || iscntrl(*s))
93 s--;
94
95 *(s + 1) = '\0';
96}
97
Jens Axboeb765a372006-10-27 15:00:16 +020098static int check_range_bytes(const char *str, unsigned long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +020099{
100 char suffix;
101
102 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
103 *val *= get_mult_bytes(suffix);
104 return 0;
105 }
106
107 if (sscanf(str, "%lu", val) == 1)
108 return 0;
109
110 return 1;
111}
112
Jens Axboeb4692822006-10-27 13:43:22 +0200113static int check_int(const char *p, unsigned int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200114{
Jens Axboee1f36502006-10-27 10:54:08 +0200115 if (sscanf(p, "%u", val) == 1)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200116 return 0;
117
118 return 1;
119}
120
Jens Axboee1f36502006-10-27 10:54:08 +0200121static struct fio_option *find_option(struct fio_option *options,
122 const char *opt)
123{
Jens Axboe33963c62006-10-27 11:33:01 +0200124 struct fio_option *o = &options[0];
Jens Axboee1f36502006-10-27 10:54:08 +0200125
Jens Axboe33963c62006-10-27 11:33:01 +0200126 while (o->name) {
Jens Axboee1f36502006-10-27 10:54:08 +0200127 if (!strcmp(o->name, opt))
128 return o;
129
Jens Axboe33963c62006-10-27 11:33:01 +0200130 o++;
131 }
Jens Axboee1f36502006-10-27 10:54:08 +0200132
133 return NULL;
134}
135
Jens Axboef90eff52006-11-06 11:08:21 +0100136static int __handle_option(struct fio_option *o, const char *ptr, void *data,
137 int first)
Jens Axboee1f36502006-10-27 10:54:08 +0200138{
Jens Axboe75e6f362006-11-03 08:17:09 +0100139 unsigned int il, *ilp1, *ilp2;
Jens Axboee1f36502006-10-27 10:54:08 +0200140 unsigned long long ull, *ullp;
Jens Axboe75e6f362006-11-03 08:17:09 +0100141 unsigned long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200142 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200143 int ret = 0, is_time = 0;
144
Jens Axboee1f36502006-10-27 10:54:08 +0200145 switch (o->type) {
146 case FIO_OPT_STR: {
147 fio_opt_str_fn *fn = o->cb;
148
149 ret = fn(data, ptr);
150 break;
151 }
152 case FIO_OPT_STR_VAL_TIME:
153 is_time = 1;
Jens Axboe75e6f362006-11-03 08:17:09 +0100154 case FIO_OPT_STR_VAL:
155 case FIO_OPT_STR_VAL_INT: {
Jens Axboee1f36502006-10-27 10:54:08 +0200156 fio_opt_str_val_fn *fn = o->cb;
157
158 if (is_time)
159 ret = check_str_time(ptr, &ull);
160 else
161 ret = check_str_bytes(ptr, &ull);
162
163 if (ret)
164 break;
165
166 if (o->max_val && ull > o->max_val)
167 ull = o->max_val;
168
169 if (fn)
170 ret = fn(data, &ull);
171 else {
Jens Axboe75e6f362006-11-03 08:17:09 +0100172 if (o->type == FIO_OPT_STR_VAL_INT) {
Jens Axboef90eff52006-11-06 11:08:21 +0100173 if (first) {
174 ilp1 = td_var(data, o->off1);
175 *ilp1 = ull;
176 if (o->off2) {
177 ilp1 = td_var(data, o->off2);
178 *ilp1 = ull;
179 }
180 } else if (o->off2) {
181 ilp1 = td_var(data, o->off2);
182 *ilp1 = ull;
183 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100184 } else {
Jens Axboef90eff52006-11-06 11:08:21 +0100185 if (first) {
186 ullp = td_var(data, o->off1);
187 *ullp = ull;
188 if (o->off2) {
189 ullp = td_var(data, o->off2);
190 *ullp = ull;
191 }
192 } else if (o->off2) {
193 ullp = td_var(data, o->off2);
194 *ullp = ull;
195 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100196 }
Jens Axboee1f36502006-10-27 10:54:08 +0200197 }
198 break;
199 }
200 case FIO_OPT_STR_STORE:
201 cp = td_var(data, o->off1);
202 *cp = strdup(ptr);
203 break;
204 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200205 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200206 char *p1, *p2;
207
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100208 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200209
210 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200211 if (!p1) {
212 ret = 1;
213 break;
214 }
215
216 p2 = p1 + 1;
217 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200218 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200219
220 ret = 1;
221 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
222 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200223 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100224 unsigned long foo = ul1;
225
226 ul1 = ul2;
227 ul2 = foo;
228 }
229
230 if (first) {
231 ilp1 = td_var(data, o->off1);
232 ilp2 = td_var(data, o->off2);
Jens Axboe75e6f362006-11-03 08:17:09 +0100233 *ilp1 = ul1;
Jens Axboef90eff52006-11-06 11:08:21 +0100234 *ilp2 = ul2;
235 if (o->off3 && o->off4) {
236 ilp1 = td_var(data, o->off3);
237 ilp2 = td_var(data, o->off4);
238 *ilp1 = ul1;
239 *ilp2 = ul2;
240 }
241 } else if (o->off3 && o->off4) {
242 ilp1 = td_var(data, o->off3);
243 ilp2 = td_var(data, o->off4);
244 *ilp1 = ul1;
245 *ilp2 = ul2;
Jens Axboee1f36502006-10-27 10:54:08 +0200246 }
247 }
248
249 break;
250 }
251 case FIO_OPT_INT: {
252 fio_opt_int_fn *fn = o->cb;
253
254 ret = check_int(ptr, &il);
255 if (ret)
256 break;
257
258 if (o->max_val && il > o->max_val)
259 il = o->max_val;
260
261 if (fn)
262 ret = fn(data, &il);
263 else {
Jens Axboef90eff52006-11-06 11:08:21 +0100264 if (first || !o->off2)
265 ilp1 = td_var(data, o->off1);
266 else
267 ilp1 = td_var(data, o->off2);
268
Jens Axboe75e6f362006-11-03 08:17:09 +0100269 *ilp1 = il;
Jens Axboee1f36502006-10-27 10:54:08 +0200270 }
271 break;
272 }
273 case FIO_OPT_STR_SET: {
274 fio_opt_str_set_fn *fn = o->cb;
275
276 if (fn)
277 ret = fn(data);
278 else {
Jens Axboef90eff52006-11-06 11:08:21 +0100279 if (first || !o->off2)
280 ilp1 = td_var(data, o->off1);
281 else
282 ilp1 = td_var(data, o->off2);
283
Jens Axboe75e6f362006-11-03 08:17:09 +0100284 *ilp1 = 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200285 }
286 break;
287 }
288 default:
289 fprintf(stderr, "Bad option type %d\n", o->type);
290 ret = 1;
291 }
292
Jens Axboee1f36502006-10-27 10:54:08 +0200293 return ret;
294}
295
Jens Axboef90eff52006-11-06 11:08:21 +0100296static int handle_option(struct fio_option *o, const char *ptr, void *data)
297{
298 const char *ptr2;
299 int ret;
300
301 ret = __handle_option(o, ptr, data, 1);
302 if (ret)
303 return ret;
304
305 /*
306 * See if we have a second set of parameters, hidden after a comma
307 */
308 ptr2 = strchr(ptr, ',');
309 if (!ptr2)
310 return 0;
311
312 ptr2++;
313 return __handle_option(o, ptr2, data, 0);
314}
315
Jens Axboeb4692822006-10-27 13:43:22 +0200316int parse_cmd_option(const char *opt, const char *val,
317 struct fio_option *options, void *data)
318{
319 struct fio_option *o;
320
321 o = find_option(options, opt);
322 if (!o) {
323 fprintf(stderr, "Bad option %s\n", opt);
324 return 1;
325 }
326
Jens Axboeb1508cf2006-11-02 09:12:40 +0100327 if (!handle_option(o, val, data))
328 return 0;
329
330 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
331 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200332}
333
Jens Axboee1f36502006-10-27 10:54:08 +0200334int parse_option(const char *opt, struct fio_option *options, void *data)
335{
Jens Axboeb4692822006-10-27 13:43:22 +0200336 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200337 char *pre, *post;
338 char tmp[64];
339
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100340 strncpy(tmp, opt, sizeof(tmp) - 1);
Jens Axboee1f36502006-10-27 10:54:08 +0200341
342 pre = strchr(tmp, '=');
343 if (pre) {
344 post = pre;
345 *pre = '\0';
346 pre = tmp;
347 post++;
348 o = find_option(options, pre);
349 } else {
350 o = find_option(options, tmp);
351 post = NULL;
352 }
353
354 if (!o) {
355 fprintf(stderr, "Bad option %s\n", tmp);
356 return 1;
357 }
358
Jens Axboeb1508cf2006-11-02 09:12:40 +0100359 if (!handle_option(o, post, data))
360 return 0;
361
362 fprintf(stderr, "fio: failed parsing %s\n", opt);
363 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200364}