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