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