blob: 18c4530183dbb7b74ca4b97a2e6edf3e181c9595 [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 Axboee25839d2012-11-06 10:49:42 +010017#include "minmax.h"
Jens Axboecb2c86f2006-10-26 13:48:34 +020018
Jens Axboe3b8b7132008-06-10 19:46:23 +020019static struct fio_option *fio_options;
Jens Axboed6978a32009-07-18 21:04:09 +020020extern unsigned int fio_get_kb_base(void *);
Jens Axboe3b8b7132008-06-10 19:46:23 +020021
Jens Axboef0857372007-03-19 13:15:23 +010022static int vp_cmp(const void *p1, const void *p2)
23{
24 const struct value_pair *vp1 = p1;
25 const struct value_pair *vp2 = p2;
26
27 return strlen(vp2->ival) - strlen(vp1->ival);
28}
29
Jens Axboece952ab2011-08-31 14:29:22 -060030static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
Jens Axboef0857372007-03-19 13:15:23 +010031{
32 const struct value_pair *vp;
33 int entries;
34
35 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
36
37 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
38 vp = &o->posval[entries];
39 if (!vp->ival || vp->ival[0] == '\0')
40 break;
41
42 memcpy(&vpmap[entries], vp, sizeof(*vp));
43 }
44
45 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
46}
47
Jens Axboe06b0be62011-10-04 10:31:10 +020048static void show_option_range(struct fio_option *o,
49 int (*logger)(const char *format, ...))
Jens Axboeb1ec1da2007-02-23 10:29:16 +010050{
Yu-ju Hong83349192011-08-13 00:53:44 +020051 if (o->type == FIO_OPT_FLOAT_LIST){
52 if (isnan(o->minfp) && isnan(o->maxfp))
53 return;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010054
Jens Axboe06b0be62011-10-04 10:31:10 +020055 logger("%20s: min=%f", "range", o->minfp);
Yu-ju Hong83349192011-08-13 00:53:44 +020056 if (!isnan(o->maxfp))
Jens Axboe06b0be62011-10-04 10:31:10 +020057 logger(", max=%f", o->maxfp);
58 logger("\n");
Yu-ju Hong83349192011-08-13 00:53:44 +020059 } else {
60 if (!o->minval && !o->maxval)
61 return;
62
Jens Axboe06b0be62011-10-04 10:31:10 +020063 logger("%20s: min=%d", "range", o->minval);
Yu-ju Hong83349192011-08-13 00:53:44 +020064 if (o->maxval)
Jens Axboe06b0be62011-10-04 10:31:10 +020065 logger(", max=%d", o->maxval);
66 logger("\n");
Yu-ju Hong83349192011-08-13 00:53:44 +020067 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +010068}
69
70static void show_option_values(struct fio_option *o)
71{
Jens Axboea3073f42010-03-05 10:06:15 +010072 int i;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010073
Jens Axboea3073f42010-03-05 10:06:15 +010074 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboe78372132007-03-14 13:24:07 +010075 const struct value_pair *vp = &o->posval[i];
76
77 if (!vp->ival)
Jens Axboea3073f42010-03-05 10:06:15 +010078 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010079
Jens Axboe06b0be62011-10-04 10:31:10 +020080 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
Jens Axboe78372132007-03-14 13:24:07 +010081 if (vp->help)
Jens Axboe06b0be62011-10-04 10:31:10 +020082 log_info(" %s", vp->help);
83 log_info("\n");
Jens Axboea3073f42010-03-05 10:06:15 +010084 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +010085
86 if (i)
Jens Axboe06b0be62011-10-04 10:31:10 +020087 log_info("\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010088}
89
Jens Axboe06b0be62011-10-04 10:31:10 +020090static void show_option_help(struct fio_option *o, int is_err)
Jens Axboed447a8c2009-07-17 22:26:15 +020091{
92 const char *typehelp[] = {
Jens Axboe07b32322010-03-05 09:48:44 +010093 "invalid",
Jens Axboed447a8c2009-07-17 22:26:15 +020094 "string (opt=bla)",
Jens Axboeae3fb6f2010-09-14 13:10:35 +020095 "string (opt=bla)",
Jens Axboed447a8c2009-07-17 22:26:15 +020096 "string with possible k/m/g postfix (opt=4k)",
97 "string with time postfix (opt=10s)",
98 "string (opt=bla)",
99 "string with dual range (opt=1k-4k,4k-8k)",
100 "integer value (opt=100)",
101 "boolean value (opt=1)",
Yu-ju Hong83349192011-08-13 00:53:44 +0200102 "list of floating point values separated by ':' (opt=5.9:7.8)",
Jens Axboed447a8c2009-07-17 22:26:15 +0200103 "no argument (opt)",
Jens Axboe07b32322010-03-05 09:48:44 +0100104 "deprecated",
Jens Axboed447a8c2009-07-17 22:26:15 +0200105 };
Jens Axboe06b0be62011-10-04 10:31:10 +0200106 int (*logger)(const char *format, ...);
107
108 if (is_err)
109 logger = log_err;
110 else
111 logger = log_info;
Jens Axboed447a8c2009-07-17 22:26:15 +0200112
113 if (o->alias)
Jens Axboe06b0be62011-10-04 10:31:10 +0200114 logger("%20s: %s\n", "alias", o->alias);
Jens Axboed447a8c2009-07-17 22:26:15 +0200115
Jens Axboe06b0be62011-10-04 10:31:10 +0200116 logger("%20s: %s\n", "type", typehelp[o->type]);
117 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
Jens Axboe07b32322010-03-05 09:48:44 +0100118 if (o->prof_name)
Jens Axboe06b0be62011-10-04 10:31:10 +0200119 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
120 show_option_range(o, logger);
Jens Axboed447a8c2009-07-17 22:26:15 +0200121 show_option_values(o);
122}
123
Jens Axboecb2c86f2006-10-26 13:48:34 +0200124static unsigned long get_mult_time(char c)
125{
126 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100127 case 'm':
128 case 'M':
129 return 60;
130 case 'h':
131 case 'H':
132 return 60 * 60;
133 case 'd':
134 case 'D':
135 return 24 * 60 * 60;
136 default:
137 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200138 }
139}
140
Jens Axboe7bb59102011-07-12 19:47:03 +0200141static unsigned long long __get_mult_bytes(const char *p, void *data,
142 int *percent)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200143{
Jens Axboed6978a32009-07-18 21:04:09 +0200144 unsigned int kb_base = fio_get_kb_base(data);
Jens Axboea639f0b2009-07-18 08:25:35 +0200145 unsigned long long ret = 1;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200146 unsigned int i, pow = 0, mult = kb_base;
147 char *c;
Jens Axboea639f0b2009-07-18 08:25:35 +0200148
Jens Axboe57fc29f2010-06-23 22:24:07 +0200149 if (!p)
150 return 1;
Jens Axboea639f0b2009-07-18 08:25:35 +0200151
Jens Axboe57fc29f2010-06-23 22:24:07 +0200152 c = strdup(p);
153
154 for (i = 0; i < strlen(c); i++)
155 c[i] = tolower(c[i]);
156
157 if (!strcmp("pib", c)) {
158 pow = 5;
159 mult = 1000;
160 } else if (!strcmp("tib", c)) {
161 pow = 4;
162 mult = 1000;
163 } else if (!strcmp("gib", c)) {
164 pow = 3;
165 mult = 1000;
166 } else if (!strcmp("mib", c)) {
167 pow = 2;
168 mult = 1000;
169 } else if (!strcmp("kib", c)) {
170 pow = 1;
171 mult = 1000;
172 } else if (!strcmp("p", c) || !strcmp("pb", c))
173 pow = 5;
174 else if (!strcmp("t", c) || !strcmp("tb", c))
175 pow = 4;
176 else if (!strcmp("g", c) || !strcmp("gb", c))
177 pow = 3;
178 else if (!strcmp("m", c) || !strcmp("mb", c))
179 pow = 2;
180 else if (!strcmp("k", c) || !strcmp("kb", c))
181 pow = 1;
Jens Axboe7bb59102011-07-12 19:47:03 +0200182 else if (!strcmp("%", c)) {
183 *percent = 1;
Jens Axboee721c572012-02-09 20:55:29 +0100184 free(c);
Jens Axboe7bb59102011-07-12 19:47:03 +0200185 return ret;
186 }
Jens Axboe57fc29f2010-06-23 22:24:07 +0200187
188 while (pow--)
189 ret *= (unsigned long long) mult;
190
191 free(c);
Jens Axboea639f0b2009-07-18 08:25:35 +0200192 return ret;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200193}
194
Jens Axboe7bb59102011-07-12 19:47:03 +0200195static unsigned long long get_mult_bytes(const char *str, int len, void *data,
Jens Axboe6925dd32011-09-07 22:10:11 +0200196 int *percent)
Jens Axboe57fc29f2010-06-23 22:24:07 +0200197{
Jens Axboe55ed9632011-03-27 20:55:09 +0200198 const char *p = str;
Jens Axboeba4ddd62011-09-07 22:11:00 +0200199 int digit_seen = 0;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200200
Jens Axboe1d1c1872010-07-29 10:50:05 +0200201 if (len < 2)
Jens Axboe7bb59102011-07-12 19:47:03 +0200202 return __get_mult_bytes(str, data, percent);
Jens Axboe1d1c1872010-07-29 10:50:05 +0200203
Steven Langd0c814e2011-10-28 08:37:13 +0200204 /*
205 * Go forward until we hit a non-digit, or +/- sign
206 */
Jens Axboe55ed9632011-03-27 20:55:09 +0200207 while ((p - str) <= len) {
Jens Axboeba4ddd62011-09-07 22:11:00 +0200208 if (!isdigit((int) *p) &&
209 (((*p != '+') && (*p != '-')) || digit_seen))
Jens Axboe55ed9632011-03-27 20:55:09 +0200210 break;
Jens Axboe3c703d12011-09-21 09:27:24 +0200211 digit_seen |= isdigit((int) *p);
Jens Axboe55ed9632011-03-27 20:55:09 +0200212 p++;
213 }
214
Jens Axboe7bb59102011-07-12 19:47:03 +0200215 if (!isalpha((int) *p) && (*p != '%'))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200216 p = NULL;
217
Jens Axboe7bb59102011-07-12 19:47:03 +0200218 return __get_mult_bytes(p, data, percent);
Jens Axboe57fc29f2010-06-23 22:24:07 +0200219}
220
Jens Axboecb2c86f2006-10-26 13:48:34 +0200221/*
Yu-ju Hong83349192011-08-13 00:53:44 +0200222 * Convert string into a floating number. Return 1 for success and 0 otherwise.
223 */
Jens Axboee25839d2012-11-06 10:49:42 +0100224int str_to_float(const char *str, double *val)
Yu-ju Hong83349192011-08-13 00:53:44 +0200225{
226 return (1 == sscanf(str, "%lf", val));
227}
228
229/*
Jens Axboee1f36502006-10-27 10:54:08 +0200230 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200231 */
Jens Axboe6925dd32011-09-07 22:10:11 +0200232int str_to_decimal(const char *str, long long *val, int kilo, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200233{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100234 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200235
Jens Axboecb2c86f2006-10-26 13:48:34 +0200236 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100237 if (!len)
238 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200239
Jens Axboeb347f9d2009-03-09 14:15:21 +0100240 if (strstr(str, "0x") || strstr(str, "0X"))
241 base = 16;
242 else
243 base = 10;
244
245 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100246 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200247 return 1;
248
Jens Axboe7bb59102011-07-12 19:47:03 +0200249 if (kilo) {
250 unsigned long long mult;
251 int perc = 0;
252
Jens Axboe6925dd32011-09-07 22:10:11 +0200253 mult = get_mult_bytes(str, len, data, &perc);
Jens Axboe7bb59102011-07-12 19:47:03 +0200254 if (perc)
255 *val = -1ULL - *val;
256 else
257 *val *= mult;
258 } else
Jens Axboecb2c86f2006-10-26 13:48:34 +0200259 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100260
Jens Axboecb2c86f2006-10-26 13:48:34 +0200261 return 0;
262}
263
Jens Axboe6925dd32011-09-07 22:10:11 +0200264static int check_str_bytes(const char *p, long long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200265{
Jens Axboe6925dd32011-09-07 22:10:11 +0200266 return str_to_decimal(p, val, 1, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200267}
268
Jens Axboe63f29372007-01-10 13:20:09 +0100269static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200270{
Jens Axboe6925dd32011-09-07 22:10:11 +0200271 return str_to_decimal(p, val, 0, NULL);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200272}
273
274void strip_blank_front(char **p)
275{
276 char *s = *p;
277
Jens Axboe4c8e9642011-10-15 14:37:38 +0200278 if (!strlen(s))
279 return;
Jens Axboe76cd9372011-07-08 21:14:57 +0200280 while (isspace((int) *s))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200281 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200282
283 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200284}
285
286void strip_blank_end(char *p)
287{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100288 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200289
Jens Axboe4c8e9642011-10-15 14:37:38 +0200290 if (!strlen(p))
291 return;
292
Jens Axboe523bfad2007-04-04 11:04:06 +0200293 s = strchr(p, ';');
294 if (s)
295 *s = '\0';
296 s = strchr(p, '#');
297 if (s)
298 *s = '\0';
299 if (s)
300 p = s;
301
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200302 s = p + strlen(p);
Jens Axboe76cd9372011-07-08 21:14:57 +0200303 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200304 s--;
305
306 *(s + 1) = '\0';
307}
308
Jens Axboed6978a32009-07-18 21:04:09 +0200309static int check_range_bytes(const char *str, long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200310{
Jens Axboe57fc29f2010-06-23 22:24:07 +0200311 long long __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200312
Jens Axboe6925dd32011-09-07 22:10:11 +0200313 if (!str_to_decimal(str, &__val, 1, data)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200314 *val = __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200315 return 0;
316 }
317
Jens Axboecb2c86f2006-10-26 13:48:34 +0200318 return 1;
319}
320
Jens Axboe63f29372007-01-10 13:20:09 +0100321static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200322{
Jens Axboe787f7e92006-11-06 13:26:29 +0100323 if (!strlen(p))
324 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200325 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200326 if (sscanf(p, "%x", val) == 1)
327 return 0;
328 } else {
329 if (sscanf(p, "%u", val) == 1)
330 return 0;
331 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200332
333 return 1;
334}
335
Jens Axboe808def72010-07-14 16:27:02 -0600336static int opt_len(const char *str)
337{
338 char *postfix;
339
340 postfix = strchr(str, ':');
341 if (!postfix)
342 return strlen(str);
343
344 return (int)(postfix - str);
345}
346
Jens Axboe119cd932012-12-06 17:34:57 +0100347static int str_match_len(const struct value_pair *vp, const char *str)
348{
349 return max(strlen(vp->ival), opt_len(str));
350}
351
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100352#define val_store(ptr, val, off, or, data) \
Jens Axboe17abbe82006-11-06 11:23:10 +0100353 do { \
354 ptr = td_var((data), (off)); \
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100355 if ((or)) \
356 *ptr |= (val); \
357 else \
358 *ptr = (val); \
Jens Axboe17abbe82006-11-06 11:23:10 +0100359 } while (0)
360
Jens Axboef90eff52006-11-06 11:08:21 +0100361static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Yu-ju Hong83349192011-08-13 00:53:44 +0200362 int first, int more, int curr)
Jens Axboee1f36502006-10-27 10:54:08 +0200363{
Jens Axboe63f29372007-01-10 13:20:09 +0100364 int il, *ilp;
Yu-ju Hong83349192011-08-13 00:53:44 +0200365 double* flp;
Jens Axboe63f29372007-01-10 13:20:09 +0100366 long long ull, *ullp;
367 long ul1, ul2;
Yu-ju Hong83349192011-08-13 00:53:44 +0200368 double uf;
Jens Axboebfe1d592012-11-29 21:33:03 +0100369 char **cp = NULL;
Jens Axboee42b01e2011-05-05 12:05:10 -0600370 int ret = 0, is_time = 0;
Jens Axboec44b1ff2011-08-30 20:43:53 -0600371 const struct value_pair *vp;
372 struct value_pair posval[PARSE_MAX_VP];
373 int i, all_skipped = 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200374
Jens Axboea3d741f2008-02-27 18:32:33 +0100375 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
376 o->type, ptr);
377
Jens Axboee3cedca2008-11-19 19:57:52 +0100378 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200379 log_err("Option %s requires an argument\n", o->name);
Jens Axboe08e26e32006-11-21 13:15:10 +0100380 return 1;
381 }
382
Jens Axboee1f36502006-10-27 10:54:08 +0200383 switch (o->type) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100384 case FIO_OPT_STR:
385 case FIO_OPT_STR_MULTI: {
Jens Axboee1f36502006-10-27 10:54:08 +0200386 fio_opt_str_fn *fn = o->cb;
387
Jens Axboef0857372007-03-19 13:15:23 +0100388 posval_sort(o, posval);
389
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100390 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100391 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100392 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100393 if (!vp->ival || vp->ival[0] == '\0')
Jens Axboea3073f42010-03-05 10:06:15 +0100394 continue;
Jens Axboeae2ddba2010-03-10 12:49:03 +0100395 all_skipped = 0;
Jens Axboe119cd932012-12-06 17:34:57 +0100396 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100397 ret = 0;
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100398 if (o->roff1) {
399 if (vp->or)
400 *(unsigned int *) o->roff1 |= vp->oval;
401 else
402 *(unsigned int *) o->roff1 = vp->oval;
403 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100404 if (!o->off1)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100405 continue;
406 val_store(ilp, vp->oval, o->off1, vp->or, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100407 }
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100408 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100409 }
410 }
411
Jens Axboeae2ddba2010-03-10 12:49:03 +0100412 if (ret && !all_skipped)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100413 show_option_values(o);
414 else if (fn)
415 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200416 break;
417 }
Jens Axboee42b01e2011-05-05 12:05:10 -0600418 case FIO_OPT_STR_VAL_TIME:
419 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100420 case FIO_OPT_INT:
Jens Axboee42b01e2011-05-05 12:05:10 -0600421 case FIO_OPT_STR_VAL: {
422 fio_opt_str_val_fn *fn = o->cb;
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200423 char tmp[128], *p;
424
425 strncpy(tmp, ptr, sizeof(tmp) - 1);
426 p = strchr(tmp, ',');
427 if (p)
428 *p = '\0';
Jens Axboee1f36502006-10-27 10:54:08 +0200429
Jens Axboee42b01e2011-05-05 12:05:10 -0600430 if (is_time)
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200431 ret = check_str_time(tmp, &ull);
Jens Axboee42b01e2011-05-05 12:05:10 -0600432 else
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200433 ret = check_str_bytes(tmp, &ull, data);
Jens Axboee42b01e2011-05-05 12:05:10 -0600434
Jens Axboee1f36502006-10-27 10:54:08 +0200435 if (ret)
436 break;
437
Jens Axboedb8e0162007-01-10 13:41:26 +0100438 if (o->maxval && ull > o->maxval) {
Jens Axboed4fc2f02012-09-24 14:32:26 +0200439 log_err("max value out of range: %llu"
440 " (%u max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100441 return 1;
442 }
443 if (o->minval && ull < o->minval) {
Jens Axboed4fc2f02012-09-24 14:32:26 +0200444 log_err("min value out of range: %llu"
445 " (%u min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100446 return 1;
447 }
Jens Axboee1f36502006-10-27 10:54:08 +0200448
449 if (fn)
450 ret = fn(data, &ull);
451 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200452 if (o->type == FIO_OPT_INT) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100453 if (first) {
454 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100455 *(unsigned int *) o->roff1 = ull;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100456 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100457 val_store(ilp, ull, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100458 }
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200459 if (curr == 1) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100460 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100461 *(unsigned int *) o->roff2 = ull;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100462 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100463 val_store(ilp, ull, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100464 }
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200465 if (curr == 2) {
466 if (o->roff3)
467 *(unsigned int *) o->roff3 = ull;
468 else if (o->off3)
469 val_store(ilp, ull, o->off3, 0, data);
470 }
471 if (!more) {
472 if (curr < 1) {
473 if (o->roff2)
474 *(unsigned int *) o->roff2 = ull;
475 else if (o->off2)
476 val_store(ilp, ull, o->off2, 0, data);
477 }
478 if (curr < 2) {
479 if (o->roff3)
480 *(unsigned int *) o->roff3 = ull;
481 else if (o->off3)
482 val_store(ilp, ull, o->off3, 0, data);
483 }
484 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100485 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100486 if (first) {
487 if (o->roff1)
488 *(unsigned long long *) o->roff1 = ull;
489 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100490 val_store(ullp, ull, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100491 }
492 if (!more) {
493 if (o->roff2)
494 *(unsigned long long *) o->roff2 = ull;
495 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100496 val_store(ullp, ull, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100497 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100498 }
Jens Axboee1f36502006-10-27 10:54:08 +0200499 }
500 break;
501 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200502 case FIO_OPT_FLOAT_LIST: {
503
504 if (first) {
505 ul2 = 1;
506 ilp = td_var(data, o->off2);
507 *ilp = ul2;
508 }
509 if (curr >= o->maxlen) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200510 log_err("the list exceeding max length %d\n",
Yu-ju Hong83349192011-08-13 00:53:44 +0200511 o->maxlen);
512 return 1;
513 }
Jens Axboee25839d2012-11-06 10:49:42 +0100514 if (!str_to_float(ptr, &uf)){
Jens Axboe28d7c362011-10-05 20:30:24 +0200515 log_err("not a floating point value: %s\n", ptr);
Yu-ju Hong83349192011-08-13 00:53:44 +0200516 return 1;
517 }
518 if (!isnan(o->maxfp) && uf > o->maxfp) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200519 log_err("value out of range: %f"
Yu-ju Hong83349192011-08-13 00:53:44 +0200520 " (range max: %f)\n", uf, o->maxfp);
521 return 1;
522 }
523 if (!isnan(o->minfp) && uf < o->minfp) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200524 log_err("value out of range: %f"
Yu-ju Hong83349192011-08-13 00:53:44 +0200525 " (range min: %f)\n", uf, o->minfp);
526 return 1;
527 }
528
529 flp = td_var(data, o->off1);
530 flp[curr] = uf;
531
532 break;
533 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100534 case FIO_OPT_STR_STORE: {
535 fio_opt_str_fn *fn = o->cb;
536
Steven Lang184b4092011-11-16 10:33:51 +0100537 if (o->roff1 || o->off1) {
538 if (o->roff1)
539 cp = (char **) o->roff1;
540 else if (o->off1)
541 cp = td_var(data, o->off1);
Jens Axboece952ab2011-08-31 14:29:22 -0600542
Steven Lang184b4092011-11-16 10:33:51 +0100543 *cp = strdup(ptr);
Jens Axboe1c964ce2011-08-31 16:45:03 -0600544 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100545
Steven Lang184b4092011-11-16 10:33:51 +0100546 if (fn)
547 ret = fn(data, ptr);
548 else if (o->posval[0].ival) {
549 posval_sort(o, posval);
Jens Axboec44b1ff2011-08-30 20:43:53 -0600550
Steven Lang184b4092011-11-16 10:33:51 +0100551 ret = 1;
552 for (i = 0; i < PARSE_MAX_VP; i++) {
553 vp = &posval[i];
554 if (!vp->ival || vp->ival[0] == '\0')
555 continue;
556 all_skipped = 0;
Jens Axboe119cd932012-12-06 17:34:57 +0100557 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
Steven Lang184b4092011-11-16 10:33:51 +0100558 char *rest;
559
560 ret = 0;
561 if (vp->cb)
562 fn = vp->cb;
563 rest = strstr(*cp ?: ptr, ":");
564 if (rest) {
565 if (*cp)
566 *rest = '\0';
567 ptr = rest + 1;
568 } else
569 ptr = NULL;
570 break;
571 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100572 }
573 }
Jens Axboec44b1ff2011-08-30 20:43:53 -0600574
Steven Lang184b4092011-11-16 10:33:51 +0100575 if (!all_skipped) {
576 if (ret && !*cp)
577 show_option_values(o);
578 else if (ret && *cp)
579 ret = 0;
580 else if (fn && ptr)
581 ret = fn(data, ptr);
582 }
Jens Axboec44b1ff2011-08-30 20:43:53 -0600583
Jens Axboee1f36502006-10-27 10:54:08 +0200584 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100585 }
Jens Axboee1f36502006-10-27 10:54:08 +0200586 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200587 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200588 char *p1, *p2;
589
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100590 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200591
Dave Engberg31d23f42011-07-23 21:07:13 +0200592 /* Handle bsrange with separate read,write values: */
593 p1 = strchr(tmp, ',');
594 if (p1)
595 *p1 = '\0';
596
Jens Axboeb765a372006-10-27 15:00:16 +0200597 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200598 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100599 p1 = strchr(tmp, ':');
600 if (!p1) {
601 ret = 1;
602 break;
603 }
Jens Axboee1f36502006-10-27 10:54:08 +0200604 }
605
606 p2 = p1 + 1;
607 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200608 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200609
610 ret = 1;
Jens Axboed6978a32009-07-18 21:04:09 +0200611 if (!check_range_bytes(p1, &ul1, data) &&
612 !check_range_bytes(p2, &ul2, data)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200613 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200614 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100615 unsigned long foo = ul1;
616
617 ul1 = ul2;
618 ul2 = foo;
619 }
620
621 if (first) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100622 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100623 *(unsigned int *) o->roff1 = ul1;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100624 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100625 val_store(ilp, ul1, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100626 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100627 *(unsigned int *) o->roff2 = ul2;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100628 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100629 val_store(ilp, ul2, o->off2, 0, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200630 }
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200631 if (curr == 1) {
632 if (o->roff3 && o->roff4) {
633 *(unsigned int *) o->roff3 = ul1;
634 *(unsigned int *) o->roff4 = ul2;
635 } else if (o->off3 && o->off4) {
636 val_store(ilp, ul1, o->off3, 0, data);
637 val_store(ilp, ul2, o->off4, 0, data);
638 }
639 }
640 if (curr == 2) {
641 if (o->roff5 && o->roff6) {
642 *(unsigned int *) o->roff5 = ul1;
643 *(unsigned int *) o->roff6 = ul2;
644 } else if (o->off5 && o->off6) {
645 val_store(ilp, ul1, o->off5, 0, data);
646 val_store(ilp, ul2, o->off6, 0, data);
647 }
648 }
649 if (!more) {
650 if (curr < 1) {
651 if (o->roff3 && o->roff4) {
652 *(unsigned int *) o->roff3 = ul1;
653 *(unsigned int *) o->roff4 = ul2;
654 } else if (o->off3 && o->off4) {
655 val_store(ilp, ul1, o->off3, 0, data);
656 val_store(ilp, ul2, o->off4, 0, data);
657 }
658 }
659 if (curr < 2) {
660 if (o->roff5 && o->roff6) {
661 *(unsigned int *) o->roff5 = ul1;
662 *(unsigned int *) o->roff6 = ul2;
663 } else if (o->off5 && o->off6) {
664 val_store(ilp, ul1, o->off5, 0, data);
665 val_store(ilp, ul2, o->off6, 0, data);
666 }
667 }
Jens Axboe17abbe82006-11-06 11:23:10 +0100668 }
669 }
670
Jens Axboee1f36502006-10-27 10:54:08 +0200671 break;
672 }
Jens Axboe74ba1802011-07-15 09:09:15 +0200673 case FIO_OPT_BOOL:
674 case FIO_OPT_STR_SET: {
Jens Axboee1f36502006-10-27 10:54:08 +0200675 fio_opt_int_fn *fn = o->cb;
676
Jens Axboe74ba1802011-07-15 09:09:15 +0200677 if (ptr)
678 ret = check_int(ptr, &il);
679 else if (o->type == FIO_OPT_BOOL)
680 ret = 1;
681 else
682 il = 1;
683
Jens Axboee1f36502006-10-27 10:54:08 +0200684 if (ret)
685 break;
686
Jens Axboedb8e0162007-01-10 13:41:26 +0100687 if (o->maxval && il > (int) o->maxval) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200688 log_err("max value out of range: %d (%d max)\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100689 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100690 return 1;
691 }
692 if (o->minval && il < o->minval) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200693 log_err("min value out of range: %d (%d min)\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100694 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100695 return 1;
696 }
Jens Axboee1f36502006-10-27 10:54:08 +0200697
Jens Axboe76a43db2007-01-11 13:24:44 +0100698 if (o->neg)
699 il = !il;
700
Jens Axboee1f36502006-10-27 10:54:08 +0200701 if (fn)
702 ret = fn(data, &il);
703 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100704 if (first) {
705 if (o->roff1)
706 *(unsigned int *)o->roff1 = il;
707 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100708 val_store(ilp, il, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100709 }
710 if (!more) {
711 if (o->roff2)
712 *(unsigned int *) o->roff2 = il;
713 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100714 val_store(ilp, il, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100715 }
Jens Axboee1f36502006-10-27 10:54:08 +0200716 }
717 break;
718 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200719 case FIO_OPT_DEPRECATED:
Jens Axboe06b0be62011-10-04 10:31:10 +0200720 log_info("Option %s is deprecated\n", o->name);
Jens Axboe15ca1502008-04-07 09:26:02 +0200721 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200722 default:
Jens Axboe06b0be62011-10-04 10:31:10 +0200723 log_err("Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200724 ret = 1;
725 }
726
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200727 if (ret)
728 return ret;
729
Jens Axboed447a8c2009-07-17 22:26:15 +0200730 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200731 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200732 if (ret) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200733 log_err("Correct format for offending option\n");
734 log_err("%20s: %s\n", o->name, o->help);
Jens Axboe06b0be62011-10-04 10:31:10 +0200735 show_option_help(o, 1);
Jens Axboed447a8c2009-07-17 22:26:15 +0200736 }
737 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200738
Jens Axboee1f36502006-10-27 10:54:08 +0200739 return ret;
740}
741
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200742static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100743{
Jens Axboeb62bdf22010-03-10 12:36:17 +0100744 char *o_ptr, *ptr, *ptr2;
745 int ret, done;
Jens Axboef90eff52006-11-06 11:08:21 +0100746
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200747 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
748
Jens Axboeb62bdf22010-03-10 12:36:17 +0100749 o_ptr = ptr = NULL;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200750 if (__ptr)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100751 o_ptr = ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100752
Jens Axboef90eff52006-11-06 11:08:21 +0100753 /*
Jens Axboeb62bdf22010-03-10 12:36:17 +0100754 * See if we have another set of parameters, hidden after a comma.
755 * Do this before parsing this round, to check if we should
Jens Axboe787f7e92006-11-06 13:26:29 +0100756 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100757 */
Jens Axboeb62bdf22010-03-10 12:36:17 +0100758 done = 0;
759 ret = 1;
760 do {
761 int __ret;
762
763 ptr2 = NULL;
764 if (ptr &&
765 (o->type != FIO_OPT_STR_STORE) &&
Yu-ju Hong83349192011-08-13 00:53:44 +0200766 (o->type != FIO_OPT_STR) &&
767 (o->type != FIO_OPT_FLOAT_LIST)) {
Jens Axboeb62bdf22010-03-10 12:36:17 +0100768 ptr2 = strchr(ptr, ',');
769 if (ptr2 && *(ptr2 + 1) == '\0')
770 *ptr2 = '\0';
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200771 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
Jens Axboeb62bdf22010-03-10 12:36:17 +0100772 if (!ptr2)
773 ptr2 = strchr(ptr, ':');
774 if (!ptr2)
775 ptr2 = strchr(ptr, '-');
776 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200777 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
778 ptr2 = strchr(ptr, ':');
Jens Axboeb62bdf22010-03-10 12:36:17 +0100779 }
780
781 /*
782 * Don't return early if parsing the first option fails - if
783 * we are doing multiple arguments, we can allow the first one
784 * being empty.
785 */
Yu-ju Hong83349192011-08-13 00:53:44 +0200786 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
Jens Axboeb62bdf22010-03-10 12:36:17 +0100787 if (ret)
788 ret = __ret;
789
Jens Axboe0c9baf92007-01-11 15:59:26 +0100790 if (!ptr2)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100791 break;
Jens Axboe787f7e92006-11-06 13:26:29 +0100792
Jens Axboeb62bdf22010-03-10 12:36:17 +0100793 ptr = ptr2 + 1;
794 done++;
795 } while (1);
Jens Axboe787f7e92006-11-06 13:26:29 +0100796
Jens Axboeb62bdf22010-03-10 12:36:17 +0100797 if (o_ptr)
798 free(o_ptr);
799 return ret;
Jens Axboef90eff52006-11-06 11:08:21 +0100800}
801
Steven Langde890a12011-11-09 14:03:34 +0100802static struct fio_option *get_option(char *opt,
Jens Axboe07b32322010-03-05 09:48:44 +0100803 struct fio_option *options, char **post)
Jens Axboe3b8b7132008-06-10 19:46:23 +0200804{
805 struct fio_option *o;
806 char *ret;
807
808 ret = strchr(opt, '=');
809 if (ret) {
810 *post = ret;
811 *ret = '\0';
Steven Langde890a12011-11-09 14:03:34 +0100812 ret = opt;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200813 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100814 strip_blank_end(ret);
Jens Axboe07b32322010-03-05 09:48:44 +0100815 o = find_option(options, ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200816 } else {
Jens Axboe07b32322010-03-05 09:48:44 +0100817 o = find_option(options, opt);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200818 *post = NULL;
819 }
820
821 return o;
822}
823
824static int opt_cmp(const void *p1, const void *p2)
825{
Steven Langde890a12011-11-09 14:03:34 +0100826 struct fio_option *o;
827 char *s, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100828 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200829
Jens Axboe8cdabc12008-11-19 12:31:43 +0100830 prio1 = prio2 = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200831
Steven Langde890a12011-11-09 14:03:34 +0100832 if (*(char **)p1) {
833 s = strdup(*((char **) p1));
834 o = get_option(s, fio_options, &foo);
835 if (o)
836 prio1 = o->prio;
837 free(s);
838 }
839 if (*(char **)p2) {
840 s = strdup(*((char **) p2));
841 o = get_option(s, fio_options, &foo);
842 if (o)
843 prio2 = o->prio;
844 free(s);
845 }
846
Jens Axboe8cdabc12008-11-19 12:31:43 +0100847 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200848}
849
850void sort_options(char **opts, struct fio_option *options, int num_opts)
851{
852 fio_options = options;
853 qsort(opts, num_opts, sizeof(char *), opt_cmp);
854 fio_options = NULL;
855}
856
Jens Axboeb4692822006-10-27 13:43:22 +0200857int parse_cmd_option(const char *opt, const char *val,
Jens Axboe07b32322010-03-05 09:48:44 +0100858 struct fio_option *options, void *data)
Jens Axboeb4692822006-10-27 13:43:22 +0200859{
860 struct fio_option *o;
861
Jens Axboe07b32322010-03-05 09:48:44 +0100862 o = find_option(options, opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200863 if (!o) {
Jens Axboe06b0be62011-10-04 10:31:10 +0200864 log_err("Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200865 return 1;
866 }
867
Jens Axboeb1508cf2006-11-02 09:12:40 +0100868 if (!handle_option(o, val, data))
869 return 0;
870
Jens Axboe06b0be62011-10-04 10:31:10 +0200871 log_err("fio: failed parsing %s=%s\n", opt, val);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100872 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200873}
874
Steven Langde890a12011-11-09 14:03:34 +0100875int parse_option(char *opt, const char *input,
876 struct fio_option *options, struct fio_option **o, void *data)
Jens Axboee1f36502006-10-27 10:54:08 +0200877{
Steven Langd0c814e2011-10-28 08:37:13 +0200878 char *post;
Jens Axboee1f36502006-10-27 10:54:08 +0200879
Steven Langd0c814e2011-10-28 08:37:13 +0200880 if (!opt) {
881 log_err("fio: failed parsing %s\n", input);
Steven Langde890a12011-11-09 14:03:34 +0100882 *o = NULL;
Jens Axboe38789b52010-03-03 09:23:20 +0100883 return 1;
Steven Langd0c814e2011-10-28 08:37:13 +0200884 }
Jens Axboee1f36502006-10-27 10:54:08 +0200885
Steven Langde890a12011-11-09 14:03:34 +0100886 *o = get_option(opt, options, &post);
887 if (!*o) {
888 if (post) {
889 int len = strlen(opt);
890 if (opt + len + 1 != post)
891 memmove(opt + len + 1, post, strlen(post));
892 opt[len] = '=';
893 }
Jens Axboee1f36502006-10-27 10:54:08 +0200894 return 1;
895 }
896
Steven Langde890a12011-11-09 14:03:34 +0100897 if (!handle_option(*o, post, data)) {
Jens Axboeb1508cf2006-11-02 09:12:40 +0100898 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200899 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100900
Steven Langd0c814e2011-10-28 08:37:13 +0200901 log_err("fio: failed parsing %s\n", input);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100902 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200903}
Jens Axboefd28ca42007-01-09 21:20:13 +0100904
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100905/*
906 * Option match, levenshtein distance. Handy for not quite remembering what
907 * the option name is.
908 */
909static int string_distance(const char *s1, const char *s2)
910{
911 unsigned int s1_len = strlen(s1);
912 unsigned int s2_len = strlen(s2);
913 unsigned int *p, *q, *r;
914 unsigned int i, j;
915
916 p = malloc(sizeof(unsigned int) * (s2_len + 1));
917 q = malloc(sizeof(unsigned int) * (s2_len + 1));
918
919 p[0] = 0;
920 for (i = 1; i <= s2_len; i++)
921 p[i] = p[i - 1] + 1;
922
923 for (i = 1; i <= s1_len; i++) {
924 q[0] = p[0] + 1;
925 for (j = 1; j <= s2_len; j++) {
926 unsigned int sub = p[j - 1];
927
928 if (s1[i - 1] != s2[j - 1])
929 sub++;
930
931 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
932 }
933 r = p;
934 p = q;
935 q = r;
936 }
937
938 i = p[s2_len];
939 free(p);
940 free(q);
941 return i;
942}
943
Jens Axboeafdf9352007-07-31 16:14:34 +0200944static struct fio_option *find_child(struct fio_option *options,
945 struct fio_option *o)
946{
947 struct fio_option *__o;
948
Jens Axboefdf28742007-07-31 23:06:09 +0200949 for (__o = options + 1; __o->name; __o++)
950 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200951 return __o;
952
953 return NULL;
954}
955
Jens Axboe323d9112008-03-01 15:12:14 +0100956static void __print_option(struct fio_option *o, struct fio_option *org,
957 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200958{
959 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100960 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200961
962 if (!o)
963 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200964 if (!org)
965 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100966
Jens Axboeafdf9352007-07-31 16:14:34 +0200967 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100968 depth = level;
969 while (depth--)
970 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200971
972 sprintf(p, "%s", o->name);
973
Jens Axboe28d7c362011-10-05 20:30:24 +0200974 log_info("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100975}
976
977static void print_option(struct fio_option *o)
978{
979 struct fio_option *parent;
980 struct fio_option *__o;
981 unsigned int printed;
982 unsigned int level;
983
984 __print_option(o, NULL, 0);
985 parent = o;
986 level = 0;
987 do {
988 level++;
989 printed = 0;
990
991 while ((__o = find_child(o, parent)) != NULL) {
992 __print_option(__o, o, level);
993 o = __o;
994 printed++;
995 }
996
997 parent = o;
998 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200999}
1000
Jens Axboe07b32322010-03-05 09:48:44 +01001001int show_cmd_help(struct fio_option *options, const char *name)
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001002{
1003 struct fio_option *o, *closest;
Jens Axboed091d092010-04-01 19:56:23 +02001004 unsigned int best_dist = -1U;
Jens Axboe29fc6af2007-01-09 21:22:02 +01001005 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +01001006 int show_all = 0;
1007
1008 if (!name || !strcmp(name, "all"))
1009 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +01001010
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001011 closest = NULL;
1012 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +01001013 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +01001014 int match = 0;
1015
Jens Axboe15ca1502008-04-07 09:26:02 +02001016 if (o->type == FIO_OPT_DEPRECATED)
1017 continue;
Jens Axboe07b32322010-03-05 09:48:44 +01001018 if (!exec_profile && o->prof_name)
1019 continue;
Jens Axboe15ca1502008-04-07 09:26:02 +02001020
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001021 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +01001022 if (!strcmp(name, o->name) ||
1023 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001024 match = 1;
1025 else {
1026 unsigned int dist;
1027
1028 dist = string_distance(name, o->name);
1029 if (dist < best_dist) {
1030 best_dist = dist;
1031 closest = o;
1032 }
1033 }
1034 }
Jens Axboefd28ca42007-01-09 21:20:13 +01001035
1036 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +01001037 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +01001038 if (match)
Jens Axboe28d7c362011-10-05 20:30:24 +02001039 log_info("%20s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +01001040 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +02001041 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +01001042 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +01001043 continue;
Jens Axboec167ded2007-03-14 13:02:53 +01001044 }
Jens Axboefd28ca42007-01-09 21:20:13 +01001045 }
1046
Jens Axboe70df2f12007-01-11 14:04:47 +01001047 if (!match)
1048 continue;
1049
Jens Axboe06b0be62011-10-04 10:31:10 +02001050 show_option_help(o, 0);
Jens Axboefd28ca42007-01-09 21:20:13 +01001051 }
1052
Jens Axboe29fc6af2007-01-09 21:22:02 +01001053 if (found)
1054 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +01001055
Jens Axboe28d7c362011-10-05 20:30:24 +02001056 log_err("No such command: %s", name);
Jens Axboed091d092010-04-01 19:56:23 +02001057
1058 /*
1059 * Only print an appropriately close option, one where the edit
1060 * distance isn't too big. Otherwise we get crazy matches.
1061 */
1062 if (closest && best_dist < 3) {
Jens Axboe28d7c362011-10-05 20:30:24 +02001063 log_info(" - showing closest match\n");
1064 log_info("%20s: %s\n", closest->name, closest->help);
Jens Axboe06b0be62011-10-04 10:31:10 +02001065 show_option_help(closest, 0);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001066 } else
Jens Axboe28d7c362011-10-05 20:30:24 +02001067 log_info("\n");
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001068
Jens Axboe29fc6af2007-01-09 21:22:02 +01001069 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +01001070}
Jens Axboeee738492007-01-10 11:23:16 +01001071
Jens Axboe13335dd2007-01-10 13:14:02 +01001072/*
1073 * Handle parsing of default parameters.
1074 */
Jens Axboeee738492007-01-10 11:23:16 +01001075void fill_default_options(void *data, struct fio_option *options)
1076{
Jens Axboe4945ba12007-01-11 14:36:56 +01001077 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +01001078
Jens Axboea3d741f2008-02-27 18:32:33 +01001079 dprint(FD_PARSE, "filling default options\n");
1080
Jens Axboe4945ba12007-01-11 14:36:56 +01001081 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +01001082 if (o->def)
1083 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +01001084}
Jens Axboe13335dd2007-01-10 13:14:02 +01001085
Jens Axboe9f988e22010-03-04 10:42:38 +01001086void option_init(struct fio_option *o)
1087{
1088 if (o->type == FIO_OPT_DEPRECATED)
1089 return;
1090 if (o->type == FIO_OPT_BOOL) {
1091 o->minval = 0;
1092 o->maxval = 1;
1093 }
Jens Axboed4fc2f02012-09-24 14:32:26 +02001094 if (o->type == FIO_OPT_INT) {
1095 if (!o->maxval)
1096 o->maxval = UINT_MAX;
1097 }
Yu-ju Hong83349192011-08-13 00:53:44 +02001098 if (o->type == FIO_OPT_FLOAT_LIST) {
1099 o->minfp = NAN;
1100 o->maxfp = NAN;
1101 }
Jens Axboe9f988e22010-03-04 10:42:38 +01001102 if (o->type == FIO_OPT_STR_SET && o->def) {
Jens Axboe06b0be62011-10-04 10:31:10 +02001103 log_err("Option %s: string set option with"
Jens Axboe9f988e22010-03-04 10:42:38 +01001104 " default will always be true\n", o->name);
1105 }
Jens Axboe06b0be62011-10-04 10:31:10 +02001106 if (!o->cb && (!o->off1 && !o->roff1))
1107 log_err("Option %s: neither cb nor offset given\n", o->name);
Jens Axboe5f6ddf12010-03-09 20:40:44 +01001108 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1109 o->type == FIO_OPT_STR_MULTI)
Jens Axboe9f988e22010-03-04 10:42:38 +01001110 return;
Jens Axboee2de69d2010-03-04 14:05:48 +01001111 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
1112 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
Jens Axboe06b0be62011-10-04 10:31:10 +02001113 log_err("Option %s: both cb and offset given\n", o->name);
Jens Axboe9f988e22010-03-04 10:42:38 +01001114 }
1115}
1116
Jens Axboe13335dd2007-01-10 13:14:02 +01001117/*
1118 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +01001119 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +01001120 */
1121void options_init(struct fio_option *options)
1122{
Jens Axboe4945ba12007-01-11 14:36:56 +01001123 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +01001124
Jens Axboea3d741f2008-02-27 18:32:33 +01001125 dprint(FD_PARSE, "init options\n");
1126
Jens Axboe9f988e22010-03-04 10:42:38 +01001127 for (o = &options[0]; o->name; o++)
1128 option_init(o);
Jens Axboe13335dd2007-01-10 13:14:02 +01001129}
Jens Axboe7e356b22011-10-14 10:55:16 +02001130
1131void options_free(struct fio_option *options, void *data)
1132{
1133 struct fio_option *o;
1134 char **ptr;
1135
1136 dprint(FD_PARSE, "free options\n");
1137
1138 for (o = &options[0]; o->name; o++) {
Steven Langde890a12011-11-09 14:03:34 +01001139 if (o->type != FIO_OPT_STR_STORE || !o->off1)
Jens Axboe7e356b22011-10-14 10:55:16 +02001140 continue;
1141
1142 ptr = td_var(data, o->off1);
1143 if (*ptr) {
1144 free(*ptr);
1145 *ptr = NULL;
1146 }
1147 }
1148}