blob: 78218615b79246a50c02107b7907ef747dd95476 [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;
Jens Axboed6978a32009-07-18 21:04:09 +020017extern unsigned int fio_get_kb_base(void *);
Jens Axboe3b8b7132008-06-10 19:46:23 +020018
Jens Axboef0857372007-03-19 13:15:23 +010019static int vp_cmp(const void *p1, const void *p2)
20{
21 const struct value_pair *vp1 = p1;
22 const struct value_pair *vp2 = p2;
23
24 return strlen(vp2->ival) - strlen(vp1->ival);
25}
26
27static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
28{
29 const struct value_pair *vp;
30 int entries;
31
32 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
33
34 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
35 vp = &o->posval[entries];
36 if (!vp->ival || vp->ival[0] == '\0')
37 break;
38
39 memcpy(&vpmap[entries], vp, sizeof(*vp));
40 }
41
42 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
43}
44
Jens Axboed447a8c2009-07-17 22:26:15 +020045static void show_option_range(struct fio_option *o, FILE *out)
Jens Axboeb1ec1da2007-02-23 10:29:16 +010046{
47 if (!o->minval && !o->maxval)
48 return;
49
Jens Axboed447a8c2009-07-17 22:26:15 +020050 fprintf(out, "%20s: min=%d", "range", o->minval);
Jens Axboe39801272009-07-01 09:06:12 +020051 if (o->maxval)
Jens Axboed447a8c2009-07-17 22:26:15 +020052 fprintf(out, ", max=%d", o->maxval);
53 fprintf(out, "\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010054}
55
56static void show_option_values(struct fio_option *o)
57{
Jens Axboeb1ec1da2007-02-23 10:29:16 +010058 int i = 0;
59
60 do {
Jens Axboe78372132007-03-14 13:24:07 +010061 const struct value_pair *vp = &o->posval[i];
62
63 if (!vp->ival)
Jens Axboeb1ec1da2007-02-23 10:29:16 +010064 break;
65
Jens Axboe78372132007-03-14 13:24:07 +010066 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
67 if (vp->help)
68 printf(" %s", vp->help);
69 printf("\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010070 i++;
71 } while (i < PARSE_MAX_VP);
72
73 if (i)
74 printf("\n");
75}
76
Jens Axboed447a8c2009-07-17 22:26:15 +020077static void show_option_help(struct fio_option *o, FILE *out)
78{
79 const char *typehelp[] = {
80 "string (opt=bla)",
81 "string with possible k/m/g postfix (opt=4k)",
82 "string with time postfix (opt=10s)",
83 "string (opt=bla)",
84 "string with dual range (opt=1k-4k,4k-8k)",
85 "integer value (opt=100)",
86 "boolean value (opt=1)",
87 "no argument (opt)",
88 };
89
90 if (o->alias)
91 fprintf(out, "%20s: %s\n", "alias", o->alias);
92
93 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
94 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
95 show_option_range(o, stdout);
96 show_option_values(o);
97}
98
Jens Axboecb2c86f2006-10-26 13:48:34 +020099static unsigned long get_mult_time(char c)
100{
101 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100102 case 'm':
103 case 'M':
104 return 60;
105 case 'h':
106 case 'H':
107 return 60 * 60;
108 case 'd':
109 case 'D':
110 return 24 * 60 * 60;
111 default:
112 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200113 }
114}
115
Jens Axboed6978a32009-07-18 21:04:09 +0200116static unsigned long long get_mult_bytes(char c, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200117{
Jens Axboed6978a32009-07-18 21:04:09 +0200118 unsigned int kb_base = fio_get_kb_base(data);
Jens Axboea639f0b2009-07-18 08:25:35 +0200119 unsigned long long ret = 1;
120
Jens Axboecb2c86f2006-10-26 13:48:34 +0200121 switch (c) {
Jens Axboea639f0b2009-07-18 08:25:35 +0200122 default:
123 break;
Jens Axboeb09da8f2009-07-17 23:16:17 +0200124 case 'p':
125 case 'P':
Jens Axboed6978a32009-07-18 21:04:09 +0200126 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200127 case 't':
128 case 'T':
Jens Axboed6978a32009-07-18 21:04:09 +0200129 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200130 case 'g':
131 case 'G':
Jens Axboed6978a32009-07-18 21:04:09 +0200132 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200133 case 'm':
134 case 'M':
Jens Axboed6978a32009-07-18 21:04:09 +0200135 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200136 case 'k':
137 case 'K':
Jens Axboed6978a32009-07-18 21:04:09 +0200138 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200139 break;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200140 }
Jens Axboea639f0b2009-07-18 08:25:35 +0200141
142 return ret;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200143}
144
145/*
Jens Axboee1f36502006-10-27 10:54:08 +0200146 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200147 */
Jens Axboed6978a32009-07-18 21:04:09 +0200148int str_to_decimal(const char *str, long long *val, int kilo, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200149{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100150 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200151
Jens Axboecb2c86f2006-10-26 13:48:34 +0200152 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100153 if (!len)
154 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200155
Jens Axboeb347f9d2009-03-09 14:15:21 +0100156 if (strstr(str, "0x") || strstr(str, "0X"))
157 base = 16;
158 else
159 base = 10;
160
161 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100162 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200163 return 1;
164
165 if (kilo)
Jens Axboed6978a32009-07-18 21:04:09 +0200166 *val *= get_mult_bytes(str[len - 1], data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200167 else
168 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100169
Jens Axboecb2c86f2006-10-26 13:48:34 +0200170 return 0;
171}
172
Jens Axboed6978a32009-07-18 21:04:09 +0200173static int check_str_bytes(const char *p, long long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200174{
Jens Axboed6978a32009-07-18 21:04:09 +0200175 return str_to_decimal(p, val, 1, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200176}
177
Jens Axboe63f29372007-01-10 13:20:09 +0100178static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200179{
Jens Axboed6978a32009-07-18 21:04:09 +0200180 return str_to_decimal(p, val, 0, NULL);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200181}
182
183void strip_blank_front(char **p)
184{
185 char *s = *p;
186
187 while (isspace(*s))
188 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200189
190 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200191}
192
193void strip_blank_end(char *p)
194{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100195 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200196
Jens Axboe523bfad2007-04-04 11:04:06 +0200197 s = strchr(p, ';');
198 if (s)
199 *s = '\0';
200 s = strchr(p, '#');
201 if (s)
202 *s = '\0';
203 if (s)
204 p = s;
205
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200206 s = p + strlen(p);
Jens Axboe853ee7f2009-03-06 20:29:29 +0100207 while ((isspace(*s) || iscntrl(*s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200208 s--;
209
210 *(s + 1) = '\0';
211}
212
Jens Axboed6978a32009-07-18 21:04:09 +0200213static int check_range_bytes(const char *str, long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200214{
215 char suffix;
216
Jens Axboe787f7e92006-11-06 13:26:29 +0100217 if (!strlen(str))
218 return 1;
219
Jens Axboecb2c86f2006-10-26 13:48:34 +0200220 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
Jens Axboed6978a32009-07-18 21:04:09 +0200221 *val *= get_mult_bytes(suffix, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200222 return 0;
223 }
224
225 if (sscanf(str, "%lu", val) == 1)
226 return 0;
227
228 return 1;
229}
230
Jens Axboe63f29372007-01-10 13:20:09 +0100231static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200232{
Jens Axboe787f7e92006-11-06 13:26:29 +0100233 if (!strlen(p))
234 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200235 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200236 if (sscanf(p, "%x", val) == 1)
237 return 0;
238 } else {
239 if (sscanf(p, "%u", val) == 1)
240 return 0;
241 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200242
243 return 1;
244}
245
Jens Axboee1f36502006-10-27 10:54:08 +0200246static struct fio_option *find_option(struct fio_option *options,
247 const char *opt)
248{
Jens Axboe4945ba12007-01-11 14:36:56 +0100249 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200250
Jens Axboe4945ba12007-01-11 14:36:56 +0100251 for (o = &options[0]; o->name; o++) {
Jens Axboee1f36502006-10-27 10:54:08 +0200252 if (!strcmp(o->name, opt))
253 return o;
Jens Axboe03b74b32007-01-11 11:04:31 +0100254 else if (o->alias && !strcmp(o->alias, opt))
255 return o;
Jens Axboe33963c62006-10-27 11:33:01 +0200256 }
Jens Axboee1f36502006-10-27 10:54:08 +0200257
258 return NULL;
259}
260
Jens Axboe17abbe82006-11-06 11:23:10 +0100261#define val_store(ptr, val, off, data) \
262 do { \
263 ptr = td_var((data), (off)); \
264 *ptr = (val); \
265 } while (0)
266
Jens Axboef90eff52006-11-06 11:08:21 +0100267static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100268 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200269{
Jens Axboe63f29372007-01-10 13:20:09 +0100270 int il, *ilp;
271 long long ull, *ullp;
272 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200273 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200274 int ret = 0, is_time = 0;
275
Jens Axboea3d741f2008-02-27 18:32:33 +0100276 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
277 o->type, ptr);
278
Jens Axboee3cedca2008-11-19 19:57:52 +0100279 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe08e26e32006-11-21 13:15:10 +0100280 fprintf(stderr, "Option %s requires an argument\n", o->name);
281 return 1;
282 }
283
Jens Axboee1f36502006-10-27 10:54:08 +0200284 switch (o->type) {
285 case FIO_OPT_STR: {
286 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100287 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100288 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100289 int i;
Jens Axboee1f36502006-10-27 10:54:08 +0200290
Jens Axboef0857372007-03-19 13:15:23 +0100291 posval_sort(o, posval);
292
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100293 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100294 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100295 if (!vp->ival || vp->ival[0] == '\0')
296 break;
Jens Axboe6612a272007-03-13 08:54:06 +0100297 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100298 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
299 ret = 0;
300 if (!o->off1)
301 break;
302 val_store(ilp, vp->oval, o->off1, data);
303 break;
304 }
305 }
306
307 if (ret)
308 show_option_values(o);
309 else if (fn)
310 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200311 break;
312 }
313 case FIO_OPT_STR_VAL_TIME:
314 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100315 case FIO_OPT_INT:
Jens Axboee01b22b2009-06-09 15:43:25 +0200316 case FIO_OPT_STR_VAL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200317 fio_opt_str_val_fn *fn = o->cb;
318
319 if (is_time)
320 ret = check_str_time(ptr, &ull);
321 else
Jens Axboed6978a32009-07-18 21:04:09 +0200322 ret = check_str_bytes(ptr, &ull, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200323
324 if (ret)
325 break;
326
Jens Axboedb8e0162007-01-10 13:41:26 +0100327 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100328 fprintf(stderr, "max value out of range: %lld"
329 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100330 return 1;
331 }
332 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100333 fprintf(stderr, "min value out of range: %lld"
334 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100335 return 1;
336 }
Jens Axboee1f36502006-10-27 10:54:08 +0200337
338 if (fn)
339 ret = fn(data, &ull);
340 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200341 if (o->type == FIO_OPT_INT) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100342 if (first)
343 val_store(ilp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100344 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100345 val_store(ilp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100346 } else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100347 if (first)
348 val_store(ullp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100349 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100350 val_store(ullp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100351 }
Jens Axboee1f36502006-10-27 10:54:08 +0200352 }
353 break;
354 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100355 case FIO_OPT_STR_STORE: {
356 fio_opt_str_fn *fn = o->cb;
357
Jens Axboee1f36502006-10-27 10:54:08 +0200358 cp = td_var(data, o->off1);
359 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100360 if (fn) {
361 ret = fn(data, ptr);
362 if (ret) {
363 free(*cp);
364 *cp = NULL;
365 }
366 }
Jens Axboee1f36502006-10-27 10:54:08 +0200367 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100368 }
Jens Axboee1f36502006-10-27 10:54:08 +0200369 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200370 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200371 char *p1, *p2;
372
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100373 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200374
375 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200376 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100377 p1 = strchr(tmp, ':');
378 if (!p1) {
379 ret = 1;
380 break;
381 }
Jens Axboee1f36502006-10-27 10:54:08 +0200382 }
383
384 p2 = p1 + 1;
385 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200386 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200387
388 ret = 1;
Jens Axboed6978a32009-07-18 21:04:09 +0200389 if (!check_range_bytes(p1, &ul1, data) &&
390 !check_range_bytes(p2, &ul2, data)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200391 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200392 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100393 unsigned long foo = ul1;
394
395 ul1 = ul2;
396 ul2 = foo;
397 }
398
399 if (first) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100400 val_store(ilp, ul1, o->off1, data);
401 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200402 }
Jens Axboe97e8cd42008-02-05 09:57:39 +0100403 if (o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100404 val_store(ilp, ul1, o->off3, data);
405 val_store(ilp, ul2, o->off4, data);
406 }
407 }
408
Jens Axboee1f36502006-10-27 10:54:08 +0200409 break;
410 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100411 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200412 fio_opt_int_fn *fn = o->cb;
413
414 ret = check_int(ptr, &il);
415 if (ret)
416 break;
417
Jens Axboedb8e0162007-01-10 13:41:26 +0100418 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100419 fprintf(stderr, "max value out of range: %d (%d max)\n",
420 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100421 return 1;
422 }
423 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100424 fprintf(stderr, "min value out of range: %d (%d min)\n",
425 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100426 return 1;
427 }
Jens Axboee1f36502006-10-27 10:54:08 +0200428
Jens Axboe76a43db2007-01-11 13:24:44 +0100429 if (o->neg)
430 il = !il;
431
Jens Axboee1f36502006-10-27 10:54:08 +0200432 if (fn)
433 ret = fn(data, &il);
434 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100435 if (first)
436 val_store(ilp, il, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100437 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100438 val_store(ilp, il, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200439 }
440 break;
441 }
442 case FIO_OPT_STR_SET: {
443 fio_opt_str_set_fn *fn = o->cb;
444
445 if (fn)
446 ret = fn(data);
447 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100448 if (first)
449 val_store(ilp, 1, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100450 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100451 val_store(ilp, 1, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200452 }
453 break;
454 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200455 case FIO_OPT_DEPRECATED:
456 fprintf(stdout, "Option %s is deprecated\n", o->name);
457 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200458 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100459 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200460 ret = 1;
461 }
462
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200463 if (ret)
464 return ret;
465
Jens Axboed447a8c2009-07-17 22:26:15 +0200466 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200467 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200468 if (ret) {
469 fprintf(stderr,"Correct format for offending option\n");
470 fprintf(stderr, "%20s: %s\n", o->name, o->help);
471 show_option_help(o, stderr);
472 }
473 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200474
Jens Axboee1f36502006-10-27 10:54:08 +0200475 return ret;
476}
477
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200478static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100479{
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200480 char *ptr, *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100481 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100482
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200483 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
484
485 ptr = NULL;
486 if (__ptr)
487 ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100488
Jens Axboef90eff52006-11-06 11:08:21 +0100489 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100490 * See if we have a second set of parameters, hidden after a comma.
491 * Do this before parsing the first round, to check if we should
492 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100493 */
Joel Beckerad231bc2007-03-01 08:22:42 +0100494 if (ptr &&
495 (o->type != FIO_OPT_STR_STORE) &&
496 (o->type != FIO_OPT_STR)) {
Jens Axboe92b586f2006-11-07 14:29:17 +0100497 ptr2 = strchr(ptr, ',');
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200498 if (ptr2 && *(ptr2 + 1) == '\0')
499 *ptr2 = '\0';
Jens Axboe0c9baf92007-01-11 15:59:26 +0100500 if (!ptr2)
501 ptr2 = strchr(ptr, ':');
Jens Axboeb486f762007-03-15 09:15:12 +0100502 if (!ptr2)
503 ptr2 = strchr(ptr, '-');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100504 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100505
506 /*
507 * Don't return early if parsing the first option fails - if
508 * we are doing multiple arguments, we can allow the first one
509 * being empty.
510 */
511 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
512
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200513 if (!ptr2) {
514 if (ptr)
515 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100516 return r1;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200517 }
Jens Axboef90eff52006-11-06 11:08:21 +0100518
519 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100520 r2 = __handle_option(o, ptr2, data, 0, 0);
521
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200522 if (ptr)
523 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100524 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100525}
526
Jens Axboe3b8b7132008-06-10 19:46:23 +0200527static struct fio_option *get_option(const char *opt,
528 struct fio_option *options, char **post)
529{
530 struct fio_option *o;
531 char *ret;
532
533 ret = strchr(opt, '=');
534 if (ret) {
535 *post = ret;
536 *ret = '\0';
537 ret = (char *) opt;
538 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100539 strip_blank_end(ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200540 o = find_option(options, ret);
541 } else {
542 o = find_option(options, opt);
543 *post = NULL;
544 }
545
546 return o;
547}
548
549static int opt_cmp(const void *p1, const void *p2)
550{
551 struct fio_option *o1, *o2;
552 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100553 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200554
555 s1 = strdup(*((char **) p1));
556 s2 = strdup(*((char **) p2));
557
558 o1 = get_option(s1, fio_options, &foo);
559 o2 = get_option(s2, fio_options, &foo);
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200560
Jens Axboe8cdabc12008-11-19 12:31:43 +0100561 prio1 = prio2 = 0;
562 if (o1)
563 prio1 = o1->prio;
564 if (o2)
565 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200566
567 free(s1);
568 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100569 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200570}
571
572void sort_options(char **opts, struct fio_option *options, int num_opts)
573{
574 fio_options = options;
575 qsort(opts, num_opts, sizeof(char *), opt_cmp);
576 fio_options = NULL;
577}
578
Jens Axboeb4692822006-10-27 13:43:22 +0200579int parse_cmd_option(const char *opt, const char *val,
580 struct fio_option *options, void *data)
581{
582 struct fio_option *o;
583
584 o = find_option(options, opt);
585 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100586 fprintf(stderr, "Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200587 return 1;
588 }
589
Jens Axboeb1508cf2006-11-02 09:12:40 +0100590 if (!handle_option(o, val, data))
591 return 0;
592
593 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
594 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200595}
596
Aaron Carroll88b5a392008-10-07 11:25:20 +0200597/*
598 * Return a copy of the input string with substrings of the form ${VARNAME}
599 * substituted with the value of the environment variable VARNAME. The
600 * substitution always occurs, even if VARNAME is empty or the corresponding
601 * environment variable undefined.
602 */
603static char *option_dup_subs(const char *opt)
604{
605 char out[OPT_LEN_MAX+1];
606 char in[OPT_LEN_MAX+1];
607 char *outptr = out;
608 char *inptr = in;
609 char *ch1, *ch2, *env;
610 ssize_t nchr = OPT_LEN_MAX;
611 size_t envlen;
612
613 in[OPT_LEN_MAX] = '\0';
614 strncpy(in, opt, OPT_LEN_MAX);
615
616 while (*inptr && nchr > 0) {
617 if (inptr[0] == '$' && inptr[1] == '{') {
618 ch2 = strchr(inptr, '}');
619 if (ch2 && inptr+1 < ch2) {
620 ch1 = inptr+2;
621 inptr = ch2+1;
622 *ch2 = '\0';
623
624 env = getenv(ch1);
625 if (env) {
626 envlen = strlen(env);
627 if (envlen <= nchr) {
628 memcpy(outptr, env, envlen);
629 outptr += envlen;
630 nchr -= envlen;
631 }
632 }
633
634 continue;
635 }
636 }
637
638 *outptr++ = *inptr++;
639 --nchr;
640 }
641
642 *outptr = '\0';
643 return strdup(out);
644}
645
Jens Axboee1f36502006-10-27 10:54:08 +0200646int parse_option(const char *opt, struct fio_option *options, void *data)
647{
Jens Axboeb4692822006-10-27 13:43:22 +0200648 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200649 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200650
Aaron Carroll88b5a392008-10-07 11:25:20 +0200651 tmp = option_dup_subs(opt);
Jens Axboee1f36502006-10-27 10:54:08 +0200652
Jens Axboe3b8b7132008-06-10 19:46:23 +0200653 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200654 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100655 fprintf(stderr, "Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200656 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200657 return 1;
658 }
659
Jens Axboe0401bca2007-03-29 11:00:43 +0200660 if (!handle_option(o, post, data)) {
661 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100662 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200663 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100664
665 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200666 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100667 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200668}
Jens Axboefd28ca42007-01-09 21:20:13 +0100669
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100670/*
671 * Option match, levenshtein distance. Handy for not quite remembering what
672 * the option name is.
673 */
674static int string_distance(const char *s1, const char *s2)
675{
676 unsigned int s1_len = strlen(s1);
677 unsigned int s2_len = strlen(s2);
678 unsigned int *p, *q, *r;
679 unsigned int i, j;
680
681 p = malloc(sizeof(unsigned int) * (s2_len + 1));
682 q = malloc(sizeof(unsigned int) * (s2_len + 1));
683
684 p[0] = 0;
685 for (i = 1; i <= s2_len; i++)
686 p[i] = p[i - 1] + 1;
687
688 for (i = 1; i <= s1_len; i++) {
689 q[0] = p[0] + 1;
690 for (j = 1; j <= s2_len; j++) {
691 unsigned int sub = p[j - 1];
692
693 if (s1[i - 1] != s2[j - 1])
694 sub++;
695
696 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
697 }
698 r = p;
699 p = q;
700 q = r;
701 }
702
703 i = p[s2_len];
704 free(p);
705 free(q);
706 return i;
707}
708
Jens Axboeafdf9352007-07-31 16:14:34 +0200709static struct fio_option *find_child(struct fio_option *options,
710 struct fio_option *o)
711{
712 struct fio_option *__o;
713
Jens Axboefdf28742007-07-31 23:06:09 +0200714 for (__o = options + 1; __o->name; __o++)
715 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200716 return __o;
717
718 return NULL;
719}
720
Jens Axboe323d9112008-03-01 15:12:14 +0100721static void __print_option(struct fio_option *o, struct fio_option *org,
722 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200723{
724 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100725 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200726
727 if (!o)
728 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200729 if (!org)
730 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100731
Jens Axboeafdf9352007-07-31 16:14:34 +0200732 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100733 depth = level;
734 while (depth--)
735 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200736
737 sprintf(p, "%s", o->name);
738
739 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100740}
741
742static void print_option(struct fio_option *o)
743{
744 struct fio_option *parent;
745 struct fio_option *__o;
746 unsigned int printed;
747 unsigned int level;
748
749 __print_option(o, NULL, 0);
750 parent = o;
751 level = 0;
752 do {
753 level++;
754 printed = 0;
755
756 while ((__o = find_child(o, parent)) != NULL) {
757 __print_option(__o, o, level);
758 o = __o;
759 printed++;
760 }
761
762 parent = o;
763 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200764}
765
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100766int show_cmd_help(struct fio_option *options, const char *name)
767{
768 struct fio_option *o, *closest;
769 unsigned int best_dist;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100770 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100771 int show_all = 0;
772
773 if (!name || !strcmp(name, "all"))
774 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100775
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100776 closest = NULL;
777 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100778 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100779 int match = 0;
780
Jens Axboe15ca1502008-04-07 09:26:02 +0200781 if (o->type == FIO_OPT_DEPRECATED)
782 continue;
783
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100784 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100785 if (!strcmp(name, o->name) ||
786 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100787 match = 1;
788 else {
789 unsigned int dist;
790
791 dist = string_distance(name, o->name);
792 if (dist < best_dist) {
793 best_dist = dist;
794 closest = o;
795 }
796 }
797 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100798
799 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100800 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100801 if (match)
Jens Axboeafdf9352007-07-31 16:14:34 +0200802 printf("%24s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +0100803 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +0200804 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +0100805 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +0100806 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100807 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100808 }
809
Jens Axboe70df2f12007-01-11 14:04:47 +0100810 if (!match)
811 continue;
812
Jens Axboed447a8c2009-07-17 22:26:15 +0200813 show_option_help(o, stdout);
Jens Axboefd28ca42007-01-09 21:20:13 +0100814 }
815
Jens Axboe29fc6af2007-01-09 21:22:02 +0100816 if (found)
817 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100818
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100819 printf("No such command: %s", name);
820 if (closest) {
821 printf(" - showing closest match\n");
822 printf("%20s: %s\n", closest->name, closest->help);
Jens Axboed447a8c2009-07-17 22:26:15 +0200823 show_option_help(closest, stdout);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100824 } else
825 printf("\n");
826
Jens Axboe29fc6af2007-01-09 21:22:02 +0100827 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100828}
Jens Axboeee738492007-01-10 11:23:16 +0100829
Jens Axboe13335dd2007-01-10 13:14:02 +0100830/*
831 * Handle parsing of default parameters.
832 */
Jens Axboeee738492007-01-10 11:23:16 +0100833void fill_default_options(void *data, struct fio_option *options)
834{
Jens Axboe4945ba12007-01-11 14:36:56 +0100835 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100836
Jens Axboea3d741f2008-02-27 18:32:33 +0100837 dprint(FD_PARSE, "filling default options\n");
838
Jens Axboe4945ba12007-01-11 14:36:56 +0100839 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100840 if (o->def)
841 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100842}
Jens Axboe13335dd2007-01-10 13:14:02 +0100843
844/*
845 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100846 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100847 */
848void options_init(struct fio_option *options)
849{
Jens Axboe4945ba12007-01-11 14:36:56 +0100850 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100851
Jens Axboea3d741f2008-02-27 18:32:33 +0100852 dprint(FD_PARSE, "init options\n");
853
Jens Axboe4945ba12007-01-11 14:36:56 +0100854 for (o = &options[0]; o->name; o++) {
Jens Axboe15ca1502008-04-07 09:26:02 +0200855 if (o->type == FIO_OPT_DEPRECATED)
856 continue;
Jens Axboe13335dd2007-01-10 13:14:02 +0100857 if (o->type == FIO_OPT_BOOL) {
858 o->minval = 0;
859 o->maxval = 1;
860 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100861 if (o->type == FIO_OPT_STR_SET && o->def) {
862 fprintf(stderr, "Option %s: string set option with"
863 " default will always be true\n",
864 o->name);
865 }
866 if (!o->cb && !o->off1) {
867 fprintf(stderr, "Option %s: neither cb nor offset"
868 " given\n", o->name);
869 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100870 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100871 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100872 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
873 fprintf(stderr, "Option %s: both cb and offset given\n",
874 o->name);
875 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100876 }
877}