blob: 97ea4aab47961115a97a16cbbe6b0034e429027d [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 Axboe57fc29f2010-06-23 22:24:07 +0200121static unsigned long long __get_mult_bytes(const char *p, 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;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200125 unsigned int i, pow = 0, mult = kb_base;
126 char *c;
Jens Axboea639f0b2009-07-18 08:25:35 +0200127
Jens Axboe57fc29f2010-06-23 22:24:07 +0200128 if (!p)
129 return 1;
Jens Axboea639f0b2009-07-18 08:25:35 +0200130
Jens Axboe57fc29f2010-06-23 22:24:07 +0200131 c = strdup(p);
132
133 for (i = 0; i < strlen(c); i++)
134 c[i] = tolower(c[i]);
135
136 if (!strcmp("pib", c)) {
137 pow = 5;
138 mult = 1000;
139 } else if (!strcmp("tib", c)) {
140 pow = 4;
141 mult = 1000;
142 } else if (!strcmp("gib", c)) {
143 pow = 3;
144 mult = 1000;
145 } else if (!strcmp("mib", c)) {
146 pow = 2;
147 mult = 1000;
148 } else if (!strcmp("kib", c)) {
149 pow = 1;
150 mult = 1000;
151 } else if (!strcmp("p", c) || !strcmp("pb", c))
152 pow = 5;
153 else if (!strcmp("t", c) || !strcmp("tb", c))
154 pow = 4;
155 else if (!strcmp("g", c) || !strcmp("gb", c))
156 pow = 3;
157 else if (!strcmp("m", c) || !strcmp("mb", c))
158 pow = 2;
159 else if (!strcmp("k", c) || !strcmp("kb", c))
160 pow = 1;
161
162 while (pow--)
163 ret *= (unsigned long long) mult;
164
165 free(c);
Jens Axboea639f0b2009-07-18 08:25:35 +0200166 return ret;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200167}
168
Jens Axboe57fc29f2010-06-23 22:24:07 +0200169static unsigned long long get_mult_bytes(const char *str, int len, void *data)
170{
Jens Axboe55ed9632011-03-27 20:55:09 +0200171 const char *p = str;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200172
Jens Axboe1d1c1872010-07-29 10:50:05 +0200173 if (len < 2)
174 return __get_mult_bytes(str, data);
175
Jens Axboe55ed9632011-03-27 20:55:09 +0200176 /*
177 * Go forward until we hit a non-digit
178 */
179 while ((p - str) <= len) {
Jens Axboe76cd9372011-07-08 21:14:57 +0200180 if (!isdigit((int) *p))
Jens Axboe55ed9632011-03-27 20:55:09 +0200181 break;
182 p++;
183 }
184
Jens Axboe76cd9372011-07-08 21:14:57 +0200185 if (!isalpha((int) *p))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200186 p = NULL;
187
188 return __get_mult_bytes(p, data);
189}
190
Jens Axboecb2c86f2006-10-26 13:48:34 +0200191/*
Jens Axboee1f36502006-10-27 10:54:08 +0200192 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200193 */
Jens Axboed6978a32009-07-18 21:04:09 +0200194int str_to_decimal(const char *str, long long *val, int kilo, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200195{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100196 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200197
Jens Axboecb2c86f2006-10-26 13:48:34 +0200198 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100199 if (!len)
200 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200201
Jens Axboeb347f9d2009-03-09 14:15:21 +0100202 if (strstr(str, "0x") || strstr(str, "0X"))
203 base = 16;
204 else
205 base = 10;
206
207 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100208 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200209 return 1;
210
Jens Axboe57fc29f2010-06-23 22:24:07 +0200211 if (kilo)
212 *val *= get_mult_bytes(str, len, data);
213 else
Jens Axboecb2c86f2006-10-26 13:48:34 +0200214 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100215
Jens Axboecb2c86f2006-10-26 13:48:34 +0200216 return 0;
217}
218
Jens Axboed6978a32009-07-18 21:04:09 +0200219static int check_str_bytes(const char *p, long long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200220{
Jens Axboed6978a32009-07-18 21:04:09 +0200221 return str_to_decimal(p, val, 1, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200222}
223
Jens Axboe63f29372007-01-10 13:20:09 +0100224static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200225{
Jens Axboed6978a32009-07-18 21:04:09 +0200226 return str_to_decimal(p, val, 0, NULL);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200227}
228
229void strip_blank_front(char **p)
230{
231 char *s = *p;
232
Jens Axboe76cd9372011-07-08 21:14:57 +0200233 while (isspace((int) *s))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200234 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200235
236 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200237}
238
239void strip_blank_end(char *p)
240{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100241 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200242
Jens Axboe523bfad2007-04-04 11:04:06 +0200243 s = strchr(p, ';');
244 if (s)
245 *s = '\0';
246 s = strchr(p, '#');
247 if (s)
248 *s = '\0';
249 if (s)
250 p = s;
251
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200252 s = p + strlen(p);
Jens Axboe76cd9372011-07-08 21:14:57 +0200253 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200254 s--;
255
256 *(s + 1) = '\0';
257}
258
Jens Axboed6978a32009-07-18 21:04:09 +0200259static int check_range_bytes(const char *str, long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200260{
Jens Axboe57fc29f2010-06-23 22:24:07 +0200261 long long __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200262
Jens Axboe57fc29f2010-06-23 22:24:07 +0200263 if (!str_to_decimal(str, &__val, 1, data)) {
264 *val = __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200265 return 0;
266 }
267
Jens Axboecb2c86f2006-10-26 13:48:34 +0200268 return 1;
269}
270
Jens Axboe63f29372007-01-10 13:20:09 +0100271static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200272{
Jens Axboe787f7e92006-11-06 13:26:29 +0100273 if (!strlen(p))
274 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200275 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200276 if (sscanf(p, "%x", val) == 1)
277 return 0;
278 } else {
279 if (sscanf(p, "%u", val) == 1)
280 return 0;
281 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200282
283 return 1;
284}
285
Jens Axboe808def72010-07-14 16:27:02 -0600286static int opt_len(const char *str)
287{
288 char *postfix;
289
290 postfix = strchr(str, ':');
291 if (!postfix)
292 return strlen(str);
293
294 return (int)(postfix - str);
295}
296
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100297#define val_store(ptr, val, off, or, data) \
Jens Axboe17abbe82006-11-06 11:23:10 +0100298 do { \
299 ptr = td_var((data), (off)); \
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100300 if ((or)) \
301 *ptr |= (val); \
302 else \
303 *ptr = (val); \
Jens Axboe17abbe82006-11-06 11:23:10 +0100304 } while (0)
305
Jens Axboef90eff52006-11-06 11:08:21 +0100306static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100307 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200308{
Jens Axboe63f29372007-01-10 13:20:09 +0100309 int il, *ilp;
310 long long ull, *ullp;
311 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200312 char **cp;
Jens Axboee42b01e2011-05-05 12:05:10 -0600313 int ret = 0, is_time = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200314
Jens Axboea3d741f2008-02-27 18:32:33 +0100315 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
316 o->type, ptr);
317
Jens Axboee3cedca2008-11-19 19:57:52 +0100318 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe08e26e32006-11-21 13:15:10 +0100319 fprintf(stderr, "Option %s requires an argument\n", o->name);
320 return 1;
321 }
322
Jens Axboee1f36502006-10-27 10:54:08 +0200323 switch (o->type) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100324 case FIO_OPT_STR:
325 case FIO_OPT_STR_MULTI: {
Jens Axboee1f36502006-10-27 10:54:08 +0200326 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100327 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100328 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeae2ddba2010-03-10 12:49:03 +0100329 int i, all_skipped = 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200330
Jens Axboef0857372007-03-19 13:15:23 +0100331 posval_sort(o, posval);
332
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100333 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100334 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100335 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100336 if (!vp->ival || vp->ival[0] == '\0')
Jens Axboea3073f42010-03-05 10:06:15 +0100337 continue;
Jens Axboeae2ddba2010-03-10 12:49:03 +0100338 all_skipped = 0;
Jens Axboe808def72010-07-14 16:27:02 -0600339 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100340 ret = 0;
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100341 if (o->roff1) {
342 if (vp->or)
343 *(unsigned int *) o->roff1 |= vp->oval;
344 else
345 *(unsigned int *) o->roff1 = vp->oval;
346 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100347 if (!o->off1)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100348 continue;
349 val_store(ilp, vp->oval, o->off1, vp->or, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100350 }
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100351 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100352 }
353 }
354
Jens Axboeae2ddba2010-03-10 12:49:03 +0100355 if (ret && !all_skipped)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100356 show_option_values(o);
357 else if (fn)
358 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200359 break;
360 }
Jens Axboee42b01e2011-05-05 12:05:10 -0600361 case FIO_OPT_STR_VAL_TIME:
362 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100363 case FIO_OPT_INT:
Jens Axboee42b01e2011-05-05 12:05:10 -0600364 case FIO_OPT_STR_VAL: {
365 fio_opt_str_val_fn *fn = o->cb;
Jens Axboee1f36502006-10-27 10:54:08 +0200366
Jens Axboee42b01e2011-05-05 12:05:10 -0600367 if (is_time)
368 ret = check_str_time(ptr, &ull);
369 else
370 ret = check_str_bytes(ptr, &ull, data);
371
Jens Axboee1f36502006-10-27 10:54:08 +0200372 if (ret)
373 break;
374
Jens Axboedb8e0162007-01-10 13:41:26 +0100375 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100376 fprintf(stderr, "max value out of range: %lld"
377 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100378 return 1;
379 }
380 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100381 fprintf(stderr, "min value out of range: %lld"
382 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100383 return 1;
384 }
Jens Axboee1f36502006-10-27 10:54:08 +0200385
386 if (fn)
387 ret = fn(data, &ull);
388 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200389 if (o->type == FIO_OPT_INT) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100390 if (first) {
391 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100392 *(unsigned int *) o->roff1 = ull;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100393 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100394 val_store(ilp, ull, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100395 }
396 if (!more) {
397 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100398 *(unsigned int *) o->roff2 = ull;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100399 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100400 val_store(ilp, ull, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100401 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100402 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100403 if (first) {
404 if (o->roff1)
405 *(unsigned long long *) o->roff1 = ull;
406 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100407 val_store(ullp, ull, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100408 }
409 if (!more) {
410 if (o->roff2)
411 *(unsigned long long *) o->roff2 = ull;
412 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100413 val_store(ullp, ull, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100414 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100415 }
Jens Axboee1f36502006-10-27 10:54:08 +0200416 }
417 break;
418 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100419 case FIO_OPT_STR_STORE: {
420 fio_opt_str_fn *fn = o->cb;
421
Jens Axboe02c6aad2010-03-04 13:49:11 +0100422 if (o->roff1)
423 cp = (char **) o->roff1;
424 else
425 cp = td_var(data, o->off1);
426
Jens Axboee1f36502006-10-27 10:54:08 +0200427 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100428 if (fn) {
429 ret = fn(data, ptr);
430 if (ret) {
431 free(*cp);
432 *cp = NULL;
433 }
434 }
Jens Axboee1f36502006-10-27 10:54:08 +0200435 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100436 }
Jens Axboee1f36502006-10-27 10:54:08 +0200437 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200438 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200439 char *p1, *p2;
440
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100441 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200442
443 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200444 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100445 p1 = strchr(tmp, ':');
446 if (!p1) {
447 ret = 1;
448 break;
449 }
Jens Axboee1f36502006-10-27 10:54:08 +0200450 }
451
452 p2 = p1 + 1;
453 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200454 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200455
456 ret = 1;
Jens Axboed6978a32009-07-18 21:04:09 +0200457 if (!check_range_bytes(p1, &ul1, data) &&
458 !check_range_bytes(p2, &ul2, data)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200459 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200460 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100461 unsigned long foo = ul1;
462
463 ul1 = ul2;
464 ul2 = foo;
465 }
466
467 if (first) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100468 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100469 *(unsigned int *) o->roff1 = ul1;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100470 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100471 val_store(ilp, ul1, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100472 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100473 *(unsigned int *) o->roff2 = ul2;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100474 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100475 val_store(ilp, ul2, o->off2, 0, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200476 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100477 if (o->roff3 && o->roff4) {
Jens Axboe7758d4f2010-03-18 16:50:31 +0100478 *(unsigned int *) o->roff3 = ul1;
479 *(unsigned int *) o->roff4 = ul2;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100480 } else if (o->off3 && o->off4) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100481 val_store(ilp, ul1, o->off3, 0, data);
482 val_store(ilp, ul2, o->off4, 0, data);
Jens Axboe17abbe82006-11-06 11:23:10 +0100483 }
484 }
485
Jens Axboee1f36502006-10-27 10:54:08 +0200486 break;
487 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100488 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200489 fio_opt_int_fn *fn = o->cb;
490
491 ret = check_int(ptr, &il);
492 if (ret)
493 break;
494
Jens Axboedb8e0162007-01-10 13:41:26 +0100495 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100496 fprintf(stderr, "max value out of range: %d (%d max)\n",
497 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100498 return 1;
499 }
500 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100501 fprintf(stderr, "min value out of range: %d (%d min)\n",
502 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100503 return 1;
504 }
Jens Axboee1f36502006-10-27 10:54:08 +0200505
Jens Axboe76a43db2007-01-11 13:24:44 +0100506 if (o->neg)
507 il = !il;
508
Jens Axboee1f36502006-10-27 10:54:08 +0200509 if (fn)
510 ret = fn(data, &il);
511 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100512 if (first) {
513 if (o->roff1)
514 *(unsigned int *)o->roff1 = il;
515 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100516 val_store(ilp, il, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100517 }
518 if (!more) {
519 if (o->roff2)
520 *(unsigned int *) o->roff2 = il;
521 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100522 val_store(ilp, il, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100523 }
Jens Axboee1f36502006-10-27 10:54:08 +0200524 }
525 break;
526 }
527 case FIO_OPT_STR_SET: {
528 fio_opt_str_set_fn *fn = o->cb;
529
530 if (fn)
531 ret = fn(data);
532 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100533 if (first) {
534 if (o->roff1)
535 *(unsigned int *) o->roff1 = 1;
536 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100537 val_store(ilp, 1, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100538 }
539 if (!more) {
540 if (o->roff2)
541 *(unsigned int *) o->roff2 = 1;
542 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100543 val_store(ilp, 1, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100544 }
Jens Axboee1f36502006-10-27 10:54:08 +0200545 }
546 break;
547 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200548 case FIO_OPT_DEPRECATED:
549 fprintf(stdout, "Option %s is deprecated\n", o->name);
550 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200551 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100552 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200553 ret = 1;
554 }
555
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200556 if (ret)
557 return ret;
558
Jens Axboed447a8c2009-07-17 22:26:15 +0200559 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200560 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200561 if (ret) {
562 fprintf(stderr,"Correct format for offending option\n");
563 fprintf(stderr, "%20s: %s\n", o->name, o->help);
564 show_option_help(o, stderr);
565 }
566 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200567
Jens Axboee1f36502006-10-27 10:54:08 +0200568 return ret;
569}
570
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200571static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100572{
Jens Axboeb62bdf22010-03-10 12:36:17 +0100573 char *o_ptr, *ptr, *ptr2;
574 int ret, done;
Jens Axboef90eff52006-11-06 11:08:21 +0100575
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200576 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
577
Jens Axboeb62bdf22010-03-10 12:36:17 +0100578 o_ptr = ptr = NULL;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200579 if (__ptr)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100580 o_ptr = ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100581
Jens Axboef90eff52006-11-06 11:08:21 +0100582 /*
Jens Axboeb62bdf22010-03-10 12:36:17 +0100583 * See if we have another set of parameters, hidden after a comma.
584 * Do this before parsing this round, to check if we should
Jens Axboe787f7e92006-11-06 13:26:29 +0100585 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100586 */
Jens Axboeb62bdf22010-03-10 12:36:17 +0100587 done = 0;
588 ret = 1;
589 do {
590 int __ret;
591
592 ptr2 = NULL;
593 if (ptr &&
594 (o->type != FIO_OPT_STR_STORE) &&
595 (o->type != FIO_OPT_STR)) {
596 ptr2 = strchr(ptr, ',');
597 if (ptr2 && *(ptr2 + 1) == '\0')
598 *ptr2 = '\0';
599 if (o->type != FIO_OPT_STR_MULTI) {
600 if (!ptr2)
601 ptr2 = strchr(ptr, ':');
602 if (!ptr2)
603 ptr2 = strchr(ptr, '-');
604 }
605 }
606
607 /*
608 * Don't return early if parsing the first option fails - if
609 * we are doing multiple arguments, we can allow the first one
610 * being empty.
611 */
612 __ret = __handle_option(o, ptr, data, !done, !!ptr2);
613 if (ret)
614 ret = __ret;
615
Jens Axboe0c9baf92007-01-11 15:59:26 +0100616 if (!ptr2)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100617 break;
Jens Axboe787f7e92006-11-06 13:26:29 +0100618
Jens Axboeb62bdf22010-03-10 12:36:17 +0100619 ptr = ptr2 + 1;
620 done++;
621 } while (1);
Jens Axboe787f7e92006-11-06 13:26:29 +0100622
Jens Axboeb62bdf22010-03-10 12:36:17 +0100623 if (o_ptr)
624 free(o_ptr);
625 return ret;
Jens Axboef90eff52006-11-06 11:08:21 +0100626}
627
Jens Axboe3b8b7132008-06-10 19:46:23 +0200628static struct fio_option *get_option(const char *opt,
Jens Axboe07b32322010-03-05 09:48:44 +0100629 struct fio_option *options, char **post)
Jens Axboe3b8b7132008-06-10 19:46:23 +0200630{
631 struct fio_option *o;
632 char *ret;
633
634 ret = strchr(opt, '=');
635 if (ret) {
636 *post = ret;
637 *ret = '\0';
638 ret = (char *) opt;
639 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100640 strip_blank_end(ret);
Jens Axboe07b32322010-03-05 09:48:44 +0100641 o = find_option(options, ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200642 } else {
Jens Axboe07b32322010-03-05 09:48:44 +0100643 o = find_option(options, opt);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200644 *post = NULL;
645 }
646
647 return o;
648}
649
650static int opt_cmp(const void *p1, const void *p2)
651{
652 struct fio_option *o1, *o2;
653 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100654 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200655
656 s1 = strdup(*((char **) p1));
657 s2 = strdup(*((char **) p2));
658
Jens Axboe07b32322010-03-05 09:48:44 +0100659 o1 = get_option(s1, fio_options, &foo);
660 o2 = get_option(s2, fio_options, &foo);
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200661
Jens Axboe8cdabc12008-11-19 12:31:43 +0100662 prio1 = prio2 = 0;
663 if (o1)
664 prio1 = o1->prio;
665 if (o2)
666 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200667
668 free(s1);
669 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100670 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200671}
672
673void sort_options(char **opts, struct fio_option *options, int num_opts)
674{
675 fio_options = options;
676 qsort(opts, num_opts, sizeof(char *), opt_cmp);
677 fio_options = NULL;
678}
679
Jens Axboeb4692822006-10-27 13:43:22 +0200680int parse_cmd_option(const char *opt, const char *val,
Jens Axboe07b32322010-03-05 09:48:44 +0100681 struct fio_option *options, void *data)
Jens Axboeb4692822006-10-27 13:43:22 +0200682{
683 struct fio_option *o;
684
Jens Axboe07b32322010-03-05 09:48:44 +0100685 o = find_option(options, opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200686 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100687 fprintf(stderr, "Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200688 return 1;
689 }
690
Jens Axboeb1508cf2006-11-02 09:12:40 +0100691 if (!handle_option(o, val, data))
692 return 0;
693
694 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
695 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200696}
697
Aaron Carroll88b5a392008-10-07 11:25:20 +0200698/*
699 * Return a copy of the input string with substrings of the form ${VARNAME}
700 * substituted with the value of the environment variable VARNAME. The
701 * substitution always occurs, even if VARNAME is empty or the corresponding
702 * environment variable undefined.
703 */
704static char *option_dup_subs(const char *opt)
705{
706 char out[OPT_LEN_MAX+1];
707 char in[OPT_LEN_MAX+1];
708 char *outptr = out;
709 char *inptr = in;
710 char *ch1, *ch2, *env;
711 ssize_t nchr = OPT_LEN_MAX;
712 size_t envlen;
713
Jens Axboea0741cb2010-03-05 12:46:37 +0100714 if (strlen(opt) + 1 > OPT_LEN_MAX) {
Jens Axboe38789b52010-03-03 09:23:20 +0100715 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
716 return NULL;
717 }
718
Aaron Carroll88b5a392008-10-07 11:25:20 +0200719 in[OPT_LEN_MAX] = '\0';
720 strncpy(in, opt, OPT_LEN_MAX);
721
722 while (*inptr && nchr > 0) {
723 if (inptr[0] == '$' && inptr[1] == '{') {
724 ch2 = strchr(inptr, '}');
725 if (ch2 && inptr+1 < ch2) {
726 ch1 = inptr+2;
727 inptr = ch2+1;
728 *ch2 = '\0';
729
730 env = getenv(ch1);
731 if (env) {
732 envlen = strlen(env);
733 if (envlen <= nchr) {
734 memcpy(outptr, env, envlen);
735 outptr += envlen;
736 nchr -= envlen;
737 }
738 }
739
740 continue;
741 }
742 }
743
744 *outptr++ = *inptr++;
745 --nchr;
746 }
747
748 *outptr = '\0';
749 return strdup(out);
750}
751
Jens Axboe07b32322010-03-05 09:48:44 +0100752int parse_option(const char *opt, struct fio_option *options, void *data)
Jens Axboee1f36502006-10-27 10:54:08 +0200753{
Jens Axboeb4692822006-10-27 13:43:22 +0200754 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200755 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200756
Aaron Carroll88b5a392008-10-07 11:25:20 +0200757 tmp = option_dup_subs(opt);
Jens Axboe38789b52010-03-03 09:23:20 +0100758 if (!tmp)
759 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200760
Jens Axboe07b32322010-03-05 09:48:44 +0100761 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200762 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100763 fprintf(stderr, "Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200764 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200765 return 1;
766 }
767
Jens Axboe0401bca2007-03-29 11:00:43 +0200768 if (!handle_option(o, post, data)) {
769 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100770 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200771 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100772
773 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200774 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100775 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200776}
Jens Axboefd28ca42007-01-09 21:20:13 +0100777
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100778/*
779 * Option match, levenshtein distance. Handy for not quite remembering what
780 * the option name is.
781 */
782static int string_distance(const char *s1, const char *s2)
783{
784 unsigned int s1_len = strlen(s1);
785 unsigned int s2_len = strlen(s2);
786 unsigned int *p, *q, *r;
787 unsigned int i, j;
788
789 p = malloc(sizeof(unsigned int) * (s2_len + 1));
790 q = malloc(sizeof(unsigned int) * (s2_len + 1));
791
792 p[0] = 0;
793 for (i = 1; i <= s2_len; i++)
794 p[i] = p[i - 1] + 1;
795
796 for (i = 1; i <= s1_len; i++) {
797 q[0] = p[0] + 1;
798 for (j = 1; j <= s2_len; j++) {
799 unsigned int sub = p[j - 1];
800
801 if (s1[i - 1] != s2[j - 1])
802 sub++;
803
804 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
805 }
806 r = p;
807 p = q;
808 q = r;
809 }
810
811 i = p[s2_len];
812 free(p);
813 free(q);
814 return i;
815}
816
Jens Axboeafdf9352007-07-31 16:14:34 +0200817static struct fio_option *find_child(struct fio_option *options,
818 struct fio_option *o)
819{
820 struct fio_option *__o;
821
Jens Axboefdf28742007-07-31 23:06:09 +0200822 for (__o = options + 1; __o->name; __o++)
823 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200824 return __o;
825
826 return NULL;
827}
828
Jens Axboe323d9112008-03-01 15:12:14 +0100829static void __print_option(struct fio_option *o, struct fio_option *org,
830 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200831{
832 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100833 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200834
835 if (!o)
836 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200837 if (!org)
838 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100839
Jens Axboeafdf9352007-07-31 16:14:34 +0200840 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100841 depth = level;
842 while (depth--)
843 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200844
845 sprintf(p, "%s", o->name);
846
847 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100848}
849
850static void print_option(struct fio_option *o)
851{
852 struct fio_option *parent;
853 struct fio_option *__o;
854 unsigned int printed;
855 unsigned int level;
856
857 __print_option(o, NULL, 0);
858 parent = o;
859 level = 0;
860 do {
861 level++;
862 printed = 0;
863
864 while ((__o = find_child(o, parent)) != NULL) {
865 __print_option(__o, o, level);
866 o = __o;
867 printed++;
868 }
869
870 parent = o;
871 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200872}
873
Jens Axboe07b32322010-03-05 09:48:44 +0100874int show_cmd_help(struct fio_option *options, const char *name)
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100875{
876 struct fio_option *o, *closest;
Jens Axboed091d092010-04-01 19:56:23 +0200877 unsigned int best_dist = -1U;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100878 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100879 int show_all = 0;
880
881 if (!name || !strcmp(name, "all"))
882 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100883
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100884 closest = NULL;
885 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100886 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100887 int match = 0;
888
Jens Axboe15ca1502008-04-07 09:26:02 +0200889 if (o->type == FIO_OPT_DEPRECATED)
890 continue;
Jens Axboe07b32322010-03-05 09:48:44 +0100891 if (!exec_profile && o->prof_name)
892 continue;
Jens Axboe15ca1502008-04-07 09:26:02 +0200893
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100894 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100895 if (!strcmp(name, o->name) ||
896 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100897 match = 1;
898 else {
899 unsigned int dist;
900
901 dist = string_distance(name, o->name);
902 if (dist < best_dist) {
903 best_dist = dist;
904 closest = o;
905 }
906 }
907 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100908
909 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100910 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100911 if (match)
Jens Axboe07b32322010-03-05 09:48:44 +0100912 printf("%20s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +0100913 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +0200914 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +0100915 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +0100916 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100917 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100918 }
919
Jens Axboe70df2f12007-01-11 14:04:47 +0100920 if (!match)
921 continue;
922
Jens Axboed447a8c2009-07-17 22:26:15 +0200923 show_option_help(o, stdout);
Jens Axboefd28ca42007-01-09 21:20:13 +0100924 }
925
Jens Axboe29fc6af2007-01-09 21:22:02 +0100926 if (found)
927 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100928
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100929 printf("No such command: %s", name);
Jens Axboed091d092010-04-01 19:56:23 +0200930
931 /*
932 * Only print an appropriately close option, one where the edit
933 * distance isn't too big. Otherwise we get crazy matches.
934 */
935 if (closest && best_dist < 3) {
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100936 printf(" - showing closest match\n");
937 printf("%20s: %s\n", closest->name, closest->help);
Jens Axboed447a8c2009-07-17 22:26:15 +0200938 show_option_help(closest, stdout);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100939 } else
940 printf("\n");
941
Jens Axboe29fc6af2007-01-09 21:22:02 +0100942 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100943}
Jens Axboeee738492007-01-10 11:23:16 +0100944
Jens Axboe13335dd2007-01-10 13:14:02 +0100945/*
946 * Handle parsing of default parameters.
947 */
Jens Axboeee738492007-01-10 11:23:16 +0100948void fill_default_options(void *data, struct fio_option *options)
949{
Jens Axboe4945ba12007-01-11 14:36:56 +0100950 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100951
Jens Axboea3d741f2008-02-27 18:32:33 +0100952 dprint(FD_PARSE, "filling default options\n");
953
Jens Axboe4945ba12007-01-11 14:36:56 +0100954 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100955 if (o->def)
956 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100957}
Jens Axboe13335dd2007-01-10 13:14:02 +0100958
Jens Axboe9f988e22010-03-04 10:42:38 +0100959void option_init(struct fio_option *o)
960{
961 if (o->type == FIO_OPT_DEPRECATED)
962 return;
963 if (o->type == FIO_OPT_BOOL) {
964 o->minval = 0;
965 o->maxval = 1;
966 }
967 if (o->type == FIO_OPT_STR_SET && o->def) {
968 fprintf(stderr, "Option %s: string set option with"
969 " default will always be true\n", o->name);
970 }
Jens Axboee2de69d2010-03-04 14:05:48 +0100971 if (!o->cb && (!o->off1 && !o->roff1)) {
Jens Axboe9f988e22010-03-04 10:42:38 +0100972 fprintf(stderr, "Option %s: neither cb nor offset given\n",
973 o->name);
974 }
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100975 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
976 o->type == FIO_OPT_STR_MULTI)
Jens Axboe9f988e22010-03-04 10:42:38 +0100977 return;
Jens Axboee2de69d2010-03-04 14:05:48 +0100978 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
979 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
Jens Axboe9f988e22010-03-04 10:42:38 +0100980 fprintf(stderr, "Option %s: both cb and offset given\n",
981 o->name);
982 }
983}
984
Jens Axboe13335dd2007-01-10 13:14:02 +0100985/*
986 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100987 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100988 */
989void options_init(struct fio_option *options)
990{
Jens Axboe4945ba12007-01-11 14:36:56 +0100991 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100992
Jens Axboea3d741f2008-02-27 18:32:33 +0100993 dprint(FD_PARSE, "init options\n");
994
Jens Axboe9f988e22010-03-04 10:42:38 +0100995 for (o = &options[0]; o->name; o++)
996 option_init(o);
Jens Axboe13335dd2007-01-10 13:14:02 +0100997}