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