blob: ef23fbe90ed6b3ce8b4309bfe8751b56c23ae9aa [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)",
Jens Axboeae3fb6f2010-09-14 13:10:35 +020082 "string (opt=bla)",
Jens Axboed447a8c2009-07-17 22:26:15 +020083 "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 Axboe7bb59102011-07-12 19:47:03 +0200121static unsigned long long __get_mult_bytes(const char *p, void *data,
122 int *percent)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200123{
Jens Axboed6978a32009-07-18 21:04:09 +0200124 unsigned int kb_base = fio_get_kb_base(data);
Jens Axboea639f0b2009-07-18 08:25:35 +0200125 unsigned long long ret = 1;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200126 unsigned int i, pow = 0, mult = kb_base;
127 char *c;
Jens Axboea639f0b2009-07-18 08:25:35 +0200128
Jens Axboe57fc29f2010-06-23 22:24:07 +0200129 if (!p)
130 return 1;
Jens Axboea639f0b2009-07-18 08:25:35 +0200131
Jens Axboe57fc29f2010-06-23 22:24:07 +0200132 c = strdup(p);
133
134 for (i = 0; i < strlen(c); i++)
135 c[i] = tolower(c[i]);
136
137 if (!strcmp("pib", c)) {
138 pow = 5;
139 mult = 1000;
140 } else if (!strcmp("tib", c)) {
141 pow = 4;
142 mult = 1000;
143 } else if (!strcmp("gib", c)) {
144 pow = 3;
145 mult = 1000;
146 } else if (!strcmp("mib", c)) {
147 pow = 2;
148 mult = 1000;
149 } else if (!strcmp("kib", c)) {
150 pow = 1;
151 mult = 1000;
152 } else if (!strcmp("p", c) || !strcmp("pb", c))
153 pow = 5;
154 else if (!strcmp("t", c) || !strcmp("tb", c))
155 pow = 4;
156 else if (!strcmp("g", c) || !strcmp("gb", c))
157 pow = 3;
158 else if (!strcmp("m", c) || !strcmp("mb", c))
159 pow = 2;
160 else if (!strcmp("k", c) || !strcmp("kb", c))
161 pow = 1;
Jens Axboe7bb59102011-07-12 19:47:03 +0200162 else if (!strcmp("%", c)) {
163 *percent = 1;
164 return ret;
165 }
Jens Axboe57fc29f2010-06-23 22:24:07 +0200166
167 while (pow--)
168 ret *= (unsigned long long) mult;
169
170 free(c);
Jens Axboea639f0b2009-07-18 08:25:35 +0200171 return ret;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200172}
173
Jens Axboe7bb59102011-07-12 19:47:03 +0200174static unsigned long long get_mult_bytes(const char *str, int len, void *data,
175 int *percent)
Jens Axboe57fc29f2010-06-23 22:24:07 +0200176{
Jens Axboe55ed9632011-03-27 20:55:09 +0200177 const char *p = str;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200178
Jens Axboe1d1c1872010-07-29 10:50:05 +0200179 if (len < 2)
Jens Axboe7bb59102011-07-12 19:47:03 +0200180 return __get_mult_bytes(str, data, percent);
Jens Axboe1d1c1872010-07-29 10:50:05 +0200181
Jens Axboe55ed9632011-03-27 20:55:09 +0200182 /*
183 * Go forward until we hit a non-digit
184 */
185 while ((p - str) <= len) {
Jens Axboe76cd9372011-07-08 21:14:57 +0200186 if (!isdigit((int) *p))
Jens Axboe55ed9632011-03-27 20:55:09 +0200187 break;
188 p++;
189 }
190
Jens Axboe7bb59102011-07-12 19:47:03 +0200191 if (!isalpha((int) *p) && (*p != '%'))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200192 p = NULL;
193
Jens Axboe7bb59102011-07-12 19:47:03 +0200194 return __get_mult_bytes(p, data, percent);
Jens Axboe57fc29f2010-06-23 22:24:07 +0200195}
196
Jens Axboecb2c86f2006-10-26 13:48:34 +0200197/*
Jens Axboee1f36502006-10-27 10:54:08 +0200198 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200199 */
Jens Axboed6978a32009-07-18 21:04:09 +0200200int str_to_decimal(const char *str, long long *val, int kilo, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200201{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100202 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200203
Jens Axboecb2c86f2006-10-26 13:48:34 +0200204 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100205 if (!len)
206 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200207
Jens Axboeb347f9d2009-03-09 14:15:21 +0100208 if (strstr(str, "0x") || strstr(str, "0X"))
209 base = 16;
210 else
211 base = 10;
212
213 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100214 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200215 return 1;
216
Jens Axboe7bb59102011-07-12 19:47:03 +0200217 if (kilo) {
218 unsigned long long mult;
219 int perc = 0;
220
221 mult = get_mult_bytes(str, len, data, &perc);
222 if (perc)
223 *val = -1ULL - *val;
224 else
225 *val *= mult;
226 } else
Jens Axboecb2c86f2006-10-26 13:48:34 +0200227 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100228
Jens Axboecb2c86f2006-10-26 13:48:34 +0200229 return 0;
230}
231
Jens Axboed6978a32009-07-18 21:04:09 +0200232static int check_str_bytes(const char *p, long long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200233{
Jens Axboed6978a32009-07-18 21:04:09 +0200234 return str_to_decimal(p, val, 1, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200235}
236
Jens Axboe63f29372007-01-10 13:20:09 +0100237static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200238{
Jens Axboed6978a32009-07-18 21:04:09 +0200239 return str_to_decimal(p, val, 0, NULL);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200240}
241
242void strip_blank_front(char **p)
243{
244 char *s = *p;
245
Jens Axboe76cd9372011-07-08 21:14:57 +0200246 while (isspace((int) *s))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200247 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200248
249 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200250}
251
252void strip_blank_end(char *p)
253{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100254 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200255
Jens Axboe523bfad2007-04-04 11:04:06 +0200256 s = strchr(p, ';');
257 if (s)
258 *s = '\0';
259 s = strchr(p, '#');
260 if (s)
261 *s = '\0';
262 if (s)
263 p = s;
264
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200265 s = p + strlen(p);
Jens Axboe76cd9372011-07-08 21:14:57 +0200266 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200267 s--;
268
269 *(s + 1) = '\0';
270}
271
Jens Axboed6978a32009-07-18 21:04:09 +0200272static int check_range_bytes(const char *str, long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200273{
Jens Axboe57fc29f2010-06-23 22:24:07 +0200274 long long __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200275
Jens Axboe57fc29f2010-06-23 22:24:07 +0200276 if (!str_to_decimal(str, &__val, 1, data)) {
277 *val = __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200278 return 0;
279 }
280
Jens Axboecb2c86f2006-10-26 13:48:34 +0200281 return 1;
282}
283
Jens Axboe63f29372007-01-10 13:20:09 +0100284static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200285{
Jens Axboe787f7e92006-11-06 13:26:29 +0100286 if (!strlen(p))
287 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200288 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200289 if (sscanf(p, "%x", val) == 1)
290 return 0;
291 } else {
292 if (sscanf(p, "%u", val) == 1)
293 return 0;
294 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200295
296 return 1;
297}
298
Jens Axboe808def72010-07-14 16:27:02 -0600299static int opt_len(const char *str)
300{
301 char *postfix;
302
303 postfix = strchr(str, ':');
304 if (!postfix)
305 return strlen(str);
306
307 return (int)(postfix - str);
308}
309
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100310#define val_store(ptr, val, off, or, data) \
Jens Axboe17abbe82006-11-06 11:23:10 +0100311 do { \
312 ptr = td_var((data), (off)); \
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100313 if ((or)) \
314 *ptr |= (val); \
315 else \
316 *ptr = (val); \
Jens Axboe17abbe82006-11-06 11:23:10 +0100317 } while (0)
318
Jens Axboef90eff52006-11-06 11:08:21 +0100319static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100320 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200321{
Jens Axboe63f29372007-01-10 13:20:09 +0100322 int il, *ilp;
323 long long ull, *ullp;
324 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200325 char **cp;
Jens Axboee42b01e2011-05-05 12:05:10 -0600326 int ret = 0, is_time = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200327
Jens Axboea3d741f2008-02-27 18:32:33 +0100328 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
329 o->type, ptr);
330
Jens Axboee3cedca2008-11-19 19:57:52 +0100331 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe08e26e32006-11-21 13:15:10 +0100332 fprintf(stderr, "Option %s requires an argument\n", o->name);
333 return 1;
334 }
335
Jens Axboee1f36502006-10-27 10:54:08 +0200336 switch (o->type) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100337 case FIO_OPT_STR:
338 case FIO_OPT_STR_MULTI: {
Jens Axboee1f36502006-10-27 10:54:08 +0200339 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100340 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100341 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeae2ddba2010-03-10 12:49:03 +0100342 int i, all_skipped = 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200343
Jens Axboef0857372007-03-19 13:15:23 +0100344 posval_sort(o, posval);
345
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100346 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100347 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100348 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100349 if (!vp->ival || vp->ival[0] == '\0')
Jens Axboea3073f42010-03-05 10:06:15 +0100350 continue;
Jens Axboeae2ddba2010-03-10 12:49:03 +0100351 all_skipped = 0;
Jens Axboe808def72010-07-14 16:27:02 -0600352 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100353 ret = 0;
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100354 if (o->roff1) {
355 if (vp->or)
356 *(unsigned int *) o->roff1 |= vp->oval;
357 else
358 *(unsigned int *) o->roff1 = vp->oval;
359 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100360 if (!o->off1)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100361 continue;
362 val_store(ilp, vp->oval, o->off1, vp->or, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100363 }
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100364 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100365 }
366 }
367
Jens Axboeae2ddba2010-03-10 12:49:03 +0100368 if (ret && !all_skipped)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100369 show_option_values(o);
370 else if (fn)
371 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200372 break;
373 }
Jens Axboee42b01e2011-05-05 12:05:10 -0600374 case FIO_OPT_STR_VAL_TIME:
375 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100376 case FIO_OPT_INT:
Jens Axboee42b01e2011-05-05 12:05:10 -0600377 case FIO_OPT_STR_VAL: {
378 fio_opt_str_val_fn *fn = o->cb;
Jens Axboee1f36502006-10-27 10:54:08 +0200379
Jens Axboee42b01e2011-05-05 12:05:10 -0600380 if (is_time)
381 ret = check_str_time(ptr, &ull);
382 else
383 ret = check_str_bytes(ptr, &ull, data);
384
Jens Axboee1f36502006-10-27 10:54:08 +0200385 if (ret)
386 break;
387
Jens Axboedb8e0162007-01-10 13:41:26 +0100388 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100389 fprintf(stderr, "max value out of range: %lld"
390 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100391 return 1;
392 }
393 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100394 fprintf(stderr, "min value out of range: %lld"
395 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100396 return 1;
397 }
Jens Axboee1f36502006-10-27 10:54:08 +0200398
399 if (fn)
400 ret = fn(data, &ull);
401 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200402 if (o->type == FIO_OPT_INT) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100403 if (first) {
404 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100405 *(unsigned int *) o->roff1 = ull;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100406 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100407 val_store(ilp, ull, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100408 }
409 if (!more) {
410 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100411 *(unsigned int *) o->roff2 = ull;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100412 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100413 val_store(ilp, ull, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100414 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100415 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100416 if (first) {
417 if (o->roff1)
418 *(unsigned long long *) o->roff1 = ull;
419 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100420 val_store(ullp, ull, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100421 }
422 if (!more) {
423 if (o->roff2)
424 *(unsigned long long *) o->roff2 = ull;
425 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100426 val_store(ullp, ull, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100427 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100428 }
Jens Axboee1f36502006-10-27 10:54:08 +0200429 }
430 break;
431 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100432 case FIO_OPT_STR_STORE: {
433 fio_opt_str_fn *fn = o->cb;
434
Jens Axboe02c6aad2010-03-04 13:49:11 +0100435 if (o->roff1)
436 cp = (char **) o->roff1;
437 else
438 cp = td_var(data, o->off1);
439
Jens Axboee1f36502006-10-27 10:54:08 +0200440 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100441 if (fn) {
442 ret = fn(data, ptr);
443 if (ret) {
444 free(*cp);
445 *cp = NULL;
446 }
447 }
Jens Axboee1f36502006-10-27 10:54:08 +0200448 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100449 }
Jens Axboee1f36502006-10-27 10:54:08 +0200450 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200451 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200452 char *p1, *p2;
453
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100454 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200455
456 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200457 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100458 p1 = strchr(tmp, ':');
459 if (!p1) {
460 ret = 1;
461 break;
462 }
Jens Axboee1f36502006-10-27 10:54:08 +0200463 }
464
465 p2 = p1 + 1;
466 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200467 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200468
469 ret = 1;
Jens Axboed6978a32009-07-18 21:04:09 +0200470 if (!check_range_bytes(p1, &ul1, data) &&
471 !check_range_bytes(p2, &ul2, data)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200472 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200473 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100474 unsigned long foo = ul1;
475
476 ul1 = ul2;
477 ul2 = foo;
478 }
479
480 if (first) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100481 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100482 *(unsigned int *) o->roff1 = ul1;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100483 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100484 val_store(ilp, ul1, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100485 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100486 *(unsigned int *) o->roff2 = ul2;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100487 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100488 val_store(ilp, ul2, o->off2, 0, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200489 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100490 if (o->roff3 && o->roff4) {
Jens Axboe7758d4f2010-03-18 16:50:31 +0100491 *(unsigned int *) o->roff3 = ul1;
492 *(unsigned int *) o->roff4 = ul2;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100493 } else if (o->off3 && o->off4) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100494 val_store(ilp, ul1, o->off3, 0, data);
495 val_store(ilp, ul2, o->off4, 0, data);
Jens Axboe17abbe82006-11-06 11:23:10 +0100496 }
497 }
498
Jens Axboee1f36502006-10-27 10:54:08 +0200499 break;
500 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100501 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200502 fio_opt_int_fn *fn = o->cb;
503
504 ret = check_int(ptr, &il);
505 if (ret)
506 break;
507
Jens Axboedb8e0162007-01-10 13:41:26 +0100508 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100509 fprintf(stderr, "max value out of range: %d (%d max)\n",
510 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100511 return 1;
512 }
513 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100514 fprintf(stderr, "min value out of range: %d (%d min)\n",
515 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100516 return 1;
517 }
Jens Axboee1f36502006-10-27 10:54:08 +0200518
Jens Axboe76a43db2007-01-11 13:24:44 +0100519 if (o->neg)
520 il = !il;
521
Jens Axboee1f36502006-10-27 10:54:08 +0200522 if (fn)
523 ret = fn(data, &il);
524 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100525 if (first) {
526 if (o->roff1)
527 *(unsigned int *)o->roff1 = il;
528 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100529 val_store(ilp, il, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100530 }
531 if (!more) {
532 if (o->roff2)
533 *(unsigned int *) o->roff2 = il;
534 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100535 val_store(ilp, il, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100536 }
Jens Axboee1f36502006-10-27 10:54:08 +0200537 }
538 break;
539 }
540 case FIO_OPT_STR_SET: {
541 fio_opt_str_set_fn *fn = o->cb;
542
543 if (fn)
544 ret = fn(data);
545 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100546 if (first) {
547 if (o->roff1)
548 *(unsigned int *) o->roff1 = 1;
549 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100550 val_store(ilp, 1, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100551 }
552 if (!more) {
553 if (o->roff2)
554 *(unsigned int *) o->roff2 = 1;
555 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100556 val_store(ilp, 1, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100557 }
Jens Axboee1f36502006-10-27 10:54:08 +0200558 }
559 break;
560 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200561 case FIO_OPT_DEPRECATED:
562 fprintf(stdout, "Option %s is deprecated\n", o->name);
563 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200564 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100565 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200566 ret = 1;
567 }
568
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200569 if (ret)
570 return ret;
571
Jens Axboed447a8c2009-07-17 22:26:15 +0200572 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200573 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200574 if (ret) {
575 fprintf(stderr,"Correct format for offending option\n");
576 fprintf(stderr, "%20s: %s\n", o->name, o->help);
577 show_option_help(o, stderr);
578 }
579 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200580
Jens Axboee1f36502006-10-27 10:54:08 +0200581 return ret;
582}
583
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200584static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100585{
Jens Axboeb62bdf22010-03-10 12:36:17 +0100586 char *o_ptr, *ptr, *ptr2;
587 int ret, done;
Jens Axboef90eff52006-11-06 11:08:21 +0100588
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200589 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
590
Jens Axboeb62bdf22010-03-10 12:36:17 +0100591 o_ptr = ptr = NULL;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200592 if (__ptr)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100593 o_ptr = ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100594
Jens Axboef90eff52006-11-06 11:08:21 +0100595 /*
Jens Axboeb62bdf22010-03-10 12:36:17 +0100596 * See if we have another set of parameters, hidden after a comma.
597 * Do this before parsing this round, to check if we should
Jens Axboe787f7e92006-11-06 13:26:29 +0100598 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100599 */
Jens Axboeb62bdf22010-03-10 12:36:17 +0100600 done = 0;
601 ret = 1;
602 do {
603 int __ret;
604
605 ptr2 = NULL;
606 if (ptr &&
607 (o->type != FIO_OPT_STR_STORE) &&
608 (o->type != FIO_OPT_STR)) {
609 ptr2 = strchr(ptr, ',');
610 if (ptr2 && *(ptr2 + 1) == '\0')
611 *ptr2 = '\0';
612 if (o->type != FIO_OPT_STR_MULTI) {
613 if (!ptr2)
614 ptr2 = strchr(ptr, ':');
615 if (!ptr2)
616 ptr2 = strchr(ptr, '-');
617 }
618 }
619
620 /*
621 * Don't return early if parsing the first option fails - if
622 * we are doing multiple arguments, we can allow the first one
623 * being empty.
624 */
625 __ret = __handle_option(o, ptr, data, !done, !!ptr2);
626 if (ret)
627 ret = __ret;
628
Jens Axboe0c9baf92007-01-11 15:59:26 +0100629 if (!ptr2)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100630 break;
Jens Axboe787f7e92006-11-06 13:26:29 +0100631
Jens Axboeb62bdf22010-03-10 12:36:17 +0100632 ptr = ptr2 + 1;
633 done++;
634 } while (1);
Jens Axboe787f7e92006-11-06 13:26:29 +0100635
Jens Axboeb62bdf22010-03-10 12:36:17 +0100636 if (o_ptr)
637 free(o_ptr);
638 return ret;
Jens Axboef90eff52006-11-06 11:08:21 +0100639}
640
Jens Axboe3b8b7132008-06-10 19:46:23 +0200641static struct fio_option *get_option(const char *opt,
Jens Axboe07b32322010-03-05 09:48:44 +0100642 struct fio_option *options, char **post)
Jens Axboe3b8b7132008-06-10 19:46:23 +0200643{
644 struct fio_option *o;
645 char *ret;
646
647 ret = strchr(opt, '=');
648 if (ret) {
649 *post = ret;
650 *ret = '\0';
651 ret = (char *) opt;
652 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100653 strip_blank_end(ret);
Jens Axboe07b32322010-03-05 09:48:44 +0100654 o = find_option(options, ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200655 } else {
Jens Axboe07b32322010-03-05 09:48:44 +0100656 o = find_option(options, opt);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200657 *post = NULL;
658 }
659
660 return o;
661}
662
663static int opt_cmp(const void *p1, const void *p2)
664{
665 struct fio_option *o1, *o2;
666 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100667 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200668
669 s1 = strdup(*((char **) p1));
670 s2 = strdup(*((char **) p2));
671
Jens Axboe07b32322010-03-05 09:48:44 +0100672 o1 = get_option(s1, fio_options, &foo);
673 o2 = get_option(s2, fio_options, &foo);
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200674
Jens Axboe8cdabc12008-11-19 12:31:43 +0100675 prio1 = prio2 = 0;
676 if (o1)
677 prio1 = o1->prio;
678 if (o2)
679 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200680
681 free(s1);
682 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100683 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200684}
685
686void sort_options(char **opts, struct fio_option *options, int num_opts)
687{
688 fio_options = options;
689 qsort(opts, num_opts, sizeof(char *), opt_cmp);
690 fio_options = NULL;
691}
692
Jens Axboeb4692822006-10-27 13:43:22 +0200693int parse_cmd_option(const char *opt, const char *val,
Jens Axboe07b32322010-03-05 09:48:44 +0100694 struct fio_option *options, void *data)
Jens Axboeb4692822006-10-27 13:43:22 +0200695{
696 struct fio_option *o;
697
Jens Axboe07b32322010-03-05 09:48:44 +0100698 o = find_option(options, opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200699 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100700 fprintf(stderr, "Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200701 return 1;
702 }
703
Jens Axboeb1508cf2006-11-02 09:12:40 +0100704 if (!handle_option(o, val, data))
705 return 0;
706
707 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
708 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200709}
710
Aaron Carroll88b5a392008-10-07 11:25:20 +0200711/*
712 * Return a copy of the input string with substrings of the form ${VARNAME}
713 * substituted with the value of the environment variable VARNAME. The
714 * substitution always occurs, even if VARNAME is empty or the corresponding
715 * environment variable undefined.
716 */
717static char *option_dup_subs(const char *opt)
718{
719 char out[OPT_LEN_MAX+1];
720 char in[OPT_LEN_MAX+1];
721 char *outptr = out;
722 char *inptr = in;
723 char *ch1, *ch2, *env;
724 ssize_t nchr = OPT_LEN_MAX;
725 size_t envlen;
726
Jens Axboea0741cb2010-03-05 12:46:37 +0100727 if (strlen(opt) + 1 > OPT_LEN_MAX) {
Jens Axboe38789b52010-03-03 09:23:20 +0100728 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
729 return NULL;
730 }
731
Aaron Carroll88b5a392008-10-07 11:25:20 +0200732 in[OPT_LEN_MAX] = '\0';
733 strncpy(in, opt, OPT_LEN_MAX);
734
735 while (*inptr && nchr > 0) {
736 if (inptr[0] == '$' && inptr[1] == '{') {
737 ch2 = strchr(inptr, '}');
738 if (ch2 && inptr+1 < ch2) {
739 ch1 = inptr+2;
740 inptr = ch2+1;
741 *ch2 = '\0';
742
743 env = getenv(ch1);
744 if (env) {
745 envlen = strlen(env);
746 if (envlen <= nchr) {
747 memcpy(outptr, env, envlen);
748 outptr += envlen;
749 nchr -= envlen;
750 }
751 }
752
753 continue;
754 }
755 }
756
757 *outptr++ = *inptr++;
758 --nchr;
759 }
760
761 *outptr = '\0';
762 return strdup(out);
763}
764
Jens Axboe07b32322010-03-05 09:48:44 +0100765int parse_option(const char *opt, struct fio_option *options, void *data)
Jens Axboee1f36502006-10-27 10:54:08 +0200766{
Jens Axboeb4692822006-10-27 13:43:22 +0200767 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200768 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200769
Aaron Carroll88b5a392008-10-07 11:25:20 +0200770 tmp = option_dup_subs(opt);
Jens Axboe38789b52010-03-03 09:23:20 +0100771 if (!tmp)
772 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200773
Jens Axboe07b32322010-03-05 09:48:44 +0100774 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200775 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100776 fprintf(stderr, "Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200777 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200778 return 1;
779 }
780
Jens Axboe0401bca2007-03-29 11:00:43 +0200781 if (!handle_option(o, post, data)) {
782 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100783 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200784 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100785
786 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200787 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100788 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200789}
Jens Axboefd28ca42007-01-09 21:20:13 +0100790
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100791/*
792 * Option match, levenshtein distance. Handy for not quite remembering what
793 * the option name is.
794 */
795static int string_distance(const char *s1, const char *s2)
796{
797 unsigned int s1_len = strlen(s1);
798 unsigned int s2_len = strlen(s2);
799 unsigned int *p, *q, *r;
800 unsigned int i, j;
801
802 p = malloc(sizeof(unsigned int) * (s2_len + 1));
803 q = malloc(sizeof(unsigned int) * (s2_len + 1));
804
805 p[0] = 0;
806 for (i = 1; i <= s2_len; i++)
807 p[i] = p[i - 1] + 1;
808
809 for (i = 1; i <= s1_len; i++) {
810 q[0] = p[0] + 1;
811 for (j = 1; j <= s2_len; j++) {
812 unsigned int sub = p[j - 1];
813
814 if (s1[i - 1] != s2[j - 1])
815 sub++;
816
817 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
818 }
819 r = p;
820 p = q;
821 q = r;
822 }
823
824 i = p[s2_len];
825 free(p);
826 free(q);
827 return i;
828}
829
Jens Axboeafdf9352007-07-31 16:14:34 +0200830static struct fio_option *find_child(struct fio_option *options,
831 struct fio_option *o)
832{
833 struct fio_option *__o;
834
Jens Axboefdf28742007-07-31 23:06:09 +0200835 for (__o = options + 1; __o->name; __o++)
836 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200837 return __o;
838
839 return NULL;
840}
841
Jens Axboe323d9112008-03-01 15:12:14 +0100842static void __print_option(struct fio_option *o, struct fio_option *org,
843 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200844{
845 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100846 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200847
848 if (!o)
849 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200850 if (!org)
851 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100852
Jens Axboeafdf9352007-07-31 16:14:34 +0200853 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100854 depth = level;
855 while (depth--)
856 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200857
858 sprintf(p, "%s", o->name);
859
860 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100861}
862
863static void print_option(struct fio_option *o)
864{
865 struct fio_option *parent;
866 struct fio_option *__o;
867 unsigned int printed;
868 unsigned int level;
869
870 __print_option(o, NULL, 0);
871 parent = o;
872 level = 0;
873 do {
874 level++;
875 printed = 0;
876
877 while ((__o = find_child(o, parent)) != NULL) {
878 __print_option(__o, o, level);
879 o = __o;
880 printed++;
881 }
882
883 parent = o;
884 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200885}
886
Jens Axboe07b32322010-03-05 09:48:44 +0100887int show_cmd_help(struct fio_option *options, const char *name)
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100888{
889 struct fio_option *o, *closest;
Jens Axboed091d092010-04-01 19:56:23 +0200890 unsigned int best_dist = -1U;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100891 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100892 int show_all = 0;
893
894 if (!name || !strcmp(name, "all"))
895 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100896
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100897 closest = NULL;
898 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100899 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100900 int match = 0;
901
Jens Axboe15ca1502008-04-07 09:26:02 +0200902 if (o->type == FIO_OPT_DEPRECATED)
903 continue;
Jens Axboe07b32322010-03-05 09:48:44 +0100904 if (!exec_profile && o->prof_name)
905 continue;
Jens Axboe15ca1502008-04-07 09:26:02 +0200906
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100907 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100908 if (!strcmp(name, o->name) ||
909 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100910 match = 1;
911 else {
912 unsigned int dist;
913
914 dist = string_distance(name, o->name);
915 if (dist < best_dist) {
916 best_dist = dist;
917 closest = o;
918 }
919 }
920 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100921
922 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100923 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100924 if (match)
Jens Axboe07b32322010-03-05 09:48:44 +0100925 printf("%20s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +0100926 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +0200927 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +0100928 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +0100929 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100930 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100931 }
932
Jens Axboe70df2f12007-01-11 14:04:47 +0100933 if (!match)
934 continue;
935
Jens Axboed447a8c2009-07-17 22:26:15 +0200936 show_option_help(o, stdout);
Jens Axboefd28ca42007-01-09 21:20:13 +0100937 }
938
Jens Axboe29fc6af2007-01-09 21:22:02 +0100939 if (found)
940 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100941
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100942 printf("No such command: %s", name);
Jens Axboed091d092010-04-01 19:56:23 +0200943
944 /*
945 * Only print an appropriately close option, one where the edit
946 * distance isn't too big. Otherwise we get crazy matches.
947 */
948 if (closest && best_dist < 3) {
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100949 printf(" - showing closest match\n");
950 printf("%20s: %s\n", closest->name, closest->help);
Jens Axboed447a8c2009-07-17 22:26:15 +0200951 show_option_help(closest, stdout);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100952 } else
953 printf("\n");
954
Jens Axboe29fc6af2007-01-09 21:22:02 +0100955 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100956}
Jens Axboeee738492007-01-10 11:23:16 +0100957
Jens Axboe13335dd2007-01-10 13:14:02 +0100958/*
959 * Handle parsing of default parameters.
960 */
Jens Axboeee738492007-01-10 11:23:16 +0100961void fill_default_options(void *data, struct fio_option *options)
962{
Jens Axboe4945ba12007-01-11 14:36:56 +0100963 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100964
Jens Axboea3d741f2008-02-27 18:32:33 +0100965 dprint(FD_PARSE, "filling default options\n");
966
Jens Axboe4945ba12007-01-11 14:36:56 +0100967 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100968 if (o->def)
969 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100970}
Jens Axboe13335dd2007-01-10 13:14:02 +0100971
Jens Axboe9f988e22010-03-04 10:42:38 +0100972void option_init(struct fio_option *o)
973{
974 if (o->type == FIO_OPT_DEPRECATED)
975 return;
976 if (o->type == FIO_OPT_BOOL) {
977 o->minval = 0;
978 o->maxval = 1;
979 }
980 if (o->type == FIO_OPT_STR_SET && o->def) {
981 fprintf(stderr, "Option %s: string set option with"
982 " default will always be true\n", o->name);
983 }
Jens Axboee2de69d2010-03-04 14:05:48 +0100984 if (!o->cb && (!o->off1 && !o->roff1)) {
Jens Axboe9f988e22010-03-04 10:42:38 +0100985 fprintf(stderr, "Option %s: neither cb nor offset given\n",
986 o->name);
987 }
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100988 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
989 o->type == FIO_OPT_STR_MULTI)
Jens Axboe9f988e22010-03-04 10:42:38 +0100990 return;
Jens Axboee2de69d2010-03-04 14:05:48 +0100991 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
992 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
Jens Axboe9f988e22010-03-04 10:42:38 +0100993 fprintf(stderr, "Option %s: both cb and offset given\n",
994 o->name);
995 }
996}
997
Jens Axboe13335dd2007-01-10 13:14:02 +0100998/*
999 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +01001000 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +01001001 */
1002void options_init(struct fio_option *options)
1003{
Jens Axboe4945ba12007-01-11 14:36:56 +01001004 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +01001005
Jens Axboea3d741f2008-02-27 18:32:33 +01001006 dprint(FD_PARSE, "init options\n");
1007
Jens Axboe9f988e22010-03-04 10:42:38 +01001008 for (o = &options[0]; o->name; o++)
1009 option_init(o);
Jens Axboe13335dd2007-01-10 13:14:02 +01001010}