blob: 55de4f0b8fa3643703294e640c39ee5b32819ae8 [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 Axboef20485a2011-09-07 21:21:07 +0200187 int *percent, int is_range)
Jens Axboe57fc29f2010-06-23 22:24:07 +0200188{
Jens Axboe55ed9632011-03-27 20:55:09 +0200189 const char *p = str;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200190
Jens Axboe1d1c1872010-07-29 10:50:05 +0200191 if (len < 2)
Jens Axboe7bb59102011-07-12 19:47:03 +0200192 return __get_mult_bytes(str, data, percent);
Jens Axboe1d1c1872010-07-29 10:50:05 +0200193
Jens Axboe55ed9632011-03-27 20:55:09 +0200194 /*
Jens Axboee2979752011-08-31 12:55:27 -0600195 * Go forward until we hit a non-digit, or +/- sign
Jens Axboe55ed9632011-03-27 20:55:09 +0200196 */
197 while ((p - str) <= len) {
Jens Axboef20485a2011-09-07 21:21:07 +0200198 if (!isdigit((int) *p) && (*p != '+'))
199 break;
200 if (!is_range && (*p != '-'))
Jens Axboe55ed9632011-03-27 20:55:09 +0200201 break;
202 p++;
203 }
204
Jens Axboe7bb59102011-07-12 19:47:03 +0200205 if (!isalpha((int) *p) && (*p != '%'))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200206 p = NULL;
207
Jens Axboe7bb59102011-07-12 19:47:03 +0200208 return __get_mult_bytes(p, data, percent);
Jens Axboe57fc29f2010-06-23 22:24:07 +0200209}
210
Jens Axboecb2c86f2006-10-26 13:48:34 +0200211/*
Yu-ju Hong83349192011-08-13 00:53:44 +0200212 * Convert string into a floating number. Return 1 for success and 0 otherwise.
213 */
214int str_to_float(const char *str, double *val)
215{
216 return (1 == sscanf(str, "%lf", val));
217}
218
219/*
Jens Axboee1f36502006-10-27 10:54:08 +0200220 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200221 */
Jens Axboef20485a2011-09-07 21:21:07 +0200222int str_to_decimal(const char *str, long long *val, int kilo, int is_range, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200223{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100224 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200225
Jens Axboecb2c86f2006-10-26 13:48:34 +0200226 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100227 if (!len)
228 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200229
Jens Axboeb347f9d2009-03-09 14:15:21 +0100230 if (strstr(str, "0x") || strstr(str, "0X"))
231 base = 16;
232 else
233 base = 10;
234
235 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100236 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200237 return 1;
238
Jens Axboe7bb59102011-07-12 19:47:03 +0200239 if (kilo) {
240 unsigned long long mult;
241 int perc = 0;
242
Jens Axboef20485a2011-09-07 21:21:07 +0200243 mult = get_mult_bytes(str, len, data, &perc, is_range);
Jens Axboe7bb59102011-07-12 19:47:03 +0200244 if (perc)
245 *val = -1ULL - *val;
246 else
247 *val *= mult;
248 } else
Jens Axboecb2c86f2006-10-26 13:48:34 +0200249 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100250
Jens Axboecb2c86f2006-10-26 13:48:34 +0200251 return 0;
252}
253
Jens Axboef20485a2011-09-07 21:21:07 +0200254static int check_str_bytes(const char *p, long long *val, int is_range, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200255{
Jens Axboef20485a2011-09-07 21:21:07 +0200256 return str_to_decimal(p, val, 1, is_range, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200257}
258
Jens Axboe63f29372007-01-10 13:20:09 +0100259static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200260{
Jens Axboef20485a2011-09-07 21:21:07 +0200261 return str_to_decimal(p, val, 0, 0, NULL);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200262}
263
264void strip_blank_front(char **p)
265{
266 char *s = *p;
267
Jens Axboe76cd9372011-07-08 21:14:57 +0200268 while (isspace((int) *s))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200269 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200270
271 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200272}
273
274void strip_blank_end(char *p)
275{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100276 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200277
Jens Axboe523bfad2007-04-04 11:04:06 +0200278 s = strchr(p, ';');
279 if (s)
280 *s = '\0';
281 s = strchr(p, '#');
282 if (s)
283 *s = '\0';
284 if (s)
285 p = s;
286
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200287 s = p + strlen(p);
Jens Axboe76cd9372011-07-08 21:14:57 +0200288 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200289 s--;
290
291 *(s + 1) = '\0';
292}
293
Jens Axboed6978a32009-07-18 21:04:09 +0200294static int check_range_bytes(const char *str, long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200295{
Jens Axboe57fc29f2010-06-23 22:24:07 +0200296 long long __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200297
Jens Axboef20485a2011-09-07 21:21:07 +0200298 if (!str_to_decimal(str, &__val, 1, 1, data)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200299 *val = __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200300 return 0;
301 }
302
Jens Axboecb2c86f2006-10-26 13:48:34 +0200303 return 1;
304}
305
Jens Axboe63f29372007-01-10 13:20:09 +0100306static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200307{
Jens Axboe787f7e92006-11-06 13:26:29 +0100308 if (!strlen(p))
309 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200310 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200311 if (sscanf(p, "%x", val) == 1)
312 return 0;
313 } else {
314 if (sscanf(p, "%u", val) == 1)
315 return 0;
316 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200317
318 return 1;
319}
320
Jens Axboe808def72010-07-14 16:27:02 -0600321static int opt_len(const char *str)
322{
323 char *postfix;
324
325 postfix = strchr(str, ':');
326 if (!postfix)
327 return strlen(str);
328
329 return (int)(postfix - str);
330}
331
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100332#define val_store(ptr, val, off, or, data) \
Jens Axboe17abbe82006-11-06 11:23:10 +0100333 do { \
334 ptr = td_var((data), (off)); \
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100335 if ((or)) \
336 *ptr |= (val); \
337 else \
338 *ptr = (val); \
Jens Axboe17abbe82006-11-06 11:23:10 +0100339 } while (0)
340
Jens Axboef90eff52006-11-06 11:08:21 +0100341static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Yu-ju Hong83349192011-08-13 00:53:44 +0200342 int first, int more, int curr)
Jens Axboee1f36502006-10-27 10:54:08 +0200343{
Jens Axboe63f29372007-01-10 13:20:09 +0100344 int il, *ilp;
Yu-ju Hong83349192011-08-13 00:53:44 +0200345 double* flp;
Jens Axboe63f29372007-01-10 13:20:09 +0100346 long long ull, *ullp;
347 long ul1, ul2;
Yu-ju Hong83349192011-08-13 00:53:44 +0200348 double uf;
Jens Axboeb4692822006-10-27 13:43:22 +0200349 char **cp;
Jens Axboee42b01e2011-05-05 12:05:10 -0600350 int ret = 0, is_time = 0;
Jens Axboec44b1ff2011-08-30 20:43:53 -0600351 const struct value_pair *vp;
352 struct value_pair posval[PARSE_MAX_VP];
353 int i, all_skipped = 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200354
Jens Axboea3d741f2008-02-27 18:32:33 +0100355 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
356 o->type, ptr);
357
Jens Axboee3cedca2008-11-19 19:57:52 +0100358 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe08e26e32006-11-21 13:15:10 +0100359 fprintf(stderr, "Option %s requires an argument\n", o->name);
360 return 1;
361 }
362
Jens Axboee1f36502006-10-27 10:54:08 +0200363 switch (o->type) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100364 case FIO_OPT_STR:
365 case FIO_OPT_STR_MULTI: {
Jens Axboee1f36502006-10-27 10:54:08 +0200366 fio_opt_str_fn *fn = o->cb;
367
Jens Axboef0857372007-03-19 13:15:23 +0100368 posval_sort(o, posval);
369
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100370 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100371 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100372 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100373 if (!vp->ival || vp->ival[0] == '\0')
Jens Axboea3073f42010-03-05 10:06:15 +0100374 continue;
Jens Axboeae2ddba2010-03-10 12:49:03 +0100375 all_skipped = 0;
Jens Axboe808def72010-07-14 16:27:02 -0600376 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100377 ret = 0;
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100378 if (o->roff1) {
379 if (vp->or)
380 *(unsigned int *) o->roff1 |= vp->oval;
381 else
382 *(unsigned int *) o->roff1 = vp->oval;
383 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100384 if (!o->off1)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100385 continue;
386 val_store(ilp, vp->oval, o->off1, vp->or, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100387 }
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100388 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100389 }
390 }
391
Jens Axboeae2ddba2010-03-10 12:49:03 +0100392 if (ret && !all_skipped)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100393 show_option_values(o);
394 else if (fn)
395 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200396 break;
397 }
Jens Axboee42b01e2011-05-05 12:05:10 -0600398 case FIO_OPT_STR_VAL_TIME:
399 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100400 case FIO_OPT_INT:
Jens Axboee42b01e2011-05-05 12:05:10 -0600401 case FIO_OPT_STR_VAL: {
402 fio_opt_str_val_fn *fn = o->cb;
Jens Axboee1f36502006-10-27 10:54:08 +0200403
Jens Axboee42b01e2011-05-05 12:05:10 -0600404 if (is_time)
405 ret = check_str_time(ptr, &ull);
406 else
Jens Axboef20485a2011-09-07 21:21:07 +0200407 ret = check_str_bytes(ptr, &ull, 0, data);
Jens Axboee42b01e2011-05-05 12:05:10 -0600408
Jens Axboee1f36502006-10-27 10:54:08 +0200409 if (ret)
410 break;
411
Jens Axboedb8e0162007-01-10 13:41:26 +0100412 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100413 fprintf(stderr, "max value out of range: %lld"
414 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100415 return 1;
416 }
417 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100418 fprintf(stderr, "min value out of range: %lld"
419 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100420 return 1;
421 }
Jens Axboee1f36502006-10-27 10:54:08 +0200422
423 if (fn)
424 ret = fn(data, &ull);
425 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200426 if (o->type == FIO_OPT_INT) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100427 if (first) {
428 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100429 *(unsigned int *) o->roff1 = ull;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100430 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100431 val_store(ilp, ull, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100432 }
433 if (!more) {
434 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100435 *(unsigned int *) o->roff2 = ull;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100436 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100437 val_store(ilp, ull, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100438 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100439 } else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100440 if (first) {
441 if (o->roff1)
442 *(unsigned long long *) o->roff1 = ull;
443 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100444 val_store(ullp, ull, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100445 }
446 if (!more) {
447 if (o->roff2)
448 *(unsigned long long *) o->roff2 = ull;
449 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100450 val_store(ullp, ull, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100451 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100452 }
Jens Axboee1f36502006-10-27 10:54:08 +0200453 }
454 break;
455 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200456 case FIO_OPT_FLOAT_LIST: {
457
458 if (first) {
459 ul2 = 1;
460 ilp = td_var(data, o->off2);
461 *ilp = ul2;
462 }
463 if (curr >= o->maxlen) {
464 fprintf(stderr, "the list exceeding max length %d\n",
465 o->maxlen);
466 return 1;
467 }
468 if(!str_to_float(ptr, &uf)){
469 fprintf(stderr, "not a floating point value: %s\n",
470 ptr);
471 return 1;
472 }
473 if (!isnan(o->maxfp) && uf > o->maxfp) {
474 fprintf(stderr, "value out of range: %f"
475 " (range max: %f)\n", uf, o->maxfp);
476 return 1;
477 }
478 if (!isnan(o->minfp) && uf < o->minfp) {
479 fprintf(stderr, "value out of range: %f"
480 " (range min: %f)\n", uf, o->minfp);
481 return 1;
482 }
483
484 flp = td_var(data, o->off1);
485 flp[curr] = uf;
486
487 break;
488 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100489 case FIO_OPT_STR_STORE: {
490 fio_opt_str_fn *fn = o->cb;
491
Jens Axboece952ab2011-08-31 14:29:22 -0600492 posval_sort(o, posval);
493
Jens Axboe1c964ce2011-08-31 16:45:03 -0600494 if (!o->posval[0].ival) {
495 vp = NULL;
Jens Axboee2979752011-08-31 12:55:27 -0600496 goto match;
Jens Axboe1c964ce2011-08-31 16:45:03 -0600497 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100498
Jens Axboec44b1ff2011-08-30 20:43:53 -0600499 ret = 1;
500 for (i = 0; i < PARSE_MAX_VP; i++) {
501 vp = &posval[i];
502 if (!vp->ival || vp->ival[0] == '\0')
503 continue;
504 all_skipped = 0;
505 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
506 char *rest;
507
508 ret = 0;
509 if (vp->cb)
510 fn = vp->cb;
Jens Axboee2979752011-08-31 12:55:27 -0600511match:
Jens Axboec44b1ff2011-08-30 20:43:53 -0600512 if (o->roff1)
513 cp = (char **) o->roff1;
514 else
515 cp = td_var(data, o->off1);
516 *cp = strdup(ptr);
517 rest = strstr(*cp, ":");
518 if (rest) {
519 *rest = '\0';
520 ptr = rest + 1;
Jens Axboe1c964ce2011-08-31 16:45:03 -0600521 } else if (vp && vp->cb)
Jens Axboec44b1ff2011-08-30 20:43:53 -0600522 ptr = NULL;
523 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100524 }
525 }
Jens Axboec44b1ff2011-08-30 20:43:53 -0600526
527 if (ret && !all_skipped)
528 show_option_values(o);
529 else if (fn && ptr)
530 ret = fn(data, ptr);
531
Jens Axboee1f36502006-10-27 10:54:08 +0200532 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100533 }
Jens Axboee1f36502006-10-27 10:54:08 +0200534 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200535 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200536 char *p1, *p2;
537
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100538 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200539
Dave Engberg31d23f42011-07-23 21:07:13 +0200540 /* Handle bsrange with separate read,write values: */
541 p1 = strchr(tmp, ',');
542 if (p1)
543 *p1 = '\0';
544
Jens Axboeb765a372006-10-27 15:00:16 +0200545 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200546 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100547 p1 = strchr(tmp, ':');
548 if (!p1) {
549 ret = 1;
550 break;
551 }
Jens Axboee1f36502006-10-27 10:54:08 +0200552 }
553
554 p2 = p1 + 1;
555 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200556 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200557
558 ret = 1;
Jens Axboed6978a32009-07-18 21:04:09 +0200559 if (!check_range_bytes(p1, &ul1, data) &&
560 !check_range_bytes(p2, &ul2, data)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200561 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200562 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100563 unsigned long foo = ul1;
564
565 ul1 = ul2;
566 ul2 = foo;
567 }
568
569 if (first) {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100570 if (o->roff1)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100571 *(unsigned int *) o->roff1 = ul1;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100572 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100573 val_store(ilp, ul1, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100574 if (o->roff2)
Jens Axboe7758d4f2010-03-18 16:50:31 +0100575 *(unsigned int *) o->roff2 = ul2;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100576 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100577 val_store(ilp, ul2, o->off2, 0, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200578 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100579 if (o->roff3 && o->roff4) {
Jens Axboe7758d4f2010-03-18 16:50:31 +0100580 *(unsigned int *) o->roff3 = ul1;
581 *(unsigned int *) o->roff4 = ul2;
Jens Axboe02c6aad2010-03-04 13:49:11 +0100582 } else if (o->off3 && o->off4) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100583 val_store(ilp, ul1, o->off3, 0, data);
584 val_store(ilp, ul2, o->off4, 0, data);
Jens Axboe17abbe82006-11-06 11:23:10 +0100585 }
586 }
587
Jens Axboee1f36502006-10-27 10:54:08 +0200588 break;
589 }
Jens Axboe74ba1802011-07-15 09:09:15 +0200590 case FIO_OPT_BOOL:
591 case FIO_OPT_STR_SET: {
Jens Axboee1f36502006-10-27 10:54:08 +0200592 fio_opt_int_fn *fn = o->cb;
593
Jens Axboe74ba1802011-07-15 09:09:15 +0200594 if (ptr)
595 ret = check_int(ptr, &il);
596 else if (o->type == FIO_OPT_BOOL)
597 ret = 1;
598 else
599 il = 1;
600
Jens Axboee1f36502006-10-27 10:54:08 +0200601 if (ret)
602 break;
603
Jens Axboedb8e0162007-01-10 13:41:26 +0100604 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100605 fprintf(stderr, "max value out of range: %d (%d max)\n",
606 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100607 return 1;
608 }
609 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100610 fprintf(stderr, "min value out of range: %d (%d min)\n",
611 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100612 return 1;
613 }
Jens Axboee1f36502006-10-27 10:54:08 +0200614
Jens Axboe76a43db2007-01-11 13:24:44 +0100615 if (o->neg)
616 il = !il;
617
Jens Axboee1f36502006-10-27 10:54:08 +0200618 if (fn)
619 ret = fn(data, &il);
620 else {
Jens Axboe02c6aad2010-03-04 13:49:11 +0100621 if (first) {
622 if (o->roff1)
623 *(unsigned int *)o->roff1 = il;
624 else
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100625 val_store(ilp, il, o->off1, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100626 }
627 if (!more) {
628 if (o->roff2)
629 *(unsigned int *) o->roff2 = il;
630 else if (o->off2)
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100631 val_store(ilp, il, o->off2, 0, data);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100632 }
Jens Axboee1f36502006-10-27 10:54:08 +0200633 }
634 break;
635 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200636 case FIO_OPT_DEPRECATED:
637 fprintf(stdout, "Option %s is deprecated\n", o->name);
638 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200639 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100640 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200641 ret = 1;
642 }
643
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200644 if (ret)
645 return ret;
646
Jens Axboed447a8c2009-07-17 22:26:15 +0200647 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200648 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200649 if (ret) {
650 fprintf(stderr,"Correct format for offending option\n");
651 fprintf(stderr, "%20s: %s\n", o->name, o->help);
652 show_option_help(o, stderr);
653 }
654 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200655
Jens Axboee1f36502006-10-27 10:54:08 +0200656 return ret;
657}
658
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200659static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100660{
Jens Axboeb62bdf22010-03-10 12:36:17 +0100661 char *o_ptr, *ptr, *ptr2;
662 int ret, done;
Jens Axboef90eff52006-11-06 11:08:21 +0100663
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200664 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
665
Jens Axboeb62bdf22010-03-10 12:36:17 +0100666 o_ptr = ptr = NULL;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200667 if (__ptr)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100668 o_ptr = ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100669
Jens Axboef90eff52006-11-06 11:08:21 +0100670 /*
Jens Axboeb62bdf22010-03-10 12:36:17 +0100671 * See if we have another set of parameters, hidden after a comma.
672 * Do this before parsing this round, to check if we should
Jens Axboe787f7e92006-11-06 13:26:29 +0100673 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100674 */
Jens Axboeb62bdf22010-03-10 12:36:17 +0100675 done = 0;
676 ret = 1;
677 do {
678 int __ret;
679
680 ptr2 = NULL;
681 if (ptr &&
682 (o->type != FIO_OPT_STR_STORE) &&
Yu-ju Hong83349192011-08-13 00:53:44 +0200683 (o->type != FIO_OPT_STR) &&
684 (o->type != FIO_OPT_FLOAT_LIST)) {
Jens Axboeb62bdf22010-03-10 12:36:17 +0100685 ptr2 = strchr(ptr, ',');
686 if (ptr2 && *(ptr2 + 1) == '\0')
687 *ptr2 = '\0';
688 if (o->type != FIO_OPT_STR_MULTI) {
689 if (!ptr2)
690 ptr2 = strchr(ptr, ':');
691 if (!ptr2)
692 ptr2 = strchr(ptr, '-');
693 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200694 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
695 ptr2 = strchr(ptr, ':');
Jens Axboeb62bdf22010-03-10 12:36:17 +0100696 }
697
698 /*
699 * Don't return early if parsing the first option fails - if
700 * we are doing multiple arguments, we can allow the first one
701 * being empty.
702 */
Yu-ju Hong83349192011-08-13 00:53:44 +0200703 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
Jens Axboeb62bdf22010-03-10 12:36:17 +0100704 if (ret)
705 ret = __ret;
706
Jens Axboe0c9baf92007-01-11 15:59:26 +0100707 if (!ptr2)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100708 break;
Jens Axboe787f7e92006-11-06 13:26:29 +0100709
Jens Axboeb62bdf22010-03-10 12:36:17 +0100710 ptr = ptr2 + 1;
711 done++;
712 } while (1);
Jens Axboe787f7e92006-11-06 13:26:29 +0100713
Jens Axboeb62bdf22010-03-10 12:36:17 +0100714 if (o_ptr)
715 free(o_ptr);
716 return ret;
Jens Axboef90eff52006-11-06 11:08:21 +0100717}
718
Jens Axboe3b8b7132008-06-10 19:46:23 +0200719static struct fio_option *get_option(const char *opt,
Jens Axboe07b32322010-03-05 09:48:44 +0100720 struct fio_option *options, char **post)
Jens Axboe3b8b7132008-06-10 19:46:23 +0200721{
722 struct fio_option *o;
723 char *ret;
724
725 ret = strchr(opt, '=');
726 if (ret) {
727 *post = ret;
728 *ret = '\0';
729 ret = (char *) opt;
730 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100731 strip_blank_end(ret);
Jens Axboe07b32322010-03-05 09:48:44 +0100732 o = find_option(options, ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200733 } else {
Jens Axboe07b32322010-03-05 09:48:44 +0100734 o = find_option(options, opt);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200735 *post = NULL;
736 }
737
738 return o;
739}
740
741static int opt_cmp(const void *p1, const void *p2)
742{
743 struct fio_option *o1, *o2;
744 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100745 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200746
747 s1 = strdup(*((char **) p1));
748 s2 = strdup(*((char **) p2));
749
Jens Axboe07b32322010-03-05 09:48:44 +0100750 o1 = get_option(s1, fio_options, &foo);
751 o2 = get_option(s2, fio_options, &foo);
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200752
Jens Axboe8cdabc12008-11-19 12:31:43 +0100753 prio1 = prio2 = 0;
754 if (o1)
755 prio1 = o1->prio;
756 if (o2)
757 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200758
759 free(s1);
760 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100761 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200762}
763
764void sort_options(char **opts, struct fio_option *options, int num_opts)
765{
766 fio_options = options;
767 qsort(opts, num_opts, sizeof(char *), opt_cmp);
768 fio_options = NULL;
769}
770
Jens Axboeb4692822006-10-27 13:43:22 +0200771int parse_cmd_option(const char *opt, const char *val,
Jens Axboe07b32322010-03-05 09:48:44 +0100772 struct fio_option *options, void *data)
Jens Axboeb4692822006-10-27 13:43:22 +0200773{
774 struct fio_option *o;
775
Jens Axboe07b32322010-03-05 09:48:44 +0100776 o = find_option(options, opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200777 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100778 fprintf(stderr, "Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200779 return 1;
780 }
781
Jens Axboeb1508cf2006-11-02 09:12:40 +0100782 if (!handle_option(o, val, data))
783 return 0;
784
785 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
786 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200787}
788
Aaron Carroll88b5a392008-10-07 11:25:20 +0200789/*
790 * Return a copy of the input string with substrings of the form ${VARNAME}
791 * substituted with the value of the environment variable VARNAME. The
792 * substitution always occurs, even if VARNAME is empty or the corresponding
793 * environment variable undefined.
794 */
795static char *option_dup_subs(const char *opt)
796{
797 char out[OPT_LEN_MAX+1];
798 char in[OPT_LEN_MAX+1];
799 char *outptr = out;
800 char *inptr = in;
801 char *ch1, *ch2, *env;
802 ssize_t nchr = OPT_LEN_MAX;
803 size_t envlen;
804
Jens Axboea0741cb2010-03-05 12:46:37 +0100805 if (strlen(opt) + 1 > OPT_LEN_MAX) {
Jens Axboe38789b52010-03-03 09:23:20 +0100806 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
807 return NULL;
808 }
809
Aaron Carroll88b5a392008-10-07 11:25:20 +0200810 in[OPT_LEN_MAX] = '\0';
811 strncpy(in, opt, OPT_LEN_MAX);
812
813 while (*inptr && nchr > 0) {
814 if (inptr[0] == '$' && inptr[1] == '{') {
815 ch2 = strchr(inptr, '}');
816 if (ch2 && inptr+1 < ch2) {
817 ch1 = inptr+2;
818 inptr = ch2+1;
819 *ch2 = '\0';
820
821 env = getenv(ch1);
822 if (env) {
823 envlen = strlen(env);
824 if (envlen <= nchr) {
825 memcpy(outptr, env, envlen);
826 outptr += envlen;
827 nchr -= envlen;
828 }
829 }
830
831 continue;
832 }
833 }
834
835 *outptr++ = *inptr++;
836 --nchr;
837 }
838
839 *outptr = '\0';
840 return strdup(out);
841}
842
Jens Axboe07b32322010-03-05 09:48:44 +0100843int parse_option(const char *opt, struct fio_option *options, void *data)
Jens Axboee1f36502006-10-27 10:54:08 +0200844{
Jens Axboeb4692822006-10-27 13:43:22 +0200845 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200846 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200847
Aaron Carroll88b5a392008-10-07 11:25:20 +0200848 tmp = option_dup_subs(opt);
Jens Axboe38789b52010-03-03 09:23:20 +0100849 if (!tmp)
850 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200851
Jens Axboe07b32322010-03-05 09:48:44 +0100852 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200853 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100854 fprintf(stderr, "Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200855 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200856 return 1;
857 }
858
Jens Axboe0401bca2007-03-29 11:00:43 +0200859 if (!handle_option(o, post, data)) {
860 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100861 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200862 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100863
864 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200865 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100866 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200867}
Jens Axboefd28ca42007-01-09 21:20:13 +0100868
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100869/*
870 * Option match, levenshtein distance. Handy for not quite remembering what
871 * the option name is.
872 */
873static int string_distance(const char *s1, const char *s2)
874{
875 unsigned int s1_len = strlen(s1);
876 unsigned int s2_len = strlen(s2);
877 unsigned int *p, *q, *r;
878 unsigned int i, j;
879
880 p = malloc(sizeof(unsigned int) * (s2_len + 1));
881 q = malloc(sizeof(unsigned int) * (s2_len + 1));
882
883 p[0] = 0;
884 for (i = 1; i <= s2_len; i++)
885 p[i] = p[i - 1] + 1;
886
887 for (i = 1; i <= s1_len; i++) {
888 q[0] = p[0] + 1;
889 for (j = 1; j <= s2_len; j++) {
890 unsigned int sub = p[j - 1];
891
892 if (s1[i - 1] != s2[j - 1])
893 sub++;
894
895 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
896 }
897 r = p;
898 p = q;
899 q = r;
900 }
901
902 i = p[s2_len];
903 free(p);
904 free(q);
905 return i;
906}
907
Jens Axboeafdf9352007-07-31 16:14:34 +0200908static struct fio_option *find_child(struct fio_option *options,
909 struct fio_option *o)
910{
911 struct fio_option *__o;
912
Jens Axboefdf28742007-07-31 23:06:09 +0200913 for (__o = options + 1; __o->name; __o++)
914 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200915 return __o;
916
917 return NULL;
918}
919
Jens Axboe323d9112008-03-01 15:12:14 +0100920static void __print_option(struct fio_option *o, struct fio_option *org,
921 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200922{
923 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100924 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200925
926 if (!o)
927 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200928 if (!org)
929 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100930
Jens Axboeafdf9352007-07-31 16:14:34 +0200931 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100932 depth = level;
933 while (depth--)
934 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200935
936 sprintf(p, "%s", o->name);
937
938 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100939}
940
941static void print_option(struct fio_option *o)
942{
943 struct fio_option *parent;
944 struct fio_option *__o;
945 unsigned int printed;
946 unsigned int level;
947
948 __print_option(o, NULL, 0);
949 parent = o;
950 level = 0;
951 do {
952 level++;
953 printed = 0;
954
955 while ((__o = find_child(o, parent)) != NULL) {
956 __print_option(__o, o, level);
957 o = __o;
958 printed++;
959 }
960
961 parent = o;
962 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200963}
964
Jens Axboe07b32322010-03-05 09:48:44 +0100965int show_cmd_help(struct fio_option *options, const char *name)
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100966{
967 struct fio_option *o, *closest;
Jens Axboed091d092010-04-01 19:56:23 +0200968 unsigned int best_dist = -1U;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100969 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100970 int show_all = 0;
971
972 if (!name || !strcmp(name, "all"))
973 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100974
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100975 closest = NULL;
976 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100977 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100978 int match = 0;
979
Jens Axboe15ca1502008-04-07 09:26:02 +0200980 if (o->type == FIO_OPT_DEPRECATED)
981 continue;
Jens Axboe07b32322010-03-05 09:48:44 +0100982 if (!exec_profile && o->prof_name)
983 continue;
Jens Axboe15ca1502008-04-07 09:26:02 +0200984
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100985 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100986 if (!strcmp(name, o->name) ||
987 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100988 match = 1;
989 else {
990 unsigned int dist;
991
992 dist = string_distance(name, o->name);
993 if (dist < best_dist) {
994 best_dist = dist;
995 closest = o;
996 }
997 }
998 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100999
1000 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +01001001 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +01001002 if (match)
Jens Axboe07b32322010-03-05 09:48:44 +01001003 printf("%20s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +01001004 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +02001005 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +01001006 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +01001007 continue;
Jens Axboec167ded2007-03-14 13:02:53 +01001008 }
Jens Axboefd28ca42007-01-09 21:20:13 +01001009 }
1010
Jens Axboe70df2f12007-01-11 14:04:47 +01001011 if (!match)
1012 continue;
1013
Jens Axboed447a8c2009-07-17 22:26:15 +02001014 show_option_help(o, stdout);
Jens Axboefd28ca42007-01-09 21:20:13 +01001015 }
1016
Jens Axboe29fc6af2007-01-09 21:22:02 +01001017 if (found)
1018 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +01001019
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001020 printf("No such command: %s", name);
Jens Axboed091d092010-04-01 19:56:23 +02001021
1022 /*
1023 * Only print an appropriately close option, one where the edit
1024 * distance isn't too big. Otherwise we get crazy matches.
1025 */
1026 if (closest && best_dist < 3) {
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001027 printf(" - showing closest match\n");
1028 printf("%20s: %s\n", closest->name, closest->help);
Jens Axboed447a8c2009-07-17 22:26:15 +02001029 show_option_help(closest, stdout);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001030 } else
1031 printf("\n");
1032
Jens Axboe29fc6af2007-01-09 21:22:02 +01001033 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +01001034}
Jens Axboeee738492007-01-10 11:23:16 +01001035
Jens Axboe13335dd2007-01-10 13:14:02 +01001036/*
1037 * Handle parsing of default parameters.
1038 */
Jens Axboeee738492007-01-10 11:23:16 +01001039void fill_default_options(void *data, struct fio_option *options)
1040{
Jens Axboe4945ba12007-01-11 14:36:56 +01001041 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +01001042
Jens Axboea3d741f2008-02-27 18:32:33 +01001043 dprint(FD_PARSE, "filling default options\n");
1044
Jens Axboe4945ba12007-01-11 14:36:56 +01001045 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +01001046 if (o->def)
1047 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +01001048}
Jens Axboe13335dd2007-01-10 13:14:02 +01001049
Jens Axboe9f988e22010-03-04 10:42:38 +01001050void option_init(struct fio_option *o)
1051{
1052 if (o->type == FIO_OPT_DEPRECATED)
1053 return;
1054 if (o->type == FIO_OPT_BOOL) {
1055 o->minval = 0;
1056 o->maxval = 1;
1057 }
Yu-ju Hong83349192011-08-13 00:53:44 +02001058 if (o->type == FIO_OPT_FLOAT_LIST) {
1059 o->minfp = NAN;
1060 o->maxfp = NAN;
1061 }
Jens Axboe9f988e22010-03-04 10:42:38 +01001062 if (o->type == FIO_OPT_STR_SET && o->def) {
1063 fprintf(stderr, "Option %s: string set option with"
1064 " default will always be true\n", o->name);
1065 }
Jens Axboee2de69d2010-03-04 14:05:48 +01001066 if (!o->cb && (!o->off1 && !o->roff1)) {
Jens Axboe9f988e22010-03-04 10:42:38 +01001067 fprintf(stderr, "Option %s: neither cb nor offset given\n",
1068 o->name);
1069 }
Jens Axboe5f6ddf12010-03-09 20:40:44 +01001070 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1071 o->type == FIO_OPT_STR_MULTI)
Jens Axboe9f988e22010-03-04 10:42:38 +01001072 return;
Jens Axboee2de69d2010-03-04 14:05:48 +01001073 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
1074 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
Jens Axboe9f988e22010-03-04 10:42:38 +01001075 fprintf(stderr, "Option %s: both cb and offset given\n",
1076 o->name);
1077 }
1078}
1079
Jens Axboe13335dd2007-01-10 13:14:02 +01001080/*
1081 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +01001082 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +01001083 */
1084void options_init(struct fio_option *options)
1085{
Jens Axboe4945ba12007-01-11 14:36:56 +01001086 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +01001087
Jens Axboea3d741f2008-02-27 18:32:33 +01001088 dprint(FD_PARSE, "init options\n");
1089
Jens Axboe9f988e22010-03-04 10:42:38 +01001090 for (o = &options[0]; o->name; o++)
1091 option_init(o);
Jens Axboe13335dd2007-01-10 13:14:02 +01001092}