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> |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 12 | |
| 13 | #include "parse.h" |
Jens Axboe | a3d741f | 2008-02-27 18:32:33 +0100 | [diff] [blame] | 14 | #include "debug.h" |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 15 | |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 16 | static struct fio_option *fio_options; |
| 17 | |
Jens Axboe | f085737 | 2007-03-19 13:15:23 +0100 | [diff] [blame] | 18 | static int vp_cmp(const void *p1, const void *p2) |
| 19 | { |
| 20 | const struct value_pair *vp1 = p1; |
| 21 | const struct value_pair *vp2 = p2; |
| 22 | |
| 23 | return strlen(vp2->ival) - strlen(vp1->ival); |
| 24 | } |
| 25 | |
| 26 | static void posval_sort(struct fio_option *o, struct value_pair *vpmap) |
| 27 | { |
| 28 | const struct value_pair *vp; |
| 29 | int entries; |
| 30 | |
| 31 | memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair)); |
| 32 | |
| 33 | for (entries = 0; entries < PARSE_MAX_VP; entries++) { |
| 34 | vp = &o->posval[entries]; |
| 35 | if (!vp->ival || vp->ival[0] == '\0') |
| 36 | break; |
| 37 | |
| 38 | memcpy(&vpmap[entries], vp, sizeof(*vp)); |
| 39 | } |
| 40 | |
| 41 | qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp); |
| 42 | } |
| 43 | |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 44 | static void show_option_range(struct fio_option *o) |
| 45 | { |
| 46 | if (!o->minval && !o->maxval) |
| 47 | return; |
| 48 | |
Jens Axboe | 3980127 | 2009-07-01 09:06:12 +0200 | [diff] [blame^] | 49 | printf("%20s: min=%d", "range", o->minval); |
| 50 | if (o->maxval) |
| 51 | printf(", max=%d", o->maxval); |
| 52 | printf("\n"); |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static void show_option_values(struct fio_option *o) |
| 56 | { |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 57 | int i = 0; |
| 58 | |
| 59 | do { |
Jens Axboe | 7837213 | 2007-03-14 13:24:07 +0100 | [diff] [blame] | 60 | const struct value_pair *vp = &o->posval[i]; |
| 61 | |
| 62 | if (!vp->ival) |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 63 | break; |
| 64 | |
Jens Axboe | 7837213 | 2007-03-14 13:24:07 +0100 | [diff] [blame] | 65 | printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival); |
| 66 | if (vp->help) |
| 67 | printf(" %s", vp->help); |
| 68 | printf("\n"); |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 69 | i++; |
| 70 | } while (i < PARSE_MAX_VP); |
| 71 | |
| 72 | if (i) |
| 73 | printf("\n"); |
| 74 | } |
| 75 | |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 76 | static unsigned long get_mult_time(char c) |
| 77 | { |
| 78 | switch (c) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 79 | case 'm': |
| 80 | case 'M': |
| 81 | return 60; |
| 82 | case 'h': |
| 83 | case 'H': |
| 84 | return 60 * 60; |
| 85 | case 'd': |
| 86 | case 'D': |
| 87 | return 24 * 60 * 60; |
| 88 | default: |
| 89 | return 1; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | static unsigned long get_mult_bytes(char c) |
| 94 | { |
| 95 | switch (c) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 96 | case 'k': |
| 97 | case 'K': |
| 98 | return 1024; |
| 99 | case 'm': |
| 100 | case 'M': |
| 101 | return 1024 * 1024; |
| 102 | case 'g': |
| 103 | case 'G': |
| 104 | return 1024 * 1024 * 1024; |
| 105 | case 'e': |
| 106 | case 'E': |
| 107 | return 1024 * 1024 * 1024 * 1024UL; |
| 108 | default: |
| 109 | return 1; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | /* |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 114 | * convert string into decimal value, noting any size suffix |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 115 | */ |
Jens Axboe | 564ca97 | 2007-12-14 12:21:19 +0100 | [diff] [blame] | 116 | int str_to_decimal(const char *str, long long *val, int kilo) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 117 | { |
Jens Axboe | b347f9d | 2009-03-09 14:15:21 +0100 | [diff] [blame] | 118 | int len, base; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 119 | |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 120 | len = strlen(str); |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 121 | if (!len) |
| 122 | return 1; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 123 | |
Jens Axboe | b347f9d | 2009-03-09 14:15:21 +0100 | [diff] [blame] | 124 | if (strstr(str, "0x") || strstr(str, "0X")) |
| 125 | base = 16; |
| 126 | else |
| 127 | base = 10; |
| 128 | |
| 129 | *val = strtoll(str, NULL, base); |
Jens Axboe | cda866c | 2007-01-10 16:02:32 +0100 | [diff] [blame] | 130 | if (*val == LONG_MAX && errno == ERANGE) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 131 | return 1; |
| 132 | |
| 133 | if (kilo) |
| 134 | *val *= get_mult_bytes(str[len - 1]); |
| 135 | else |
| 136 | *val *= get_mult_time(str[len - 1]); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 137 | |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 141 | static int check_str_bytes(const char *p, long long *val) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 142 | { |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 143 | return str_to_decimal(p, val, 1); |
| 144 | } |
| 145 | |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 146 | static int check_str_time(const char *p, long long *val) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 147 | { |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 148 | return str_to_decimal(p, val, 0); |
| 149 | } |
| 150 | |
| 151 | void strip_blank_front(char **p) |
| 152 | { |
| 153 | char *s = *p; |
| 154 | |
| 155 | while (isspace(*s)) |
| 156 | s++; |
Jens Axboe | 4d651da | 2007-03-28 19:34:53 +0200 | [diff] [blame] | 157 | |
| 158 | *p = s; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void strip_blank_end(char *p) |
| 162 | { |
Jens Axboe | 853ee7f | 2009-03-06 20:29:29 +0100 | [diff] [blame] | 163 | char *start = p, *s; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 164 | |
Jens Axboe | 523bfad | 2007-04-04 11:04:06 +0200 | [diff] [blame] | 165 | s = strchr(p, ';'); |
| 166 | if (s) |
| 167 | *s = '\0'; |
| 168 | s = strchr(p, '#'); |
| 169 | if (s) |
| 170 | *s = '\0'; |
| 171 | if (s) |
| 172 | p = s; |
| 173 | |
Jens Axboe | 7f7e6e5 | 2007-07-19 14:50:05 +0200 | [diff] [blame] | 174 | s = p + strlen(p); |
Jens Axboe | 853ee7f | 2009-03-06 20:29:29 +0100 | [diff] [blame] | 175 | while ((isspace(*s) || iscntrl(*s)) && (s > start)) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 176 | s--; |
| 177 | |
| 178 | *(s + 1) = '\0'; |
| 179 | } |
| 180 | |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 181 | static int check_range_bytes(const char *str, long *val) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 182 | { |
| 183 | char suffix; |
| 184 | |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 185 | if (!strlen(str)) |
| 186 | return 1; |
| 187 | |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 188 | if (sscanf(str, "%lu%c", val, &suffix) == 2) { |
| 189 | *val *= get_mult_bytes(suffix); |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | if (sscanf(str, "%lu", val) == 1) |
| 194 | return 0; |
| 195 | |
| 196 | return 1; |
| 197 | } |
| 198 | |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 199 | static int check_int(const char *p, int *val) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 200 | { |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 201 | if (!strlen(p)) |
| 202 | return 1; |
Jens Axboe | d78ee46 | 2007-08-10 14:03:20 +0200 | [diff] [blame] | 203 | if (strstr(p, "0x") || strstr(p, "0X")) { |
Jens Axboe | a61bdfd | 2007-07-30 09:07:04 +0200 | [diff] [blame] | 204 | if (sscanf(p, "%x", val) == 1) |
| 205 | return 0; |
| 206 | } else { |
| 207 | if (sscanf(p, "%u", val) == 1) |
| 208 | return 0; |
| 209 | } |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 210 | |
| 211 | return 1; |
| 212 | } |
| 213 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 214 | static struct fio_option *find_option(struct fio_option *options, |
| 215 | const char *opt) |
| 216 | { |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 217 | struct fio_option *o; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 218 | |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 219 | for (o = &options[0]; o->name; o++) { |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 220 | if (!strcmp(o->name, opt)) |
| 221 | return o; |
Jens Axboe | 03b74b3 | 2007-01-11 11:04:31 +0100 | [diff] [blame] | 222 | else if (o->alias && !strcmp(o->alias, opt)) |
| 223 | return o; |
Jens Axboe | 33963c6 | 2006-10-27 11:33:01 +0200 | [diff] [blame] | 224 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 225 | |
| 226 | return NULL; |
| 227 | } |
| 228 | |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 229 | #define val_store(ptr, val, off, data) \ |
| 230 | do { \ |
| 231 | ptr = td_var((data), (off)); \ |
| 232 | *ptr = (val); \ |
| 233 | } while (0) |
| 234 | |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 235 | static int __handle_option(struct fio_option *o, const char *ptr, void *data, |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 236 | int first, int more) |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 237 | { |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 238 | int il, *ilp; |
| 239 | long long ull, *ullp; |
| 240 | long ul1, ul2; |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 241 | char **cp; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 242 | int ret = 0, is_time = 0; |
| 243 | |
Jens Axboe | a3d741f | 2008-02-27 18:32:33 +0100 | [diff] [blame] | 244 | dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name, |
| 245 | o->type, ptr); |
| 246 | |
Jens Axboe | e3cedca | 2008-11-19 19:57:52 +0100 | [diff] [blame] | 247 | if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) { |
Jens Axboe | 08e26e3 | 2006-11-21 13:15:10 +0100 | [diff] [blame] | 248 | fprintf(stderr, "Option %s requires an argument\n", o->name); |
| 249 | return 1; |
| 250 | } |
| 251 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 252 | switch (o->type) { |
| 253 | case FIO_OPT_STR: { |
| 254 | fio_opt_str_fn *fn = o->cb; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 255 | const struct value_pair *vp; |
Jens Axboe | f085737 | 2007-03-19 13:15:23 +0100 | [diff] [blame] | 256 | struct value_pair posval[PARSE_MAX_VP]; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 257 | int i; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 258 | |
Jens Axboe | f085737 | 2007-03-19 13:15:23 +0100 | [diff] [blame] | 259 | posval_sort(o, posval); |
| 260 | |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 261 | for (i = 0; i < PARSE_MAX_VP; i++) { |
Jens Axboe | f085737 | 2007-03-19 13:15:23 +0100 | [diff] [blame] | 262 | vp = &posval[i]; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 263 | if (!vp->ival || vp->ival[0] == '\0') |
| 264 | break; |
Jens Axboe | 6612a27 | 2007-03-13 08:54:06 +0100 | [diff] [blame] | 265 | ret = 1; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 266 | if (!strncmp(vp->ival, ptr, strlen(vp->ival))) { |
| 267 | ret = 0; |
| 268 | if (!o->off1) |
| 269 | break; |
| 270 | val_store(ilp, vp->oval, o->off1, data); |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if (ret) |
| 276 | show_option_values(o); |
| 277 | else if (fn) |
| 278 | ret = fn(data, ptr); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 279 | break; |
| 280 | } |
| 281 | case FIO_OPT_STR_VAL_TIME: |
| 282 | is_time = 1; |
Jens Axboe | f7fa265 | 2009-03-09 14:20:20 +0100 | [diff] [blame] | 283 | case FIO_OPT_INT: |
Jens Axboe | e01b22b | 2009-06-09 15:43:25 +0200 | [diff] [blame] | 284 | case FIO_OPT_STR_VAL: { |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 285 | fio_opt_str_val_fn *fn = o->cb; |
| 286 | |
| 287 | if (is_time) |
| 288 | ret = check_str_time(ptr, &ull); |
| 289 | else |
| 290 | ret = check_str_bytes(ptr, &ull); |
| 291 | |
| 292 | if (ret) |
| 293 | break; |
| 294 | |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 295 | if (o->maxval && ull > o->maxval) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 296 | fprintf(stderr, "max value out of range: %lld" |
| 297 | " (%d max)\n", ull, o->maxval); |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 298 | return 1; |
| 299 | } |
| 300 | if (o->minval && ull < o->minval) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 301 | fprintf(stderr, "min value out of range: %lld" |
| 302 | " (%d min)\n", ull, o->minval); |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 303 | return 1; |
| 304 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 305 | |
| 306 | if (fn) |
| 307 | ret = fn(data, &ull); |
| 308 | else { |
Jens Axboe | e01b22b | 2009-06-09 15:43:25 +0200 | [diff] [blame] | 309 | if (o->type == FIO_OPT_INT) { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 310 | if (first) |
| 311 | val_store(ilp, ull, o->off1, data); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 312 | if (!more && o->off2) |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 313 | val_store(ilp, ull, o->off2, data); |
Jens Axboe | 75e6f36 | 2006-11-03 08:17:09 +0100 | [diff] [blame] | 314 | } else { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 315 | if (first) |
| 316 | val_store(ullp, ull, o->off1, data); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 317 | if (!more && o->off2) |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 318 | val_store(ullp, ull, o->off2, data); |
Jens Axboe | 75e6f36 | 2006-11-03 08:17:09 +0100 | [diff] [blame] | 319 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 320 | } |
| 321 | break; |
| 322 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 323 | case FIO_OPT_STR_STORE: { |
| 324 | fio_opt_str_fn *fn = o->cb; |
| 325 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 326 | cp = td_var(data, o->off1); |
| 327 | *cp = strdup(ptr); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 328 | if (fn) { |
| 329 | ret = fn(data, ptr); |
| 330 | if (ret) { |
| 331 | free(*cp); |
| 332 | *cp = NULL; |
| 333 | } |
| 334 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 335 | break; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 336 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 337 | case FIO_OPT_RANGE: { |
Jens Axboe | b765a37 | 2006-10-27 15:00:16 +0200 | [diff] [blame] | 338 | char tmp[128]; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 339 | char *p1, *p2; |
| 340 | |
Jens Axboe | 0bbab0e | 2006-11-02 09:18:36 +0100 | [diff] [blame] | 341 | strncpy(tmp, ptr, sizeof(tmp) - 1); |
Jens Axboe | b765a37 | 2006-10-27 15:00:16 +0200 | [diff] [blame] | 342 | |
| 343 | p1 = strchr(tmp, '-'); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 344 | if (!p1) { |
Jens Axboe | 0c9baf9 | 2007-01-11 15:59:26 +0100 | [diff] [blame] | 345 | p1 = strchr(tmp, ':'); |
| 346 | if (!p1) { |
| 347 | ret = 1; |
| 348 | break; |
| 349 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | p2 = p1 + 1; |
| 353 | *p1 = '\0'; |
Jens Axboe | b765a37 | 2006-10-27 15:00:16 +0200 | [diff] [blame] | 354 | p1 = tmp; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 355 | |
| 356 | ret = 1; |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 357 | if (!check_range_bytes(p1, &ul1) && |
| 358 | !check_range_bytes(p2, &ul2)) { |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 359 | ret = 0; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 360 | if (ul1 > ul2) { |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 361 | unsigned long foo = ul1; |
| 362 | |
| 363 | ul1 = ul2; |
| 364 | ul2 = foo; |
| 365 | } |
| 366 | |
| 367 | if (first) { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 368 | val_store(ilp, ul1, o->off1, data); |
| 369 | val_store(ilp, ul2, o->off2, data); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 370 | } |
Jens Axboe | 97e8cd4 | 2008-02-05 09:57:39 +0100 | [diff] [blame] | 371 | if (o->off3 && o->off4) { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 372 | val_store(ilp, ul1, o->off3, data); |
| 373 | val_store(ilp, ul2, o->off4, data); |
| 374 | } |
| 375 | } |
| 376 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 377 | break; |
| 378 | } |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 379 | case FIO_OPT_BOOL: { |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 380 | fio_opt_int_fn *fn = o->cb; |
| 381 | |
| 382 | ret = check_int(ptr, &il); |
| 383 | if (ret) |
| 384 | break; |
| 385 | |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 386 | if (o->maxval && il > (int) o->maxval) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 387 | fprintf(stderr, "max value out of range: %d (%d max)\n", |
| 388 | il, o->maxval); |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 389 | return 1; |
| 390 | } |
| 391 | if (o->minval && il < o->minval) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 392 | fprintf(stderr, "min value out of range: %d (%d min)\n", |
| 393 | il, o->minval); |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 394 | return 1; |
| 395 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 396 | |
Jens Axboe | 76a43db | 2007-01-11 13:24:44 +0100 | [diff] [blame] | 397 | if (o->neg) |
| 398 | il = !il; |
| 399 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 400 | if (fn) |
| 401 | ret = fn(data, &il); |
| 402 | else { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 403 | if (first) |
| 404 | val_store(ilp, il, o->off1, data); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 405 | if (!more && o->off2) |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 406 | val_store(ilp, il, o->off2, data); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 407 | } |
| 408 | break; |
| 409 | } |
| 410 | case FIO_OPT_STR_SET: { |
| 411 | fio_opt_str_set_fn *fn = o->cb; |
| 412 | |
| 413 | if (fn) |
| 414 | ret = fn(data); |
| 415 | else { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 416 | if (first) |
| 417 | val_store(ilp, 1, o->off1, data); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 418 | if (!more && o->off2) |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 419 | val_store(ilp, 1, o->off2, data); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 420 | } |
| 421 | break; |
| 422 | } |
Jens Axboe | 15ca150 | 2008-04-07 09:26:02 +0200 | [diff] [blame] | 423 | case FIO_OPT_DEPRECATED: |
| 424 | fprintf(stdout, "Option %s is deprecated\n", o->name); |
| 425 | break; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 426 | default: |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 427 | fprintf(stderr, "Bad option type %u\n", o->type); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 428 | ret = 1; |
| 429 | } |
| 430 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 431 | return ret; |
| 432 | } |
| 433 | |
Jens Axboe | 7c8f1a5 | 2009-06-09 12:28:57 +0200 | [diff] [blame] | 434 | 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] | 435 | { |
Jens Axboe | 7c8f1a5 | 2009-06-09 12:28:57 +0200 | [diff] [blame] | 436 | char *ptr, *ptr2 = NULL; |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 437 | int r1, r2; |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 438 | |
Jens Axboe | 7c8f1a5 | 2009-06-09 12:28:57 +0200 | [diff] [blame] | 439 | dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr); |
| 440 | |
| 441 | ptr = NULL; |
| 442 | if (__ptr) |
| 443 | ptr = strdup(__ptr); |
Jens Axboe | a3d741f | 2008-02-27 18:32:33 +0100 | [diff] [blame] | 444 | |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 445 | /* |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 446 | * See if we have a second set of parameters, hidden after a comma. |
| 447 | * Do this before parsing the first round, to check if we should |
| 448 | * copy set 1 options to set 2. |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 449 | */ |
Joel Becker | ad231bc | 2007-03-01 08:22:42 +0100 | [diff] [blame] | 450 | if (ptr && |
| 451 | (o->type != FIO_OPT_STR_STORE) && |
| 452 | (o->type != FIO_OPT_STR)) { |
Jens Axboe | 92b586f | 2006-11-07 14:29:17 +0100 | [diff] [blame] | 453 | ptr2 = strchr(ptr, ','); |
Jens Axboe | 7c8f1a5 | 2009-06-09 12:28:57 +0200 | [diff] [blame] | 454 | if (ptr2 && *(ptr2 + 1) == '\0') |
| 455 | *ptr2 = '\0'; |
Jens Axboe | 0c9baf9 | 2007-01-11 15:59:26 +0100 | [diff] [blame] | 456 | if (!ptr2) |
| 457 | ptr2 = strchr(ptr, ':'); |
Jens Axboe | b486f76 | 2007-03-15 09:15:12 +0100 | [diff] [blame] | 458 | if (!ptr2) |
| 459 | ptr2 = strchr(ptr, '-'); |
Jens Axboe | 0c9baf9 | 2007-01-11 15:59:26 +0100 | [diff] [blame] | 460 | } |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 461 | |
| 462 | /* |
| 463 | * Don't return early if parsing the first option fails - if |
| 464 | * we are doing multiple arguments, we can allow the first one |
| 465 | * being empty. |
| 466 | */ |
| 467 | r1 = __handle_option(o, ptr, data, 1, !!ptr2); |
| 468 | |
Jens Axboe | 7c8f1a5 | 2009-06-09 12:28:57 +0200 | [diff] [blame] | 469 | if (!ptr2) { |
| 470 | if (ptr) |
| 471 | free(ptr); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 472 | return r1; |
Jens Axboe | 7c8f1a5 | 2009-06-09 12:28:57 +0200 | [diff] [blame] | 473 | } |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 474 | |
| 475 | ptr2++; |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 476 | r2 = __handle_option(o, ptr2, data, 0, 0); |
| 477 | |
Jens Axboe | 7c8f1a5 | 2009-06-09 12:28:57 +0200 | [diff] [blame] | 478 | if (ptr) |
| 479 | free(ptr); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 480 | return r1 && r2; |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 481 | } |
| 482 | |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 483 | static struct fio_option *get_option(const char *opt, |
| 484 | struct fio_option *options, char **post) |
| 485 | { |
| 486 | struct fio_option *o; |
| 487 | char *ret; |
| 488 | |
| 489 | ret = strchr(opt, '='); |
| 490 | if (ret) { |
| 491 | *post = ret; |
| 492 | *ret = '\0'; |
| 493 | ret = (char *) opt; |
| 494 | (*post)++; |
Jens Axboe | 43c129b | 2008-12-08 14:06:42 +0100 | [diff] [blame] | 495 | strip_blank_end(ret); |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 496 | o = find_option(options, ret); |
| 497 | } else { |
| 498 | o = find_option(options, opt); |
| 499 | *post = NULL; |
| 500 | } |
| 501 | |
| 502 | return o; |
| 503 | } |
| 504 | |
| 505 | static int opt_cmp(const void *p1, const void *p2) |
| 506 | { |
| 507 | struct fio_option *o1, *o2; |
| 508 | char *s1, *s2, *foo; |
Jens Axboe | 8cdabc1 | 2008-11-19 12:31:43 +0100 | [diff] [blame] | 509 | int prio1, prio2; |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 510 | |
| 511 | s1 = strdup(*((char **) p1)); |
| 512 | s2 = strdup(*((char **) p2)); |
| 513 | |
| 514 | o1 = get_option(s1, fio_options, &foo); |
| 515 | o2 = get_option(s2, fio_options, &foo); |
Jens Axboe | 8cdabc1 | 2008-11-19 12:31:43 +0100 | [diff] [blame] | 516 | |
| 517 | prio1 = prio2 = 0; |
| 518 | if (o1) |
| 519 | prio1 = o1->prio; |
| 520 | if (o2) |
| 521 | prio2 = o2->prio; |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 522 | |
| 523 | free(s1); |
| 524 | free(s2); |
Jens Axboe | 8cdabc1 | 2008-11-19 12:31:43 +0100 | [diff] [blame] | 525 | return prio2 - prio1; |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | void sort_options(char **opts, struct fio_option *options, int num_opts) |
| 529 | { |
| 530 | fio_options = options; |
| 531 | qsort(opts, num_opts, sizeof(char *), opt_cmp); |
| 532 | fio_options = NULL; |
| 533 | } |
| 534 | |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 535 | int parse_cmd_option(const char *opt, const char *val, |
| 536 | struct fio_option *options, void *data) |
| 537 | { |
| 538 | struct fio_option *o; |
| 539 | |
| 540 | o = find_option(options, opt); |
| 541 | if (!o) { |
Jens Axboe | 43c129b | 2008-12-08 14:06:42 +0100 | [diff] [blame] | 542 | fprintf(stderr, "Bad option <%s>\n", opt); |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 543 | return 1; |
| 544 | } |
| 545 | |
Jens Axboe | b1508cf | 2006-11-02 09:12:40 +0100 | [diff] [blame] | 546 | if (!handle_option(o, val, data)) |
| 547 | return 0; |
| 548 | |
| 549 | fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val); |
| 550 | return 1; |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 551 | } |
| 552 | |
Aaron Carroll | 88b5a39 | 2008-10-07 11:25:20 +0200 | [diff] [blame] | 553 | /* |
| 554 | * Return a copy of the input string with substrings of the form ${VARNAME} |
| 555 | * substituted with the value of the environment variable VARNAME. The |
| 556 | * substitution always occurs, even if VARNAME is empty or the corresponding |
| 557 | * environment variable undefined. |
| 558 | */ |
| 559 | static char *option_dup_subs(const char *opt) |
| 560 | { |
| 561 | char out[OPT_LEN_MAX+1]; |
| 562 | char in[OPT_LEN_MAX+1]; |
| 563 | char *outptr = out; |
| 564 | char *inptr = in; |
| 565 | char *ch1, *ch2, *env; |
| 566 | ssize_t nchr = OPT_LEN_MAX; |
| 567 | size_t envlen; |
| 568 | |
| 569 | in[OPT_LEN_MAX] = '\0'; |
| 570 | strncpy(in, opt, OPT_LEN_MAX); |
| 571 | |
| 572 | while (*inptr && nchr > 0) { |
| 573 | if (inptr[0] == '$' && inptr[1] == '{') { |
| 574 | ch2 = strchr(inptr, '}'); |
| 575 | if (ch2 && inptr+1 < ch2) { |
| 576 | ch1 = inptr+2; |
| 577 | inptr = ch2+1; |
| 578 | *ch2 = '\0'; |
| 579 | |
| 580 | env = getenv(ch1); |
| 581 | if (env) { |
| 582 | envlen = strlen(env); |
| 583 | if (envlen <= nchr) { |
| 584 | memcpy(outptr, env, envlen); |
| 585 | outptr += envlen; |
| 586 | nchr -= envlen; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | continue; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | *outptr++ = *inptr++; |
| 595 | --nchr; |
| 596 | } |
| 597 | |
| 598 | *outptr = '\0'; |
| 599 | return strdup(out); |
| 600 | } |
| 601 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 602 | int parse_option(const char *opt, struct fio_option *options, void *data) |
| 603 | { |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 604 | struct fio_option *o; |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 605 | char *post, *tmp; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 606 | |
Aaron Carroll | 88b5a39 | 2008-10-07 11:25:20 +0200 | [diff] [blame] | 607 | tmp = option_dup_subs(opt); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 608 | |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 609 | o = get_option(tmp, options, &post); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 610 | if (!o) { |
Jens Axboe | 43c129b | 2008-12-08 14:06:42 +0100 | [diff] [blame] | 611 | fprintf(stderr, "Bad option <%s>\n", tmp); |
Jens Axboe | 0401bca | 2007-03-29 11:00:43 +0200 | [diff] [blame] | 612 | free(tmp); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 613 | return 1; |
| 614 | } |
| 615 | |
Jens Axboe | 0401bca | 2007-03-29 11:00:43 +0200 | [diff] [blame] | 616 | if (!handle_option(o, post, data)) { |
| 617 | free(tmp); |
Jens Axboe | b1508cf | 2006-11-02 09:12:40 +0100 | [diff] [blame] | 618 | return 0; |
Jens Axboe | 0401bca | 2007-03-29 11:00:43 +0200 | [diff] [blame] | 619 | } |
Jens Axboe | b1508cf | 2006-11-02 09:12:40 +0100 | [diff] [blame] | 620 | |
| 621 | fprintf(stderr, "fio: failed parsing %s\n", opt); |
Jens Axboe | 0401bca | 2007-03-29 11:00:43 +0200 | [diff] [blame] | 622 | free(tmp); |
Jens Axboe | b1508cf | 2006-11-02 09:12:40 +0100 | [diff] [blame] | 623 | return 1; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 624 | } |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 625 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 626 | /* |
| 627 | * Option match, levenshtein distance. Handy for not quite remembering what |
| 628 | * the option name is. |
| 629 | */ |
| 630 | static int string_distance(const char *s1, const char *s2) |
| 631 | { |
| 632 | unsigned int s1_len = strlen(s1); |
| 633 | unsigned int s2_len = strlen(s2); |
| 634 | unsigned int *p, *q, *r; |
| 635 | unsigned int i, j; |
| 636 | |
| 637 | p = malloc(sizeof(unsigned int) * (s2_len + 1)); |
| 638 | q = malloc(sizeof(unsigned int) * (s2_len + 1)); |
| 639 | |
| 640 | p[0] = 0; |
| 641 | for (i = 1; i <= s2_len; i++) |
| 642 | p[i] = p[i - 1] + 1; |
| 643 | |
| 644 | for (i = 1; i <= s1_len; i++) { |
| 645 | q[0] = p[0] + 1; |
| 646 | for (j = 1; j <= s2_len; j++) { |
| 647 | unsigned int sub = p[j - 1]; |
| 648 | |
| 649 | if (s1[i - 1] != s2[j - 1]) |
| 650 | sub++; |
| 651 | |
| 652 | q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub)); |
| 653 | } |
| 654 | r = p; |
| 655 | p = q; |
| 656 | q = r; |
| 657 | } |
| 658 | |
| 659 | i = p[s2_len]; |
| 660 | free(p); |
| 661 | free(q); |
| 662 | return i; |
| 663 | } |
| 664 | |
| 665 | static void show_option_help(struct fio_option *o) |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 666 | { |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 667 | const char *typehelp[] = { |
| 668 | "string (opt=bla)", |
| 669 | "string with possible k/m/g postfix (opt=4k)", |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 670 | "string with time postfix (opt=10s)", |
| 671 | "string (opt=bla)", |
| 672 | "string with dual range (opt=1k-4k,4k-8k)", |
| 673 | "integer value (opt=100)", |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 674 | "boolean value (opt=1)", |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 675 | "no argument (opt)", |
| 676 | }; |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 677 | |
Jens Axboe | d2bb7fe | 2007-03-15 14:11:38 +0100 | [diff] [blame] | 678 | if (o->alias) |
| 679 | printf("%20s: %s\n", "alias", o->alias); |
| 680 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 681 | printf("%20s: %s\n", "type", typehelp[o->type]); |
| 682 | printf("%20s: %s\n", "default", o->def ? o->def : "no default"); |
| 683 | show_option_range(o); |
| 684 | show_option_values(o); |
| 685 | } |
| 686 | |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 687 | static struct fio_option *find_child(struct fio_option *options, |
| 688 | struct fio_option *o) |
| 689 | { |
| 690 | struct fio_option *__o; |
| 691 | |
Jens Axboe | fdf2874 | 2007-07-31 23:06:09 +0200 | [diff] [blame] | 692 | for (__o = options + 1; __o->name; __o++) |
| 693 | if (__o->parent && !strcmp(__o->parent, o->name)) |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 694 | return __o; |
| 695 | |
| 696 | return NULL; |
| 697 | } |
| 698 | |
Jens Axboe | 323d911 | 2008-03-01 15:12:14 +0100 | [diff] [blame] | 699 | static void __print_option(struct fio_option *o, struct fio_option *org, |
| 700 | int level) |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 701 | { |
| 702 | char name[256], *p; |
Jens Axboe | 323d911 | 2008-03-01 15:12:14 +0100 | [diff] [blame] | 703 | int depth; |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 704 | |
| 705 | if (!o) |
| 706 | return; |
Jens Axboe | ef9aff5 | 2007-07-31 22:56:53 +0200 | [diff] [blame] | 707 | if (!org) |
| 708 | org = o; |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 709 | |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 710 | p = name; |
Jens Axboe | 323d911 | 2008-03-01 15:12:14 +0100 | [diff] [blame] | 711 | depth = level; |
| 712 | while (depth--) |
| 713 | p += sprintf(p, "%s", " "); |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 714 | |
| 715 | sprintf(p, "%s", o->name); |
| 716 | |
| 717 | printf("%-24s: %s\n", name, o->help); |
Jens Axboe | 323d911 | 2008-03-01 15:12:14 +0100 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | static void print_option(struct fio_option *o) |
| 721 | { |
| 722 | struct fio_option *parent; |
| 723 | struct fio_option *__o; |
| 724 | unsigned int printed; |
| 725 | unsigned int level; |
| 726 | |
| 727 | __print_option(o, NULL, 0); |
| 728 | parent = o; |
| 729 | level = 0; |
| 730 | do { |
| 731 | level++; |
| 732 | printed = 0; |
| 733 | |
| 734 | while ((__o = find_child(o, parent)) != NULL) { |
| 735 | __print_option(__o, o, level); |
| 736 | o = __o; |
| 737 | printed++; |
| 738 | } |
| 739 | |
| 740 | parent = o; |
| 741 | } while (printed); |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 742 | } |
| 743 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 744 | int show_cmd_help(struct fio_option *options, const char *name) |
| 745 | { |
| 746 | struct fio_option *o, *closest; |
| 747 | unsigned int best_dist; |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 748 | int found = 0; |
Jens Axboe | 320beef | 2007-03-01 10:44:12 +0100 | [diff] [blame] | 749 | int show_all = 0; |
| 750 | |
| 751 | if (!name || !strcmp(name, "all")) |
| 752 | show_all = 1; |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 753 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 754 | closest = NULL; |
| 755 | best_dist = -1; |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 756 | for (o = &options[0]; o->name; o++) { |
Jens Axboe | 320beef | 2007-03-01 10:44:12 +0100 | [diff] [blame] | 757 | int match = 0; |
| 758 | |
Jens Axboe | 15ca150 | 2008-04-07 09:26:02 +0200 | [diff] [blame] | 759 | if (o->type == FIO_OPT_DEPRECATED) |
| 760 | continue; |
| 761 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 762 | if (name) { |
Jens Axboe | 7f9348f | 2007-03-15 14:09:28 +0100 | [diff] [blame] | 763 | if (!strcmp(name, o->name) || |
| 764 | (o->alias && !strcmp(name, o->alias))) |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 765 | match = 1; |
| 766 | else { |
| 767 | unsigned int dist; |
| 768 | |
| 769 | dist = string_distance(name, o->name); |
| 770 | if (dist < best_dist) { |
| 771 | best_dist = dist; |
| 772 | closest = o; |
| 773 | } |
| 774 | } |
| 775 | } |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 776 | |
| 777 | if (show_all || match) { |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 778 | found = 1; |
Jens Axboe | c167ded | 2007-03-14 13:02:53 +0100 | [diff] [blame] | 779 | if (match) |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 780 | printf("%24s: %s\n", o->name, o->help); |
Jens Axboe | c167ded | 2007-03-14 13:02:53 +0100 | [diff] [blame] | 781 | if (show_all) { |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 782 | if (!o->parent) |
Jens Axboe | 323d911 | 2008-03-01 15:12:14 +0100 | [diff] [blame] | 783 | print_option(o); |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 784 | continue; |
Jens Axboe | c167ded | 2007-03-14 13:02:53 +0100 | [diff] [blame] | 785 | } |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 786 | } |
| 787 | |
Jens Axboe | 70df2f1 | 2007-01-11 14:04:47 +0100 | [diff] [blame] | 788 | if (!match) |
| 789 | continue; |
| 790 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 791 | show_option_help(o); |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 792 | } |
| 793 | |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 794 | if (found) |
| 795 | return 0; |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 796 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 797 | printf("No such command: %s", name); |
| 798 | if (closest) { |
| 799 | printf(" - showing closest match\n"); |
| 800 | printf("%20s: %s\n", closest->name, closest->help); |
| 801 | show_option_help(closest); |
| 802 | } else |
| 803 | printf("\n"); |
| 804 | |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 805 | return 1; |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 806 | } |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 807 | |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 808 | /* |
| 809 | * Handle parsing of default parameters. |
| 810 | */ |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 811 | void fill_default_options(void *data, struct fio_option *options) |
| 812 | { |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 813 | struct fio_option *o; |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 814 | |
Jens Axboe | a3d741f | 2008-02-27 18:32:33 +0100 | [diff] [blame] | 815 | dprint(FD_PARSE, "filling default options\n"); |
| 816 | |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 817 | for (o = &options[0]; o->name; o++) |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 818 | if (o->def) |
| 819 | handle_option(o, o->def, data); |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 820 | } |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 821 | |
| 822 | /* |
| 823 | * 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] | 824 | * values and whether both callback and offsets are given. |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 825 | */ |
| 826 | void options_init(struct fio_option *options) |
| 827 | { |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 828 | struct fio_option *o; |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 829 | |
Jens Axboe | a3d741f | 2008-02-27 18:32:33 +0100 | [diff] [blame] | 830 | dprint(FD_PARSE, "init options\n"); |
| 831 | |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 832 | for (o = &options[0]; o->name; o++) { |
Jens Axboe | 15ca150 | 2008-04-07 09:26:02 +0200 | [diff] [blame] | 833 | if (o->type == FIO_OPT_DEPRECATED) |
| 834 | continue; |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 835 | if (o->type == FIO_OPT_BOOL) { |
| 836 | o->minval = 0; |
| 837 | o->maxval = 1; |
| 838 | } |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 839 | if (o->type == FIO_OPT_STR_SET && o->def) { |
| 840 | fprintf(stderr, "Option %s: string set option with" |
| 841 | " default will always be true\n", |
| 842 | o->name); |
| 843 | } |
| 844 | if (!o->cb && !o->off1) { |
| 845 | fprintf(stderr, "Option %s: neither cb nor offset" |
| 846 | " given\n", o->name); |
| 847 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 848 | if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE) |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 849 | continue; |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 850 | if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) { |
| 851 | fprintf(stderr, "Option %s: both cb and offset given\n", |
| 852 | o->name); |
| 853 | } |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 854 | } |
| 855 | } |