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 | 33963c6 | 2006-10-27 11:33:01 +0200 | [diff] [blame] | 130 | struct fio_option *o = &options[0]; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 131 | |
Jens Axboe | 33963c6 | 2006-10-27 11:33:01 +0200 | [diff] [blame] | 132 | while (o->name) { |
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 | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 137 | |
Jens Axboe | 33963c6 | 2006-10-27 11:33:01 +0200 | [diff] [blame] | 138 | o++; |
| 139 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 140 | |
| 141 | return NULL; |
| 142 | } |
| 143 | |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 144 | #define val_store(ptr, val, off, data) \ |
| 145 | do { \ |
| 146 | ptr = td_var((data), (off)); \ |
| 147 | *ptr = (val); \ |
| 148 | } while (0) |
| 149 | |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 150 | 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] | 151 | int first, int more) |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 152 | { |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 153 | int il, *ilp; |
| 154 | long long ull, *ullp; |
| 155 | long ul1, ul2; |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 156 | char **cp; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 157 | int ret = 0, is_time = 0; |
| 158 | |
Jens Axboe | 08e26e3 | 2006-11-21 13:15:10 +0100 | [diff] [blame] | 159 | if (!ptr && o->type != FIO_OPT_STR_SET) { |
| 160 | fprintf(stderr, "Option %s requires an argument\n", o->name); |
| 161 | return 1; |
| 162 | } |
| 163 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 164 | switch (o->type) { |
| 165 | case FIO_OPT_STR: { |
| 166 | fio_opt_str_fn *fn = o->cb; |
| 167 | |
| 168 | ret = fn(data, ptr); |
| 169 | break; |
| 170 | } |
| 171 | case FIO_OPT_STR_VAL_TIME: |
| 172 | is_time = 1; |
Jens Axboe | 75e6f36 | 2006-11-03 08:17:09 +0100 | [diff] [blame] | 173 | case FIO_OPT_STR_VAL: |
| 174 | case FIO_OPT_STR_VAL_INT: { |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 175 | fio_opt_str_val_fn *fn = o->cb; |
| 176 | |
| 177 | if (is_time) |
| 178 | ret = check_str_time(ptr, &ull); |
| 179 | else |
| 180 | ret = check_str_bytes(ptr, &ull); |
| 181 | |
| 182 | if (ret) |
| 183 | break; |
| 184 | |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 185 | if (o->maxval && ull > o->maxval) { |
| 186 | fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval); |
| 187 | return 1; |
| 188 | } |
| 189 | if (o->minval && ull < o->minval) { |
| 190 | fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval); |
| 191 | return 1; |
| 192 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 193 | |
| 194 | if (fn) |
| 195 | ret = fn(data, &ull); |
| 196 | else { |
Jens Axboe | 75e6f36 | 2006-11-03 08:17:09 +0100 | [diff] [blame] | 197 | if (o->type == FIO_OPT_STR_VAL_INT) { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 198 | if (first) |
| 199 | val_store(ilp, ull, o->off1, data); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 200 | if (!more && o->off2) |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 201 | val_store(ilp, ull, o->off2, data); |
Jens Axboe | 75e6f36 | 2006-11-03 08:17:09 +0100 | [diff] [blame] | 202 | } else { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 203 | if (first) |
| 204 | val_store(ullp, ull, o->off1, data); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 205 | if (!more && o->off2) |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 206 | val_store(ullp, ull, o->off2, data); |
Jens Axboe | 75e6f36 | 2006-11-03 08:17:09 +0100 | [diff] [blame] | 207 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 208 | } |
| 209 | break; |
| 210 | } |
| 211 | case FIO_OPT_STR_STORE: |
| 212 | cp = td_var(data, o->off1); |
| 213 | *cp = strdup(ptr); |
| 214 | break; |
| 215 | case FIO_OPT_RANGE: { |
Jens Axboe | b765a37 | 2006-10-27 15:00:16 +0200 | [diff] [blame] | 216 | char tmp[128]; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 217 | char *p1, *p2; |
| 218 | |
Jens Axboe | 0bbab0e | 2006-11-02 09:18:36 +0100 | [diff] [blame] | 219 | strncpy(tmp, ptr, sizeof(tmp) - 1); |
Jens Axboe | b765a37 | 2006-10-27 15:00:16 +0200 | [diff] [blame] | 220 | |
| 221 | p1 = strchr(tmp, '-'); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 222 | if (!p1) { |
| 223 | ret = 1; |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | p2 = p1 + 1; |
| 228 | *p1 = '\0'; |
Jens Axboe | b765a37 | 2006-10-27 15:00:16 +0200 | [diff] [blame] | 229 | p1 = tmp; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 230 | |
| 231 | ret = 1; |
| 232 | if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) { |
| 233 | ret = 0; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 234 | if (ul1 > ul2) { |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 235 | unsigned long foo = ul1; |
| 236 | |
| 237 | ul1 = ul2; |
| 238 | ul2 = foo; |
| 239 | } |
| 240 | |
| 241 | if (first) { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 242 | val_store(ilp, ul1, o->off1, data); |
| 243 | val_store(ilp, ul2, o->off2, data); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 244 | } |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 245 | if (!more && o->off3 && o->off4) { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 246 | val_store(ilp, ul1, o->off3, data); |
| 247 | val_store(ilp, ul2, o->off4, data); |
| 248 | } |
| 249 | } |
| 250 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 251 | break; |
| 252 | } |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 253 | case FIO_OPT_INT: |
| 254 | case FIO_OPT_BOOL: { |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 255 | fio_opt_int_fn *fn = o->cb; |
| 256 | |
| 257 | ret = check_int(ptr, &il); |
| 258 | if (ret) |
| 259 | break; |
| 260 | |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 261 | if (o->maxval && il > (int) o->maxval) { |
| 262 | fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval); |
| 263 | return 1; |
| 264 | } |
| 265 | if (o->minval && il < o->minval) { |
| 266 | fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval); |
| 267 | return 1; |
| 268 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 269 | |
| 270 | if (fn) |
| 271 | ret = fn(data, &il); |
| 272 | else { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 273 | if (first) |
| 274 | val_store(ilp, il, o->off1, data); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 275 | if (!more && o->off2) |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 276 | val_store(ilp, il, o->off2, data); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 277 | } |
| 278 | break; |
| 279 | } |
| 280 | case FIO_OPT_STR_SET: { |
| 281 | fio_opt_str_set_fn *fn = o->cb; |
| 282 | |
| 283 | if (fn) |
| 284 | ret = fn(data); |
| 285 | else { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 286 | if (first) |
| 287 | val_store(ilp, 1, o->off1, data); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 288 | if (!more && o->off2) |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 289 | val_store(ilp, 1, o->off2, data); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 290 | } |
| 291 | break; |
| 292 | } |
| 293 | default: |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 294 | fprintf(stderr, "Bad option type %u\n", o->type); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 295 | ret = 1; |
| 296 | } |
| 297 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 298 | return ret; |
| 299 | } |
| 300 | |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 301 | static int handle_option(struct fio_option *o, const char *ptr, void *data) |
| 302 | { |
Jens Axboe | 92b586f | 2006-11-07 14:29:17 +0100 | [diff] [blame] | 303 | const char *ptr2 = NULL; |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 304 | int r1, r2; |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 305 | |
| 306 | /* |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 307 | * See if we have a second set of parameters, hidden after a comma. |
| 308 | * Do this before parsing the first round, to check if we should |
| 309 | * copy set 1 options to set 2. |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 310 | */ |
Jens Axboe | 92b586f | 2006-11-07 14:29:17 +0100 | [diff] [blame] | 311 | if (ptr) |
| 312 | ptr2 = strchr(ptr, ','); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 313 | |
| 314 | /* |
| 315 | * Don't return early if parsing the first option fails - if |
| 316 | * we are doing multiple arguments, we can allow the first one |
| 317 | * being empty. |
| 318 | */ |
| 319 | r1 = __handle_option(o, ptr, data, 1, !!ptr2); |
| 320 | |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 321 | if (!ptr2) |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 322 | return r1; |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 323 | |
| 324 | ptr2++; |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 325 | r2 = __handle_option(o, ptr2, data, 0, 0); |
| 326 | |
| 327 | return r1 && r2; |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 328 | } |
| 329 | |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 330 | int parse_cmd_option(const char *opt, const char *val, |
| 331 | struct fio_option *options, void *data) |
| 332 | { |
| 333 | struct fio_option *o; |
| 334 | |
| 335 | o = find_option(options, opt); |
| 336 | if (!o) { |
| 337 | fprintf(stderr, "Bad option %s\n", opt); |
| 338 | return 1; |
| 339 | } |
| 340 | |
Jens Axboe | b1508cf | 2006-11-02 09:12:40 +0100 | [diff] [blame] | 341 | if (!handle_option(o, val, data)) |
| 342 | return 0; |
| 343 | |
| 344 | fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val); |
| 345 | return 1; |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 346 | } |
| 347 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 348 | int parse_option(const char *opt, struct fio_option *options, void *data) |
| 349 | { |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 350 | struct fio_option *o; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 351 | char *pre, *post; |
| 352 | char tmp[64]; |
| 353 | |
Jens Axboe | 0bbab0e | 2006-11-02 09:18:36 +0100 | [diff] [blame] | 354 | strncpy(tmp, opt, sizeof(tmp) - 1); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 355 | |
| 356 | pre = strchr(tmp, '='); |
| 357 | if (pre) { |
| 358 | post = pre; |
| 359 | *pre = '\0'; |
| 360 | pre = tmp; |
| 361 | post++; |
| 362 | o = find_option(options, pre); |
| 363 | } else { |
| 364 | o = find_option(options, tmp); |
| 365 | post = NULL; |
| 366 | } |
| 367 | |
| 368 | if (!o) { |
| 369 | fprintf(stderr, "Bad option %s\n", tmp); |
| 370 | return 1; |
| 371 | } |
| 372 | |
Jens Axboe | b1508cf | 2006-11-02 09:12:40 +0100 | [diff] [blame] | 373 | if (!handle_option(o, post, data)) |
| 374 | return 0; |
| 375 | |
| 376 | fprintf(stderr, "fio: failed parsing %s\n", opt); |
| 377 | return 1; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 378 | } |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 379 | |
Jens Axboe | 15f7918 | 2007-01-10 12:26:18 +0100 | [diff] [blame] | 380 | static void show_option_range(struct fio_option *o) |
| 381 | { |
| 382 | if (!o->minval && !o->maxval) |
| 383 | return; |
| 384 | |
| 385 | printf("%16s: min=%d, max=%d\n", "range", o->minval, o->maxval); |
| 386 | } |
| 387 | |
| 388 | static void show_option_values(struct fio_option *o) |
| 389 | { |
| 390 | const char *msg; |
| 391 | int i = 0; |
| 392 | |
| 393 | if (!o->posval) |
| 394 | return; |
| 395 | |
| 396 | do { |
| 397 | msg = o->posval[i]; |
| 398 | if (!msg) |
| 399 | break; |
| 400 | |
| 401 | if (!i) |
| 402 | printf("%16s: ", "valid values"); |
| 403 | |
| 404 | printf("%s,", msg); |
| 405 | i++; |
| 406 | } while (1); |
| 407 | |
| 408 | if (i) |
| 409 | printf("\n"); |
| 410 | } |
| 411 | |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 412 | int show_cmd_help(struct fio_option *options, const char *name) |
| 413 | { |
| 414 | int show_all = !strcmp(name, "all"); |
| 415 | struct fio_option *o = &options[0]; |
| 416 | const char *typehelp[] = { |
| 417 | "string (opt=bla)", |
| 418 | "string with possible k/m/g postfix (opt=4k)", |
| 419 | "string with range and postfix (opt=1k-4k)", |
| 420 | "string with time postfix (opt=10s)", |
| 421 | "string (opt=bla)", |
| 422 | "string with dual range (opt=1k-4k,4k-8k)", |
| 423 | "integer value (opt=100)", |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 424 | "boolean value (opt=1)", |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 425 | "no argument (opt)", |
| 426 | }; |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 427 | int found = 0; |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 428 | |
| 429 | while (o->name) { |
| 430 | int match = !strcmp(name, o->name); |
| 431 | |
| 432 | if (show_all || match) { |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 433 | found = 1; |
Jens Axboe | facba0e | 2007-01-10 11:41:11 +0100 | [diff] [blame] | 434 | printf("%16s: %s\n", o->name, o->help); |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 435 | if (match) { |
Jens Axboe | facba0e | 2007-01-10 11:41:11 +0100 | [diff] [blame] | 436 | printf("%16s: %s\n", "type", typehelp[o->type]); |
| 437 | printf("%16s: %s\n", "default", o->def ? o->def : "no default"); |
Jens Axboe | 15f7918 | 2007-01-10 12:26:18 +0100 | [diff] [blame] | 438 | show_option_range(o); |
| 439 | show_option_values(o); |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 440 | } |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | o++; |
| 444 | } |
| 445 | |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 446 | if (found) |
| 447 | return 0; |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 448 | |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 449 | printf("No such command: %s\n", name); |
| 450 | return 1; |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 451 | } |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 452 | |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 453 | /* |
| 454 | * Handle parsing of default parameters. |
| 455 | */ |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 456 | void fill_default_options(void *data, struct fio_option *options) |
| 457 | { |
| 458 | struct fio_option *o = &options[0]; |
| 459 | |
| 460 | while (o->name) { |
| 461 | if (o->def) |
| 462 | handle_option(o, o->def, data); |
| 463 | o++; |
| 464 | } |
| 465 | } |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 466 | |
| 467 | /* |
| 468 | * Sanitize the options structure. For now it just sets min/max for bool |
| 469 | * values. |
| 470 | */ |
| 471 | void options_init(struct fio_option *options) |
| 472 | { |
| 473 | struct fio_option *o = &options[0]; |
| 474 | |
| 475 | while (o->name) { |
| 476 | if (o->type == FIO_OPT_BOOL) { |
| 477 | o->minval = 0; |
| 478 | o->maxval = 1; |
| 479 | } |
| 480 | o++; |
| 481 | } |
| 482 | } |