blob: 29f444b40ba1d6589a2d1a9813ab0bfb058d73f1 [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>
Aaron Carroll88b5a392008-10-07 11:25:20 +020011#include <stdlib.h>
Jens Axboecb2c86f2006-10-26 13:48:34 +020012
13#include "parse.h"
Jens Axboea3d741f2008-02-27 18:32:33 +010014#include "debug.h"
Jens Axboecb2c86f2006-10-26 13:48:34 +020015
Jens Axboe3b8b7132008-06-10 19:46:23 +020016static struct fio_option *fio_options;
17
Jens Axboef0857372007-03-19 13:15:23 +010018static int vp_cmp(const void *p1, const void *p2)
19{
20 const struct value_pair *vp1 = p1;
21 const struct value_pair *vp2 = p2;
22
23 return strlen(vp2->ival) - strlen(vp1->ival);
24}
25
26static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
27{
28 const struct value_pair *vp;
29 int entries;
30
31 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
32
33 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
34 vp = &o->posval[entries];
35 if (!vp->ival || vp->ival[0] == '\0')
36 break;
37
38 memcpy(&vpmap[entries], vp, sizeof(*vp));
39 }
40
41 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
42}
43
Jens Axboed447a8c2009-07-17 22:26:15 +020044static void show_option_range(struct fio_option *o, FILE *out)
Jens Axboeb1ec1da2007-02-23 10:29:16 +010045{
46 if (!o->minval && !o->maxval)
47 return;
48
Jens Axboed447a8c2009-07-17 22:26:15 +020049 fprintf(out, "%20s: min=%d", "range", o->minval);
Jens Axboe39801272009-07-01 09:06:12 +020050 if (o->maxval)
Jens Axboed447a8c2009-07-17 22:26:15 +020051 fprintf(out, ", max=%d", o->maxval);
52 fprintf(out, "\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010053}
54
55static void show_option_values(struct fio_option *o)
56{
Jens Axboeb1ec1da2007-02-23 10:29:16 +010057 int i = 0;
58
59 do {
Jens Axboe78372132007-03-14 13:24:07 +010060 const struct value_pair *vp = &o->posval[i];
61
62 if (!vp->ival)
Jens Axboeb1ec1da2007-02-23 10:29:16 +010063 break;
64
Jens Axboe78372132007-03-14 13:24:07 +010065 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
66 if (vp->help)
67 printf(" %s", vp->help);
68 printf("\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010069 i++;
70 } while (i < PARSE_MAX_VP);
71
72 if (i)
73 printf("\n");
74}
75
Jens Axboed447a8c2009-07-17 22:26:15 +020076static void show_option_help(struct fio_option *o, FILE *out)
77{
78 const char *typehelp[] = {
79 "string (opt=bla)",
80 "string with possible k/m/g postfix (opt=4k)",
81 "string with time postfix (opt=10s)",
82 "string (opt=bla)",
83 "string with dual range (opt=1k-4k,4k-8k)",
84 "integer value (opt=100)",
85 "boolean value (opt=1)",
86 "no argument (opt)",
87 };
88
89 if (o->alias)
90 fprintf(out, "%20s: %s\n", "alias", o->alias);
91
92 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
93 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
94 show_option_range(o, stdout);
95 show_option_values(o);
96}
97
Jens Axboecb2c86f2006-10-26 13:48:34 +020098static unsigned long get_mult_time(char c)
99{
100 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100101 case 'm':
102 case 'M':
103 return 60;
104 case 'h':
105 case 'H':
106 return 60 * 60;
107 case 'd':
108 case 'D':
109 return 24 * 60 * 60;
110 default:
111 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200112 }
113}
114
115static unsigned long get_mult_bytes(char c)
116{
117 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100118 case 'k':
119 case 'K':
120 return 1024;
121 case 'm':
122 case 'M':
123 return 1024 * 1024;
124 case 'g':
125 case 'G':
126 return 1024 * 1024 * 1024;
127 case 'e':
128 case 'E':
129 return 1024 * 1024 * 1024 * 1024UL;
130 default:
131 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200132 }
133}
134
135/*
Jens Axboee1f36502006-10-27 10:54:08 +0200136 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200137 */
Jens Axboe564ca972007-12-14 12:21:19 +0100138int str_to_decimal(const char *str, long long *val, int kilo)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200139{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100140 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200141
Jens Axboecb2c86f2006-10-26 13:48:34 +0200142 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100143 if (!len)
144 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200145
Jens Axboeb347f9d2009-03-09 14:15:21 +0100146 if (strstr(str, "0x") || strstr(str, "0X"))
147 base = 16;
148 else
149 base = 10;
150
151 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100152 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200153 return 1;
154
155 if (kilo)
156 *val *= get_mult_bytes(str[len - 1]);
157 else
158 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100159
Jens Axboecb2c86f2006-10-26 13:48:34 +0200160 return 0;
161}
162
Jens Axboe63f29372007-01-10 13:20:09 +0100163static int check_str_bytes(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200164{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200165 return str_to_decimal(p, val, 1);
166}
167
Jens Axboe63f29372007-01-10 13:20:09 +0100168static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200169{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200170 return str_to_decimal(p, val, 0);
171}
172
173void strip_blank_front(char **p)
174{
175 char *s = *p;
176
177 while (isspace(*s))
178 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200179
180 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200181}
182
183void strip_blank_end(char *p)
184{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100185 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200186
Jens Axboe523bfad2007-04-04 11:04:06 +0200187 s = strchr(p, ';');
188 if (s)
189 *s = '\0';
190 s = strchr(p, '#');
191 if (s)
192 *s = '\0';
193 if (s)
194 p = s;
195
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200196 s = p + strlen(p);
Jens Axboe853ee7f2009-03-06 20:29:29 +0100197 while ((isspace(*s) || iscntrl(*s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200198 s--;
199
200 *(s + 1) = '\0';
201}
202
Jens Axboe63f29372007-01-10 13:20:09 +0100203static int check_range_bytes(const char *str, long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200204{
205 char suffix;
206
Jens Axboe787f7e92006-11-06 13:26:29 +0100207 if (!strlen(str))
208 return 1;
209
Jens Axboecb2c86f2006-10-26 13:48:34 +0200210 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
211 *val *= get_mult_bytes(suffix);
212 return 0;
213 }
214
215 if (sscanf(str, "%lu", val) == 1)
216 return 0;
217
218 return 1;
219}
220
Jens Axboe63f29372007-01-10 13:20:09 +0100221static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200222{
Jens Axboe787f7e92006-11-06 13:26:29 +0100223 if (!strlen(p))
224 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200225 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200226 if (sscanf(p, "%x", val) == 1)
227 return 0;
228 } else {
229 if (sscanf(p, "%u", val) == 1)
230 return 0;
231 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200232
233 return 1;
234}
235
Jens Axboee1f36502006-10-27 10:54:08 +0200236static struct fio_option *find_option(struct fio_option *options,
237 const char *opt)
238{
Jens Axboe4945ba12007-01-11 14:36:56 +0100239 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200240
Jens Axboe4945ba12007-01-11 14:36:56 +0100241 for (o = &options[0]; o->name; o++) {
Jens Axboee1f36502006-10-27 10:54:08 +0200242 if (!strcmp(o->name, opt))
243 return o;
Jens Axboe03b74b32007-01-11 11:04:31 +0100244 else if (o->alias && !strcmp(o->alias, opt))
245 return o;
Jens Axboe33963c62006-10-27 11:33:01 +0200246 }
Jens Axboee1f36502006-10-27 10:54:08 +0200247
248 return NULL;
249}
250
Jens Axboe17abbe82006-11-06 11:23:10 +0100251#define val_store(ptr, val, off, data) \
252 do { \
253 ptr = td_var((data), (off)); \
254 *ptr = (val); \
255 } while (0)
256
Jens Axboef90eff52006-11-06 11:08:21 +0100257static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100258 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200259{
Jens Axboe63f29372007-01-10 13:20:09 +0100260 int il, *ilp;
261 long long ull, *ullp;
262 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200263 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200264 int ret = 0, is_time = 0;
265
Jens Axboea3d741f2008-02-27 18:32:33 +0100266 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
267 o->type, ptr);
268
Jens Axboee3cedca2008-11-19 19:57:52 +0100269 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe08e26e32006-11-21 13:15:10 +0100270 fprintf(stderr, "Option %s requires an argument\n", o->name);
271 return 1;
272 }
273
Jens Axboee1f36502006-10-27 10:54:08 +0200274 switch (o->type) {
275 case FIO_OPT_STR: {
276 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100277 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100278 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100279 int i;
Jens Axboee1f36502006-10-27 10:54:08 +0200280
Jens Axboef0857372007-03-19 13:15:23 +0100281 posval_sort(o, posval);
282
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100283 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100284 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100285 if (!vp->ival || vp->ival[0] == '\0')
286 break;
Jens Axboe6612a272007-03-13 08:54:06 +0100287 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100288 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
289 ret = 0;
290 if (!o->off1)
291 break;
292 val_store(ilp, vp->oval, o->off1, data);
293 break;
294 }
295 }
296
297 if (ret)
298 show_option_values(o);
299 else if (fn)
300 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200301 break;
302 }
303 case FIO_OPT_STR_VAL_TIME:
304 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100305 case FIO_OPT_INT:
Jens Axboee01b22b2009-06-09 15:43:25 +0200306 case FIO_OPT_STR_VAL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200307 fio_opt_str_val_fn *fn = o->cb;
308
309 if (is_time)
310 ret = check_str_time(ptr, &ull);
311 else
312 ret = check_str_bytes(ptr, &ull);
313
314 if (ret)
315 break;
316
Jens Axboedb8e0162007-01-10 13:41:26 +0100317 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100318 fprintf(stderr, "max value out of range: %lld"
319 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100320 return 1;
321 }
322 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100323 fprintf(stderr, "min value out of range: %lld"
324 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100325 return 1;
326 }
Jens Axboee1f36502006-10-27 10:54:08 +0200327
328 if (fn)
329 ret = fn(data, &ull);
330 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200331 if (o->type == FIO_OPT_INT) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100332 if (first)
333 val_store(ilp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100334 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100335 val_store(ilp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100336 } else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100337 if (first)
338 val_store(ullp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100339 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100340 val_store(ullp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100341 }
Jens Axboee1f36502006-10-27 10:54:08 +0200342 }
343 break;
344 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100345 case FIO_OPT_STR_STORE: {
346 fio_opt_str_fn *fn = o->cb;
347
Jens Axboee1f36502006-10-27 10:54:08 +0200348 cp = td_var(data, o->off1);
349 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100350 if (fn) {
351 ret = fn(data, ptr);
352 if (ret) {
353 free(*cp);
354 *cp = NULL;
355 }
356 }
Jens Axboee1f36502006-10-27 10:54:08 +0200357 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100358 }
Jens Axboee1f36502006-10-27 10:54:08 +0200359 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200360 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200361 char *p1, *p2;
362
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100363 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200364
365 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200366 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100367 p1 = strchr(tmp, ':');
368 if (!p1) {
369 ret = 1;
370 break;
371 }
Jens Axboee1f36502006-10-27 10:54:08 +0200372 }
373
374 p2 = p1 + 1;
375 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200376 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200377
378 ret = 1;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100379 if (!check_range_bytes(p1, &ul1) &&
380 !check_range_bytes(p2, &ul2)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200381 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200382 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100383 unsigned long foo = ul1;
384
385 ul1 = ul2;
386 ul2 = foo;
387 }
388
389 if (first) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100390 val_store(ilp, ul1, o->off1, data);
391 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200392 }
Jens Axboe97e8cd42008-02-05 09:57:39 +0100393 if (o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100394 val_store(ilp, ul1, o->off3, data);
395 val_store(ilp, ul2, o->off4, data);
396 }
397 }
398
Jens Axboee1f36502006-10-27 10:54:08 +0200399 break;
400 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100401 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200402 fio_opt_int_fn *fn = o->cb;
403
404 ret = check_int(ptr, &il);
405 if (ret)
406 break;
407
Jens Axboedb8e0162007-01-10 13:41:26 +0100408 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100409 fprintf(stderr, "max value out of range: %d (%d max)\n",
410 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100411 return 1;
412 }
413 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100414 fprintf(stderr, "min value out of range: %d (%d min)\n",
415 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100416 return 1;
417 }
Jens Axboee1f36502006-10-27 10:54:08 +0200418
Jens Axboe76a43db2007-01-11 13:24:44 +0100419 if (o->neg)
420 il = !il;
421
Jens Axboee1f36502006-10-27 10:54:08 +0200422 if (fn)
423 ret = fn(data, &il);
424 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100425 if (first)
426 val_store(ilp, il, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100427 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100428 val_store(ilp, il, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200429 }
430 break;
431 }
432 case FIO_OPT_STR_SET: {
433 fio_opt_str_set_fn *fn = o->cb;
434
435 if (fn)
436 ret = fn(data);
437 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100438 if (first)
439 val_store(ilp, 1, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100440 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100441 val_store(ilp, 1, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200442 }
443 break;
444 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200445 case FIO_OPT_DEPRECATED:
446 fprintf(stdout, "Option %s is deprecated\n", o->name);
447 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200448 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100449 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200450 ret = 1;
451 }
452
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200453 if (ret)
454 return ret;
455
Jens Axboed447a8c2009-07-17 22:26:15 +0200456 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200457 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200458 if (ret) {
459 fprintf(stderr,"Correct format for offending option\n");
460 fprintf(stderr, "%20s: %s\n", o->name, o->help);
461 show_option_help(o, stderr);
462 }
463 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200464
Jens Axboee1f36502006-10-27 10:54:08 +0200465 return ret;
466}
467
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200468static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100469{
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200470 char *ptr, *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100471 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100472
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200473 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
474
475 ptr = NULL;
476 if (__ptr)
477 ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100478
Jens Axboef90eff52006-11-06 11:08:21 +0100479 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100480 * See if we have a second set of parameters, hidden after a comma.
481 * Do this before parsing the first round, to check if we should
482 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100483 */
Joel Beckerad231bc2007-03-01 08:22:42 +0100484 if (ptr &&
485 (o->type != FIO_OPT_STR_STORE) &&
486 (o->type != FIO_OPT_STR)) {
Jens Axboe92b586f2006-11-07 14:29:17 +0100487 ptr2 = strchr(ptr, ',');
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200488 if (ptr2 && *(ptr2 + 1) == '\0')
489 *ptr2 = '\0';
Jens Axboe0c9baf92007-01-11 15:59:26 +0100490 if (!ptr2)
491 ptr2 = strchr(ptr, ':');
Jens Axboeb486f762007-03-15 09:15:12 +0100492 if (!ptr2)
493 ptr2 = strchr(ptr, '-');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100494 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100495
496 /*
497 * Don't return early if parsing the first option fails - if
498 * we are doing multiple arguments, we can allow the first one
499 * being empty.
500 */
501 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
502
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200503 if (!ptr2) {
504 if (ptr)
505 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100506 return r1;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200507 }
Jens Axboef90eff52006-11-06 11:08:21 +0100508
509 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100510 r2 = __handle_option(o, ptr2, data, 0, 0);
511
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200512 if (ptr)
513 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100514 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100515}
516
Jens Axboe3b8b7132008-06-10 19:46:23 +0200517static struct fio_option *get_option(const char *opt,
518 struct fio_option *options, char **post)
519{
520 struct fio_option *o;
521 char *ret;
522
523 ret = strchr(opt, '=');
524 if (ret) {
525 *post = ret;
526 *ret = '\0';
527 ret = (char *) opt;
528 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100529 strip_blank_end(ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200530 o = find_option(options, ret);
531 } else {
532 o = find_option(options, opt);
533 *post = NULL;
534 }
535
536 return o;
537}
538
539static int opt_cmp(const void *p1, const void *p2)
540{
541 struct fio_option *o1, *o2;
542 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100543 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200544
545 s1 = strdup(*((char **) p1));
546 s2 = strdup(*((char **) p2));
547
548 o1 = get_option(s1, fio_options, &foo);
549 o2 = get_option(s2, fio_options, &foo);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100550
551 prio1 = prio2 = 0;
552 if (o1)
553 prio1 = o1->prio;
554 if (o2)
555 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200556
557 free(s1);
558 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100559 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200560}
561
562void sort_options(char **opts, struct fio_option *options, int num_opts)
563{
564 fio_options = options;
565 qsort(opts, num_opts, sizeof(char *), opt_cmp);
566 fio_options = NULL;
567}
568
Jens Axboeb4692822006-10-27 13:43:22 +0200569int parse_cmd_option(const char *opt, const char *val,
570 struct fio_option *options, void *data)
571{
572 struct fio_option *o;
573
574 o = find_option(options, opt);
575 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100576 fprintf(stderr, "Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200577 return 1;
578 }
579
Jens Axboeb1508cf2006-11-02 09:12:40 +0100580 if (!handle_option(o, val, data))
581 return 0;
582
583 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
584 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200585}
586
Aaron Carroll88b5a392008-10-07 11:25:20 +0200587/*
588 * Return a copy of the input string with substrings of the form ${VARNAME}
589 * substituted with the value of the environment variable VARNAME. The
590 * substitution always occurs, even if VARNAME is empty or the corresponding
591 * environment variable undefined.
592 */
593static char *option_dup_subs(const char *opt)
594{
595 char out[OPT_LEN_MAX+1];
596 char in[OPT_LEN_MAX+1];
597 char *outptr = out;
598 char *inptr = in;
599 char *ch1, *ch2, *env;
600 ssize_t nchr = OPT_LEN_MAX;
601 size_t envlen;
602
603 in[OPT_LEN_MAX] = '\0';
604 strncpy(in, opt, OPT_LEN_MAX);
605
606 while (*inptr && nchr > 0) {
607 if (inptr[0] == '$' && inptr[1] == '{') {
608 ch2 = strchr(inptr, '}');
609 if (ch2 && inptr+1 < ch2) {
610 ch1 = inptr+2;
611 inptr = ch2+1;
612 *ch2 = '\0';
613
614 env = getenv(ch1);
615 if (env) {
616 envlen = strlen(env);
617 if (envlen <= nchr) {
618 memcpy(outptr, env, envlen);
619 outptr += envlen;
620 nchr -= envlen;
621 }
622 }
623
624 continue;
625 }
626 }
627
628 *outptr++ = *inptr++;
629 --nchr;
630 }
631
632 *outptr = '\0';
633 return strdup(out);
634}
635
Jens Axboee1f36502006-10-27 10:54:08 +0200636int parse_option(const char *opt, struct fio_option *options, void *data)
637{
Jens Axboeb4692822006-10-27 13:43:22 +0200638 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200639 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200640
Aaron Carroll88b5a392008-10-07 11:25:20 +0200641 tmp = option_dup_subs(opt);
Jens Axboee1f36502006-10-27 10:54:08 +0200642
Jens Axboe3b8b7132008-06-10 19:46:23 +0200643 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200644 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100645 fprintf(stderr, "Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200646 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200647 return 1;
648 }
649
Jens Axboe0401bca2007-03-29 11:00:43 +0200650 if (!handle_option(o, post, data)) {
651 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100652 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200653 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100654
655 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200656 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100657 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200658}
Jens Axboefd28ca42007-01-09 21:20:13 +0100659
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100660/*
661 * Option match, levenshtein distance. Handy for not quite remembering what
662 * the option name is.
663 */
664static int string_distance(const char *s1, const char *s2)
665{
666 unsigned int s1_len = strlen(s1);
667 unsigned int s2_len = strlen(s2);
668 unsigned int *p, *q, *r;
669 unsigned int i, j;
670
671 p = malloc(sizeof(unsigned int) * (s2_len + 1));
672 q = malloc(sizeof(unsigned int) * (s2_len + 1));
673
674 p[0] = 0;
675 for (i = 1; i <= s2_len; i++)
676 p[i] = p[i - 1] + 1;
677
678 for (i = 1; i <= s1_len; i++) {
679 q[0] = p[0] + 1;
680 for (j = 1; j <= s2_len; j++) {
681 unsigned int sub = p[j - 1];
682
683 if (s1[i - 1] != s2[j - 1])
684 sub++;
685
686 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
687 }
688 r = p;
689 p = q;
690 q = r;
691 }
692
693 i = p[s2_len];
694 free(p);
695 free(q);
696 return i;
697}
698
Jens Axboeafdf9352007-07-31 16:14:34 +0200699static struct fio_option *find_child(struct fio_option *options,
700 struct fio_option *o)
701{
702 struct fio_option *__o;
703
Jens Axboefdf28742007-07-31 23:06:09 +0200704 for (__o = options + 1; __o->name; __o++)
705 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200706 return __o;
707
708 return NULL;
709}
710
Jens Axboe323d9112008-03-01 15:12:14 +0100711static void __print_option(struct fio_option *o, struct fio_option *org,
712 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200713{
714 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100715 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200716
717 if (!o)
718 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200719 if (!org)
720 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100721
Jens Axboeafdf9352007-07-31 16:14:34 +0200722 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100723 depth = level;
724 while (depth--)
725 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200726
727 sprintf(p, "%s", o->name);
728
729 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100730}
731
732static void print_option(struct fio_option *o)
733{
734 struct fio_option *parent;
735 struct fio_option *__o;
736 unsigned int printed;
737 unsigned int level;
738
739 __print_option(o, NULL, 0);
740 parent = o;
741 level = 0;
742 do {
743 level++;
744 printed = 0;
745
746 while ((__o = find_child(o, parent)) != NULL) {
747 __print_option(__o, o, level);
748 o = __o;
749 printed++;
750 }
751
752 parent = o;
753 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200754}
755
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100756int show_cmd_help(struct fio_option *options, const char *name)
757{
758 struct fio_option *o, *closest;
759 unsigned int best_dist;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100760 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100761 int show_all = 0;
762
763 if (!name || !strcmp(name, "all"))
764 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100765
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100766 closest = NULL;
767 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100768 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100769 int match = 0;
770
Jens Axboe15ca1502008-04-07 09:26:02 +0200771 if (o->type == FIO_OPT_DEPRECATED)
772 continue;
773
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100774 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100775 if (!strcmp(name, o->name) ||
776 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100777 match = 1;
778 else {
779 unsigned int dist;
780
781 dist = string_distance(name, o->name);
782 if (dist < best_dist) {
783 best_dist = dist;
784 closest = o;
785 }
786 }
787 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100788
789 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100790 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100791 if (match)
Jens Axboeafdf9352007-07-31 16:14:34 +0200792 printf("%24s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +0100793 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +0200794 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +0100795 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +0100796 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100797 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100798 }
799
Jens Axboe70df2f12007-01-11 14:04:47 +0100800 if (!match)
801 continue;
802
Jens Axboed447a8c2009-07-17 22:26:15 +0200803 show_option_help(o, stdout);
Jens Axboefd28ca42007-01-09 21:20:13 +0100804 }
805
Jens Axboe29fc6af2007-01-09 21:22:02 +0100806 if (found)
807 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100808
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100809 printf("No such command: %s", name);
810 if (closest) {
811 printf(" - showing closest match\n");
812 printf("%20s: %s\n", closest->name, closest->help);
Jens Axboed447a8c2009-07-17 22:26:15 +0200813 show_option_help(closest, stdout);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100814 } else
815 printf("\n");
816
Jens Axboe29fc6af2007-01-09 21:22:02 +0100817 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100818}
Jens Axboeee738492007-01-10 11:23:16 +0100819
Jens Axboe13335dd2007-01-10 13:14:02 +0100820/*
821 * Handle parsing of default parameters.
822 */
Jens Axboeee738492007-01-10 11:23:16 +0100823void fill_default_options(void *data, struct fio_option *options)
824{
Jens Axboe4945ba12007-01-11 14:36:56 +0100825 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100826
Jens Axboea3d741f2008-02-27 18:32:33 +0100827 dprint(FD_PARSE, "filling default options\n");
828
Jens Axboe4945ba12007-01-11 14:36:56 +0100829 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100830 if (o->def)
831 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100832}
Jens Axboe13335dd2007-01-10 13:14:02 +0100833
834/*
835 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100836 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100837 */
838void options_init(struct fio_option *options)
839{
Jens Axboe4945ba12007-01-11 14:36:56 +0100840 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100841
Jens Axboea3d741f2008-02-27 18:32:33 +0100842 dprint(FD_PARSE, "init options\n");
843
Jens Axboe4945ba12007-01-11 14:36:56 +0100844 for (o = &options[0]; o->name; o++) {
Jens Axboe15ca1502008-04-07 09:26:02 +0200845 if (o->type == FIO_OPT_DEPRECATED)
846 continue;
Jens Axboe13335dd2007-01-10 13:14:02 +0100847 if (o->type == FIO_OPT_BOOL) {
848 o->minval = 0;
849 o->maxval = 1;
850 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100851 if (o->type == FIO_OPT_STR_SET && o->def) {
852 fprintf(stderr, "Option %s: string set option with"
853 " default will always be true\n",
854 o->name);
855 }
856 if (!o->cb && !o->off1) {
857 fprintf(stderr, "Option %s: neither cb nor offset"
858 " given\n", o->name);
859 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100860 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100861 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100862 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
863 fprintf(stderr, "Option %s: both cb and offset given\n",
864 o->name);
865 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100866 }
867}