blob: f5a35fbce7e9e086da5e4cdb2e6cf35a1ba69d66 [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 Axboea3073f42010-03-05 10:06:15 +010059 int i;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010060
Jens Axboea3073f42010-03-05 10:06:15 +010061 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboe78372132007-03-14 13:24:07 +010062 const struct value_pair *vp = &o->posval[i];
63
64 if (!vp->ival)
Jens Axboea3073f42010-03-05 10:06:15 +010065 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010066
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 Axboea3073f42010-03-05 10:06:15 +010071 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +010072
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[] = {
Jens Axboe07b32322010-03-05 09:48:44 +010080 "invalid",
Jens Axboed447a8c2009-07-17 22:26:15 +020081 "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)",
Jens Axboe07b32322010-03-05 09:48:44 +010089 "deprecated",
Jens Axboed447a8c2009-07-17 22:26:15 +020090 };
91
92 if (o->alias)
93 fprintf(out, "%20s: %s\n", "alias", o->alias);
94
95 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
96 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
Jens Axboe07b32322010-03-05 09:48:44 +010097 if (o->prof_name)
98 fprintf(out, "%20s: only for profile '%s'\n", "valid", o->prof_name);
Jens Axboed447a8c2009-07-17 22:26:15 +020099 show_option_range(o, stdout);
100 show_option_values(o);
101}
102
Jens Axboecb2c86f2006-10-26 13:48:34 +0200103static unsigned long get_mult_time(char c)
104{
105 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100106 case 'm':
107 case 'M':
108 return 60;
109 case 'h':
110 case 'H':
111 return 60 * 60;
112 case 'd':
113 case 'D':
114 return 24 * 60 * 60;
115 default:
116 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200117 }
118}
119
Jens Axboed6978a32009-07-18 21:04:09 +0200120static unsigned long long get_mult_bytes(char c, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200121{
Jens Axboed6978a32009-07-18 21:04:09 +0200122 unsigned int kb_base = fio_get_kb_base(data);
Jens Axboea639f0b2009-07-18 08:25:35 +0200123 unsigned long long ret = 1;
124
Jens Axboecb2c86f2006-10-26 13:48:34 +0200125 switch (c) {
Jens Axboea639f0b2009-07-18 08:25:35 +0200126 default:
127 break;
Jens Axboeb09da8f2009-07-17 23:16:17 +0200128 case 'p':
129 case 'P':
Jens Axboed6978a32009-07-18 21:04:09 +0200130 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200131 case 't':
132 case 'T':
Jens Axboed6978a32009-07-18 21:04:09 +0200133 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200134 case 'g':
135 case 'G':
Jens Axboed6978a32009-07-18 21:04:09 +0200136 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200137 case 'm':
138 case 'M':
Jens Axboed6978a32009-07-18 21:04:09 +0200139 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200140 case 'k':
141 case 'K':
Jens Axboed6978a32009-07-18 21:04:09 +0200142 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200143 break;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200144 }
Jens Axboea639f0b2009-07-18 08:25:35 +0200145
146 return ret;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200147}
148
149/*
Jens Axboee1f36502006-10-27 10:54:08 +0200150 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200151 */
Jens Axboed6978a32009-07-18 21:04:09 +0200152int str_to_decimal(const char *str, long long *val, int kilo, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200153{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100154 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200155
Jens Axboecb2c86f2006-10-26 13:48:34 +0200156 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100157 if (!len)
158 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200159
Jens Axboeb347f9d2009-03-09 14:15:21 +0100160 if (strstr(str, "0x") || strstr(str, "0X"))
161 base = 16;
162 else
163 base = 10;
164
165 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100166 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200167 return 1;
168
Jens Axboe975462a2009-12-23 08:54:52 +0100169 if (kilo) {
170 const char *p;
171 /*
172 * if the last char is 'b' or 'B', the user likely used
173 * "1gb" instead of just "1g". If the second to last is also
174 * a letter, adjust.
175 */
176 p = str + len - 1;
177 if ((*p == 'b' || *p == 'B') && isalpha(*(p - 1)))
178 --p;
179
180 *val *= get_mult_bytes(*p, data);
181 } else
Jens Axboecb2c86f2006-10-26 13:48:34 +0200182 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100183
Jens Axboecb2c86f2006-10-26 13:48:34 +0200184 return 0;
185}
186
Jens Axboed6978a32009-07-18 21:04:09 +0200187static int check_str_bytes(const char *p, long long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200188{
Jens Axboed6978a32009-07-18 21:04:09 +0200189 return str_to_decimal(p, val, 1, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200190}
191
Jens Axboe63f29372007-01-10 13:20:09 +0100192static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200193{
Jens Axboed6978a32009-07-18 21:04:09 +0200194 return str_to_decimal(p, val, 0, NULL);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200195}
196
197void strip_blank_front(char **p)
198{
199 char *s = *p;
200
201 while (isspace(*s))
202 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200203
204 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200205}
206
207void strip_blank_end(char *p)
208{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100209 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200210
Jens Axboe523bfad2007-04-04 11:04:06 +0200211 s = strchr(p, ';');
212 if (s)
213 *s = '\0';
214 s = strchr(p, '#');
215 if (s)
216 *s = '\0';
217 if (s)
218 p = s;
219
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200220 s = p + strlen(p);
Jens Axboe853ee7f2009-03-06 20:29:29 +0100221 while ((isspace(*s) || iscntrl(*s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200222 s--;
223
224 *(s + 1) = '\0';
225}
226
Jens Axboed6978a32009-07-18 21:04:09 +0200227static int check_range_bytes(const char *str, long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200228{
229 char suffix;
230
Jens Axboe787f7e92006-11-06 13:26:29 +0100231 if (!strlen(str))
232 return 1;
233
Jens Axboecb2c86f2006-10-26 13:48:34 +0200234 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
Jens Axboed6978a32009-07-18 21:04:09 +0200235 *val *= get_mult_bytes(suffix, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200236 return 0;
237 }
238
239 if (sscanf(str, "%lu", val) == 1)
240 return 0;
241
242 return 1;
243}
244
Jens Axboe63f29372007-01-10 13:20:09 +0100245static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200246{
Jens Axboe787f7e92006-11-06 13:26:29 +0100247 if (!strlen(p))
248 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200249 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200250 if (sscanf(p, "%x", val) == 1)
251 return 0;
252 } else {
253 if (sscanf(p, "%u", val) == 1)
254 return 0;
255 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200256
257 return 1;
258}
259
Jens Axboe9f988e22010-03-04 10:42:38 +0100260static inline int o_match(struct fio_option *o, const char *opt)
Jens Axboee1f36502006-10-27 10:54:08 +0200261{
Jens Axboe9f988e22010-03-04 10:42:38 +0100262 if (!strcmp(o->name, opt))
263 return 1;
264 else if (o->alias && !strcmp(o->alias, opt))
265 return 1;
266
267 return 0;
268}
269
270static struct fio_option *find_option(struct fio_option *options,
Jens Axboe07b32322010-03-05 09:48:44 +0100271 const char *opt)
Jens Axboe9f988e22010-03-04 10:42:38 +0100272{
Jens Axboe4945ba12007-01-11 14:36:56 +0100273 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200274
Jens Axboe9f988e22010-03-04 10:42:38 +0100275 for (o = &options[0]; o->name; o++)
276 if (o_match(o, opt))
Jens Axboee1f36502006-10-27 10:54:08 +0200277 return o;
Jens Axboe9f988e22010-03-04 10:42:38 +0100278
Jens Axboee1f36502006-10-27 10:54:08 +0200279 return NULL;
280}
281
Jens Axboe17abbe82006-11-06 11:23:10 +0100282#define val_store(ptr, val, off, data) \
283 do { \
284 ptr = td_var((data), (off)); \
285 *ptr = (val); \
286 } while (0)
287
Jens Axboef90eff52006-11-06 11:08:21 +0100288static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100289 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200290{
Jens Axboe63f29372007-01-10 13:20:09 +0100291 int il, *ilp;
292 long long ull, *ullp;
293 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200294 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200295 int ret = 0, is_time = 0;
296
Jens Axboea3d741f2008-02-27 18:32:33 +0100297 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
298 o->type, ptr);
299
Jens Axboee3cedca2008-11-19 19:57:52 +0100300 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe08e26e32006-11-21 13:15:10 +0100301 fprintf(stderr, "Option %s requires an argument\n", o->name);
302 return 1;
303 }
304
Jens Axboee1f36502006-10-27 10:54:08 +0200305 switch (o->type) {
306 case FIO_OPT_STR: {
307 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100308 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100309 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100310 int i;
Jens Axboee1f36502006-10-27 10:54:08 +0200311
Jens Axboef0857372007-03-19 13:15:23 +0100312 posval_sort(o, posval);
313
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100314 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100315 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100316 if (!vp->ival || vp->ival[0] == '\0')
Jens Axboea3073f42010-03-05 10:06:15 +0100317 continue;
Jens Axboe6612a272007-03-13 08:54:06 +0100318 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100319 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
320 ret = 0;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100321 if (o->roff1)
322 *(unsigned int *) o->roff1 = vp->oval;
323 else {
324 if (!o->off1)
325 break;
326 val_store(ilp, vp->oval, o->off1, data);
327 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100328 break;
329 }
330 }
331
332 if (ret)
333 show_option_values(o);
334 else if (fn)
335 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200336 break;
337 }
338 case FIO_OPT_STR_VAL_TIME:
339 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100340 case FIO_OPT_INT:
Jens Axboee01b22b2009-06-09 15:43:25 +0200341 case FIO_OPT_STR_VAL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200342 fio_opt_str_val_fn *fn = o->cb;
343
344 if (is_time)
345 ret = check_str_time(ptr, &ull);
346 else
Jens Axboed6978a32009-07-18 21:04:09 +0200347 ret = check_str_bytes(ptr, &ull, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200348
349 if (ret)
350 break;
351
Jens Axboedb8e0162007-01-10 13:41:26 +0100352 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100353 fprintf(stderr, "max value out of range: %lld"
354 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100355 return 1;
356 }
357 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100358 fprintf(stderr, "min value out of range: %lld"
359 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100360 return 1;
361 }
Jens Axboee1f36502006-10-27 10:54:08 +0200362
363 if (fn)
364 ret = fn(data, &ull);
365 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200366 if (o->type == FIO_OPT_INT) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100367 if (first) {
368 if (o->roff1)
369 *(unsigned long long *) o->roff1 = ull;
370 else
371 val_store(ilp, ull, o->off1, data);
372 }
373 if (!more) {
374 if (o->roff2)
375 *(unsigned long long *) o->roff2 = ull;
376 else if (o->off2)
377 val_store(ilp, ull, o->off2, data);
378 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100379 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100380 if (first) {
381 if (o->roff1)
382 *(unsigned long long *) o->roff1 = ull;
383 else
384 val_store(ullp, ull, o->off1, data);
385 }
386 if (!more) {
387 if (o->roff2)
388 *(unsigned long long *) o->roff2 = ull;
389 else if (o->off2)
390 val_store(ullp, ull, o->off2, data);
391 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100392 }
Jens Axboee1f36502006-10-27 10:54:08 +0200393 }
394 break;
395 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100396 case FIO_OPT_STR_STORE: {
397 fio_opt_str_fn *fn = o->cb;
398
Jens Axboe02c6aad2010-03-04 13:49:11 +0100399 if (o->roff1)
400 cp = (char **) o->roff1;
401 else
402 cp = td_var(data, o->off1);
403
Jens Axboee1f36502006-10-27 10:54:08 +0200404 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100405 if (fn) {
406 ret = fn(data, ptr);
407 if (ret) {
408 free(*cp);
409 *cp = NULL;
410 }
411 }
Jens Axboee1f36502006-10-27 10:54:08 +0200412 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100413 }
Jens Axboee1f36502006-10-27 10:54:08 +0200414 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200415 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200416 char *p1, *p2;
417
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100418 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200419
420 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200421 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100422 p1 = strchr(tmp, ':');
423 if (!p1) {
424 ret = 1;
425 break;
426 }
Jens Axboee1f36502006-10-27 10:54:08 +0200427 }
428
429 p2 = p1 + 1;
430 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200431 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200432
433 ret = 1;
Jens Axboed6978a32009-07-18 21:04:09 +0200434 if (!check_range_bytes(p1, &ul1, data) &&
435 !check_range_bytes(p2, &ul2, data)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200436 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200437 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100438 unsigned long foo = ul1;
439
440 ul1 = ul2;
441 ul2 = foo;
442 }
443
444 if (first) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100445 if (o->roff1)
446 *(unsigned long *) o->roff1 = ul1;
447 else
448 val_store(ilp, ul1, o->off1, data);
449 if (o->roff2)
450 *(unsigned long *) o->roff2 = ul2;
451 else
452 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200453 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100454 if (o->roff3 && o->roff4) {
455 *(unsigned long *) o->roff3 = ul1;
456 *(unsigned long *) o->roff4 = ul2;
457 } else if (o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100458 val_store(ilp, ul1, o->off3, data);
459 val_store(ilp, ul2, o->off4, data);
460 }
461 }
462
Jens Axboee1f36502006-10-27 10:54:08 +0200463 break;
464 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100465 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200466 fio_opt_int_fn *fn = o->cb;
467
468 ret = check_int(ptr, &il);
469 if (ret)
470 break;
471
Jens Axboedb8e0162007-01-10 13:41:26 +0100472 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100473 fprintf(stderr, "max value out of range: %d (%d max)\n",
474 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100475 return 1;
476 }
477 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100478 fprintf(stderr, "min value out of range: %d (%d min)\n",
479 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100480 return 1;
481 }
Jens Axboee1f36502006-10-27 10:54:08 +0200482
Jens Axboe76a43db2007-01-11 13:24:44 +0100483 if (o->neg)
484 il = !il;
485
Jens Axboee1f36502006-10-27 10:54:08 +0200486 if (fn)
487 ret = fn(data, &il);
488 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100489 if (first) {
490 if (o->roff1)
491 *(unsigned int *)o->roff1 = il;
492 else
493 val_store(ilp, il, o->off1, data);
494 }
495 if (!more) {
496 if (o->roff2)
497 *(unsigned int *) o->roff2 = il;
498 else if (o->off2)
499 val_store(ilp, il, o->off2, data);
500 }
Jens Axboee1f36502006-10-27 10:54:08 +0200501 }
502 break;
503 }
504 case FIO_OPT_STR_SET: {
505 fio_opt_str_set_fn *fn = o->cb;
506
507 if (fn)
508 ret = fn(data);
509 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100510 if (first) {
511 if (o->roff1)
512 *(unsigned int *) o->roff1 = 1;
513 else
514 val_store(ilp, 1, o->off1, data);
515 }
516 if (!more) {
517 if (o->roff2)
518 *(unsigned int *) o->roff2 = 1;
519 else if (o->off2)
520 val_store(ilp, 1, o->off2, data);
521 }
Jens Axboee1f36502006-10-27 10:54:08 +0200522 }
523 break;
524 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200525 case FIO_OPT_DEPRECATED:
526 fprintf(stdout, "Option %s is deprecated\n", o->name);
527 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200528 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100529 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200530 ret = 1;
531 }
532
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200533 if (ret)
534 return ret;
535
Jens Axboed447a8c2009-07-17 22:26:15 +0200536 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200537 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200538 if (ret) {
539 fprintf(stderr,"Correct format for offending option\n");
540 fprintf(stderr, "%20s: %s\n", o->name, o->help);
541 show_option_help(o, stderr);
542 }
543 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200544
Jens Axboee1f36502006-10-27 10:54:08 +0200545 return ret;
546}
547
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200548static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100549{
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200550 char *ptr, *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100551 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100552
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200553 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
554
555 ptr = NULL;
556 if (__ptr)
557 ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100558
Jens Axboef90eff52006-11-06 11:08:21 +0100559 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100560 * See if we have a second set of parameters, hidden after a comma.
561 * Do this before parsing the first round, to check if we should
562 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100563 */
Joel Beckerad231bc2007-03-01 08:22:42 +0100564 if (ptr &&
565 (o->type != FIO_OPT_STR_STORE) &&
566 (o->type != FIO_OPT_STR)) {
Jens Axboe92b586f2006-11-07 14:29:17 +0100567 ptr2 = strchr(ptr, ',');
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200568 if (ptr2 && *(ptr2 + 1) == '\0')
569 *ptr2 = '\0';
Jens Axboe0c9baf92007-01-11 15:59:26 +0100570 if (!ptr2)
571 ptr2 = strchr(ptr, ':');
Jens Axboeb486f762007-03-15 09:15:12 +0100572 if (!ptr2)
573 ptr2 = strchr(ptr, '-');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100574 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100575
576 /*
577 * Don't return early if parsing the first option fails - if
578 * we are doing multiple arguments, we can allow the first one
579 * being empty.
580 */
581 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
582
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200583 if (!ptr2) {
584 if (ptr)
585 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100586 return r1;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200587 }
Jens Axboef90eff52006-11-06 11:08:21 +0100588
589 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100590 r2 = __handle_option(o, ptr2, data, 0, 0);
591
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200592 if (ptr)
593 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100594 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100595}
596
Jens Axboe3b8b7132008-06-10 19:46:23 +0200597static struct fio_option *get_option(const char *opt,
Jens Axboe07b32322010-03-05 09:48:44 +0100598 struct fio_option *options, char **post)
Jens Axboe3b8b7132008-06-10 19:46:23 +0200599{
600 struct fio_option *o;
601 char *ret;
602
603 ret = strchr(opt, '=');
604 if (ret) {
605 *post = ret;
606 *ret = '\0';
607 ret = (char *) opt;
608 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100609 strip_blank_end(ret);
Jens Axboe07b32322010-03-05 09:48:44 +0100610 o = find_option(options, ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200611 } else {
Jens Axboe07b32322010-03-05 09:48:44 +0100612 o = find_option(options, opt);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200613 *post = NULL;
614 }
615
616 return o;
617}
618
619static int opt_cmp(const void *p1, const void *p2)
620{
621 struct fio_option *o1, *o2;
622 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100623 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200624
625 s1 = strdup(*((char **) p1));
626 s2 = strdup(*((char **) p2));
627
Jens Axboe07b32322010-03-05 09:48:44 +0100628 o1 = get_option(s1, fio_options, &foo);
629 o2 = get_option(s2, fio_options, &foo);
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200630
Jens Axboe8cdabc12008-11-19 12:31:43 +0100631 prio1 = prio2 = 0;
632 if (o1)
633 prio1 = o1->prio;
634 if (o2)
635 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200636
637 free(s1);
638 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100639 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200640}
641
642void sort_options(char **opts, struct fio_option *options, int num_opts)
643{
644 fio_options = options;
645 qsort(opts, num_opts, sizeof(char *), opt_cmp);
646 fio_options = NULL;
647}
648
Jens Axboeb4692822006-10-27 13:43:22 +0200649int parse_cmd_option(const char *opt, const char *val,
Jens Axboe07b32322010-03-05 09:48:44 +0100650 struct fio_option *options, void *data)
Jens Axboeb4692822006-10-27 13:43:22 +0200651{
652 struct fio_option *o;
653
Jens Axboe07b32322010-03-05 09:48:44 +0100654 o = find_option(options, opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200655 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100656 fprintf(stderr, "Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200657 return 1;
658 }
659
Jens Axboeb1508cf2006-11-02 09:12:40 +0100660 if (!handle_option(o, val, data))
661 return 0;
662
663 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
664 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200665}
666
Aaron Carroll88b5a392008-10-07 11:25:20 +0200667/*
668 * Return a copy of the input string with substrings of the form ${VARNAME}
669 * substituted with the value of the environment variable VARNAME. The
670 * substitution always occurs, even if VARNAME is empty or the corresponding
671 * environment variable undefined.
672 */
673static char *option_dup_subs(const char *opt)
674{
675 char out[OPT_LEN_MAX+1];
676 char in[OPT_LEN_MAX+1];
677 char *outptr = out;
678 char *inptr = in;
679 char *ch1, *ch2, *env;
680 ssize_t nchr = OPT_LEN_MAX;
681 size_t envlen;
682
Jens Axboe38789b52010-03-03 09:23:20 +0100683 if (strlen(in) + 1 > OPT_LEN_MAX) {
684 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
685 return NULL;
686 }
687
Aaron Carroll88b5a392008-10-07 11:25:20 +0200688 in[OPT_LEN_MAX] = '\0';
689 strncpy(in, opt, OPT_LEN_MAX);
690
691 while (*inptr && nchr > 0) {
692 if (inptr[0] == '$' && inptr[1] == '{') {
693 ch2 = strchr(inptr, '}');
694 if (ch2 && inptr+1 < ch2) {
695 ch1 = inptr+2;
696 inptr = ch2+1;
697 *ch2 = '\0';
698
699 env = getenv(ch1);
700 if (env) {
701 envlen = strlen(env);
702 if (envlen <= nchr) {
703 memcpy(outptr, env, envlen);
704 outptr += envlen;
705 nchr -= envlen;
706 }
707 }
708
709 continue;
710 }
711 }
712
713 *outptr++ = *inptr++;
714 --nchr;
715 }
716
717 *outptr = '\0';
718 return strdup(out);
719}
720
Jens Axboe07b32322010-03-05 09:48:44 +0100721int parse_option(const char *opt, struct fio_option *options, void *data)
Jens Axboee1f36502006-10-27 10:54:08 +0200722{
Jens Axboeb4692822006-10-27 13:43:22 +0200723 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200724 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200725
Aaron Carroll88b5a392008-10-07 11:25:20 +0200726 tmp = option_dup_subs(opt);
Jens Axboe38789b52010-03-03 09:23:20 +0100727 if (!tmp)
728 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200729
Jens Axboe07b32322010-03-05 09:48:44 +0100730 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200731 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100732 fprintf(stderr, "Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200733 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200734 return 1;
735 }
736
Jens Axboe0401bca2007-03-29 11:00:43 +0200737 if (!handle_option(o, post, data)) {
738 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100739 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200740 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100741
742 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200743 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100744 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200745}
Jens Axboefd28ca42007-01-09 21:20:13 +0100746
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100747/*
748 * Option match, levenshtein distance. Handy for not quite remembering what
749 * the option name is.
750 */
751static int string_distance(const char *s1, const char *s2)
752{
753 unsigned int s1_len = strlen(s1);
754 unsigned int s2_len = strlen(s2);
755 unsigned int *p, *q, *r;
756 unsigned int i, j;
757
758 p = malloc(sizeof(unsigned int) * (s2_len + 1));
759 q = malloc(sizeof(unsigned int) * (s2_len + 1));
760
761 p[0] = 0;
762 for (i = 1; i <= s2_len; i++)
763 p[i] = p[i - 1] + 1;
764
765 for (i = 1; i <= s1_len; i++) {
766 q[0] = p[0] + 1;
767 for (j = 1; j <= s2_len; j++) {
768 unsigned int sub = p[j - 1];
769
770 if (s1[i - 1] != s2[j - 1])
771 sub++;
772
773 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
774 }
775 r = p;
776 p = q;
777 q = r;
778 }
779
780 i = p[s2_len];
781 free(p);
782 free(q);
783 return i;
784}
785
Jens Axboeafdf9352007-07-31 16:14:34 +0200786static struct fio_option *find_child(struct fio_option *options,
787 struct fio_option *o)
788{
789 struct fio_option *__o;
790
Jens Axboefdf28742007-07-31 23:06:09 +0200791 for (__o = options + 1; __o->name; __o++)
792 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200793 return __o;
794
795 return NULL;
796}
797
Jens Axboe323d9112008-03-01 15:12:14 +0100798static void __print_option(struct fio_option *o, struct fio_option *org,
799 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200800{
801 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100802 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200803
804 if (!o)
805 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200806 if (!org)
807 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100808
Jens Axboeafdf9352007-07-31 16:14:34 +0200809 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100810 depth = level;
811 while (depth--)
812 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200813
814 sprintf(p, "%s", o->name);
815
816 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100817}
818
819static void print_option(struct fio_option *o)
820{
821 struct fio_option *parent;
822 struct fio_option *__o;
823 unsigned int printed;
824 unsigned int level;
825
826 __print_option(o, NULL, 0);
827 parent = o;
828 level = 0;
829 do {
830 level++;
831 printed = 0;
832
833 while ((__o = find_child(o, parent)) != NULL) {
834 __print_option(__o, o, level);
835 o = __o;
836 printed++;
837 }
838
839 parent = o;
840 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200841}
842
Jens Axboe07b32322010-03-05 09:48:44 +0100843int show_cmd_help(struct fio_option *options, const char *name)
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100844{
845 struct fio_option *o, *closest;
846 unsigned int best_dist;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100847 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100848 int show_all = 0;
849
850 if (!name || !strcmp(name, "all"))
851 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100852
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100853 closest = NULL;
854 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100855 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100856 int match = 0;
857
Jens Axboe15ca1502008-04-07 09:26:02 +0200858 if (o->type == FIO_OPT_DEPRECATED)
859 continue;
Jens Axboe07b32322010-03-05 09:48:44 +0100860 if (!exec_profile && o->prof_name)
861 continue;
Jens Axboe15ca1502008-04-07 09:26:02 +0200862
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100863 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100864 if (!strcmp(name, o->name) ||
865 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100866 match = 1;
867 else {
868 unsigned int dist;
869
870 dist = string_distance(name, o->name);
871 if (dist < best_dist) {
872 best_dist = dist;
873 closest = o;
874 }
875 }
876 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100877
878 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100879 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100880 if (match)
Jens Axboe07b32322010-03-05 09:48:44 +0100881 printf("%20s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +0100882 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +0200883 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +0100884 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +0100885 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100886 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100887 }
888
Jens Axboe70df2f12007-01-11 14:04:47 +0100889 if (!match)
890 continue;
891
Jens Axboed447a8c2009-07-17 22:26:15 +0200892 show_option_help(o, stdout);
Jens Axboefd28ca42007-01-09 21:20:13 +0100893 }
894
Jens Axboe29fc6af2007-01-09 21:22:02 +0100895 if (found)
896 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100897
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100898 printf("No such command: %s", name);
899 if (closest) {
900 printf(" - showing closest match\n");
901 printf("%20s: %s\n", closest->name, closest->help);
Jens Axboed447a8c2009-07-17 22:26:15 +0200902 show_option_help(closest, stdout);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100903 } else
904 printf("\n");
905
Jens Axboe29fc6af2007-01-09 21:22:02 +0100906 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100907}
Jens Axboeee738492007-01-10 11:23:16 +0100908
Jens Axboe13335dd2007-01-10 13:14:02 +0100909/*
910 * Handle parsing of default parameters.
911 */
Jens Axboeee738492007-01-10 11:23:16 +0100912void fill_default_options(void *data, struct fio_option *options)
913{
Jens Axboe4945ba12007-01-11 14:36:56 +0100914 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100915
Jens Axboea3d741f2008-02-27 18:32:33 +0100916 dprint(FD_PARSE, "filling default options\n");
917
Jens Axboe4945ba12007-01-11 14:36:56 +0100918 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100919 if (o->def)
920 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100921}
Jens Axboe13335dd2007-01-10 13:14:02 +0100922
Jens Axboe9f988e22010-03-04 10:42:38 +0100923void option_init(struct fio_option *o)
924{
925 if (o->type == FIO_OPT_DEPRECATED)
926 return;
927 if (o->type == FIO_OPT_BOOL) {
928 o->minval = 0;
929 o->maxval = 1;
930 }
931 if (o->type == FIO_OPT_STR_SET && o->def) {
932 fprintf(stderr, "Option %s: string set option with"
933 " default will always be true\n", o->name);
934 }
Jens Axboee2de69d2010-03-04 14:05:48 +0100935 if (!o->cb && (!o->off1 && !o->roff1)) {
Jens Axboe9f988e22010-03-04 10:42:38 +0100936 fprintf(stderr, "Option %s: neither cb nor offset given\n",
937 o->name);
938 }
939 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
940 return;
Jens Axboee2de69d2010-03-04 14:05:48 +0100941 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
942 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
Jens Axboe9f988e22010-03-04 10:42:38 +0100943 fprintf(stderr, "Option %s: both cb and offset given\n",
944 o->name);
945 }
946}
947
Jens Axboe13335dd2007-01-10 13:14:02 +0100948/*
949 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100950 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100951 */
952void options_init(struct fio_option *options)
953{
Jens Axboe4945ba12007-01-11 14:36:56 +0100954 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100955
Jens Axboea3d741f2008-02-27 18:32:33 +0100956 dprint(FD_PARSE, "init options\n");
957
Jens Axboe9f988e22010-03-04 10:42:38 +0100958 for (o = &options[0]; o->name; o++)
959 option_init(o);
Jens Axboe13335dd2007-01-10 13:14:02 +0100960}