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