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