blob: 1cc55dc63ed8b1af03b9e81a51592311fdc748f3 [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]);
Jens Axboe787f7e92006-11-06 13:26:29 +010067
Jens Axboecb2c86f2006-10-26 13:48:34 +020068 return 0;
69}
70
Jens Axboeb4692822006-10-27 13:43:22 +020071static int check_str_bytes(const char *p, unsigned long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +020072{
Jens Axboecb2c86f2006-10-26 13:48:34 +020073 return str_to_decimal(p, val, 1);
74}
75
Jens Axboeb4692822006-10-27 13:43:22 +020076static int check_str_time(const char *p, unsigned long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +020077{
Jens Axboecb2c86f2006-10-26 13:48:34 +020078 return str_to_decimal(p, val, 0);
79}
80
81void strip_blank_front(char **p)
82{
83 char *s = *p;
84
85 while (isspace(*s))
86 s++;
87}
88
89void strip_blank_end(char *p)
90{
91 char *s = p + strlen(p) - 1;
92
93 while (isspace(*s) || iscntrl(*s))
94 s--;
95
96 *(s + 1) = '\0';
97}
98
Jens Axboeb765a372006-10-27 15:00:16 +020099static int check_range_bytes(const char *str, unsigned long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200100{
101 char suffix;
102
Jens Axboe787f7e92006-11-06 13:26:29 +0100103 if (!strlen(str))
104 return 1;
105
Jens Axboecb2c86f2006-10-26 13:48:34 +0200106 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
107 *val *= get_mult_bytes(suffix);
108 return 0;
109 }
110
111 if (sscanf(str, "%lu", val) == 1)
112 return 0;
113
114 return 1;
115}
116
Jens Axboeb4692822006-10-27 13:43:22 +0200117static int check_int(const char *p, unsigned int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200118{
Jens Axboe787f7e92006-11-06 13:26:29 +0100119 if (!strlen(p))
120 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200121 if (sscanf(p, "%u", val) == 1)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200122 return 0;
123
124 return 1;
125}
126
Jens Axboee1f36502006-10-27 10:54:08 +0200127static struct fio_option *find_option(struct fio_option *options,
128 const char *opt)
129{
Jens Axboe33963c62006-10-27 11:33:01 +0200130 struct fio_option *o = &options[0];
Jens Axboee1f36502006-10-27 10:54:08 +0200131
Jens Axboe33963c62006-10-27 11:33:01 +0200132 while (o->name) {
Jens Axboee1f36502006-10-27 10:54:08 +0200133 if (!strcmp(o->name, opt))
134 return o;
135
Jens Axboe33963c62006-10-27 11:33:01 +0200136 o++;
137 }
Jens Axboee1f36502006-10-27 10:54:08 +0200138
139 return NULL;
140}
141
Jens Axboe17abbe82006-11-06 11:23:10 +0100142#define val_store(ptr, val, off, data) \
143 do { \
144 ptr = td_var((data), (off)); \
145 *ptr = (val); \
146 } while (0)
147
Jens Axboef90eff52006-11-06 11:08:21 +0100148static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100149 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200150{
Jens Axboe17abbe82006-11-06 11:23:10 +0100151 unsigned int il, *ilp;
Jens Axboee1f36502006-10-27 10:54:08 +0200152 unsigned long long ull, *ullp;
Jens Axboe75e6f362006-11-03 08:17:09 +0100153 unsigned long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200154 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200155 int ret = 0, is_time = 0;
156
Jens Axboe08e26e32006-11-21 13:15:10 +0100157 if (!ptr && o->type != FIO_OPT_STR_SET) {
158 fprintf(stderr, "Option %s requires an argument\n", o->name);
159 return 1;
160 }
161
Jens Axboee1f36502006-10-27 10:54:08 +0200162 switch (o->type) {
163 case FIO_OPT_STR: {
164 fio_opt_str_fn *fn = o->cb;
165
166 ret = fn(data, ptr);
167 break;
168 }
169 case FIO_OPT_STR_VAL_TIME:
170 is_time = 1;
Jens Axboe75e6f362006-11-03 08:17:09 +0100171 case FIO_OPT_STR_VAL:
172 case FIO_OPT_STR_VAL_INT: {
Jens Axboee1f36502006-10-27 10:54:08 +0200173 fio_opt_str_val_fn *fn = o->cb;
174
175 if (is_time)
176 ret = check_str_time(ptr, &ull);
177 else
178 ret = check_str_bytes(ptr, &ull);
179
180 if (ret)
181 break;
182
Jens Axboeee738492007-01-10 11:23:16 +0100183 if (o->maxval && ull > o->maxval)
184 ull = o->maxval;
185 if (o->minval && ull < o->minval)
186 ull = o->minval;
Jens Axboee1f36502006-10-27 10:54:08 +0200187
188 if (fn)
189 ret = fn(data, &ull);
190 else {
Jens Axboe75e6f362006-11-03 08:17:09 +0100191 if (o->type == FIO_OPT_STR_VAL_INT) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100192 if (first)
193 val_store(ilp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100194 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100195 val_store(ilp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100196 } else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100197 if (first)
198 val_store(ullp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100199 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100200 val_store(ullp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100201 }
Jens Axboee1f36502006-10-27 10:54:08 +0200202 }
203 break;
204 }
205 case FIO_OPT_STR_STORE:
206 cp = td_var(data, o->off1);
207 *cp = strdup(ptr);
208 break;
209 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200210 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200211 char *p1, *p2;
212
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100213 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200214
215 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200216 if (!p1) {
217 ret = 1;
218 break;
219 }
220
221 p2 = p1 + 1;
222 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200223 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200224
225 ret = 1;
226 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
227 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200228 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100229 unsigned long foo = ul1;
230
231 ul1 = ul2;
232 ul2 = foo;
233 }
234
235 if (first) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100236 val_store(ilp, ul1, o->off1, data);
237 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200238 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100239 if (!more && o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100240 val_store(ilp, ul1, o->off3, data);
241 val_store(ilp, ul2, o->off4, data);
242 }
243 }
244
Jens Axboee1f36502006-10-27 10:54:08 +0200245 break;
246 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100247 case FIO_OPT_INT:
248 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200249 fio_opt_int_fn *fn = o->cb;
250
251 ret = check_int(ptr, &il);
252 if (ret)
253 break;
254
Jens Axboeee738492007-01-10 11:23:16 +0100255 if (o->maxval && il > o->maxval)
256 il = o->maxval;
257 if (o->minval && il < o->minval)
258 il = o->minval;
Jens Axboee1f36502006-10-27 10:54:08 +0200259
260 if (fn)
261 ret = fn(data, &il);
262 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100263 if (first)
264 val_store(ilp, il, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100265 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100266 val_store(ilp, il, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200267 }
268 break;
269 }
270 case FIO_OPT_STR_SET: {
271 fio_opt_str_set_fn *fn = o->cb;
272
273 if (fn)
274 ret = fn(data);
275 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100276 if (first)
277 val_store(ilp, 1, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100278 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100279 val_store(ilp, 1, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200280 }
281 break;
282 }
283 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100284 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200285 ret = 1;
286 }
287
Jens Axboee1f36502006-10-27 10:54:08 +0200288 return ret;
289}
290
Jens Axboef90eff52006-11-06 11:08:21 +0100291static int handle_option(struct fio_option *o, const char *ptr, void *data)
292{
Jens Axboe92b586f2006-11-07 14:29:17 +0100293 const char *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100294 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100295
296 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100297 * See if we have a second set of parameters, hidden after a comma.
298 * Do this before parsing the first round, to check if we should
299 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100300 */
Jens Axboe92b586f2006-11-07 14:29:17 +0100301 if (ptr)
302 ptr2 = strchr(ptr, ',');
Jens Axboe787f7e92006-11-06 13:26:29 +0100303
304 /*
305 * Don't return early if parsing the first option fails - if
306 * we are doing multiple arguments, we can allow the first one
307 * being empty.
308 */
309 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
310
Jens Axboef90eff52006-11-06 11:08:21 +0100311 if (!ptr2)
Jens Axboe787f7e92006-11-06 13:26:29 +0100312 return r1;
Jens Axboef90eff52006-11-06 11:08:21 +0100313
314 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100315 r2 = __handle_option(o, ptr2, data, 0, 0);
316
317 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100318}
319
Jens Axboeb4692822006-10-27 13:43:22 +0200320int parse_cmd_option(const char *opt, const char *val,
321 struct fio_option *options, void *data)
322{
323 struct fio_option *o;
324
325 o = find_option(options, opt);
326 if (!o) {
327 fprintf(stderr, "Bad option %s\n", opt);
328 return 1;
329 }
330
Jens Axboeb1508cf2006-11-02 09:12:40 +0100331 if (!handle_option(o, val, data))
332 return 0;
333
334 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
335 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200336}
337
Jens Axboee1f36502006-10-27 10:54:08 +0200338int parse_option(const char *opt, struct fio_option *options, void *data)
339{
Jens Axboeb4692822006-10-27 13:43:22 +0200340 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200341 char *pre, *post;
342 char tmp[64];
343
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100344 strncpy(tmp, opt, sizeof(tmp) - 1);
Jens Axboee1f36502006-10-27 10:54:08 +0200345
346 pre = strchr(tmp, '=');
347 if (pre) {
348 post = pre;
349 *pre = '\0';
350 pre = tmp;
351 post++;
352 o = find_option(options, pre);
353 } else {
354 o = find_option(options, tmp);
355 post = NULL;
356 }
357
358 if (!o) {
359 fprintf(stderr, "Bad option %s\n", tmp);
360 return 1;
361 }
362
Jens Axboeb1508cf2006-11-02 09:12:40 +0100363 if (!handle_option(o, post, data))
364 return 0;
365
366 fprintf(stderr, "fio: failed parsing %s\n", opt);
367 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200368}
Jens Axboefd28ca42007-01-09 21:20:13 +0100369
Jens Axboe15f79182007-01-10 12:26:18 +0100370static void show_option_range(struct fio_option *o)
371{
372 if (!o->minval && !o->maxval)
373 return;
374
375 printf("%16s: min=%d, max=%d\n", "range", o->minval, o->maxval);
376}
377
378static void show_option_values(struct fio_option *o)
379{
380 const char *msg;
381 int i = 0;
382
383 if (!o->posval)
384 return;
385
386 do {
387 msg = o->posval[i];
388 if (!msg)
389 break;
390
391 if (!i)
392 printf("%16s: ", "valid values");
393
394 printf("%s,", msg);
395 i++;
396 } while (1);
397
398 if (i)
399 printf("\n");
400}
401
Jens Axboefd28ca42007-01-09 21:20:13 +0100402int show_cmd_help(struct fio_option *options, const char *name)
403{
404 int show_all = !strcmp(name, "all");
405 struct fio_option *o = &options[0];
406 const char *typehelp[] = {
407 "string (opt=bla)",
408 "string with possible k/m/g postfix (opt=4k)",
409 "string with range and postfix (opt=1k-4k)",
410 "string with time postfix (opt=10s)",
411 "string (opt=bla)",
412 "string with dual range (opt=1k-4k,4k-8k)",
413 "integer value (opt=100)",
Jens Axboe13335dd2007-01-10 13:14:02 +0100414 "boolean value (opt=1)",
Jens Axboefd28ca42007-01-09 21:20:13 +0100415 "no argument (opt)",
416 };
Jens Axboe29fc6af2007-01-09 21:22:02 +0100417 int found = 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100418
419 while (o->name) {
420 int match = !strcmp(name, o->name);
421
422 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100423 found = 1;
Jens Axboefacba0e2007-01-10 11:41:11 +0100424 printf("%16s: %s\n", o->name, o->help);
Jens Axboeee738492007-01-10 11:23:16 +0100425 if (match) {
Jens Axboefacba0e2007-01-10 11:41:11 +0100426 printf("%16s: %s\n", "type", typehelp[o->type]);
427 printf("%16s: %s\n", "default", o->def ? o->def : "no default");
Jens Axboe15f79182007-01-10 12:26:18 +0100428 show_option_range(o);
429 show_option_values(o);
Jens Axboeee738492007-01-10 11:23:16 +0100430 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100431 }
432
433 o++;
434 }
435
Jens Axboe29fc6af2007-01-09 21:22:02 +0100436 if (found)
437 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100438
Jens Axboe29fc6af2007-01-09 21:22:02 +0100439 printf("No such command: %s\n", name);
440 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100441}
Jens Axboeee738492007-01-10 11:23:16 +0100442
Jens Axboe13335dd2007-01-10 13:14:02 +0100443/*
444 * Handle parsing of default parameters.
445 */
Jens Axboeee738492007-01-10 11:23:16 +0100446void fill_default_options(void *data, struct fio_option *options)
447{
448 struct fio_option *o = &options[0];
449
450 while (o->name) {
451 if (o->def)
452 handle_option(o, o->def, data);
453 o++;
454 }
455}
Jens Axboe13335dd2007-01-10 13:14:02 +0100456
457/*
458 * Sanitize the options structure. For now it just sets min/max for bool
459 * values.
460 */
461void options_init(struct fio_option *options)
462{
463 struct fio_option *o = &options[0];
464
465 while (o->name) {
466 if (o->type == FIO_OPT_BOOL) {
467 o->minval = 0;
468 o->maxval = 1;
469 }
470 o++;
471 }
472}