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