blob: 9015b1d72bd5a527ef36ed0db12646ce01fcc945 [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>
11
12#include "parse.h"
13
Jens Axboef0857372007-03-19 13:15:23 +010014static int vp_cmp(const void *p1, const void *p2)
15{
16 const struct value_pair *vp1 = p1;
17 const struct value_pair *vp2 = p2;
18
19 return strlen(vp2->ival) - strlen(vp1->ival);
20}
21
22static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
23{
24 const struct value_pair *vp;
25 int entries;
26
27 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
28
29 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
30 vp = &o->posval[entries];
31 if (!vp->ival || vp->ival[0] == '\0')
32 break;
33
34 memcpy(&vpmap[entries], vp, sizeof(*vp));
35 }
36
37 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
38}
39
Jens Axboeb1ec1da2007-02-23 10:29:16 +010040static void show_option_range(struct fio_option *o)
41{
42 if (!o->minval && !o->maxval)
43 return;
44
Jens Axboeec1aee02007-02-26 13:18:47 +010045 printf("%20s: min=%d, max=%d\n", "range", o->minval, o->maxval);
Jens Axboeb1ec1da2007-02-23 10:29:16 +010046}
47
48static void show_option_values(struct fio_option *o)
49{
Jens Axboeb1ec1da2007-02-23 10:29:16 +010050 int i = 0;
51
52 do {
Jens Axboe78372132007-03-14 13:24:07 +010053 const struct value_pair *vp = &o->posval[i];
54
55 if (!vp->ival)
Jens Axboeb1ec1da2007-02-23 10:29:16 +010056 break;
57
Jens Axboe78372132007-03-14 13:24:07 +010058 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
59 if (vp->help)
60 printf(" %s", vp->help);
61 printf("\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010062 i++;
63 } while (i < PARSE_MAX_VP);
64
65 if (i)
66 printf("\n");
67}
68
Jens Axboecb2c86f2006-10-26 13:48:34 +020069static unsigned long get_mult_time(char c)
70{
71 switch (c) {
72 case 'm':
73 case 'M':
74 return 60;
75 case 'h':
76 case 'H':
77 return 60 * 60;
78 case 'd':
79 case 'D':
80 return 24 * 60 * 60;
81 default:
82 return 1;
83 }
84}
85
86static unsigned long get_mult_bytes(char c)
87{
88 switch (c) {
89 case 'k':
90 case 'K':
91 return 1024;
92 case 'm':
93 case 'M':
94 return 1024 * 1024;
95 case 'g':
96 case 'G':
97 return 1024 * 1024 * 1024;
Jens Axboef3502ba2007-02-14 01:27:09 +010098 case 'e':
99 case 'E':
100 return 1024 * 1024 * 1024 * 1024UL;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200101 default:
102 return 1;
103 }
104}
105
106/*
Jens Axboee1f36502006-10-27 10:54:08 +0200107 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200108 */
Jens Axboe63f29372007-01-10 13:20:09 +0100109static int str_to_decimal(const char *str, long long *val, int kilo)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200110{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200111 int len;
112
Jens Axboecb2c86f2006-10-26 13:48:34 +0200113 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100114 if (!len)
115 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200116
Jens Axboe675de852007-02-10 19:01:07 +0100117 *val = strtoll(str, NULL, 10);
Jens Axboecda866c2007-01-10 16:02:32 +0100118 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200119 return 1;
120
121 if (kilo)
122 *val *= get_mult_bytes(str[len - 1]);
123 else
124 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100125
Jens Axboecb2c86f2006-10-26 13:48:34 +0200126 return 0;
127}
128
Jens Axboe63f29372007-01-10 13:20:09 +0100129static int check_str_bytes(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200130{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200131 return str_to_decimal(p, val, 1);
132}
133
Jens Axboe63f29372007-01-10 13:20:09 +0100134static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200135{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200136 return str_to_decimal(p, val, 0);
137}
138
139void strip_blank_front(char **p)
140{
141 char *s = *p;
142
143 while (isspace(*s))
144 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200145
146 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200147}
148
149void strip_blank_end(char *p)
150{
Jens Axboe523bfad2007-04-04 11:04:06 +0200151 char *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200152
Jens Axboe523bfad2007-04-04 11:04:06 +0200153 s = strchr(p, ';');
154 if (s)
155 *s = '\0';
156 s = strchr(p, '#');
157 if (s)
158 *s = '\0';
159 if (s)
160 p = s;
161
162 s = p + strlen(p) - 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200163 while (isspace(*s) || iscntrl(*s))
164 s--;
165
166 *(s + 1) = '\0';
167}
168
Jens Axboe63f29372007-01-10 13:20:09 +0100169static int check_range_bytes(const char *str, long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200170{
171 char suffix;
172
Jens Axboe787f7e92006-11-06 13:26:29 +0100173 if (!strlen(str))
174 return 1;
175
Jens Axboecb2c86f2006-10-26 13:48:34 +0200176 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
177 *val *= get_mult_bytes(suffix);
178 return 0;
179 }
180
181 if (sscanf(str, "%lu", val) == 1)
182 return 0;
183
184 return 1;
185}
186
Jens Axboe63f29372007-01-10 13:20:09 +0100187static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200188{
Jens Axboe787f7e92006-11-06 13:26:29 +0100189 if (!strlen(p))
190 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200191 if (sscanf(p, "%u", val) == 1)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200192 return 0;
193
194 return 1;
195}
196
Jens Axboee1f36502006-10-27 10:54:08 +0200197static struct fio_option *find_option(struct fio_option *options,
198 const char *opt)
199{
Jens Axboe4945ba12007-01-11 14:36:56 +0100200 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200201
Jens Axboe4945ba12007-01-11 14:36:56 +0100202 for (o = &options[0]; o->name; o++) {
Jens Axboee1f36502006-10-27 10:54:08 +0200203 if (!strcmp(o->name, opt))
204 return o;
Jens Axboe03b74b32007-01-11 11:04:31 +0100205 else if (o->alias && !strcmp(o->alias, opt))
206 return o;
Jens Axboe33963c62006-10-27 11:33:01 +0200207 }
Jens Axboee1f36502006-10-27 10:54:08 +0200208
209 return NULL;
210}
211
Jens Axboe17abbe82006-11-06 11:23:10 +0100212#define val_store(ptr, val, off, data) \
213 do { \
214 ptr = td_var((data), (off)); \
215 *ptr = (val); \
216 } while (0)
217
Jens Axboef90eff52006-11-06 11:08:21 +0100218static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100219 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200220{
Jens Axboe63f29372007-01-10 13:20:09 +0100221 int il, *ilp;
222 long long ull, *ullp;
223 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200224 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200225 int ret = 0, is_time = 0;
226
Jens Axboe08e26e32006-11-21 13:15:10 +0100227 if (!ptr && o->type != FIO_OPT_STR_SET) {
228 fprintf(stderr, "Option %s requires an argument\n", o->name);
229 return 1;
230 }
231
Jens Axboee1f36502006-10-27 10:54:08 +0200232 switch (o->type) {
233 case FIO_OPT_STR: {
234 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100235 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100236 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100237 int i;
Jens Axboee1f36502006-10-27 10:54:08 +0200238
Jens Axboef0857372007-03-19 13:15:23 +0100239 posval_sort(o, posval);
240
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100241 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100242 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100243 if (!vp->ival || vp->ival[0] == '\0')
244 break;
Jens Axboe6612a272007-03-13 08:54:06 +0100245 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100246 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
247 ret = 0;
248 if (!o->off1)
249 break;
250 val_store(ilp, vp->oval, o->off1, data);
251 break;
252 }
253 }
254
255 if (ret)
256 show_option_values(o);
257 else if (fn)
258 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200259 break;
260 }
261 case FIO_OPT_STR_VAL_TIME:
262 is_time = 1;
Jens Axboe75e6f362006-11-03 08:17:09 +0100263 case FIO_OPT_STR_VAL:
264 case FIO_OPT_STR_VAL_INT: {
Jens Axboee1f36502006-10-27 10:54:08 +0200265 fio_opt_str_val_fn *fn = o->cb;
266
267 if (is_time)
268 ret = check_str_time(ptr, &ull);
269 else
270 ret = check_str_bytes(ptr, &ull);
271
272 if (ret)
273 break;
274
Jens Axboedb8e0162007-01-10 13:41:26 +0100275 if (o->maxval && ull > o->maxval) {
276 fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval);
277 return 1;
278 }
279 if (o->minval && ull < o->minval) {
280 fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval);
281 return 1;
282 }
Jens Axboee1f36502006-10-27 10:54:08 +0200283
284 if (fn)
285 ret = fn(data, &ull);
286 else {
Jens Axboe75e6f362006-11-03 08:17:09 +0100287 if (o->type == FIO_OPT_STR_VAL_INT) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100288 if (first)
289 val_store(ilp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100290 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100291 val_store(ilp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100292 } else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100293 if (first)
294 val_store(ullp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100295 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100296 val_store(ullp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100297 }
Jens Axboee1f36502006-10-27 10:54:08 +0200298 }
299 break;
300 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100301 case FIO_OPT_STR_STORE: {
302 fio_opt_str_fn *fn = o->cb;
303
Jens Axboee1f36502006-10-27 10:54:08 +0200304 cp = td_var(data, o->off1);
305 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100306 if (fn) {
307 ret = fn(data, ptr);
308 if (ret) {
309 free(*cp);
310 *cp = NULL;
311 }
312 }
Jens Axboee1f36502006-10-27 10:54:08 +0200313 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100314 }
Jens Axboee1f36502006-10-27 10:54:08 +0200315 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200316 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200317 char *p1, *p2;
318
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100319 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200320
321 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200322 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100323 p1 = strchr(tmp, ':');
324 if (!p1) {
325 ret = 1;
326 break;
327 }
Jens Axboee1f36502006-10-27 10:54:08 +0200328 }
329
330 p2 = p1 + 1;
331 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200332 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200333
334 ret = 1;
335 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
336 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200337 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100338 unsigned long foo = ul1;
339
340 ul1 = ul2;
341 ul2 = foo;
342 }
343
344 if (first) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100345 val_store(ilp, ul1, o->off1, data);
346 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200347 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100348 if (!more && o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100349 val_store(ilp, ul1, o->off3, data);
350 val_store(ilp, ul2, o->off4, data);
351 }
352 }
353
Jens Axboee1f36502006-10-27 10:54:08 +0200354 break;
355 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100356 case FIO_OPT_INT:
357 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200358 fio_opt_int_fn *fn = o->cb;
359
360 ret = check_int(ptr, &il);
361 if (ret)
362 break;
363
Jens Axboedb8e0162007-01-10 13:41:26 +0100364 if (o->maxval && il > (int) o->maxval) {
365 fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval);
366 return 1;
367 }
368 if (o->minval && il < o->minval) {
369 fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval);
370 return 1;
371 }
Jens Axboee1f36502006-10-27 10:54:08 +0200372
Jens Axboe76a43db2007-01-11 13:24:44 +0100373 if (o->neg)
374 il = !il;
375
Jens Axboee1f36502006-10-27 10:54:08 +0200376 if (fn)
377 ret = fn(data, &il);
378 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100379 if (first)
380 val_store(ilp, il, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100381 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100382 val_store(ilp, il, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200383 }
384 break;
385 }
386 case FIO_OPT_STR_SET: {
387 fio_opt_str_set_fn *fn = o->cb;
388
389 if (fn)
390 ret = fn(data);
391 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100392 if (first)
393 val_store(ilp, 1, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100394 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100395 val_store(ilp, 1, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200396 }
397 break;
398 }
399 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100400 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200401 ret = 1;
402 }
403
Jens Axboee1f36502006-10-27 10:54:08 +0200404 return ret;
405}
406
Jens Axboef90eff52006-11-06 11:08:21 +0100407static int handle_option(struct fio_option *o, const char *ptr, void *data)
408{
Jens Axboe92b586f2006-11-07 14:29:17 +0100409 const char *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100410 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100411
412 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100413 * See if we have a second set of parameters, hidden after a comma.
414 * Do this before parsing the first round, to check if we should
415 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100416 */
Joel Beckerad231bc2007-03-01 08:22:42 +0100417 if (ptr &&
418 (o->type != FIO_OPT_STR_STORE) &&
419 (o->type != FIO_OPT_STR)) {
Jens Axboe92b586f2006-11-07 14:29:17 +0100420 ptr2 = strchr(ptr, ',');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100421 if (!ptr2)
422 ptr2 = strchr(ptr, ':');
Jens Axboeb486f762007-03-15 09:15:12 +0100423 if (!ptr2)
424 ptr2 = strchr(ptr, '-');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100425 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100426
427 /*
428 * Don't return early if parsing the first option fails - if
429 * we are doing multiple arguments, we can allow the first one
430 * being empty.
431 */
432 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
433
Jens Axboef90eff52006-11-06 11:08:21 +0100434 if (!ptr2)
Jens Axboe787f7e92006-11-06 13:26:29 +0100435 return r1;
Jens Axboef90eff52006-11-06 11:08:21 +0100436
437 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100438 r2 = __handle_option(o, ptr2, data, 0, 0);
439
440 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100441}
442
Jens Axboeb4692822006-10-27 13:43:22 +0200443int parse_cmd_option(const char *opt, const char *val,
444 struct fio_option *options, void *data)
445{
446 struct fio_option *o;
447
448 o = find_option(options, opt);
449 if (!o) {
450 fprintf(stderr, "Bad option %s\n", opt);
451 return 1;
452 }
453
Jens Axboeb1508cf2006-11-02 09:12:40 +0100454 if (!handle_option(o, val, data))
455 return 0;
456
457 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
458 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200459}
460
Jens Axboee1f36502006-10-27 10:54:08 +0200461int parse_option(const char *opt, struct fio_option *options, void *data)
462{
Jens Axboeb4692822006-10-27 13:43:22 +0200463 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200464 char *pre, *post;
Jens Axboe0401bca2007-03-29 11:00:43 +0200465 char *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200466
Jens Axboe0401bca2007-03-29 11:00:43 +0200467 tmp = strdup(opt);
Jens Axboee1f36502006-10-27 10:54:08 +0200468
469 pre = strchr(tmp, '=');
470 if (pre) {
471 post = pre;
472 *pre = '\0';
473 pre = tmp;
474 post++;
475 o = find_option(options, pre);
476 } else {
477 o = find_option(options, tmp);
478 post = NULL;
479 }
480
481 if (!o) {
482 fprintf(stderr, "Bad option %s\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200483 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200484 return 1;
485 }
486
Jens Axboe0401bca2007-03-29 11:00:43 +0200487 if (!handle_option(o, post, data)) {
488 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100489 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200490 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100491
492 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200493 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100494 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200495}
Jens Axboefd28ca42007-01-09 21:20:13 +0100496
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100497/*
498 * Option match, levenshtein distance. Handy for not quite remembering what
499 * the option name is.
500 */
501static int string_distance(const char *s1, const char *s2)
502{
503 unsigned int s1_len = strlen(s1);
504 unsigned int s2_len = strlen(s2);
505 unsigned int *p, *q, *r;
506 unsigned int i, j;
507
508 p = malloc(sizeof(unsigned int) * (s2_len + 1));
509 q = malloc(sizeof(unsigned int) * (s2_len + 1));
510
511 p[0] = 0;
512 for (i = 1; i <= s2_len; i++)
513 p[i] = p[i - 1] + 1;
514
515 for (i = 1; i <= s1_len; i++) {
516 q[0] = p[0] + 1;
517 for (j = 1; j <= s2_len; j++) {
518 unsigned int sub = p[j - 1];
519
520 if (s1[i - 1] != s2[j - 1])
521 sub++;
522
523 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
524 }
525 r = p;
526 p = q;
527 q = r;
528 }
529
530 i = p[s2_len];
531 free(p);
532 free(q);
533 return i;
534}
535
536static void show_option_help(struct fio_option *o)
Jens Axboefd28ca42007-01-09 21:20:13 +0100537{
Jens Axboefd28ca42007-01-09 21:20:13 +0100538 const char *typehelp[] = {
539 "string (opt=bla)",
540 "string with possible k/m/g postfix (opt=4k)",
541 "string with range and postfix (opt=1k-4k)",
542 "string with time postfix (opt=10s)",
543 "string (opt=bla)",
544 "string with dual range (opt=1k-4k,4k-8k)",
545 "integer value (opt=100)",
Jens Axboe13335dd2007-01-10 13:14:02 +0100546 "boolean value (opt=1)",
Jens Axboefd28ca42007-01-09 21:20:13 +0100547 "no argument (opt)",
548 };
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100549
Jens Axboed2bb7fe2007-03-15 14:11:38 +0100550 if (o->alias)
551 printf("%20s: %s\n", "alias", o->alias);
552
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100553 printf("%20s: %s\n", "type", typehelp[o->type]);
554 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
555 show_option_range(o);
556 show_option_values(o);
557}
558
559int show_cmd_help(struct fio_option *options, const char *name)
560{
561 struct fio_option *o, *closest;
562 unsigned int best_dist;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100563 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100564 int show_all = 0;
565
566 if (!name || !strcmp(name, "all"))
567 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100568
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100569 closest = NULL;
570 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100571 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100572 int match = 0;
573
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100574 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100575 if (!strcmp(name, o->name) ||
576 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100577 match = 1;
578 else {
579 unsigned int dist;
580
581 dist = string_distance(name, o->name);
582 if (dist < best_dist) {
583 best_dist = dist;
584 closest = o;
585 }
586 }
587 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100588
589 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100590 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100591 if (match)
592 printf("%20s: %s\n", o->name, o->help);
593 if (show_all) {
594 printf("%-20s: %s\n", o->name, o->help);
Jens Axboe4945ba12007-01-11 14:36:56 +0100595 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100596 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100597 }
598
Jens Axboe70df2f12007-01-11 14:04:47 +0100599 if (!match)
600 continue;
601
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100602 show_option_help(o);
Jens Axboefd28ca42007-01-09 21:20:13 +0100603 }
604
Jens Axboe29fc6af2007-01-09 21:22:02 +0100605 if (found)
606 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100607
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100608 printf("No such command: %s", name);
609 if (closest) {
610 printf(" - showing closest match\n");
611 printf("%20s: %s\n", closest->name, closest->help);
612 show_option_help(closest);
613 } else
614 printf("\n");
615
Jens Axboe29fc6af2007-01-09 21:22:02 +0100616 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100617}
Jens Axboeee738492007-01-10 11:23:16 +0100618
Jens Axboe13335dd2007-01-10 13:14:02 +0100619/*
620 * Handle parsing of default parameters.
621 */
Jens Axboeee738492007-01-10 11:23:16 +0100622void fill_default_options(void *data, struct fio_option *options)
623{
Jens Axboe4945ba12007-01-11 14:36:56 +0100624 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100625
Jens Axboe4945ba12007-01-11 14:36:56 +0100626 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100627 if (o->def)
628 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100629}
Jens Axboe13335dd2007-01-10 13:14:02 +0100630
631/*
632 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100633 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100634 */
635void options_init(struct fio_option *options)
636{
Jens Axboe4945ba12007-01-11 14:36:56 +0100637 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100638
Jens Axboe4945ba12007-01-11 14:36:56 +0100639 for (o = &options[0]; o->name; o++) {
Jens Axboe13335dd2007-01-10 13:14:02 +0100640 if (o->type == FIO_OPT_BOOL) {
641 o->minval = 0;
642 o->maxval = 1;
643 }
Jens Axboe0f3e35e2007-03-20 14:28:34 +0100644 if (o->type == FIO_OPT_STR_SET && o->def)
645 fprintf(stderr, "Option %s: string set option with default will always be true\n", o->name);
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100646 if (!o->cb && !o->off1)
647 fprintf(stderr, "Option %s: neither cb nor offset given\n", o->name);
Jens Axboeaf52b342007-03-13 10:07:47 +0100648 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100649 continue;
Jens Axboe5b0a8882007-01-11 14:40:27 +0100650 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
651 fprintf(stderr, "Option %s: both cb and offset given\n", o->name);
Jens Axboe13335dd2007-01-10 13:14:02 +0100652 }
653}