blob: 70cfa0b07a46c05f665f68c53659f776c6fa98bd [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 Axboe08e26e32006-11-21 13:15:10 +0100367 fprintf(stderr, "Option %s requires an argument\n", o->name);
368 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 Axboe5ec10ea2008-03-06 15:42:00 +0100421 fprintf(stderr, "max value out of range: %lld"
422 " (%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 Axboe5ec10ea2008-03-06 15:42:00 +0100426 fprintf(stderr, "min value out of range: %lld"
427 " (%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) {
472 fprintf(stderr, "the list exceeding max length %d\n",
473 o->maxlen);
474 return 1;
475 }
476 if(!str_to_float(ptr, &uf)){
477 fprintf(stderr, "not a floating point value: %s\n",
478 ptr);
479 return 1;
480 }
481 if (!isnan(o->maxfp) && uf > o->maxfp) {
482 fprintf(stderr, "value out of range: %f"
483 " (range max: %f)\n", uf, o->maxfp);
484 return 1;
485 }
486 if (!isnan(o->minfp) && uf < o->minfp) {
487 fprintf(stderr, "value out of range: %f"
488 " (range min: %f)\n", uf, o->minfp);
489 return 1;
490 }
491
492 flp = td_var(data, o->off1);
493 flp[curr] = uf;
494
495 break;
496 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100497 case FIO_OPT_STR_STORE: {
498 fio_opt_str_fn *fn = o->cb;
499
Jens Axboece952ab2011-08-31 14:29:22 -0600500 posval_sort(o, posval);
501
Jens Axboe1c964ce2011-08-31 16:45:03 -0600502 if (!o->posval[0].ival) {
503 vp = NULL;
Jens Axboee2979752011-08-31 12:55:27 -0600504 goto match;
Jens Axboe1c964ce2011-08-31 16:45:03 -0600505 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100506
Jens Axboec44b1ff2011-08-30 20:43:53 -0600507 ret = 1;
508 for (i = 0; i < PARSE_MAX_VP; i++) {
509 vp = &posval[i];
510 if (!vp->ival || vp->ival[0] == '\0')
511 continue;
512 all_skipped = 0;
513 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
514 char *rest;
515
516 ret = 0;
517 if (vp->cb)
518 fn = vp->cb;
Jens Axboee2979752011-08-31 12:55:27 -0600519match:
Jens Axboec44b1ff2011-08-30 20:43:53 -0600520 if (o->roff1)
521 cp = (char **) o->roff1;
522 else
523 cp = td_var(data, o->off1);
524 *cp = strdup(ptr);
525 rest = strstr(*cp, ":");
526 if (rest) {
527 *rest = '\0';
528 ptr = rest + 1;
Jens Axboe1c964ce2011-08-31 16:45:03 -0600529 } else if (vp && vp->cb)
Jens Axboec44b1ff2011-08-30 20:43:53 -0600530 ptr = NULL;
531 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100532 }
533 }
Jens Axboec44b1ff2011-08-30 20:43:53 -0600534
535 if (ret && !all_skipped)
536 show_option_values(o);
537 else if (fn && ptr)
538 ret = fn(data, ptr);
539
Jens Axboee1f36502006-10-27 10:54:08 +0200540 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100541 }
Jens Axboee1f36502006-10-27 10:54:08 +0200542 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200543 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200544 char *p1, *p2;
545
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100546 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200547
Dave Engberg31d23f42011-07-23 21:07:13 +0200548 /* Handle bsrange with separate read,write values: */
549 p1 = strchr(tmp, ',');
550 if (p1)
551 *p1 = '\0';
552
Jens Axboeb765a372006-10-27 15:00:16 +0200553 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200554 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100555 p1 = strchr(tmp, ':');
556 if (!p1) {
557 ret = 1;
558 break;
559 }
Jens Axboee1f36502006-10-27 10:54:08 +0200560 }
561
562 p2 = p1 + 1;
563 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200564 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200565
566 ret = 1;
Jens Axboed6978a32009-07-18 21:04:09 +0200567 if (!check_range_bytes(p1, &ul1, data) &&
568 !check_range_bytes(p2, &ul2, data)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200569 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200570 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100571 unsigned long foo = ul1;
572
573 ul1 = ul2;
574 ul2 = foo;
575 }
576
577 if (first) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100578 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100579 *(unsigned int *) o->roff1 = ul1;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100580 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100581 val_store(ilp, ul1, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100582 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100583 *(unsigned int *) o->roff2 = ul2;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100584 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100585 val_store(ilp, ul2, o->off2, 0, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200586 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100587 if (o->roff3 && o->roff4) {
Jens Axboe7758d4f2010-03-18 16:50:31 +0100588 *(unsigned int *) o->roff3 = ul1;
589 *(unsigned int *) o->roff4 = ul2;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100590 } else if (o->off3 && o->off4) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100591 val_store(ilp, ul1, o->off3, 0, data);
592 val_store(ilp, ul2, o->off4, 0, data);
Jens Axboe17abbe82006-11-06 11:23:10 +0100593 }
594 }
595
Jens Axboee1f36502006-10-27 10:54:08 +0200596 break;
597 }
Jens Axboe74ba1802011-07-15 09:09:15 +0200598 case FIO_OPT_BOOL:
599 case FIO_OPT_STR_SET: {
Jens Axboee1f36502006-10-27 10:54:08 +0200600 fio_opt_int_fn *fn = o->cb;
601
Jens Axboe74ba1802011-07-15 09:09:15 +0200602 if (ptr)
603 ret = check_int(ptr, &il);
604 else if (o->type == FIO_OPT_BOOL)
605 ret = 1;
606 else
607 il = 1;
608
Jens Axboee1f36502006-10-27 10:54:08 +0200609 if (ret)
610 break;
611
Jens Axboedb8e0162007-01-10 13:41:26 +0100612 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100613 fprintf(stderr, "max value out of range: %d (%d max)\n",
614 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100615 return 1;
616 }
617 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100618 fprintf(stderr, "min value out of range: %d (%d min)\n",
619 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100620 return 1;
621 }
Jens Axboee1f36502006-10-27 10:54:08 +0200622
Jens Axboe76a43db2007-01-11 13:24:44 +0100623 if (o->neg)
624 il = !il;
625
Jens Axboee1f36502006-10-27 10:54:08 +0200626 if (fn)
627 ret = fn(data, &il);
628 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100629 if (first) {
630 if (o->roff1)
631 *(unsigned int *)o->roff1 = il;
632 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100633 val_store(ilp, il, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100634 }
635 if (!more) {
636 if (o->roff2)
637 *(unsigned int *) o->roff2 = il;
638 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100639 val_store(ilp, il, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100640 }
Jens Axboee1f36502006-10-27 10:54:08 +0200641 }
642 break;
643 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200644 case FIO_OPT_DEPRECATED:
Jens Axboe06b0be62011-10-04 10:31:10 +0200645 log_info("Option %s is deprecated\n", o->name);
Jens Axboe15ca1502008-04-07 09:26:02 +0200646 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200647 default:
Jens Axboe06b0be62011-10-04 10:31:10 +0200648 log_err("Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200649 ret = 1;
650 }
651
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200652 if (ret)
653 return ret;
654
Jens Axboed447a8c2009-07-17 22:26:15 +0200655 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200656 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200657 if (ret) {
658 fprintf(stderr,"Correct format for offending option\n");
659 fprintf(stderr, "%20s: %s\n", o->name, o->help);
Jens Axboe06b0be62011-10-04 10:31:10 +0200660 show_option_help(o, 1);
Jens Axboed447a8c2009-07-17 22:26:15 +0200661 }
662 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200663
Jens Axboee1f36502006-10-27 10:54:08 +0200664 return ret;
665}
666
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200667static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100668{
Jens Axboeb62bdf22010-03-10 12:36:17 +0100669 char *o_ptr, *ptr, *ptr2;
670 int ret, done;
Jens Axboef90eff52006-11-06 11:08:21 +0100671
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200672 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
673
Jens Axboeb62bdf22010-03-10 12:36:17 +0100674 o_ptr = ptr = NULL;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200675 if (__ptr)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100676 o_ptr = ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100677
Jens Axboef90eff52006-11-06 11:08:21 +0100678 /*
Jens Axboeb62bdf22010-03-10 12:36:17 +0100679 * See if we have another set of parameters, hidden after a comma.
680 * Do this before parsing this round, to check if we should
Jens Axboe787f7e92006-11-06 13:26:29 +0100681 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100682 */
Jens Axboeb62bdf22010-03-10 12:36:17 +0100683 done = 0;
684 ret = 1;
685 do {
686 int __ret;
687
688 ptr2 = NULL;
689 if (ptr &&
690 (o->type != FIO_OPT_STR_STORE) &&
Yu-ju Hong83349192011-08-13 00:53:44 +0200691 (o->type != FIO_OPT_STR) &&
692 (o->type != FIO_OPT_FLOAT_LIST)) {
Jens Axboeb62bdf22010-03-10 12:36:17 +0100693 ptr2 = strchr(ptr, ',');
694 if (ptr2 && *(ptr2 + 1) == '\0')
695 *ptr2 = '\0';
696 if (o->type != FIO_OPT_STR_MULTI) {
697 if (!ptr2)
698 ptr2 = strchr(ptr, ':');
699 if (!ptr2)
700 ptr2 = strchr(ptr, '-');
701 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200702 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
703 ptr2 = strchr(ptr, ':');
Jens Axboeb62bdf22010-03-10 12:36:17 +0100704 }
705
706 /*
707 * Don't return early if parsing the first option fails - if
708 * we are doing multiple arguments, we can allow the first one
709 * being empty.
710 */
Yu-ju Hong83349192011-08-13 00:53:44 +0200711 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
Jens Axboeb62bdf22010-03-10 12:36:17 +0100712 if (ret)
713 ret = __ret;
714
Jens Axboe0c9baf92007-01-11 15:59:26 +0100715 if (!ptr2)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100716 break;
Jens Axboe787f7e92006-11-06 13:26:29 +0100717
Jens Axboeb62bdf22010-03-10 12:36:17 +0100718 ptr = ptr2 + 1;
719 done++;
720 } while (1);
Jens Axboe787f7e92006-11-06 13:26:29 +0100721
Jens Axboeb62bdf22010-03-10 12:36:17 +0100722 if (o_ptr)
723 free(o_ptr);
724 return ret;
Jens Axboef90eff52006-11-06 11:08:21 +0100725}
726
Jens Axboe3b8b7132008-06-10 19:46:23 +0200727static struct fio_option *get_option(const char *opt,
Jens Axboe07b32322010-03-05 09:48:44 +0100728 struct fio_option *options, char **post)
Jens Axboe3b8b7132008-06-10 19:46:23 +0200729{
730 struct fio_option *o;
731 char *ret;
732
733 ret = strchr(opt, '=');
734 if (ret) {
735 *post = ret;
736 *ret = '\0';
737 ret = (char *) opt;
738 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100739 strip_blank_end(ret);
Jens Axboe07b32322010-03-05 09:48:44 +0100740 o = find_option(options, ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200741 } else {
Jens Axboe07b32322010-03-05 09:48:44 +0100742 o = find_option(options, opt);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200743 *post = NULL;
744 }
745
746 return o;
747}
748
749static int opt_cmp(const void *p1, const void *p2)
750{
751 struct fio_option *o1, *o2;
752 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100753 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200754
755 s1 = strdup(*((char **) p1));
756 s2 = strdup(*((char **) p2));
757
Jens Axboe07b32322010-03-05 09:48:44 +0100758 o1 = get_option(s1, fio_options, &foo);
759 o2 = get_option(s2, fio_options, &foo);
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200760
Jens Axboe8cdabc12008-11-19 12:31:43 +0100761 prio1 = prio2 = 0;
762 if (o1)
763 prio1 = o1->prio;
764 if (o2)
765 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200766
767 free(s1);
768 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100769 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200770}
771
772void sort_options(char **opts, struct fio_option *options, int num_opts)
773{
774 fio_options = options;
775 qsort(opts, num_opts, sizeof(char *), opt_cmp);
776 fio_options = NULL;
777}
778
Jens Axboeb4692822006-10-27 13:43:22 +0200779int parse_cmd_option(const char *opt, const char *val,
Jens Axboe07b32322010-03-05 09:48:44 +0100780 struct fio_option *options, void *data)
Jens Axboeb4692822006-10-27 13:43:22 +0200781{
782 struct fio_option *o;
783
Jens Axboe07b32322010-03-05 09:48:44 +0100784 o = find_option(options, opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200785 if (!o) {
Jens Axboe06b0be62011-10-04 10:31:10 +0200786 log_err("Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200787 return 1;
788 }
789
Jens Axboeb1508cf2006-11-02 09:12:40 +0100790 if (!handle_option(o, val, data))
791 return 0;
792
Jens Axboe06b0be62011-10-04 10:31:10 +0200793 log_err("fio: failed parsing %s=%s\n", opt, val);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100794 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200795}
796
Aaron Carroll88b5a392008-10-07 11:25:20 +0200797/*
798 * Return a copy of the input string with substrings of the form ${VARNAME}
799 * substituted with the value of the environment variable VARNAME. The
800 * substitution always occurs, even if VARNAME is empty or the corresponding
801 * environment variable undefined.
802 */
803static char *option_dup_subs(const char *opt)
804{
805 char out[OPT_LEN_MAX+1];
806 char in[OPT_LEN_MAX+1];
807 char *outptr = out;
808 char *inptr = in;
809 char *ch1, *ch2, *env;
810 ssize_t nchr = OPT_LEN_MAX;
811 size_t envlen;
812
Jens Axboea0741cb2010-03-05 12:46:37 +0100813 if (strlen(opt) + 1 > OPT_LEN_MAX) {
Jens Axboe38789b52010-03-03 09:23:20 +0100814 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
815 return NULL;
816 }
817
Aaron Carroll88b5a392008-10-07 11:25:20 +0200818 in[OPT_LEN_MAX] = '\0';
819 strncpy(in, opt, OPT_LEN_MAX);
820
821 while (*inptr && nchr > 0) {
822 if (inptr[0] == '$' && inptr[1] == '{') {
823 ch2 = strchr(inptr, '}');
824 if (ch2 && inptr+1 < ch2) {
825 ch1 = inptr+2;
826 inptr = ch2+1;
827 *ch2 = '\0';
828
829 env = getenv(ch1);
830 if (env) {
831 envlen = strlen(env);
832 if (envlen <= nchr) {
833 memcpy(outptr, env, envlen);
834 outptr += envlen;
835 nchr -= envlen;
836 }
837 }
838
839 continue;
840 }
841 }
842
843 *outptr++ = *inptr++;
844 --nchr;
845 }
846
847 *outptr = '\0';
848 return strdup(out);
849}
850
Jens Axboe07b32322010-03-05 09:48:44 +0100851int parse_option(const char *opt, struct fio_option *options, void *data)
Jens Axboee1f36502006-10-27 10:54:08 +0200852{
Jens Axboeb4692822006-10-27 13:43:22 +0200853 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200854 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200855
Aaron Carroll88b5a392008-10-07 11:25:20 +0200856 tmp = option_dup_subs(opt);
Jens Axboe38789b52010-03-03 09:23:20 +0100857 if (!tmp)
858 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200859
Jens Axboe07b32322010-03-05 09:48:44 +0100860 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200861 if (!o) {
Jens Axboe06b0be62011-10-04 10:31:10 +0200862 log_err("Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200863 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200864 return 1;
865 }
866
Jens Axboe0401bca2007-03-29 11:00:43 +0200867 if (!handle_option(o, post, data)) {
868 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100869 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200870 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100871
Jens Axboe06b0be62011-10-04 10:31:10 +0200872 log_err("fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200873 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100874 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200875}
Jens Axboefd28ca42007-01-09 21:20:13 +0100876
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100877/*
878 * Option match, levenshtein distance. Handy for not quite remembering what
879 * the option name is.
880 */
881static int string_distance(const char *s1, const char *s2)
882{
883 unsigned int s1_len = strlen(s1);
884 unsigned int s2_len = strlen(s2);
885 unsigned int *p, *q, *r;
886 unsigned int i, j;
887
888 p = malloc(sizeof(unsigned int) * (s2_len + 1));
889 q = malloc(sizeof(unsigned int) * (s2_len + 1));
890
891 p[0] = 0;
892 for (i = 1; i <= s2_len; i++)
893 p[i] = p[i - 1] + 1;
894
895 for (i = 1; i <= s1_len; i++) {
896 q[0] = p[0] + 1;
897 for (j = 1; j <= s2_len; j++) {
898 unsigned int sub = p[j - 1];
899
900 if (s1[i - 1] != s2[j - 1])
901 sub++;
902
903 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
904 }
905 r = p;
906 p = q;
907 q = r;
908 }
909
910 i = p[s2_len];
911 free(p);
912 free(q);
913 return i;
914}
915
Jens Axboeafdf9352007-07-31 16:14:34 +0200916static struct fio_option *find_child(struct fio_option *options,
917 struct fio_option *o)
918{
919 struct fio_option *__o;
920
Jens Axboefdf28742007-07-31 23:06:09 +0200921 for (__o = options + 1; __o->name; __o++)
922 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200923 return __o;
924
925 return NULL;
926}
927
Jens Axboe323d9112008-03-01 15:12:14 +0100928static void __print_option(struct fio_option *o, struct fio_option *org,
929 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200930{
931 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100932 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200933
934 if (!o)
935 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200936 if (!org)
937 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100938
Jens Axboeafdf9352007-07-31 16:14:34 +0200939 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100940 depth = level;
941 while (depth--)
942 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200943
944 sprintf(p, "%s", o->name);
945
946 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100947}
948
949static void print_option(struct fio_option *o)
950{
951 struct fio_option *parent;
952 struct fio_option *__o;
953 unsigned int printed;
954 unsigned int level;
955
956 __print_option(o, NULL, 0);
957 parent = o;
958 level = 0;
959 do {
960 level++;
961 printed = 0;
962
963 while ((__o = find_child(o, parent)) != NULL) {
964 __print_option(__o, o, level);
965 o = __o;
966 printed++;
967 }
968
969 parent = o;
970 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200971}
972
Jens Axboe07b32322010-03-05 09:48:44 +0100973int show_cmd_help(struct fio_option *options, const char *name)
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100974{
975 struct fio_option *o, *closest;
Jens Axboed091d092010-04-01 19:56:23 +0200976 unsigned int best_dist = -1U;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100977 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100978 int show_all = 0;
979
980 if (!name || !strcmp(name, "all"))
981 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100982
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100983 closest = NULL;
984 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100985 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100986 int match = 0;
987
Jens Axboe15ca1502008-04-07 09:26:02 +0200988 if (o->type == FIO_OPT_DEPRECATED)
989 continue;
Jens Axboe07b32322010-03-05 09:48:44 +0100990 if (!exec_profile && o->prof_name)
991 continue;
Jens Axboe15ca1502008-04-07 09:26:02 +0200992
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100993 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100994 if (!strcmp(name, o->name) ||
995 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100996 match = 1;
997 else {
998 unsigned int dist;
999
1000 dist = string_distance(name, o->name);
1001 if (dist < best_dist) {
1002 best_dist = dist;
1003 closest = o;
1004 }
1005 }
1006 }
Jens Axboefd28ca42007-01-09 21:20:13 +01001007
1008 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +01001009 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +01001010 if (match)
Jens Axboe07b32322010-03-05 09:48:44 +01001011 printf("%20s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +01001012 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +02001013 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +01001014 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +01001015 continue;
Jens Axboec167ded2007-03-14 13:02:53 +01001016 }
Jens Axboefd28ca42007-01-09 21:20:13 +01001017 }
1018
Jens Axboe70df2f12007-01-11 14:04:47 +01001019 if (!match)
1020 continue;
1021
Jens Axboe06b0be62011-10-04 10:31:10 +02001022 show_option_help(o, 0);
Jens Axboefd28ca42007-01-09 21:20:13 +01001023 }
1024
Jens Axboe29fc6af2007-01-09 21:22:02 +01001025 if (found)
1026 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +01001027
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001028 printf("No such command: %s", name);
Jens Axboed091d092010-04-01 19:56:23 +02001029
1030 /*
1031 * Only print an appropriately close option, one where the edit
1032 * distance isn't too big. Otherwise we get crazy matches.
1033 */
1034 if (closest && best_dist < 3) {
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001035 printf(" - showing closest match\n");
1036 printf("%20s: %s\n", closest->name, closest->help);
Jens Axboe06b0be62011-10-04 10:31:10 +02001037 show_option_help(closest, 0);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001038 } else
1039 printf("\n");
1040
Jens Axboe29fc6af2007-01-09 21:22:02 +01001041 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +01001042}
Jens Axboeee738492007-01-10 11:23:16 +01001043
Jens Axboe13335dd2007-01-10 13:14:02 +01001044/*
1045 * Handle parsing of default parameters.
1046 */
Jens Axboeee738492007-01-10 11:23:16 +01001047void fill_default_options(void *data, struct fio_option *options)
1048{
Jens Axboe4945ba12007-01-11 14:36:56 +01001049 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +01001050
Jens Axboea3d741f2008-02-27 18:32:33 +01001051 dprint(FD_PARSE, "filling default options\n");
1052
Jens Axboe4945ba12007-01-11 14:36:56 +01001053 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +01001054 if (o->def)
1055 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +01001056}
Jens Axboe13335dd2007-01-10 13:14:02 +01001057
Jens Axboe9f988e22010-03-04 10:42:38 +01001058void option_init(struct fio_option *o)
1059{
1060 if (o->type == FIO_OPT_DEPRECATED)
1061 return;
1062 if (o->type == FIO_OPT_BOOL) {
1063 o->minval = 0;
1064 o->maxval = 1;
1065 }
Yu-ju Hong83349192011-08-13 00:53:44 +02001066 if (o->type == FIO_OPT_FLOAT_LIST) {
1067 o->minfp = NAN;
1068 o->maxfp = NAN;
1069 }
Jens Axboe9f988e22010-03-04 10:42:38 +01001070 if (o->type == FIO_OPT_STR_SET && o->def) {
Jens Axboe06b0be62011-10-04 10:31:10 +02001071 log_err("Option %s: string set option with"
Jens Axboe9f988e22010-03-04 10:42:38 +01001072 " default will always be true\n", o->name);
1073 }
Jens Axboe06b0be62011-10-04 10:31:10 +02001074 if (!o->cb && (!o->off1 && !o->roff1))
1075 log_err("Option %s: neither cb nor offset given\n", o->name);
Jens Axboe5f6ddf12010-03-09 20:40:44 +01001076 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1077 o->type == FIO_OPT_STR_MULTI)
Jens Axboe9f988e22010-03-04 10:42:38 +01001078 return;
Jens Axboee2de69d2010-03-04 14:05:48 +01001079 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
1080 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
Jens Axboe06b0be62011-10-04 10:31:10 +02001081 log_err("Option %s: both cb and offset given\n", o->name);
Jens Axboe9f988e22010-03-04 10:42:38 +01001082 }
1083}
1084
Jens Axboe13335dd2007-01-10 13:14:02 +01001085/*
1086 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +01001087 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +01001088 */
1089void options_init(struct fio_option *options)
1090{
Jens Axboe4945ba12007-01-11 14:36:56 +01001091 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +01001092
Jens Axboea3d741f2008-02-27 18:32:33 +01001093 dprint(FD_PARSE, "init options\n");
1094
Jens Axboe9f988e22010-03-04 10:42:38 +01001095 for (o = &options[0]; o->name; o++)
1096 option_init(o);
Jens Axboe13335dd2007-01-10 13:14:02 +01001097}