blob: db2f5a42f85aa68b5c7e768f0dee5f2be14cef2a [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
Jens Axboe1d1c1872010-07-29 10:50:05 +0200172 if (len < 2)
173 return __get_mult_bytes(str, data);
174
Jens Axboe57fc29f2010-06-23 22:24:07 +0200175 /*
176 * if the last char is 'b' or 'B', the user likely used
177 * "1gb" instead of just "1g". If the second to last is also
178 * a letter, adjust.
179 */
180 p = str + len - 1;
181 while (isalpha(*(p - 1)))
182 p--;
183 if (!isalpha(*p))
184 p = NULL;
185
186 return __get_mult_bytes(p, data);
187}
188
Jens Axboecb2c86f2006-10-26 13:48:34 +0200189/*
Jens Axboee1f36502006-10-27 10:54:08 +0200190 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200191 */
Jens Axboed6978a32009-07-18 21:04:09 +0200192int str_to_decimal(const char *str, long long *val, int kilo, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200193{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100194 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200195
Jens Axboecb2c86f2006-10-26 13:48:34 +0200196 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100197 if (!len)
198 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200199
Jens Axboeb347f9d2009-03-09 14:15:21 +0100200 if (strstr(str, "0x") || strstr(str, "0X"))
201 base = 16;
202 else
203 base = 10;
204
205 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100206 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200207 return 1;
208
Jens Axboe57fc29f2010-06-23 22:24:07 +0200209 if (kilo)
210 *val *= get_mult_bytes(str, len, data);
211 else
Jens Axboecb2c86f2006-10-26 13:48:34 +0200212 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100213
Jens Axboecb2c86f2006-10-26 13:48:34 +0200214 return 0;
215}
216
Jens Axboed6978a32009-07-18 21:04:09 +0200217static int check_str_bytes(const char *p, long long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200218{
Jens Axboed6978a32009-07-18 21:04:09 +0200219 return str_to_decimal(p, val, 1, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200220}
221
Jens Axboe63f29372007-01-10 13:20:09 +0100222static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200223{
Jens Axboed6978a32009-07-18 21:04:09 +0200224 return str_to_decimal(p, val, 0, NULL);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200225}
226
227void strip_blank_front(char **p)
228{
229 char *s = *p;
230
231 while (isspace(*s))
232 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200233
234 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200235}
236
237void strip_blank_end(char *p)
238{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100239 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200240
Jens Axboe523bfad2007-04-04 11:04:06 +0200241 s = strchr(p, ';');
242 if (s)
243 *s = '\0';
244 s = strchr(p, '#');
245 if (s)
246 *s = '\0';
247 if (s)
248 p = s;
249
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200250 s = p + strlen(p);
Jens Axboe853ee7f2009-03-06 20:29:29 +0100251 while ((isspace(*s) || iscntrl(*s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200252 s--;
253
254 *(s + 1) = '\0';
255}
256
Jens Axboed6978a32009-07-18 21:04:09 +0200257static int check_range_bytes(const char *str, long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200258{
Jens Axboe57fc29f2010-06-23 22:24:07 +0200259 long long __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200260
Jens Axboe57fc29f2010-06-23 22:24:07 +0200261 if (!str_to_decimal(str, &__val, 1, data)) {
262 *val = __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200263 return 0;
264 }
265
Jens Axboecb2c86f2006-10-26 13:48:34 +0200266 return 1;
267}
268
Jens Axboe63f29372007-01-10 13:20:09 +0100269static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200270{
Jens Axboe787f7e92006-11-06 13:26:29 +0100271 if (!strlen(p))
272 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200273 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200274 if (sscanf(p, "%x", val) == 1)
275 return 0;
276 } else {
277 if (sscanf(p, "%u", val) == 1)
278 return 0;
279 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200280
281 return 1;
282}
283
Jens Axboe808def72010-07-14 16:27:02 -0600284static int opt_len(const char *str)
285{
286 char *postfix;
287
288 postfix = strchr(str, ':');
289 if (!postfix)
290 return strlen(str);
291
292 return (int)(postfix - str);
293}
294
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100295#define val_store(ptr, val, off, or, data) \
Jens Axboe17abbe82006-11-06 11:23:10 +0100296 do { \
297 ptr = td_var((data), (off)); \
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100298 if ((or)) \
299 *ptr |= (val); \
300 else \
301 *ptr = (val); \
Jens Axboe17abbe82006-11-06 11:23:10 +0100302 } while (0)
303
Jens Axboef90eff52006-11-06 11:08:21 +0100304static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100305 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200306{
Jens Axboe63f29372007-01-10 13:20:09 +0100307 int il, *ilp;
308 long long ull, *ullp;
309 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200310 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200311 int ret = 0, is_time = 0;
312
Jens Axboea3d741f2008-02-27 18:32:33 +0100313 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
314 o->type, ptr);
315
Jens Axboee3cedca2008-11-19 19:57:52 +0100316 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe08e26e32006-11-21 13:15:10 +0100317 fprintf(stderr, "Option %s requires an argument\n", o->name);
318 return 1;
319 }
320
Jens Axboee1f36502006-10-27 10:54:08 +0200321 switch (o->type) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100322 case FIO_OPT_STR:
323 case FIO_OPT_STR_MULTI: {
Jens Axboee1f36502006-10-27 10:54:08 +0200324 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100325 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100326 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeae2ddba2010-03-10 12:49:03 +0100327 int i, all_skipped = 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200328
Jens Axboef0857372007-03-19 13:15:23 +0100329 posval_sort(o, posval);
330
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100331 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100332 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100333 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100334 if (!vp->ival || vp->ival[0] == '\0')
Jens Axboea3073f42010-03-05 10:06:15 +0100335 continue;
Jens Axboeae2ddba2010-03-10 12:49:03 +0100336 all_skipped = 0;
Jens Axboe808def72010-07-14 16:27:02 -0600337 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100338 ret = 0;
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100339 if (o->roff1) {
340 if (vp->or)
341 *(unsigned int *) o->roff1 |= vp->oval;
342 else
343 *(unsigned int *) o->roff1 = vp->oval;
344 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100345 if (!o->off1)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100346 continue;
347 val_store(ilp, vp->oval, o->off1, vp->or, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100348 }
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100349 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100350 }
351 }
352
Jens Axboeae2ddba2010-03-10 12:49:03 +0100353 if (ret && !all_skipped)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100354 show_option_values(o);
355 else if (fn)
356 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200357 break;
358 }
359 case FIO_OPT_STR_VAL_TIME:
360 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100361 case FIO_OPT_INT:
Jens Axboee01b22b2009-06-09 15:43:25 +0200362 case FIO_OPT_STR_VAL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200363 fio_opt_str_val_fn *fn = o->cb;
364
365 if (is_time)
366 ret = check_str_time(ptr, &ull);
367 else
Jens Axboed6978a32009-07-18 21:04:09 +0200368 ret = check_str_bytes(ptr, &ull, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200369
370 if (ret)
371 break;
372
Jens Axboedb8e0162007-01-10 13:41:26 +0100373 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100374 fprintf(stderr, "max value out of range: %lld"
375 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100376 return 1;
377 }
378 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100379 fprintf(stderr, "min value out of range: %lld"
380 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100381 return 1;
382 }
Jens Axboee1f36502006-10-27 10:54:08 +0200383
384 if (fn)
385 ret = fn(data, &ull);
386 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200387 if (o->type == FIO_OPT_INT) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100388 if (first) {
389 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100390 *(unsigned int *) o->roff1 = ull;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100391 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100392 val_store(ilp, ull, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100393 }
394 if (!more) {
395 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100396 *(unsigned int *) o->roff2 = ull;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100397 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100398 val_store(ilp, ull, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100399 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100400 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100401 if (first) {
402 if (o->roff1)
403 *(unsigned long long *) o->roff1 = ull;
404 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100405 val_store(ullp, ull, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100406 }
407 if (!more) {
408 if (o->roff2)
409 *(unsigned long long *) o->roff2 = ull;
410 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100411 val_store(ullp, ull, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100412 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100413 }
Jens Axboee1f36502006-10-27 10:54:08 +0200414 }
415 break;
416 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100417 case FIO_OPT_STR_STORE: {
418 fio_opt_str_fn *fn = o->cb;
419
Jens Axboe02c6aad2010-03-04 13:49:11 +0100420 if (o->roff1)
421 cp = (char **) o->roff1;
422 else
423 cp = td_var(data, o->off1);
424
Jens Axboee1f36502006-10-27 10:54:08 +0200425 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100426 if (fn) {
427 ret = fn(data, ptr);
428 if (ret) {
429 free(*cp);
430 *cp = NULL;
431 }
432 }
Jens Axboee1f36502006-10-27 10:54:08 +0200433 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100434 }
Jens Axboee1f36502006-10-27 10:54:08 +0200435 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200436 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200437 char *p1, *p2;
438
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100439 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200440
441 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200442 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100443 p1 = strchr(tmp, ':');
444 if (!p1) {
445 ret = 1;
446 break;
447 }
Jens Axboee1f36502006-10-27 10:54:08 +0200448 }
449
450 p2 = p1 + 1;
451 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200452 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200453
454 ret = 1;
Jens Axboed6978a32009-07-18 21:04:09 +0200455 if (!check_range_bytes(p1, &ul1, data) &&
456 !check_range_bytes(p2, &ul2, data)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200457 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200458 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100459 unsigned long foo = ul1;
460
461 ul1 = ul2;
462 ul2 = foo;
463 }
464
465 if (first) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100466 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100467 *(unsigned int *) o->roff1 = ul1;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100468 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100469 val_store(ilp, ul1, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100470 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100471 *(unsigned int *) o->roff2 = ul2;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100472 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100473 val_store(ilp, ul2, o->off2, 0, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200474 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100475 if (o->roff3 && o->roff4) {
Jens Axboe7758d4f2010-03-18 16:50:31 +0100476 *(unsigned int *) o->roff3 = ul1;
477 *(unsigned int *) o->roff4 = ul2;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100478 } else if (o->off3 && o->off4) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100479 val_store(ilp, ul1, o->off3, 0, data);
480 val_store(ilp, ul2, o->off4, 0, data);
Jens Axboe17abbe82006-11-06 11:23:10 +0100481 }
482 }
483
Jens Axboee1f36502006-10-27 10:54:08 +0200484 break;
485 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100486 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200487 fio_opt_int_fn *fn = o->cb;
488
489 ret = check_int(ptr, &il);
490 if (ret)
491 break;
492
Jens Axboedb8e0162007-01-10 13:41:26 +0100493 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100494 fprintf(stderr, "max value out of range: %d (%d max)\n",
495 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100496 return 1;
497 }
498 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100499 fprintf(stderr, "min value out of range: %d (%d min)\n",
500 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100501 return 1;
502 }
Jens Axboee1f36502006-10-27 10:54:08 +0200503
Jens Axboe76a43db2007-01-11 13:24:44 +0100504 if (o->neg)
505 il = !il;
506
Jens Axboee1f36502006-10-27 10:54:08 +0200507 if (fn)
508 ret = fn(data, &il);
509 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100510 if (first) {
511 if (o->roff1)
512 *(unsigned int *)o->roff1 = il;
513 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100514 val_store(ilp, il, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100515 }
516 if (!more) {
517 if (o->roff2)
518 *(unsigned int *) o->roff2 = il;
519 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100520 val_store(ilp, il, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100521 }
Jens Axboee1f36502006-10-27 10:54:08 +0200522 }
523 break;
524 }
525 case FIO_OPT_STR_SET: {
526 fio_opt_str_set_fn *fn = o->cb;
527
528 if (fn)
529 ret = fn(data);
530 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100531 if (first) {
532 if (o->roff1)
533 *(unsigned int *) o->roff1 = 1;
534 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100535 val_store(ilp, 1, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100536 }
537 if (!more) {
538 if (o->roff2)
539 *(unsigned int *) o->roff2 = 1;
540 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100541 val_store(ilp, 1, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100542 }
Jens Axboee1f36502006-10-27 10:54:08 +0200543 }
544 break;
545 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200546 case FIO_OPT_DEPRECATED:
547 fprintf(stdout, "Option %s is deprecated\n", o->name);
548 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200549 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100550 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200551 ret = 1;
552 }
553
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200554 if (ret)
555 return ret;
556
Jens Axboed447a8c2009-07-17 22:26:15 +0200557 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200558 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200559 if (ret) {
560 fprintf(stderr,"Correct format for offending option\n");
561 fprintf(stderr, "%20s: %s\n", o->name, o->help);
562 show_option_help(o, stderr);
563 }
564 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200565
Jens Axboee1f36502006-10-27 10:54:08 +0200566 return ret;
567}
568
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200569static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100570{
Jens Axboeb62bdf22010-03-10 12:36:17 +0100571 char *o_ptr, *ptr, *ptr2;
572 int ret, done;
Jens Axboef90eff52006-11-06 11:08:21 +0100573
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200574 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
575
Jens Axboeb62bdf22010-03-10 12:36:17 +0100576 o_ptr = ptr = NULL;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200577 if (__ptr)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100578 o_ptr = ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100579
Jens Axboef90eff52006-11-06 11:08:21 +0100580 /*
Jens Axboeb62bdf22010-03-10 12:36:17 +0100581 * See if we have another set of parameters, hidden after a comma.
582 * Do this before parsing this round, to check if we should
Jens Axboe787f7e92006-11-06 13:26:29 +0100583 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100584 */
Jens Axboeb62bdf22010-03-10 12:36:17 +0100585 done = 0;
586 ret = 1;
587 do {
588 int __ret;
589
590 ptr2 = NULL;
591 if (ptr &&
592 (o->type != FIO_OPT_STR_STORE) &&
593 (o->type != FIO_OPT_STR)) {
594 ptr2 = strchr(ptr, ',');
595 if (ptr2 && *(ptr2 + 1) == '\0')
596 *ptr2 = '\0';
597 if (o->type != FIO_OPT_STR_MULTI) {
598 if (!ptr2)
599 ptr2 = strchr(ptr, ':');
600 if (!ptr2)
601 ptr2 = strchr(ptr, '-');
602 }
603 }
604
605 /*
606 * Don't return early if parsing the first option fails - if
607 * we are doing multiple arguments, we can allow the first one
608 * being empty.
609 */
610 __ret = __handle_option(o, ptr, data, !done, !!ptr2);
611 if (ret)
612 ret = __ret;
613
Jens Axboe0c9baf92007-01-11 15:59:26 +0100614 if (!ptr2)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100615 break;
Jens Axboe787f7e92006-11-06 13:26:29 +0100616
Jens Axboeb62bdf22010-03-10 12:36:17 +0100617 ptr = ptr2 + 1;
618 done++;
619 } while (1);
Jens Axboe787f7e92006-11-06 13:26:29 +0100620
Jens Axboeb62bdf22010-03-10 12:36:17 +0100621 if (o_ptr)
622 free(o_ptr);
623 return ret;
Jens Axboef90eff52006-11-06 11:08:21 +0100624}
625
Jens Axboe3b8b7132008-06-10 19:46:23 +0200626static struct fio_option *get_option(const char *opt,
Jens Axboe07b32322010-03-05 09:48:44 +0100627 struct fio_option *options, char **post)
Jens Axboe3b8b7132008-06-10 19:46:23 +0200628{
629 struct fio_option *o;
630 char *ret;
631
632 ret = strchr(opt, '=');
633 if (ret) {
634 *post = ret;
635 *ret = '\0';
636 ret = (char *) opt;
637 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100638 strip_blank_end(ret);
Jens Axboe07b32322010-03-05 09:48:44 +0100639 o = find_option(options, ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200640 } else {
Jens Axboe07b32322010-03-05 09:48:44 +0100641 o = find_option(options, opt);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200642 *post = NULL;
643 }
644
645 return o;
646}
647
648static int opt_cmp(const void *p1, const void *p2)
649{
650 struct fio_option *o1, *o2;
651 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100652 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200653
654 s1 = strdup(*((char **) p1));
655 s2 = strdup(*((char **) p2));
656
Jens Axboe07b32322010-03-05 09:48:44 +0100657 o1 = get_option(s1, fio_options, &foo);
658 o2 = get_option(s2, fio_options, &foo);
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200659
Jens Axboe8cdabc12008-11-19 12:31:43 +0100660 prio1 = prio2 = 0;
661 if (o1)
662 prio1 = o1->prio;
663 if (o2)
664 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200665
666 free(s1);
667 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100668 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200669}
670
671void sort_options(char **opts, struct fio_option *options, int num_opts)
672{
673 fio_options = options;
674 qsort(opts, num_opts, sizeof(char *), opt_cmp);
675 fio_options = NULL;
676}
677
Jens Axboeb4692822006-10-27 13:43:22 +0200678int parse_cmd_option(const char *opt, const char *val,
Jens Axboe07b32322010-03-05 09:48:44 +0100679 struct fio_option *options, void *data)
Jens Axboeb4692822006-10-27 13:43:22 +0200680{
681 struct fio_option *o;
682
Jens Axboe07b32322010-03-05 09:48:44 +0100683 o = find_option(options, opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200684 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100685 fprintf(stderr, "Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200686 return 1;
687 }
688
Jens Axboeb1508cf2006-11-02 09:12:40 +0100689 if (!handle_option(o, val, data))
690 return 0;
691
692 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
693 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200694}
695
Aaron Carroll88b5a392008-10-07 11:25:20 +0200696/*
697 * Return a copy of the input string with substrings of the form ${VARNAME}
698 * substituted with the value of the environment variable VARNAME. The
699 * substitution always occurs, even if VARNAME is empty or the corresponding
700 * environment variable undefined.
701 */
702static char *option_dup_subs(const char *opt)
703{
704 char out[OPT_LEN_MAX+1];
705 char in[OPT_LEN_MAX+1];
706 char *outptr = out;
707 char *inptr = in;
708 char *ch1, *ch2, *env;
709 ssize_t nchr = OPT_LEN_MAX;
710 size_t envlen;
711
Jens Axboea0741cb2010-03-05 12:46:37 +0100712 if (strlen(opt) + 1 > OPT_LEN_MAX) {
Jens Axboe38789b52010-03-03 09:23:20 +0100713 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
714 return NULL;
715 }
716
Aaron Carroll88b5a392008-10-07 11:25:20 +0200717 in[OPT_LEN_MAX] = '\0';
718 strncpy(in, opt, OPT_LEN_MAX);
719
720 while (*inptr && nchr > 0) {
721 if (inptr[0] == '$' && inptr[1] == '{') {
722 ch2 = strchr(inptr, '}');
723 if (ch2 && inptr+1 < ch2) {
724 ch1 = inptr+2;
725 inptr = ch2+1;
726 *ch2 = '\0';
727
728 env = getenv(ch1);
729 if (env) {
730 envlen = strlen(env);
731 if (envlen <= nchr) {
732 memcpy(outptr, env, envlen);
733 outptr += envlen;
734 nchr -= envlen;
735 }
736 }
737
738 continue;
739 }
740 }
741
742 *outptr++ = *inptr++;
743 --nchr;
744 }
745
746 *outptr = '\0';
747 return strdup(out);
748}
749
Jens Axboe07b32322010-03-05 09:48:44 +0100750int parse_option(const char *opt, struct fio_option *options, void *data)
Jens Axboee1f36502006-10-27 10:54:08 +0200751{
Jens Axboeb4692822006-10-27 13:43:22 +0200752 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200753 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200754
Aaron Carroll88b5a392008-10-07 11:25:20 +0200755 tmp = option_dup_subs(opt);
Jens Axboe38789b52010-03-03 09:23:20 +0100756 if (!tmp)
757 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200758
Jens Axboe07b32322010-03-05 09:48:44 +0100759 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200760 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100761 fprintf(stderr, "Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200762 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200763 return 1;
764 }
765
Jens Axboe0401bca2007-03-29 11:00:43 +0200766 if (!handle_option(o, post, data)) {
767 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100768 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200769 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100770
771 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200772 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100773 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200774}
Jens Axboefd28ca42007-01-09 21:20:13 +0100775
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100776/*
777 * Option match, levenshtein distance. Handy for not quite remembering what
778 * the option name is.
779 */
780static int string_distance(const char *s1, const char *s2)
781{
782 unsigned int s1_len = strlen(s1);
783 unsigned int s2_len = strlen(s2);
784 unsigned int *p, *q, *r;
785 unsigned int i, j;
786
787 p = malloc(sizeof(unsigned int) * (s2_len + 1));
788 q = malloc(sizeof(unsigned int) * (s2_len + 1));
789
790 p[0] = 0;
791 for (i = 1; i <= s2_len; i++)
792 p[i] = p[i - 1] + 1;
793
794 for (i = 1; i <= s1_len; i++) {
795 q[0] = p[0] + 1;
796 for (j = 1; j <= s2_len; j++) {
797 unsigned int sub = p[j - 1];
798
799 if (s1[i - 1] != s2[j - 1])
800 sub++;
801
802 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
803 }
804 r = p;
805 p = q;
806 q = r;
807 }
808
809 i = p[s2_len];
810 free(p);
811 free(q);
812 return i;
813}
814
Jens Axboeafdf9352007-07-31 16:14:34 +0200815static struct fio_option *find_child(struct fio_option *options,
816 struct fio_option *o)
817{
818 struct fio_option *__o;
819
Jens Axboefdf28742007-07-31 23:06:09 +0200820 for (__o = options + 1; __o->name; __o++)
821 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200822 return __o;
823
824 return NULL;
825}
826
Jens Axboe323d9112008-03-01 15:12:14 +0100827static void __print_option(struct fio_option *o, struct fio_option *org,
828 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200829{
830 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100831 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200832
833 if (!o)
834 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200835 if (!org)
836 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100837
Jens Axboeafdf9352007-07-31 16:14:34 +0200838 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100839 depth = level;
840 while (depth--)
841 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200842
843 sprintf(p, "%s", o->name);
844
845 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100846}
847
848static void print_option(struct fio_option *o)
849{
850 struct fio_option *parent;
851 struct fio_option *__o;
852 unsigned int printed;
853 unsigned int level;
854
855 __print_option(o, NULL, 0);
856 parent = o;
857 level = 0;
858 do {
859 level++;
860 printed = 0;
861
862 while ((__o = find_child(o, parent)) != NULL) {
863 __print_option(__o, o, level);
864 o = __o;
865 printed++;
866 }
867
868 parent = o;
869 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200870}
871
Jens Axboe07b32322010-03-05 09:48:44 +0100872int show_cmd_help(struct fio_option *options, const char *name)
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100873{
874 struct fio_option *o, *closest;
Jens Axboed091d092010-04-01 19:56:23 +0200875 unsigned int best_dist = -1U;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100876 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100877 int show_all = 0;
878
879 if (!name || !strcmp(name, "all"))
880 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100881
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100882 closest = NULL;
883 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100884 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100885 int match = 0;
886
Jens Axboe15ca1502008-04-07 09:26:02 +0200887 if (o->type == FIO_OPT_DEPRECATED)
888 continue;
Jens Axboe07b32322010-03-05 09:48:44 +0100889 if (!exec_profile && o->prof_name)
890 continue;
Jens Axboe15ca1502008-04-07 09:26:02 +0200891
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100892 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100893 if (!strcmp(name, o->name) ||
894 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100895 match = 1;
896 else {
897 unsigned int dist;
898
899 dist = string_distance(name, o->name);
900 if (dist < best_dist) {
901 best_dist = dist;
902 closest = o;
903 }
904 }
905 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100906
907 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100908 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100909 if (match)
Jens Axboe07b32322010-03-05 09:48:44 +0100910 printf("%20s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +0100911 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +0200912 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +0100913 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +0100914 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100915 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100916 }
917
Jens Axboe70df2f12007-01-11 14:04:47 +0100918 if (!match)
919 continue;
920
Jens Axboed447a8c2009-07-17 22:26:15 +0200921 show_option_help(o, stdout);
Jens Axboefd28ca42007-01-09 21:20:13 +0100922 }
923
Jens Axboe29fc6af2007-01-09 21:22:02 +0100924 if (found)
925 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100926
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100927 printf("No such command: %s", name);
Jens Axboed091d092010-04-01 19:56:23 +0200928
929 /*
930 * Only print an appropriately close option, one where the edit
931 * distance isn't too big. Otherwise we get crazy matches.
932 */
933 if (closest && best_dist < 3) {
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100934 printf(" - showing closest match\n");
935 printf("%20s: %s\n", closest->name, closest->help);
Jens Axboed447a8c2009-07-17 22:26:15 +0200936 show_option_help(closest, stdout);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100937 } else
938 printf("\n");
939
Jens Axboe29fc6af2007-01-09 21:22:02 +0100940 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100941}
Jens Axboeee738492007-01-10 11:23:16 +0100942
Jens Axboe13335dd2007-01-10 13:14:02 +0100943/*
944 * Handle parsing of default parameters.
945 */
Jens Axboeee738492007-01-10 11:23:16 +0100946void fill_default_options(void *data, struct fio_option *options)
947{
Jens Axboe4945ba12007-01-11 14:36:56 +0100948 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100949
Jens Axboea3d741f2008-02-27 18:32:33 +0100950 dprint(FD_PARSE, "filling default options\n");
951
Jens Axboe4945ba12007-01-11 14:36:56 +0100952 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100953 if (o->def)
954 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100955}
Jens Axboe13335dd2007-01-10 13:14:02 +0100956
Jens Axboe9f988e22010-03-04 10:42:38 +0100957void option_init(struct fio_option *o)
958{
959 if (o->type == FIO_OPT_DEPRECATED)
960 return;
961 if (o->type == FIO_OPT_BOOL) {
962 o->minval = 0;
963 o->maxval = 1;
964 }
965 if (o->type == FIO_OPT_STR_SET && o->def) {
966 fprintf(stderr, "Option %s: string set option with"
967 " default will always be true\n", o->name);
968 }
Jens Axboee2de69d2010-03-04 14:05:48 +0100969 if (!o->cb && (!o->off1 && !o->roff1)) {
Jens Axboe9f988e22010-03-04 10:42:38 +0100970 fprintf(stderr, "Option %s: neither cb nor offset given\n",
971 o->name);
972 }
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100973 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
974 o->type == FIO_OPT_STR_MULTI)
Jens Axboe9f988e22010-03-04 10:42:38 +0100975 return;
Jens Axboee2de69d2010-03-04 14:05:48 +0100976 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
977 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
Jens Axboe9f988e22010-03-04 10:42:38 +0100978 fprintf(stderr, "Option %s: both cb and offset given\n",
979 o->name);
980 }
981}
982
Jens Axboe13335dd2007-01-10 13:14:02 +0100983/*
984 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100985 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100986 */
987void options_init(struct fio_option *options)
988{
Jens Axboe4945ba12007-01-11 14:36:56 +0100989 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100990
Jens Axboea3d741f2008-02-27 18:32:33 +0100991 dprint(FD_PARSE, "init options\n");
992
Jens Axboe9f988e22010-03-04 10:42:38 +0100993 for (o = &options[0]; o->name; o++)
994 option_init(o);
Jens Axboe13335dd2007-01-10 13:14:02 +0100995}