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