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