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> |
Aaron Carroll | 88b5a39 | 2008-10-07 11:25:20 +0200 | [diff] [blame] | 11 | #include <stdlib.h> |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 12 | |
| 13 | #include "parse.h" |
Jens Axboe | a3d741f | 2008-02-27 18:32:33 +0100 | [diff] [blame] | 14 | #include "debug.h" |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 15 | |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 16 | static struct fio_option *fio_options; |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 17 | extern unsigned int fio_get_kb_base(void *); |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 18 | |
Jens Axboe | f085737 | 2007-03-19 13:15:23 +0100 | [diff] [blame] | 19 | static int vp_cmp(const void *p1, const void *p2) |
| 20 | { |
| 21 | const struct value_pair *vp1 = p1; |
| 22 | const struct value_pair *vp2 = p2; |
| 23 | |
| 24 | return strlen(vp2->ival) - strlen(vp1->ival); |
| 25 | } |
| 26 | |
| 27 | static void posval_sort(struct fio_option *o, struct value_pair *vpmap) |
| 28 | { |
| 29 | const struct value_pair *vp; |
| 30 | int entries; |
| 31 | |
| 32 | memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair)); |
| 33 | |
| 34 | for (entries = 0; entries < PARSE_MAX_VP; entries++) { |
| 35 | vp = &o->posval[entries]; |
| 36 | if (!vp->ival || vp->ival[0] == '\0') |
| 37 | break; |
| 38 | |
| 39 | memcpy(&vpmap[entries], vp, sizeof(*vp)); |
| 40 | } |
| 41 | |
| 42 | qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp); |
| 43 | } |
| 44 | |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 45 | static void show_option_range(struct fio_option *o, FILE *out) |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 46 | { |
| 47 | if (!o->minval && !o->maxval) |
| 48 | return; |
| 49 | |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 50 | fprintf(out, "%20s: min=%d", "range", o->minval); |
Jens Axboe | 3980127 | 2009-07-01 09:06:12 +0200 | [diff] [blame] | 51 | if (o->maxval) |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 52 | fprintf(out, ", max=%d", o->maxval); |
| 53 | fprintf(out, "\n"); |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static void show_option_values(struct fio_option *o) |
| 57 | { |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 58 | int i = 0; |
| 59 | |
| 60 | do { |
Jens Axboe | 7837213 | 2007-03-14 13:24:07 +0100 | [diff] [blame] | 61 | const struct value_pair *vp = &o->posval[i]; |
| 62 | |
| 63 | if (!vp->ival) |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 64 | break; |
| 65 | |
Jens Axboe | 7837213 | 2007-03-14 13:24:07 +0100 | [diff] [blame] | 66 | printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival); |
| 67 | if (vp->help) |
| 68 | printf(" %s", vp->help); |
| 69 | printf("\n"); |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 70 | i++; |
| 71 | } while (i < PARSE_MAX_VP); |
| 72 | |
| 73 | if (i) |
| 74 | printf("\n"); |
| 75 | } |
| 76 | |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 77 | static void show_option_help(struct fio_option *o, FILE *out) |
| 78 | { |
| 79 | const char *typehelp[] = { |
| 80 | "string (opt=bla)", |
| 81 | "string with possible k/m/g postfix (opt=4k)", |
| 82 | "string with time postfix (opt=10s)", |
| 83 | "string (opt=bla)", |
| 84 | "string with dual range (opt=1k-4k,4k-8k)", |
| 85 | "integer value (opt=100)", |
| 86 | "boolean value (opt=1)", |
| 87 | "no argument (opt)", |
| 88 | }; |
| 89 | |
| 90 | if (o->alias) |
| 91 | fprintf(out, "%20s: %s\n", "alias", o->alias); |
| 92 | |
| 93 | fprintf(out, "%20s: %s\n", "type", typehelp[o->type]); |
| 94 | fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default"); |
| 95 | show_option_range(o, stdout); |
| 96 | show_option_values(o); |
| 97 | } |
| 98 | |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 99 | static unsigned long get_mult_time(char c) |
| 100 | { |
| 101 | switch (c) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 102 | case 'm': |
| 103 | case 'M': |
| 104 | return 60; |
| 105 | case 'h': |
| 106 | case 'H': |
| 107 | return 60 * 60; |
| 108 | case 'd': |
| 109 | case 'D': |
| 110 | return 24 * 60 * 60; |
| 111 | default: |
| 112 | return 1; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 116 | static unsigned long long get_mult_bytes(char c, void *data) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 117 | { |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 118 | unsigned int kb_base = fio_get_kb_base(data); |
Jens Axboe | a639f0b | 2009-07-18 08:25:35 +0200 | [diff] [blame] | 119 | unsigned long long ret = 1; |
| 120 | |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 121 | switch (c) { |
Jens Axboe | a639f0b | 2009-07-18 08:25:35 +0200 | [diff] [blame] | 122 | default: |
| 123 | break; |
Jens Axboe | b09da8f | 2009-07-17 23:16:17 +0200 | [diff] [blame] | 124 | case 'p': |
| 125 | case 'P': |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 126 | ret *= (unsigned long long) kb_base; |
Jens Axboe | a639f0b | 2009-07-18 08:25:35 +0200 | [diff] [blame] | 127 | case 't': |
| 128 | case 'T': |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 129 | ret *= (unsigned long long) kb_base; |
Jens Axboe | a639f0b | 2009-07-18 08:25:35 +0200 | [diff] [blame] | 130 | case 'g': |
| 131 | case 'G': |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 132 | ret *= (unsigned long long) kb_base; |
Jens Axboe | a639f0b | 2009-07-18 08:25:35 +0200 | [diff] [blame] | 133 | case 'm': |
| 134 | case 'M': |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 135 | ret *= (unsigned long long) kb_base; |
Jens Axboe | a639f0b | 2009-07-18 08:25:35 +0200 | [diff] [blame] | 136 | case 'k': |
| 137 | case 'K': |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 138 | ret *= (unsigned long long) kb_base; |
Jens Axboe | a639f0b | 2009-07-18 08:25:35 +0200 | [diff] [blame] | 139 | break; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 140 | } |
Jens Axboe | a639f0b | 2009-07-18 08:25:35 +0200 | [diff] [blame] | 141 | |
| 142 | return ret; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 146 | * convert string into decimal value, noting any size suffix |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 147 | */ |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 148 | int str_to_decimal(const char *str, long long *val, int kilo, void *data) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 149 | { |
Jens Axboe | b347f9d | 2009-03-09 14:15:21 +0100 | [diff] [blame] | 150 | int len, base; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 151 | |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 152 | len = strlen(str); |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 153 | if (!len) |
| 154 | return 1; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 155 | |
Jens Axboe | b347f9d | 2009-03-09 14:15:21 +0100 | [diff] [blame] | 156 | if (strstr(str, "0x") || strstr(str, "0X")) |
| 157 | base = 16; |
| 158 | else |
| 159 | base = 10; |
| 160 | |
| 161 | *val = strtoll(str, NULL, base); |
Jens Axboe | cda866c | 2007-01-10 16:02:32 +0100 | [diff] [blame] | 162 | if (*val == LONG_MAX && errno == ERANGE) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 163 | return 1; |
| 164 | |
| 165 | if (kilo) |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 166 | *val *= get_mult_bytes(str[len - 1], data); |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 167 | else |
| 168 | *val *= get_mult_time(str[len - 1]); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 169 | |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 173 | static int check_str_bytes(const char *p, long long *val, void *data) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 174 | { |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 175 | return str_to_decimal(p, val, 1, data); |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 178 | static int check_str_time(const char *p, long long *val) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 179 | { |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 180 | return str_to_decimal(p, val, 0, NULL); |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void strip_blank_front(char **p) |
| 184 | { |
| 185 | char *s = *p; |
| 186 | |
| 187 | while (isspace(*s)) |
| 188 | s++; |
Jens Axboe | 4d651da | 2007-03-28 19:34:53 +0200 | [diff] [blame] | 189 | |
| 190 | *p = s; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void strip_blank_end(char *p) |
| 194 | { |
Jens Axboe | 853ee7f | 2009-03-06 20:29:29 +0100 | [diff] [blame] | 195 | char *start = p, *s; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 196 | |
Jens Axboe | 523bfad | 2007-04-04 11:04:06 +0200 | [diff] [blame] | 197 | s = strchr(p, ';'); |
| 198 | if (s) |
| 199 | *s = '\0'; |
| 200 | s = strchr(p, '#'); |
| 201 | if (s) |
| 202 | *s = '\0'; |
| 203 | if (s) |
| 204 | p = s; |
| 205 | |
Jens Axboe | 7f7e6e5 | 2007-07-19 14:50:05 +0200 | [diff] [blame] | 206 | s = p + strlen(p); |
Jens Axboe | 853ee7f | 2009-03-06 20:29:29 +0100 | [diff] [blame] | 207 | while ((isspace(*s) || iscntrl(*s)) && (s > start)) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 208 | s--; |
| 209 | |
| 210 | *(s + 1) = '\0'; |
| 211 | } |
| 212 | |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 213 | static int check_range_bytes(const char *str, long *val, void *data) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 214 | { |
| 215 | char suffix; |
| 216 | |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 217 | if (!strlen(str)) |
| 218 | return 1; |
| 219 | |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 220 | if (sscanf(str, "%lu%c", val, &suffix) == 2) { |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 221 | *val *= get_mult_bytes(suffix, data); |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | if (sscanf(str, "%lu", val) == 1) |
| 226 | return 0; |
| 227 | |
| 228 | return 1; |
| 229 | } |
| 230 | |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 231 | static int check_int(const char *p, int *val) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 232 | { |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 233 | if (!strlen(p)) |
| 234 | return 1; |
Jens Axboe | d78ee46 | 2007-08-10 14:03:20 +0200 | [diff] [blame] | 235 | if (strstr(p, "0x") || strstr(p, "0X")) { |
Jens Axboe | a61bdfd | 2007-07-30 09:07:04 +0200 | [diff] [blame] | 236 | if (sscanf(p, "%x", val) == 1) |
| 237 | return 0; |
| 238 | } else { |
| 239 | if (sscanf(p, "%u", val) == 1) |
| 240 | return 0; |
| 241 | } |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 242 | |
| 243 | return 1; |
| 244 | } |
| 245 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 246 | static struct fio_option *find_option(struct fio_option *options, |
| 247 | const char *opt) |
| 248 | { |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 249 | struct fio_option *o; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 250 | |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 251 | for (o = &options[0]; o->name; o++) { |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 252 | if (!strcmp(o->name, opt)) |
| 253 | return o; |
Jens Axboe | 03b74b3 | 2007-01-11 11:04:31 +0100 | [diff] [blame] | 254 | else if (o->alias && !strcmp(o->alias, opt)) |
| 255 | return o; |
Jens Axboe | 33963c6 | 2006-10-27 11:33:01 +0200 | [diff] [blame] | 256 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 257 | |
| 258 | return NULL; |
| 259 | } |
| 260 | |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 261 | #define val_store(ptr, val, off, data) \ |
| 262 | do { \ |
| 263 | ptr = td_var((data), (off)); \ |
| 264 | *ptr = (val); \ |
| 265 | } while (0) |
| 266 | |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 267 | 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] | 268 | int first, int more) |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 269 | { |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 270 | int il, *ilp; |
| 271 | long long ull, *ullp; |
| 272 | long ul1, ul2; |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 273 | char **cp; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 274 | int ret = 0, is_time = 0; |
| 275 | |
Jens Axboe | a3d741f | 2008-02-27 18:32:33 +0100 | [diff] [blame] | 276 | dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name, |
| 277 | o->type, ptr); |
| 278 | |
Jens Axboe | e3cedca | 2008-11-19 19:57:52 +0100 | [diff] [blame] | 279 | if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) { |
Jens Axboe | 08e26e3 | 2006-11-21 13:15:10 +0100 | [diff] [blame] | 280 | fprintf(stderr, "Option %s requires an argument\n", o->name); |
| 281 | return 1; |
| 282 | } |
| 283 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 284 | switch (o->type) { |
| 285 | case FIO_OPT_STR: { |
| 286 | fio_opt_str_fn *fn = o->cb; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 287 | const struct value_pair *vp; |
Jens Axboe | f085737 | 2007-03-19 13:15:23 +0100 | [diff] [blame] | 288 | struct value_pair posval[PARSE_MAX_VP]; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 289 | int i; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 290 | |
Jens Axboe | f085737 | 2007-03-19 13:15:23 +0100 | [diff] [blame] | 291 | posval_sort(o, posval); |
| 292 | |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 293 | for (i = 0; i < PARSE_MAX_VP; i++) { |
Jens Axboe | f085737 | 2007-03-19 13:15:23 +0100 | [diff] [blame] | 294 | vp = &posval[i]; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 295 | if (!vp->ival || vp->ival[0] == '\0') |
| 296 | break; |
Jens Axboe | 6612a27 | 2007-03-13 08:54:06 +0100 | [diff] [blame] | 297 | ret = 1; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 298 | if (!strncmp(vp->ival, ptr, strlen(vp->ival))) { |
| 299 | ret = 0; |
| 300 | if (!o->off1) |
| 301 | break; |
| 302 | val_store(ilp, vp->oval, o->off1, data); |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | if (ret) |
| 308 | show_option_values(o); |
| 309 | else if (fn) |
| 310 | ret = fn(data, ptr); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 311 | break; |
| 312 | } |
| 313 | case FIO_OPT_STR_VAL_TIME: |
| 314 | is_time = 1; |
Jens Axboe | f7fa265 | 2009-03-09 14:20:20 +0100 | [diff] [blame] | 315 | case FIO_OPT_INT: |
Jens Axboe | e01b22b | 2009-06-09 15:43:25 +0200 | [diff] [blame] | 316 | case FIO_OPT_STR_VAL: { |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 317 | fio_opt_str_val_fn *fn = o->cb; |
| 318 | |
| 319 | if (is_time) |
| 320 | ret = check_str_time(ptr, &ull); |
| 321 | else |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 322 | ret = check_str_bytes(ptr, &ull, data); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 323 | |
| 324 | if (ret) |
| 325 | break; |
| 326 | |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 327 | if (o->maxval && ull > o->maxval) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 328 | fprintf(stderr, "max value out of range: %lld" |
| 329 | " (%d max)\n", ull, o->maxval); |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 330 | return 1; |
| 331 | } |
| 332 | if (o->minval && ull < o->minval) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 333 | fprintf(stderr, "min value out of range: %lld" |
| 334 | " (%d min)\n", ull, o->minval); |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 335 | return 1; |
| 336 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 337 | |
| 338 | if (fn) |
| 339 | ret = fn(data, &ull); |
| 340 | else { |
Jens Axboe | e01b22b | 2009-06-09 15:43:25 +0200 | [diff] [blame] | 341 | if (o->type == FIO_OPT_INT) { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 342 | if (first) |
| 343 | val_store(ilp, ull, o->off1, data); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 344 | if (!more && o->off2) |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 345 | val_store(ilp, ull, o->off2, data); |
Jens Axboe | 75e6f36 | 2006-11-03 08:17:09 +0100 | [diff] [blame] | 346 | } else { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 347 | if (first) |
| 348 | val_store(ullp, ull, o->off1, data); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 349 | if (!more && o->off2) |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 350 | val_store(ullp, ull, o->off2, data); |
Jens Axboe | 75e6f36 | 2006-11-03 08:17:09 +0100 | [diff] [blame] | 351 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 352 | } |
| 353 | break; |
| 354 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 355 | case FIO_OPT_STR_STORE: { |
| 356 | fio_opt_str_fn *fn = o->cb; |
| 357 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 358 | cp = td_var(data, o->off1); |
| 359 | *cp = strdup(ptr); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 360 | if (fn) { |
| 361 | ret = fn(data, ptr); |
| 362 | if (ret) { |
| 363 | free(*cp); |
| 364 | *cp = NULL; |
| 365 | } |
| 366 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 367 | break; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 368 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 369 | case FIO_OPT_RANGE: { |
Jens Axboe | b765a37 | 2006-10-27 15:00:16 +0200 | [diff] [blame] | 370 | char tmp[128]; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 371 | char *p1, *p2; |
| 372 | |
Jens Axboe | 0bbab0e | 2006-11-02 09:18:36 +0100 | [diff] [blame] | 373 | strncpy(tmp, ptr, sizeof(tmp) - 1); |
Jens Axboe | b765a37 | 2006-10-27 15:00:16 +0200 | [diff] [blame] | 374 | |
| 375 | p1 = strchr(tmp, '-'); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 376 | if (!p1) { |
Jens Axboe | 0c9baf9 | 2007-01-11 15:59:26 +0100 | [diff] [blame] | 377 | p1 = strchr(tmp, ':'); |
| 378 | if (!p1) { |
| 379 | ret = 1; |
| 380 | break; |
| 381 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | p2 = p1 + 1; |
| 385 | *p1 = '\0'; |
Jens Axboe | b765a37 | 2006-10-27 15:00:16 +0200 | [diff] [blame] | 386 | p1 = tmp; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 387 | |
| 388 | ret = 1; |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 389 | if (!check_range_bytes(p1, &ul1, data) && |
| 390 | !check_range_bytes(p2, &ul2, data)) { |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 391 | ret = 0; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 392 | if (ul1 > ul2) { |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 393 | unsigned long foo = ul1; |
| 394 | |
| 395 | ul1 = ul2; |
| 396 | ul2 = foo; |
| 397 | } |
| 398 | |
| 399 | if (first) { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 400 | val_store(ilp, ul1, o->off1, data); |
| 401 | val_store(ilp, ul2, o->off2, data); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 402 | } |
Jens Axboe | 97e8cd4 | 2008-02-05 09:57:39 +0100 | [diff] [blame] | 403 | if (o->off3 && o->off4) { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 404 | val_store(ilp, ul1, o->off3, data); |
| 405 | val_store(ilp, ul2, o->off4, data); |
| 406 | } |
| 407 | } |
| 408 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 409 | break; |
| 410 | } |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 411 | case FIO_OPT_BOOL: { |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 412 | fio_opt_int_fn *fn = o->cb; |
| 413 | |
| 414 | ret = check_int(ptr, &il); |
| 415 | if (ret) |
| 416 | break; |
| 417 | |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 418 | if (o->maxval && il > (int) o->maxval) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 419 | fprintf(stderr, "max value out of range: %d (%d max)\n", |
| 420 | il, o->maxval); |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 421 | return 1; |
| 422 | } |
| 423 | if (o->minval && il < o->minval) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 424 | fprintf(stderr, "min value out of range: %d (%d min)\n", |
| 425 | il, o->minval); |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 426 | return 1; |
| 427 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 428 | |
Jens Axboe | 76a43db | 2007-01-11 13:24:44 +0100 | [diff] [blame] | 429 | if (o->neg) |
| 430 | il = !il; |
| 431 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 432 | if (fn) |
| 433 | ret = fn(data, &il); |
| 434 | else { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 435 | if (first) |
| 436 | val_store(ilp, il, o->off1, data); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 437 | if (!more && o->off2) |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 438 | val_store(ilp, il, o->off2, data); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 439 | } |
| 440 | break; |
| 441 | } |
| 442 | case FIO_OPT_STR_SET: { |
| 443 | fio_opt_str_set_fn *fn = o->cb; |
| 444 | |
| 445 | if (fn) |
| 446 | ret = fn(data); |
| 447 | else { |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 448 | if (first) |
| 449 | val_store(ilp, 1, o->off1, data); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 450 | if (!more && o->off2) |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 451 | val_store(ilp, 1, o->off2, data); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 452 | } |
| 453 | break; |
| 454 | } |
Jens Axboe | 15ca150 | 2008-04-07 09:26:02 +0200 | [diff] [blame] | 455 | case FIO_OPT_DEPRECATED: |
| 456 | fprintf(stdout, "Option %s is deprecated\n", o->name); |
| 457 | break; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 458 | default: |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 459 | fprintf(stderr, "Bad option type %u\n", o->type); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 460 | ret = 1; |
| 461 | } |
| 462 | |
Jens Axboe | 70a4c0c | 2009-07-01 09:24:05 +0200 | [diff] [blame] | 463 | if (ret) |
| 464 | return ret; |
| 465 | |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 466 | if (o->verify) { |
Jens Axboe | 70a4c0c | 2009-07-01 09:24:05 +0200 | [diff] [blame] | 467 | ret = o->verify(o, data); |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 468 | if (ret) { |
| 469 | fprintf(stderr,"Correct format for offending option\n"); |
| 470 | fprintf(stderr, "%20s: %s\n", o->name, o->help); |
| 471 | show_option_help(o, stderr); |
| 472 | } |
| 473 | } |
Jens Axboe | 70a4c0c | 2009-07-01 09:24:05 +0200 | [diff] [blame] | 474 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 475 | return ret; |
| 476 | } |
| 477 | |
Jens Axboe | 7c8f1a5 | 2009-06-09 12:28:57 +0200 | [diff] [blame] | 478 | static int handle_option(struct fio_option *o, const char *__ptr, void *data) |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 479 | { |
Jens Axboe | 7c8f1a5 | 2009-06-09 12:28:57 +0200 | [diff] [blame] | 480 | char *ptr, *ptr2 = NULL; |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 481 | int r1, r2; |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 482 | |
Jens Axboe | 7c8f1a5 | 2009-06-09 12:28:57 +0200 | [diff] [blame] | 483 | dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr); |
| 484 | |
| 485 | ptr = NULL; |
| 486 | if (__ptr) |
| 487 | ptr = strdup(__ptr); |
Jens Axboe | a3d741f | 2008-02-27 18:32:33 +0100 | [diff] [blame] | 488 | |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 489 | /* |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 490 | * See if we have a second set of parameters, hidden after a comma. |
| 491 | * Do this before parsing the first round, to check if we should |
| 492 | * copy set 1 options to set 2. |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 493 | */ |
Joel Becker | ad231bc | 2007-03-01 08:22:42 +0100 | [diff] [blame] | 494 | if (ptr && |
| 495 | (o->type != FIO_OPT_STR_STORE) && |
| 496 | (o->type != FIO_OPT_STR)) { |
Jens Axboe | 92b586f | 2006-11-07 14:29:17 +0100 | [diff] [blame] | 497 | ptr2 = strchr(ptr, ','); |
Jens Axboe | 7c8f1a5 | 2009-06-09 12:28:57 +0200 | [diff] [blame] | 498 | if (ptr2 && *(ptr2 + 1) == '\0') |
| 499 | *ptr2 = '\0'; |
Jens Axboe | 0c9baf9 | 2007-01-11 15:59:26 +0100 | [diff] [blame] | 500 | if (!ptr2) |
| 501 | ptr2 = strchr(ptr, ':'); |
Jens Axboe | b486f76 | 2007-03-15 09:15:12 +0100 | [diff] [blame] | 502 | if (!ptr2) |
| 503 | ptr2 = strchr(ptr, '-'); |
Jens Axboe | 0c9baf9 | 2007-01-11 15:59:26 +0100 | [diff] [blame] | 504 | } |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 505 | |
| 506 | /* |
| 507 | * Don't return early if parsing the first option fails - if |
| 508 | * we are doing multiple arguments, we can allow the first one |
| 509 | * being empty. |
| 510 | */ |
| 511 | r1 = __handle_option(o, ptr, data, 1, !!ptr2); |
| 512 | |
Jens Axboe | 7c8f1a5 | 2009-06-09 12:28:57 +0200 | [diff] [blame] | 513 | if (!ptr2) { |
| 514 | if (ptr) |
| 515 | free(ptr); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 516 | return r1; |
Jens Axboe | 7c8f1a5 | 2009-06-09 12:28:57 +0200 | [diff] [blame] | 517 | } |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 518 | |
| 519 | ptr2++; |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 520 | r2 = __handle_option(o, ptr2, data, 0, 0); |
| 521 | |
Jens Axboe | 7c8f1a5 | 2009-06-09 12:28:57 +0200 | [diff] [blame] | 522 | if (ptr) |
| 523 | free(ptr); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 524 | return r1 && r2; |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 525 | } |
| 526 | |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 527 | static struct fio_option *get_option(const char *opt, |
| 528 | struct fio_option *options, char **post) |
| 529 | { |
| 530 | struct fio_option *o; |
| 531 | char *ret; |
| 532 | |
| 533 | ret = strchr(opt, '='); |
| 534 | if (ret) { |
| 535 | *post = ret; |
| 536 | *ret = '\0'; |
| 537 | ret = (char *) opt; |
| 538 | (*post)++; |
Jens Axboe | 43c129b | 2008-12-08 14:06:42 +0100 | [diff] [blame] | 539 | strip_blank_end(ret); |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 540 | o = find_option(options, ret); |
| 541 | } else { |
| 542 | o = find_option(options, opt); |
| 543 | *post = NULL; |
| 544 | } |
| 545 | |
| 546 | return o; |
| 547 | } |
| 548 | |
| 549 | static int opt_cmp(const void *p1, const void *p2) |
| 550 | { |
| 551 | struct fio_option *o1, *o2; |
| 552 | char *s1, *s2, *foo; |
Jens Axboe | 8cdabc1 | 2008-11-19 12:31:43 +0100 | [diff] [blame] | 553 | int prio1, prio2; |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 554 | |
| 555 | s1 = strdup(*((char **) p1)); |
| 556 | s2 = strdup(*((char **) p2)); |
| 557 | |
| 558 | o1 = get_option(s1, fio_options, &foo); |
| 559 | o2 = get_option(s2, fio_options, &foo); |
Jens Axboe | 8cdabc1 | 2008-11-19 12:31:43 +0100 | [diff] [blame] | 560 | |
| 561 | prio1 = prio2 = 0; |
| 562 | if (o1) |
| 563 | prio1 = o1->prio; |
| 564 | if (o2) |
| 565 | prio2 = o2->prio; |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 566 | |
| 567 | free(s1); |
| 568 | free(s2); |
Jens Axboe | 8cdabc1 | 2008-11-19 12:31:43 +0100 | [diff] [blame] | 569 | return prio2 - prio1; |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | void sort_options(char **opts, struct fio_option *options, int num_opts) |
| 573 | { |
| 574 | fio_options = options; |
| 575 | qsort(opts, num_opts, sizeof(char *), opt_cmp); |
| 576 | fio_options = NULL; |
| 577 | } |
| 578 | |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 579 | int parse_cmd_option(const char *opt, const char *val, |
| 580 | struct fio_option *options, void *data) |
| 581 | { |
| 582 | struct fio_option *o; |
| 583 | |
| 584 | o = find_option(options, opt); |
| 585 | if (!o) { |
Jens Axboe | 43c129b | 2008-12-08 14:06:42 +0100 | [diff] [blame] | 586 | fprintf(stderr, "Bad option <%s>\n", opt); |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 587 | return 1; |
| 588 | } |
| 589 | |
Jens Axboe | b1508cf | 2006-11-02 09:12:40 +0100 | [diff] [blame] | 590 | if (!handle_option(o, val, data)) |
| 591 | return 0; |
| 592 | |
| 593 | fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val); |
| 594 | return 1; |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 595 | } |
| 596 | |
Aaron Carroll | 88b5a39 | 2008-10-07 11:25:20 +0200 | [diff] [blame] | 597 | /* |
| 598 | * Return a copy of the input string with substrings of the form ${VARNAME} |
| 599 | * substituted with the value of the environment variable VARNAME. The |
| 600 | * substitution always occurs, even if VARNAME is empty or the corresponding |
| 601 | * environment variable undefined. |
| 602 | */ |
| 603 | static char *option_dup_subs(const char *opt) |
| 604 | { |
| 605 | char out[OPT_LEN_MAX+1]; |
| 606 | char in[OPT_LEN_MAX+1]; |
| 607 | char *outptr = out; |
| 608 | char *inptr = in; |
| 609 | char *ch1, *ch2, *env; |
| 610 | ssize_t nchr = OPT_LEN_MAX; |
| 611 | size_t envlen; |
| 612 | |
| 613 | in[OPT_LEN_MAX] = '\0'; |
| 614 | strncpy(in, opt, OPT_LEN_MAX); |
| 615 | |
| 616 | while (*inptr && nchr > 0) { |
| 617 | if (inptr[0] == '$' && inptr[1] == '{') { |
| 618 | ch2 = strchr(inptr, '}'); |
| 619 | if (ch2 && inptr+1 < ch2) { |
| 620 | ch1 = inptr+2; |
| 621 | inptr = ch2+1; |
| 622 | *ch2 = '\0'; |
| 623 | |
| 624 | env = getenv(ch1); |
| 625 | if (env) { |
| 626 | envlen = strlen(env); |
| 627 | if (envlen <= nchr) { |
| 628 | memcpy(outptr, env, envlen); |
| 629 | outptr += envlen; |
| 630 | nchr -= envlen; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | continue; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | *outptr++ = *inptr++; |
| 639 | --nchr; |
| 640 | } |
| 641 | |
| 642 | *outptr = '\0'; |
| 643 | return strdup(out); |
| 644 | } |
| 645 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 646 | int parse_option(const char *opt, struct fio_option *options, void *data) |
| 647 | { |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 648 | struct fio_option *o; |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 649 | char *post, *tmp; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 650 | |
Aaron Carroll | 88b5a39 | 2008-10-07 11:25:20 +0200 | [diff] [blame] | 651 | tmp = option_dup_subs(opt); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 652 | |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 653 | o = get_option(tmp, options, &post); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 654 | if (!o) { |
Jens Axboe | 43c129b | 2008-12-08 14:06:42 +0100 | [diff] [blame] | 655 | fprintf(stderr, "Bad option <%s>\n", tmp); |
Jens Axboe | 0401bca | 2007-03-29 11:00:43 +0200 | [diff] [blame] | 656 | free(tmp); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 657 | return 1; |
| 658 | } |
| 659 | |
Jens Axboe | 0401bca | 2007-03-29 11:00:43 +0200 | [diff] [blame] | 660 | if (!handle_option(o, post, data)) { |
| 661 | free(tmp); |
Jens Axboe | b1508cf | 2006-11-02 09:12:40 +0100 | [diff] [blame] | 662 | return 0; |
Jens Axboe | 0401bca | 2007-03-29 11:00:43 +0200 | [diff] [blame] | 663 | } |
Jens Axboe | b1508cf | 2006-11-02 09:12:40 +0100 | [diff] [blame] | 664 | |
| 665 | fprintf(stderr, "fio: failed parsing %s\n", opt); |
Jens Axboe | 0401bca | 2007-03-29 11:00:43 +0200 | [diff] [blame] | 666 | free(tmp); |
Jens Axboe | b1508cf | 2006-11-02 09:12:40 +0100 | [diff] [blame] | 667 | return 1; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 668 | } |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 669 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 670 | /* |
| 671 | * Option match, levenshtein distance. Handy for not quite remembering what |
| 672 | * the option name is. |
| 673 | */ |
| 674 | static int string_distance(const char *s1, const char *s2) |
| 675 | { |
| 676 | unsigned int s1_len = strlen(s1); |
| 677 | unsigned int s2_len = strlen(s2); |
| 678 | unsigned int *p, *q, *r; |
| 679 | unsigned int i, j; |
| 680 | |
| 681 | p = malloc(sizeof(unsigned int) * (s2_len + 1)); |
| 682 | q = malloc(sizeof(unsigned int) * (s2_len + 1)); |
| 683 | |
| 684 | p[0] = 0; |
| 685 | for (i = 1; i <= s2_len; i++) |
| 686 | p[i] = p[i - 1] + 1; |
| 687 | |
| 688 | for (i = 1; i <= s1_len; i++) { |
| 689 | q[0] = p[0] + 1; |
| 690 | for (j = 1; j <= s2_len; j++) { |
| 691 | unsigned int sub = p[j - 1]; |
| 692 | |
| 693 | if (s1[i - 1] != s2[j - 1]) |
| 694 | sub++; |
| 695 | |
| 696 | q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub)); |
| 697 | } |
| 698 | r = p; |
| 699 | p = q; |
| 700 | q = r; |
| 701 | } |
| 702 | |
| 703 | i = p[s2_len]; |
| 704 | free(p); |
| 705 | free(q); |
| 706 | return i; |
| 707 | } |
| 708 | |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 709 | static struct fio_option *find_child(struct fio_option *options, |
| 710 | struct fio_option *o) |
| 711 | { |
| 712 | struct fio_option *__o; |
| 713 | |
Jens Axboe | fdf2874 | 2007-07-31 23:06:09 +0200 | [diff] [blame] | 714 | for (__o = options + 1; __o->name; __o++) |
| 715 | if (__o->parent && !strcmp(__o->parent, o->name)) |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 716 | return __o; |
| 717 | |
| 718 | return NULL; |
| 719 | } |
| 720 | |
Jens Axboe | 323d911 | 2008-03-01 15:12:14 +0100 | [diff] [blame] | 721 | static void __print_option(struct fio_option *o, struct fio_option *org, |
| 722 | int level) |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 723 | { |
| 724 | char name[256], *p; |
Jens Axboe | 323d911 | 2008-03-01 15:12:14 +0100 | [diff] [blame] | 725 | int depth; |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 726 | |
| 727 | if (!o) |
| 728 | return; |
Jens Axboe | ef9aff5 | 2007-07-31 22:56:53 +0200 | [diff] [blame] | 729 | if (!org) |
| 730 | org = o; |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 731 | |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 732 | p = name; |
Jens Axboe | 323d911 | 2008-03-01 15:12:14 +0100 | [diff] [blame] | 733 | depth = level; |
| 734 | while (depth--) |
| 735 | p += sprintf(p, "%s", " "); |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 736 | |
| 737 | sprintf(p, "%s", o->name); |
| 738 | |
| 739 | printf("%-24s: %s\n", name, o->help); |
Jens Axboe | 323d911 | 2008-03-01 15:12:14 +0100 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | static void print_option(struct fio_option *o) |
| 743 | { |
| 744 | struct fio_option *parent; |
| 745 | struct fio_option *__o; |
| 746 | unsigned int printed; |
| 747 | unsigned int level; |
| 748 | |
| 749 | __print_option(o, NULL, 0); |
| 750 | parent = o; |
| 751 | level = 0; |
| 752 | do { |
| 753 | level++; |
| 754 | printed = 0; |
| 755 | |
| 756 | while ((__o = find_child(o, parent)) != NULL) { |
| 757 | __print_option(__o, o, level); |
| 758 | o = __o; |
| 759 | printed++; |
| 760 | } |
| 761 | |
| 762 | parent = o; |
| 763 | } while (printed); |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 764 | } |
| 765 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 766 | int show_cmd_help(struct fio_option *options, const char *name) |
| 767 | { |
| 768 | struct fio_option *o, *closest; |
| 769 | unsigned int best_dist; |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 770 | int found = 0; |
Jens Axboe | 320beef | 2007-03-01 10:44:12 +0100 | [diff] [blame] | 771 | int show_all = 0; |
| 772 | |
| 773 | if (!name || !strcmp(name, "all")) |
| 774 | show_all = 1; |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 775 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 776 | closest = NULL; |
| 777 | best_dist = -1; |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 778 | for (o = &options[0]; o->name; o++) { |
Jens Axboe | 320beef | 2007-03-01 10:44:12 +0100 | [diff] [blame] | 779 | int match = 0; |
| 780 | |
Jens Axboe | 15ca150 | 2008-04-07 09:26:02 +0200 | [diff] [blame] | 781 | if (o->type == FIO_OPT_DEPRECATED) |
| 782 | continue; |
| 783 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 784 | if (name) { |
Jens Axboe | 7f9348f | 2007-03-15 14:09:28 +0100 | [diff] [blame] | 785 | if (!strcmp(name, o->name) || |
| 786 | (o->alias && !strcmp(name, o->alias))) |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 787 | match = 1; |
| 788 | else { |
| 789 | unsigned int dist; |
| 790 | |
| 791 | dist = string_distance(name, o->name); |
| 792 | if (dist < best_dist) { |
| 793 | best_dist = dist; |
| 794 | closest = o; |
| 795 | } |
| 796 | } |
| 797 | } |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 798 | |
| 799 | if (show_all || match) { |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 800 | found = 1; |
Jens Axboe | c167ded | 2007-03-14 13:02:53 +0100 | [diff] [blame] | 801 | if (match) |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 802 | printf("%24s: %s\n", o->name, o->help); |
Jens Axboe | c167ded | 2007-03-14 13:02:53 +0100 | [diff] [blame] | 803 | if (show_all) { |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 804 | if (!o->parent) |
Jens Axboe | 323d911 | 2008-03-01 15:12:14 +0100 | [diff] [blame] | 805 | print_option(o); |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 806 | continue; |
Jens Axboe | c167ded | 2007-03-14 13:02:53 +0100 | [diff] [blame] | 807 | } |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 808 | } |
| 809 | |
Jens Axboe | 70df2f1 | 2007-01-11 14:04:47 +0100 | [diff] [blame] | 810 | if (!match) |
| 811 | continue; |
| 812 | |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 813 | show_option_help(o, stdout); |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 814 | } |
| 815 | |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 816 | if (found) |
| 817 | return 0; |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 818 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 819 | printf("No such command: %s", name); |
| 820 | if (closest) { |
| 821 | printf(" - showing closest match\n"); |
| 822 | printf("%20s: %s\n", closest->name, closest->help); |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 823 | show_option_help(closest, stdout); |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 824 | } else |
| 825 | printf("\n"); |
| 826 | |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 827 | return 1; |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 828 | } |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 829 | |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 830 | /* |
| 831 | * Handle parsing of default parameters. |
| 832 | */ |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 833 | void fill_default_options(void *data, struct fio_option *options) |
| 834 | { |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 835 | struct fio_option *o; |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 836 | |
Jens Axboe | a3d741f | 2008-02-27 18:32:33 +0100 | [diff] [blame] | 837 | dprint(FD_PARSE, "filling default options\n"); |
| 838 | |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 839 | for (o = &options[0]; o->name; o++) |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 840 | if (o->def) |
| 841 | handle_option(o, o->def, data); |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 842 | } |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 843 | |
| 844 | /* |
| 845 | * 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] | 846 | * values and whether both callback and offsets are given. |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 847 | */ |
| 848 | void options_init(struct fio_option *options) |
| 849 | { |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 850 | struct fio_option *o; |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 851 | |
Jens Axboe | a3d741f | 2008-02-27 18:32:33 +0100 | [diff] [blame] | 852 | dprint(FD_PARSE, "init options\n"); |
| 853 | |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 854 | for (o = &options[0]; o->name; o++) { |
Jens Axboe | 15ca150 | 2008-04-07 09:26:02 +0200 | [diff] [blame] | 855 | if (o->type == FIO_OPT_DEPRECATED) |
| 856 | continue; |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 857 | if (o->type == FIO_OPT_BOOL) { |
| 858 | o->minval = 0; |
| 859 | o->maxval = 1; |
| 860 | } |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 861 | if (o->type == FIO_OPT_STR_SET && o->def) { |
| 862 | fprintf(stderr, "Option %s: string set option with" |
| 863 | " default will always be true\n", |
| 864 | o->name); |
| 865 | } |
| 866 | if (!o->cb && !o->off1) { |
| 867 | fprintf(stderr, "Option %s: neither cb nor offset" |
| 868 | " given\n", o->name); |
| 869 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 870 | if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE) |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 871 | continue; |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 872 | if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) { |
| 873 | fprintf(stderr, "Option %s: both cb and offset given\n", |
| 874 | o->name); |
| 875 | } |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 876 | } |
| 877 | } |