blob: bf1dea908817a5cf8530d0d76aec603173a20f47 [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{
151 char *s = p + strlen(p) - 1;
152
153 while (isspace(*s) || iscntrl(*s))
154 s--;
155
156 *(s + 1) = '\0';
157}
158
Jens Axboe63f29372007-01-10 13:20:09 +0100159static int check_range_bytes(const char *str, long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200160{
161 char suffix;
162
Jens Axboe787f7e92006-11-06 13:26:29 +0100163 if (!strlen(str))
164 return 1;
165
Jens Axboecb2c86f2006-10-26 13:48:34 +0200166 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
167 *val *= get_mult_bytes(suffix);
168 return 0;
169 }
170
171 if (sscanf(str, "%lu", val) == 1)
172 return 0;
173
174 return 1;
175}
176
Jens Axboe63f29372007-01-10 13:20:09 +0100177static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200178{
Jens Axboe787f7e92006-11-06 13:26:29 +0100179 if (!strlen(p))
180 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200181 if (sscanf(p, "%u", val) == 1)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200182 return 0;
183
184 return 1;
185}
186
Jens Axboee1f36502006-10-27 10:54:08 +0200187static struct fio_option *find_option(struct fio_option *options,
188 const char *opt)
189{
Jens Axboe4945ba12007-01-11 14:36:56 +0100190 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200191
Jens Axboe4945ba12007-01-11 14:36:56 +0100192 for (o = &options[0]; o->name; o++) {
Jens Axboee1f36502006-10-27 10:54:08 +0200193 if (!strcmp(o->name, opt))
194 return o;
Jens Axboe03b74b32007-01-11 11:04:31 +0100195 else if (o->alias && !strcmp(o->alias, opt))
196 return o;
Jens Axboe33963c62006-10-27 11:33:01 +0200197 }
Jens Axboee1f36502006-10-27 10:54:08 +0200198
199 return NULL;
200}
201
Jens Axboe17abbe82006-11-06 11:23:10 +0100202#define val_store(ptr, val, off, data) \
203 do { \
204 ptr = td_var((data), (off)); \
205 *ptr = (val); \
206 } while (0)
207
Jens Axboef90eff52006-11-06 11:08:21 +0100208static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100209 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200210{
Jens Axboe63f29372007-01-10 13:20:09 +0100211 int il, *ilp;
212 long long ull, *ullp;
213 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200214 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200215 int ret = 0, is_time = 0;
216
Jens Axboe08e26e32006-11-21 13:15:10 +0100217 if (!ptr && o->type != FIO_OPT_STR_SET) {
218 fprintf(stderr, "Option %s requires an argument\n", o->name);
219 return 1;
220 }
221
Jens Axboee1f36502006-10-27 10:54:08 +0200222 switch (o->type) {
223 case FIO_OPT_STR: {
224 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100225 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100226 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100227 int i;
Jens Axboee1f36502006-10-27 10:54:08 +0200228
Jens Axboef0857372007-03-19 13:15:23 +0100229 posval_sort(o, posval);
230
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100231 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100232 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100233 if (!vp->ival || vp->ival[0] == '\0')
234 break;
Jens Axboe6612a272007-03-13 08:54:06 +0100235 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100236 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
237 ret = 0;
238 if (!o->off1)
239 break;
240 val_store(ilp, vp->oval, o->off1, data);
241 break;
242 }
243 }
244
245 if (ret)
246 show_option_values(o);
247 else if (fn)
248 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200249 break;
250 }
251 case FIO_OPT_STR_VAL_TIME:
252 is_time = 1;
Jens Axboe75e6f362006-11-03 08:17:09 +0100253 case FIO_OPT_STR_VAL:
254 case FIO_OPT_STR_VAL_INT: {
Jens Axboee1f36502006-10-27 10:54:08 +0200255 fio_opt_str_val_fn *fn = o->cb;
256
257 if (is_time)
258 ret = check_str_time(ptr, &ull);
259 else
260 ret = check_str_bytes(ptr, &ull);
261
262 if (ret)
263 break;
264
Jens Axboedb8e0162007-01-10 13:41:26 +0100265 if (o->maxval && ull > o->maxval) {
266 fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval);
267 return 1;
268 }
269 if (o->minval && ull < o->minval) {
270 fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval);
271 return 1;
272 }
Jens Axboee1f36502006-10-27 10:54:08 +0200273
274 if (fn)
275 ret = fn(data, &ull);
276 else {
Jens Axboe75e6f362006-11-03 08:17:09 +0100277 if (o->type == FIO_OPT_STR_VAL_INT) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100278 if (first)
279 val_store(ilp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100280 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100281 val_store(ilp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100282 } else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100283 if (first)
284 val_store(ullp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100285 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100286 val_store(ullp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100287 }
Jens Axboee1f36502006-10-27 10:54:08 +0200288 }
289 break;
290 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100291 case FIO_OPT_STR_STORE: {
292 fio_opt_str_fn *fn = o->cb;
293
Jens Axboee1f36502006-10-27 10:54:08 +0200294 cp = td_var(data, o->off1);
295 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100296 if (fn) {
297 ret = fn(data, ptr);
298 if (ret) {
299 free(*cp);
300 *cp = NULL;
301 }
302 }
Jens Axboee1f36502006-10-27 10:54:08 +0200303 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100304 }
Jens Axboee1f36502006-10-27 10:54:08 +0200305 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200306 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200307 char *p1, *p2;
308
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100309 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200310
311 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200312 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100313 p1 = strchr(tmp, ':');
314 if (!p1) {
315 ret = 1;
316 break;
317 }
Jens Axboee1f36502006-10-27 10:54:08 +0200318 }
319
320 p2 = p1 + 1;
321 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200322 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200323
324 ret = 1;
325 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
326 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200327 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100328 unsigned long foo = ul1;
329
330 ul1 = ul2;
331 ul2 = foo;
332 }
333
334 if (first) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100335 val_store(ilp, ul1, o->off1, data);
336 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200337 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100338 if (!more && o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100339 val_store(ilp, ul1, o->off3, data);
340 val_store(ilp, ul2, o->off4, data);
341 }
342 }
343
Jens Axboee1f36502006-10-27 10:54:08 +0200344 break;
345 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100346 case FIO_OPT_INT:
347 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200348 fio_opt_int_fn *fn = o->cb;
349
350 ret = check_int(ptr, &il);
351 if (ret)
352 break;
353
Jens Axboedb8e0162007-01-10 13:41:26 +0100354 if (o->maxval && il > (int) o->maxval) {
355 fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval);
356 return 1;
357 }
358 if (o->minval && il < o->minval) {
359 fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval);
360 return 1;
361 }
Jens Axboee1f36502006-10-27 10:54:08 +0200362
Jens Axboe76a43db2007-01-11 13:24:44 +0100363 if (o->neg)
364 il = !il;
365
Jens Axboee1f36502006-10-27 10:54:08 +0200366 if (fn)
367 ret = fn(data, &il);
368 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100369 if (first)
370 val_store(ilp, il, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100371 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100372 val_store(ilp, il, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200373 }
374 break;
375 }
376 case FIO_OPT_STR_SET: {
377 fio_opt_str_set_fn *fn = o->cb;
378
379 if (fn)
380 ret = fn(data);
381 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100382 if (first)
383 val_store(ilp, 1, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100384 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100385 val_store(ilp, 1, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200386 }
387 break;
388 }
389 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100390 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200391 ret = 1;
392 }
393
Jens Axboee1f36502006-10-27 10:54:08 +0200394 return ret;
395}
396
Jens Axboef90eff52006-11-06 11:08:21 +0100397static int handle_option(struct fio_option *o, const char *ptr, void *data)
398{
Jens Axboe92b586f2006-11-07 14:29:17 +0100399 const char *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100400 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100401
402 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100403 * See if we have a second set of parameters, hidden after a comma.
404 * Do this before parsing the first round, to check if we should
405 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100406 */
Joel Beckerad231bc2007-03-01 08:22:42 +0100407 if (ptr &&
408 (o->type != FIO_OPT_STR_STORE) &&
409 (o->type != FIO_OPT_STR)) {
Jens Axboe92b586f2006-11-07 14:29:17 +0100410 ptr2 = strchr(ptr, ',');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100411 if (!ptr2)
412 ptr2 = strchr(ptr, ':');
Jens Axboeb486f762007-03-15 09:15:12 +0100413 if (!ptr2)
414 ptr2 = strchr(ptr, '-');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100415 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100416
417 /*
418 * Don't return early if parsing the first option fails - if
419 * we are doing multiple arguments, we can allow the first one
420 * being empty.
421 */
422 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
423
Jens Axboef90eff52006-11-06 11:08:21 +0100424 if (!ptr2)
Jens Axboe787f7e92006-11-06 13:26:29 +0100425 return r1;
Jens Axboef90eff52006-11-06 11:08:21 +0100426
427 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100428 r2 = __handle_option(o, ptr2, data, 0, 0);
429
430 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100431}
432
Jens Axboeb4692822006-10-27 13:43:22 +0200433int parse_cmd_option(const char *opt, const char *val,
434 struct fio_option *options, void *data)
435{
436 struct fio_option *o;
437
438 o = find_option(options, opt);
439 if (!o) {
440 fprintf(stderr, "Bad option %s\n", opt);
441 return 1;
442 }
443
Jens Axboeb1508cf2006-11-02 09:12:40 +0100444 if (!handle_option(o, val, data))
445 return 0;
446
447 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
448 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200449}
450
Jens Axboee1f36502006-10-27 10:54:08 +0200451int parse_option(const char *opt, struct fio_option *options, void *data)
452{
Jens Axboeb4692822006-10-27 13:43:22 +0200453 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200454 char *pre, *post;
455 char tmp[64];
456
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100457 strncpy(tmp, opt, sizeof(tmp) - 1);
Jens Axboee1f36502006-10-27 10:54:08 +0200458
459 pre = strchr(tmp, '=');
460 if (pre) {
461 post = pre;
462 *pre = '\0';
463 pre = tmp;
464 post++;
465 o = find_option(options, pre);
466 } else {
467 o = find_option(options, tmp);
468 post = NULL;
469 }
470
471 if (!o) {
472 fprintf(stderr, "Bad option %s\n", tmp);
473 return 1;
474 }
475
Jens Axboeb1508cf2006-11-02 09:12:40 +0100476 if (!handle_option(o, post, data))
477 return 0;
478
479 fprintf(stderr, "fio: failed parsing %s\n", opt);
480 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200481}
Jens Axboefd28ca42007-01-09 21:20:13 +0100482
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100483/*
484 * Option match, levenshtein distance. Handy for not quite remembering what
485 * the option name is.
486 */
487static int string_distance(const char *s1, const char *s2)
488{
489 unsigned int s1_len = strlen(s1);
490 unsigned int s2_len = strlen(s2);
491 unsigned int *p, *q, *r;
492 unsigned int i, j;
493
494 p = malloc(sizeof(unsigned int) * (s2_len + 1));
495 q = malloc(sizeof(unsigned int) * (s2_len + 1));
496
497 p[0] = 0;
498 for (i = 1; i <= s2_len; i++)
499 p[i] = p[i - 1] + 1;
500
501 for (i = 1; i <= s1_len; i++) {
502 q[0] = p[0] + 1;
503 for (j = 1; j <= s2_len; j++) {
504 unsigned int sub = p[j - 1];
505
506 if (s1[i - 1] != s2[j - 1])
507 sub++;
508
509 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
510 }
511 r = p;
512 p = q;
513 q = r;
514 }
515
516 i = p[s2_len];
517 free(p);
518 free(q);
519 return i;
520}
521
522static void show_option_help(struct fio_option *o)
Jens Axboefd28ca42007-01-09 21:20:13 +0100523{
Jens Axboefd28ca42007-01-09 21:20:13 +0100524 const char *typehelp[] = {
525 "string (opt=bla)",
526 "string with possible k/m/g postfix (opt=4k)",
527 "string with range and postfix (opt=1k-4k)",
528 "string with time postfix (opt=10s)",
529 "string (opt=bla)",
530 "string with dual range (opt=1k-4k,4k-8k)",
531 "integer value (opt=100)",
Jens Axboe13335dd2007-01-10 13:14:02 +0100532 "boolean value (opt=1)",
Jens Axboefd28ca42007-01-09 21:20:13 +0100533 "no argument (opt)",
534 };
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100535
Jens Axboed2bb7fe2007-03-15 14:11:38 +0100536 if (o->alias)
537 printf("%20s: %s\n", "alias", o->alias);
538
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100539 printf("%20s: %s\n", "type", typehelp[o->type]);
540 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
541 show_option_range(o);
542 show_option_values(o);
543}
544
545int show_cmd_help(struct fio_option *options, const char *name)
546{
547 struct fio_option *o, *closest;
548 unsigned int best_dist;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100549 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100550 int show_all = 0;
551
552 if (!name || !strcmp(name, "all"))
553 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100554
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100555 closest = NULL;
556 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100557 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100558 int match = 0;
559
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100560 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100561 if (!strcmp(name, o->name) ||
562 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100563 match = 1;
564 else {
565 unsigned int dist;
566
567 dist = string_distance(name, o->name);
568 if (dist < best_dist) {
569 best_dist = dist;
570 closest = o;
571 }
572 }
573 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100574
575 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100576 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100577 if (match)
578 printf("%20s: %s\n", o->name, o->help);
579 if (show_all) {
580 printf("%-20s: %s\n", o->name, o->help);
Jens Axboe4945ba12007-01-11 14:36:56 +0100581 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100582 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100583 }
584
Jens Axboe70df2f12007-01-11 14:04:47 +0100585 if (!match)
586 continue;
587
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100588 show_option_help(o);
Jens Axboefd28ca42007-01-09 21:20:13 +0100589 }
590
Jens Axboe29fc6af2007-01-09 21:22:02 +0100591 if (found)
592 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100593
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100594 printf("No such command: %s", name);
595 if (closest) {
596 printf(" - showing closest match\n");
597 printf("%20s: %s\n", closest->name, closest->help);
598 show_option_help(closest);
599 } else
600 printf("\n");
601
Jens Axboe29fc6af2007-01-09 21:22:02 +0100602 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100603}
Jens Axboeee738492007-01-10 11:23:16 +0100604
Jens Axboe13335dd2007-01-10 13:14:02 +0100605/*
606 * Handle parsing of default parameters.
607 */
Jens Axboeee738492007-01-10 11:23:16 +0100608void fill_default_options(void *data, struct fio_option *options)
609{
Jens Axboe4945ba12007-01-11 14:36:56 +0100610 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100611
Jens Axboe4945ba12007-01-11 14:36:56 +0100612 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100613 if (o->def)
614 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100615}
Jens Axboe13335dd2007-01-10 13:14:02 +0100616
617/*
618 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100619 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100620 */
621void options_init(struct fio_option *options)
622{
Jens Axboe4945ba12007-01-11 14:36:56 +0100623 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100624
Jens Axboe4945ba12007-01-11 14:36:56 +0100625 for (o = &options[0]; o->name; o++) {
Jens Axboe13335dd2007-01-10 13:14:02 +0100626 if (o->type == FIO_OPT_BOOL) {
627 o->minval = 0;
628 o->maxval = 1;
629 }
Jens Axboe0f3e35e2007-03-20 14:28:34 +0100630 if (o->type == FIO_OPT_STR_SET && o->def)
631 fprintf(stderr, "Option %s: string set option with default will always be true\n", o->name);
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100632 if (!o->cb && !o->off1)
633 fprintf(stderr, "Option %s: neither cb nor offset given\n", o->name);
Jens Axboeaf52b342007-03-13 10:07:47 +0100634 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100635 continue;
Jens Axboe5b0a8882007-01-11 14:40:27 +0100636 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
637 fprintf(stderr, "Option %s: both cb and offset given\n", o->name);
Jens Axboe13335dd2007-01-10 13:14:02 +0100638 }
639}