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> |
| 11 | |
| 12 | #include "parse.h" |
| 13 | |
Jens Axboe | f085737 | 2007-03-19 13:15:23 +0100 | [diff] [blame] | 14 | static int vp_cmp(const void *p1, const void *p2) |
| 15 | { |
| 16 | const struct value_pair *vp1 = p1; |
| 17 | const struct value_pair *vp2 = p2; |
| 18 | |
| 19 | return strlen(vp2->ival) - strlen(vp1->ival); |
| 20 | } |
| 21 | |
| 22 | static void posval_sort(struct fio_option *o, struct value_pair *vpmap) |
| 23 | { |
| 24 | const struct value_pair *vp; |
| 25 | int entries; |
| 26 | |
| 27 | memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair)); |
| 28 | |
| 29 | for (entries = 0; entries < PARSE_MAX_VP; entries++) { |
| 30 | vp = &o->posval[entries]; |
| 31 | if (!vp->ival || vp->ival[0] == '\0') |
| 32 | break; |
| 33 | |
| 34 | memcpy(&vpmap[entries], vp, sizeof(*vp)); |
| 35 | } |
| 36 | |
| 37 | qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp); |
| 38 | } |
| 39 | |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 40 | static void show_option_range(struct fio_option *o) |
| 41 | { |
| 42 | if (!o->minval && !o->maxval) |
| 43 | return; |
| 44 | |
Jens Axboe | ec1aee0 | 2007-02-26 13:18:47 +0100 | [diff] [blame] | 45 | printf("%20s: min=%d, max=%d\n", "range", o->minval, o->maxval); |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static void show_option_values(struct fio_option *o) |
| 49 | { |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 50 | int i = 0; |
| 51 | |
| 52 | do { |
Jens Axboe | 7837213 | 2007-03-14 13:24:07 +0100 | [diff] [blame] | 53 | const struct value_pair *vp = &o->posval[i]; |
| 54 | |
| 55 | if (!vp->ival) |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 56 | break; |
| 57 | |
Jens Axboe | 7837213 | 2007-03-14 13:24:07 +0100 | [diff] [blame] | 58 | printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival); |
| 59 | if (vp->help) |
| 60 | printf(" %s", vp->help); |
| 61 | printf("\n"); |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 62 | i++; |
| 63 | } while (i < PARSE_MAX_VP); |
| 64 | |
| 65 | if (i) |
| 66 | printf("\n"); |
| 67 | } |
| 68 | |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 69 | static unsigned long get_mult_time(char c) |
| 70 | { |
| 71 | switch (c) { |
| 72 | case 'm': |
| 73 | case 'M': |
| 74 | return 60; |
| 75 | case 'h': |
| 76 | case 'H': |
| 77 | return 60 * 60; |
| 78 | case 'd': |
| 79 | case 'D': |
| 80 | return 24 * 60 * 60; |
| 81 | default: |
| 82 | return 1; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static unsigned long get_mult_bytes(char c) |
| 87 | { |
| 88 | switch (c) { |
| 89 | case 'k': |
| 90 | case 'K': |
| 91 | return 1024; |
| 92 | case 'm': |
| 93 | case 'M': |
| 94 | return 1024 * 1024; |
| 95 | case 'g': |
| 96 | case 'G': |
| 97 | return 1024 * 1024 * 1024; |
Jens Axboe | f3502ba | 2007-02-14 01:27:09 +0100 | [diff] [blame] | 98 | case 'e': |
| 99 | case 'E': |
| 100 | return 1024 * 1024 * 1024 * 1024UL; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 101 | default: |
| 102 | return 1; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /* |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 107 | * convert string into decimal value, noting any size suffix |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 108 | */ |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 109 | static int str_to_decimal(const char *str, long long *val, int kilo) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 110 | { |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 111 | int len; |
| 112 | |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 113 | len = strlen(str); |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 114 | if (!len) |
| 115 | return 1; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 116 | |
Jens Axboe | 675de85 | 2007-02-10 19:01:07 +0100 | [diff] [blame] | 117 | *val = strtoll(str, NULL, 10); |
Jens Axboe | cda866c | 2007-01-10 16:02:32 +0100 | [diff] [blame] | 118 | if (*val == LONG_MAX && errno == ERANGE) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 119 | return 1; |
| 120 | |
| 121 | if (kilo) |
| 122 | *val *= get_mult_bytes(str[len - 1]); |
| 123 | else |
| 124 | *val *= get_mult_time(str[len - 1]); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 125 | |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 129 | static int check_str_bytes(const char *p, long long *val) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 130 | { |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 131 | return str_to_decimal(p, val, 1); |
| 132 | } |
| 133 | |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 134 | static int check_str_time(const char *p, long long *val) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 135 | { |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 136 | return str_to_decimal(p, val, 0); |
| 137 | } |
| 138 | |
| 139 | void strip_blank_front(char **p) |
| 140 | { |
| 141 | char *s = *p; |
| 142 | |
| 143 | while (isspace(*s)) |
| 144 | s++; |
| 145 | } |
| 146 | |
| 147 | void strip_blank_end(char *p) |
| 148 | { |
| 149 | char *s = p + strlen(p) - 1; |
| 150 | |
| 151 | while (isspace(*s) || iscntrl(*s)) |
| 152 | s--; |
| 153 | |
| 154 | *(s + 1) = '\0'; |
| 155 | } |
| 156 | |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 157 | static int check_range_bytes(const char *str, long *val) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 158 | { |
| 159 | char suffix; |
| 160 | |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 161 | if (!strlen(str)) |
| 162 | return 1; |
| 163 | |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 164 | if (sscanf(str, "%lu%c", val, &suffix) == 2) { |
| 165 | *val *= get_mult_bytes(suffix); |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | if (sscanf(str, "%lu", val) == 1) |
| 170 | return 0; |
| 171 | |
| 172 | return 1; |
| 173 | } |
| 174 | |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 175 | static int check_int(const char *p, int *val) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 176 | { |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 177 | if (!strlen(p)) |
| 178 | return 1; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 179 | if (sscanf(p, "%u", val) == 1) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 180 | return 0; |
| 181 | |
| 182 | return 1; |
| 183 | } |
| 184 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 185 | static struct fio_option *find_option(struct fio_option *options, |
| 186 | const char *opt) |
| 187 | { |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 188 | struct fio_option *o; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 189 | |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 190 | for (o = &options[0]; o->name; o++) { |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 191 | if (!strcmp(o->name, opt)) |
| 192 | return o; |
Jens Axboe | 03b74b3 | 2007-01-11 11:04:31 +0100 | [diff] [blame] | 193 | else if (o->alias && !strcmp(o->alias, opt)) |
| 194 | return o; |
Jens Axboe | 33963c6 | 2006-10-27 11:33:01 +0200 | [diff] [blame] | 195 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 196 | |
| 197 | return NULL; |
| 198 | } |
| 199 | |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 200 | #define val_store(ptr, val, off, data) \ |
| 201 | do { \ |
| 202 | ptr = td_var((data), (off)); \ |
| 203 | *ptr = (val); \ |
| 204 | } while (0) |
| 205 | |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 206 | 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] | 207 | int first, int more) |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 208 | { |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 209 | int il, *ilp; |
| 210 | long long ull, *ullp; |
| 211 | long ul1, ul2; |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 212 | char **cp; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 213 | int ret = 0, is_time = 0; |
| 214 | |
Jens Axboe | 08e26e3 | 2006-11-21 13:15:10 +0100 | [diff] [blame] | 215 | if (!ptr && o->type != FIO_OPT_STR_SET) { |
| 216 | fprintf(stderr, "Option %s requires an argument\n", o->name); |
| 217 | return 1; |
| 218 | } |
| 219 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 220 | switch (o->type) { |
| 221 | case FIO_OPT_STR: { |
| 222 | fio_opt_str_fn *fn = o->cb; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 223 | const struct value_pair *vp; |
Jens Axboe | f085737 | 2007-03-19 13:15:23 +0100 | [diff] [blame] | 224 | struct value_pair posval[PARSE_MAX_VP]; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 225 | int i; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 226 | |
Jens Axboe | f085737 | 2007-03-19 13:15:23 +0100 | [diff] [blame] | 227 | posval_sort(o, posval); |
| 228 | |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 229 | for (i = 0; i < PARSE_MAX_VP; i++) { |
Jens Axboe | f085737 | 2007-03-19 13:15:23 +0100 | [diff] [blame] | 230 | vp = &posval[i]; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 231 | if (!vp->ival || vp->ival[0] == '\0') |
| 232 | break; |
Jens Axboe | 6612a27 | 2007-03-13 08:54:06 +0100 | [diff] [blame] | 233 | ret = 1; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 234 | if (!strncmp(vp->ival, ptr, strlen(vp->ival))) { |
| 235 | ret = 0; |
| 236 | if (!o->off1) |
| 237 | break; |
| 238 | val_store(ilp, vp->oval, o->off1, data); |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (ret) |
| 244 | show_option_values(o); |
| 245 | else if (fn) |
| 246 | ret = fn(data, ptr); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 247 | break; |
| 248 | } |
| 249 | case FIO_OPT_STR_VAL_TIME: |
| 250 | is_time = 1; |
Jens Axboe | 75e6f36 | 2006-11-03 08:17:09 +0100 | [diff] [blame] | 251 | case FIO_OPT_STR_VAL: |
| 252 | case FIO_OPT_STR_VAL_INT: { |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 253 | fio_opt_str_val_fn *fn = o->cb; |
| 254 | |
| 255 | if (is_time) |
| 256 | ret = check_str_time(ptr, &ull); |
| 257 | else |
| 258 | ret = check_str_bytes(ptr, &ull); |
| 259 | |
| 260 | if (ret) |
| 261 | break; |
| 262 | |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 263 | if (o->maxval && ull > o->maxval) { |
| 264 | fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval); |
| 265 | return 1; |
| 266 | } |
| 267 | if (o->minval && ull < o->minval) { |
| 268 | fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval); |
| 269 | return 1; |
| 270 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 271 | |
| 272 | if (fn) |
| 273 | ret = fn(data, &ull); |
| 274 | else { |
Jens Axboe | 75e6f36 | 2006-11-03 08:17:09 +0100 | [diff] [blame] | 275 | if (o->type == FIO_OPT_STR_VAL_INT) { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 276 | if (first) |
| 277 | val_store(ilp, ull, o->off1, data); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 278 | if (!more && o->off2) |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 279 | val_store(ilp, ull, o->off2, data); |
Jens Axboe | 75e6f36 | 2006-11-03 08:17:09 +0100 | [diff] [blame] | 280 | } else { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 281 | if (first) |
| 282 | val_store(ullp, ull, o->off1, data); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 283 | if (!more && o->off2) |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 284 | val_store(ullp, ull, o->off2, data); |
Jens Axboe | 75e6f36 | 2006-11-03 08:17:09 +0100 | [diff] [blame] | 285 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 286 | } |
| 287 | break; |
| 288 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 289 | case FIO_OPT_STR_STORE: { |
| 290 | fio_opt_str_fn *fn = o->cb; |
| 291 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 292 | cp = td_var(data, o->off1); |
| 293 | *cp = strdup(ptr); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 294 | if (fn) { |
| 295 | ret = fn(data, ptr); |
| 296 | if (ret) { |
| 297 | free(*cp); |
| 298 | *cp = NULL; |
| 299 | } |
| 300 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 301 | break; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 302 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 303 | case FIO_OPT_RANGE: { |
Jens Axboe | b765a37 | 2006-10-27 15:00:16 +0200 | [diff] [blame] | 304 | char tmp[128]; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 305 | char *p1, *p2; |
| 306 | |
Jens Axboe | 0bbab0e | 2006-11-02 09:18:36 +0100 | [diff] [blame] | 307 | strncpy(tmp, ptr, sizeof(tmp) - 1); |
Jens Axboe | b765a37 | 2006-10-27 15:00:16 +0200 | [diff] [blame] | 308 | |
| 309 | p1 = strchr(tmp, '-'); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 310 | if (!p1) { |
Jens Axboe | 0c9baf9 | 2007-01-11 15:59:26 +0100 | [diff] [blame] | 311 | p1 = strchr(tmp, ':'); |
| 312 | if (!p1) { |
| 313 | ret = 1; |
| 314 | break; |
| 315 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | p2 = p1 + 1; |
| 319 | *p1 = '\0'; |
Jens Axboe | b765a37 | 2006-10-27 15:00:16 +0200 | [diff] [blame] | 320 | p1 = tmp; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 321 | |
| 322 | ret = 1; |
| 323 | if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) { |
| 324 | ret = 0; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 325 | if (ul1 > ul2) { |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 326 | unsigned long foo = ul1; |
| 327 | |
| 328 | ul1 = ul2; |
| 329 | ul2 = foo; |
| 330 | } |
| 331 | |
| 332 | if (first) { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 333 | val_store(ilp, ul1, o->off1, data); |
| 334 | val_store(ilp, ul2, o->off2, data); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 335 | } |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 336 | if (!more && o->off3 && o->off4) { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 337 | val_store(ilp, ul1, o->off3, data); |
| 338 | val_store(ilp, ul2, o->off4, data); |
| 339 | } |
| 340 | } |
| 341 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 342 | break; |
| 343 | } |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 344 | case FIO_OPT_INT: |
| 345 | case FIO_OPT_BOOL: { |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 346 | fio_opt_int_fn *fn = o->cb; |
| 347 | |
| 348 | ret = check_int(ptr, &il); |
| 349 | if (ret) |
| 350 | break; |
| 351 | |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 352 | if (o->maxval && il > (int) o->maxval) { |
| 353 | fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval); |
| 354 | return 1; |
| 355 | } |
| 356 | if (o->minval && il < o->minval) { |
| 357 | fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval); |
| 358 | return 1; |
| 359 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 360 | |
Jens Axboe | 76a43db | 2007-01-11 13:24:44 +0100 | [diff] [blame] | 361 | if (o->neg) |
| 362 | il = !il; |
| 363 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 364 | if (fn) |
| 365 | ret = fn(data, &il); |
| 366 | else { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 367 | if (first) |
| 368 | val_store(ilp, il, o->off1, data); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 369 | if (!more && o->off2) |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 370 | val_store(ilp, il, o->off2, data); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 371 | } |
| 372 | break; |
| 373 | } |
| 374 | case FIO_OPT_STR_SET: { |
| 375 | fio_opt_str_set_fn *fn = o->cb; |
| 376 | |
| 377 | if (fn) |
| 378 | ret = fn(data); |
| 379 | else { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 380 | if (first) |
| 381 | val_store(ilp, 1, o->off1, data); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 382 | if (!more && o->off2) |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 383 | val_store(ilp, 1, o->off2, data); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 384 | } |
| 385 | break; |
| 386 | } |
| 387 | default: |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 388 | fprintf(stderr, "Bad option type %u\n", o->type); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 389 | ret = 1; |
| 390 | } |
| 391 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 392 | return ret; |
| 393 | } |
| 394 | |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 395 | static int handle_option(struct fio_option *o, const char *ptr, void *data) |
| 396 | { |
Jens Axboe | 92b586f | 2006-11-07 14:29:17 +0100 | [diff] [blame] | 397 | const char *ptr2 = NULL; |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 398 | int r1, r2; |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 399 | |
| 400 | /* |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 401 | * See if we have a second set of parameters, hidden after a comma. |
| 402 | * Do this before parsing the first round, to check if we should |
| 403 | * copy set 1 options to set 2. |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 404 | */ |
Joel Becker | ad231bc | 2007-03-01 08:22:42 +0100 | [diff] [blame] | 405 | if (ptr && |
| 406 | (o->type != FIO_OPT_STR_STORE) && |
| 407 | (o->type != FIO_OPT_STR)) { |
Jens Axboe | 92b586f | 2006-11-07 14:29:17 +0100 | [diff] [blame] | 408 | ptr2 = strchr(ptr, ','); |
Jens Axboe | 0c9baf9 | 2007-01-11 15:59:26 +0100 | [diff] [blame] | 409 | if (!ptr2) |
| 410 | ptr2 = strchr(ptr, ':'); |
Jens Axboe | b486f76 | 2007-03-15 09:15:12 +0100 | [diff] [blame] | 411 | if (!ptr2) |
| 412 | ptr2 = strchr(ptr, '-'); |
Jens Axboe | 0c9baf9 | 2007-01-11 15:59:26 +0100 | [diff] [blame] | 413 | } |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 414 | |
| 415 | /* |
| 416 | * Don't return early if parsing the first option fails - if |
| 417 | * we are doing multiple arguments, we can allow the first one |
| 418 | * being empty. |
| 419 | */ |
| 420 | r1 = __handle_option(o, ptr, data, 1, !!ptr2); |
| 421 | |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 422 | if (!ptr2) |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 423 | return r1; |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 424 | |
| 425 | ptr2++; |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 426 | r2 = __handle_option(o, ptr2, data, 0, 0); |
| 427 | |
| 428 | return r1 && r2; |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 429 | } |
| 430 | |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 431 | int parse_cmd_option(const char *opt, const char *val, |
| 432 | struct fio_option *options, void *data) |
| 433 | { |
| 434 | struct fio_option *o; |
| 435 | |
| 436 | o = find_option(options, opt); |
| 437 | if (!o) { |
| 438 | fprintf(stderr, "Bad option %s\n", opt); |
| 439 | return 1; |
| 440 | } |
| 441 | |
Jens Axboe | b1508cf | 2006-11-02 09:12:40 +0100 | [diff] [blame] | 442 | if (!handle_option(o, val, data)) |
| 443 | return 0; |
| 444 | |
| 445 | fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val); |
| 446 | return 1; |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 447 | } |
| 448 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 449 | int parse_option(const char *opt, struct fio_option *options, void *data) |
| 450 | { |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 451 | struct fio_option *o; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 452 | char *pre, *post; |
| 453 | char tmp[64]; |
| 454 | |
Jens Axboe | 0bbab0e | 2006-11-02 09:18:36 +0100 | [diff] [blame] | 455 | strncpy(tmp, opt, sizeof(tmp) - 1); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 456 | |
| 457 | pre = strchr(tmp, '='); |
| 458 | if (pre) { |
| 459 | post = pre; |
| 460 | *pre = '\0'; |
| 461 | pre = tmp; |
| 462 | post++; |
| 463 | o = find_option(options, pre); |
| 464 | } else { |
| 465 | o = find_option(options, tmp); |
| 466 | post = NULL; |
| 467 | } |
| 468 | |
| 469 | if (!o) { |
| 470 | fprintf(stderr, "Bad option %s\n", tmp); |
| 471 | return 1; |
| 472 | } |
| 473 | |
Jens Axboe | b1508cf | 2006-11-02 09:12:40 +0100 | [diff] [blame] | 474 | if (!handle_option(o, post, data)) |
| 475 | return 0; |
| 476 | |
| 477 | fprintf(stderr, "fio: failed parsing %s\n", opt); |
| 478 | return 1; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 479 | } |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 480 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 481 | /* |
| 482 | * Option match, levenshtein distance. Handy for not quite remembering what |
| 483 | * the option name is. |
| 484 | */ |
| 485 | static int string_distance(const char *s1, const char *s2) |
| 486 | { |
| 487 | unsigned int s1_len = strlen(s1); |
| 488 | unsigned int s2_len = strlen(s2); |
| 489 | unsigned int *p, *q, *r; |
| 490 | unsigned int i, j; |
| 491 | |
| 492 | p = malloc(sizeof(unsigned int) * (s2_len + 1)); |
| 493 | q = malloc(sizeof(unsigned int) * (s2_len + 1)); |
| 494 | |
| 495 | p[0] = 0; |
| 496 | for (i = 1; i <= s2_len; i++) |
| 497 | p[i] = p[i - 1] + 1; |
| 498 | |
| 499 | for (i = 1; i <= s1_len; i++) { |
| 500 | q[0] = p[0] + 1; |
| 501 | for (j = 1; j <= s2_len; j++) { |
| 502 | unsigned int sub = p[j - 1]; |
| 503 | |
| 504 | if (s1[i - 1] != s2[j - 1]) |
| 505 | sub++; |
| 506 | |
| 507 | q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub)); |
| 508 | } |
| 509 | r = p; |
| 510 | p = q; |
| 511 | q = r; |
| 512 | } |
| 513 | |
| 514 | i = p[s2_len]; |
| 515 | free(p); |
| 516 | free(q); |
| 517 | return i; |
| 518 | } |
| 519 | |
| 520 | static void show_option_help(struct fio_option *o) |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 521 | { |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 522 | const char *typehelp[] = { |
| 523 | "string (opt=bla)", |
| 524 | "string with possible k/m/g postfix (opt=4k)", |
| 525 | "string with range and postfix (opt=1k-4k)", |
| 526 | "string with time postfix (opt=10s)", |
| 527 | "string (opt=bla)", |
| 528 | "string with dual range (opt=1k-4k,4k-8k)", |
| 529 | "integer value (opt=100)", |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 530 | "boolean value (opt=1)", |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 531 | "no argument (opt)", |
| 532 | }; |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 533 | |
Jens Axboe | d2bb7fe | 2007-03-15 14:11:38 +0100 | [diff] [blame] | 534 | if (o->alias) |
| 535 | printf("%20s: %s\n", "alias", o->alias); |
| 536 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 537 | printf("%20s: %s\n", "type", typehelp[o->type]); |
| 538 | printf("%20s: %s\n", "default", o->def ? o->def : "no default"); |
| 539 | show_option_range(o); |
| 540 | show_option_values(o); |
| 541 | } |
| 542 | |
| 543 | int show_cmd_help(struct fio_option *options, const char *name) |
| 544 | { |
| 545 | struct fio_option *o, *closest; |
| 546 | unsigned int best_dist; |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 547 | int found = 0; |
Jens Axboe | 320beef | 2007-03-01 10:44:12 +0100 | [diff] [blame] | 548 | int show_all = 0; |
| 549 | |
| 550 | if (!name || !strcmp(name, "all")) |
| 551 | show_all = 1; |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 552 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 553 | closest = NULL; |
| 554 | best_dist = -1; |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 555 | for (o = &options[0]; o->name; o++) { |
Jens Axboe | 320beef | 2007-03-01 10:44:12 +0100 | [diff] [blame] | 556 | int match = 0; |
| 557 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 558 | if (name) { |
Jens Axboe | 7f9348f | 2007-03-15 14:09:28 +0100 | [diff] [blame] | 559 | if (!strcmp(name, o->name) || |
| 560 | (o->alias && !strcmp(name, o->alias))) |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 561 | match = 1; |
| 562 | else { |
| 563 | unsigned int dist; |
| 564 | |
| 565 | dist = string_distance(name, o->name); |
| 566 | if (dist < best_dist) { |
| 567 | best_dist = dist; |
| 568 | closest = o; |
| 569 | } |
| 570 | } |
| 571 | } |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 572 | |
| 573 | if (show_all || match) { |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 574 | found = 1; |
Jens Axboe | c167ded | 2007-03-14 13:02:53 +0100 | [diff] [blame] | 575 | if (match) |
| 576 | printf("%20s: %s\n", o->name, o->help); |
| 577 | if (show_all) { |
| 578 | printf("%-20s: %s\n", o->name, o->help); |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 579 | continue; |
Jens Axboe | c167ded | 2007-03-14 13:02:53 +0100 | [diff] [blame] | 580 | } |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 581 | } |
| 582 | |
Jens Axboe | 70df2f1 | 2007-01-11 14:04:47 +0100 | [diff] [blame] | 583 | if (!match) |
| 584 | continue; |
| 585 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 586 | show_option_help(o); |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 587 | } |
| 588 | |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 589 | if (found) |
| 590 | return 0; |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 591 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 592 | printf("No such command: %s", name); |
| 593 | if (closest) { |
| 594 | printf(" - showing closest match\n"); |
| 595 | printf("%20s: %s\n", closest->name, closest->help); |
| 596 | show_option_help(closest); |
| 597 | } else |
| 598 | printf("\n"); |
| 599 | |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 600 | return 1; |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 601 | } |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 602 | |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 603 | /* |
| 604 | * Handle parsing of default parameters. |
| 605 | */ |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 606 | void fill_default_options(void *data, struct fio_option *options) |
| 607 | { |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 608 | struct fio_option *o; |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 609 | |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 610 | for (o = &options[0]; o->name; o++) |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 611 | if (o->def) |
| 612 | handle_option(o, o->def, data); |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 613 | } |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 614 | |
| 615 | /* |
| 616 | * 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] | 617 | * values and whether both callback and offsets are given. |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 618 | */ |
| 619 | void options_init(struct fio_option *options) |
| 620 | { |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 621 | struct fio_option *o; |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 622 | |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 623 | for (o = &options[0]; o->name; o++) { |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 624 | if (o->type == FIO_OPT_BOOL) { |
| 625 | o->minval = 0; |
| 626 | o->maxval = 1; |
| 627 | } |
Jens Axboe | 0f3e35e | 2007-03-20 14:28:34 +0100 | [diff] [blame] | 628 | if (o->type == FIO_OPT_STR_SET && o->def) |
| 629 | fprintf(stderr, "Option %s: string set option with default will always be true\n", o->name); |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 630 | if (!o->cb && !o->off1) |
| 631 | fprintf(stderr, "Option %s: neither cb nor offset given\n", o->name); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 632 | if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE) |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 633 | continue; |
Jens Axboe | 5b0a888 | 2007-01-11 14:40:27 +0100 | [diff] [blame] | 634 | if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) |
| 635 | fprintf(stderr, "Option %s: both cb and offset given\n", o->name); |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 636 | } |
| 637 | } |