blob: 239e37141c0fb5be468d9f841844c0ca4c64ba19 [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 Axboed447a8c2009-07-17 22:26:15 +020047static void show_option_range(struct fio_option *o, FILE *out)
Jens Axboeb1ec1da2007-02-23 10:29:16 +010048{
Yu-ju Hong83349192011-08-13 00:53:44 +020049 if (o->type == FIO_OPT_FLOAT_LIST){
50 if (isnan(o->minfp) && isnan(o->maxfp))
51 return;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010052
Yu-ju Hong83349192011-08-13 00:53:44 +020053 fprintf(out, "%20s: min=%f", "range", o->minfp);
54 if (!isnan(o->maxfp))
55 fprintf(out, ", max=%f", o->maxfp);
56 fprintf(out, "\n");
57 } else {
58 if (!o->minval && !o->maxval)
59 return;
60
61 fprintf(out, "%20s: min=%d", "range", o->minval);
62 if (o->maxval)
63 fprintf(out, ", max=%d", o->maxval);
64 fprintf(out, "\n");
65 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +010066}
67
68static void show_option_values(struct fio_option *o)
69{
Jens Axboea3073f42010-03-05 10:06:15 +010070 int i;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010071
Jens Axboea3073f42010-03-05 10:06:15 +010072 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboe78372132007-03-14 13:24:07 +010073 const struct value_pair *vp = &o->posval[i];
74
75 if (!vp->ival)
Jens Axboea3073f42010-03-05 10:06:15 +010076 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010077
Jens Axboe78372132007-03-14 13:24:07 +010078 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
79 if (vp->help)
80 printf(" %s", vp->help);
81 printf("\n");
Jens Axboea3073f42010-03-05 10:06:15 +010082 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +010083
84 if (i)
85 printf("\n");
86}
87
Jens Axboed447a8c2009-07-17 22:26:15 +020088static void show_option_help(struct fio_option *o, FILE *out)
89{
90 const char *typehelp[] = {
Jens Axboe07b32322010-03-05 09:48:44 +010091 "invalid",
Jens Axboed447a8c2009-07-17 22:26:15 +020092 "string (opt=bla)",
Jens Axboeae3fb6f2010-09-14 13:10:35 +020093 "string (opt=bla)",
Jens Axboed447a8c2009-07-17 22:26:15 +020094 "string with possible k/m/g postfix (opt=4k)",
95 "string with time postfix (opt=10s)",
96 "string (opt=bla)",
97 "string with dual range (opt=1k-4k,4k-8k)",
98 "integer value (opt=100)",
99 "boolean value (opt=1)",
Yu-ju Hong83349192011-08-13 00:53:44 +0200100 "list of floating point values separated by ':' (opt=5.9:7.8)",
Jens Axboed447a8c2009-07-17 22:26:15 +0200101 "no argument (opt)",
Jens Axboe07b32322010-03-05 09:48:44 +0100102 "deprecated",
Jens Axboed447a8c2009-07-17 22:26:15 +0200103 };
104
105 if (o->alias)
106 fprintf(out, "%20s: %s\n", "alias", o->alias);
107
108 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
109 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
Jens Axboe07b32322010-03-05 09:48:44 +0100110 if (o->prof_name)
111 fprintf(out, "%20s: only for profile '%s'\n", "valid", o->prof_name);
Jens Axboed447a8c2009-07-17 22:26:15 +0200112 show_option_range(o, stdout);
113 show_option_values(o);
114}
115
Jens Axboecb2c86f2006-10-26 13:48:34 +0200116static unsigned long get_mult_time(char c)
117{
118 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100119 case 'm':
120 case 'M':
121 return 60;
122 case 'h':
123 case 'H':
124 return 60 * 60;
125 case 'd':
126 case 'D':
127 return 24 * 60 * 60;
128 default:
129 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200130 }
131}
132
Jens Axboe7bb59102011-07-12 19:47:03 +0200133static unsigned long long __get_mult_bytes(const char *p, void *data,
134 int *percent)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200135{
Jens Axboed6978a32009-07-18 21:04:09 +0200136 unsigned int kb_base = fio_get_kb_base(data);
Jens Axboea639f0b2009-07-18 08:25:35 +0200137 unsigned long long ret = 1;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200138 unsigned int i, pow = 0, mult = kb_base;
139 char *c;
Jens Axboea639f0b2009-07-18 08:25:35 +0200140
Jens Axboe57fc29f2010-06-23 22:24:07 +0200141 if (!p)
142 return 1;
Jens Axboea639f0b2009-07-18 08:25:35 +0200143
Jens Axboe57fc29f2010-06-23 22:24:07 +0200144 c = strdup(p);
145
146 for (i = 0; i < strlen(c); i++)
147 c[i] = tolower(c[i]);
148
149 if (!strcmp("pib", c)) {
150 pow = 5;
151 mult = 1000;
152 } else if (!strcmp("tib", c)) {
153 pow = 4;
154 mult = 1000;
155 } else if (!strcmp("gib", c)) {
156 pow = 3;
157 mult = 1000;
158 } else if (!strcmp("mib", c)) {
159 pow = 2;
160 mult = 1000;
161 } else if (!strcmp("kib", c)) {
162 pow = 1;
163 mult = 1000;
164 } else if (!strcmp("p", c) || !strcmp("pb", c))
165 pow = 5;
166 else if (!strcmp("t", c) || !strcmp("tb", c))
167 pow = 4;
168 else if (!strcmp("g", c) || !strcmp("gb", c))
169 pow = 3;
170 else if (!strcmp("m", c) || !strcmp("mb", c))
171 pow = 2;
172 else if (!strcmp("k", c) || !strcmp("kb", c))
173 pow = 1;
Jens Axboe7bb59102011-07-12 19:47:03 +0200174 else if (!strcmp("%", c)) {
175 *percent = 1;
176 return ret;
177 }
Jens Axboe57fc29f2010-06-23 22:24:07 +0200178
179 while (pow--)
180 ret *= (unsigned long long) mult;
181
182 free(c);
Jens Axboea639f0b2009-07-18 08:25:35 +0200183 return ret;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200184}
185
Jens Axboe7bb59102011-07-12 19:47:03 +0200186static unsigned long long get_mult_bytes(const char *str, int len, void *data,
Jens Axboe6925dd32011-09-07 22:10:11 +0200187 int *percent)
Jens Axboe57fc29f2010-06-23 22:24:07 +0200188{
Jens Axboe55ed9632011-03-27 20:55:09 +0200189 const char *p = str;
Jens Axboeba4ddd62011-09-07 22:11:00 +0200190 int digit_seen = 0;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200191
Jens Axboe1d1c1872010-07-29 10:50:05 +0200192 if (len < 2)
Jens Axboe7bb59102011-07-12 19:47:03 +0200193 return __get_mult_bytes(str, data, percent);
Jens Axboe1d1c1872010-07-29 10:50:05 +0200194
Jens Axboe55ed9632011-03-27 20:55:09 +0200195 /*
Jens Axboee2979752011-08-31 12:55:27 -0600196 * Go forward until we hit a non-digit, or +/- sign
Jens Axboe55ed9632011-03-27 20:55:09 +0200197 */
198 while ((p - str) <= len) {
Jens Axboeba4ddd62011-09-07 22:11:00 +0200199 if (!isdigit((int) *p) &&
200 (((*p != '+') && (*p != '-')) || digit_seen))
Jens Axboe55ed9632011-03-27 20:55:09 +0200201 break;
Jens Axboe3c703d12011-09-21 09:27:24 +0200202 digit_seen |= isdigit((int) *p);
Jens Axboe55ed9632011-03-27 20:55:09 +0200203 p++;
204 }
205
Jens Axboe7bb59102011-07-12 19:47:03 +0200206 if (!isalpha((int) *p) && (*p != '%'))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200207 p = NULL;
208
Jens Axboe7bb59102011-07-12 19:47:03 +0200209 return __get_mult_bytes(p, data, percent);
Jens Axboe57fc29f2010-06-23 22:24:07 +0200210}
211
Jens Axboecb2c86f2006-10-26 13:48:34 +0200212/*
Yu-ju Hong83349192011-08-13 00:53:44 +0200213 * Convert string into a floating number. Return 1 for success and 0 otherwise.
214 */
215int str_to_float(const char *str, double *val)
216{
217 return (1 == sscanf(str, "%lf", val));
218}
219
220/*
Jens Axboee1f36502006-10-27 10:54:08 +0200221 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200222 */
Jens Axboe6925dd32011-09-07 22:10:11 +0200223int str_to_decimal(const char *str, long long *val, int kilo, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200224{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100225 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200226
Jens Axboecb2c86f2006-10-26 13:48:34 +0200227 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100228 if (!len)
229 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200230
Jens Axboeb347f9d2009-03-09 14:15:21 +0100231 if (strstr(str, "0x") || strstr(str, "0X"))
232 base = 16;
233 else
234 base = 10;
235
236 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100237 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200238 return 1;
239
Jens Axboe7bb59102011-07-12 19:47:03 +0200240 if (kilo) {
241 unsigned long long mult;
242 int perc = 0;
243
Jens Axboe6925dd32011-09-07 22:10:11 +0200244 mult = get_mult_bytes(str, len, data, &perc);
Jens Axboe7bb59102011-07-12 19:47:03 +0200245 if (perc)
246 *val = -1ULL - *val;
247 else
248 *val *= mult;
249 } else
Jens Axboecb2c86f2006-10-26 13:48:34 +0200250 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100251
Jens Axboecb2c86f2006-10-26 13:48:34 +0200252 return 0;
253}
254
Jens Axboe6925dd32011-09-07 22:10:11 +0200255static int check_str_bytes(const char *p, long long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200256{
Jens Axboe6925dd32011-09-07 22:10:11 +0200257 return str_to_decimal(p, val, 1, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200258}
259
Jens Axboe63f29372007-01-10 13:20:09 +0100260static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200261{
Jens Axboe6925dd32011-09-07 22:10:11 +0200262 return str_to_decimal(p, val, 0, NULL);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200263}
264
265void strip_blank_front(char **p)
266{
267 char *s = *p;
268
Jens Axboe76cd9372011-07-08 21:14:57 +0200269 while (isspace((int) *s))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200270 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200271
272 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200273}
274
275void strip_blank_end(char *p)
276{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100277 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200278
Jens Axboe523bfad2007-04-04 11:04:06 +0200279 s = strchr(p, ';');
280 if (s)
281 *s = '\0';
282 s = strchr(p, '#');
283 if (s)
284 *s = '\0';
285 if (s)
286 p = s;
287
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200288 s = p + strlen(p);
Jens Axboe76cd9372011-07-08 21:14:57 +0200289 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200290 s--;
291
292 *(s + 1) = '\0';
293}
294
Jens Axboed6978a32009-07-18 21:04:09 +0200295static int check_range_bytes(const char *str, long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200296{
Jens Axboe57fc29f2010-06-23 22:24:07 +0200297 long long __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200298
Jens Axboe6925dd32011-09-07 22:10:11 +0200299 if (!str_to_decimal(str, &__val, 1, data)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200300 *val = __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200301 return 0;
302 }
303
Jens Axboecb2c86f2006-10-26 13:48:34 +0200304 return 1;
305}
306
Jens Axboe63f29372007-01-10 13:20:09 +0100307static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200308{
Jens Axboe787f7e92006-11-06 13:26:29 +0100309 if (!strlen(p))
310 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200311 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200312 if (sscanf(p, "%x", val) == 1)
313 return 0;
314 } else {
315 if (sscanf(p, "%u", val) == 1)
316 return 0;
317 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200318
319 return 1;
320}
321
Jens Axboe808def72010-07-14 16:27:02 -0600322static int opt_len(const char *str)
323{
324 char *postfix;
325
326 postfix = strchr(str, ':');
327 if (!postfix)
328 return strlen(str);
329
330 return (int)(postfix - str);
331}
332
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100333#define val_store(ptr, val, off, or, data) \
Jens Axboe17abbe82006-11-06 11:23:10 +0100334 do { \
335 ptr = td_var((data), (off)); \
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100336 if ((or)) \
337 *ptr |= (val); \
338 else \
339 *ptr = (val); \
Jens Axboe17abbe82006-11-06 11:23:10 +0100340 } while (0)
341
Jens Axboef90eff52006-11-06 11:08:21 +0100342static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Yu-ju Hong83349192011-08-13 00:53:44 +0200343 int first, int more, int curr)
Jens Axboee1f36502006-10-27 10:54:08 +0200344{
Jens Axboe63f29372007-01-10 13:20:09 +0100345 int il, *ilp;
Yu-ju Hong83349192011-08-13 00:53:44 +0200346 double* flp;
Jens Axboe63f29372007-01-10 13:20:09 +0100347 long long ull, *ullp;
348 long ul1, ul2;
Yu-ju Hong83349192011-08-13 00:53:44 +0200349 double uf;
Jens Axboeb4692822006-10-27 13:43:22 +0200350 char **cp;
Jens Axboee42b01e2011-05-05 12:05:10 -0600351 int ret = 0, is_time = 0;
Jens Axboec44b1ff2011-08-30 20:43:53 -0600352 const struct value_pair *vp;
353 struct value_pair posval[PARSE_MAX_VP];
354 int i, all_skipped = 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200355
Jens Axboea3d741f2008-02-27 18:32:33 +0100356 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
357 o->type, ptr);
358
Jens Axboee3cedca2008-11-19 19:57:52 +0100359 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe08e26e32006-11-21 13:15:10 +0100360 fprintf(stderr, "Option %s requires an argument\n", o->name);
361 return 1;
362 }
363
Jens Axboee1f36502006-10-27 10:54:08 +0200364 switch (o->type) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100365 case FIO_OPT_STR:
366 case FIO_OPT_STR_MULTI: {
Jens Axboee1f36502006-10-27 10:54:08 +0200367 fio_opt_str_fn *fn = o->cb;
368
Jens Axboef0857372007-03-19 13:15:23 +0100369 posval_sort(o, posval);
370
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100371 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100372 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100373 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100374 if (!vp->ival || vp->ival[0] == '\0')
Jens Axboea3073f42010-03-05 10:06:15 +0100375 continue;
Jens Axboeae2ddba2010-03-10 12:49:03 +0100376 all_skipped = 0;
Jens Axboe808def72010-07-14 16:27:02 -0600377 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100378 ret = 0;
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100379 if (o->roff1) {
380 if (vp->or)
381 *(unsigned int *) o->roff1 |= vp->oval;
382 else
383 *(unsigned int *) o->roff1 = vp->oval;
384 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100385 if (!o->off1)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100386 continue;
387 val_store(ilp, vp->oval, o->off1, vp->or, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100388 }
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100389 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100390 }
391 }
392
Jens Axboeae2ddba2010-03-10 12:49:03 +0100393 if (ret && !all_skipped)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100394 show_option_values(o);
395 else if (fn)
396 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200397 break;
398 }
Jens Axboee42b01e2011-05-05 12:05:10 -0600399 case FIO_OPT_STR_VAL_TIME:
400 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100401 case FIO_OPT_INT:
Jens Axboee42b01e2011-05-05 12:05:10 -0600402 case FIO_OPT_STR_VAL: {
403 fio_opt_str_val_fn *fn = o->cb;
Jens Axboee1f36502006-10-27 10:54:08 +0200404
Jens Axboee42b01e2011-05-05 12:05:10 -0600405 if (is_time)
406 ret = check_str_time(ptr, &ull);
407 else
Jens Axboe6925dd32011-09-07 22:10:11 +0200408 ret = check_str_bytes(ptr, &ull, data);
Jens Axboee42b01e2011-05-05 12:05:10 -0600409
Jens Axboee1f36502006-10-27 10:54:08 +0200410 if (ret)
411 break;
412
Jens Axboedb8e0162007-01-10 13:41:26 +0100413 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100414 fprintf(stderr, "max value out of range: %lld"
415 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100416 return 1;
417 }
418 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100419 fprintf(stderr, "min value out of range: %lld"
420 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100421 return 1;
422 }
Jens Axboee1f36502006-10-27 10:54:08 +0200423
424 if (fn)
425 ret = fn(data, &ull);
426 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200427 if (o->type == FIO_OPT_INT) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100428 if (first) {
429 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100430 *(unsigned int *) o->roff1 = ull;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100431 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100432 val_store(ilp, ull, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100433 }
434 if (!more) {
435 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100436 *(unsigned int *) o->roff2 = ull;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100437 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100438 val_store(ilp, ull, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100439 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100440 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100441 if (first) {
442 if (o->roff1)
443 *(unsigned long long *) o->roff1 = ull;
444 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100445 val_store(ullp, ull, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100446 }
447 if (!more) {
448 if (o->roff2)
449 *(unsigned long long *) o->roff2 = ull;
450 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100451 val_store(ullp, ull, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100452 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100453 }
Jens Axboee1f36502006-10-27 10:54:08 +0200454 }
455 break;
456 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200457 case FIO_OPT_FLOAT_LIST: {
458
459 if (first) {
460 ul2 = 1;
461 ilp = td_var(data, o->off2);
462 *ilp = ul2;
463 }
464 if (curr >= o->maxlen) {
465 fprintf(stderr, "the list exceeding max length %d\n",
466 o->maxlen);
467 return 1;
468 }
469 if(!str_to_float(ptr, &uf)){
470 fprintf(stderr, "not a floating point value: %s\n",
471 ptr);
472 return 1;
473 }
474 if (!isnan(o->maxfp) && uf > o->maxfp) {
475 fprintf(stderr, "value out of range: %f"
476 " (range max: %f)\n", uf, o->maxfp);
477 return 1;
478 }
479 if (!isnan(o->minfp) && uf < o->minfp) {
480 fprintf(stderr, "value out of range: %f"
481 " (range min: %f)\n", uf, o->minfp);
482 return 1;
483 }
484
485 flp = td_var(data, o->off1);
486 flp[curr] = uf;
487
488 break;
489 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100490 case FIO_OPT_STR_STORE: {
491 fio_opt_str_fn *fn = o->cb;
492
Jens Axboece952ab2011-08-31 14:29:22 -0600493 posval_sort(o, posval);
494
Jens Axboe1c964ce2011-08-31 16:45:03 -0600495 if (!o->posval[0].ival) {
496 vp = NULL;
Jens Axboee2979752011-08-31 12:55:27 -0600497 goto match;
Jens Axboe1c964ce2011-08-31 16:45:03 -0600498 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100499
Jens Axboec44b1ff2011-08-30 20:43:53 -0600500 ret = 1;
501 for (i = 0; i < PARSE_MAX_VP; i++) {
502 vp = &posval[i];
503 if (!vp->ival || vp->ival[0] == '\0')
504 continue;
505 all_skipped = 0;
506 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
507 char *rest;
508
509 ret = 0;
510 if (vp->cb)
511 fn = vp->cb;
Jens Axboee2979752011-08-31 12:55:27 -0600512match:
Jens Axboec44b1ff2011-08-30 20:43:53 -0600513 if (o->roff1)
514 cp = (char **) o->roff1;
515 else
516 cp = td_var(data, o->off1);
517 *cp = strdup(ptr);
518 rest = strstr(*cp, ":");
519 if (rest) {
520 *rest = '\0';
521 ptr = rest + 1;
Jens Axboe1c964ce2011-08-31 16:45:03 -0600522 } else if (vp && vp->cb)
Jens Axboec44b1ff2011-08-30 20:43:53 -0600523 ptr = NULL;
524 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100525 }
526 }
Jens Axboec44b1ff2011-08-30 20:43:53 -0600527
528 if (ret && !all_skipped)
529 show_option_values(o);
530 else if (fn && ptr)
531 ret = fn(data, ptr);
532
Jens Axboee1f36502006-10-27 10:54:08 +0200533 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100534 }
Jens Axboee1f36502006-10-27 10:54:08 +0200535 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200536 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200537 char *p1, *p2;
538
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100539 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200540
Dave Engberg31d23f42011-07-23 21:07:13 +0200541 /* Handle bsrange with separate read,write values: */
542 p1 = strchr(tmp, ',');
543 if (p1)
544 *p1 = '\0';
545
Jens Axboeb765a372006-10-27 15:00:16 +0200546 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200547 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100548 p1 = strchr(tmp, ':');
549 if (!p1) {
550 ret = 1;
551 break;
552 }
Jens Axboee1f36502006-10-27 10:54:08 +0200553 }
554
555 p2 = p1 + 1;
556 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200557 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200558
559 ret = 1;
Jens Axboed6978a32009-07-18 21:04:09 +0200560 if (!check_range_bytes(p1, &ul1, data) &&
561 !check_range_bytes(p2, &ul2, data)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200562 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200563 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100564 unsigned long foo = ul1;
565
566 ul1 = ul2;
567 ul2 = foo;
568 }
569
570 if (first) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100571 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100572 *(unsigned int *) o->roff1 = ul1;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100573 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100574 val_store(ilp, ul1, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100575 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100576 *(unsigned int *) o->roff2 = ul2;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100577 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100578 val_store(ilp, ul2, o->off2, 0, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200579 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100580 if (o->roff3 && o->roff4) {
Jens Axboe7758d4f2010-03-18 16:50:31 +0100581 *(unsigned int *) o->roff3 = ul1;
582 *(unsigned int *) o->roff4 = ul2;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100583 } else if (o->off3 && o->off4) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100584 val_store(ilp, ul1, o->off3, 0, data);
585 val_store(ilp, ul2, o->off4, 0, data);
Jens Axboe17abbe82006-11-06 11:23:10 +0100586 }
587 }
588
Jens Axboee1f36502006-10-27 10:54:08 +0200589 break;
590 }
Jens Axboe74ba1802011-07-15 09:09:15 +0200591 case FIO_OPT_BOOL:
592 case FIO_OPT_STR_SET: {
Jens Axboee1f36502006-10-27 10:54:08 +0200593 fio_opt_int_fn *fn = o->cb;
594
Jens Axboe74ba1802011-07-15 09:09:15 +0200595 if (ptr)
596 ret = check_int(ptr, &il);
597 else if (o->type == FIO_OPT_BOOL)
598 ret = 1;
599 else
600 il = 1;
601
Jens Axboee1f36502006-10-27 10:54:08 +0200602 if (ret)
603 break;
604
Jens Axboedb8e0162007-01-10 13:41:26 +0100605 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100606 fprintf(stderr, "max value out of range: %d (%d max)\n",
607 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100608 return 1;
609 }
610 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100611 fprintf(stderr, "min value out of range: %d (%d min)\n",
612 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100613 return 1;
614 }
Jens Axboee1f36502006-10-27 10:54:08 +0200615
Jens Axboe76a43db2007-01-11 13:24:44 +0100616 if (o->neg)
617 il = !il;
618
Jens Axboee1f36502006-10-27 10:54:08 +0200619 if (fn)
620 ret = fn(data, &il);
621 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100622 if (first) {
623 if (o->roff1)
624 *(unsigned int *)o->roff1 = il;
625 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100626 val_store(ilp, il, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100627 }
628 if (!more) {
629 if (o->roff2)
630 *(unsigned int *) o->roff2 = il;
631 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100632 val_store(ilp, il, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100633 }
Jens Axboee1f36502006-10-27 10:54:08 +0200634 }
635 break;
636 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200637 case FIO_OPT_DEPRECATED:
638 fprintf(stdout, "Option %s is deprecated\n", o->name);
639 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200640 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100641 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200642 ret = 1;
643 }
644
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200645 if (ret)
646 return ret;
647
Jens Axboed447a8c2009-07-17 22:26:15 +0200648 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200649 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200650 if (ret) {
651 fprintf(stderr,"Correct format for offending option\n");
652 fprintf(stderr, "%20s: %s\n", o->name, o->help);
653 show_option_help(o, stderr);
654 }
655 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200656
Jens Axboee1f36502006-10-27 10:54:08 +0200657 return ret;
658}
659
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200660static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100661{
Jens Axboeb62bdf22010-03-10 12:36:17 +0100662 char *o_ptr, *ptr, *ptr2;
663 int ret, done;
Jens Axboef90eff52006-11-06 11:08:21 +0100664
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200665 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
666
Jens Axboeb62bdf22010-03-10 12:36:17 +0100667 o_ptr = ptr = NULL;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200668 if (__ptr)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100669 o_ptr = ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100670
Jens Axboef90eff52006-11-06 11:08:21 +0100671 /*
Jens Axboeb62bdf22010-03-10 12:36:17 +0100672 * See if we have another set of parameters, hidden after a comma.
673 * Do this before parsing this round, to check if we should
Jens Axboe787f7e92006-11-06 13:26:29 +0100674 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100675 */
Jens Axboeb62bdf22010-03-10 12:36:17 +0100676 done = 0;
677 ret = 1;
678 do {
679 int __ret;
680
681 ptr2 = NULL;
682 if (ptr &&
683 (o->type != FIO_OPT_STR_STORE) &&
Yu-ju Hong83349192011-08-13 00:53:44 +0200684 (o->type != FIO_OPT_STR) &&
685 (o->type != FIO_OPT_FLOAT_LIST)) {
Jens Axboeb62bdf22010-03-10 12:36:17 +0100686 ptr2 = strchr(ptr, ',');
687 if (ptr2 && *(ptr2 + 1) == '\0')
688 *ptr2 = '\0';
689 if (o->type != FIO_OPT_STR_MULTI) {
690 if (!ptr2)
691 ptr2 = strchr(ptr, ':');
692 if (!ptr2)
693 ptr2 = strchr(ptr, '-');
694 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200695 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
696 ptr2 = strchr(ptr, ':');
Jens Axboeb62bdf22010-03-10 12:36:17 +0100697 }
698
699 /*
700 * Don't return early if parsing the first option fails - if
701 * we are doing multiple arguments, we can allow the first one
702 * being empty.
703 */
Yu-ju Hong83349192011-08-13 00:53:44 +0200704 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
Jens Axboeb62bdf22010-03-10 12:36:17 +0100705 if (ret)
706 ret = __ret;
707
Jens Axboe0c9baf92007-01-11 15:59:26 +0100708 if (!ptr2)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100709 break;
Jens Axboe787f7e92006-11-06 13:26:29 +0100710
Jens Axboeb62bdf22010-03-10 12:36:17 +0100711 ptr = ptr2 + 1;
712 done++;
713 } while (1);
Jens Axboe787f7e92006-11-06 13:26:29 +0100714
Jens Axboeb62bdf22010-03-10 12:36:17 +0100715 if (o_ptr)
716 free(o_ptr);
717 return ret;
Jens Axboef90eff52006-11-06 11:08:21 +0100718}
719
Jens Axboe3b8b7132008-06-10 19:46:23 +0200720static struct fio_option *get_option(const char *opt,
Jens Axboe07b32322010-03-05 09:48:44 +0100721 struct fio_option *options, char **post)
Jens Axboe3b8b7132008-06-10 19:46:23 +0200722{
723 struct fio_option *o;
724 char *ret;
725
726 ret = strchr(opt, '=');
727 if (ret) {
728 *post = ret;
729 *ret = '\0';
730 ret = (char *) opt;
731 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100732 strip_blank_end(ret);
Jens Axboe07b32322010-03-05 09:48:44 +0100733 o = find_option(options, ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200734 } else {
Jens Axboe07b32322010-03-05 09:48:44 +0100735 o = find_option(options, opt);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200736 *post = NULL;
737 }
738
739 return o;
740}
741
742static int opt_cmp(const void *p1, const void *p2)
743{
744 struct fio_option *o1, *o2;
745 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100746 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200747
748 s1 = strdup(*((char **) p1));
749 s2 = strdup(*((char **) p2));
750
Jens Axboe07b32322010-03-05 09:48:44 +0100751 o1 = get_option(s1, fio_options, &foo);
752 o2 = get_option(s2, fio_options, &foo);
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200753
Jens Axboe8cdabc12008-11-19 12:31:43 +0100754 prio1 = prio2 = 0;
755 if (o1)
756 prio1 = o1->prio;
757 if (o2)
758 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200759
760 free(s1);
761 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100762 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200763}
764
765void sort_options(char **opts, struct fio_option *options, int num_opts)
766{
767 fio_options = options;
768 qsort(opts, num_opts, sizeof(char *), opt_cmp);
769 fio_options = NULL;
770}
771
Jens Axboeb4692822006-10-27 13:43:22 +0200772int parse_cmd_option(const char *opt, const char *val,
Jens Axboe07b32322010-03-05 09:48:44 +0100773 struct fio_option *options, void *data)
Jens Axboeb4692822006-10-27 13:43:22 +0200774{
775 struct fio_option *o;
776
Jens Axboe07b32322010-03-05 09:48:44 +0100777 o = find_option(options, opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200778 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100779 fprintf(stderr, "Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200780 return 1;
781 }
782
Jens Axboeb1508cf2006-11-02 09:12:40 +0100783 if (!handle_option(o, val, data))
784 return 0;
785
786 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
787 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200788}
789
Aaron Carroll88b5a392008-10-07 11:25:20 +0200790/*
791 * Return a copy of the input string with substrings of the form ${VARNAME}
792 * substituted with the value of the environment variable VARNAME. The
793 * substitution always occurs, even if VARNAME is empty or the corresponding
794 * environment variable undefined.
795 */
796static char *option_dup_subs(const char *opt)
797{
798 char out[OPT_LEN_MAX+1];
799 char in[OPT_LEN_MAX+1];
800 char *outptr = out;
801 char *inptr = in;
802 char *ch1, *ch2, *env;
803 ssize_t nchr = OPT_LEN_MAX;
804 size_t envlen;
805
Jens Axboea0741cb2010-03-05 12:46:37 +0100806 if (strlen(opt) + 1 > OPT_LEN_MAX) {
Jens Axboe38789b52010-03-03 09:23:20 +0100807 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
808 return NULL;
809 }
810
Aaron Carroll88b5a392008-10-07 11:25:20 +0200811 in[OPT_LEN_MAX] = '\0';
812 strncpy(in, opt, OPT_LEN_MAX);
813
814 while (*inptr && nchr > 0) {
815 if (inptr[0] == '$' && inptr[1] == '{') {
816 ch2 = strchr(inptr, '}');
817 if (ch2 && inptr+1 < ch2) {
818 ch1 = inptr+2;
819 inptr = ch2+1;
820 *ch2 = '\0';
821
822 env = getenv(ch1);
823 if (env) {
824 envlen = strlen(env);
825 if (envlen <= nchr) {
826 memcpy(outptr, env, envlen);
827 outptr += envlen;
828 nchr -= envlen;
829 }
830 }
831
832 continue;
833 }
834 }
835
836 *outptr++ = *inptr++;
837 --nchr;
838 }
839
840 *outptr = '\0';
841 return strdup(out);
842}
843
Jens Axboe07b32322010-03-05 09:48:44 +0100844int parse_option(const char *opt, struct fio_option *options, void *data)
Jens Axboee1f36502006-10-27 10:54:08 +0200845{
Jens Axboeb4692822006-10-27 13:43:22 +0200846 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200847 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200848
Aaron Carroll88b5a392008-10-07 11:25:20 +0200849 tmp = option_dup_subs(opt);
Jens Axboe38789b52010-03-03 09:23:20 +0100850 if (!tmp)
851 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200852
Jens Axboe07b32322010-03-05 09:48:44 +0100853 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200854 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100855 fprintf(stderr, "Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200856 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200857 return 1;
858 }
859
Jens Axboe0401bca2007-03-29 11:00:43 +0200860 if (!handle_option(o, post, data)) {
861 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100862 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200863 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100864
865 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200866 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100867 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200868}
Jens Axboefd28ca42007-01-09 21:20:13 +0100869
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100870/*
871 * Option match, levenshtein distance. Handy for not quite remembering what
872 * the option name is.
873 */
874static int string_distance(const char *s1, const char *s2)
875{
876 unsigned int s1_len = strlen(s1);
877 unsigned int s2_len = strlen(s2);
878 unsigned int *p, *q, *r;
879 unsigned int i, j;
880
881 p = malloc(sizeof(unsigned int) * (s2_len + 1));
882 q = malloc(sizeof(unsigned int) * (s2_len + 1));
883
884 p[0] = 0;
885 for (i = 1; i <= s2_len; i++)
886 p[i] = p[i - 1] + 1;
887
888 for (i = 1; i <= s1_len; i++) {
889 q[0] = p[0] + 1;
890 for (j = 1; j <= s2_len; j++) {
891 unsigned int sub = p[j - 1];
892
893 if (s1[i - 1] != s2[j - 1])
894 sub++;
895
896 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
897 }
898 r = p;
899 p = q;
900 q = r;
901 }
902
903 i = p[s2_len];
904 free(p);
905 free(q);
906 return i;
907}
908
Jens Axboeafdf9352007-07-31 16:14:34 +0200909static struct fio_option *find_child(struct fio_option *options,
910 struct fio_option *o)
911{
912 struct fio_option *__o;
913
Jens Axboefdf28742007-07-31 23:06:09 +0200914 for (__o = options + 1; __o->name; __o++)
915 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200916 return __o;
917
918 return NULL;
919}
920
Jens Axboe323d9112008-03-01 15:12:14 +0100921static void __print_option(struct fio_option *o, struct fio_option *org,
922 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200923{
924 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100925 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200926
927 if (!o)
928 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200929 if (!org)
930 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100931
Jens Axboeafdf9352007-07-31 16:14:34 +0200932 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100933 depth = level;
934 while (depth--)
935 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200936
937 sprintf(p, "%s", o->name);
938
939 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100940}
941
942static void print_option(struct fio_option *o)
943{
944 struct fio_option *parent;
945 struct fio_option *__o;
946 unsigned int printed;
947 unsigned int level;
948
949 __print_option(o, NULL, 0);
950 parent = o;
951 level = 0;
952 do {
953 level++;
954 printed = 0;
955
956 while ((__o = find_child(o, parent)) != NULL) {
957 __print_option(__o, o, level);
958 o = __o;
959 printed++;
960 }
961
962 parent = o;
963 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200964}
965
Jens Axboe07b32322010-03-05 09:48:44 +0100966int show_cmd_help(struct fio_option *options, const char *name)
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100967{
968 struct fio_option *o, *closest;
Jens Axboed091d092010-04-01 19:56:23 +0200969 unsigned int best_dist = -1U;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100970 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100971 int show_all = 0;
972
973 if (!name || !strcmp(name, "all"))
974 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100975
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100976 closest = NULL;
977 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100978 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100979 int match = 0;
980
Jens Axboe15ca1502008-04-07 09:26:02 +0200981 if (o->type == FIO_OPT_DEPRECATED)
982 continue;
Jens Axboe07b32322010-03-05 09:48:44 +0100983 if (!exec_profile && o->prof_name)
984 continue;
Jens Axboe15ca1502008-04-07 09:26:02 +0200985
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100986 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100987 if (!strcmp(name, o->name) ||
988 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100989 match = 1;
990 else {
991 unsigned int dist;
992
993 dist = string_distance(name, o->name);
994 if (dist < best_dist) {
995 best_dist = dist;
996 closest = o;
997 }
998 }
999 }
Jens Axboefd28ca42007-01-09 21:20:13 +01001000
1001 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +01001002 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +01001003 if (match)
Jens Axboe07b32322010-03-05 09:48:44 +01001004 printf("%20s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +01001005 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +02001006 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +01001007 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +01001008 continue;
Jens Axboec167ded2007-03-14 13:02:53 +01001009 }
Jens Axboefd28ca42007-01-09 21:20:13 +01001010 }
1011
Jens Axboe70df2f12007-01-11 14:04:47 +01001012 if (!match)
1013 continue;
1014
Jens Axboed447a8c2009-07-17 22:26:15 +02001015 show_option_help(o, stdout);
Jens Axboefd28ca42007-01-09 21:20:13 +01001016 }
1017
Jens Axboe29fc6af2007-01-09 21:22:02 +01001018 if (found)
1019 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +01001020
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001021 printf("No such command: %s", name);
Jens Axboed091d092010-04-01 19:56:23 +02001022
1023 /*
1024 * Only print an appropriately close option, one where the edit
1025 * distance isn't too big. Otherwise we get crazy matches.
1026 */
1027 if (closest && best_dist < 3) {
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001028 printf(" - showing closest match\n");
1029 printf("%20s: %s\n", closest->name, closest->help);
Jens Axboed447a8c2009-07-17 22:26:15 +02001030 show_option_help(closest, stdout);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001031 } else
1032 printf("\n");
1033
Jens Axboe29fc6af2007-01-09 21:22:02 +01001034 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +01001035}
Jens Axboeee738492007-01-10 11:23:16 +01001036
Jens Axboe13335dd2007-01-10 13:14:02 +01001037/*
1038 * Handle parsing of default parameters.
1039 */
Jens Axboeee738492007-01-10 11:23:16 +01001040void fill_default_options(void *data, struct fio_option *options)
1041{
Jens Axboe4945ba12007-01-11 14:36:56 +01001042 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +01001043
Jens Axboea3d741f2008-02-27 18:32:33 +01001044 dprint(FD_PARSE, "filling default options\n");
1045
Jens Axboe4945ba12007-01-11 14:36:56 +01001046 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +01001047 if (o->def)
1048 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +01001049}
Jens Axboe13335dd2007-01-10 13:14:02 +01001050
Jens Axboe9f988e22010-03-04 10:42:38 +01001051void option_init(struct fio_option *o)
1052{
1053 if (o->type == FIO_OPT_DEPRECATED)
1054 return;
1055 if (o->type == FIO_OPT_BOOL) {
1056 o->minval = 0;
1057 o->maxval = 1;
1058 }
Yu-ju Hong83349192011-08-13 00:53:44 +02001059 if (o->type == FIO_OPT_FLOAT_LIST) {
1060 o->minfp = NAN;
1061 o->maxfp = NAN;
1062 }
Jens Axboe9f988e22010-03-04 10:42:38 +01001063 if (o->type == FIO_OPT_STR_SET && o->def) {
1064 fprintf(stderr, "Option %s: string set option with"
1065 " default will always be true\n", o->name);
1066 }
Jens Axboee2de69d2010-03-04 14:05:48 +01001067 if (!o->cb && (!o->off1 && !o->roff1)) {
Jens Axboe9f988e22010-03-04 10:42:38 +01001068 fprintf(stderr, "Option %s: neither cb nor offset given\n",
1069 o->name);
1070 }
Jens Axboe5f6ddf12010-03-09 20:40:44 +01001071 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1072 o->type == FIO_OPT_STR_MULTI)
Jens Axboe9f988e22010-03-04 10:42:38 +01001073 return;
Jens Axboee2de69d2010-03-04 14:05:48 +01001074 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
1075 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
Jens Axboe9f988e22010-03-04 10:42:38 +01001076 fprintf(stderr, "Option %s: both cb and offset given\n",
1077 o->name);
1078 }
1079}
1080
Jens Axboe13335dd2007-01-10 13:14:02 +01001081/*
1082 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +01001083 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +01001084 */
1085void options_init(struct fio_option *options)
1086{
Jens Axboe4945ba12007-01-11 14:36:56 +01001087 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +01001088
Jens Axboea3d741f2008-02-27 18:32:33 +01001089 dprint(FD_PARSE, "init options\n");
1090
Jens Axboe9f988e22010-03-04 10:42:38 +01001091 for (o = &options[0]; o->name; o++)
1092 option_init(o);
Jens Axboe13335dd2007-01-10 13:14:02 +01001093}