blob: 2cdac54012515e2629baa7e91a4e52308e752466 [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 Axboe9f988e22010-03-04 10:42:38 +010015#include "options.h"
Jens Axboecb2c86f2006-10-26 13:48:34 +020016
Jens Axboe3b8b7132008-06-10 19:46:23 +020017static struct fio_option *fio_options;
Jens Axboed6978a32009-07-18 21:04:09 +020018extern unsigned int fio_get_kb_base(void *);
Jens Axboe3b8b7132008-06-10 19:46:23 +020019
Jens Axboef0857372007-03-19 13:15:23 +010020static int vp_cmp(const void *p1, const void *p2)
21{
22 const struct value_pair *vp1 = p1;
23 const struct value_pair *vp2 = p2;
24
25 return strlen(vp2->ival) - strlen(vp1->ival);
26}
27
28static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
29{
30 const struct value_pair *vp;
31 int entries;
32
33 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
34
35 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
36 vp = &o->posval[entries];
37 if (!vp->ival || vp->ival[0] == '\0')
38 break;
39
40 memcpy(&vpmap[entries], vp, sizeof(*vp));
41 }
42
43 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
44}
45
Jens Axboed447a8c2009-07-17 22:26:15 +020046static void show_option_range(struct fio_option *o, FILE *out)
Jens Axboeb1ec1da2007-02-23 10:29:16 +010047{
48 if (!o->minval && !o->maxval)
49 return;
50
Jens Axboed447a8c2009-07-17 22:26:15 +020051 fprintf(out, "%20s: min=%d", "range", o->minval);
Jens Axboe39801272009-07-01 09:06:12 +020052 if (o->maxval)
Jens Axboed447a8c2009-07-17 22:26:15 +020053 fprintf(out, ", max=%d", o->maxval);
54 fprintf(out, "\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010055}
56
57static void show_option_values(struct fio_option *o)
58{
Jens Axboeb1ec1da2007-02-23 10:29:16 +010059 int i = 0;
60
61 do {
Jens Axboe78372132007-03-14 13:24:07 +010062 const struct value_pair *vp = &o->posval[i];
63
64 if (!vp->ival)
Jens Axboeb1ec1da2007-02-23 10:29:16 +010065 break;
66
Jens Axboe78372132007-03-14 13:24:07 +010067 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
68 if (vp->help)
69 printf(" %s", vp->help);
70 printf("\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010071 i++;
72 } while (i < PARSE_MAX_VP);
73
74 if (i)
75 printf("\n");
76}
77
Jens Axboed447a8c2009-07-17 22:26:15 +020078static void show_option_help(struct fio_option *o, FILE *out)
79{
80 const char *typehelp[] = {
81 "string (opt=bla)",
82 "string with possible k/m/g postfix (opt=4k)",
83 "string with time postfix (opt=10s)",
84 "string (opt=bla)",
85 "string with dual range (opt=1k-4k,4k-8k)",
86 "integer value (opt=100)",
87 "boolean value (opt=1)",
88 "no argument (opt)",
89 };
90
91 if (o->alias)
92 fprintf(out, "%20s: %s\n", "alias", o->alias);
93
94 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
95 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
96 show_option_range(o, stdout);
97 show_option_values(o);
98}
99
Jens Axboecb2c86f2006-10-26 13:48:34 +0200100static unsigned long get_mult_time(char c)
101{
102 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100103 case 'm':
104 case 'M':
105 return 60;
106 case 'h':
107 case 'H':
108 return 60 * 60;
109 case 'd':
110 case 'D':
111 return 24 * 60 * 60;
112 default:
113 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200114 }
115}
116
Jens Axboed6978a32009-07-18 21:04:09 +0200117static unsigned long long get_mult_bytes(char c, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200118{
Jens Axboed6978a32009-07-18 21:04:09 +0200119 unsigned int kb_base = fio_get_kb_base(data);
Jens Axboea639f0b2009-07-18 08:25:35 +0200120 unsigned long long ret = 1;
121
Jens Axboecb2c86f2006-10-26 13:48:34 +0200122 switch (c) {
Jens Axboea639f0b2009-07-18 08:25:35 +0200123 default:
124 break;
Jens Axboeb09da8f2009-07-17 23:16:17 +0200125 case 'p':
126 case 'P':
Jens Axboed6978a32009-07-18 21:04:09 +0200127 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200128 case 't':
129 case 'T':
Jens Axboed6978a32009-07-18 21:04:09 +0200130 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200131 case 'g':
132 case 'G':
Jens Axboed6978a32009-07-18 21:04:09 +0200133 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200134 case 'm':
135 case 'M':
Jens Axboed6978a32009-07-18 21:04:09 +0200136 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200137 case 'k':
138 case 'K':
Jens Axboed6978a32009-07-18 21:04:09 +0200139 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200140 break;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200141 }
Jens Axboea639f0b2009-07-18 08:25:35 +0200142
143 return ret;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200144}
145
146/*
Jens Axboee1f36502006-10-27 10:54:08 +0200147 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200148 */
Jens Axboed6978a32009-07-18 21:04:09 +0200149int str_to_decimal(const char *str, long long *val, int kilo, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200150{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100151 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200152
Jens Axboecb2c86f2006-10-26 13:48:34 +0200153 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100154 if (!len)
155 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200156
Jens Axboeb347f9d2009-03-09 14:15:21 +0100157 if (strstr(str, "0x") || strstr(str, "0X"))
158 base = 16;
159 else
160 base = 10;
161
162 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100163 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200164 return 1;
165
Jens Axboe975462a2009-12-23 08:54:52 +0100166 if (kilo) {
167 const char *p;
168 /*
169 * if the last char is 'b' or 'B', the user likely used
170 * "1gb" instead of just "1g". If the second to last is also
171 * a letter, adjust.
172 */
173 p = str + len - 1;
174 if ((*p == 'b' || *p == 'B') && isalpha(*(p - 1)))
175 --p;
176
177 *val *= get_mult_bytes(*p, data);
178 } else
Jens Axboecb2c86f2006-10-26 13:48:34 +0200179 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100180
Jens Axboecb2c86f2006-10-26 13:48:34 +0200181 return 0;
182}
183
Jens Axboed6978a32009-07-18 21:04:09 +0200184static int check_str_bytes(const char *p, long long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200185{
Jens Axboed6978a32009-07-18 21:04:09 +0200186 return str_to_decimal(p, val, 1, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200187}
188
Jens Axboe63f29372007-01-10 13:20:09 +0100189static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200190{
Jens Axboed6978a32009-07-18 21:04:09 +0200191 return str_to_decimal(p, val, 0, NULL);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200192}
193
194void strip_blank_front(char **p)
195{
196 char *s = *p;
197
198 while (isspace(*s))
199 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200200
201 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200202}
203
204void strip_blank_end(char *p)
205{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100206 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200207
Jens Axboe523bfad2007-04-04 11:04:06 +0200208 s = strchr(p, ';');
209 if (s)
210 *s = '\0';
211 s = strchr(p, '#');
212 if (s)
213 *s = '\0';
214 if (s)
215 p = s;
216
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200217 s = p + strlen(p);
Jens Axboe853ee7f2009-03-06 20:29:29 +0100218 while ((isspace(*s) || iscntrl(*s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200219 s--;
220
221 *(s + 1) = '\0';
222}
223
Jens Axboed6978a32009-07-18 21:04:09 +0200224static int check_range_bytes(const char *str, long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200225{
226 char suffix;
227
Jens Axboe787f7e92006-11-06 13:26:29 +0100228 if (!strlen(str))
229 return 1;
230
Jens Axboecb2c86f2006-10-26 13:48:34 +0200231 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
Jens Axboed6978a32009-07-18 21:04:09 +0200232 *val *= get_mult_bytes(suffix, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200233 return 0;
234 }
235
236 if (sscanf(str, "%lu", val) == 1)
237 return 0;
238
239 return 1;
240}
241
Jens Axboe63f29372007-01-10 13:20:09 +0100242static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200243{
Jens Axboe787f7e92006-11-06 13:26:29 +0100244 if (!strlen(p))
245 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200246 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200247 if (sscanf(p, "%x", val) == 1)
248 return 0;
249 } else {
250 if (sscanf(p, "%u", val) == 1)
251 return 0;
252 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200253
254 return 1;
255}
256
Jens Axboe9f988e22010-03-04 10:42:38 +0100257static inline int o_match(struct fio_option *o, const char *opt)
Jens Axboee1f36502006-10-27 10:54:08 +0200258{
Jens Axboe9f988e22010-03-04 10:42:38 +0100259 if (!strcmp(o->name, opt))
260 return 1;
261 else if (o->alias && !strcmp(o->alias, opt))
262 return 1;
263
264 return 0;
265}
266
267static struct fio_option *find_option(struct fio_option *options,
268 struct flist_head *eops, const char *opt)
269{
270 struct flist_head *n;
Jens Axboe4945ba12007-01-11 14:36:56 +0100271 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200272
Jens Axboe9f988e22010-03-04 10:42:38 +0100273 for (o = &options[0]; o->name; o++)
274 if (o_match(o, opt))
Jens Axboee1f36502006-10-27 10:54:08 +0200275 return o;
Jens Axboe9f988e22010-03-04 10:42:38 +0100276
277 if (!eops)
278 return NULL;
279
280 flist_for_each(n, eops) {
281 struct ext_option *eopt;
282
283 eopt = flist_entry(n, struct ext_option, list);
284 if (o_match(&eopt->o, opt))
285 return &eopt->o;
Jens Axboe33963c62006-10-27 11:33:01 +0200286 }
Jens Axboee1f36502006-10-27 10:54:08 +0200287
288 return NULL;
289}
290
Jens Axboe17abbe82006-11-06 11:23:10 +0100291#define val_store(ptr, val, off, data) \
292 do { \
293 ptr = td_var((data), (off)); \
294 *ptr = (val); \
295 } while (0)
296
Jens Axboef90eff52006-11-06 11:08:21 +0100297static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100298 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200299{
Jens Axboe63f29372007-01-10 13:20:09 +0100300 int il, *ilp;
301 long long ull, *ullp;
302 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200303 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200304 int ret = 0, is_time = 0;
305
Jens Axboea3d741f2008-02-27 18:32:33 +0100306 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
307 o->type, ptr);
308
Jens Axboee3cedca2008-11-19 19:57:52 +0100309 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe08e26e32006-11-21 13:15:10 +0100310 fprintf(stderr, "Option %s requires an argument\n", o->name);
311 return 1;
312 }
313
Jens Axboee1f36502006-10-27 10:54:08 +0200314 switch (o->type) {
315 case FIO_OPT_STR: {
316 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100317 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100318 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100319 int i;
Jens Axboee1f36502006-10-27 10:54:08 +0200320
Jens Axboef0857372007-03-19 13:15:23 +0100321 posval_sort(o, posval);
322
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100323 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100324 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100325 if (!vp->ival || vp->ival[0] == '\0')
326 break;
Jens Axboe6612a272007-03-13 08:54:06 +0100327 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100328 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
329 ret = 0;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100330 if (o->roff1)
331 *(unsigned int *) o->roff1 = vp->oval;
332 else {
333 if (!o->off1)
334 break;
335 val_store(ilp, vp->oval, o->off1, data);
336 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100337 break;
338 }
339 }
340
341 if (ret)
342 show_option_values(o);
343 else if (fn)
344 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200345 break;
346 }
347 case FIO_OPT_STR_VAL_TIME:
348 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100349 case FIO_OPT_INT:
Jens Axboee01b22b2009-06-09 15:43:25 +0200350 case FIO_OPT_STR_VAL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200351 fio_opt_str_val_fn *fn = o->cb;
352
353 if (is_time)
354 ret = check_str_time(ptr, &ull);
355 else
Jens Axboed6978a32009-07-18 21:04:09 +0200356 ret = check_str_bytes(ptr, &ull, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200357
358 if (ret)
359 break;
360
Jens Axboedb8e0162007-01-10 13:41:26 +0100361 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100362 fprintf(stderr, "max value out of range: %lld"
363 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100364 return 1;
365 }
366 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100367 fprintf(stderr, "min value out of range: %lld"
368 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100369 return 1;
370 }
Jens Axboee1f36502006-10-27 10:54:08 +0200371
372 if (fn)
373 ret = fn(data, &ull);
374 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200375 if (o->type == FIO_OPT_INT) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100376 if (first) {
377 if (o->roff1)
378 *(unsigned long long *) o->roff1 = ull;
379 else
380 val_store(ilp, ull, o->off1, data);
381 }
382 if (!more) {
383 if (o->roff2)
384 *(unsigned long long *) o->roff2 = ull;
385 else if (o->off2)
386 val_store(ilp, ull, o->off2, data);
387 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100388 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100389 if (first) {
390 if (o->roff1)
391 *(unsigned long long *) o->roff1 = ull;
392 else
393 val_store(ullp, ull, o->off1, data);
394 }
395 if (!more) {
396 if (o->roff2)
397 *(unsigned long long *) o->roff2 = ull;
398 else if (o->off2)
399 val_store(ullp, ull, o->off2, data);
400 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100401 }
Jens Axboee1f36502006-10-27 10:54:08 +0200402 }
403 break;
404 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100405 case FIO_OPT_STR_STORE: {
406 fio_opt_str_fn *fn = o->cb;
407
Jens Axboe02c6aad2010-03-04 13:49:11 +0100408 if (o->roff1)
409 cp = (char **) o->roff1;
410 else
411 cp = td_var(data, o->off1);
412
Jens Axboee1f36502006-10-27 10:54:08 +0200413 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100414 if (fn) {
415 ret = fn(data, ptr);
416 if (ret) {
417 free(*cp);
418 *cp = NULL;
419 }
420 }
Jens Axboee1f36502006-10-27 10:54:08 +0200421 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100422 }
Jens Axboee1f36502006-10-27 10:54:08 +0200423 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200424 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200425 char *p1, *p2;
426
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100427 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200428
429 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200430 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100431 p1 = strchr(tmp, ':');
432 if (!p1) {
433 ret = 1;
434 break;
435 }
Jens Axboee1f36502006-10-27 10:54:08 +0200436 }
437
438 p2 = p1 + 1;
439 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200440 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200441
442 ret = 1;
Jens Axboed6978a32009-07-18 21:04:09 +0200443 if (!check_range_bytes(p1, &ul1, data) &&
444 !check_range_bytes(p2, &ul2, data)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200445 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200446 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100447 unsigned long foo = ul1;
448
449 ul1 = ul2;
450 ul2 = foo;
451 }
452
453 if (first) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100454 if (o->roff1)
455 *(unsigned long *) o->roff1 = ul1;
456 else
457 val_store(ilp, ul1, o->off1, data);
458 if (o->roff2)
459 *(unsigned long *) o->roff2 = ul2;
460 else
461 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200462 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100463 if (o->roff3 && o->roff4) {
464 *(unsigned long *) o->roff3 = ul1;
465 *(unsigned long *) o->roff4 = ul2;
466 } else if (o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100467 val_store(ilp, ul1, o->off3, data);
468 val_store(ilp, ul2, o->off4, data);
469 }
470 }
471
Jens Axboee1f36502006-10-27 10:54:08 +0200472 break;
473 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100474 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200475 fio_opt_int_fn *fn = o->cb;
476
477 ret = check_int(ptr, &il);
478 if (ret)
479 break;
480
Jens Axboedb8e0162007-01-10 13:41:26 +0100481 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100482 fprintf(stderr, "max value out of range: %d (%d max)\n",
483 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100484 return 1;
485 }
486 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100487 fprintf(stderr, "min value out of range: %d (%d min)\n",
488 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100489 return 1;
490 }
Jens Axboee1f36502006-10-27 10:54:08 +0200491
Jens Axboe76a43db2007-01-11 13:24:44 +0100492 if (o->neg)
493 il = !il;
494
Jens Axboee1f36502006-10-27 10:54:08 +0200495 if (fn)
496 ret = fn(data, &il);
497 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100498 if (first) {
499 if (o->roff1)
500 *(unsigned int *)o->roff1 = il;
501 else
502 val_store(ilp, il, o->off1, data);
503 }
504 if (!more) {
505 if (o->roff2)
506 *(unsigned int *) o->roff2 = il;
507 else if (o->off2)
508 val_store(ilp, il, o->off2, data);
509 }
Jens Axboee1f36502006-10-27 10:54:08 +0200510 }
511 break;
512 }
513 case FIO_OPT_STR_SET: {
514 fio_opt_str_set_fn *fn = o->cb;
515
516 if (fn)
517 ret = fn(data);
518 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100519 if (first) {
520 if (o->roff1)
521 *(unsigned int *) o->roff1 = 1;
522 else
523 val_store(ilp, 1, o->off1, data);
524 }
525 if (!more) {
526 if (o->roff2)
527 *(unsigned int *) o->roff2 = 1;
528 else if (o->off2)
529 val_store(ilp, 1, o->off2, data);
530 }
Jens Axboee1f36502006-10-27 10:54:08 +0200531 }
532 break;
533 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200534 case FIO_OPT_DEPRECATED:
535 fprintf(stdout, "Option %s is deprecated\n", o->name);
536 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200537 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100538 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200539 ret = 1;
540 }
541
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200542 if (ret)
543 return ret;
544
Jens Axboed447a8c2009-07-17 22:26:15 +0200545 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200546 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200547 if (ret) {
548 fprintf(stderr,"Correct format for offending option\n");
549 fprintf(stderr, "%20s: %s\n", o->name, o->help);
550 show_option_help(o, stderr);
551 }
552 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200553
Jens Axboee1f36502006-10-27 10:54:08 +0200554 return ret;
555}
556
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200557static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100558{
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200559 char *ptr, *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100560 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100561
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200562 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
563
564 ptr = NULL;
565 if (__ptr)
566 ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100567
Jens Axboef90eff52006-11-06 11:08:21 +0100568 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100569 * See if we have a second set of parameters, hidden after a comma.
570 * Do this before parsing the first round, to check if we should
571 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100572 */
Joel Beckerad231bc2007-03-01 08:22:42 +0100573 if (ptr &&
574 (o->type != FIO_OPT_STR_STORE) &&
575 (o->type != FIO_OPT_STR)) {
Jens Axboe92b586f2006-11-07 14:29:17 +0100576 ptr2 = strchr(ptr, ',');
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200577 if (ptr2 && *(ptr2 + 1) == '\0')
578 *ptr2 = '\0';
Jens Axboe0c9baf92007-01-11 15:59:26 +0100579 if (!ptr2)
580 ptr2 = strchr(ptr, ':');
Jens Axboeb486f762007-03-15 09:15:12 +0100581 if (!ptr2)
582 ptr2 = strchr(ptr, '-');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100583 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100584
585 /*
586 * Don't return early if parsing the first option fails - if
587 * we are doing multiple arguments, we can allow the first one
588 * being empty.
589 */
590 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
591
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200592 if (!ptr2) {
593 if (ptr)
594 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100595 return r1;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200596 }
Jens Axboef90eff52006-11-06 11:08:21 +0100597
598 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100599 r2 = __handle_option(o, ptr2, data, 0, 0);
600
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200601 if (ptr)
602 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100603 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100604}
605
Jens Axboe3b8b7132008-06-10 19:46:23 +0200606static struct fio_option *get_option(const char *opt,
Jens Axboe9f988e22010-03-04 10:42:38 +0100607 struct fio_option *options,
608 struct flist_head *eops, char **post)
Jens Axboe3b8b7132008-06-10 19:46:23 +0200609{
610 struct fio_option *o;
611 char *ret;
612
613 ret = strchr(opt, '=');
614 if (ret) {
615 *post = ret;
616 *ret = '\0';
617 ret = (char *) opt;
618 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100619 strip_blank_end(ret);
Jens Axboe9f988e22010-03-04 10:42:38 +0100620 o = find_option(options, eops, ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200621 } else {
Jens Axboe9f988e22010-03-04 10:42:38 +0100622 o = find_option(options, eops, opt);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200623 *post = NULL;
624 }
625
626 return o;
627}
628
629static int opt_cmp(const void *p1, const void *p2)
630{
631 struct fio_option *o1, *o2;
632 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100633 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200634
635 s1 = strdup(*((char **) p1));
636 s2 = strdup(*((char **) p2));
637
Jens Axboe9f988e22010-03-04 10:42:38 +0100638 o1 = get_option(s1, fio_options, NULL, &foo);
639 o2 = get_option(s2, fio_options, NULL, &foo);
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200640
Jens Axboe8cdabc12008-11-19 12:31:43 +0100641 prio1 = prio2 = 0;
642 if (o1)
643 prio1 = o1->prio;
644 if (o2)
645 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200646
647 free(s1);
648 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100649 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200650}
651
652void sort_options(char **opts, struct fio_option *options, int num_opts)
653{
654 fio_options = options;
655 qsort(opts, num_opts, sizeof(char *), opt_cmp);
656 fio_options = NULL;
657}
658
Jens Axboeb4692822006-10-27 13:43:22 +0200659int parse_cmd_option(const char *opt, const char *val,
Jens Axboe9f988e22010-03-04 10:42:38 +0100660 struct fio_option *options, struct flist_head *eops,
661 void *data)
Jens Axboeb4692822006-10-27 13:43:22 +0200662{
663 struct fio_option *o;
664
Jens Axboe9f988e22010-03-04 10:42:38 +0100665 o = find_option(options, eops, opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200666 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100667 fprintf(stderr, "Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200668 return 1;
669 }
670
Jens Axboeb1508cf2006-11-02 09:12:40 +0100671 if (!handle_option(o, val, data))
672 return 0;
673
674 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
675 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200676}
677
Aaron Carroll88b5a392008-10-07 11:25:20 +0200678/*
679 * Return a copy of the input string with substrings of the form ${VARNAME}
680 * substituted with the value of the environment variable VARNAME. The
681 * substitution always occurs, even if VARNAME is empty or the corresponding
682 * environment variable undefined.
683 */
684static char *option_dup_subs(const char *opt)
685{
686 char out[OPT_LEN_MAX+1];
687 char in[OPT_LEN_MAX+1];
688 char *outptr = out;
689 char *inptr = in;
690 char *ch1, *ch2, *env;
691 ssize_t nchr = OPT_LEN_MAX;
692 size_t envlen;
693
Jens Axboe38789b52010-03-03 09:23:20 +0100694 if (strlen(in) + 1 > OPT_LEN_MAX) {
695 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
696 return NULL;
697 }
698
Aaron Carroll88b5a392008-10-07 11:25:20 +0200699 in[OPT_LEN_MAX] = '\0';
700 strncpy(in, opt, OPT_LEN_MAX);
701
702 while (*inptr && nchr > 0) {
703 if (inptr[0] == '$' && inptr[1] == '{') {
704 ch2 = strchr(inptr, '}');
705 if (ch2 && inptr+1 < ch2) {
706 ch1 = inptr+2;
707 inptr = ch2+1;
708 *ch2 = '\0';
709
710 env = getenv(ch1);
711 if (env) {
712 envlen = strlen(env);
713 if (envlen <= nchr) {
714 memcpy(outptr, env, envlen);
715 outptr += envlen;
716 nchr -= envlen;
717 }
718 }
719
720 continue;
721 }
722 }
723
724 *outptr++ = *inptr++;
725 --nchr;
726 }
727
728 *outptr = '\0';
729 return strdup(out);
730}
731
Jens Axboe9f988e22010-03-04 10:42:38 +0100732int parse_option(const char *opt, struct fio_option *options,
733 struct flist_head *ext_opt_list, void *data)
Jens Axboee1f36502006-10-27 10:54:08 +0200734{
Jens Axboeb4692822006-10-27 13:43:22 +0200735 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200736 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200737
Aaron Carroll88b5a392008-10-07 11:25:20 +0200738 tmp = option_dup_subs(opt);
Jens Axboe38789b52010-03-03 09:23:20 +0100739 if (!tmp)
740 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200741
Jens Axboe9f988e22010-03-04 10:42:38 +0100742 o = get_option(tmp, options, ext_opt_list, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200743 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100744 fprintf(stderr, "Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200745 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200746 return 1;
747 }
748
Jens Axboe0401bca2007-03-29 11:00:43 +0200749 if (!handle_option(o, post, data)) {
750 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100751 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200752 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100753
754 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200755 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100756 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200757}
Jens Axboefd28ca42007-01-09 21:20:13 +0100758
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100759/*
760 * Option match, levenshtein distance. Handy for not quite remembering what
761 * the option name is.
762 */
763static int string_distance(const char *s1, const char *s2)
764{
765 unsigned int s1_len = strlen(s1);
766 unsigned int s2_len = strlen(s2);
767 unsigned int *p, *q, *r;
768 unsigned int i, j;
769
770 p = malloc(sizeof(unsigned int) * (s2_len + 1));
771 q = malloc(sizeof(unsigned int) * (s2_len + 1));
772
773 p[0] = 0;
774 for (i = 1; i <= s2_len; i++)
775 p[i] = p[i - 1] + 1;
776
777 for (i = 1; i <= s1_len; i++) {
778 q[0] = p[0] + 1;
779 for (j = 1; j <= s2_len; j++) {
780 unsigned int sub = p[j - 1];
781
782 if (s1[i - 1] != s2[j - 1])
783 sub++;
784
785 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
786 }
787 r = p;
788 p = q;
789 q = r;
790 }
791
792 i = p[s2_len];
793 free(p);
794 free(q);
795 return i;
796}
797
Jens Axboeafdf9352007-07-31 16:14:34 +0200798static struct fio_option *find_child(struct fio_option *options,
799 struct fio_option *o)
800{
801 struct fio_option *__o;
802
Jens Axboefdf28742007-07-31 23:06:09 +0200803 for (__o = options + 1; __o->name; __o++)
804 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200805 return __o;
806
807 return NULL;
808}
809
Jens Axboe323d9112008-03-01 15:12:14 +0100810static void __print_option(struct fio_option *o, struct fio_option *org,
811 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200812{
813 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100814 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200815
816 if (!o)
817 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200818 if (!org)
819 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100820
Jens Axboeafdf9352007-07-31 16:14:34 +0200821 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100822 depth = level;
823 while (depth--)
824 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200825
826 sprintf(p, "%s", o->name);
827
828 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100829}
830
831static void print_option(struct fio_option *o)
832{
833 struct fio_option *parent;
834 struct fio_option *__o;
835 unsigned int printed;
836 unsigned int level;
837
838 __print_option(o, NULL, 0);
839 parent = o;
840 level = 0;
841 do {
842 level++;
843 printed = 0;
844
845 while ((__o = find_child(o, parent)) != NULL) {
846 __print_option(__o, o, level);
847 o = __o;
848 printed++;
849 }
850
851 parent = o;
852 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200853}
854
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100855int show_cmd_help(struct fio_option *options, const char *name)
856{
857 struct fio_option *o, *closest;
858 unsigned int best_dist;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100859 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100860 int show_all = 0;
861
862 if (!name || !strcmp(name, "all"))
863 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100864
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100865 closest = NULL;
866 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100867 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100868 int match = 0;
869
Jens Axboe15ca1502008-04-07 09:26:02 +0200870 if (o->type == FIO_OPT_DEPRECATED)
871 continue;
872
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100873 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100874 if (!strcmp(name, o->name) ||
875 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100876 match = 1;
877 else {
878 unsigned int dist;
879
880 dist = string_distance(name, o->name);
881 if (dist < best_dist) {
882 best_dist = dist;
883 closest = o;
884 }
885 }
886 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100887
888 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100889 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100890 if (match)
Jens Axboeafdf9352007-07-31 16:14:34 +0200891 printf("%24s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +0100892 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +0200893 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +0100894 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +0100895 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100896 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100897 }
898
Jens Axboe70df2f12007-01-11 14:04:47 +0100899 if (!match)
900 continue;
901
Jens Axboed447a8c2009-07-17 22:26:15 +0200902 show_option_help(o, stdout);
Jens Axboefd28ca42007-01-09 21:20:13 +0100903 }
904
Jens Axboe29fc6af2007-01-09 21:22:02 +0100905 if (found)
906 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100907
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100908 printf("No such command: %s", name);
909 if (closest) {
910 printf(" - showing closest match\n");
911 printf("%20s: %s\n", closest->name, closest->help);
Jens Axboed447a8c2009-07-17 22:26:15 +0200912 show_option_help(closest, stdout);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100913 } else
914 printf("\n");
915
Jens Axboe29fc6af2007-01-09 21:22:02 +0100916 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100917}
Jens Axboeee738492007-01-10 11:23:16 +0100918
Jens Axboe13335dd2007-01-10 13:14:02 +0100919/*
920 * Handle parsing of default parameters.
921 */
Jens Axboeee738492007-01-10 11:23:16 +0100922void fill_default_options(void *data, struct fio_option *options)
923{
Jens Axboe4945ba12007-01-11 14:36:56 +0100924 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100925
Jens Axboea3d741f2008-02-27 18:32:33 +0100926 dprint(FD_PARSE, "filling default options\n");
927
Jens Axboe4945ba12007-01-11 14:36:56 +0100928 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100929 if (o->def)
930 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100931}
Jens Axboe13335dd2007-01-10 13:14:02 +0100932
Jens Axboe9f988e22010-03-04 10:42:38 +0100933void option_init(struct fio_option *o)
934{
935 if (o->type == FIO_OPT_DEPRECATED)
936 return;
937 if (o->type == FIO_OPT_BOOL) {
938 o->minval = 0;
939 o->maxval = 1;
940 }
941 if (o->type == FIO_OPT_STR_SET && o->def) {
942 fprintf(stderr, "Option %s: string set option with"
943 " default will always be true\n", o->name);
944 }
945 if (!o->cb && !o->off1) {
946 fprintf(stderr, "Option %s: neither cb nor offset given\n",
947 o->name);
948 }
949 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
950 return;
951 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
952 fprintf(stderr, "Option %s: both cb and offset given\n",
953 o->name);
954 }
955}
956
Jens Axboe13335dd2007-01-10 13:14:02 +0100957/*
958 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100959 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100960 */
961void options_init(struct fio_option *options)
962{
Jens Axboe4945ba12007-01-11 14:36:56 +0100963 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100964
Jens Axboea3d741f2008-02-27 18:32:33 +0100965 dprint(FD_PARSE, "init options\n");
966
Jens Axboe9f988e22010-03-04 10:42:38 +0100967 for (o = &options[0]; o->name; o++)
968 option_init(o);
Jens Axboe13335dd2007-01-10 13:14:02 +0100969}