blob: 7fb2bb6399cc564b038f6889a5f2fb3f603b5719 [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;
Jens Axboef3502ba2007-02-14 01:27:09 +010043 case 'e':
44 case 'E':
45 return 1024 * 1024 * 1024 * 1024UL;
Jens Axboecb2c86f2006-10-26 13:48:34 +020046 default:
47 return 1;
48 }
49}
50
51/*
Jens Axboee1f36502006-10-27 10:54:08 +020052 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +020053 */
Jens Axboe63f29372007-01-10 13:20:09 +010054static int str_to_decimal(const char *str, long long *val, int kilo)
Jens Axboecb2c86f2006-10-26 13:48:34 +020055{
Jens Axboecb2c86f2006-10-26 13:48:34 +020056 int len;
57
Jens Axboecb2c86f2006-10-26 13:48:34 +020058 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +010059 if (!len)
60 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +020061
Jens Axboe675de852007-02-10 19:01:07 +010062 *val = strtoll(str, NULL, 10);
Jens Axboecda866c2007-01-10 16:02:32 +010063 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +020064 return 1;
65
66 if (kilo)
67 *val *= get_mult_bytes(str[len - 1]);
68 else
69 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +010070
Jens Axboecb2c86f2006-10-26 13:48:34 +020071 return 0;
72}
73
Jens Axboe63f29372007-01-10 13:20:09 +010074static int check_str_bytes(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +020075{
Jens Axboecb2c86f2006-10-26 13:48:34 +020076 return str_to_decimal(p, val, 1);
77}
78
Jens Axboe63f29372007-01-10 13:20:09 +010079static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +020080{
Jens Axboecb2c86f2006-10-26 13:48:34 +020081 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
Jens Axboe63f29372007-01-10 13:20:09 +0100102static int check_range_bytes(const char *str, long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200103{
104 char suffix;
105
Jens Axboe787f7e92006-11-06 13:26:29 +0100106 if (!strlen(str))
107 return 1;
108
Jens Axboecb2c86f2006-10-26 13:48:34 +0200109 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
Jens Axboe63f29372007-01-10 13:20:09 +0100120static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200121{
Jens Axboe787f7e92006-11-06 13:26:29 +0100122 if (!strlen(p))
123 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200124 if (sscanf(p, "%u", val) == 1)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200125 return 0;
126
127 return 1;
128}
129
Jens Axboee1f36502006-10-27 10:54:08 +0200130static struct fio_option *find_option(struct fio_option *options,
131 const char *opt)
132{
Jens Axboe4945ba12007-01-11 14:36:56 +0100133 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200134
Jens Axboe4945ba12007-01-11 14:36:56 +0100135 for (o = &options[0]; o->name; o++) {
Jens Axboee1f36502006-10-27 10:54:08 +0200136 if (!strcmp(o->name, opt))
137 return o;
Jens Axboe03b74b32007-01-11 11:04:31 +0100138 else if (o->alias && !strcmp(o->alias, opt))
139 return o;
Jens Axboe33963c62006-10-27 11:33:01 +0200140 }
Jens Axboee1f36502006-10-27 10:54:08 +0200141
142 return NULL;
143}
144
Jens Axboe17abbe82006-11-06 11:23:10 +0100145#define val_store(ptr, val, off, data) \
146 do { \
147 ptr = td_var((data), (off)); \
148 *ptr = (val); \
149 } while (0)
150
Jens Axboef90eff52006-11-06 11:08:21 +0100151static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100152 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200153{
Jens Axboe63f29372007-01-10 13:20:09 +0100154 int il, *ilp;
155 long long ull, *ullp;
156 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200157 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200158 int ret = 0, is_time = 0;
159
Jens Axboe08e26e32006-11-21 13:15:10 +0100160 if (!ptr && o->type != FIO_OPT_STR_SET) {
161 fprintf(stderr, "Option %s requires an argument\n", o->name);
162 return 1;
163 }
164
Jens Axboee1f36502006-10-27 10:54:08 +0200165 switch (o->type) {
166 case FIO_OPT_STR: {
167 fio_opt_str_fn *fn = o->cb;
168
169 ret = fn(data, ptr);
170 break;
171 }
172 case FIO_OPT_STR_VAL_TIME:
173 is_time = 1;
Jens Axboe75e6f362006-11-03 08:17:09 +0100174 case FIO_OPT_STR_VAL:
175 case FIO_OPT_STR_VAL_INT: {
Jens Axboee1f36502006-10-27 10:54:08 +0200176 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
Jens Axboedb8e0162007-01-10 13:41:26 +0100186 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 }
Jens Axboee1f36502006-10-27 10:54:08 +0200194
195 if (fn)
196 ret = fn(data, &ull);
197 else {
Jens Axboe75e6f362006-11-03 08:17:09 +0100198 if (o->type == FIO_OPT_STR_VAL_INT) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100199 if (first)
200 val_store(ilp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100201 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100202 val_store(ilp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100203 } else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100204 if (first)
205 val_store(ullp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100206 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100207 val_store(ullp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100208 }
Jens Axboee1f36502006-10-27 10:54:08 +0200209 }
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: {
Jens Axboeb765a372006-10-27 15:00:16 +0200217 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200218 char *p1, *p2;
219
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100220 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200221
222 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200223 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100224 p1 = strchr(tmp, ':');
225 if (!p1) {
226 ret = 1;
227 break;
228 }
Jens Axboee1f36502006-10-27 10:54:08 +0200229 }
230
231 p2 = p1 + 1;
232 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200233 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200234
235 ret = 1;
236 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
237 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200238 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100239 unsigned long foo = ul1;
240
241 ul1 = ul2;
242 ul2 = foo;
243 }
244
245 if (first) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100246 val_store(ilp, ul1, o->off1, data);
247 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200248 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100249 if (!more && o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100250 val_store(ilp, ul1, o->off3, data);
251 val_store(ilp, ul2, o->off4, data);
252 }
253 }
254
Jens Axboee1f36502006-10-27 10:54:08 +0200255 break;
256 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100257 case FIO_OPT_INT:
258 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200259 fio_opt_int_fn *fn = o->cb;
260
261 ret = check_int(ptr, &il);
262 if (ret)
263 break;
264
Jens Axboedb8e0162007-01-10 13:41:26 +0100265 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 }
Jens Axboee1f36502006-10-27 10:54:08 +0200273
Jens Axboe76a43db2007-01-11 13:24:44 +0100274 if (o->neg)
275 il = !il;
276
Jens Axboee1f36502006-10-27 10:54:08 +0200277 if (fn)
278 ret = fn(data, &il);
279 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100280 if (first)
281 val_store(ilp, il, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100282 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100283 val_store(ilp, il, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200284 }
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 {
Jens Axboe17abbe82006-11-06 11:23:10 +0100293 if (first)
294 val_store(ilp, 1, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100295 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100296 val_store(ilp, 1, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200297 }
298 break;
299 }
300 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100301 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200302 ret = 1;
303 }
304
Jens Axboee1f36502006-10-27 10:54:08 +0200305 return ret;
306}
307
Jens Axboef90eff52006-11-06 11:08:21 +0100308static int handle_option(struct fio_option *o, const char *ptr, void *data)
309{
Jens Axboe92b586f2006-11-07 14:29:17 +0100310 const char *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100311 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100312
313 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100314 * 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.
Jens Axboef90eff52006-11-06 11:08:21 +0100317 */
Jens Axboeed92ac02007-02-06 14:43:52 +0100318 if (ptr && (o->type != FIO_OPT_STR_STORE)) {
Jens Axboe92b586f2006-11-07 14:29:17 +0100319 ptr2 = strchr(ptr, ',');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100320 if (!ptr2)
321 ptr2 = strchr(ptr, ':');
322 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100323
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
Jens Axboef90eff52006-11-06 11:08:21 +0100331 if (!ptr2)
Jens Axboe787f7e92006-11-06 13:26:29 +0100332 return r1;
Jens Axboef90eff52006-11-06 11:08:21 +0100333
334 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100335 r2 = __handle_option(o, ptr2, data, 0, 0);
336
337 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100338}
339
Jens Axboeb4692822006-10-27 13:43:22 +0200340int 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
Jens Axboeb1508cf2006-11-02 09:12:40 +0100351 if (!handle_option(o, val, data))
352 return 0;
353
354 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
355 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200356}
357
Jens Axboee1f36502006-10-27 10:54:08 +0200358int parse_option(const char *opt, struct fio_option *options, void *data)
359{
Jens Axboeb4692822006-10-27 13:43:22 +0200360 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200361 char *pre, *post;
362 char tmp[64];
363
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100364 strncpy(tmp, opt, sizeof(tmp) - 1);
Jens Axboee1f36502006-10-27 10:54:08 +0200365
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 }
377
378 if (!o) {
379 fprintf(stderr, "Bad option %s\n", tmp);
380 return 1;
381 }
382
Jens Axboeb1508cf2006-11-02 09:12:40 +0100383 if (!handle_option(o, post, data))
384 return 0;
385
386 fprintf(stderr, "fio: failed parsing %s\n", opt);
387 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200388}
Jens Axboefd28ca42007-01-09 21:20:13 +0100389
Jens Axboe15f79182007-01-10 12:26:18 +0100390static 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
Jens Axboefd28ca42007-01-09 21:20:13 +0100422int show_cmd_help(struct fio_option *options, const char *name)
423{
424 int show_all = !strcmp(name, "all");
Jens Axboefd28ca42007-01-09 21:20:13 +0100425 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)",
Jens Axboe13335dd2007-01-10 13:14:02 +0100433 "boolean value (opt=1)",
Jens Axboefd28ca42007-01-09 21:20:13 +0100434 "no argument (opt)",
435 };
Jens Axboe4945ba12007-01-11 14:36:56 +0100436 struct fio_option *o;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100437 int found = 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100438
Jens Axboe4945ba12007-01-11 14:36:56 +0100439 for (o = &options[0]; o->name; o++) {
Jens Axboefd28ca42007-01-09 21:20:13 +0100440 int match = !strcmp(name, o->name);
441
442 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100443 found = 1;
Jens Axboefacba0e2007-01-10 11:41:11 +0100444 printf("%16s: %s\n", o->name, o->help);
Jens Axboe4945ba12007-01-11 14:36:56 +0100445 if (show_all)
446 continue;
Jens Axboefd28ca42007-01-09 21:20:13 +0100447 }
448
Jens Axboe70df2f12007-01-11 14:04:47 +0100449 if (!match)
450 continue;
451
Jens Axboe4945ba12007-01-11 14:36:56 +0100452 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);
Jens Axboefd28ca42007-01-09 21:20:13 +0100456 }
457
Jens Axboe29fc6af2007-01-09 21:22:02 +0100458 if (found)
459 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100460
Jens Axboe29fc6af2007-01-09 21:22:02 +0100461 printf("No such command: %s\n", name);
462 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100463}
Jens Axboeee738492007-01-10 11:23:16 +0100464
Jens Axboe13335dd2007-01-10 13:14:02 +0100465/*
466 * Handle parsing of default parameters.
467 */
Jens Axboeee738492007-01-10 11:23:16 +0100468void fill_default_options(void *data, struct fio_option *options)
469{
Jens Axboe4945ba12007-01-11 14:36:56 +0100470 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100471
Jens Axboe4945ba12007-01-11 14:36:56 +0100472 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100473 if (o->def)
474 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100475}
Jens Axboe13335dd2007-01-10 13:14:02 +0100476
477/*
478 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100479 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100480 */
481void options_init(struct fio_option *options)
482{
Jens Axboe4945ba12007-01-11 14:36:56 +0100483 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100484
Jens Axboe4945ba12007-01-11 14:36:56 +0100485 for (o = &options[0]; o->name; o++) {
Jens Axboe13335dd2007-01-10 13:14:02 +0100486 if (o->type == FIO_OPT_BOOL) {
487 o->minval = 0;
488 o->maxval = 1;
489 }
Jens Axboe5b0a8882007-01-11 14:40:27 +0100490 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
491 fprintf(stderr, "Option %s: both cb and offset given\n", o->name);
Jens Axboe13335dd2007-01-10 13:14:02 +0100492 }
493}