blob: 27e7336b49af99720886905928a69058e42f3bf1 [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>
Yu-ju Hong83349192011-08-13 00:53:44 +020012#include <math.h>
Jens Axboecb2c86f2006-10-26 13:48:34 +020013
14#include "parse.h"
Jens Axboea3d741f2008-02-27 18:32:33 +010015#include "debug.h"
Jens Axboe9f988e22010-03-04 10:42:38 +010016#include "options.h"
Jens Axboecb2c86f2006-10-26 13:48:34 +020017
Jens Axboe3b8b7132008-06-10 19:46:23 +020018static struct fio_option *fio_options;
Jens Axboed6978a32009-07-18 21:04:09 +020019extern unsigned int fio_get_kb_base(void *);
Jens Axboe3b8b7132008-06-10 19:46:23 +020020
Jens Axboef0857372007-03-19 13:15:23 +010021static int vp_cmp(const void *p1, const void *p2)
22{
23 const struct value_pair *vp1 = p1;
24 const struct value_pair *vp2 = p2;
25
26 return strlen(vp2->ival) - strlen(vp1->ival);
27}
28
Jens Axboece952ab2011-08-31 14:29:22 -060029static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
Jens Axboef0857372007-03-19 13:15:23 +010030{
31 const struct value_pair *vp;
32 int entries;
33
34 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
35
36 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
37 vp = &o->posval[entries];
38 if (!vp->ival || vp->ival[0] == '\0')
39 break;
40
41 memcpy(&vpmap[entries], vp, sizeof(*vp));
42 }
43
44 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
45}
46
Jens Axboe06b0be62011-10-04 10:31:10 +020047static void show_option_range(struct fio_option *o,
48 int (*logger)(const char *format, ...))
Jens Axboeb1ec1da2007-02-23 10:29:16 +010049{
Yu-ju Hong83349192011-08-13 00:53:44 +020050 if (o->type == FIO_OPT_FLOAT_LIST){
51 if (isnan(o->minfp) && isnan(o->maxfp))
52 return;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010053
Jens Axboe06b0be62011-10-04 10:31:10 +020054 logger("%20s: min=%f", "range", o->minfp);
Yu-ju Hong83349192011-08-13 00:53:44 +020055 if (!isnan(o->maxfp))
Jens Axboe06b0be62011-10-04 10:31:10 +020056 logger(", max=%f", o->maxfp);
57 logger("\n");
Yu-ju Hong83349192011-08-13 00:53:44 +020058 } else {
59 if (!o->minval && !o->maxval)
60 return;
61
Jens Axboe06b0be62011-10-04 10:31:10 +020062 logger("%20s: min=%d", "range", o->minval);
Yu-ju Hong83349192011-08-13 00:53:44 +020063 if (o->maxval)
Jens Axboe06b0be62011-10-04 10:31:10 +020064 logger(", max=%d", o->maxval);
65 logger("\n");
Yu-ju Hong83349192011-08-13 00:53:44 +020066 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +010067}
68
69static void show_option_values(struct fio_option *o)
70{
Jens Axboea3073f42010-03-05 10:06:15 +010071 int i;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010072
Jens Axboea3073f42010-03-05 10:06:15 +010073 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboe78372132007-03-14 13:24:07 +010074 const struct value_pair *vp = &o->posval[i];
75
76 if (!vp->ival)
Jens Axboea3073f42010-03-05 10:06:15 +010077 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010078
Jens Axboe06b0be62011-10-04 10:31:10 +020079 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
Jens Axboe78372132007-03-14 13:24:07 +010080 if (vp->help)
Jens Axboe06b0be62011-10-04 10:31:10 +020081 log_info(" %s", vp->help);
82 log_info("\n");
Jens Axboea3073f42010-03-05 10:06:15 +010083 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +010084
85 if (i)
Jens Axboe06b0be62011-10-04 10:31:10 +020086 log_info("\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010087}
88
Jens Axboe06b0be62011-10-04 10:31:10 +020089static void show_option_help(struct fio_option *o, int is_err)
Jens Axboed447a8c2009-07-17 22:26:15 +020090{
91 const char *typehelp[] = {
Jens Axboe07b32322010-03-05 09:48:44 +010092 "invalid",
Jens Axboed447a8c2009-07-17 22:26:15 +020093 "string (opt=bla)",
Jens Axboeae3fb6f2010-09-14 13:10:35 +020094 "string (opt=bla)",
Jens Axboed447a8c2009-07-17 22:26:15 +020095 "string with possible k/m/g postfix (opt=4k)",
96 "string with time postfix (opt=10s)",
97 "string (opt=bla)",
98 "string with dual range (opt=1k-4k,4k-8k)",
99 "integer value (opt=100)",
100 "boolean value (opt=1)",
Yu-ju Hong83349192011-08-13 00:53:44 +0200101 "list of floating point values separated by ':' (opt=5.9:7.8)",
Jens Axboed447a8c2009-07-17 22:26:15 +0200102 "no argument (opt)",
Jens Axboe07b32322010-03-05 09:48:44 +0100103 "deprecated",
Jens Axboed447a8c2009-07-17 22:26:15 +0200104 };
Jens Axboe06b0be62011-10-04 10:31:10 +0200105 int (*logger)(const char *format, ...);
106
107 if (is_err)
108 logger = log_err;
109 else
110 logger = log_info;
Jens Axboed447a8c2009-07-17 22:26:15 +0200111
112 if (o->alias)
Jens Axboe06b0be62011-10-04 10:31:10 +0200113 logger("%20s: %s\n", "alias", o->alias);
Jens Axboed447a8c2009-07-17 22:26:15 +0200114
Jens Axboe06b0be62011-10-04 10:31:10 +0200115 logger("%20s: %s\n", "type", typehelp[o->type]);
116 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
Jens Axboe07b32322010-03-05 09:48:44 +0100117 if (o->prof_name)
Jens Axboe06b0be62011-10-04 10:31:10 +0200118 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
119 show_option_range(o, logger);
Jens Axboed447a8c2009-07-17 22:26:15 +0200120 show_option_values(o);
121}
122
Jens Axboecb2c86f2006-10-26 13:48:34 +0200123static unsigned long get_mult_time(char c)
124{
125 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100126 case 'm':
127 case 'M':
128 return 60;
129 case 'h':
130 case 'H':
131 return 60 * 60;
132 case 'd':
133 case 'D':
134 return 24 * 60 * 60;
135 default:
136 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200137 }
138}
139
Jens Axboe7bb59102011-07-12 19:47:03 +0200140static unsigned long long __get_mult_bytes(const char *p, void *data,
141 int *percent)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200142{
Jens Axboed6978a32009-07-18 21:04:09 +0200143 unsigned int kb_base = fio_get_kb_base(data);
Jens Axboea639f0b2009-07-18 08:25:35 +0200144 unsigned long long ret = 1;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200145 unsigned int i, pow = 0, mult = kb_base;
146 char *c;
Jens Axboea639f0b2009-07-18 08:25:35 +0200147
Jens Axboe57fc29f2010-06-23 22:24:07 +0200148 if (!p)
149 return 1;
Jens Axboea639f0b2009-07-18 08:25:35 +0200150
Jens Axboe57fc29f2010-06-23 22:24:07 +0200151 c = strdup(p);
152
153 for (i = 0; i < strlen(c); i++)
154 c[i] = tolower(c[i]);
155
156 if (!strcmp("pib", c)) {
157 pow = 5;
158 mult = 1000;
159 } else if (!strcmp("tib", c)) {
160 pow = 4;
161 mult = 1000;
162 } else if (!strcmp("gib", c)) {
163 pow = 3;
164 mult = 1000;
165 } else if (!strcmp("mib", c)) {
166 pow = 2;
167 mult = 1000;
168 } else if (!strcmp("kib", c)) {
169 pow = 1;
170 mult = 1000;
171 } else if (!strcmp("p", c) || !strcmp("pb", c))
172 pow = 5;
173 else if (!strcmp("t", c) || !strcmp("tb", c))
174 pow = 4;
175 else if (!strcmp("g", c) || !strcmp("gb", c))
176 pow = 3;
177 else if (!strcmp("m", c) || !strcmp("mb", c))
178 pow = 2;
179 else if (!strcmp("k", c) || !strcmp("kb", c))
180 pow = 1;
Jens Axboe7bb59102011-07-12 19:47:03 +0200181 else if (!strcmp("%", c)) {
182 *percent = 1;
183 return ret;
184 }
Jens Axboe57fc29f2010-06-23 22:24:07 +0200185
186 while (pow--)
187 ret *= (unsigned long long) mult;
188
189 free(c);
Jens Axboea639f0b2009-07-18 08:25:35 +0200190 return ret;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200191}
192
Jens Axboe7bb59102011-07-12 19:47:03 +0200193static unsigned long long get_mult_bytes(const char *str, int len, void *data,
Jens Axboe6925dd32011-09-07 22:10:11 +0200194 int *percent)
Jens Axboe57fc29f2010-06-23 22:24:07 +0200195{
Jens Axboe55ed9632011-03-27 20:55:09 +0200196 const char *p = str;
Jens Axboeba4ddd62011-09-07 22:11:00 +0200197 int digit_seen = 0;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200198
Jens Axboe1d1c1872010-07-29 10:50:05 +0200199 if (len < 2)
Jens Axboe7bb59102011-07-12 19:47:03 +0200200 return __get_mult_bytes(str, data, percent);
Jens Axboe1d1c1872010-07-29 10:50:05 +0200201
Jens Axboe55ed9632011-03-27 20:55:09 +0200202 /*
Jens Axboee2979752011-08-31 12:55:27 -0600203 * Go forward until we hit a non-digit, or +/- sign
Jens Axboe55ed9632011-03-27 20:55:09 +0200204 */
205 while ((p - str) <= len) {
Jens Axboeba4ddd62011-09-07 22:11:00 +0200206 if (!isdigit((int) *p) &&
207 (((*p != '+') && (*p != '-')) || digit_seen))
Jens Axboe55ed9632011-03-27 20:55:09 +0200208 break;
Jens Axboe3c703d12011-09-21 09:27:24 +0200209 digit_seen |= isdigit((int) *p);
Jens Axboe55ed9632011-03-27 20:55:09 +0200210 p++;
211 }
212
Jens Axboe7bb59102011-07-12 19:47:03 +0200213 if (!isalpha((int) *p) && (*p != '%'))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200214 p = NULL;
215
Jens Axboe7bb59102011-07-12 19:47:03 +0200216 return __get_mult_bytes(p, data, percent);
Jens Axboe57fc29f2010-06-23 22:24:07 +0200217}
218
Jens Axboecb2c86f2006-10-26 13:48:34 +0200219/*
Yu-ju Hong83349192011-08-13 00:53:44 +0200220 * Convert string into a floating number. Return 1 for success and 0 otherwise.
221 */
222int str_to_float(const char *str, double *val)
223{
224 return (1 == sscanf(str, "%lf", val));
225}
226
227/*
Jens Axboee1f36502006-10-27 10:54:08 +0200228 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200229 */
Jens Axboe6925dd32011-09-07 22:10:11 +0200230int str_to_decimal(const char *str, long long *val, int kilo, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200231{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100232 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200233
Jens Axboecb2c86f2006-10-26 13:48:34 +0200234 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100235 if (!len)
236 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200237
Jens Axboeb347f9d2009-03-09 14:15:21 +0100238 if (strstr(str, "0x") || strstr(str, "0X"))
239 base = 16;
240 else
241 base = 10;
242
243 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100244 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200245 return 1;
246
Jens Axboe7bb59102011-07-12 19:47:03 +0200247 if (kilo) {
248 unsigned long long mult;
249 int perc = 0;
250
Jens Axboe6925dd32011-09-07 22:10:11 +0200251 mult = get_mult_bytes(str, len, data, &perc);
Jens Axboe7bb59102011-07-12 19:47:03 +0200252 if (perc)
253 *val = -1ULL - *val;
254 else
255 *val *= mult;
256 } else
Jens Axboecb2c86f2006-10-26 13:48:34 +0200257 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100258
Jens Axboecb2c86f2006-10-26 13:48:34 +0200259 return 0;
260}
261
Jens Axboe6925dd32011-09-07 22:10:11 +0200262static int check_str_bytes(const char *p, long long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200263{
Jens Axboe6925dd32011-09-07 22:10:11 +0200264 return str_to_decimal(p, val, 1, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200265}
266
Jens Axboe63f29372007-01-10 13:20:09 +0100267static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200268{
Jens Axboe6925dd32011-09-07 22:10:11 +0200269 return str_to_decimal(p, val, 0, NULL);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200270}
271
272void strip_blank_front(char **p)
273{
274 char *s = *p;
275
Jens Axboe76cd9372011-07-08 21:14:57 +0200276 while (isspace((int) *s))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200277 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200278
279 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200280}
281
282void strip_blank_end(char *p)
283{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100284 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200285
Jens Axboe523bfad2007-04-04 11:04:06 +0200286 s = strchr(p, ';');
287 if (s)
288 *s = '\0';
289 s = strchr(p, '#');
290 if (s)
291 *s = '\0';
292 if (s)
293 p = s;
294
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200295 s = p + strlen(p);
Jens Axboe76cd9372011-07-08 21:14:57 +0200296 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200297 s--;
298
299 *(s + 1) = '\0';
300}
301
Jens Axboed6978a32009-07-18 21:04:09 +0200302static int check_range_bytes(const char *str, long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200303{
Jens Axboe57fc29f2010-06-23 22:24:07 +0200304 long long __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200305
Jens Axboe6925dd32011-09-07 22:10:11 +0200306 if (!str_to_decimal(str, &__val, 1, data)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200307 *val = __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200308 return 0;
309 }
310
Jens Axboecb2c86f2006-10-26 13:48:34 +0200311 return 1;
312}
313
Jens Axboe63f29372007-01-10 13:20:09 +0100314static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200315{
Jens Axboe787f7e92006-11-06 13:26:29 +0100316 if (!strlen(p))
317 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200318 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200319 if (sscanf(p, "%x", val) == 1)
320 return 0;
321 } else {
322 if (sscanf(p, "%u", val) == 1)
323 return 0;
324 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200325
326 return 1;
327}
328
Jens Axboe808def72010-07-14 16:27:02 -0600329static int opt_len(const char *str)
330{
331 char *postfix;
332
333 postfix = strchr(str, ':');
334 if (!postfix)
335 return strlen(str);
336
337 return (int)(postfix - str);
338}
339
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100340#define val_store(ptr, val, off, or, data) \
Jens Axboe17abbe82006-11-06 11:23:10 +0100341 do { \
342 ptr = td_var((data), (off)); \
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100343 if ((or)) \
344 *ptr |= (val); \
345 else \
346 *ptr = (val); \
Jens Axboe17abbe82006-11-06 11:23:10 +0100347 } while (0)
348
Jens Axboef90eff52006-11-06 11:08:21 +0100349static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Yu-ju Hong83349192011-08-13 00:53:44 +0200350 int first, int more, int curr)
Jens Axboee1f36502006-10-27 10:54:08 +0200351{
Jens Axboe63f29372007-01-10 13:20:09 +0100352 int il, *ilp;
Yu-ju Hong83349192011-08-13 00:53:44 +0200353 double* flp;
Jens Axboe63f29372007-01-10 13:20:09 +0100354 long long ull, *ullp;
355 long ul1, ul2;
Yu-ju Hong83349192011-08-13 00:53:44 +0200356 double uf;
Jens Axboeb4692822006-10-27 13:43:22 +0200357 char **cp;
Jens Axboee42b01e2011-05-05 12:05:10 -0600358 int ret = 0, is_time = 0;
Jens Axboec44b1ff2011-08-30 20:43:53 -0600359 const struct value_pair *vp;
360 struct value_pair posval[PARSE_MAX_VP];
361 int i, all_skipped = 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200362
Jens Axboea3d741f2008-02-27 18:32:33 +0100363 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
364 o->type, ptr);
365
Jens Axboee3cedca2008-11-19 19:57:52 +0100366 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200367 log_err("Option %s requires an argument\n", o->name);
Jens Axboe08e26e32006-11-21 13:15:10 +0100368 return 1;
369 }
370
Jens Axboee1f36502006-10-27 10:54:08 +0200371 switch (o->type) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100372 case FIO_OPT_STR:
373 case FIO_OPT_STR_MULTI: {
Jens Axboee1f36502006-10-27 10:54:08 +0200374 fio_opt_str_fn *fn = o->cb;
375
Jens Axboef0857372007-03-19 13:15:23 +0100376 posval_sort(o, posval);
377
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100378 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100379 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100380 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100381 if (!vp->ival || vp->ival[0] == '\0')
Jens Axboea3073f42010-03-05 10:06:15 +0100382 continue;
Jens Axboeae2ddba2010-03-10 12:49:03 +0100383 all_skipped = 0;
Jens Axboe808def72010-07-14 16:27:02 -0600384 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100385 ret = 0;
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100386 if (o->roff1) {
387 if (vp->or)
388 *(unsigned int *) o->roff1 |= vp->oval;
389 else
390 *(unsigned int *) o->roff1 = vp->oval;
391 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100392 if (!o->off1)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100393 continue;
394 val_store(ilp, vp->oval, o->off1, vp->or, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100395 }
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100396 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100397 }
398 }
399
Jens Axboeae2ddba2010-03-10 12:49:03 +0100400 if (ret && !all_skipped)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100401 show_option_values(o);
402 else if (fn)
403 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200404 break;
405 }
Jens Axboee42b01e2011-05-05 12:05:10 -0600406 case FIO_OPT_STR_VAL_TIME:
407 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100408 case FIO_OPT_INT:
Jens Axboee42b01e2011-05-05 12:05:10 -0600409 case FIO_OPT_STR_VAL: {
410 fio_opt_str_val_fn *fn = o->cb;
Jens Axboee1f36502006-10-27 10:54:08 +0200411
Jens Axboee42b01e2011-05-05 12:05:10 -0600412 if (is_time)
413 ret = check_str_time(ptr, &ull);
414 else
Jens Axboe6925dd32011-09-07 22:10:11 +0200415 ret = check_str_bytes(ptr, &ull, data);
Jens Axboee42b01e2011-05-05 12:05:10 -0600416
Jens Axboee1f36502006-10-27 10:54:08 +0200417 if (ret)
418 break;
419
Jens Axboedb8e0162007-01-10 13:41:26 +0100420 if (o->maxval && ull > o->maxval) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200421 log_err("max value out of range: %lld"
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100422 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100423 return 1;
424 }
425 if (o->minval && ull < o->minval) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200426 log_err("min value out of range: %lld"
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100427 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100428 return 1;
429 }
Jens Axboee1f36502006-10-27 10:54:08 +0200430
431 if (fn)
432 ret = fn(data, &ull);
433 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200434 if (o->type == FIO_OPT_INT) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100435 if (first) {
436 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100437 *(unsigned int *) o->roff1 = ull;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100438 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100439 val_store(ilp, ull, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100440 }
441 if (!more) {
442 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100443 *(unsigned int *) o->roff2 = ull;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100444 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100445 val_store(ilp, ull, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100446 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100447 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100448 if (first) {
449 if (o->roff1)
450 *(unsigned long long *) o->roff1 = ull;
451 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100452 val_store(ullp, ull, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100453 }
454 if (!more) {
455 if (o->roff2)
456 *(unsigned long long *) o->roff2 = ull;
457 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100458 val_store(ullp, ull, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100459 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100460 }
Jens Axboee1f36502006-10-27 10:54:08 +0200461 }
462 break;
463 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200464 case FIO_OPT_FLOAT_LIST: {
465
466 if (first) {
467 ul2 = 1;
468 ilp = td_var(data, o->off2);
469 *ilp = ul2;
470 }
471 if (curr >= o->maxlen) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200472 log_err("the list exceeding max length %d\n",
Yu-ju Hong83349192011-08-13 00:53:44 +0200473 o->maxlen);
474 return 1;
475 }
476 if(!str_to_float(ptr, &uf)){
Jens Axboe28d7c362011-10-05 20:30:24 +0200477 log_err("not a floating point value: %s\n", ptr);
Yu-ju Hong83349192011-08-13 00:53:44 +0200478 return 1;
479 }
480 if (!isnan(o->maxfp) && uf > o->maxfp) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200481 log_err("value out of range: %f"
Yu-ju Hong83349192011-08-13 00:53:44 +0200482 " (range max: %f)\n", uf, o->maxfp);
483 return 1;
484 }
485 if (!isnan(o->minfp) && uf < o->minfp) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200486 log_err("value out of range: %f"
Yu-ju Hong83349192011-08-13 00:53:44 +0200487 " (range min: %f)\n", uf, o->minfp);
488 return 1;
489 }
490
491 flp = td_var(data, o->off1);
492 flp[curr] = uf;
493
494 break;
495 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100496 case FIO_OPT_STR_STORE: {
497 fio_opt_str_fn *fn = o->cb;
498
Jens Axboece952ab2011-08-31 14:29:22 -0600499 posval_sort(o, posval);
500
Jens Axboe1c964ce2011-08-31 16:45:03 -0600501 if (!o->posval[0].ival) {
502 vp = NULL;
Jens Axboee2979752011-08-31 12:55:27 -0600503 goto match;
Jens Axboe1c964ce2011-08-31 16:45:03 -0600504 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100505
Jens Axboec44b1ff2011-08-30 20:43:53 -0600506 ret = 1;
507 for (i = 0; i < PARSE_MAX_VP; i++) {
508 vp = &posval[i];
509 if (!vp->ival || vp->ival[0] == '\0')
510 continue;
511 all_skipped = 0;
512 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
513 char *rest;
514
515 ret = 0;
516 if (vp->cb)
517 fn = vp->cb;
Jens Axboee2979752011-08-31 12:55:27 -0600518match:
Jens Axboec44b1ff2011-08-30 20:43:53 -0600519 if (o->roff1)
520 cp = (char **) o->roff1;
521 else
522 cp = td_var(data, o->off1);
523 *cp = strdup(ptr);
524 rest = strstr(*cp, ":");
525 if (rest) {
526 *rest = '\0';
527 ptr = rest + 1;
Jens Axboe1c964ce2011-08-31 16:45:03 -0600528 } else if (vp && vp->cb)
Jens Axboec44b1ff2011-08-30 20:43:53 -0600529 ptr = NULL;
530 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100531 }
532 }
Jens Axboec44b1ff2011-08-30 20:43:53 -0600533
534 if (ret && !all_skipped)
535 show_option_values(o);
536 else if (fn && ptr)
537 ret = fn(data, ptr);
538
Jens Axboee1f36502006-10-27 10:54:08 +0200539 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100540 }
Jens Axboee1f36502006-10-27 10:54:08 +0200541 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200542 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200543 char *p1, *p2;
544
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100545 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200546
Dave Engberg31d23f42011-07-23 21:07:13 +0200547 /* Handle bsrange with separate read,write values: */
548 p1 = strchr(tmp, ',');
549 if (p1)
550 *p1 = '\0';
551
Jens Axboeb765a372006-10-27 15:00:16 +0200552 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200553 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100554 p1 = strchr(tmp, ':');
555 if (!p1) {
556 ret = 1;
557 break;
558 }
Jens Axboee1f36502006-10-27 10:54:08 +0200559 }
560
561 p2 = p1 + 1;
562 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200563 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200564
565 ret = 1;
Jens Axboed6978a32009-07-18 21:04:09 +0200566 if (!check_range_bytes(p1, &ul1, data) &&
567 !check_range_bytes(p2, &ul2, data)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200568 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200569 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100570 unsigned long foo = ul1;
571
572 ul1 = ul2;
573 ul2 = foo;
574 }
575
576 if (first) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100577 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100578 *(unsigned int *) o->roff1 = ul1;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100579 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100580 val_store(ilp, ul1, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100581 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100582 *(unsigned int *) o->roff2 = ul2;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100583 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100584 val_store(ilp, ul2, o->off2, 0, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200585 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100586 if (o->roff3 && o->roff4) {
Jens Axboe7758d4f2010-03-18 16:50:31 +0100587 *(unsigned int *) o->roff3 = ul1;
588 *(unsigned int *) o->roff4 = ul2;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100589 } else if (o->off3 && o->off4) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100590 val_store(ilp, ul1, o->off3, 0, data);
591 val_store(ilp, ul2, o->off4, 0, data);
Jens Axboe17abbe82006-11-06 11:23:10 +0100592 }
593 }
594
Jens Axboee1f36502006-10-27 10:54:08 +0200595 break;
596 }
Jens Axboe74ba1802011-07-15 09:09:15 +0200597 case FIO_OPT_BOOL:
598 case FIO_OPT_STR_SET: {
Jens Axboee1f36502006-10-27 10:54:08 +0200599 fio_opt_int_fn *fn = o->cb;
600
Jens Axboe74ba1802011-07-15 09:09:15 +0200601 if (ptr)
602 ret = check_int(ptr, &il);
603 else if (o->type == FIO_OPT_BOOL)
604 ret = 1;
605 else
606 il = 1;
607
Jens Axboee1f36502006-10-27 10:54:08 +0200608 if (ret)
609 break;
610
Jens Axboedb8e0162007-01-10 13:41:26 +0100611 if (o->maxval && il > (int) o->maxval) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200612 log_err("max value out of range: %d (%d max)\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100613 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100614 return 1;
615 }
616 if (o->minval && il < o->minval) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200617 log_err("min value out of range: %d (%d min)\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100618 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100619 return 1;
620 }
Jens Axboee1f36502006-10-27 10:54:08 +0200621
Jens Axboe76a43db2007-01-11 13:24:44 +0100622 if (o->neg)
623 il = !il;
624
Jens Axboee1f36502006-10-27 10:54:08 +0200625 if (fn)
626 ret = fn(data, &il);
627 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100628 if (first) {
629 if (o->roff1)
630 *(unsigned int *)o->roff1 = il;
631 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100632 val_store(ilp, il, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100633 }
634 if (!more) {
635 if (o->roff2)
636 *(unsigned int *) o->roff2 = il;
637 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100638 val_store(ilp, il, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100639 }
Jens Axboee1f36502006-10-27 10:54:08 +0200640 }
641 break;
642 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200643 case FIO_OPT_DEPRECATED:
Jens Axboe06b0be62011-10-04 10:31:10 +0200644 log_info("Option %s is deprecated\n", o->name);
Jens Axboe15ca1502008-04-07 09:26:02 +0200645 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200646 default:
Jens Axboe06b0be62011-10-04 10:31:10 +0200647 log_err("Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200648 ret = 1;
649 }
650
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200651 if (ret)
652 return ret;
653
Jens Axboed447a8c2009-07-17 22:26:15 +0200654 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200655 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200656 if (ret) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200657 log_err("Correct format for offending option\n");
658 log_err("%20s: %s\n", o->name, o->help);
Jens Axboe06b0be62011-10-04 10:31:10 +0200659 show_option_help(o, 1);
Jens Axboed447a8c2009-07-17 22:26:15 +0200660 }
661 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200662
Jens Axboee1f36502006-10-27 10:54:08 +0200663 return ret;
664}
665
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200666static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100667{
Jens Axboeb62bdf22010-03-10 12:36:17 +0100668 char *o_ptr, *ptr, *ptr2;
669 int ret, done;
Jens Axboef90eff52006-11-06 11:08:21 +0100670
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200671 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
672
Jens Axboeb62bdf22010-03-10 12:36:17 +0100673 o_ptr = ptr = NULL;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200674 if (__ptr)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100675 o_ptr = ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100676
Jens Axboef90eff52006-11-06 11:08:21 +0100677 /*
Jens Axboeb62bdf22010-03-10 12:36:17 +0100678 * See if we have another set of parameters, hidden after a comma.
679 * Do this before parsing this round, to check if we should
Jens Axboe787f7e92006-11-06 13:26:29 +0100680 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100681 */
Jens Axboeb62bdf22010-03-10 12:36:17 +0100682 done = 0;
683 ret = 1;
684 do {
685 int __ret;
686
687 ptr2 = NULL;
688 if (ptr &&
689 (o->type != FIO_OPT_STR_STORE) &&
Yu-ju Hong83349192011-08-13 00:53:44 +0200690 (o->type != FIO_OPT_STR) &&
691 (o->type != FIO_OPT_FLOAT_LIST)) {
Jens Axboeb62bdf22010-03-10 12:36:17 +0100692 ptr2 = strchr(ptr, ',');
693 if (ptr2 && *(ptr2 + 1) == '\0')
694 *ptr2 = '\0';
695 if (o->type != FIO_OPT_STR_MULTI) {
696 if (!ptr2)
697 ptr2 = strchr(ptr, ':');
698 if (!ptr2)
699 ptr2 = strchr(ptr, '-');
700 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200701 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
702 ptr2 = strchr(ptr, ':');
Jens Axboeb62bdf22010-03-10 12:36:17 +0100703 }
704
705 /*
706 * Don't return early if parsing the first option fails - if
707 * we are doing multiple arguments, we can allow the first one
708 * being empty.
709 */
Yu-ju Hong83349192011-08-13 00:53:44 +0200710 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
Jens Axboeb62bdf22010-03-10 12:36:17 +0100711 if (ret)
712 ret = __ret;
713
Jens Axboe0c9baf92007-01-11 15:59:26 +0100714 if (!ptr2)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100715 break;
Jens Axboe787f7e92006-11-06 13:26:29 +0100716
Jens Axboeb62bdf22010-03-10 12:36:17 +0100717 ptr = ptr2 + 1;
718 done++;
719 } while (1);
Jens Axboe787f7e92006-11-06 13:26:29 +0100720
Jens Axboeb62bdf22010-03-10 12:36:17 +0100721 if (o_ptr)
722 free(o_ptr);
723 return ret;
Jens Axboef90eff52006-11-06 11:08:21 +0100724}
725
Jens Axboe3b8b7132008-06-10 19:46:23 +0200726static struct fio_option *get_option(const char *opt,
Jens Axboe07b32322010-03-05 09:48:44 +0100727 struct fio_option *options, char **post)
Jens Axboe3b8b7132008-06-10 19:46:23 +0200728{
729 struct fio_option *o;
730 char *ret;
731
732 ret = strchr(opt, '=');
733 if (ret) {
734 *post = ret;
735 *ret = '\0';
736 ret = (char *) opt;
737 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100738 strip_blank_end(ret);
Jens Axboe07b32322010-03-05 09:48:44 +0100739 o = find_option(options, ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200740 } else {
Jens Axboe07b32322010-03-05 09:48:44 +0100741 o = find_option(options, opt);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200742 *post = NULL;
743 }
744
745 return o;
746}
747
748static int opt_cmp(const void *p1, const void *p2)
749{
750 struct fio_option *o1, *o2;
751 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100752 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200753
754 s1 = strdup(*((char **) p1));
755 s2 = strdup(*((char **) p2));
756
Jens Axboe07b32322010-03-05 09:48:44 +0100757 o1 = get_option(s1, fio_options, &foo);
758 o2 = get_option(s2, fio_options, &foo);
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200759
Jens Axboe8cdabc12008-11-19 12:31:43 +0100760 prio1 = prio2 = 0;
761 if (o1)
762 prio1 = o1->prio;
763 if (o2)
764 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200765
766 free(s1);
767 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100768 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200769}
770
771void sort_options(char **opts, struct fio_option *options, int num_opts)
772{
773 fio_options = options;
774 qsort(opts, num_opts, sizeof(char *), opt_cmp);
775 fio_options = NULL;
776}
777
Jens Axboeb4692822006-10-27 13:43:22 +0200778int parse_cmd_option(const char *opt, const char *val,
Jens Axboe07b32322010-03-05 09:48:44 +0100779 struct fio_option *options, void *data)
Jens Axboeb4692822006-10-27 13:43:22 +0200780{
781 struct fio_option *o;
782
Jens Axboe07b32322010-03-05 09:48:44 +0100783 o = find_option(options, opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200784 if (!o) {
Jens Axboe06b0be62011-10-04 10:31:10 +0200785 log_err("Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200786 return 1;
787 }
788
Jens Axboeb1508cf2006-11-02 09:12:40 +0100789 if (!handle_option(o, val, data))
790 return 0;
791
Jens Axboe06b0be62011-10-04 10:31:10 +0200792 log_err("fio: failed parsing %s=%s\n", opt, val);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100793 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200794}
795
Aaron Carroll88b5a392008-10-07 11:25:20 +0200796/*
797 * Return a copy of the input string with substrings of the form ${VARNAME}
798 * substituted with the value of the environment variable VARNAME. The
799 * substitution always occurs, even if VARNAME is empty or the corresponding
800 * environment variable undefined.
801 */
802static char *option_dup_subs(const char *opt)
803{
804 char out[OPT_LEN_MAX+1];
805 char in[OPT_LEN_MAX+1];
806 char *outptr = out;
807 char *inptr = in;
808 char *ch1, *ch2, *env;
809 ssize_t nchr = OPT_LEN_MAX;
810 size_t envlen;
811
Jens Axboea0741cb2010-03-05 12:46:37 +0100812 if (strlen(opt) + 1 > OPT_LEN_MAX) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200813 log_err("OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
Jens Axboe38789b52010-03-03 09:23:20 +0100814 return NULL;
815 }
816
Aaron Carroll88b5a392008-10-07 11:25:20 +0200817 in[OPT_LEN_MAX] = '\0';
818 strncpy(in, opt, OPT_LEN_MAX);
819
820 while (*inptr && nchr > 0) {
821 if (inptr[0] == '$' && inptr[1] == '{') {
822 ch2 = strchr(inptr, '}');
823 if (ch2 && inptr+1 < ch2) {
824 ch1 = inptr+2;
825 inptr = ch2+1;
826 *ch2 = '\0';
827
828 env = getenv(ch1);
829 if (env) {
830 envlen = strlen(env);
831 if (envlen <= nchr) {
832 memcpy(outptr, env, envlen);
833 outptr += envlen;
834 nchr -= envlen;
835 }
836 }
837
838 continue;
839 }
840 }
841
842 *outptr++ = *inptr++;
843 --nchr;
844 }
845
846 *outptr = '\0';
847 return strdup(out);
848}
849
Jens Axboe07b32322010-03-05 09:48:44 +0100850int parse_option(const char *opt, struct fio_option *options, void *data)
Jens Axboee1f36502006-10-27 10:54:08 +0200851{
Jens Axboeb4692822006-10-27 13:43:22 +0200852 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200853 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200854
Aaron Carroll88b5a392008-10-07 11:25:20 +0200855 tmp = option_dup_subs(opt);
Jens Axboe38789b52010-03-03 09:23:20 +0100856 if (!tmp)
857 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200858
Jens Axboe07b32322010-03-05 09:48:44 +0100859 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200860 if (!o) {
Jens Axboe06b0be62011-10-04 10:31:10 +0200861 log_err("Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200862 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200863 return 1;
864 }
865
Jens Axboe0401bca2007-03-29 11:00:43 +0200866 if (!handle_option(o, post, data)) {
867 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100868 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200869 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100870
Jens Axboe06b0be62011-10-04 10:31:10 +0200871 log_err("fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200872 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100873 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200874}
Jens Axboefd28ca42007-01-09 21:20:13 +0100875
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100876/*
877 * Option match, levenshtein distance. Handy for not quite remembering what
878 * the option name is.
879 */
880static int string_distance(const char *s1, const char *s2)
881{
882 unsigned int s1_len = strlen(s1);
883 unsigned int s2_len = strlen(s2);
884 unsigned int *p, *q, *r;
885 unsigned int i, j;
886
887 p = malloc(sizeof(unsigned int) * (s2_len + 1));
888 q = malloc(sizeof(unsigned int) * (s2_len + 1));
889
890 p[0] = 0;
891 for (i = 1; i <= s2_len; i++)
892 p[i] = p[i - 1] + 1;
893
894 for (i = 1; i <= s1_len; i++) {
895 q[0] = p[0] + 1;
896 for (j = 1; j <= s2_len; j++) {
897 unsigned int sub = p[j - 1];
898
899 if (s1[i - 1] != s2[j - 1])
900 sub++;
901
902 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
903 }
904 r = p;
905 p = q;
906 q = r;
907 }
908
909 i = p[s2_len];
910 free(p);
911 free(q);
912 return i;
913}
914
Jens Axboeafdf9352007-07-31 16:14:34 +0200915static struct fio_option *find_child(struct fio_option *options,
916 struct fio_option *o)
917{
918 struct fio_option *__o;
919
Jens Axboefdf28742007-07-31 23:06:09 +0200920 for (__o = options + 1; __o->name; __o++)
921 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200922 return __o;
923
924 return NULL;
925}
926
Jens Axboe323d9112008-03-01 15:12:14 +0100927static void __print_option(struct fio_option *o, struct fio_option *org,
928 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200929{
930 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100931 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200932
933 if (!o)
934 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200935 if (!org)
936 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100937
Jens Axboeafdf9352007-07-31 16:14:34 +0200938 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100939 depth = level;
940 while (depth--)
941 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200942
943 sprintf(p, "%s", o->name);
944
Jens Axboe28d7c362011-10-05 20:30:24 +0200945 log_info("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100946}
947
948static void print_option(struct fio_option *o)
949{
950 struct fio_option *parent;
951 struct fio_option *__o;
952 unsigned int printed;
953 unsigned int level;
954
955 __print_option(o, NULL, 0);
956 parent = o;
957 level = 0;
958 do {
959 level++;
960 printed = 0;
961
962 while ((__o = find_child(o, parent)) != NULL) {
963 __print_option(__o, o, level);
964 o = __o;
965 printed++;
966 }
967
968 parent = o;
969 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200970}
971
Jens Axboe07b32322010-03-05 09:48:44 +0100972int show_cmd_help(struct fio_option *options, const char *name)
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100973{
974 struct fio_option *o, *closest;
Jens Axboed091d092010-04-01 19:56:23 +0200975 unsigned int best_dist = -1U;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100976 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100977 int show_all = 0;
978
979 if (!name || !strcmp(name, "all"))
980 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100981
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100982 closest = NULL;
983 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100984 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100985 int match = 0;
986
Jens Axboe15ca1502008-04-07 09:26:02 +0200987 if (o->type == FIO_OPT_DEPRECATED)
988 continue;
Jens Axboe07b32322010-03-05 09:48:44 +0100989 if (!exec_profile && o->prof_name)
990 continue;
Jens Axboe15ca1502008-04-07 09:26:02 +0200991
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100992 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100993 if (!strcmp(name, o->name) ||
994 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100995 match = 1;
996 else {
997 unsigned int dist;
998
999 dist = string_distance(name, o->name);
1000 if (dist < best_dist) {
1001 best_dist = dist;
1002 closest = o;
1003 }
1004 }
1005 }
Jens Axboefd28ca42007-01-09 21:20:13 +01001006
1007 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +01001008 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +01001009 if (match)
Jens Axboe28d7c362011-10-05 20:30:24 +02001010 log_info("%20s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +01001011 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +02001012 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +01001013 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +01001014 continue;
Jens Axboec167ded2007-03-14 13:02:53 +01001015 }
Jens Axboefd28ca42007-01-09 21:20:13 +01001016 }
1017
Jens Axboe70df2f12007-01-11 14:04:47 +01001018 if (!match)
1019 continue;
1020
Jens Axboe06b0be62011-10-04 10:31:10 +02001021 show_option_help(o, 0);
Jens Axboefd28ca42007-01-09 21:20:13 +01001022 }
1023
Jens Axboe29fc6af2007-01-09 21:22:02 +01001024 if (found)
1025 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +01001026
Jens Axboe28d7c362011-10-05 20:30:24 +02001027 log_err("No such command: %s", name);
Jens Axboed091d092010-04-01 19:56:23 +02001028
1029 /*
1030 * Only print an appropriately close option, one where the edit
1031 * distance isn't too big. Otherwise we get crazy matches.
1032 */
1033 if (closest && best_dist < 3) {
Jens Axboe28d7c362011-10-05 20:30:24 +02001034 log_info(" - showing closest match\n");
1035 log_info("%20s: %s\n", closest->name, closest->help);
Jens Axboe06b0be62011-10-04 10:31:10 +02001036 show_option_help(closest, 0);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001037 } else
Jens Axboe28d7c362011-10-05 20:30:24 +02001038 log_info("\n");
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001039
Jens Axboe29fc6af2007-01-09 21:22:02 +01001040 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +01001041}
Jens Axboeee738492007-01-10 11:23:16 +01001042
Jens Axboe13335dd2007-01-10 13:14:02 +01001043/*
1044 * Handle parsing of default parameters.
1045 */
Jens Axboeee738492007-01-10 11:23:16 +01001046void fill_default_options(void *data, struct fio_option *options)
1047{
Jens Axboe4945ba12007-01-11 14:36:56 +01001048 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +01001049
Jens Axboea3d741f2008-02-27 18:32:33 +01001050 dprint(FD_PARSE, "filling default options\n");
1051
Jens Axboe4945ba12007-01-11 14:36:56 +01001052 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +01001053 if (o->def)
1054 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +01001055}
Jens Axboe13335dd2007-01-10 13:14:02 +01001056
Jens Axboe9f988e22010-03-04 10:42:38 +01001057void option_init(struct fio_option *o)
1058{
1059 if (o->type == FIO_OPT_DEPRECATED)
1060 return;
1061 if (o->type == FIO_OPT_BOOL) {
1062 o->minval = 0;
1063 o->maxval = 1;
1064 }
Yu-ju Hong83349192011-08-13 00:53:44 +02001065 if (o->type == FIO_OPT_FLOAT_LIST) {
1066 o->minfp = NAN;
1067 o->maxfp = NAN;
1068 }
Jens Axboe9f988e22010-03-04 10:42:38 +01001069 if (o->type == FIO_OPT_STR_SET && o->def) {
Jens Axboe06b0be62011-10-04 10:31:10 +02001070 log_err("Option %s: string set option with"
Jens Axboe9f988e22010-03-04 10:42:38 +01001071 " default will always be true\n", o->name);
1072 }
Jens Axboe06b0be62011-10-04 10:31:10 +02001073 if (!o->cb && (!o->off1 && !o->roff1))
1074 log_err("Option %s: neither cb nor offset given\n", o->name);
Jens Axboe5f6ddf12010-03-09 20:40:44 +01001075 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1076 o->type == FIO_OPT_STR_MULTI)
Jens Axboe9f988e22010-03-04 10:42:38 +01001077 return;
Jens Axboee2de69d2010-03-04 14:05:48 +01001078 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
1079 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
Jens Axboe06b0be62011-10-04 10:31:10 +02001080 log_err("Option %s: both cb and offset given\n", o->name);
Jens Axboe9f988e22010-03-04 10:42:38 +01001081 }
1082}
1083
Jens Axboe13335dd2007-01-10 13:14:02 +01001084/*
1085 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +01001086 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +01001087 */
1088void options_init(struct fio_option *options)
1089{
Jens Axboe4945ba12007-01-11 14:36:56 +01001090 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +01001091
Jens Axboea3d741f2008-02-27 18:32:33 +01001092 dprint(FD_PARSE, "init options\n");
1093
Jens Axboe9f988e22010-03-04 10:42:38 +01001094 for (o = &options[0]; o->name; o++)
1095 option_init(o);
Jens Axboe13335dd2007-01-10 13:14:02 +01001096}