blob: 98e623a0162a481e182f9a12c63dfbe69a81ec57 [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[] = {
Jens Axboe07b32322010-03-05 09:48:44 +010081 "invalid",
Jens Axboed447a8c2009-07-17 22:26:15 +020082 "string (opt=bla)",
83 "string with possible k/m/g postfix (opt=4k)",
84 "string with time postfix (opt=10s)",
85 "string (opt=bla)",
86 "string with dual range (opt=1k-4k,4k-8k)",
87 "integer value (opt=100)",
88 "boolean value (opt=1)",
89 "no argument (opt)",
Jens Axboe07b32322010-03-05 09:48:44 +010090 "deprecated",
Jens Axboed447a8c2009-07-17 22:26:15 +020091 };
92
93 if (o->alias)
94 fprintf(out, "%20s: %s\n", "alias", o->alias);
95
96 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
97 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
Jens Axboe07b32322010-03-05 09:48:44 +010098 if (o->prof_name)
99 fprintf(out, "%20s: only for profile '%s'\n", "valid", o->prof_name);
Jens Axboed447a8c2009-07-17 22:26:15 +0200100 show_option_range(o, stdout);
101 show_option_values(o);
102}
103
Jens Axboecb2c86f2006-10-26 13:48:34 +0200104static unsigned long get_mult_time(char c)
105{
106 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100107 case 'm':
108 case 'M':
109 return 60;
110 case 'h':
111 case 'H':
112 return 60 * 60;
113 case 'd':
114 case 'D':
115 return 24 * 60 * 60;
116 default:
117 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200118 }
119}
120
Jens Axboed6978a32009-07-18 21:04:09 +0200121static unsigned long long get_mult_bytes(char c, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200122{
Jens Axboed6978a32009-07-18 21:04:09 +0200123 unsigned int kb_base = fio_get_kb_base(data);
Jens Axboea639f0b2009-07-18 08:25:35 +0200124 unsigned long long ret = 1;
125
Jens Axboecb2c86f2006-10-26 13:48:34 +0200126 switch (c) {
Jens Axboea639f0b2009-07-18 08:25:35 +0200127 default:
128 break;
Jens Axboeb09da8f2009-07-17 23:16:17 +0200129 case 'p':
130 case 'P':
Jens Axboed6978a32009-07-18 21:04:09 +0200131 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200132 case 't':
133 case 'T':
Jens Axboed6978a32009-07-18 21:04:09 +0200134 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200135 case 'g':
136 case 'G':
Jens Axboed6978a32009-07-18 21:04:09 +0200137 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200138 case 'm':
139 case 'M':
Jens Axboed6978a32009-07-18 21:04:09 +0200140 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200141 case 'k':
142 case 'K':
Jens Axboed6978a32009-07-18 21:04:09 +0200143 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200144 break;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200145 }
Jens Axboea639f0b2009-07-18 08:25:35 +0200146
147 return ret;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200148}
149
150/*
Jens Axboee1f36502006-10-27 10:54:08 +0200151 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200152 */
Jens Axboed6978a32009-07-18 21:04:09 +0200153int str_to_decimal(const char *str, long long *val, int kilo, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200154{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100155 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200156
Jens Axboecb2c86f2006-10-26 13:48:34 +0200157 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100158 if (!len)
159 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200160
Jens Axboeb347f9d2009-03-09 14:15:21 +0100161 if (strstr(str, "0x") || strstr(str, "0X"))
162 base = 16;
163 else
164 base = 10;
165
166 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100167 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200168 return 1;
169
Jens Axboe975462a2009-12-23 08:54:52 +0100170 if (kilo) {
171 const char *p;
172 /*
173 * if the last char is 'b' or 'B', the user likely used
174 * "1gb" instead of just "1g". If the second to last is also
175 * a letter, adjust.
176 */
177 p = str + len - 1;
178 if ((*p == 'b' || *p == 'B') && isalpha(*(p - 1)))
179 --p;
180
181 *val *= get_mult_bytes(*p, data);
182 } else
Jens Axboecb2c86f2006-10-26 13:48:34 +0200183 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100184
Jens Axboecb2c86f2006-10-26 13:48:34 +0200185 return 0;
186}
187
Jens Axboed6978a32009-07-18 21:04:09 +0200188static int check_str_bytes(const char *p, long long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200189{
Jens Axboed6978a32009-07-18 21:04:09 +0200190 return str_to_decimal(p, val, 1, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200191}
192
Jens Axboe63f29372007-01-10 13:20:09 +0100193static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200194{
Jens Axboed6978a32009-07-18 21:04:09 +0200195 return str_to_decimal(p, val, 0, NULL);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200196}
197
198void strip_blank_front(char **p)
199{
200 char *s = *p;
201
202 while (isspace(*s))
203 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200204
205 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200206}
207
208void strip_blank_end(char *p)
209{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100210 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200211
Jens Axboe523bfad2007-04-04 11:04:06 +0200212 s = strchr(p, ';');
213 if (s)
214 *s = '\0';
215 s = strchr(p, '#');
216 if (s)
217 *s = '\0';
218 if (s)
219 p = s;
220
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200221 s = p + strlen(p);
Jens Axboe853ee7f2009-03-06 20:29:29 +0100222 while ((isspace(*s) || iscntrl(*s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200223 s--;
224
225 *(s + 1) = '\0';
226}
227
Jens Axboed6978a32009-07-18 21:04:09 +0200228static int check_range_bytes(const char *str, long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200229{
230 char suffix;
231
Jens Axboe787f7e92006-11-06 13:26:29 +0100232 if (!strlen(str))
233 return 1;
234
Jens Axboecb2c86f2006-10-26 13:48:34 +0200235 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
Jens Axboed6978a32009-07-18 21:04:09 +0200236 *val *= get_mult_bytes(suffix, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200237 return 0;
238 }
239
240 if (sscanf(str, "%lu", val) == 1)
241 return 0;
242
243 return 1;
244}
245
Jens Axboe63f29372007-01-10 13:20:09 +0100246static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200247{
Jens Axboe787f7e92006-11-06 13:26:29 +0100248 if (!strlen(p))
249 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200250 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200251 if (sscanf(p, "%x", val) == 1)
252 return 0;
253 } else {
254 if (sscanf(p, "%u", val) == 1)
255 return 0;
256 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200257
258 return 1;
259}
260
Jens Axboe9f988e22010-03-04 10:42:38 +0100261static inline int o_match(struct fio_option *o, const char *opt)
Jens Axboee1f36502006-10-27 10:54:08 +0200262{
Jens Axboe9f988e22010-03-04 10:42:38 +0100263 if (!strcmp(o->name, opt))
264 return 1;
265 else if (o->alias && !strcmp(o->alias, opt))
266 return 1;
267
268 return 0;
269}
270
271static struct fio_option *find_option(struct fio_option *options,
Jens Axboe07b32322010-03-05 09:48:44 +0100272 const char *opt)
Jens Axboe9f988e22010-03-04 10:42:38 +0100273{
Jens Axboe4945ba12007-01-11 14:36:56 +0100274 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200275
Jens Axboe9f988e22010-03-04 10:42:38 +0100276 for (o = &options[0]; o->name; o++)
277 if (o_match(o, opt))
Jens Axboee1f36502006-10-27 10:54:08 +0200278 return o;
Jens Axboe9f988e22010-03-04 10:42:38 +0100279
Jens Axboee1f36502006-10-27 10:54:08 +0200280 return NULL;
281}
282
Jens Axboe17abbe82006-11-06 11:23:10 +0100283#define val_store(ptr, val, off, data) \
284 do { \
285 ptr = td_var((data), (off)); \
286 *ptr = (val); \
287 } while (0)
288
Jens Axboef90eff52006-11-06 11:08:21 +0100289static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100290 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200291{
Jens Axboe63f29372007-01-10 13:20:09 +0100292 int il, *ilp;
293 long long ull, *ullp;
294 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200295 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200296 int ret = 0, is_time = 0;
297
Jens Axboea3d741f2008-02-27 18:32:33 +0100298 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
299 o->type, ptr);
300
Jens Axboee3cedca2008-11-19 19:57:52 +0100301 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe08e26e32006-11-21 13:15:10 +0100302 fprintf(stderr, "Option %s requires an argument\n", o->name);
303 return 1;
304 }
305
Jens Axboee1f36502006-10-27 10:54:08 +0200306 switch (o->type) {
307 case FIO_OPT_STR: {
308 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100309 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100310 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100311 int i;
Jens Axboee1f36502006-10-27 10:54:08 +0200312
Jens Axboef0857372007-03-19 13:15:23 +0100313 posval_sort(o, posval);
314
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100315 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100316 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100317 if (!vp->ival || vp->ival[0] == '\0')
318 break;
Jens Axboe6612a272007-03-13 08:54:06 +0100319 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100320 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
321 ret = 0;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100322 if (o->roff1)
323 *(unsigned int *) o->roff1 = vp->oval;
324 else {
325 if (!o->off1)
326 break;
327 val_store(ilp, vp->oval, o->off1, data);
328 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100329 break;
330 }
331 }
332
333 if (ret)
334 show_option_values(o);
335 else if (fn)
336 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200337 break;
338 }
339 case FIO_OPT_STR_VAL_TIME:
340 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100341 case FIO_OPT_INT:
Jens Axboee01b22b2009-06-09 15:43:25 +0200342 case FIO_OPT_STR_VAL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200343 fio_opt_str_val_fn *fn = o->cb;
344
345 if (is_time)
346 ret = check_str_time(ptr, &ull);
347 else
Jens Axboed6978a32009-07-18 21:04:09 +0200348 ret = check_str_bytes(ptr, &ull, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200349
350 if (ret)
351 break;
352
Jens Axboedb8e0162007-01-10 13:41:26 +0100353 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100354 fprintf(stderr, "max value out of range: %lld"
355 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100356 return 1;
357 }
358 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100359 fprintf(stderr, "min value out of range: %lld"
360 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100361 return 1;
362 }
Jens Axboee1f36502006-10-27 10:54:08 +0200363
364 if (fn)
365 ret = fn(data, &ull);
366 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200367 if (o->type == FIO_OPT_INT) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100368 if (first) {
369 if (o->roff1)
370 *(unsigned long long *) o->roff1 = ull;
371 else
372 val_store(ilp, ull, o->off1, data);
373 }
374 if (!more) {
375 if (o->roff2)
376 *(unsigned long long *) o->roff2 = ull;
377 else if (o->off2)
378 val_store(ilp, ull, o->off2, data);
379 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100380 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100381 if (first) {
382 if (o->roff1)
383 *(unsigned long long *) o->roff1 = ull;
384 else
385 val_store(ullp, ull, o->off1, data);
386 }
387 if (!more) {
388 if (o->roff2)
389 *(unsigned long long *) o->roff2 = ull;
390 else if (o->off2)
391 val_store(ullp, ull, o->off2, data);
392 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100393 }
Jens Axboee1f36502006-10-27 10:54:08 +0200394 }
395 break;
396 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100397 case FIO_OPT_STR_STORE: {
398 fio_opt_str_fn *fn = o->cb;
399
Jens Axboe02c6aad2010-03-04 13:49:11 +0100400 if (o->roff1)
401 cp = (char **) o->roff1;
402 else
403 cp = td_var(data, o->off1);
404
Jens Axboee1f36502006-10-27 10:54:08 +0200405 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100406 if (fn) {
407 ret = fn(data, ptr);
408 if (ret) {
409 free(*cp);
410 *cp = NULL;
411 }
412 }
Jens Axboee1f36502006-10-27 10:54:08 +0200413 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100414 }
Jens Axboee1f36502006-10-27 10:54:08 +0200415 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200416 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200417 char *p1, *p2;
418
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100419 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200420
421 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200422 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100423 p1 = strchr(tmp, ':');
424 if (!p1) {
425 ret = 1;
426 break;
427 }
Jens Axboee1f36502006-10-27 10:54:08 +0200428 }
429
430 p2 = p1 + 1;
431 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200432 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200433
434 ret = 1;
Jens Axboed6978a32009-07-18 21:04:09 +0200435 if (!check_range_bytes(p1, &ul1, data) &&
436 !check_range_bytes(p2, &ul2, data)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200437 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200438 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100439 unsigned long foo = ul1;
440
441 ul1 = ul2;
442 ul2 = foo;
443 }
444
445 if (first) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100446 if (o->roff1)
447 *(unsigned long *) o->roff1 = ul1;
448 else
449 val_store(ilp, ul1, o->off1, data);
450 if (o->roff2)
451 *(unsigned long *) o->roff2 = ul2;
452 else
453 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200454 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100455 if (o->roff3 && o->roff4) {
456 *(unsigned long *) o->roff3 = ul1;
457 *(unsigned long *) o->roff4 = ul2;
458 } else if (o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100459 val_store(ilp, ul1, o->off3, data);
460 val_store(ilp, ul2, o->off4, data);
461 }
462 }
463
Jens Axboee1f36502006-10-27 10:54:08 +0200464 break;
465 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100466 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200467 fio_opt_int_fn *fn = o->cb;
468
469 ret = check_int(ptr, &il);
470 if (ret)
471 break;
472
Jens Axboedb8e0162007-01-10 13:41:26 +0100473 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100474 fprintf(stderr, "max value out of range: %d (%d max)\n",
475 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100476 return 1;
477 }
478 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100479 fprintf(stderr, "min value out of range: %d (%d min)\n",
480 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100481 return 1;
482 }
Jens Axboee1f36502006-10-27 10:54:08 +0200483
Jens Axboe76a43db2007-01-11 13:24:44 +0100484 if (o->neg)
485 il = !il;
486
Jens Axboee1f36502006-10-27 10:54:08 +0200487 if (fn)
488 ret = fn(data, &il);
489 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100490 if (first) {
491 if (o->roff1)
492 *(unsigned int *)o->roff1 = il;
493 else
494 val_store(ilp, il, o->off1, data);
495 }
496 if (!more) {
497 if (o->roff2)
498 *(unsigned int *) o->roff2 = il;
499 else if (o->off2)
500 val_store(ilp, il, o->off2, data);
501 }
Jens Axboee1f36502006-10-27 10:54:08 +0200502 }
503 break;
504 }
505 case FIO_OPT_STR_SET: {
506 fio_opt_str_set_fn *fn = o->cb;
507
508 if (fn)
509 ret = fn(data);
510 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100511 if (first) {
512 if (o->roff1)
513 *(unsigned int *) o->roff1 = 1;
514 else
515 val_store(ilp, 1, o->off1, data);
516 }
517 if (!more) {
518 if (o->roff2)
519 *(unsigned int *) o->roff2 = 1;
520 else if (o->off2)
521 val_store(ilp, 1, o->off2, data);
522 }
Jens Axboee1f36502006-10-27 10:54:08 +0200523 }
524 break;
525 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200526 case FIO_OPT_DEPRECATED:
527 fprintf(stdout, "Option %s is deprecated\n", o->name);
528 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200529 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100530 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200531 ret = 1;
532 }
533
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200534 if (ret)
535 return ret;
536
Jens Axboed447a8c2009-07-17 22:26:15 +0200537 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200538 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200539 if (ret) {
540 fprintf(stderr,"Correct format for offending option\n");
541 fprintf(stderr, "%20s: %s\n", o->name, o->help);
542 show_option_help(o, stderr);
543 }
544 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200545
Jens Axboee1f36502006-10-27 10:54:08 +0200546 return ret;
547}
548
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200549static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100550{
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200551 char *ptr, *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100552 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100553
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200554 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
555
556 ptr = NULL;
557 if (__ptr)
558 ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100559
Jens Axboef90eff52006-11-06 11:08:21 +0100560 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100561 * See if we have a second set of parameters, hidden after a comma.
562 * Do this before parsing the first round, to check if we should
563 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100564 */
Joel Beckerad231bc2007-03-01 08:22:42 +0100565 if (ptr &&
566 (o->type != FIO_OPT_STR_STORE) &&
567 (o->type != FIO_OPT_STR)) {
Jens Axboe92b586f2006-11-07 14:29:17 +0100568 ptr2 = strchr(ptr, ',');
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200569 if (ptr2 && *(ptr2 + 1) == '\0')
570 *ptr2 = '\0';
Jens Axboe0c9baf92007-01-11 15:59:26 +0100571 if (!ptr2)
572 ptr2 = strchr(ptr, ':');
Jens Axboeb486f762007-03-15 09:15:12 +0100573 if (!ptr2)
574 ptr2 = strchr(ptr, '-');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100575 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100576
577 /*
578 * Don't return early if parsing the first option fails - if
579 * we are doing multiple arguments, we can allow the first one
580 * being empty.
581 */
582 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
583
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200584 if (!ptr2) {
585 if (ptr)
586 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100587 return r1;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200588 }
Jens Axboef90eff52006-11-06 11:08:21 +0100589
590 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100591 r2 = __handle_option(o, ptr2, data, 0, 0);
592
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200593 if (ptr)
594 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100595 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100596}
597
Jens Axboe3b8b7132008-06-10 19:46:23 +0200598static struct fio_option *get_option(const char *opt,
Jens Axboe07b32322010-03-05 09:48:44 +0100599 struct fio_option *options, char **post)
Jens Axboe3b8b7132008-06-10 19:46:23 +0200600{
601 struct fio_option *o;
602 char *ret;
603
604 ret = strchr(opt, '=');
605 if (ret) {
606 *post = ret;
607 *ret = '\0';
608 ret = (char *) opt;
609 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100610 strip_blank_end(ret);
Jens Axboe07b32322010-03-05 09:48:44 +0100611 o = find_option(options, ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200612 } else {
Jens Axboe07b32322010-03-05 09:48:44 +0100613 o = find_option(options, opt);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200614 *post = NULL;
615 }
616
617 return o;
618}
619
620static int opt_cmp(const void *p1, const void *p2)
621{
622 struct fio_option *o1, *o2;
623 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100624 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200625
626 s1 = strdup(*((char **) p1));
627 s2 = strdup(*((char **) p2));
628
Jens Axboe07b32322010-03-05 09:48:44 +0100629 o1 = get_option(s1, fio_options, &foo);
630 o2 = get_option(s2, fio_options, &foo);
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200631
Jens Axboe8cdabc12008-11-19 12:31:43 +0100632 prio1 = prio2 = 0;
633 if (o1)
634 prio1 = o1->prio;
635 if (o2)
636 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200637
638 free(s1);
639 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100640 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200641}
642
643void sort_options(char **opts, struct fio_option *options, int num_opts)
644{
645 fio_options = options;
646 qsort(opts, num_opts, sizeof(char *), opt_cmp);
647 fio_options = NULL;
648}
649
Jens Axboeb4692822006-10-27 13:43:22 +0200650int parse_cmd_option(const char *opt, const char *val,
Jens Axboe07b32322010-03-05 09:48:44 +0100651 struct fio_option *options, void *data)
Jens Axboeb4692822006-10-27 13:43:22 +0200652{
653 struct fio_option *o;
654
Jens Axboe07b32322010-03-05 09:48:44 +0100655 o = find_option(options, opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200656 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100657 fprintf(stderr, "Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200658 return 1;
659 }
660
Jens Axboeb1508cf2006-11-02 09:12:40 +0100661 if (!handle_option(o, val, data))
662 return 0;
663
664 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
665 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200666}
667
Aaron Carroll88b5a392008-10-07 11:25:20 +0200668/*
669 * Return a copy of the input string with substrings of the form ${VARNAME}
670 * substituted with the value of the environment variable VARNAME. The
671 * substitution always occurs, even if VARNAME is empty or the corresponding
672 * environment variable undefined.
673 */
674static char *option_dup_subs(const char *opt)
675{
676 char out[OPT_LEN_MAX+1];
677 char in[OPT_LEN_MAX+1];
678 char *outptr = out;
679 char *inptr = in;
680 char *ch1, *ch2, *env;
681 ssize_t nchr = OPT_LEN_MAX;
682 size_t envlen;
683
Jens Axboe38789b52010-03-03 09:23:20 +0100684 if (strlen(in) + 1 > OPT_LEN_MAX) {
685 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
686 return NULL;
687 }
688
Aaron Carroll88b5a392008-10-07 11:25:20 +0200689 in[OPT_LEN_MAX] = '\0';
690 strncpy(in, opt, OPT_LEN_MAX);
691
692 while (*inptr && nchr > 0) {
693 if (inptr[0] == '$' && inptr[1] == '{') {
694 ch2 = strchr(inptr, '}');
695 if (ch2 && inptr+1 < ch2) {
696 ch1 = inptr+2;
697 inptr = ch2+1;
698 *ch2 = '\0';
699
700 env = getenv(ch1);
701 if (env) {
702 envlen = strlen(env);
703 if (envlen <= nchr) {
704 memcpy(outptr, env, envlen);
705 outptr += envlen;
706 nchr -= envlen;
707 }
708 }
709
710 continue;
711 }
712 }
713
714 *outptr++ = *inptr++;
715 --nchr;
716 }
717
718 *outptr = '\0';
719 return strdup(out);
720}
721
Jens Axboe07b32322010-03-05 09:48:44 +0100722int parse_option(const char *opt, struct fio_option *options, void *data)
Jens Axboee1f36502006-10-27 10:54:08 +0200723{
Jens Axboeb4692822006-10-27 13:43:22 +0200724 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200725 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200726
Aaron Carroll88b5a392008-10-07 11:25:20 +0200727 tmp = option_dup_subs(opt);
Jens Axboe38789b52010-03-03 09:23:20 +0100728 if (!tmp)
729 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200730
Jens Axboe07b32322010-03-05 09:48:44 +0100731 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200732 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100733 fprintf(stderr, "Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200734 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200735 return 1;
736 }
737
Jens Axboe0401bca2007-03-29 11:00:43 +0200738 if (!handle_option(o, post, data)) {
739 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100740 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200741 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100742
743 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200744 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100745 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200746}
Jens Axboefd28ca42007-01-09 21:20:13 +0100747
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100748/*
749 * Option match, levenshtein distance. Handy for not quite remembering what
750 * the option name is.
751 */
752static int string_distance(const char *s1, const char *s2)
753{
754 unsigned int s1_len = strlen(s1);
755 unsigned int s2_len = strlen(s2);
756 unsigned int *p, *q, *r;
757 unsigned int i, j;
758
759 p = malloc(sizeof(unsigned int) * (s2_len + 1));
760 q = malloc(sizeof(unsigned int) * (s2_len + 1));
761
762 p[0] = 0;
763 for (i = 1; i <= s2_len; i++)
764 p[i] = p[i - 1] + 1;
765
766 for (i = 1; i <= s1_len; i++) {
767 q[0] = p[0] + 1;
768 for (j = 1; j <= s2_len; j++) {
769 unsigned int sub = p[j - 1];
770
771 if (s1[i - 1] != s2[j - 1])
772 sub++;
773
774 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
775 }
776 r = p;
777 p = q;
778 q = r;
779 }
780
781 i = p[s2_len];
782 free(p);
783 free(q);
784 return i;
785}
786
Jens Axboeafdf9352007-07-31 16:14:34 +0200787static struct fio_option *find_child(struct fio_option *options,
788 struct fio_option *o)
789{
790 struct fio_option *__o;
791
Jens Axboefdf28742007-07-31 23:06:09 +0200792 for (__o = options + 1; __o->name; __o++)
793 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200794 return __o;
795
796 return NULL;
797}
798
Jens Axboe323d9112008-03-01 15:12:14 +0100799static void __print_option(struct fio_option *o, struct fio_option *org,
800 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200801{
802 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100803 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200804
805 if (!o)
806 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200807 if (!org)
808 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100809
Jens Axboeafdf9352007-07-31 16:14:34 +0200810 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100811 depth = level;
812 while (depth--)
813 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200814
815 sprintf(p, "%s", o->name);
816
817 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100818}
819
820static void print_option(struct fio_option *o)
821{
822 struct fio_option *parent;
823 struct fio_option *__o;
824 unsigned int printed;
825 unsigned int level;
826
827 __print_option(o, NULL, 0);
828 parent = o;
829 level = 0;
830 do {
831 level++;
832 printed = 0;
833
834 while ((__o = find_child(o, parent)) != NULL) {
835 __print_option(__o, o, level);
836 o = __o;
837 printed++;
838 }
839
840 parent = o;
841 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200842}
843
Jens Axboe07b32322010-03-05 09:48:44 +0100844int show_cmd_help(struct fio_option *options, const char *name)
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100845{
846 struct fio_option *o, *closest;
847 unsigned int best_dist;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100848 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100849 int show_all = 0;
850
851 if (!name || !strcmp(name, "all"))
852 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100853
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100854 closest = NULL;
855 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100856 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100857 int match = 0;
858
Jens Axboe15ca1502008-04-07 09:26:02 +0200859 if (o->type == FIO_OPT_DEPRECATED)
860 continue;
Jens Axboe07b32322010-03-05 09:48:44 +0100861 if (!exec_profile && o->prof_name)
862 continue;
Jens Axboe15ca1502008-04-07 09:26:02 +0200863
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100864 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100865 if (!strcmp(name, o->name) ||
866 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100867 match = 1;
868 else {
869 unsigned int dist;
870
871 dist = string_distance(name, o->name);
872 if (dist < best_dist) {
873 best_dist = dist;
874 closest = o;
875 }
876 }
877 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100878
879 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100880 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100881 if (match)
Jens Axboe07b32322010-03-05 09:48:44 +0100882 printf("%20s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +0100883 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +0200884 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +0100885 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +0100886 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100887 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100888 }
889
Jens Axboe70df2f12007-01-11 14:04:47 +0100890 if (!match)
891 continue;
892
Jens Axboed447a8c2009-07-17 22:26:15 +0200893 show_option_help(o, stdout);
Jens Axboefd28ca42007-01-09 21:20:13 +0100894 }
895
Jens Axboe29fc6af2007-01-09 21:22:02 +0100896 if (found)
897 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100898
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100899 printf("No such command: %s", name);
900 if (closest) {
901 printf(" - showing closest match\n");
902 printf("%20s: %s\n", closest->name, closest->help);
Jens Axboed447a8c2009-07-17 22:26:15 +0200903 show_option_help(closest, stdout);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100904 } else
905 printf("\n");
906
Jens Axboe29fc6af2007-01-09 21:22:02 +0100907 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100908}
Jens Axboeee738492007-01-10 11:23:16 +0100909
Jens Axboe13335dd2007-01-10 13:14:02 +0100910/*
911 * Handle parsing of default parameters.
912 */
Jens Axboeee738492007-01-10 11:23:16 +0100913void fill_default_options(void *data, struct fio_option *options)
914{
Jens Axboe4945ba12007-01-11 14:36:56 +0100915 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100916
Jens Axboea3d741f2008-02-27 18:32:33 +0100917 dprint(FD_PARSE, "filling default options\n");
918
Jens Axboe4945ba12007-01-11 14:36:56 +0100919 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100920 if (o->def)
921 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100922}
Jens Axboe13335dd2007-01-10 13:14:02 +0100923
Jens Axboe9f988e22010-03-04 10:42:38 +0100924void option_init(struct fio_option *o)
925{
926 if (o->type == FIO_OPT_DEPRECATED)
927 return;
928 if (o->type == FIO_OPT_BOOL) {
929 o->minval = 0;
930 o->maxval = 1;
931 }
932 if (o->type == FIO_OPT_STR_SET && o->def) {
933 fprintf(stderr, "Option %s: string set option with"
934 " default will always be true\n", o->name);
935 }
Jens Axboee2de69d2010-03-04 14:05:48 +0100936 if (!o->cb && (!o->off1 && !o->roff1)) {
Jens Axboe9f988e22010-03-04 10:42:38 +0100937 fprintf(stderr, "Option %s: neither cb nor offset given\n",
938 o->name);
939 }
940 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
941 return;
Jens Axboee2de69d2010-03-04 14:05:48 +0100942 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
943 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
Jens Axboe9f988e22010-03-04 10:42:38 +0100944 fprintf(stderr, "Option %s: both cb and offset given\n",
945 o->name);
946 }
947}
948
Jens Axboe13335dd2007-01-10 13:14:02 +0100949/*
950 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100951 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100952 */
953void options_init(struct fio_option *options)
954{
Jens Axboe4945ba12007-01-11 14:36:56 +0100955 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100956
Jens Axboea3d741f2008-02-27 18:32:33 +0100957 dprint(FD_PARSE, "init options\n");
958
Jens Axboe9f988e22010-03-04 10:42:38 +0100959 for (o = &options[0]; o->name; o++)
960 option_init(o);
Jens Axboe13335dd2007-01-10 13:14:02 +0100961}