Commit | Line | Data |
---|---|---|
cb2c86fd JA |
1 | /* |
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" | |
a3d741fa | 13 | #include "debug.h" |
cb2c86fd | 14 | |
f085737f JA |
15 | static int vp_cmp(const void *p1, const void *p2) |
16 | { | |
17 | const struct value_pair *vp1 = p1; | |
18 | const struct value_pair *vp2 = p2; | |
19 | ||
20 | return strlen(vp2->ival) - strlen(vp1->ival); | |
21 | } | |
22 | ||
23 | static void posval_sort(struct fio_option *o, struct value_pair *vpmap) | |
24 | { | |
25 | const struct value_pair *vp; | |
26 | int entries; | |
27 | ||
28 | memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair)); | |
29 | ||
30 | for (entries = 0; entries < PARSE_MAX_VP; entries++) { | |
31 | vp = &o->posval[entries]; | |
32 | if (!vp->ival || vp->ival[0] == '\0') | |
33 | break; | |
34 | ||
35 | memcpy(&vpmap[entries], vp, sizeof(*vp)); | |
36 | } | |
37 | ||
38 | qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp); | |
39 | } | |
40 | ||
b1ec1da6 JA |
41 | static void show_option_range(struct fio_option *o) |
42 | { | |
43 | if (!o->minval && !o->maxval) | |
44 | return; | |
45 | ||
ec1aee01 | 46 | printf("%20s: min=%d, max=%d\n", "range", o->minval, o->maxval); |
b1ec1da6 JA |
47 | } |
48 | ||
49 | static void show_option_values(struct fio_option *o) | |
50 | { | |
b1ec1da6 JA |
51 | int i = 0; |
52 | ||
53 | do { | |
7837213b | 54 | const struct value_pair *vp = &o->posval[i]; |
b1ec1da6 | 55 | |
7837213b JA |
56 | if (!vp->ival) |
57 | break; | |
b1ec1da6 | 58 | |
7837213b JA |
59 | printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival); |
60 | if (vp->help) | |
61 | printf(" %s", vp->help); | |
62 | printf("\n"); | |
b1ec1da6 JA |
63 | i++; |
64 | } while (i < PARSE_MAX_VP); | |
65 | ||
66 | if (i) | |
67 | printf("\n"); | |
68 | } | |
69 | ||
cb2c86fd JA |
70 | static unsigned long get_mult_time(char c) |
71 | { | |
72 | switch (c) { | |
73 | case 'm': | |
74 | case 'M': | |
75 | return 60; | |
76 | case 'h': | |
77 | case 'H': | |
78 | return 60 * 60; | |
79 | case 'd': | |
80 | case 'D': | |
81 | return 24 * 60 * 60; | |
82 | default: | |
83 | return 1; | |
84 | } | |
85 | } | |
86 | ||
87 | static unsigned long get_mult_bytes(char c) | |
88 | { | |
89 | switch (c) { | |
90 | case 'k': | |
91 | case 'K': | |
92 | return 1024; | |
93 | case 'm': | |
94 | case 'M': | |
95 | return 1024 * 1024; | |
96 | case 'g': | |
97 | case 'G': | |
98 | return 1024 * 1024 * 1024; | |
f3502ba2 JA |
99 | case 'e': |
100 | case 'E': | |
101 | return 1024 * 1024 * 1024 * 1024UL; | |
cb2c86fd JA |
102 | default: |
103 | return 1; | |
104 | } | |
105 | } | |
106 | ||
107 | /* | |
e1f36503 | 108 | * convert string into decimal value, noting any size suffix |
cb2c86fd | 109 | */ |
564ca972 | 110 | int str_to_decimal(const char *str, long long *val, int kilo) |
cb2c86fd | 111 | { |
cb2c86fd JA |
112 | int len; |
113 | ||
cb2c86fd | 114 | len = strlen(str); |
f90eff5a JA |
115 | if (!len) |
116 | return 1; | |
cb2c86fd | 117 | |
675de85a | 118 | *val = strtoll(str, NULL, 10); |
cda866ca | 119 | if (*val == LONG_MAX && errno == ERANGE) |
cb2c86fd JA |
120 | return 1; |
121 | ||
122 | if (kilo) | |
123 | *val *= get_mult_bytes(str[len - 1]); | |
124 | else | |
125 | *val *= get_mult_time(str[len - 1]); | |
787f7e95 | 126 | |
cb2c86fd JA |
127 | return 0; |
128 | } | |
129 | ||
63f29372 | 130 | static int check_str_bytes(const char *p, long long *val) |
cb2c86fd | 131 | { |
cb2c86fd JA |
132 | return str_to_decimal(p, val, 1); |
133 | } | |
134 | ||
63f29372 | 135 | static int check_str_time(const char *p, long long *val) |
cb2c86fd | 136 | { |
cb2c86fd JA |
137 | return str_to_decimal(p, val, 0); |
138 | } | |
139 | ||
140 | void strip_blank_front(char **p) | |
141 | { | |
142 | char *s = *p; | |
143 | ||
144 | while (isspace(*s)) | |
145 | s++; | |
4d651dad JA |
146 | |
147 | *p = s; | |
cb2c86fd JA |
148 | } |
149 | ||
150 | void strip_blank_end(char *p) | |
151 | { | |
523bfadb JA |
152 | char *s; |
153 | ||
154 | s = strchr(p, ';'); | |
155 | if (s) | |
156 | *s = '\0'; | |
157 | s = strchr(p, '#'); | |
158 | if (s) | |
159 | *s = '\0'; | |
160 | if (s) | |
161 | p = s; | |
162 | ||
7f7e6e59 JA |
163 | s = p + strlen(p); |
164 | while ((isspace(*s) || iscntrl(*s)) && (s > p)) | |
cb2c86fd JA |
165 | s--; |
166 | ||
167 | *(s + 1) = '\0'; | |
168 | } | |
169 | ||
63f29372 | 170 | static int check_range_bytes(const char *str, long *val) |
cb2c86fd JA |
171 | { |
172 | char suffix; | |
173 | ||
787f7e95 JA |
174 | if (!strlen(str)) |
175 | return 1; | |
176 | ||
cb2c86fd JA |
177 | if (sscanf(str, "%lu%c", val, &suffix) == 2) { |
178 | *val *= get_mult_bytes(suffix); | |
179 | return 0; | |
180 | } | |
181 | ||
182 | if (sscanf(str, "%lu", val) == 1) | |
183 | return 0; | |
184 | ||
185 | return 1; | |
186 | } | |
187 | ||
63f29372 | 188 | static int check_int(const char *p, int *val) |
cb2c86fd | 189 | { |
787f7e95 JA |
190 | if (!strlen(p)) |
191 | return 1; | |
d78ee463 | 192 | if (strstr(p, "0x") || strstr(p, "0X")) { |
a61bdfd8 JA |
193 | if (sscanf(p, "%x", val) == 1) |
194 | return 0; | |
195 | } else { | |
196 | if (sscanf(p, "%u", val) == 1) | |
197 | return 0; | |
198 | } | |
cb2c86fd | 199 | |
e1f36503 JA |
200 | return 1; |
201 | } | |
cb2c86fd | 202 | |
e1f36503 JA |
203 | static struct fio_option *find_option(struct fio_option *options, |
204 | const char *opt) | |
205 | { | |
4945ba12 | 206 | struct fio_option *o; |
cb2c86fd | 207 | |
4945ba12 | 208 | for (o = &options[0]; o->name; o++) { |
e1f36503 JA |
209 | if (!strcmp(o->name, opt)) |
210 | return o; | |
03b74b3e JA |
211 | else if (o->alias && !strcmp(o->alias, opt)) |
212 | return o; | |
33963c6c | 213 | } |
cb2c86fd | 214 | |
e1f36503 | 215 | return NULL; |
cb2c86fd JA |
216 | } |
217 | ||
17abbe89 JA |
218 | #define val_store(ptr, val, off, data) \ |
219 | do { \ | |
220 | ptr = td_var((data), (off)); \ | |
221 | *ptr = (val); \ | |
222 | } while (0) | |
223 | ||
f90eff5a | 224 | static int __handle_option(struct fio_option *o, const char *ptr, void *data, |
787f7e95 | 225 | int first, int more) |
cb2c86fd | 226 | { |
63f29372 JA |
227 | int il, *ilp; |
228 | long long ull, *ullp; | |
229 | long ul1, ul2; | |
b4692828 | 230 | char **cp; |
e1f36503 | 231 | int ret = 0, is_time = 0; |
cb2c86fd | 232 | |
a3d741fa JA |
233 | dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name, |
234 | o->type, ptr); | |
235 | ||
08e26e35 JA |
236 | if (!ptr && o->type != FIO_OPT_STR_SET) { |
237 | fprintf(stderr, "Option %s requires an argument\n", o->name); | |
238 | return 1; | |
239 | } | |
240 | ||
e1f36503 JA |
241 | switch (o->type) { |
242 | case FIO_OPT_STR: { | |
243 | fio_opt_str_fn *fn = o->cb; | |
b1ec1da6 | 244 | const struct value_pair *vp; |
f085737f | 245 | struct value_pair posval[PARSE_MAX_VP]; |
b1ec1da6 JA |
246 | int i; |
247 | ||
f085737f JA |
248 | posval_sort(o, posval); |
249 | ||
b1ec1da6 | 250 | for (i = 0; i < PARSE_MAX_VP; i++) { |
f085737f | 251 | vp = &posval[i]; |
b1ec1da6 JA |
252 | if (!vp->ival || vp->ival[0] == '\0') |
253 | break; | |
6612a27b | 254 | ret = 1; |
b1ec1da6 JA |
255 | if (!strncmp(vp->ival, ptr, strlen(vp->ival))) { |
256 | ret = 0; | |
257 | if (!o->off1) | |
258 | break; | |
259 | val_store(ilp, vp->oval, o->off1, data); | |
260 | break; | |
261 | } | |
262 | } | |
cb2c86fd | 263 | |
b1ec1da6 JA |
264 | if (ret) |
265 | show_option_values(o); | |
266 | else if (fn) | |
267 | ret = fn(data, ptr); | |
e1f36503 JA |
268 | break; |
269 | } | |
270 | case FIO_OPT_STR_VAL_TIME: | |
271 | is_time = 1; | |
75e6f36f JA |
272 | case FIO_OPT_STR_VAL: |
273 | case FIO_OPT_STR_VAL_INT: { | |
e1f36503 JA |
274 | fio_opt_str_val_fn *fn = o->cb; |
275 | ||
276 | if (is_time) | |
277 | ret = check_str_time(ptr, &ull); | |
278 | else | |
279 | ret = check_str_bytes(ptr, &ull); | |
280 | ||
281 | if (ret) | |
282 | break; | |
283 | ||
db8e0165 JA |
284 | if (o->maxval && ull > o->maxval) { |
285 | fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval); | |
286 | return 1; | |
287 | } | |
288 | if (o->minval && ull < o->minval) { | |
289 | fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval); | |
290 | return 1; | |
291 | } | |
e1f36503 JA |
292 | |
293 | if (fn) | |
294 | ret = fn(data, &ull); | |
295 | else { | |
75e6f36f | 296 | if (o->type == FIO_OPT_STR_VAL_INT) { |
17abbe89 JA |
297 | if (first) |
298 | val_store(ilp, ull, o->off1, data); | |
787f7e95 | 299 | if (!more && o->off2) |
17abbe89 | 300 | val_store(ilp, ull, o->off2, data); |
75e6f36f | 301 | } else { |
17abbe89 JA |
302 | if (first) |
303 | val_store(ullp, ull, o->off1, data); | |
787f7e95 | 304 | if (!more && o->off2) |
17abbe89 | 305 | val_store(ullp, ull, o->off2, data); |
75e6f36f | 306 | } |
e1f36503 JA |
307 | } |
308 | break; | |
309 | } | |
af52b345 JA |
310 | case FIO_OPT_STR_STORE: { |
311 | fio_opt_str_fn *fn = o->cb; | |
312 | ||
e1f36503 JA |
313 | cp = td_var(data, o->off1); |
314 | *cp = strdup(ptr); | |
af52b345 JA |
315 | if (fn) { |
316 | ret = fn(data, ptr); | |
317 | if (ret) { | |
318 | free(*cp); | |
319 | *cp = NULL; | |
320 | } | |
321 | } | |
e1f36503 | 322 | break; |
af52b345 | 323 | } |
e1f36503 | 324 | case FIO_OPT_RANGE: { |
b765a372 | 325 | char tmp[128]; |
e1f36503 JA |
326 | char *p1, *p2; |
327 | ||
0bbab0e7 | 328 | strncpy(tmp, ptr, sizeof(tmp) - 1); |
b765a372 JA |
329 | |
330 | p1 = strchr(tmp, '-'); | |
e1f36503 | 331 | if (!p1) { |
0c9baf91 JA |
332 | p1 = strchr(tmp, ':'); |
333 | if (!p1) { | |
334 | ret = 1; | |
335 | break; | |
336 | } | |
e1f36503 JA |
337 | } |
338 | ||
339 | p2 = p1 + 1; | |
340 | *p1 = '\0'; | |
b765a372 | 341 | p1 = tmp; |
e1f36503 JA |
342 | |
343 | ret = 1; | |
344 | if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) { | |
345 | ret = 0; | |
e1f36503 | 346 | if (ul1 > ul2) { |
f90eff5a JA |
347 | unsigned long foo = ul1; |
348 | ||
349 | ul1 = ul2; | |
350 | ul2 = foo; | |
351 | } | |
352 | ||
353 | if (first) { | |
17abbe89 JA |
354 | val_store(ilp, ul1, o->off1, data); |
355 | val_store(ilp, ul2, o->off2, data); | |
356 | } | |
97e8cd44 | 357 | if (o->off3 && o->off4) { |
17abbe89 JA |
358 | val_store(ilp, ul1, o->off3, data); |
359 | val_store(ilp, ul2, o->off4, data); | |
e1f36503 | 360 | } |
17abbe89 JA |
361 | } |
362 | ||
e1f36503 JA |
363 | break; |
364 | } | |
13335ddb JA |
365 | case FIO_OPT_INT: |
366 | case FIO_OPT_BOOL: { | |
e1f36503 JA |
367 | fio_opt_int_fn *fn = o->cb; |
368 | ||
369 | ret = check_int(ptr, &il); | |
370 | if (ret) | |
371 | break; | |
372 | ||
db8e0165 JA |
373 | if (o->maxval && il > (int) o->maxval) { |
374 | fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval); | |
375 | return 1; | |
376 | } | |
377 | if (o->minval && il < o->minval) { | |
378 | fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval); | |
379 | return 1; | |
380 | } | |
e1f36503 | 381 | |
76a43db4 JA |
382 | if (o->neg) |
383 | il = !il; | |
384 | ||
e1f36503 JA |
385 | if (fn) |
386 | ret = fn(data, &il); | |
387 | else { | |
17abbe89 JA |
388 | if (first) |
389 | val_store(ilp, il, o->off1, data); | |
787f7e95 | 390 | if (!more && o->off2) |
17abbe89 | 391 | val_store(ilp, il, o->off2, data); |
e1f36503 JA |
392 | } |
393 | break; | |
394 | } | |
395 | case FIO_OPT_STR_SET: { | |
396 | fio_opt_str_set_fn *fn = o->cb; | |
397 | ||
398 | if (fn) | |
399 | ret = fn(data); | |
400 | else { | |
17abbe89 JA |
401 | if (first) |
402 | val_store(ilp, 1, o->off1, data); | |
787f7e95 | 403 | if (!more && o->off2) |
17abbe89 | 404 | val_store(ilp, 1, o->off2, data); |
e1f36503 JA |
405 | } |
406 | break; | |
407 | } | |
408 | default: | |
1e97cce9 | 409 | fprintf(stderr, "Bad option type %u\n", o->type); |
e1f36503 JA |
410 | ret = 1; |
411 | } | |
cb2c86fd | 412 | |
e1f36503 | 413 | return ret; |
cb2c86fd JA |
414 | } |
415 | ||
f90eff5a JA |
416 | static int handle_option(struct fio_option *o, const char *ptr, void *data) |
417 | { | |
92b586f8 | 418 | const char *ptr2 = NULL; |
787f7e95 | 419 | int r1, r2; |
f90eff5a | 420 | |
a3d741fa JA |
421 | dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, ptr); |
422 | ||
f90eff5a | 423 | /* |
787f7e95 JA |
424 | * See if we have a second set of parameters, hidden after a comma. |
425 | * Do this before parsing the first round, to check if we should | |
426 | * copy set 1 options to set 2. | |
f90eff5a | 427 | */ |
ad231bc4 JB |
428 | if (ptr && |
429 | (o->type != FIO_OPT_STR_STORE) && | |
430 | (o->type != FIO_OPT_STR)) { | |
92b586f8 | 431 | ptr2 = strchr(ptr, ','); |
0c9baf91 JA |
432 | if (!ptr2) |
433 | ptr2 = strchr(ptr, ':'); | |
b486f76a JA |
434 | if (!ptr2) |
435 | ptr2 = strchr(ptr, '-'); | |
0c9baf91 | 436 | } |
787f7e95 JA |
437 | |
438 | /* | |
439 | * Don't return early if parsing the first option fails - if | |
440 | * we are doing multiple arguments, we can allow the first one | |
441 | * being empty. | |
442 | */ | |
443 | r1 = __handle_option(o, ptr, data, 1, !!ptr2); | |
444 | ||
f90eff5a | 445 | if (!ptr2) |
787f7e95 | 446 | return r1; |
f90eff5a JA |
447 | |
448 | ptr2++; | |
787f7e95 JA |
449 | r2 = __handle_option(o, ptr2, data, 0, 0); |
450 | ||
451 | return r1 && r2; | |
f90eff5a JA |
452 | } |
453 | ||
b4692828 JA |
454 | int parse_cmd_option(const char *opt, const char *val, |
455 | struct fio_option *options, void *data) | |
456 | { | |
457 | struct fio_option *o; | |
458 | ||
459 | o = find_option(options, opt); | |
460 | if (!o) { | |
461 | fprintf(stderr, "Bad option %s\n", opt); | |
462 | return 1; | |
463 | } | |
464 | ||
b1508cf9 JA |
465 | if (!handle_option(o, val, data)) |
466 | return 0; | |
467 | ||
468 | fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val); | |
469 | return 1; | |
b4692828 JA |
470 | } |
471 | ||
e1f36503 | 472 | int parse_option(const char *opt, struct fio_option *options, void *data) |
cb2c86fd | 473 | { |
b4692828 | 474 | struct fio_option *o; |
e1f36503 | 475 | char *pre, *post; |
0401bca6 | 476 | char *tmp; |
e1f36503 | 477 | |
0401bca6 | 478 | tmp = strdup(opt); |
e1f36503 JA |
479 | |
480 | pre = strchr(tmp, '='); | |
481 | if (pre) { | |
482 | post = pre; | |
483 | *pre = '\0'; | |
484 | pre = tmp; | |
485 | post++; | |
486 | o = find_option(options, pre); | |
487 | } else { | |
488 | o = find_option(options, tmp); | |
489 | post = NULL; | |
490 | } | |
cb2c86fd | 491 | |
e1f36503 JA |
492 | if (!o) { |
493 | fprintf(stderr, "Bad option %s\n", tmp); | |
0401bca6 | 494 | free(tmp); |
e1f36503 JA |
495 | return 1; |
496 | } | |
497 | ||
0401bca6 JA |
498 | if (!handle_option(o, post, data)) { |
499 | free(tmp); | |
b1508cf9 | 500 | return 0; |
0401bca6 | 501 | } |
b1508cf9 JA |
502 | |
503 | fprintf(stderr, "fio: failed parsing %s\n", opt); | |
0401bca6 | 504 | free(tmp); |
b1508cf9 | 505 | return 1; |
e1f36503 | 506 | } |
fd28ca49 | 507 | |
0e9f7fac JA |
508 | /* |
509 | * Option match, levenshtein distance. Handy for not quite remembering what | |
510 | * the option name is. | |
511 | */ | |
512 | static int string_distance(const char *s1, const char *s2) | |
513 | { | |
514 | unsigned int s1_len = strlen(s1); | |
515 | unsigned int s2_len = strlen(s2); | |
516 | unsigned int *p, *q, *r; | |
517 | unsigned int i, j; | |
518 | ||
519 | p = malloc(sizeof(unsigned int) * (s2_len + 1)); | |
520 | q = malloc(sizeof(unsigned int) * (s2_len + 1)); | |
521 | ||
522 | p[0] = 0; | |
523 | for (i = 1; i <= s2_len; i++) | |
524 | p[i] = p[i - 1] + 1; | |
525 | ||
526 | for (i = 1; i <= s1_len; i++) { | |
527 | q[0] = p[0] + 1; | |
528 | for (j = 1; j <= s2_len; j++) { | |
529 | unsigned int sub = p[j - 1]; | |
530 | ||
531 | if (s1[i - 1] != s2[j - 1]) | |
532 | sub++; | |
533 | ||
534 | q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub)); | |
535 | } | |
536 | r = p; | |
537 | p = q; | |
538 | q = r; | |
539 | } | |
540 | ||
541 | i = p[s2_len]; | |
542 | free(p); | |
543 | free(q); | |
544 | return i; | |
545 | } | |
546 | ||
547 | static void show_option_help(struct fio_option *o) | |
fd28ca49 | 548 | { |
fd28ca49 JA |
549 | const char *typehelp[] = { |
550 | "string (opt=bla)", | |
551 | "string with possible k/m/g postfix (opt=4k)", | |
552 | "string with range and postfix (opt=1k-4k)", | |
553 | "string with time postfix (opt=10s)", | |
554 | "string (opt=bla)", | |
555 | "string with dual range (opt=1k-4k,4k-8k)", | |
556 | "integer value (opt=100)", | |
13335ddb | 557 | "boolean value (opt=1)", |
fd28ca49 JA |
558 | "no argument (opt)", |
559 | }; | |
0e9f7fac | 560 | |
d2bb7fea JA |
561 | if (o->alias) |
562 | printf("%20s: %s\n", "alias", o->alias); | |
563 | ||
0e9f7fac JA |
564 | printf("%20s: %s\n", "type", typehelp[o->type]); |
565 | printf("%20s: %s\n", "default", o->def ? o->def : "no default"); | |
566 | show_option_range(o); | |
567 | show_option_values(o); | |
568 | } | |
569 | ||
afdf9352 JA |
570 | static struct fio_option *find_child(struct fio_option *options, |
571 | struct fio_option *o) | |
572 | { | |
573 | struct fio_option *__o; | |
574 | ||
fdf28744 JA |
575 | for (__o = options + 1; __o->name; __o++) |
576 | if (__o->parent && !strcmp(__o->parent, o->name)) | |
afdf9352 JA |
577 | return __o; |
578 | ||
579 | return NULL; | |
580 | } | |
581 | ||
323d9113 JA |
582 | static void __print_option(struct fio_option *o, struct fio_option *org, |
583 | int level) | |
afdf9352 JA |
584 | { |
585 | char name[256], *p; | |
323d9113 | 586 | int depth; |
afdf9352 JA |
587 | |
588 | if (!o) | |
589 | return; | |
ef9aff52 JA |
590 | if (!org) |
591 | org = o; | |
afdf9352 JA |
592 | |
593 | p = name; | |
323d9113 JA |
594 | depth = level; |
595 | while (depth--) | |
596 | p += sprintf(p, "%s", " "); | |
afdf9352 JA |
597 | |
598 | sprintf(p, "%s", o->name); | |
599 | ||
600 | printf("%-24s: %s\n", name, o->help); | |
323d9113 JA |
601 | } |
602 | ||
603 | static void print_option(struct fio_option *o) | |
604 | { | |
605 | struct fio_option *parent; | |
606 | struct fio_option *__o; | |
607 | unsigned int printed; | |
608 | unsigned int level; | |
609 | ||
610 | __print_option(o, NULL, 0); | |
611 | parent = o; | |
612 | level = 0; | |
613 | do { | |
614 | level++; | |
615 | printed = 0; | |
616 | ||
617 | while ((__o = find_child(o, parent)) != NULL) { | |
618 | __print_option(__o, o, level); | |
619 | o = __o; | |
620 | printed++; | |
621 | } | |
622 | ||
623 | parent = o; | |
624 | } while (printed); | |
afdf9352 JA |
625 | } |
626 | ||
0e9f7fac JA |
627 | int show_cmd_help(struct fio_option *options, const char *name) |
628 | { | |
629 | struct fio_option *o, *closest; | |
630 | unsigned int best_dist; | |
29fc6afe | 631 | int found = 0; |
320beefe JA |
632 | int show_all = 0; |
633 | ||
634 | if (!name || !strcmp(name, "all")) | |
635 | show_all = 1; | |
fd28ca49 | 636 | |
0e9f7fac JA |
637 | closest = NULL; |
638 | best_dist = -1; | |
4945ba12 | 639 | for (o = &options[0]; o->name; o++) { |
320beefe JA |
640 | int match = 0; |
641 | ||
0e9f7fac | 642 | if (name) { |
7f9348f8 JA |
643 | if (!strcmp(name, o->name) || |
644 | (o->alias && !strcmp(name, o->alias))) | |
0e9f7fac JA |
645 | match = 1; |
646 | else { | |
647 | unsigned int dist; | |
648 | ||
649 | dist = string_distance(name, o->name); | |
650 | if (dist < best_dist) { | |
651 | best_dist = dist; | |
652 | closest = o; | |
653 | } | |
654 | } | |
655 | } | |
fd28ca49 JA |
656 | |
657 | if (show_all || match) { | |
29fc6afe | 658 | found = 1; |
c167dedc | 659 | if (match) |
afdf9352 | 660 | printf("%24s: %s\n", o->name, o->help); |
c167dedc | 661 | if (show_all) { |
afdf9352 | 662 | if (!o->parent) |
323d9113 | 663 | print_option(o); |
4945ba12 | 664 | continue; |
c167dedc | 665 | } |
fd28ca49 JA |
666 | } |
667 | ||
70df2f19 JA |
668 | if (!match) |
669 | continue; | |
670 | ||
0e9f7fac | 671 | show_option_help(o); |
fd28ca49 JA |
672 | } |
673 | ||
29fc6afe JA |
674 | if (found) |
675 | return 0; | |
fd28ca49 | 676 | |
0e9f7fac JA |
677 | printf("No such command: %s", name); |
678 | if (closest) { | |
679 | printf(" - showing closest match\n"); | |
680 | printf("%20s: %s\n", closest->name, closest->help); | |
681 | show_option_help(closest); | |
682 | } else | |
683 | printf("\n"); | |
684 | ||
29fc6afe | 685 | return 1; |
fd28ca49 | 686 | } |
ee738499 | 687 | |
13335ddb JA |
688 | /* |
689 | * Handle parsing of default parameters. | |
690 | */ | |
ee738499 JA |
691 | void fill_default_options(void *data, struct fio_option *options) |
692 | { | |
4945ba12 | 693 | struct fio_option *o; |
ee738499 | 694 | |
a3d741fa JA |
695 | dprint(FD_PARSE, "filling default options\n"); |
696 | ||
4945ba12 | 697 | for (o = &options[0]; o->name; o++) |
ee738499 JA |
698 | if (o->def) |
699 | handle_option(o, o->def, data); | |
ee738499 | 700 | } |
13335ddb JA |
701 | |
702 | /* | |
703 | * Sanitize the options structure. For now it just sets min/max for bool | |
5b0a8880 | 704 | * values and whether both callback and offsets are given. |
13335ddb JA |
705 | */ |
706 | void options_init(struct fio_option *options) | |
707 | { | |
4945ba12 | 708 | struct fio_option *o; |
13335ddb | 709 | |
a3d741fa JA |
710 | dprint(FD_PARSE, "init options\n"); |
711 | ||
4945ba12 | 712 | for (o = &options[0]; o->name; o++) { |
13335ddb JA |
713 | if (o->type == FIO_OPT_BOOL) { |
714 | o->minval = 0; | |
715 | o->maxval = 1; | |
716 | } | |
0f3e35ef JA |
717 | if (o->type == FIO_OPT_STR_SET && o->def) |
718 | fprintf(stderr, "Option %s: string set option with default will always be true\n", o->name); | |
b1ec1da6 JA |
719 | if (!o->cb && !o->off1) |
720 | fprintf(stderr, "Option %s: neither cb nor offset given\n", o->name); | |
af52b345 | 721 | if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE) |
b1ec1da6 | 722 | continue; |
5b0a8880 JA |
723 | if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) |
724 | fprintf(stderr, "Option %s: both cb and offset given\n", o->name); | |
13335ddb JA |
725 | } |
726 | } |