blob: 48703b6938a3af666b38e3c8f3ed6c9feaa2b94b [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);
56
57 *val = strtoul(str, NULL, 10);
58 if (*val == ULONG_MAX && errno == ERANGE)
59 return 1;
60
61 if (kilo)
62 *val *= get_mult_bytes(str[len - 1]);
63 else
64 *val *= get_mult_time(str[len - 1]);
65 return 0;
66}
67
Jens Axboeb4692822006-10-27 13:43:22 +020068static int check_str_bytes(const char *p, unsigned long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +020069{
Jens Axboecb2c86f2006-10-26 13:48:34 +020070 return str_to_decimal(p, val, 1);
71}
72
Jens Axboeb4692822006-10-27 13:43:22 +020073static int check_str_time(const char *p, unsigned long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +020074{
Jens Axboecb2c86f2006-10-26 13:48:34 +020075 return str_to_decimal(p, val, 0);
76}
77
78void strip_blank_front(char **p)
79{
80 char *s = *p;
81
82 while (isspace(*s))
83 s++;
84}
85
86void strip_blank_end(char *p)
87{
88 char *s = p + strlen(p) - 1;
89
90 while (isspace(*s) || iscntrl(*s))
91 s--;
92
93 *(s + 1) = '\0';
94}
95
Jens Axboee1f36502006-10-27 10:54:08 +020096static int check_range_bytes(char *str, unsigned long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +020097{
98 char suffix;
99
100 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
101 *val *= get_mult_bytes(suffix);
102 return 0;
103 }
104
105 if (sscanf(str, "%lu", val) == 1)
106 return 0;
107
108 return 1;
109}
110
Jens Axboeb4692822006-10-27 13:43:22 +0200111static int check_int(const char *p, unsigned int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200112{
Jens Axboee1f36502006-10-27 10:54:08 +0200113 if (sscanf(p, "%u", val) == 1)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200114 return 0;
115
116 return 1;
117}
118
Jens Axboee1f36502006-10-27 10:54:08 +0200119static struct fio_option *find_option(struct fio_option *options,
120 const char *opt)
121{
Jens Axboe33963c62006-10-27 11:33:01 +0200122 struct fio_option *o = &options[0];
Jens Axboee1f36502006-10-27 10:54:08 +0200123
Jens Axboe33963c62006-10-27 11:33:01 +0200124 while (o->name) {
Jens Axboee1f36502006-10-27 10:54:08 +0200125 if (!strcmp(o->name, opt))
126 return o;
127
Jens Axboe33963c62006-10-27 11:33:01 +0200128 o++;
129 }
Jens Axboee1f36502006-10-27 10:54:08 +0200130
131 return NULL;
132}
133
Jens Axboeb4692822006-10-27 13:43:22 +0200134static int handle_option(struct fio_option *o, const char *ptr, void *data)
Jens Axboee1f36502006-10-27 10:54:08 +0200135{
136 unsigned int il, *ilp;
137 unsigned long long ull, *ullp;
138 unsigned long ul1, ul2, *ulp1, *ulp2;
Jens Axboeb4692822006-10-27 13:43:22 +0200139 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200140 int ret = 0, is_time = 0;
141
Jens Axboee1f36502006-10-27 10:54:08 +0200142 switch (o->type) {
143 case FIO_OPT_STR: {
144 fio_opt_str_fn *fn = o->cb;
145
146 ret = fn(data, ptr);
147 break;
148 }
149 case FIO_OPT_STR_VAL_TIME:
150 is_time = 1;
151 case FIO_OPT_STR_VAL: {
152 fio_opt_str_val_fn *fn = o->cb;
153
154 if (is_time)
155 ret = check_str_time(ptr, &ull);
156 else
157 ret = check_str_bytes(ptr, &ull);
158
159 if (ret)
160 break;
161
162 if (o->max_val && ull > o->max_val)
163 ull = o->max_val;
164
165 if (fn)
166 ret = fn(data, &ull);
167 else {
168 ullp = td_var(data, o->off1);
169 *ullp = ull;
170 }
171 break;
172 }
173 case FIO_OPT_STR_STORE:
174 cp = td_var(data, o->off1);
175 *cp = strdup(ptr);
176 break;
177 case FIO_OPT_RANGE: {
178 char *p1, *p2;
179
180 p1 = strchr(ptr, '-');
181 if (!p1) {
182 ret = 1;
183 break;
184 }
185
186 p2 = p1 + 1;
187 *p1 = '\0';
188
189 ret = 1;
190 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
191 ret = 0;
192 ulp1 = td_var(data, o->off1);
193 ulp2 = td_var(data, o->off2);
194 if (ul1 > ul2) {
195 *ulp1 = ul2;
196 *ulp2 = ul1;
197 } else {
198 *ulp2 = ul2;
199 *ulp1 = ul1;
200 }
201 }
202
203 break;
204 }
205 case FIO_OPT_INT: {
206 fio_opt_int_fn *fn = o->cb;
207
208 ret = check_int(ptr, &il);
209 if (ret)
210 break;
211
212 if (o->max_val && il > o->max_val)
213 il = o->max_val;
214
215 if (fn)
216 ret = fn(data, &il);
217 else {
218 ilp = td_var(data, o->off1);
219 *ilp = il;
220 }
221 break;
222 }
223 case FIO_OPT_STR_SET: {
224 fio_opt_str_set_fn *fn = o->cb;
225
226 if (fn)
227 ret = fn(data);
228 else {
229 ilp = td_var(data, o->off1);
230 *ilp = 1;
231 }
232 break;
233 }
234 default:
235 fprintf(stderr, "Bad option type %d\n", o->type);
236 ret = 1;
237 }
238
Jens Axboee1f36502006-10-27 10:54:08 +0200239 return ret;
240}
241
Jens Axboeb4692822006-10-27 13:43:22 +0200242int parse_cmd_option(const char *opt, const char *val,
243 struct fio_option *options, void *data)
244{
245 struct fio_option *o;
246
247 o = find_option(options, opt);
248 if (!o) {
249 fprintf(stderr, "Bad option %s\n", opt);
250 return 1;
251 }
252
253 return handle_option(o, val, data);
254}
255
Jens Axboee1f36502006-10-27 10:54:08 +0200256int parse_option(const char *opt, struct fio_option *options, void *data)
257{
Jens Axboeb4692822006-10-27 13:43:22 +0200258 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200259 char *pre, *post;
260 char tmp[64];
261
262 strcpy(tmp, opt);
263
264 pre = strchr(tmp, '=');
265 if (pre) {
266 post = pre;
267 *pre = '\0';
268 pre = tmp;
269 post++;
270 o = find_option(options, pre);
271 } else {
272 o = find_option(options, tmp);
273 post = NULL;
274 }
275
276 if (!o) {
277 fprintf(stderr, "Bad option %s\n", tmp);
278 return 1;
279 }
280
281 return handle_option(o, post, data);
282}