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