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