blob: d8061610a8a92563c3701b02e8e7ee305f480ee5 [file] [log] [blame]
Jens Axboecb2c86f2006-10-26 13:48:34 +02001/*
2 * This file contains the ini and command liner parser main.
3 */
4#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <ctype.h>
8#include <string.h>
9#include <errno.h>
10#include <limits.h>
Aaron Carroll88b5a392008-10-07 11:25:20 +020011#include <stdlib.h>
Jens Axboecb2c86f2006-10-26 13:48:34 +020012
13#include "parse.h"
Jens Axboea3d741f2008-02-27 18:32:33 +010014#include "debug.h"
Jens Axboe9f988e22010-03-04 10:42:38 +010015#include "options.h"
Jens Axboecb2c86f2006-10-26 13:48:34 +020016
Jens Axboe3b8b7132008-06-10 19:46:23 +020017static struct fio_option *fio_options;
Jens Axboed6978a32009-07-18 21:04:09 +020018extern unsigned int fio_get_kb_base(void *);
Jens Axboe3b8b7132008-06-10 19:46:23 +020019
Jens Axboef0857372007-03-19 13:15:23 +010020static int vp_cmp(const void *p1, const void *p2)
21{
22 const struct value_pair *vp1 = p1;
23 const struct value_pair *vp2 = p2;
24
25 return strlen(vp2->ival) - strlen(vp1->ival);
26}
27
28static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
29{
30 const struct value_pair *vp;
31 int entries;
32
33 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
34
35 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
36 vp = &o->posval[entries];
37 if (!vp->ival || vp->ival[0] == '\0')
38 break;
39
40 memcpy(&vpmap[entries], vp, sizeof(*vp));
41 }
42
43 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
44}
45
Jens Axboed447a8c2009-07-17 22:26:15 +020046static void show_option_range(struct fio_option *o, FILE *out)
Jens Axboeb1ec1da2007-02-23 10:29:16 +010047{
48 if (!o->minval && !o->maxval)
49 return;
50
Jens Axboed447a8c2009-07-17 22:26:15 +020051 fprintf(out, "%20s: min=%d", "range", o->minval);
Jens Axboe39801272009-07-01 09:06:12 +020052 if (o->maxval)
Jens Axboed447a8c2009-07-17 22:26:15 +020053 fprintf(out, ", max=%d", o->maxval);
54 fprintf(out, "\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010055}
56
57static void show_option_values(struct fio_option *o)
58{
Jens Axboea3073f42010-03-05 10:06:15 +010059 int i;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010060
Jens Axboea3073f42010-03-05 10:06:15 +010061 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboe78372132007-03-14 13:24:07 +010062 const struct value_pair *vp = &o->posval[i];
63
64 if (!vp->ival)
Jens Axboea3073f42010-03-05 10:06:15 +010065 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010066
Jens Axboe78372132007-03-14 13:24:07 +010067 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
68 if (vp->help)
69 printf(" %s", vp->help);
70 printf("\n");
Jens Axboea3073f42010-03-05 10:06:15 +010071 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +010072
73 if (i)
74 printf("\n");
75}
76
Jens Axboed447a8c2009-07-17 22:26:15 +020077static void show_option_help(struct fio_option *o, FILE *out)
78{
79 const char *typehelp[] = {
Jens Axboe07b32322010-03-05 09:48:44 +010080 "invalid",
Jens Axboed447a8c2009-07-17 22:26:15 +020081 "string (opt=bla)",
82 "string with possible k/m/g postfix (opt=4k)",
83 "string with time postfix (opt=10s)",
84 "string (opt=bla)",
85 "string with dual range (opt=1k-4k,4k-8k)",
86 "integer value (opt=100)",
87 "boolean value (opt=1)",
88 "no argument (opt)",
Jens Axboe07b32322010-03-05 09:48:44 +010089 "deprecated",
Jens Axboed447a8c2009-07-17 22:26:15 +020090 };
91
92 if (o->alias)
93 fprintf(out, "%20s: %s\n", "alias", o->alias);
94
95 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
96 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
Jens Axboe07b32322010-03-05 09:48:44 +010097 if (o->prof_name)
98 fprintf(out, "%20s: only for profile '%s'\n", "valid", o->prof_name);
Jens Axboed447a8c2009-07-17 22:26:15 +020099 show_option_range(o, stdout);
100 show_option_values(o);
101}
102
Jens Axboecb2c86f2006-10-26 13:48:34 +0200103static unsigned long get_mult_time(char c)
104{
105 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100106 case 'm':
107 case 'M':
108 return 60;
109 case 'h':
110 case 'H':
111 return 60 * 60;
112 case 'd':
113 case 'D':
114 return 24 * 60 * 60;
115 default:
116 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200117 }
118}
119
Jens Axboe57fc29f2010-06-23 22:24:07 +0200120static unsigned long long __get_mult_bytes(const char *p, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200121{
Jens Axboed6978a32009-07-18 21:04:09 +0200122 unsigned int kb_base = fio_get_kb_base(data);
Jens Axboea639f0b2009-07-18 08:25:35 +0200123 unsigned long long ret = 1;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200124 unsigned int i, pow = 0, mult = kb_base;
125 char *c;
Jens Axboea639f0b2009-07-18 08:25:35 +0200126
Jens Axboe57fc29f2010-06-23 22:24:07 +0200127 if (!p)
128 return 1;
Jens Axboea639f0b2009-07-18 08:25:35 +0200129
Jens Axboe57fc29f2010-06-23 22:24:07 +0200130 c = strdup(p);
131
132 for (i = 0; i < strlen(c); i++)
133 c[i] = tolower(c[i]);
134
135 if (!strcmp("pib", c)) {
136 pow = 5;
137 mult = 1000;
138 } else if (!strcmp("tib", c)) {
139 pow = 4;
140 mult = 1000;
141 } else if (!strcmp("gib", c)) {
142 pow = 3;
143 mult = 1000;
144 } else if (!strcmp("mib", c)) {
145 pow = 2;
146 mult = 1000;
147 } else if (!strcmp("kib", c)) {
148 pow = 1;
149 mult = 1000;
150 } else if (!strcmp("p", c) || !strcmp("pb", c))
151 pow = 5;
152 else if (!strcmp("t", c) || !strcmp("tb", c))
153 pow = 4;
154 else if (!strcmp("g", c) || !strcmp("gb", c))
155 pow = 3;
156 else if (!strcmp("m", c) || !strcmp("mb", c))
157 pow = 2;
158 else if (!strcmp("k", c) || !strcmp("kb", c))
159 pow = 1;
160
161 while (pow--)
162 ret *= (unsigned long long) mult;
163
164 free(c);
Jens Axboea639f0b2009-07-18 08:25:35 +0200165 return ret;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200166}
167
Jens Axboe57fc29f2010-06-23 22:24:07 +0200168static unsigned long long get_mult_bytes(const char *str, int len, void *data)
169{
170 const char *p;
171
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 while (isalpha(*(p - 1)))
179 p--;
180 if (!isalpha(*p))
181 p = NULL;
182
183 return __get_mult_bytes(p, data);
184}
185
Jens Axboecb2c86f2006-10-26 13:48:34 +0200186/*
Jens Axboee1f36502006-10-27 10:54:08 +0200187 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200188 */
Jens Axboed6978a32009-07-18 21:04:09 +0200189int str_to_decimal(const char *str, long long *val, int kilo, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200190{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100191 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200192
Jens Axboecb2c86f2006-10-26 13:48:34 +0200193 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100194 if (!len)
195 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200196
Jens Axboeb347f9d2009-03-09 14:15:21 +0100197 if (strstr(str, "0x") || strstr(str, "0X"))
198 base = 16;
199 else
200 base = 10;
201
202 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100203 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200204 return 1;
205
Jens Axboe57fc29f2010-06-23 22:24:07 +0200206 if (kilo)
207 *val *= get_mult_bytes(str, len, data);
208 else
Jens Axboecb2c86f2006-10-26 13:48:34 +0200209 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100210
Jens Axboecb2c86f2006-10-26 13:48:34 +0200211 return 0;
212}
213
Jens Axboed6978a32009-07-18 21:04:09 +0200214static int check_str_bytes(const char *p, long long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200215{
Jens Axboed6978a32009-07-18 21:04:09 +0200216 return str_to_decimal(p, val, 1, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200217}
218
Jens Axboe63f29372007-01-10 13:20:09 +0100219static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200220{
Jens Axboed6978a32009-07-18 21:04:09 +0200221 return str_to_decimal(p, val, 0, NULL);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200222}
223
224void strip_blank_front(char **p)
225{
226 char *s = *p;
227
228 while (isspace(*s))
229 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200230
231 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200232}
233
234void strip_blank_end(char *p)
235{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100236 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200237
Jens Axboe523bfad2007-04-04 11:04:06 +0200238 s = strchr(p, ';');
239 if (s)
240 *s = '\0';
241 s = strchr(p, '#');
242 if (s)
243 *s = '\0';
244 if (s)
245 p = s;
246
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200247 s = p + strlen(p);
Jens Axboe853ee7f2009-03-06 20:29:29 +0100248 while ((isspace(*s) || iscntrl(*s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200249 s--;
250
251 *(s + 1) = '\0';
252}
253
Jens Axboed6978a32009-07-18 21:04:09 +0200254static int check_range_bytes(const char *str, long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200255{
Jens Axboe57fc29f2010-06-23 22:24:07 +0200256 long long __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200257
Jens Axboe57fc29f2010-06-23 22:24:07 +0200258 if (!str_to_decimal(str, &__val, 1, data)) {
259 *val = __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200260 return 0;
261 }
262
Jens Axboecb2c86f2006-10-26 13:48:34 +0200263 return 1;
264}
265
Jens Axboe63f29372007-01-10 13:20:09 +0100266static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200267{
Jens Axboe787f7e92006-11-06 13:26:29 +0100268 if (!strlen(p))
269 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200270 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200271 if (sscanf(p, "%x", val) == 1)
272 return 0;
273 } else {
274 if (sscanf(p, "%u", val) == 1)
275 return 0;
276 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200277
278 return 1;
279}
280
Jens Axboe808def72010-07-14 16:27:02 -0600281static int opt_len(const char *str)
282{
283 char *postfix;
284
285 postfix = strchr(str, ':');
286 if (!postfix)
287 return strlen(str);
288
289 return (int)(postfix - str);
290}
291
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100292#define val_store(ptr, val, off, or, data) \
Jens Axboe17abbe82006-11-06 11:23:10 +0100293 do { \
294 ptr = td_var((data), (off)); \
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100295 if ((or)) \
296 *ptr |= (val); \
297 else \
298 *ptr = (val); \
Jens Axboe17abbe82006-11-06 11:23:10 +0100299 } while (0)
300
Jens Axboef90eff52006-11-06 11:08:21 +0100301static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100302 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200303{
Jens Axboe63f29372007-01-10 13:20:09 +0100304 int il, *ilp;
305 long long ull, *ullp;
306 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200307 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200308 int ret = 0, is_time = 0;
309
Jens Axboea3d741f2008-02-27 18:32:33 +0100310 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
311 o->type, ptr);
312
Jens Axboee3cedca2008-11-19 19:57:52 +0100313 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe08e26e32006-11-21 13:15:10 +0100314 fprintf(stderr, "Option %s requires an argument\n", o->name);
315 return 1;
316 }
317
Jens Axboee1f36502006-10-27 10:54:08 +0200318 switch (o->type) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100319 case FIO_OPT_STR:
320 case FIO_OPT_STR_MULTI: {
Jens Axboee1f36502006-10-27 10:54:08 +0200321 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100322 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100323 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeae2ddba2010-03-10 12:49:03 +0100324 int i, all_skipped = 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200325
Jens Axboef0857372007-03-19 13:15:23 +0100326 posval_sort(o, posval);
327
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100328 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100329 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100330 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100331 if (!vp->ival || vp->ival[0] == '\0')
Jens Axboea3073f42010-03-05 10:06:15 +0100332 continue;
Jens Axboeae2ddba2010-03-10 12:49:03 +0100333 all_skipped = 0;
Jens Axboe808def72010-07-14 16:27:02 -0600334 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100335 ret = 0;
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100336 if (o->roff1) {
337 if (vp->or)
338 *(unsigned int *) o->roff1 |= vp->oval;
339 else
340 *(unsigned int *) o->roff1 = vp->oval;
341 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100342 if (!o->off1)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100343 continue;
344 val_store(ilp, vp->oval, o->off1, vp->or, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100345 }
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100346 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100347 }
348 }
349
Jens Axboeae2ddba2010-03-10 12:49:03 +0100350 if (ret && !all_skipped)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100351 show_option_values(o);
352 else if (fn)
353 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200354 break;
355 }
356 case FIO_OPT_STR_VAL_TIME:
357 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100358 case FIO_OPT_INT:
Jens Axboee01b22b2009-06-09 15:43:25 +0200359 case FIO_OPT_STR_VAL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200360 fio_opt_str_val_fn *fn = o->cb;
361
362 if (is_time)
363 ret = check_str_time(ptr, &ull);
364 else
Jens Axboed6978a32009-07-18 21:04:09 +0200365 ret = check_str_bytes(ptr, &ull, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200366
367 if (ret)
368 break;
369
Jens Axboedb8e0162007-01-10 13:41:26 +0100370 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100371 fprintf(stderr, "max value out of range: %lld"
372 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100373 return 1;
374 }
375 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100376 fprintf(stderr, "min value out of range: %lld"
377 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100378 return 1;
379 }
Jens Axboee1f36502006-10-27 10:54:08 +0200380
381 if (fn)
382 ret = fn(data, &ull);
383 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200384 if (o->type == FIO_OPT_INT) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100385 if (first) {
386 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100387 *(unsigned int *) o->roff1 = ull;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100388 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100389 val_store(ilp, ull, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100390 }
391 if (!more) {
392 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100393 *(unsigned int *) o->roff2 = ull;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100394 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100395 val_store(ilp, ull, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100396 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100397 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100398 if (first) {
399 if (o->roff1)
400 *(unsigned long long *) o->roff1 = ull;
401 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100402 val_store(ullp, ull, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100403 }
404 if (!more) {
405 if (o->roff2)
406 *(unsigned long long *) o->roff2 = ull;
407 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100408 val_store(ullp, ull, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100409 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100410 }
Jens Axboee1f36502006-10-27 10:54:08 +0200411 }
412 break;
413 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100414 case FIO_OPT_STR_STORE: {
415 fio_opt_str_fn *fn = o->cb;
416
Jens Axboe02c6aad2010-03-04 13:49:11 +0100417 if (o->roff1)
418 cp = (char **) o->roff1;
419 else
420 cp = td_var(data, o->off1);
421
Jens Axboee1f36502006-10-27 10:54:08 +0200422 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100423 if (fn) {
424 ret = fn(data, ptr);
425 if (ret) {
426 free(*cp);
427 *cp = NULL;
428 }
429 }
Jens Axboee1f36502006-10-27 10:54:08 +0200430 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100431 }
Jens Axboee1f36502006-10-27 10:54:08 +0200432 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200433 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200434 char *p1, *p2;
435
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100436 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200437
438 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200439 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100440 p1 = strchr(tmp, ':');
441 if (!p1) {
442 ret = 1;
443 break;
444 }
Jens Axboee1f36502006-10-27 10:54:08 +0200445 }
446
447 p2 = p1 + 1;
448 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200449 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200450
451 ret = 1;
Jens Axboed6978a32009-07-18 21:04:09 +0200452 if (!check_range_bytes(p1, &ul1, data) &&
453 !check_range_bytes(p2, &ul2, data)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200454 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200455 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100456 unsigned long foo = ul1;
457
458 ul1 = ul2;
459 ul2 = foo;
460 }
461
462 if (first) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100463 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100464 *(unsigned int *) o->roff1 = ul1;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100465 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100466 val_store(ilp, ul1, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100467 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100468 *(unsigned int *) o->roff2 = ul2;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100469 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100470 val_store(ilp, ul2, o->off2, 0, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200471 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100472 if (o->roff3 && o->roff4) {
Jens Axboe7758d4f2010-03-18 16:50:31 +0100473 *(unsigned int *) o->roff3 = ul1;
474 *(unsigned int *) o->roff4 = ul2;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100475 } else if (o->off3 && o->off4) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100476 val_store(ilp, ul1, o->off3, 0, data);
477 val_store(ilp, ul2, o->off4, 0, data);
Jens Axboe17abbe82006-11-06 11:23:10 +0100478 }
479 }
480
Jens Axboee1f36502006-10-27 10:54:08 +0200481 break;
482 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100483 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200484 fio_opt_int_fn *fn = o->cb;
485
486 ret = check_int(ptr, &il);
487 if (ret)
488 break;
489
Jens Axboedb8e0162007-01-10 13:41:26 +0100490 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100491 fprintf(stderr, "max value out of range: %d (%d max)\n",
492 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100493 return 1;
494 }
495 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100496 fprintf(stderr, "min value out of range: %d (%d min)\n",
497 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100498 return 1;
499 }
Jens Axboee1f36502006-10-27 10:54:08 +0200500
Jens Axboe76a43db2007-01-11 13:24:44 +0100501 if (o->neg)
502 il = !il;
503
Jens Axboee1f36502006-10-27 10:54:08 +0200504 if (fn)
505 ret = fn(data, &il);
506 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100507 if (first) {
508 if (o->roff1)
509 *(unsigned int *)o->roff1 = il;
510 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100511 val_store(ilp, il, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100512 }
513 if (!more) {
514 if (o->roff2)
515 *(unsigned int *) o->roff2 = il;
516 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100517 val_store(ilp, il, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100518 }
Jens Axboee1f36502006-10-27 10:54:08 +0200519 }
520 break;
521 }
522 case FIO_OPT_STR_SET: {
523 fio_opt_str_set_fn *fn = o->cb;
524
525 if (fn)
526 ret = fn(data);
527 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100528 if (first) {
529 if (o->roff1)
530 *(unsigned int *) o->roff1 = 1;
531 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100532 val_store(ilp, 1, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100533 }
534 if (!more) {
535 if (o->roff2)
536 *(unsigned int *) o->roff2 = 1;
537 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100538 val_store(ilp, 1, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100539 }
Jens Axboee1f36502006-10-27 10:54:08 +0200540 }
541 break;
542 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200543 case FIO_OPT_DEPRECATED:
544 fprintf(stdout, "Option %s is deprecated\n", o->name);
545 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200546 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100547 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200548 ret = 1;
549 }
550
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200551 if (ret)
552 return ret;
553
Jens Axboed447a8c2009-07-17 22:26:15 +0200554 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200555 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200556 if (ret) {
557 fprintf(stderr,"Correct format for offending option\n");
558 fprintf(stderr, "%20s: %s\n", o->name, o->help);
559 show_option_help(o, stderr);
560 }
561 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200562
Jens Axboee1f36502006-10-27 10:54:08 +0200563 return ret;
564}
565
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200566static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100567{
Jens Axboeb62bdf22010-03-10 12:36:17 +0100568 char *o_ptr, *ptr, *ptr2;
569 int ret, done;
Jens Axboef90eff52006-11-06 11:08:21 +0100570
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200571 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
572
Jens Axboeb62bdf22010-03-10 12:36:17 +0100573 o_ptr = ptr = NULL;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200574 if (__ptr)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100575 o_ptr = ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100576
Jens Axboef90eff52006-11-06 11:08:21 +0100577 /*
Jens Axboeb62bdf22010-03-10 12:36:17 +0100578 * See if we have another set of parameters, hidden after a comma.
579 * Do this before parsing this round, to check if we should
Jens Axboe787f7e92006-11-06 13:26:29 +0100580 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100581 */
Jens Axboeb62bdf22010-03-10 12:36:17 +0100582 done = 0;
583 ret = 1;
584 do {
585 int __ret;
586
587 ptr2 = NULL;
588 if (ptr &&
589 (o->type != FIO_OPT_STR_STORE) &&
590 (o->type != FIO_OPT_STR)) {
591 ptr2 = strchr(ptr, ',');
592 if (ptr2 && *(ptr2 + 1) == '\0')
593 *ptr2 = '\0';
594 if (o->type != FIO_OPT_STR_MULTI) {
595 if (!ptr2)
596 ptr2 = strchr(ptr, ':');
597 if (!ptr2)
598 ptr2 = strchr(ptr, '-');
599 }
600 }
601
602 /*
603 * Don't return early if parsing the first option fails - if
604 * we are doing multiple arguments, we can allow the first one
605 * being empty.
606 */
607 __ret = __handle_option(o, ptr, data, !done, !!ptr2);
608 if (ret)
609 ret = __ret;
610
Jens Axboe0c9baf92007-01-11 15:59:26 +0100611 if (!ptr2)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100612 break;
Jens Axboe787f7e92006-11-06 13:26:29 +0100613
Jens Axboeb62bdf22010-03-10 12:36:17 +0100614 ptr = ptr2 + 1;
615 done++;
616 } while (1);
Jens Axboe787f7e92006-11-06 13:26:29 +0100617
Jens Axboeb62bdf22010-03-10 12:36:17 +0100618 if (o_ptr)
619 free(o_ptr);
620 return ret;
Jens Axboef90eff52006-11-06 11:08:21 +0100621}
622
Jens Axboe3b8b7132008-06-10 19:46:23 +0200623static struct fio_option *get_option(const char *opt,
Jens Axboe07b32322010-03-05 09:48:44 +0100624 struct fio_option *options, char **post)
Jens Axboe3b8b7132008-06-10 19:46:23 +0200625{
626 struct fio_option *o;
627 char *ret;
628
629 ret = strchr(opt, '=');
630 if (ret) {
631 *post = ret;
632 *ret = '\0';
633 ret = (char *) opt;
634 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100635 strip_blank_end(ret);
Jens Axboe07b32322010-03-05 09:48:44 +0100636 o = find_option(options, ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200637 } else {
Jens Axboe07b32322010-03-05 09:48:44 +0100638 o = find_option(options, opt);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200639 *post = NULL;
640 }
641
642 return o;
643}
644
645static int opt_cmp(const void *p1, const void *p2)
646{
647 struct fio_option *o1, *o2;
648 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100649 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200650
651 s1 = strdup(*((char **) p1));
652 s2 = strdup(*((char **) p2));
653
Jens Axboe07b32322010-03-05 09:48:44 +0100654 o1 = get_option(s1, fio_options, &foo);
655 o2 = get_option(s2, fio_options, &foo);
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200656
Jens Axboe8cdabc12008-11-19 12:31:43 +0100657 prio1 = prio2 = 0;
658 if (o1)
659 prio1 = o1->prio;
660 if (o2)
661 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200662
663 free(s1);
664 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100665 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200666}
667
668void sort_options(char **opts, struct fio_option *options, int num_opts)
669{
670 fio_options = options;
671 qsort(opts, num_opts, sizeof(char *), opt_cmp);
672 fio_options = NULL;
673}
674
Jens Axboeb4692822006-10-27 13:43:22 +0200675int parse_cmd_option(const char *opt, const char *val,
Jens Axboe07b32322010-03-05 09:48:44 +0100676 struct fio_option *options, void *data)
Jens Axboeb4692822006-10-27 13:43:22 +0200677{
678 struct fio_option *o;
679
Jens Axboe07b32322010-03-05 09:48:44 +0100680 o = find_option(options, opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200681 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100682 fprintf(stderr, "Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200683 return 1;
684 }
685
Jens Axboeb1508cf2006-11-02 09:12:40 +0100686 if (!handle_option(o, val, data))
687 return 0;
688
689 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
690 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200691}
692
Aaron Carroll88b5a392008-10-07 11:25:20 +0200693/*
694 * Return a copy of the input string with substrings of the form ${VARNAME}
695 * substituted with the value of the environment variable VARNAME. The
696 * substitution always occurs, even if VARNAME is empty or the corresponding
697 * environment variable undefined.
698 */
699static char *option_dup_subs(const char *opt)
700{
701 char out[OPT_LEN_MAX+1];
702 char in[OPT_LEN_MAX+1];
703 char *outptr = out;
704 char *inptr = in;
705 char *ch1, *ch2, *env;
706 ssize_t nchr = OPT_LEN_MAX;
707 size_t envlen;
708
Jens Axboea0741cb2010-03-05 12:46:37 +0100709 if (strlen(opt) + 1 > OPT_LEN_MAX) {
Jens Axboe38789b52010-03-03 09:23:20 +0100710 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
711 return NULL;
712 }
713
Aaron Carroll88b5a392008-10-07 11:25:20 +0200714 in[OPT_LEN_MAX] = '\0';
715 strncpy(in, opt, OPT_LEN_MAX);
716
717 while (*inptr && nchr > 0) {
718 if (inptr[0] == '$' && inptr[1] == '{') {
719 ch2 = strchr(inptr, '}');
720 if (ch2 && inptr+1 < ch2) {
721 ch1 = inptr+2;
722 inptr = ch2+1;
723 *ch2 = '\0';
724
725 env = getenv(ch1);
726 if (env) {
727 envlen = strlen(env);
728 if (envlen <= nchr) {
729 memcpy(outptr, env, envlen);
730 outptr += envlen;
731 nchr -= envlen;
732 }
733 }
734
735 continue;
736 }
737 }
738
739 *outptr++ = *inptr++;
740 --nchr;
741 }
742
743 *outptr = '\0';
744 return strdup(out);
745}
746
Jens Axboe07b32322010-03-05 09:48:44 +0100747int parse_option(const char *opt, struct fio_option *options, void *data)
Jens Axboee1f36502006-10-27 10:54:08 +0200748{
Jens Axboeb4692822006-10-27 13:43:22 +0200749 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200750 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200751
Aaron Carroll88b5a392008-10-07 11:25:20 +0200752 tmp = option_dup_subs(opt);
Jens Axboe38789b52010-03-03 09:23:20 +0100753 if (!tmp)
754 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200755
Jens Axboe07b32322010-03-05 09:48:44 +0100756 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200757 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100758 fprintf(stderr, "Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200759 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200760 return 1;
761 }
762
Jens Axboe0401bca2007-03-29 11:00:43 +0200763 if (!handle_option(o, post, data)) {
764 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100765 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200766 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100767
768 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200769 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100770 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200771}
Jens Axboefd28ca42007-01-09 21:20:13 +0100772
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100773/*
774 * Option match, levenshtein distance. Handy for not quite remembering what
775 * the option name is.
776 */
777static int string_distance(const char *s1, const char *s2)
778{
779 unsigned int s1_len = strlen(s1);
780 unsigned int s2_len = strlen(s2);
781 unsigned int *p, *q, *r;
782 unsigned int i, j;
783
784 p = malloc(sizeof(unsigned int) * (s2_len + 1));
785 q = malloc(sizeof(unsigned int) * (s2_len + 1));
786
787 p[0] = 0;
788 for (i = 1; i <= s2_len; i++)
789 p[i] = p[i - 1] + 1;
790
791 for (i = 1; i <= s1_len; i++) {
792 q[0] = p[0] + 1;
793 for (j = 1; j <= s2_len; j++) {
794 unsigned int sub = p[j - 1];
795
796 if (s1[i - 1] != s2[j - 1])
797 sub++;
798
799 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
800 }
801 r = p;
802 p = q;
803 q = r;
804 }
805
806 i = p[s2_len];
807 free(p);
808 free(q);
809 return i;
810}
811
Jens Axboeafdf9352007-07-31 16:14:34 +0200812static struct fio_option *find_child(struct fio_option *options,
813 struct fio_option *o)
814{
815 struct fio_option *__o;
816
Jens Axboefdf28742007-07-31 23:06:09 +0200817 for (__o = options + 1; __o->name; __o++)
818 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200819 return __o;
820
821 return NULL;
822}
823
Jens Axboe323d9112008-03-01 15:12:14 +0100824static void __print_option(struct fio_option *o, struct fio_option *org,
825 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200826{
827 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100828 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200829
830 if (!o)
831 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200832 if (!org)
833 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100834
Jens Axboeafdf9352007-07-31 16:14:34 +0200835 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100836 depth = level;
837 while (depth--)
838 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200839
840 sprintf(p, "%s", o->name);
841
842 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100843}
844
845static void print_option(struct fio_option *o)
846{
847 struct fio_option *parent;
848 struct fio_option *__o;
849 unsigned int printed;
850 unsigned int level;
851
852 __print_option(o, NULL, 0);
853 parent = o;
854 level = 0;
855 do {
856 level++;
857 printed = 0;
858
859 while ((__o = find_child(o, parent)) != NULL) {
860 __print_option(__o, o, level);
861 o = __o;
862 printed++;
863 }
864
865 parent = o;
866 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200867}
868
Jens Axboe07b32322010-03-05 09:48:44 +0100869int show_cmd_help(struct fio_option *options, const char *name)
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100870{
871 struct fio_option *o, *closest;
Jens Axboed091d092010-04-01 19:56:23 +0200872 unsigned int best_dist = -1U;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100873 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100874 int show_all = 0;
875
876 if (!name || !strcmp(name, "all"))
877 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100878
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100879 closest = NULL;
880 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100881 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100882 int match = 0;
883
Jens Axboe15ca1502008-04-07 09:26:02 +0200884 if (o->type == FIO_OPT_DEPRECATED)
885 continue;
Jens Axboe07b32322010-03-05 09:48:44 +0100886 if (!exec_profile && o->prof_name)
887 continue;
Jens Axboe15ca1502008-04-07 09:26:02 +0200888
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100889 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100890 if (!strcmp(name, o->name) ||
891 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100892 match = 1;
893 else {
894 unsigned int dist;
895
896 dist = string_distance(name, o->name);
897 if (dist < best_dist) {
898 best_dist = dist;
899 closest = o;
900 }
901 }
902 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100903
904 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100905 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100906 if (match)
Jens Axboe07b32322010-03-05 09:48:44 +0100907 printf("%20s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +0100908 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +0200909 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +0100910 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +0100911 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100912 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100913 }
914
Jens Axboe70df2f12007-01-11 14:04:47 +0100915 if (!match)
916 continue;
917
Jens Axboed447a8c2009-07-17 22:26:15 +0200918 show_option_help(o, stdout);
Jens Axboefd28ca42007-01-09 21:20:13 +0100919 }
920
Jens Axboe29fc6af2007-01-09 21:22:02 +0100921 if (found)
922 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100923
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100924 printf("No such command: %s", name);
Jens Axboed091d092010-04-01 19:56:23 +0200925
926 /*
927 * Only print an appropriately close option, one where the edit
928 * distance isn't too big. Otherwise we get crazy matches.
929 */
930 if (closest && best_dist < 3) {
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100931 printf(" - showing closest match\n");
932 printf("%20s: %s\n", closest->name, closest->help);
Jens Axboed447a8c2009-07-17 22:26:15 +0200933 show_option_help(closest, stdout);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100934 } else
935 printf("\n");
936
Jens Axboe29fc6af2007-01-09 21:22:02 +0100937 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100938}
Jens Axboeee738492007-01-10 11:23:16 +0100939
Jens Axboe13335dd2007-01-10 13:14:02 +0100940/*
941 * Handle parsing of default parameters.
942 */
Jens Axboeee738492007-01-10 11:23:16 +0100943void fill_default_options(void *data, struct fio_option *options)
944{
Jens Axboe4945ba12007-01-11 14:36:56 +0100945 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100946
Jens Axboea3d741f2008-02-27 18:32:33 +0100947 dprint(FD_PARSE, "filling default options\n");
948
Jens Axboe4945ba12007-01-11 14:36:56 +0100949 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100950 if (o->def)
951 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100952}
Jens Axboe13335dd2007-01-10 13:14:02 +0100953
Jens Axboe9f988e22010-03-04 10:42:38 +0100954void option_init(struct fio_option *o)
955{
956 if (o->type == FIO_OPT_DEPRECATED)
957 return;
958 if (o->type == FIO_OPT_BOOL) {
959 o->minval = 0;
960 o->maxval = 1;
961 }
962 if (o->type == FIO_OPT_STR_SET && o->def) {
963 fprintf(stderr, "Option %s: string set option with"
964 " default will always be true\n", o->name);
965 }
Jens Axboee2de69d2010-03-04 14:05:48 +0100966 if (!o->cb && (!o->off1 && !o->roff1)) {
Jens Axboe9f988e22010-03-04 10:42:38 +0100967 fprintf(stderr, "Option %s: neither cb nor offset given\n",
968 o->name);
969 }
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100970 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
971 o->type == FIO_OPT_STR_MULTI)
Jens Axboe9f988e22010-03-04 10:42:38 +0100972 return;
Jens Axboee2de69d2010-03-04 14:05:48 +0100973 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
974 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
Jens Axboe9f988e22010-03-04 10:42:38 +0100975 fprintf(stderr, "Option %s: both cb and offset given\n",
976 o->name);
977 }
978}
979
Jens Axboe13335dd2007-01-10 13:14:02 +0100980/*
981 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100982 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100983 */
984void options_init(struct fio_option *options)
985{
Jens Axboe4945ba12007-01-11 14:36:56 +0100986 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100987
Jens Axboea3d741f2008-02-27 18:32:33 +0100988 dprint(FD_PARSE, "init options\n");
989
Jens Axboe9f988e22010-03-04 10:42:38 +0100990 for (o = &options[0]; o->name; o++)
991 option_init(o);
Jens Axboe13335dd2007-01-10 13:14:02 +0100992}