| 1 | /* |
| 2 | * This file contains the ini and command liner parser main. |
| 3 | */ |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <ctype.h> |
| 7 | #include <string.h> |
| 8 | #include <errno.h> |
| 9 | #include <limits.h> |
| 10 | #include <float.h> |
| 11 | |
| 12 | #include "compiler/compiler.h" |
| 13 | #include "parse.h" |
| 14 | #include "debug.h" |
| 15 | #include "log.h" |
| 16 | #include "options.h" |
| 17 | #include "optgroup.h" |
| 18 | #include "minmax.h" |
| 19 | #include "lib/ieee754.h" |
| 20 | #include "lib/pow2.h" |
| 21 | |
| 22 | #ifdef CONFIG_ARITHMETIC |
| 23 | #include "y.tab.h" |
| 24 | #endif |
| 25 | |
| 26 | static const char *opt_type_names[] = { |
| 27 | "OPT_INVALID", |
| 28 | "OPT_STR", |
| 29 | "OPT_STR_ULL", |
| 30 | "OPT_STR_MULTI", |
| 31 | "OPT_STR_VAL", |
| 32 | "OPT_STR_VAL_TIME", |
| 33 | "OPT_STR_STORE", |
| 34 | "OPT_RANGE", |
| 35 | "OPT_INT", |
| 36 | "OPT_ULL", |
| 37 | "OPT_BOOL", |
| 38 | "OPT_FLOAT_LIST", |
| 39 | "OPT_STR_SET", |
| 40 | "OPT_DEPRECATED", |
| 41 | "OPT_SOFT_DEPRECATED", |
| 42 | "OPT_UNSUPPORTED", |
| 43 | }; |
| 44 | |
| 45 | static const struct fio_option *__fio_options; |
| 46 | |
| 47 | static int vp_cmp(const void *p1, const void *p2) |
| 48 | { |
| 49 | const struct value_pair *vp1 = p1; |
| 50 | const struct value_pair *vp2 = p2; |
| 51 | |
| 52 | return strlen(vp2->ival) - strlen(vp1->ival); |
| 53 | } |
| 54 | |
| 55 | static void posval_sort(const struct fio_option *o, struct value_pair *vpmap) |
| 56 | { |
| 57 | const struct value_pair *vp; |
| 58 | int entries; |
| 59 | |
| 60 | memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair)); |
| 61 | |
| 62 | for (entries = 0; entries < PARSE_MAX_VP; entries++) { |
| 63 | vp = &o->posval[entries]; |
| 64 | if (!vp->ival || vp->ival[0] == '\0') |
| 65 | break; |
| 66 | |
| 67 | memcpy(&vpmap[entries], vp, sizeof(*vp)); |
| 68 | } |
| 69 | |
| 70 | qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp); |
| 71 | } |
| 72 | |
| 73 | static void show_option_range(const struct fio_option *o, |
| 74 | ssize_t (*logger)(const char *format, ...)) |
| 75 | { |
| 76 | if (o->type == FIO_OPT_FLOAT_LIST) { |
| 77 | const char *sep = ""; |
| 78 | if (!o->minfp && !o->maxfp) |
| 79 | return; |
| 80 | |
| 81 | logger("%20s: ", "range"); |
| 82 | if (o->minfp != DBL_MIN) { |
| 83 | logger("min=%f", o->minfp); |
| 84 | sep = ", "; |
| 85 | } |
| 86 | if (o->maxfp != DBL_MAX) |
| 87 | logger("%smax=%f", sep, o->maxfp); |
| 88 | logger("\n"); |
| 89 | } else if (!o->posval[0].ival) { |
| 90 | if (!o->minval && !o->maxval) |
| 91 | return; |
| 92 | |
| 93 | logger("%20s: min=%d", "range", o->minval); |
| 94 | if (o->maxval) |
| 95 | logger(", max=%d", o->maxval); |
| 96 | logger("\n"); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | static void show_option_values(const struct fio_option *o) |
| 101 | { |
| 102 | int i; |
| 103 | |
| 104 | for (i = 0; i < PARSE_MAX_VP; i++) { |
| 105 | const struct value_pair *vp = &o->posval[i]; |
| 106 | |
| 107 | if (!vp->ival) |
| 108 | continue; |
| 109 | |
| 110 | log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival); |
| 111 | if (vp->help) |
| 112 | log_info(" %s", vp->help); |
| 113 | log_info("\n"); |
| 114 | } |
| 115 | |
| 116 | if (i) |
| 117 | log_info("\n"); |
| 118 | } |
| 119 | |
| 120 | static void show_option_help(const struct fio_option *o, int is_err) |
| 121 | { |
| 122 | const char *typehelp[] = { |
| 123 | "invalid", |
| 124 | "string (opt=bla)", |
| 125 | "string (opt=bla)", |
| 126 | "string with possible k/m/g postfix (opt=4k)", |
| 127 | "string with time postfix (opt=10s)", |
| 128 | "string (opt=bla)", |
| 129 | "string with dual range (opt=1k-4k,4k-8k)", |
| 130 | "integer value (opt=100)", |
| 131 | "boolean value (opt=1)", |
| 132 | "list of floating point values separated by ':' (opt=5.9:7.8)", |
| 133 | "no argument (opt)", |
| 134 | "deprecated", |
| 135 | "unsupported", |
| 136 | }; |
| 137 | ssize_t (*logger)(const char *format, ...); |
| 138 | |
| 139 | if (is_err) |
| 140 | logger = log_err; |
| 141 | else |
| 142 | logger = log_info; |
| 143 | |
| 144 | if (o->alias) |
| 145 | logger("%20s: %s\n", "alias", o->alias); |
| 146 | |
| 147 | logger("%20s: %s\n", "type", typehelp[o->type]); |
| 148 | logger("%20s: %s\n", "default", o->def ? o->def : "no default"); |
| 149 | if (o->prof_name) |
| 150 | logger("%20s: only for profile '%s'\n", "valid", o->prof_name); |
| 151 | show_option_range(o, logger); |
| 152 | show_option_values(o); |
| 153 | } |
| 154 | |
| 155 | static unsigned long long get_mult_time(const char *str, int len, |
| 156 | int is_seconds) |
| 157 | { |
| 158 | const char *p = str; |
| 159 | char *c; |
| 160 | unsigned long long mult = 1; |
| 161 | int i; |
| 162 | |
| 163 | /* |
| 164 | * Go forward until we hit a non-digit, or +/- sign |
| 165 | */ |
| 166 | while ((p - str) <= len) { |
| 167 | if (!isdigit((int) *p) && (*p != '+') && (*p != '-')) |
| 168 | break; |
| 169 | p++; |
| 170 | } |
| 171 | |
| 172 | if (!isalpha((int) *p)) { |
| 173 | if (is_seconds) |
| 174 | return 1000000UL; |
| 175 | else |
| 176 | return 1; |
| 177 | } |
| 178 | |
| 179 | c = strdup(p); |
| 180 | for (i = 0; i < strlen(c); i++) |
| 181 | c[i] = tolower((unsigned char)c[i]); |
| 182 | |
| 183 | if (!strncmp("us", c, 2) || !strncmp("usec", c, 4)) |
| 184 | mult = 1; |
| 185 | else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4)) |
| 186 | mult = 1000; |
| 187 | else if (!strcmp("s", c)) |
| 188 | mult = 1000000; |
| 189 | else if (!strcmp("m", c)) |
| 190 | mult = 60 * 1000000UL; |
| 191 | else if (!strcmp("h", c)) |
| 192 | mult = 60 * 60 * 1000000UL; |
| 193 | else if (!strcmp("d", c)) |
| 194 | mult = 24 * 60 * 60 * 1000000ULL; |
| 195 | |
| 196 | free(c); |
| 197 | return mult; |
| 198 | } |
| 199 | |
| 200 | static int is_separator(char c) |
| 201 | { |
| 202 | switch (c) { |
| 203 | case ':': |
| 204 | case '-': |
| 205 | case ',': |
| 206 | case '/': |
| 207 | return 1; |
| 208 | default: |
| 209 | return 0; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | static unsigned long long __get_mult_bytes(const char *p, void *data, |
| 214 | int *percent) |
| 215 | { |
| 216 | unsigned int kb_base = fio_get_kb_base(data); |
| 217 | unsigned long long ret = 1; |
| 218 | unsigned int i, pow = 0, mult = kb_base; |
| 219 | char *c; |
| 220 | |
| 221 | if (!p) |
| 222 | return 1; |
| 223 | |
| 224 | c = strdup(p); |
| 225 | |
| 226 | for (i = 0; i < strlen(c); i++) { |
| 227 | c[i] = tolower((unsigned char)c[i]); |
| 228 | if (is_separator(c[i])) { |
| 229 | c[i] = '\0'; |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /* If kb_base is 1000, use true units. |
| 235 | * If kb_base is 1024, use opposite units. |
| 236 | */ |
| 237 | if (!strncmp("pib", c, 3)) { |
| 238 | pow = 5; |
| 239 | if (kb_base == 1000) |
| 240 | mult = 1024; |
| 241 | else if (kb_base == 1024) |
| 242 | mult = 1000; |
| 243 | } else if (!strncmp("tib", c, 3)) { |
| 244 | pow = 4; |
| 245 | if (kb_base == 1000) |
| 246 | mult = 1024; |
| 247 | else if (kb_base == 1024) |
| 248 | mult = 1000; |
| 249 | } else if (!strncmp("gib", c, 3)) { |
| 250 | pow = 3; |
| 251 | if (kb_base == 1000) |
| 252 | mult = 1024; |
| 253 | else if (kb_base == 1024) |
| 254 | mult = 1000; |
| 255 | } else if (!strncmp("mib", c, 3)) { |
| 256 | pow = 2; |
| 257 | if (kb_base == 1000) |
| 258 | mult = 1024; |
| 259 | else if (kb_base == 1024) |
| 260 | mult = 1000; |
| 261 | } else if (!strncmp("kib", c, 3)) { |
| 262 | pow = 1; |
| 263 | if (kb_base == 1000) |
| 264 | mult = 1024; |
| 265 | else if (kb_base == 1024) |
| 266 | mult = 1000; |
| 267 | } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2)) { |
| 268 | pow = 5; |
| 269 | } else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2)) { |
| 270 | pow = 4; |
| 271 | } else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2)) { |
| 272 | pow = 3; |
| 273 | } else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2)) { |
| 274 | pow = 2; |
| 275 | } else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2)) { |
| 276 | pow = 1; |
| 277 | } else if (!strncmp("%", c, 1)) { |
| 278 | *percent = 1; |
| 279 | free(c); |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | while (pow--) |
| 284 | ret *= (unsigned long long) mult; |
| 285 | |
| 286 | free(c); |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | static unsigned long long get_mult_bytes(const char *str, int len, void *data, |
| 291 | int *percent) |
| 292 | { |
| 293 | const char *p = str; |
| 294 | int digit_seen = 0; |
| 295 | |
| 296 | if (len < 2) |
| 297 | return __get_mult_bytes(str, data, percent); |
| 298 | |
| 299 | /* |
| 300 | * Go forward until we hit a non-digit, or +/- sign |
| 301 | */ |
| 302 | while ((p - str) <= len) { |
| 303 | if (!isdigit((int) *p) && |
| 304 | (((*p != '+') && (*p != '-')) || digit_seen)) |
| 305 | break; |
| 306 | digit_seen |= isdigit((int) *p); |
| 307 | p++; |
| 308 | } |
| 309 | |
| 310 | if (!isalpha((int) *p) && (*p != '%')) |
| 311 | p = NULL; |
| 312 | |
| 313 | return __get_mult_bytes(p, data, percent); |
| 314 | } |
| 315 | |
| 316 | extern int evaluate_arithmetic_expression(const char *buffer, long long *ival, |
| 317 | double *dval, double implied_units, |
| 318 | int is_time); |
| 319 | |
| 320 | /* |
| 321 | * Convert string into a floating number. Return 1 for success and 0 otherwise. |
| 322 | */ |
| 323 | int str_to_float(const char *str, double *val, int is_time) |
| 324 | { |
| 325 | #ifdef CONFIG_ARITHMETIC |
| 326 | int rc; |
| 327 | long long ival; |
| 328 | double dval; |
| 329 | |
| 330 | if (str[0] == '(') { |
| 331 | rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0, is_time); |
| 332 | if (!rc) { |
| 333 | *val = dval; |
| 334 | return 1; |
| 335 | } |
| 336 | } |
| 337 | #endif |
| 338 | return 1 == sscanf(str, "%lf", val); |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * convert string into decimal value, noting any size suffix |
| 343 | */ |
| 344 | int str_to_decimal(const char *str, long long *val, int kilo, void *data, |
| 345 | int is_seconds, int is_time) |
| 346 | { |
| 347 | int len, base; |
| 348 | int rc = 1; |
| 349 | #ifdef CONFIG_ARITHMETIC |
| 350 | long long ival; |
| 351 | double dval; |
| 352 | double implied_units = 1.0; |
| 353 | #endif |
| 354 | |
| 355 | len = strlen(str); |
| 356 | if (!len) |
| 357 | return 1; |
| 358 | |
| 359 | #ifdef CONFIG_ARITHMETIC |
| 360 | if (is_seconds) |
| 361 | implied_units = 1000000.0; |
| 362 | if (str[0] == '(') |
| 363 | rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time); |
| 364 | if (str[0] == '(' && !rc) { |
| 365 | if (!kilo && is_seconds) |
| 366 | *val = ival / 1000000LL; |
| 367 | else |
| 368 | *val = ival; |
| 369 | } |
| 370 | #endif |
| 371 | |
| 372 | if (rc == 1) { |
| 373 | if (strstr(str, "0x") || strstr(str, "0X")) |
| 374 | base = 16; |
| 375 | else |
| 376 | base = 10; |
| 377 | |
| 378 | *val = strtoll(str, NULL, base); |
| 379 | if (*val == LONG_MAX && errno == ERANGE) |
| 380 | return 1; |
| 381 | } |
| 382 | |
| 383 | if (kilo) { |
| 384 | unsigned long long mult; |
| 385 | int perc = 0; |
| 386 | |
| 387 | mult = get_mult_bytes(str, len, data, &perc); |
| 388 | if (perc) |
| 389 | *val = -1ULL - *val; |
| 390 | else |
| 391 | *val *= mult; |
| 392 | } else |
| 393 | *val *= get_mult_time(str, len, is_seconds); |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | int check_str_bytes(const char *p, long long *val, void *data) |
| 399 | { |
| 400 | return str_to_decimal(p, val, 1, data, 0, 0); |
| 401 | } |
| 402 | |
| 403 | int check_str_time(const char *p, long long *val, int is_seconds) |
| 404 | { |
| 405 | return str_to_decimal(p, val, 0, NULL, is_seconds, 1); |
| 406 | } |
| 407 | |
| 408 | void strip_blank_front(char **p) |
| 409 | { |
| 410 | char *s = *p; |
| 411 | |
| 412 | if (!strlen(s)) |
| 413 | return; |
| 414 | while (isspace((int) *s)) |
| 415 | s++; |
| 416 | |
| 417 | *p = s; |
| 418 | } |
| 419 | |
| 420 | void strip_blank_end(char *p) |
| 421 | { |
| 422 | char *start = p, *s; |
| 423 | |
| 424 | if (!strlen(p)) |
| 425 | return; |
| 426 | |
| 427 | s = strchr(p, ';'); |
| 428 | if (s) |
| 429 | *s = '\0'; |
| 430 | s = strchr(p, '#'); |
| 431 | if (s) |
| 432 | *s = '\0'; |
| 433 | if (s) |
| 434 | p = s; |
| 435 | |
| 436 | s = p + strlen(p); |
| 437 | while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start)) |
| 438 | s--; |
| 439 | |
| 440 | *(s + 1) = '\0'; |
| 441 | } |
| 442 | |
| 443 | static int check_range_bytes(const char *str, long long *val, void *data) |
| 444 | { |
| 445 | long long __val; |
| 446 | |
| 447 | if (!str_to_decimal(str, &__val, 1, data, 0, 0)) { |
| 448 | *val = __val; |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | return 1; |
| 453 | } |
| 454 | |
| 455 | static int check_int(const char *p, int *val) |
| 456 | { |
| 457 | if (!strlen(p)) |
| 458 | return 1; |
| 459 | if (strstr(p, "0x") || strstr(p, "0X")) { |
| 460 | if (sscanf(p, "%x", val) == 1) |
| 461 | return 0; |
| 462 | } else { |
| 463 | if (sscanf(p, "%u", val) == 1) |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | return 1; |
| 468 | } |
| 469 | |
| 470 | static size_t opt_len(const char *str) |
| 471 | { |
| 472 | char *postfix; |
| 473 | |
| 474 | postfix = strchr(str, ':'); |
| 475 | if (!postfix) |
| 476 | return strlen(str); |
| 477 | |
| 478 | return (int)(postfix - str); |
| 479 | } |
| 480 | |
| 481 | static int str_match_len(const struct value_pair *vp, const char *str) |
| 482 | { |
| 483 | return max(strlen(vp->ival), opt_len(str)); |
| 484 | } |
| 485 | |
| 486 | #define val_store(ptr, val, off, or, data, o) \ |
| 487 | do { \ |
| 488 | ptr = td_var((data), (o), (off)); \ |
| 489 | if ((or)) \ |
| 490 | *ptr |= (val); \ |
| 491 | else \ |
| 492 | *ptr = (val); \ |
| 493 | } while (0) |
| 494 | |
| 495 | static const char *opt_type_name(const struct fio_option *o) |
| 496 | { |
| 497 | compiletime_assert(ARRAY_SIZE(opt_type_names) - 1 == FIO_OPT_UNSUPPORTED, |
| 498 | "opt_type_names[] index"); |
| 499 | |
| 500 | if (o->type <= FIO_OPT_UNSUPPORTED) |
| 501 | return opt_type_names[o->type]; |
| 502 | |
| 503 | return "OPT_UNKNOWN?"; |
| 504 | } |
| 505 | |
| 506 | static int __handle_option(const struct fio_option *o, const char *ptr, |
| 507 | void *data, int first, int more, int curr) |
| 508 | { |
| 509 | int il=0, *ilp; |
| 510 | fio_fp64_t *flp; |
| 511 | long long ull, *ullp; |
| 512 | long ul2; |
| 513 | long long ull1, ull2; |
| 514 | double uf; |
| 515 | char **cp = NULL; |
| 516 | int ret = 0, is_time = 0; |
| 517 | const struct value_pair *vp; |
| 518 | struct value_pair posval[PARSE_MAX_VP]; |
| 519 | int i, all_skipped = 1; |
| 520 | |
| 521 | dprint(FD_PARSE, "__handle_option=%s, type=%s, ptr=%s\n", o->name, |
| 522 | opt_type_name(o), ptr); |
| 523 | |
| 524 | if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) { |
| 525 | log_err("Option %s requires an argument\n", o->name); |
| 526 | return 1; |
| 527 | } |
| 528 | |
| 529 | switch (o->type) { |
| 530 | case FIO_OPT_STR: |
| 531 | case FIO_OPT_STR_ULL: |
| 532 | case FIO_OPT_STR_MULTI: { |
| 533 | fio_opt_str_fn *fn = o->cb; |
| 534 | |
| 535 | posval_sort(o, posval); |
| 536 | |
| 537 | ret = 1; |
| 538 | for (i = 0; i < PARSE_MAX_VP; i++) { |
| 539 | vp = &posval[i]; |
| 540 | if (!vp->ival || vp->ival[0] == '\0') |
| 541 | continue; |
| 542 | all_skipped = 0; |
| 543 | if (!ptr) |
| 544 | break; |
| 545 | if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) { |
| 546 | ret = 0; |
| 547 | if (!o->off1) |
| 548 | continue; |
| 549 | if (o->type == FIO_OPT_STR_ULL) |
| 550 | val_store(ullp, vp->oval, o->off1, vp->orval, data, o); |
| 551 | else |
| 552 | val_store(ilp, vp->oval, o->off1, vp->orval, data, o); |
| 553 | continue; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | if (ret && !all_skipped) |
| 558 | show_option_values(o); |
| 559 | else if (fn) |
| 560 | ret = fn(data, ptr); |
| 561 | break; |
| 562 | } |
| 563 | case FIO_OPT_STR_VAL_TIME: |
| 564 | is_time = 1; |
| 565 | /* fall through */ |
| 566 | case FIO_OPT_ULL: |
| 567 | case FIO_OPT_INT: |
| 568 | case FIO_OPT_STR_VAL: { |
| 569 | fio_opt_str_val_fn *fn = o->cb; |
| 570 | char tmp[128], *p; |
| 571 | |
| 572 | if (!is_time && o->is_time) |
| 573 | is_time = o->is_time; |
| 574 | |
| 575 | tmp[sizeof(tmp) - 1] = '\0'; |
| 576 | strncpy(tmp, ptr, sizeof(tmp) - 1); |
| 577 | p = strchr(tmp, ','); |
| 578 | if (p) |
| 579 | *p = '\0'; |
| 580 | |
| 581 | if (is_time) |
| 582 | ret = check_str_time(tmp, &ull, o->is_seconds); |
| 583 | else |
| 584 | ret = check_str_bytes(tmp, &ull, data); |
| 585 | |
| 586 | dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull); |
| 587 | |
| 588 | if (ret) |
| 589 | break; |
| 590 | if (o->pow2 && !is_power_of_2(ull)) { |
| 591 | log_err("%s: must be a power-of-2\n", o->name); |
| 592 | return 1; |
| 593 | } |
| 594 | |
| 595 | if (o->maxval && ull > o->maxval) { |
| 596 | log_err("max value out of range: %llu" |
| 597 | " (%llu max)\n", ull, o->maxval); |
| 598 | return 1; |
| 599 | } |
| 600 | if (o->minval && ull < o->minval) { |
| 601 | log_err("min value out of range: %lld" |
| 602 | " (%d min)\n", ull, o->minval); |
| 603 | return 1; |
| 604 | } |
| 605 | if (o->posval[0].ival) { |
| 606 | posval_sort(o, posval); |
| 607 | |
| 608 | ret = 1; |
| 609 | for (i = 0; i < PARSE_MAX_VP; i++) { |
| 610 | vp = &posval[i]; |
| 611 | if (!vp->ival || vp->ival[0] == '\0') |
| 612 | continue; |
| 613 | if (vp->oval == ull) { |
| 614 | ret = 0; |
| 615 | break; |
| 616 | } |
| 617 | } |
| 618 | if (ret) { |
| 619 | log_err("fio: value %llu not allowed:\n", ull); |
| 620 | show_option_values(o); |
| 621 | return 1; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | if (fn) |
| 626 | ret = fn(data, &ull); |
| 627 | else { |
| 628 | if (o->type == FIO_OPT_INT) { |
| 629 | if (first) |
| 630 | val_store(ilp, ull, o->off1, 0, data, o); |
| 631 | if (curr == 1) { |
| 632 | if (o->off2) |
| 633 | val_store(ilp, ull, o->off2, 0, data, o); |
| 634 | } |
| 635 | if (curr == 2) { |
| 636 | if (o->off3) |
| 637 | val_store(ilp, ull, o->off3, 0, data, o); |
| 638 | } |
| 639 | if (!more) { |
| 640 | if (curr < 1) { |
| 641 | if (o->off2) |
| 642 | val_store(ilp, ull, o->off2, 0, data, o); |
| 643 | } |
| 644 | if (curr < 2) { |
| 645 | if (o->off3) |
| 646 | val_store(ilp, ull, o->off3, 0, data, o); |
| 647 | } |
| 648 | } |
| 649 | } else if (o->type == FIO_OPT_ULL) { |
| 650 | if (first) |
| 651 | val_store(ullp, ull, o->off1, 0, data, o); |
| 652 | if (curr == 1) { |
| 653 | if (o->off2) |
| 654 | val_store(ullp, ull, o->off2, 0, data, o); |
| 655 | } |
| 656 | if (curr == 2) { |
| 657 | if (o->off3) |
| 658 | val_store(ullp, ull, o->off3, 0, data, o); |
| 659 | } |
| 660 | if (!more) { |
| 661 | if (curr < 1) { |
| 662 | if (o->off2) |
| 663 | val_store(ullp, ull, o->off2, 0, data, o); |
| 664 | } |
| 665 | if (curr < 2) { |
| 666 | if (o->off3) |
| 667 | val_store(ullp, ull, o->off3, 0, data, o); |
| 668 | } |
| 669 | } |
| 670 | } else { |
| 671 | if (first) |
| 672 | val_store(ullp, ull, o->off1, 0, data, o); |
| 673 | if (!more) { |
| 674 | if (o->off2) |
| 675 | val_store(ullp, ull, o->off2, 0, data, o); |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | break; |
| 680 | } |
| 681 | case FIO_OPT_FLOAT_LIST: { |
| 682 | char *cp2; |
| 683 | |
| 684 | if (first) { |
| 685 | /* |
| 686 | ** Initialize precision to 0 and zero out list |
| 687 | ** in case specified list is shorter than default |
| 688 | */ |
| 689 | if (o->off2) { |
| 690 | ul2 = 0; |
| 691 | ilp = td_var(data, o, o->off2); |
| 692 | *ilp = ul2; |
| 693 | } |
| 694 | |
| 695 | flp = td_var(data, o, o->off1); |
| 696 | for(i = 0; i < o->maxlen; i++) |
| 697 | flp[i].u.f = 0.0; |
| 698 | } |
| 699 | if (curr >= o->maxlen) { |
| 700 | log_err("the list exceeding max length %d\n", |
| 701 | o->maxlen); |
| 702 | return 1; |
| 703 | } |
| 704 | if (!str_to_float(ptr, &uf, 0)) { /* this breaks if we ever have lists of times */ |
| 705 | log_err("not a floating point value: %s\n", ptr); |
| 706 | return 1; |
| 707 | } |
| 708 | if (o->minfp || o->maxfp) { |
| 709 | if (uf > o->maxfp) { |
| 710 | log_err("value out of range: %f" |
| 711 | " (range max: %f)\n", uf, o->maxfp); |
| 712 | return 1; |
| 713 | } |
| 714 | if (uf < o->minfp) { |
| 715 | log_err("value out of range: %f" |
| 716 | " (range min: %f)\n", uf, o->minfp); |
| 717 | return 1; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | flp = td_var(data, o, o->off1); |
| 722 | flp[curr].u.f = uf; |
| 723 | |
| 724 | dprint(FD_PARSE, " out=%f\n", uf); |
| 725 | |
| 726 | /* |
| 727 | ** Calculate precision for output by counting |
| 728 | ** number of digits after period. Find first |
| 729 | ** period in entire remaining list each time |
| 730 | */ |
| 731 | cp2 = strchr(ptr, '.'); |
| 732 | if (cp2 != NULL) { |
| 733 | int len = 0; |
| 734 | |
| 735 | while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9') |
| 736 | len++; |
| 737 | |
| 738 | if (o->off2) { |
| 739 | ilp = td_var(data, o, o->off2); |
| 740 | if (len > *ilp) |
| 741 | *ilp = len; |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | break; |
| 746 | } |
| 747 | case FIO_OPT_STR_STORE: { |
| 748 | fio_opt_str_fn *fn = o->cb; |
| 749 | |
| 750 | if (!strlen(ptr)) |
| 751 | return 1; |
| 752 | |
| 753 | if (o->off1) { |
| 754 | cp = td_var(data, o, o->off1); |
| 755 | *cp = strdup(ptr); |
| 756 | } |
| 757 | |
| 758 | if (fn) |
| 759 | ret = fn(data, ptr); |
| 760 | else if (o->posval[0].ival) { |
| 761 | posval_sort(o, posval); |
| 762 | |
| 763 | ret = 1; |
| 764 | for (i = 0; i < PARSE_MAX_VP; i++) { |
| 765 | vp = &posval[i]; |
| 766 | if (!vp->ival || vp->ival[0] == '\0' || !cp) |
| 767 | continue; |
| 768 | all_skipped = 0; |
| 769 | if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) { |
| 770 | char *rest; |
| 771 | |
| 772 | ret = 0; |
| 773 | if (vp->cb) |
| 774 | fn = vp->cb; |
| 775 | rest = strstr(*cp ?: ptr, ":"); |
| 776 | if (rest) { |
| 777 | if (*cp) |
| 778 | *rest = '\0'; |
| 779 | ptr = rest + 1; |
| 780 | } else |
| 781 | ptr = NULL; |
| 782 | break; |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | if (!all_skipped) { |
| 788 | if (ret && !*cp) |
| 789 | show_option_values(o); |
| 790 | else if (ret && *cp) |
| 791 | ret = 0; |
| 792 | else if (fn && ptr) |
| 793 | ret = fn(data, ptr); |
| 794 | } |
| 795 | |
| 796 | break; |
| 797 | } |
| 798 | case FIO_OPT_RANGE: { |
| 799 | char tmp[128]; |
| 800 | char *p1, *p2; |
| 801 | |
| 802 | tmp[sizeof(tmp) - 1] = '\0'; |
| 803 | strncpy(tmp, ptr, sizeof(tmp) - 1); |
| 804 | |
| 805 | /* Handle bsrange with separate read,write values: */ |
| 806 | p1 = strchr(tmp, ','); |
| 807 | if (p1) |
| 808 | *p1 = '\0'; |
| 809 | |
| 810 | p1 = strchr(tmp, '-'); |
| 811 | if (!p1) { |
| 812 | p1 = strchr(tmp, ':'); |
| 813 | if (!p1) { |
| 814 | ret = 1; |
| 815 | break; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | p2 = p1 + 1; |
| 820 | *p1 = '\0'; |
| 821 | p1 = tmp; |
| 822 | |
| 823 | ret = 1; |
| 824 | if (!check_range_bytes(p1, &ull1, data) && |
| 825 | !check_range_bytes(p2, &ull2, data)) { |
| 826 | ret = 0; |
| 827 | if (ull1 > ull2) { |
| 828 | unsigned long long foo = ull1; |
| 829 | |
| 830 | ull1 = ull2; |
| 831 | ull2 = foo; |
| 832 | } |
| 833 | |
| 834 | if (first) { |
| 835 | val_store(ullp, ull1, o->off1, 0, data, o); |
| 836 | val_store(ullp, ull2, o->off2, 0, data, o); |
| 837 | } |
| 838 | if (curr == 1) { |
| 839 | if (o->off3 && o->off4) { |
| 840 | val_store(ullp, ull1, o->off3, 0, data, o); |
| 841 | val_store(ullp, ull2, o->off4, 0, data, o); |
| 842 | } |
| 843 | } |
| 844 | if (curr == 2) { |
| 845 | if (o->off5 && o->off6) { |
| 846 | val_store(ullp, ull1, o->off5, 0, data, o); |
| 847 | val_store(ullp, ull2, o->off6, 0, data, o); |
| 848 | } |
| 849 | } |
| 850 | if (!more) { |
| 851 | if (curr < 1) { |
| 852 | if (o->off3 && o->off4) { |
| 853 | val_store(ullp, ull1, o->off3, 0, data, o); |
| 854 | val_store(ullp, ull2, o->off4, 0, data, o); |
| 855 | } |
| 856 | } |
| 857 | if (curr < 2) { |
| 858 | if (o->off5 && o->off6) { |
| 859 | val_store(ullp, ull1, o->off5, 0, data, o); |
| 860 | val_store(ullp, ull2, o->off6, 0, data, o); |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | break; |
| 867 | } |
| 868 | case FIO_OPT_BOOL: |
| 869 | case FIO_OPT_STR_SET: { |
| 870 | fio_opt_int_fn *fn = o->cb; |
| 871 | |
| 872 | if (ptr) |
| 873 | ret = check_int(ptr, &il); |
| 874 | else if (o->type == FIO_OPT_BOOL) |
| 875 | ret = 1; |
| 876 | else |
| 877 | il = 1; |
| 878 | |
| 879 | dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il); |
| 880 | |
| 881 | if (ret) |
| 882 | break; |
| 883 | |
| 884 | if (o->maxval && il > (int) o->maxval) { |
| 885 | log_err("max value out of range: %d (%llu max)\n", |
| 886 | il, o->maxval); |
| 887 | return 1; |
| 888 | } |
| 889 | if (o->minval && il < o->minval) { |
| 890 | log_err("min value out of range: %d (%d min)\n", |
| 891 | il, o->minval); |
| 892 | return 1; |
| 893 | } |
| 894 | |
| 895 | if (o->neg) |
| 896 | il = !il; |
| 897 | |
| 898 | if (fn) |
| 899 | ret = fn(data, &il); |
| 900 | else { |
| 901 | if (first) |
| 902 | val_store(ilp, il, o->off1, 0, data, o); |
| 903 | if (!more) { |
| 904 | if (o->off2) |
| 905 | val_store(ilp, il, o->off2, 0, data, o); |
| 906 | } |
| 907 | } |
| 908 | break; |
| 909 | } |
| 910 | case FIO_OPT_DEPRECATED: |
| 911 | ret = 1; |
| 912 | /* fall through */ |
| 913 | case FIO_OPT_SOFT_DEPRECATED: |
| 914 | log_info("Option %s is deprecated\n", o->name); |
| 915 | break; |
| 916 | default: |
| 917 | log_err("Bad option type %u\n", o->type); |
| 918 | ret = 1; |
| 919 | } |
| 920 | |
| 921 | if (ret) |
| 922 | return ret; |
| 923 | |
| 924 | if (o->verify) { |
| 925 | ret = o->verify(o, data); |
| 926 | if (ret) { |
| 927 | log_err("Correct format for offending option\n"); |
| 928 | log_err("%20s: %s\n", o->name, o->help); |
| 929 | show_option_help(o, 1); |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | return ret; |
| 934 | } |
| 935 | |
| 936 | static int handle_option(const struct fio_option *o, const char *__ptr, |
| 937 | void *data) |
| 938 | { |
| 939 | char *o_ptr, *ptr, *ptr2; |
| 940 | int ret, done; |
| 941 | |
| 942 | dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr); |
| 943 | |
| 944 | o_ptr = ptr = NULL; |
| 945 | if (__ptr) |
| 946 | o_ptr = ptr = strdup(__ptr); |
| 947 | |
| 948 | /* |
| 949 | * See if we have another set of parameters, hidden after a comma. |
| 950 | * Do this before parsing this round, to check if we should |
| 951 | * copy set 1 options to set 2. |
| 952 | */ |
| 953 | done = 0; |
| 954 | ret = 1; |
| 955 | do { |
| 956 | int __ret; |
| 957 | |
| 958 | ptr2 = NULL; |
| 959 | if (ptr && |
| 960 | (o->type != FIO_OPT_STR_STORE) && |
| 961 | (o->type != FIO_OPT_STR) && |
| 962 | (o->type != FIO_OPT_FLOAT_LIST)) { |
| 963 | ptr2 = strchr(ptr, ','); |
| 964 | if (ptr2 && *(ptr2 + 1) == '\0') |
| 965 | *ptr2 = '\0'; |
| 966 | if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) { |
| 967 | if (!ptr2) |
| 968 | ptr2 = strchr(ptr, ':'); |
| 969 | if (!ptr2) |
| 970 | ptr2 = strchr(ptr, '-'); |
| 971 | } |
| 972 | } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) { |
| 973 | ptr2 = strchr(ptr, ':'); |
| 974 | } |
| 975 | |
| 976 | /* |
| 977 | * Don't return early if parsing the first option fails - if |
| 978 | * we are doing multiple arguments, we can allow the first one |
| 979 | * being empty. |
| 980 | */ |
| 981 | __ret = __handle_option(o, ptr, data, !done, !!ptr2, done); |
| 982 | if (ret) |
| 983 | ret = __ret; |
| 984 | |
| 985 | if (!ptr2) |
| 986 | break; |
| 987 | |
| 988 | ptr = ptr2 + 1; |
| 989 | done++; |
| 990 | } while (1); |
| 991 | |
| 992 | if (o_ptr) |
| 993 | free(o_ptr); |
| 994 | return ret; |
| 995 | } |
| 996 | |
| 997 | struct fio_option *find_option(struct fio_option *options, const char *opt) |
| 998 | { |
| 999 | struct fio_option *o; |
| 1000 | |
| 1001 | for (o = &options[0]; o->name; o++) { |
| 1002 | if (!o_match(o, opt)) |
| 1003 | continue; |
| 1004 | if (o->type == FIO_OPT_UNSUPPORTED) { |
| 1005 | log_err("Option <%s>: %s\n", o->name, o->help); |
| 1006 | continue; |
| 1007 | } |
| 1008 | |
| 1009 | return o; |
| 1010 | } |
| 1011 | |
| 1012 | return NULL; |
| 1013 | } |
| 1014 | |
| 1015 | const struct fio_option * |
| 1016 | find_option_c(const struct fio_option *options, const char *opt) |
| 1017 | { |
| 1018 | return find_option((struct fio_option *)options, opt); |
| 1019 | } |
| 1020 | |
| 1021 | static const struct fio_option * |
| 1022 | get_option(char *opt, const struct fio_option *options, char **post) |
| 1023 | { |
| 1024 | const struct fio_option *o; |
| 1025 | char *ret; |
| 1026 | |
| 1027 | ret = strchr(opt, '='); |
| 1028 | if (ret) { |
| 1029 | *post = ret; |
| 1030 | *ret = '\0'; |
| 1031 | ret = opt; |
| 1032 | (*post)++; |
| 1033 | strip_blank_end(ret); |
| 1034 | o = find_option_c(options, ret); |
| 1035 | } else { |
| 1036 | o = find_option_c(options, opt); |
| 1037 | *post = NULL; |
| 1038 | } |
| 1039 | |
| 1040 | return o; |
| 1041 | } |
| 1042 | |
| 1043 | static int opt_cmp(const void *p1, const void *p2) |
| 1044 | { |
| 1045 | const struct fio_option *o; |
| 1046 | char *s, *foo; |
| 1047 | int prio1, prio2; |
| 1048 | |
| 1049 | prio1 = prio2 = 0; |
| 1050 | |
| 1051 | if (*(char **)p1) { |
| 1052 | s = strdup(*((char **) p1)); |
| 1053 | o = get_option(s, __fio_options, &foo); |
| 1054 | if (o) |
| 1055 | prio1 = o->prio; |
| 1056 | free(s); |
| 1057 | } |
| 1058 | if (*(char **)p2) { |
| 1059 | s = strdup(*((char **) p2)); |
| 1060 | o = get_option(s, __fio_options, &foo); |
| 1061 | if (o) |
| 1062 | prio2 = o->prio; |
| 1063 | free(s); |
| 1064 | } |
| 1065 | |
| 1066 | return prio2 - prio1; |
| 1067 | } |
| 1068 | |
| 1069 | void sort_options(char **opts, const struct fio_option *options, int num_opts) |
| 1070 | { |
| 1071 | __fio_options = options; |
| 1072 | qsort(opts, num_opts, sizeof(char *), opt_cmp); |
| 1073 | __fio_options = NULL; |
| 1074 | } |
| 1075 | |
| 1076 | static void add_to_dump_list(const struct fio_option *o, |
| 1077 | struct flist_head *dump_list, const char *post) |
| 1078 | { |
| 1079 | struct print_option *p; |
| 1080 | |
| 1081 | if (!dump_list) |
| 1082 | return; |
| 1083 | |
| 1084 | p = malloc(sizeof(*p)); |
| 1085 | p->name = strdup(o->name); |
| 1086 | if (post) |
| 1087 | p->value = strdup(post); |
| 1088 | else |
| 1089 | p->value = NULL; |
| 1090 | |
| 1091 | flist_add_tail(&p->list, dump_list); |
| 1092 | } |
| 1093 | |
| 1094 | int parse_cmd_option(const char *opt, const char *val, |
| 1095 | const struct fio_option *options, void *data, |
| 1096 | struct flist_head *dump_list) |
| 1097 | { |
| 1098 | const struct fio_option *o; |
| 1099 | |
| 1100 | o = find_option_c(options, opt); |
| 1101 | if (!o) { |
| 1102 | log_err("Bad option <%s>\n", opt); |
| 1103 | return 1; |
| 1104 | } |
| 1105 | |
| 1106 | if (handle_option(o, val, data)) { |
| 1107 | log_err("fio: failed parsing %s=%s\n", opt, val); |
| 1108 | return 1; |
| 1109 | } |
| 1110 | |
| 1111 | add_to_dump_list(o, dump_list, val); |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
| 1115 | int parse_option(char *opt, const char *input, const struct fio_option *options, |
| 1116 | const struct fio_option **o, void *data, |
| 1117 | struct flist_head *dump_list) |
| 1118 | { |
| 1119 | char *post; |
| 1120 | |
| 1121 | if (!opt) { |
| 1122 | log_err("fio: failed parsing %s\n", input); |
| 1123 | *o = NULL; |
| 1124 | return 1; |
| 1125 | } |
| 1126 | |
| 1127 | *o = get_option(opt, options, &post); |
| 1128 | if (!*o) { |
| 1129 | if (post) { |
| 1130 | int len = strlen(opt); |
| 1131 | if (opt + len + 1 != post) |
| 1132 | memmove(opt + len + 1, post, strlen(post)); |
| 1133 | opt[len] = '='; |
| 1134 | } |
| 1135 | return 1; |
| 1136 | } |
| 1137 | |
| 1138 | if (handle_option(*o, post, data)) { |
| 1139 | log_err("fio: failed parsing %s\n", input); |
| 1140 | return 1; |
| 1141 | } |
| 1142 | |
| 1143 | add_to_dump_list(*o, dump_list, post); |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | /* |
| 1148 | * Option match, levenshtein distance. Handy for not quite remembering what |
| 1149 | * the option name is. |
| 1150 | */ |
| 1151 | int string_distance(const char *s1, const char *s2) |
| 1152 | { |
| 1153 | unsigned int s1_len = strlen(s1); |
| 1154 | unsigned int s2_len = strlen(s2); |
| 1155 | unsigned int *p, *q, *r; |
| 1156 | unsigned int i, j; |
| 1157 | |
| 1158 | p = malloc(sizeof(unsigned int) * (s2_len + 1)); |
| 1159 | q = malloc(sizeof(unsigned int) * (s2_len + 1)); |
| 1160 | |
| 1161 | p[0] = 0; |
| 1162 | for (i = 1; i <= s2_len; i++) |
| 1163 | p[i] = p[i - 1] + 1; |
| 1164 | |
| 1165 | for (i = 1; i <= s1_len; i++) { |
| 1166 | q[0] = p[0] + 1; |
| 1167 | for (j = 1; j <= s2_len; j++) { |
| 1168 | unsigned int sub = p[j - 1]; |
| 1169 | unsigned int pmin; |
| 1170 | |
| 1171 | if (s1[i - 1] != s2[j - 1]) |
| 1172 | sub++; |
| 1173 | |
| 1174 | pmin = min(q[j - 1] + 1, sub); |
| 1175 | q[j] = min(p[j] + 1, pmin); |
| 1176 | } |
| 1177 | r = p; |
| 1178 | p = q; |
| 1179 | q = r; |
| 1180 | } |
| 1181 | |
| 1182 | i = p[s2_len]; |
| 1183 | free(p); |
| 1184 | free(q); |
| 1185 | return i; |
| 1186 | } |
| 1187 | |
| 1188 | /* |
| 1189 | * Make a guess of whether the distance from 's1' is significant enough |
| 1190 | * to warrant printing the guess. We set this to a 1/2 match. |
| 1191 | */ |
| 1192 | int string_distance_ok(const char *opt, int distance) |
| 1193 | { |
| 1194 | size_t len; |
| 1195 | |
| 1196 | len = strlen(opt); |
| 1197 | len = (len + 1) / 2; |
| 1198 | return distance <= len; |
| 1199 | } |
| 1200 | |
| 1201 | static const struct fio_option *find_child(const struct fio_option *options, |
| 1202 | const struct fio_option *o) |
| 1203 | { |
| 1204 | const struct fio_option *__o; |
| 1205 | |
| 1206 | for (__o = options + 1; __o->name; __o++) |
| 1207 | if (__o->parent && !strcmp(__o->parent, o->name)) |
| 1208 | return __o; |
| 1209 | |
| 1210 | return NULL; |
| 1211 | } |
| 1212 | |
| 1213 | static void __print_option(const struct fio_option *o, |
| 1214 | const struct fio_option *org, |
| 1215 | int level) |
| 1216 | { |
| 1217 | char name[256], *p; |
| 1218 | int depth; |
| 1219 | |
| 1220 | if (!o) |
| 1221 | return; |
| 1222 | |
| 1223 | p = name; |
| 1224 | depth = level; |
| 1225 | while (depth--) |
| 1226 | p += sprintf(p, "%s", " "); |
| 1227 | |
| 1228 | sprintf(p, "%s", o->name); |
| 1229 | |
| 1230 | log_info("%-24s: %s\n", name, o->help); |
| 1231 | } |
| 1232 | |
| 1233 | static void print_option(const struct fio_option *o) |
| 1234 | { |
| 1235 | const struct fio_option *parent; |
| 1236 | const struct fio_option *__o; |
| 1237 | unsigned int printed; |
| 1238 | unsigned int level; |
| 1239 | |
| 1240 | __print_option(o, NULL, 0); |
| 1241 | parent = o; |
| 1242 | level = 0; |
| 1243 | do { |
| 1244 | level++; |
| 1245 | printed = 0; |
| 1246 | |
| 1247 | while ((__o = find_child(o, parent)) != NULL) { |
| 1248 | __print_option(__o, o, level); |
| 1249 | o = __o; |
| 1250 | printed++; |
| 1251 | } |
| 1252 | |
| 1253 | parent = o; |
| 1254 | } while (printed); |
| 1255 | } |
| 1256 | |
| 1257 | int show_cmd_help(const struct fio_option *options, const char *name) |
| 1258 | { |
| 1259 | const struct fio_option *o, *closest; |
| 1260 | unsigned int best_dist = -1U; |
| 1261 | int found = 0; |
| 1262 | int show_all = 0; |
| 1263 | |
| 1264 | if (!name || !strcmp(name, "all")) |
| 1265 | show_all = 1; |
| 1266 | |
| 1267 | closest = NULL; |
| 1268 | best_dist = -1; |
| 1269 | for (o = &options[0]; o->name; o++) { |
| 1270 | int match = 0; |
| 1271 | |
| 1272 | if (o->type == FIO_OPT_DEPRECATED || |
| 1273 | o->type == FIO_OPT_SOFT_DEPRECATED) |
| 1274 | continue; |
| 1275 | if (!exec_profile && o->prof_name) |
| 1276 | continue; |
| 1277 | if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name))) |
| 1278 | continue; |
| 1279 | |
| 1280 | if (name) { |
| 1281 | if (!strcmp(name, o->name) || |
| 1282 | (o->alias && !strcmp(name, o->alias))) |
| 1283 | match = 1; |
| 1284 | else { |
| 1285 | unsigned int dist; |
| 1286 | |
| 1287 | dist = string_distance(name, o->name); |
| 1288 | if (dist < best_dist) { |
| 1289 | best_dist = dist; |
| 1290 | closest = o; |
| 1291 | } |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | if (show_all || match) { |
| 1296 | found = 1; |
| 1297 | if (match) |
| 1298 | log_info("%20s: %s\n", o->name, o->help); |
| 1299 | if (show_all) { |
| 1300 | if (!o->parent) |
| 1301 | print_option(o); |
| 1302 | continue; |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | if (!match) |
| 1307 | continue; |
| 1308 | |
| 1309 | show_option_help(o, 0); |
| 1310 | } |
| 1311 | |
| 1312 | if (found) |
| 1313 | return 0; |
| 1314 | |
| 1315 | log_err("No such command: %s", name); |
| 1316 | |
| 1317 | /* |
| 1318 | * Only print an appropriately close option, one where the edit |
| 1319 | * distance isn't too big. Otherwise we get crazy matches. |
| 1320 | */ |
| 1321 | if (closest && best_dist < 3) { |
| 1322 | log_info(" - showing closest match\n"); |
| 1323 | log_info("%20s: %s\n", closest->name, closest->help); |
| 1324 | show_option_help(closest, 0); |
| 1325 | } else |
| 1326 | log_info("\n"); |
| 1327 | |
| 1328 | return 1; |
| 1329 | } |
| 1330 | |
| 1331 | /* |
| 1332 | * Handle parsing of default parameters. |
| 1333 | */ |
| 1334 | void fill_default_options(void *data, const struct fio_option *options) |
| 1335 | { |
| 1336 | const struct fio_option *o; |
| 1337 | |
| 1338 | dprint(FD_PARSE, "filling default options\n"); |
| 1339 | |
| 1340 | for (o = &options[0]; o->name; o++) |
| 1341 | if (o->def) |
| 1342 | handle_option(o, o->def, data); |
| 1343 | } |
| 1344 | |
| 1345 | static void option_init(struct fio_option *o) |
| 1346 | { |
| 1347 | if (o->type == FIO_OPT_DEPRECATED || o->type == FIO_OPT_UNSUPPORTED || |
| 1348 | o->type == FIO_OPT_SOFT_DEPRECATED) |
| 1349 | return; |
| 1350 | if (o->name && !o->lname) |
| 1351 | log_err("Option %s: missing long option name\n", o->name); |
| 1352 | if (o->type == FIO_OPT_BOOL) { |
| 1353 | o->minval = 0; |
| 1354 | o->maxval = 1; |
| 1355 | } |
| 1356 | if (o->type == FIO_OPT_INT) { |
| 1357 | if (!o->maxval) |
| 1358 | o->maxval = UINT_MAX; |
| 1359 | } |
| 1360 | if (o->type == FIO_OPT_ULL) { |
| 1361 | if (!o->maxval) |
| 1362 | o->maxval = ULLONG_MAX; |
| 1363 | } |
| 1364 | if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) { |
| 1365 | log_err("Option %s: string set option with" |
| 1366 | " default will always be true\n", o->name); |
| 1367 | } |
| 1368 | if (!o->cb && !o->off1) |
| 1369 | log_err("Option %s: neither cb nor offset given\n", o->name); |
| 1370 | if (!o->category) { |
| 1371 | log_info("Option %s: no category defined. Setting to misc\n", o->name); |
| 1372 | o->category = FIO_OPT_C_GENERAL; |
| 1373 | o->group = FIO_OPT_G_INVALID; |
| 1374 | } |
| 1375 | if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE || |
| 1376 | o->type == FIO_OPT_STR_MULTI) |
| 1377 | return; |
| 1378 | } |
| 1379 | |
| 1380 | /* |
| 1381 | * Sanitize the options structure. For now it just sets min/max for bool |
| 1382 | * values and whether both callback and offsets are given. |
| 1383 | */ |
| 1384 | void options_init(struct fio_option *options) |
| 1385 | { |
| 1386 | struct fio_option *o; |
| 1387 | |
| 1388 | dprint(FD_PARSE, "init options\n"); |
| 1389 | |
| 1390 | for (o = &options[0]; o->name; o++) { |
| 1391 | option_init(o); |
| 1392 | if (o->inverse) |
| 1393 | o->inv_opt = find_option(options, o->inverse); |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | void options_mem_dupe(const struct fio_option *options, void *data) |
| 1398 | { |
| 1399 | const struct fio_option *o; |
| 1400 | char **ptr; |
| 1401 | |
| 1402 | dprint(FD_PARSE, "dup options\n"); |
| 1403 | |
| 1404 | for (o = &options[0]; o->name; o++) { |
| 1405 | if (o->type != FIO_OPT_STR_STORE) |
| 1406 | continue; |
| 1407 | |
| 1408 | ptr = td_var(data, o, o->off1); |
| 1409 | if (*ptr) |
| 1410 | *ptr = strdup(*ptr); |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | void options_free(const struct fio_option *options, void *data) |
| 1415 | { |
| 1416 | const struct fio_option *o; |
| 1417 | char **ptr; |
| 1418 | |
| 1419 | dprint(FD_PARSE, "free options\n"); |
| 1420 | |
| 1421 | for (o = &options[0]; o->name; o++) { |
| 1422 | if (o->type != FIO_OPT_STR_STORE || !o->off1 || o->no_free) |
| 1423 | continue; |
| 1424 | |
| 1425 | ptr = td_var(data, o, o->off1); |
| 1426 | if (*ptr) { |
| 1427 | free(*ptr); |
| 1428 | *ptr = NULL; |
| 1429 | } |
| 1430 | } |
| 1431 | } |