Update io_u.c comments
[fio.git] / parse.c
CommitLineData
cb2c86fd
JA
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
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;
f3502ba2
JA
43 case 'e':
44 case 'E':
45 return 1024 * 1024 * 1024 * 1024UL;
cb2c86fd
JA
46 default:
47 return 1;
48 }
49}
50
51/*
e1f36503 52 * convert string into decimal value, noting any size suffix
cb2c86fd 53 */
63f29372 54static int str_to_decimal(const char *str, long long *val, int kilo)
cb2c86fd 55{
cb2c86fd
JA
56 int len;
57
cb2c86fd 58 len = strlen(str);
f90eff5a
JA
59 if (!len)
60 return 1;
cb2c86fd 61
675de85a 62 *val = strtoll(str, NULL, 10);
cda866ca 63 if (*val == LONG_MAX && errno == ERANGE)
cb2c86fd
JA
64 return 1;
65
66 if (kilo)
67 *val *= get_mult_bytes(str[len - 1]);
68 else
69 *val *= get_mult_time(str[len - 1]);
787f7e95 70
cb2c86fd
JA
71 return 0;
72}
73
63f29372 74static int check_str_bytes(const char *p, long long *val)
cb2c86fd 75{
cb2c86fd
JA
76 return str_to_decimal(p, val, 1);
77}
78
63f29372 79static int check_str_time(const char *p, long long *val)
cb2c86fd 80{
cb2c86fd
JA
81 return str_to_decimal(p, val, 0);
82}
83
84void strip_blank_front(char **p)
85{
86 char *s = *p;
87
88 while (isspace(*s))
89 s++;
90}
91
92void strip_blank_end(char *p)
93{
94 char *s = p + strlen(p) - 1;
95
96 while (isspace(*s) || iscntrl(*s))
97 s--;
98
99 *(s + 1) = '\0';
100}
101
63f29372 102static int check_range_bytes(const char *str, long *val)
cb2c86fd
JA
103{
104 char suffix;
105
787f7e95
JA
106 if (!strlen(str))
107 return 1;
108
cb2c86fd
JA
109 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
110 *val *= get_mult_bytes(suffix);
111 return 0;
112 }
113
114 if (sscanf(str, "%lu", val) == 1)
115 return 0;
116
117 return 1;
118}
119
63f29372 120static int check_int(const char *p, int *val)
cb2c86fd 121{
787f7e95
JA
122 if (!strlen(p))
123 return 1;
e1f36503
JA
124 if (sscanf(p, "%u", val) == 1)
125 return 0;
cb2c86fd 126
e1f36503
JA
127 return 1;
128}
cb2c86fd 129
e1f36503
JA
130static struct fio_option *find_option(struct fio_option *options,
131 const char *opt)
132{
4945ba12 133 struct fio_option *o;
cb2c86fd 134
4945ba12 135 for (o = &options[0]; o->name; o++) {
e1f36503
JA
136 if (!strcmp(o->name, opt))
137 return o;
03b74b3e
JA
138 else if (o->alias && !strcmp(o->alias, opt))
139 return o;
33963c6c 140 }
cb2c86fd 141
e1f36503 142 return NULL;
cb2c86fd
JA
143}
144
17abbe89
JA
145#define val_store(ptr, val, off, data) \
146 do { \
147 ptr = td_var((data), (off)); \
148 *ptr = (val); \
149 } while (0)
150
f90eff5a 151static int __handle_option(struct fio_option *o, const char *ptr, void *data,
787f7e95 152 int first, int more)
cb2c86fd 153{
63f29372
JA
154 int il, *ilp;
155 long long ull, *ullp;
156 long ul1, ul2;
b4692828 157 char **cp;
e1f36503 158 int ret = 0, is_time = 0;
cb2c86fd 159
08e26e35
JA
160 if (!ptr && o->type != FIO_OPT_STR_SET) {
161 fprintf(stderr, "Option %s requires an argument\n", o->name);
162 return 1;
163 }
164
e1f36503
JA
165 switch (o->type) {
166 case FIO_OPT_STR: {
167 fio_opt_str_fn *fn = o->cb;
cb2c86fd 168
e1f36503
JA
169 ret = fn(data, ptr);
170 break;
171 }
172 case FIO_OPT_STR_VAL_TIME:
173 is_time = 1;
75e6f36f
JA
174 case FIO_OPT_STR_VAL:
175 case FIO_OPT_STR_VAL_INT: {
e1f36503
JA
176 fio_opt_str_val_fn *fn = o->cb;
177
178 if (is_time)
179 ret = check_str_time(ptr, &ull);
180 else
181 ret = check_str_bytes(ptr, &ull);
182
183 if (ret)
184 break;
185
db8e0165
JA
186 if (o->maxval && ull > o->maxval) {
187 fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval);
188 return 1;
189 }
190 if (o->minval && ull < o->minval) {
191 fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval);
192 return 1;
193 }
e1f36503
JA
194
195 if (fn)
196 ret = fn(data, &ull);
197 else {
75e6f36f 198 if (o->type == FIO_OPT_STR_VAL_INT) {
17abbe89
JA
199 if (first)
200 val_store(ilp, ull, o->off1, data);
787f7e95 201 if (!more && o->off2)
17abbe89 202 val_store(ilp, ull, o->off2, data);
75e6f36f 203 } else {
17abbe89
JA
204 if (first)
205 val_store(ullp, ull, o->off1, data);
787f7e95 206 if (!more && o->off2)
17abbe89 207 val_store(ullp, ull, o->off2, data);
75e6f36f 208 }
e1f36503
JA
209 }
210 break;
211 }
212 case FIO_OPT_STR_STORE:
213 cp = td_var(data, o->off1);
214 *cp = strdup(ptr);
215 break;
216 case FIO_OPT_RANGE: {
b765a372 217 char tmp[128];
e1f36503
JA
218 char *p1, *p2;
219
0bbab0e7 220 strncpy(tmp, ptr, sizeof(tmp) - 1);
b765a372
JA
221
222 p1 = strchr(tmp, '-');
e1f36503 223 if (!p1) {
0c9baf91
JA
224 p1 = strchr(tmp, ':');
225 if (!p1) {
226 ret = 1;
227 break;
228 }
e1f36503
JA
229 }
230
231 p2 = p1 + 1;
232 *p1 = '\0';
b765a372 233 p1 = tmp;
e1f36503
JA
234
235 ret = 1;
236 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
237 ret = 0;
e1f36503 238 if (ul1 > ul2) {
f90eff5a
JA
239 unsigned long foo = ul1;
240
241 ul1 = ul2;
242 ul2 = foo;
243 }
244
245 if (first) {
17abbe89
JA
246 val_store(ilp, ul1, o->off1, data);
247 val_store(ilp, ul2, o->off2, data);
248 }
787f7e95 249 if (!more && o->off3 && o->off4) {
17abbe89
JA
250 val_store(ilp, ul1, o->off3, data);
251 val_store(ilp, ul2, o->off4, data);
e1f36503 252 }
17abbe89
JA
253 }
254
e1f36503
JA
255 break;
256 }
13335ddb
JA
257 case FIO_OPT_INT:
258 case FIO_OPT_BOOL: {
e1f36503
JA
259 fio_opt_int_fn *fn = o->cb;
260
261 ret = check_int(ptr, &il);
262 if (ret)
263 break;
264
db8e0165
JA
265 if (o->maxval && il > (int) o->maxval) {
266 fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval);
267 return 1;
268 }
269 if (o->minval && il < o->minval) {
270 fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval);
271 return 1;
272 }
e1f36503 273
76a43db4
JA
274 if (o->neg)
275 il = !il;
276
e1f36503
JA
277 if (fn)
278 ret = fn(data, &il);
279 else {
17abbe89
JA
280 if (first)
281 val_store(ilp, il, o->off1, data);
787f7e95 282 if (!more && o->off2)
17abbe89 283 val_store(ilp, il, o->off2, data);
e1f36503
JA
284 }
285 break;
286 }
287 case FIO_OPT_STR_SET: {
288 fio_opt_str_set_fn *fn = o->cb;
289
290 if (fn)
291 ret = fn(data);
292 else {
17abbe89
JA
293 if (first)
294 val_store(ilp, 1, o->off1, data);
787f7e95 295 if (!more && o->off2)
17abbe89 296 val_store(ilp, 1, o->off2, data);
e1f36503
JA
297 }
298 break;
299 }
300 default:
1e97cce9 301 fprintf(stderr, "Bad option type %u\n", o->type);
e1f36503
JA
302 ret = 1;
303 }
cb2c86fd 304
e1f36503 305 return ret;
cb2c86fd
JA
306}
307
f90eff5a
JA
308static int handle_option(struct fio_option *o, const char *ptr, void *data)
309{
92b586f8 310 const char *ptr2 = NULL;
787f7e95 311 int r1, r2;
f90eff5a
JA
312
313 /*
787f7e95
JA
314 * See if we have a second set of parameters, hidden after a comma.
315 * Do this before parsing the first round, to check if we should
316 * copy set 1 options to set 2.
f90eff5a 317 */
ed92ac0c 318 if (ptr && (o->type != FIO_OPT_STR_STORE)) {
92b586f8 319 ptr2 = strchr(ptr, ',');
0c9baf91
JA
320 if (!ptr2)
321 ptr2 = strchr(ptr, ':');
322 }
787f7e95
JA
323
324 /*
325 * Don't return early if parsing the first option fails - if
326 * we are doing multiple arguments, we can allow the first one
327 * being empty.
328 */
329 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
330
f90eff5a 331 if (!ptr2)
787f7e95 332 return r1;
f90eff5a
JA
333
334 ptr2++;
787f7e95
JA
335 r2 = __handle_option(o, ptr2, data, 0, 0);
336
337 return r1 && r2;
f90eff5a
JA
338}
339
b4692828
JA
340int parse_cmd_option(const char *opt, const char *val,
341 struct fio_option *options, void *data)
342{
343 struct fio_option *o;
344
345 o = find_option(options, opt);
346 if (!o) {
347 fprintf(stderr, "Bad option %s\n", opt);
348 return 1;
349 }
350
b1508cf9
JA
351 if (!handle_option(o, val, data))
352 return 0;
353
354 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
355 return 1;
b4692828
JA
356}
357
e1f36503 358int parse_option(const char *opt, struct fio_option *options, void *data)
cb2c86fd 359{
b4692828 360 struct fio_option *o;
e1f36503
JA
361 char *pre, *post;
362 char tmp[64];
363
0bbab0e7 364 strncpy(tmp, opt, sizeof(tmp) - 1);
e1f36503
JA
365
366 pre = strchr(tmp, '=');
367 if (pre) {
368 post = pre;
369 *pre = '\0';
370 pre = tmp;
371 post++;
372 o = find_option(options, pre);
373 } else {
374 o = find_option(options, tmp);
375 post = NULL;
376 }
cb2c86fd 377
e1f36503
JA
378 if (!o) {
379 fprintf(stderr, "Bad option %s\n", tmp);
380 return 1;
381 }
382
b1508cf9
JA
383 if (!handle_option(o, post, data))
384 return 0;
385
386 fprintf(stderr, "fio: failed parsing %s\n", opt);
387 return 1;
e1f36503 388}
fd28ca49 389
15f7918f
JA
390static void show_option_range(struct fio_option *o)
391{
392 if (!o->minval && !o->maxval)
393 return;
394
395 printf("%16s: min=%d, max=%d\n", "range", o->minval, o->maxval);
396}
397
398static void show_option_values(struct fio_option *o)
399{
400 const char *msg;
401 int i = 0;
402
403 if (!o->posval)
404 return;
405
406 do {
407 msg = o->posval[i];
408 if (!msg)
409 break;
410
411 if (!i)
412 printf("%16s: ", "valid values");
413
414 printf("%s,", msg);
415 i++;
416 } while (1);
417
418 if (i)
419 printf("\n");
420}
421
fd28ca49
JA
422int show_cmd_help(struct fio_option *options, const char *name)
423{
424 int show_all = !strcmp(name, "all");
fd28ca49
JA
425 const char *typehelp[] = {
426 "string (opt=bla)",
427 "string with possible k/m/g postfix (opt=4k)",
428 "string with range and postfix (opt=1k-4k)",
429 "string with time postfix (opt=10s)",
430 "string (opt=bla)",
431 "string with dual range (opt=1k-4k,4k-8k)",
432 "integer value (opt=100)",
13335ddb 433 "boolean value (opt=1)",
fd28ca49
JA
434 "no argument (opt)",
435 };
4945ba12 436 struct fio_option *o;
29fc6afe 437 int found = 0;
fd28ca49 438
4945ba12 439 for (o = &options[0]; o->name; o++) {
fd28ca49
JA
440 int match = !strcmp(name, o->name);
441
442 if (show_all || match) {
29fc6afe 443 found = 1;
facba0e5 444 printf("%16s: %s\n", o->name, o->help);
4945ba12
JA
445 if (show_all)
446 continue;
fd28ca49
JA
447 }
448
70df2f19
JA
449 if (!match)
450 continue;
451
4945ba12
JA
452 printf("%16s: %s\n", "type", typehelp[o->type]);
453 printf("%16s: %s\n", "default", o->def ? o->def : "no default");
454 show_option_range(o);
455 show_option_values(o);
fd28ca49
JA
456 }
457
29fc6afe
JA
458 if (found)
459 return 0;
fd28ca49 460
29fc6afe
JA
461 printf("No such command: %s\n", name);
462 return 1;
fd28ca49 463}
ee738499 464
13335ddb
JA
465/*
466 * Handle parsing of default parameters.
467 */
ee738499
JA
468void fill_default_options(void *data, struct fio_option *options)
469{
4945ba12 470 struct fio_option *o;
ee738499 471
4945ba12 472 for (o = &options[0]; o->name; o++)
ee738499
JA
473 if (o->def)
474 handle_option(o, o->def, data);
ee738499 475}
13335ddb
JA
476
477/*
478 * Sanitize the options structure. For now it just sets min/max for bool
5b0a8880 479 * values and whether both callback and offsets are given.
13335ddb
JA
480 */
481void options_init(struct fio_option *options)
482{
4945ba12 483 struct fio_option *o;
13335ddb 484
4945ba12 485 for (o = &options[0]; o->name; o++) {
13335ddb
JA
486 if (o->type == FIO_OPT_BOOL) {
487 o->minval = 0;
488 o->maxval = 1;
489 }
5b0a8880
JA
490 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
491 fprintf(stderr, "Option %s: both cb and offset given\n", o->name);
13335ddb
JA
492 }
493}