[PATCH] bs= and bsrange= takes both read and write sizes
[fio.git] / parse.c
1 /*
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
14 static 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
31 static 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 /*
49  * convert string into decimal value, noting any size suffix
50  */
51 static int str_to_decimal(const char *str, unsigned long long *val, int kilo)
52 {
53         int len;
54
55         len = strlen(str);
56         if (!len)
57                 return 1;
58
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
70 static int check_str_bytes(const char *p, unsigned long long *val)
71 {
72         return str_to_decimal(p, val, 1);
73 }
74
75 static int check_str_time(const char *p, unsigned long long *val)
76 {
77         return str_to_decimal(p, val, 0);
78 }
79
80 void strip_blank_front(char **p)
81 {
82         char *s = *p;
83
84         while (isspace(*s))
85                 s++;
86 }
87
88 void 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
98 static int check_range_bytes(const char *str, unsigned long *val)
99 {
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
113 static int check_int(const char *p, unsigned int *val)
114 {
115         if (sscanf(p, "%u", val) == 1)
116                 return 0;
117
118         return 1;
119 }
120
121 static struct fio_option *find_option(struct fio_option *options,
122                                       const char *opt)
123 {
124         struct fio_option *o = &options[0];
125
126         while (o->name) {
127                 if (!strcmp(o->name, opt))
128                         return o;
129
130                 o++;
131         }
132
133         return NULL;
134 }
135
136 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
137                            int first)
138 {
139         unsigned int il, *ilp1, *ilp2;
140         unsigned long long ull, *ullp;
141         unsigned long ul1, ul2;
142         char **cp;
143         int ret = 0, is_time = 0;
144
145         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;
154         case FIO_OPT_STR_VAL:
155         case FIO_OPT_STR_VAL_INT: {
156                 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 {
172                         if (o->type == FIO_OPT_STR_VAL_INT) {
173                                 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                                 }
184                         } else {
185                                 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                                 }
196                         }
197                 }
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: {
205                 char tmp[128];
206                 char *p1, *p2;
207
208                 strncpy(tmp, ptr, sizeof(tmp) - 1);
209
210                 p1 = strchr(tmp, '-');
211                 if (!p1) {
212                         ret = 1;
213                         break;
214                 }
215
216                 p2 = p1 + 1;
217                 *p1 = '\0';
218                 p1 = tmp;
219
220                 ret = 1;
221                 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
222                         ret = 0;
223                         if (ul1 > ul2) {
224                                 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);
233                                 *ilp1 = ul1;
234                                 *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;
246                         }
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 {
264                         if (first || !o->off2)
265                                 ilp1 = td_var(data, o->off1);
266                         else
267                                 ilp1 = td_var(data, o->off2);
268
269                         *ilp1 = il;
270                 }
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 {
279                         if (first || !o->off2)
280                                 ilp1 = td_var(data, o->off1);
281                         else
282                                 ilp1 = td_var(data, o->off2);
283
284                         *ilp1 = 1;
285                 }
286                 break;
287         }
288         default:
289                 fprintf(stderr, "Bad option type %d\n", o->type);
290                 ret = 1;
291         }
292
293         return ret;
294 }
295
296 static 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
316 int 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
327         if (!handle_option(o, val, data))
328                 return 0;
329
330         fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
331         return 1;
332 }
333
334 int parse_option(const char *opt, struct fio_option *options, void *data)
335 {
336         struct fio_option *o;
337         char *pre, *post;
338         char tmp[64];
339
340         strncpy(tmp, opt, sizeof(tmp) - 1);
341
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
359         if (!handle_option(o, post, data))
360                 return 0;
361
362         fprintf(stderr, "fio: failed parsing %s\n", opt);
363         return 1;
364 }