blob: 57865b112e7a9118a9a4e6fc3dd0a60a0d66a9a8 [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++;
145}
146
147void strip_blank_end(char *p)
148{
149 char *s = p + strlen(p) - 1;
150
151 while (isspace(*s) || iscntrl(*s))
152 s--;
153
154 *(s + 1) = '\0';
155}
156
Jens Axboe63f29372007-01-10 13:20:09 +0100157static int check_range_bytes(const char *str, long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200158{
159 char suffix;
160
Jens Axboe787f7e92006-11-06 13:26:29 +0100161 if (!strlen(str))
162 return 1;
163
Jens Axboecb2c86f2006-10-26 13:48:34 +0200164 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
165 *val *= get_mult_bytes(suffix);
166 return 0;
167 }
168
169 if (sscanf(str, "%lu", val) == 1)
170 return 0;
171
172 return 1;
173}
174
Jens Axboe63f29372007-01-10 13:20:09 +0100175static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200176{
Jens Axboe787f7e92006-11-06 13:26:29 +0100177 if (!strlen(p))
178 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200179 if (sscanf(p, "%u", val) == 1)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200180 return 0;
181
182 return 1;
183}
184
Jens Axboee1f36502006-10-27 10:54:08 +0200185static struct fio_option *find_option(struct fio_option *options,
186 const char *opt)
187{
Jens Axboe4945ba12007-01-11 14:36:56 +0100188 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200189
Jens Axboe4945ba12007-01-11 14:36:56 +0100190 for (o = &options[0]; o->name; o++) {
Jens Axboee1f36502006-10-27 10:54:08 +0200191 if (!strcmp(o->name, opt))
192 return o;
Jens Axboe03b74b32007-01-11 11:04:31 +0100193 else if (o->alias && !strcmp(o->alias, opt))
194 return o;
Jens Axboe33963c62006-10-27 11:33:01 +0200195 }
Jens Axboee1f36502006-10-27 10:54:08 +0200196
197 return NULL;
198}
199
Jens Axboe17abbe82006-11-06 11:23:10 +0100200#define val_store(ptr, val, off, data) \
201 do { \
202 ptr = td_var((data), (off)); \
203 *ptr = (val); \
204 } while (0)
205
Jens Axboef90eff52006-11-06 11:08:21 +0100206static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100207 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200208{
Jens Axboe63f29372007-01-10 13:20:09 +0100209 int il, *ilp;
210 long long ull, *ullp;
211 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200212 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200213 int ret = 0, is_time = 0;
214
Jens Axboe08e26e32006-11-21 13:15:10 +0100215 if (!ptr && o->type != FIO_OPT_STR_SET) {
216 fprintf(stderr, "Option %s requires an argument\n", o->name);
217 return 1;
218 }
219
Jens Axboee1f36502006-10-27 10:54:08 +0200220 switch (o->type) {
221 case FIO_OPT_STR: {
222 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100223 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100224 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100225 int i;
Jens Axboee1f36502006-10-27 10:54:08 +0200226
Jens Axboef0857372007-03-19 13:15:23 +0100227 posval_sort(o, posval);
228
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100229 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100230 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100231 if (!vp->ival || vp->ival[0] == '\0')
232 break;
Jens Axboe6612a272007-03-13 08:54:06 +0100233 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100234 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
235 ret = 0;
236 if (!o->off1)
237 break;
238 val_store(ilp, vp->oval, o->off1, data);
239 break;
240 }
241 }
242
243 if (ret)
244 show_option_values(o);
245 else if (fn)
246 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200247 break;
248 }
249 case FIO_OPT_STR_VAL_TIME:
250 is_time = 1;
Jens Axboe75e6f362006-11-03 08:17:09 +0100251 case FIO_OPT_STR_VAL:
252 case FIO_OPT_STR_VAL_INT: {
Jens Axboee1f36502006-10-27 10:54:08 +0200253 fio_opt_str_val_fn *fn = o->cb;
254
255 if (is_time)
256 ret = check_str_time(ptr, &ull);
257 else
258 ret = check_str_bytes(ptr, &ull);
259
260 if (ret)
261 break;
262
Jens Axboedb8e0162007-01-10 13:41:26 +0100263 if (o->maxval && ull > o->maxval) {
264 fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval);
265 return 1;
266 }
267 if (o->minval && ull < o->minval) {
268 fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval);
269 return 1;
270 }
Jens Axboee1f36502006-10-27 10:54:08 +0200271
272 if (fn)
273 ret = fn(data, &ull);
274 else {
Jens Axboe75e6f362006-11-03 08:17:09 +0100275 if (o->type == FIO_OPT_STR_VAL_INT) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100276 if (first)
277 val_store(ilp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100278 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100279 val_store(ilp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100280 } else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100281 if (first)
282 val_store(ullp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100283 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100284 val_store(ullp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100285 }
Jens Axboee1f36502006-10-27 10:54:08 +0200286 }
287 break;
288 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100289 case FIO_OPT_STR_STORE: {
290 fio_opt_str_fn *fn = o->cb;
291
Jens Axboee1f36502006-10-27 10:54:08 +0200292 cp = td_var(data, o->off1);
293 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100294 if (fn) {
295 ret = fn(data, ptr);
296 if (ret) {
297 free(*cp);
298 *cp = NULL;
299 }
300 }
Jens Axboee1f36502006-10-27 10:54:08 +0200301 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100302 }
Jens Axboee1f36502006-10-27 10:54:08 +0200303 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200304 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200305 char *p1, *p2;
306
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100307 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200308
309 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200310 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100311 p1 = strchr(tmp, ':');
312 if (!p1) {
313 ret = 1;
314 break;
315 }
Jens Axboee1f36502006-10-27 10:54:08 +0200316 }
317
318 p2 = p1 + 1;
319 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200320 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200321
322 ret = 1;
323 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
324 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200325 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100326 unsigned long foo = ul1;
327
328 ul1 = ul2;
329 ul2 = foo;
330 }
331
332 if (first) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100333 val_store(ilp, ul1, o->off1, data);
334 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200335 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100336 if (!more && o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100337 val_store(ilp, ul1, o->off3, data);
338 val_store(ilp, ul2, o->off4, data);
339 }
340 }
341
Jens Axboee1f36502006-10-27 10:54:08 +0200342 break;
343 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100344 case FIO_OPT_INT:
345 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200346 fio_opt_int_fn *fn = o->cb;
347
348 ret = check_int(ptr, &il);
349 if (ret)
350 break;
351
Jens Axboedb8e0162007-01-10 13:41:26 +0100352 if (o->maxval && il > (int) o->maxval) {
353 fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval);
354 return 1;
355 }
356 if (o->minval && il < o->minval) {
357 fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval);
358 return 1;
359 }
Jens Axboee1f36502006-10-27 10:54:08 +0200360
Jens Axboe76a43db2007-01-11 13:24:44 +0100361 if (o->neg)
362 il = !il;
363
Jens Axboee1f36502006-10-27 10:54:08 +0200364 if (fn)
365 ret = fn(data, &il);
366 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100367 if (first)
368 val_store(ilp, il, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100369 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100370 val_store(ilp, il, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200371 }
372 break;
373 }
374 case FIO_OPT_STR_SET: {
375 fio_opt_str_set_fn *fn = o->cb;
376
377 if (fn)
378 ret = fn(data);
379 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100380 if (first)
381 val_store(ilp, 1, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100382 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100383 val_store(ilp, 1, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200384 }
385 break;
386 }
387 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100388 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200389 ret = 1;
390 }
391
Jens Axboee1f36502006-10-27 10:54:08 +0200392 return ret;
393}
394
Jens Axboef90eff52006-11-06 11:08:21 +0100395static int handle_option(struct fio_option *o, const char *ptr, void *data)
396{
Jens Axboe92b586f2006-11-07 14:29:17 +0100397 const char *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100398 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100399
400 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100401 * See if we have a second set of parameters, hidden after a comma.
402 * Do this before parsing the first round, to check if we should
403 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100404 */
Joel Beckerad231bc2007-03-01 08:22:42 +0100405 if (ptr &&
406 (o->type != FIO_OPT_STR_STORE) &&
407 (o->type != FIO_OPT_STR)) {
Jens Axboe92b586f2006-11-07 14:29:17 +0100408 ptr2 = strchr(ptr, ',');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100409 if (!ptr2)
410 ptr2 = strchr(ptr, ':');
Jens Axboeb486f762007-03-15 09:15:12 +0100411 if (!ptr2)
412 ptr2 = strchr(ptr, '-');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100413 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100414
415 /*
416 * Don't return early if parsing the first option fails - if
417 * we are doing multiple arguments, we can allow the first one
418 * being empty.
419 */
420 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
421
Jens Axboef90eff52006-11-06 11:08:21 +0100422 if (!ptr2)
Jens Axboe787f7e92006-11-06 13:26:29 +0100423 return r1;
Jens Axboef90eff52006-11-06 11:08:21 +0100424
425 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100426 r2 = __handle_option(o, ptr2, data, 0, 0);
427
428 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100429}
430
Jens Axboeb4692822006-10-27 13:43:22 +0200431int parse_cmd_option(const char *opt, const char *val,
432 struct fio_option *options, void *data)
433{
434 struct fio_option *o;
435
436 o = find_option(options, opt);
437 if (!o) {
438 fprintf(stderr, "Bad option %s\n", opt);
439 return 1;
440 }
441
Jens Axboeb1508cf2006-11-02 09:12:40 +0100442 if (!handle_option(o, val, data))
443 return 0;
444
445 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
446 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200447}
448
Jens Axboee1f36502006-10-27 10:54:08 +0200449int parse_option(const char *opt, struct fio_option *options, void *data)
450{
Jens Axboeb4692822006-10-27 13:43:22 +0200451 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200452 char *pre, *post;
453 char tmp[64];
454
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100455 strncpy(tmp, opt, sizeof(tmp) - 1);
Jens Axboee1f36502006-10-27 10:54:08 +0200456
457 pre = strchr(tmp, '=');
458 if (pre) {
459 post = pre;
460 *pre = '\0';
461 pre = tmp;
462 post++;
463 o = find_option(options, pre);
464 } else {
465 o = find_option(options, tmp);
466 post = NULL;
467 }
468
469 if (!o) {
470 fprintf(stderr, "Bad option %s\n", tmp);
471 return 1;
472 }
473
Jens Axboeb1508cf2006-11-02 09:12:40 +0100474 if (!handle_option(o, post, data))
475 return 0;
476
477 fprintf(stderr, "fio: failed parsing %s\n", opt);
478 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200479}
Jens Axboefd28ca42007-01-09 21:20:13 +0100480
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100481/*
482 * Option match, levenshtein distance. Handy for not quite remembering what
483 * the option name is.
484 */
485static int string_distance(const char *s1, const char *s2)
486{
487 unsigned int s1_len = strlen(s1);
488 unsigned int s2_len = strlen(s2);
489 unsigned int *p, *q, *r;
490 unsigned int i, j;
491
492 p = malloc(sizeof(unsigned int) * (s2_len + 1));
493 q = malloc(sizeof(unsigned int) * (s2_len + 1));
494
495 p[0] = 0;
496 for (i = 1; i <= s2_len; i++)
497 p[i] = p[i - 1] + 1;
498
499 for (i = 1; i <= s1_len; i++) {
500 q[0] = p[0] + 1;
501 for (j = 1; j <= s2_len; j++) {
502 unsigned int sub = p[j - 1];
503
504 if (s1[i - 1] != s2[j - 1])
505 sub++;
506
507 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
508 }
509 r = p;
510 p = q;
511 q = r;
512 }
513
514 i = p[s2_len];
515 free(p);
516 free(q);
517 return i;
518}
519
520static void show_option_help(struct fio_option *o)
Jens Axboefd28ca42007-01-09 21:20:13 +0100521{
Jens Axboefd28ca42007-01-09 21:20:13 +0100522 const char *typehelp[] = {
523 "string (opt=bla)",
524 "string with possible k/m/g postfix (opt=4k)",
525 "string with range and postfix (opt=1k-4k)",
526 "string with time postfix (opt=10s)",
527 "string (opt=bla)",
528 "string with dual range (opt=1k-4k,4k-8k)",
529 "integer value (opt=100)",
Jens Axboe13335dd2007-01-10 13:14:02 +0100530 "boolean value (opt=1)",
Jens Axboefd28ca42007-01-09 21:20:13 +0100531 "no argument (opt)",
532 };
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100533
Jens Axboed2bb7fe2007-03-15 14:11:38 +0100534 if (o->alias)
535 printf("%20s: %s\n", "alias", o->alias);
536
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100537 printf("%20s: %s\n", "type", typehelp[o->type]);
538 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
539 show_option_range(o);
540 show_option_values(o);
541}
542
543int show_cmd_help(struct fio_option *options, const char *name)
544{
545 struct fio_option *o, *closest;
546 unsigned int best_dist;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100547 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100548 int show_all = 0;
549
550 if (!name || !strcmp(name, "all"))
551 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100552
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100553 closest = NULL;
554 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100555 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100556 int match = 0;
557
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100558 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100559 if (!strcmp(name, o->name) ||
560 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100561 match = 1;
562 else {
563 unsigned int dist;
564
565 dist = string_distance(name, o->name);
566 if (dist < best_dist) {
567 best_dist = dist;
568 closest = o;
569 }
570 }
571 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100572
573 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100574 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100575 if (match)
576 printf("%20s: %s\n", o->name, o->help);
577 if (show_all) {
578 printf("%-20s: %s\n", o->name, o->help);
Jens Axboe4945ba12007-01-11 14:36:56 +0100579 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100580 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100581 }
582
Jens Axboe70df2f12007-01-11 14:04:47 +0100583 if (!match)
584 continue;
585
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100586 show_option_help(o);
Jens Axboefd28ca42007-01-09 21:20:13 +0100587 }
588
Jens Axboe29fc6af2007-01-09 21:22:02 +0100589 if (found)
590 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100591
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100592 printf("No such command: %s", name);
593 if (closest) {
594 printf(" - showing closest match\n");
595 printf("%20s: %s\n", closest->name, closest->help);
596 show_option_help(closest);
597 } else
598 printf("\n");
599
Jens Axboe29fc6af2007-01-09 21:22:02 +0100600 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100601}
Jens Axboeee738492007-01-10 11:23:16 +0100602
Jens Axboe13335dd2007-01-10 13:14:02 +0100603/*
604 * Handle parsing of default parameters.
605 */
Jens Axboeee738492007-01-10 11:23:16 +0100606void fill_default_options(void *data, struct fio_option *options)
607{
Jens Axboe4945ba12007-01-11 14:36:56 +0100608 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100609
Jens Axboe4945ba12007-01-11 14:36:56 +0100610 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100611 if (o->def)
612 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100613}
Jens Axboe13335dd2007-01-10 13:14:02 +0100614
615/*
616 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100617 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100618 */
619void options_init(struct fio_option *options)
620{
Jens Axboe4945ba12007-01-11 14:36:56 +0100621 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100622
Jens Axboe4945ba12007-01-11 14:36:56 +0100623 for (o = &options[0]; o->name; o++) {
Jens Axboe13335dd2007-01-10 13:14:02 +0100624 if (o->type == FIO_OPT_BOOL) {
625 o->minval = 0;
626 o->maxval = 1;
627 }
Jens Axboe0f3e35e2007-03-20 14:28:34 +0100628 if (o->type == FIO_OPT_STR_SET && o->def)
629 fprintf(stderr, "Option %s: string set option with default will always be true\n", o->name);
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100630 if (!o->cb && !o->off1)
631 fprintf(stderr, "Option %s: neither cb nor offset given\n", o->name);
Jens Axboeaf52b342007-03-13 10:07:47 +0100632 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100633 continue;
Jens Axboe5b0a8882007-01-11 14:40:27 +0100634 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
635 fprintf(stderr, "Option %s: both cb and offset given\n", o->name);
Jens Axboe13335dd2007-01-10 13:14:02 +0100636 }
637}