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