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> |
Yu-ju Hong | 8334919 | 2011-08-13 00:53:44 +0200 | [diff] [blame] | 12 | #include <math.h> |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 13 | |
| 14 | #include "parse.h" |
Jens Axboe | a3d741f | 2008-02-27 18:32:33 +0100 | [diff] [blame] | 15 | #include "debug.h" |
Jens Axboe | 9f988e2 | 2010-03-04 10:42:38 +0100 | [diff] [blame] | 16 | #include "options.h" |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 17 | |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 18 | static struct fio_option *fio_options; |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 19 | extern unsigned int fio_get_kb_base(void *); |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 20 | |
Jens Axboe | f085737 | 2007-03-19 13:15:23 +0100 | [diff] [blame] | 21 | static int vp_cmp(const void *p1, const void *p2) |
| 22 | { |
| 23 | const struct value_pair *vp1 = p1; |
| 24 | const struct value_pair *vp2 = p2; |
| 25 | |
| 26 | return strlen(vp2->ival) - strlen(vp1->ival); |
| 27 | } |
| 28 | |
| 29 | static void posval_sort(struct fio_option *o, struct value_pair *vpmap) |
| 30 | { |
| 31 | const struct value_pair *vp; |
| 32 | int entries; |
| 33 | |
| 34 | memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair)); |
| 35 | |
| 36 | for (entries = 0; entries < PARSE_MAX_VP; entries++) { |
| 37 | vp = &o->posval[entries]; |
| 38 | if (!vp->ival || vp->ival[0] == '\0') |
| 39 | break; |
| 40 | |
| 41 | memcpy(&vpmap[entries], vp, sizeof(*vp)); |
| 42 | } |
| 43 | |
| 44 | qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp); |
| 45 | } |
| 46 | |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 47 | static void show_option_range(struct fio_option *o, FILE *out) |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 48 | { |
Yu-ju Hong | 8334919 | 2011-08-13 00:53:44 +0200 | [diff] [blame] | 49 | if (o->type == FIO_OPT_FLOAT_LIST){ |
| 50 | if (isnan(o->minfp) && isnan(o->maxfp)) |
| 51 | return; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 52 | |
Yu-ju Hong | 8334919 | 2011-08-13 00:53:44 +0200 | [diff] [blame] | 53 | fprintf(out, "%20s: min=%f", "range", o->minfp); |
| 54 | if (!isnan(o->maxfp)) |
| 55 | fprintf(out, ", max=%f", o->maxfp); |
| 56 | fprintf(out, "\n"); |
| 57 | } else { |
| 58 | if (!o->minval && !o->maxval) |
| 59 | return; |
| 60 | |
| 61 | fprintf(out, "%20s: min=%d", "range", o->minval); |
| 62 | if (o->maxval) |
| 63 | fprintf(out, ", max=%d", o->maxval); |
| 64 | fprintf(out, "\n"); |
| 65 | } |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static void show_option_values(struct fio_option *o) |
| 69 | { |
Jens Axboe | a3073f4 | 2010-03-05 10:06:15 +0100 | [diff] [blame] | 70 | int i; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 71 | |
Jens Axboe | a3073f4 | 2010-03-05 10:06:15 +0100 | [diff] [blame] | 72 | for (i = 0; i < PARSE_MAX_VP; i++) { |
Jens Axboe | 7837213 | 2007-03-14 13:24:07 +0100 | [diff] [blame] | 73 | const struct value_pair *vp = &o->posval[i]; |
| 74 | |
| 75 | if (!vp->ival) |
Jens Axboe | a3073f4 | 2010-03-05 10:06:15 +0100 | [diff] [blame] | 76 | continue; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 77 | |
Jens Axboe | 7837213 | 2007-03-14 13:24:07 +0100 | [diff] [blame] | 78 | printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival); |
| 79 | if (vp->help) |
| 80 | printf(" %s", vp->help); |
| 81 | printf("\n"); |
Jens Axboe | a3073f4 | 2010-03-05 10:06:15 +0100 | [diff] [blame] | 82 | } |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 83 | |
| 84 | if (i) |
| 85 | printf("\n"); |
| 86 | } |
| 87 | |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 88 | static void show_option_help(struct fio_option *o, FILE *out) |
| 89 | { |
| 90 | const char *typehelp[] = { |
Jens Axboe | 07b3232 | 2010-03-05 09:48:44 +0100 | [diff] [blame] | 91 | "invalid", |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 92 | "string (opt=bla)", |
Jens Axboe | ae3fb6f | 2010-09-14 13:10:35 +0200 | [diff] [blame] | 93 | "string (opt=bla)", |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 94 | "string with possible k/m/g postfix (opt=4k)", |
| 95 | "string with time postfix (opt=10s)", |
| 96 | "string (opt=bla)", |
| 97 | "string with dual range (opt=1k-4k,4k-8k)", |
| 98 | "integer value (opt=100)", |
| 99 | "boolean value (opt=1)", |
Yu-ju Hong | 8334919 | 2011-08-13 00:53:44 +0200 | [diff] [blame] | 100 | "list of floating point values separated by ':' (opt=5.9:7.8)", |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 101 | "no argument (opt)", |
Jens Axboe | 07b3232 | 2010-03-05 09:48:44 +0100 | [diff] [blame] | 102 | "deprecated", |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | if (o->alias) |
| 106 | fprintf(out, "%20s: %s\n", "alias", o->alias); |
| 107 | |
| 108 | fprintf(out, "%20s: %s\n", "type", typehelp[o->type]); |
| 109 | fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default"); |
Jens Axboe | 07b3232 | 2010-03-05 09:48:44 +0100 | [diff] [blame] | 110 | if (o->prof_name) |
| 111 | fprintf(out, "%20s: only for profile '%s'\n", "valid", o->prof_name); |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 112 | show_option_range(o, stdout); |
| 113 | show_option_values(o); |
| 114 | } |
| 115 | |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 116 | static unsigned long get_mult_time(char c) |
| 117 | { |
| 118 | switch (c) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 119 | case 'm': |
| 120 | case 'M': |
| 121 | return 60; |
| 122 | case 'h': |
| 123 | case 'H': |
| 124 | return 60 * 60; |
| 125 | case 'd': |
| 126 | case 'D': |
| 127 | return 24 * 60 * 60; |
| 128 | default: |
| 129 | return 1; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Jens Axboe | 7bb5910 | 2011-07-12 19:47:03 +0200 | [diff] [blame] | 133 | static unsigned long long __get_mult_bytes(const char *p, void *data, |
| 134 | int *percent) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 135 | { |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 136 | unsigned int kb_base = fio_get_kb_base(data); |
Jens Axboe | a639f0b | 2009-07-18 08:25:35 +0200 | [diff] [blame] | 137 | unsigned long long ret = 1; |
Jens Axboe | 57fc29f | 2010-06-23 22:24:07 +0200 | [diff] [blame] | 138 | unsigned int i, pow = 0, mult = kb_base; |
| 139 | char *c; |
Jens Axboe | a639f0b | 2009-07-18 08:25:35 +0200 | [diff] [blame] | 140 | |
Jens Axboe | 57fc29f | 2010-06-23 22:24:07 +0200 | [diff] [blame] | 141 | if (!p) |
| 142 | return 1; |
Jens Axboe | a639f0b | 2009-07-18 08:25:35 +0200 | [diff] [blame] | 143 | |
Jens Axboe | 57fc29f | 2010-06-23 22:24:07 +0200 | [diff] [blame] | 144 | c = strdup(p); |
| 145 | |
| 146 | for (i = 0; i < strlen(c); i++) |
| 147 | c[i] = tolower(c[i]); |
| 148 | |
| 149 | if (!strcmp("pib", c)) { |
| 150 | pow = 5; |
| 151 | mult = 1000; |
| 152 | } else if (!strcmp("tib", c)) { |
| 153 | pow = 4; |
| 154 | mult = 1000; |
| 155 | } else if (!strcmp("gib", c)) { |
| 156 | pow = 3; |
| 157 | mult = 1000; |
| 158 | } else if (!strcmp("mib", c)) { |
| 159 | pow = 2; |
| 160 | mult = 1000; |
| 161 | } else if (!strcmp("kib", c)) { |
| 162 | pow = 1; |
| 163 | mult = 1000; |
| 164 | } else if (!strcmp("p", c) || !strcmp("pb", c)) |
| 165 | pow = 5; |
| 166 | else if (!strcmp("t", c) || !strcmp("tb", c)) |
| 167 | pow = 4; |
| 168 | else if (!strcmp("g", c) || !strcmp("gb", c)) |
| 169 | pow = 3; |
| 170 | else if (!strcmp("m", c) || !strcmp("mb", c)) |
| 171 | pow = 2; |
| 172 | else if (!strcmp("k", c) || !strcmp("kb", c)) |
| 173 | pow = 1; |
Jens Axboe | 7bb5910 | 2011-07-12 19:47:03 +0200 | [diff] [blame] | 174 | else if (!strcmp("%", c)) { |
| 175 | *percent = 1; |
| 176 | return ret; |
| 177 | } |
Jens Axboe | 57fc29f | 2010-06-23 22:24:07 +0200 | [diff] [blame] | 178 | |
| 179 | while (pow--) |
| 180 | ret *= (unsigned long long) mult; |
| 181 | |
| 182 | free(c); |
Jens Axboe | a639f0b | 2009-07-18 08:25:35 +0200 | [diff] [blame] | 183 | return ret; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Jens Axboe | 7bb5910 | 2011-07-12 19:47:03 +0200 | [diff] [blame] | 186 | static unsigned long long get_mult_bytes(const char *str, int len, void *data, |
| 187 | int *percent) |
Jens Axboe | 57fc29f | 2010-06-23 22:24:07 +0200 | [diff] [blame] | 188 | { |
Jens Axboe | 55ed963 | 2011-03-27 20:55:09 +0200 | [diff] [blame] | 189 | const char *p = str; |
Jens Axboe | 57fc29f | 2010-06-23 22:24:07 +0200 | [diff] [blame] | 190 | |
Jens Axboe | 1d1c187 | 2010-07-29 10:50:05 +0200 | [diff] [blame] | 191 | if (len < 2) |
Jens Axboe | 7bb5910 | 2011-07-12 19:47:03 +0200 | [diff] [blame] | 192 | return __get_mult_bytes(str, data, percent); |
Jens Axboe | 1d1c187 | 2010-07-29 10:50:05 +0200 | [diff] [blame] | 193 | |
Jens Axboe | 55ed963 | 2011-03-27 20:55:09 +0200 | [diff] [blame] | 194 | /* |
| 195 | * Go forward until we hit a non-digit |
| 196 | */ |
| 197 | while ((p - str) <= len) { |
Jens Axboe | 76cd937 | 2011-07-08 21:14:57 +0200 | [diff] [blame] | 198 | if (!isdigit((int) *p)) |
Jens Axboe | 55ed963 | 2011-03-27 20:55:09 +0200 | [diff] [blame] | 199 | break; |
| 200 | p++; |
| 201 | } |
| 202 | |
Jens Axboe | 7bb5910 | 2011-07-12 19:47:03 +0200 | [diff] [blame] | 203 | if (!isalpha((int) *p) && (*p != '%')) |
Jens Axboe | 57fc29f | 2010-06-23 22:24:07 +0200 | [diff] [blame] | 204 | p = NULL; |
| 205 | |
Jens Axboe | 7bb5910 | 2011-07-12 19:47:03 +0200 | [diff] [blame] | 206 | return __get_mult_bytes(p, data, percent); |
Jens Axboe | 57fc29f | 2010-06-23 22:24:07 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 209 | /* |
Yu-ju Hong | 8334919 | 2011-08-13 00:53:44 +0200 | [diff] [blame] | 210 | * Convert string into a floating number. Return 1 for success and 0 otherwise. |
| 211 | */ |
| 212 | int str_to_float(const char *str, double *val) |
| 213 | { |
| 214 | return (1 == sscanf(str, "%lf", val)); |
| 215 | } |
| 216 | |
| 217 | /* |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 218 | * convert string into decimal value, noting any size suffix |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 219 | */ |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 220 | 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] | 221 | { |
Jens Axboe | b347f9d | 2009-03-09 14:15:21 +0100 | [diff] [blame] | 222 | int len, base; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 223 | |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 224 | len = strlen(str); |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 225 | if (!len) |
| 226 | return 1; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 227 | |
Jens Axboe | b347f9d | 2009-03-09 14:15:21 +0100 | [diff] [blame] | 228 | if (strstr(str, "0x") || strstr(str, "0X")) |
| 229 | base = 16; |
| 230 | else |
| 231 | base = 10; |
| 232 | |
| 233 | *val = strtoll(str, NULL, base); |
Jens Axboe | cda866c | 2007-01-10 16:02:32 +0100 | [diff] [blame] | 234 | if (*val == LONG_MAX && errno == ERANGE) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 235 | return 1; |
| 236 | |
Jens Axboe | 7bb5910 | 2011-07-12 19:47:03 +0200 | [diff] [blame] | 237 | if (kilo) { |
| 238 | unsigned long long mult; |
| 239 | int perc = 0; |
| 240 | |
| 241 | mult = get_mult_bytes(str, len, data, &perc); |
| 242 | if (perc) |
| 243 | *val = -1ULL - *val; |
| 244 | else |
| 245 | *val *= mult; |
| 246 | } else |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 247 | *val *= get_mult_time(str[len - 1]); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 248 | |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 249 | return 0; |
| 250 | } |
| 251 | |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 252 | 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] | 253 | { |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 254 | return str_to_decimal(p, val, 1, data); |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 257 | static int check_str_time(const char *p, long long *val) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 258 | { |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 259 | return str_to_decimal(p, val, 0, NULL); |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void strip_blank_front(char **p) |
| 263 | { |
| 264 | char *s = *p; |
| 265 | |
Jens Axboe | 76cd937 | 2011-07-08 21:14:57 +0200 | [diff] [blame] | 266 | while (isspace((int) *s)) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 267 | s++; |
Jens Axboe | 4d651da | 2007-03-28 19:34:53 +0200 | [diff] [blame] | 268 | |
| 269 | *p = s; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void strip_blank_end(char *p) |
| 273 | { |
Jens Axboe | 853ee7f | 2009-03-06 20:29:29 +0100 | [diff] [blame] | 274 | char *start = p, *s; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 275 | |
Jens Axboe | 523bfad | 2007-04-04 11:04:06 +0200 | [diff] [blame] | 276 | s = strchr(p, ';'); |
| 277 | if (s) |
| 278 | *s = '\0'; |
| 279 | s = strchr(p, '#'); |
| 280 | if (s) |
| 281 | *s = '\0'; |
| 282 | if (s) |
| 283 | p = s; |
| 284 | |
Jens Axboe | 7f7e6e5 | 2007-07-19 14:50:05 +0200 | [diff] [blame] | 285 | s = p + strlen(p); |
Jens Axboe | 76cd937 | 2011-07-08 21:14:57 +0200 | [diff] [blame] | 286 | while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start)) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 287 | s--; |
| 288 | |
| 289 | *(s + 1) = '\0'; |
| 290 | } |
| 291 | |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 292 | static int check_range_bytes(const char *str, long *val, void *data) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 293 | { |
Jens Axboe | 57fc29f | 2010-06-23 22:24:07 +0200 | [diff] [blame] | 294 | long long __val; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 295 | |
Jens Axboe | 57fc29f | 2010-06-23 22:24:07 +0200 | [diff] [blame] | 296 | if (!str_to_decimal(str, &__val, 1, data)) { |
| 297 | *val = __val; |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 298 | return 0; |
| 299 | } |
| 300 | |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 301 | return 1; |
| 302 | } |
| 303 | |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 304 | static int check_int(const char *p, int *val) |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 305 | { |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 306 | if (!strlen(p)) |
| 307 | return 1; |
Jens Axboe | d78ee46 | 2007-08-10 14:03:20 +0200 | [diff] [blame] | 308 | if (strstr(p, "0x") || strstr(p, "0X")) { |
Jens Axboe | a61bdfd | 2007-07-30 09:07:04 +0200 | [diff] [blame] | 309 | if (sscanf(p, "%x", val) == 1) |
| 310 | return 0; |
| 311 | } else { |
| 312 | if (sscanf(p, "%u", val) == 1) |
| 313 | return 0; |
| 314 | } |
Jens Axboe | cb2c86f | 2006-10-26 13:48:34 +0200 | [diff] [blame] | 315 | |
| 316 | return 1; |
| 317 | } |
| 318 | |
Jens Axboe | 808def7 | 2010-07-14 16:27:02 -0600 | [diff] [blame] | 319 | static int opt_len(const char *str) |
| 320 | { |
| 321 | char *postfix; |
| 322 | |
| 323 | postfix = strchr(str, ':'); |
| 324 | if (!postfix) |
| 325 | return strlen(str); |
| 326 | |
| 327 | return (int)(postfix - str); |
| 328 | } |
| 329 | |
Jens Axboe | 5f6ddf1 | 2010-03-09 20:40:44 +0100 | [diff] [blame] | 330 | #define val_store(ptr, val, off, or, data) \ |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 331 | do { \ |
| 332 | ptr = td_var((data), (off)); \ |
Jens Axboe | 5f6ddf1 | 2010-03-09 20:40:44 +0100 | [diff] [blame] | 333 | if ((or)) \ |
| 334 | *ptr |= (val); \ |
| 335 | else \ |
| 336 | *ptr = (val); \ |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 337 | } while (0) |
| 338 | |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 339 | static int __handle_option(struct fio_option *o, const char *ptr, void *data, |
Yu-ju Hong | 8334919 | 2011-08-13 00:53:44 +0200 | [diff] [blame] | 340 | int first, int more, int curr) |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 341 | { |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 342 | int il, *ilp; |
Yu-ju Hong | 8334919 | 2011-08-13 00:53:44 +0200 | [diff] [blame] | 343 | double* flp; |
Jens Axboe | 63f2937 | 2007-01-10 13:20:09 +0100 | [diff] [blame] | 344 | long long ull, *ullp; |
| 345 | long ul1, ul2; |
Yu-ju Hong | 8334919 | 2011-08-13 00:53:44 +0200 | [diff] [blame] | 346 | double uf; |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 347 | char **cp; |
Jens Axboe | e42b01e | 2011-05-05 12:05:10 -0600 | [diff] [blame] | 348 | int ret = 0, is_time = 0; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 349 | |
Jens Axboe | a3d741f | 2008-02-27 18:32:33 +0100 | [diff] [blame] | 350 | dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name, |
| 351 | o->type, ptr); |
| 352 | |
Jens Axboe | e3cedca | 2008-11-19 19:57:52 +0100 | [diff] [blame] | 353 | 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] | 354 | fprintf(stderr, "Option %s requires an argument\n", o->name); |
| 355 | return 1; |
| 356 | } |
| 357 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 358 | switch (o->type) { |
Jens Axboe | 5f6ddf1 | 2010-03-09 20:40:44 +0100 | [diff] [blame] | 359 | case FIO_OPT_STR: |
| 360 | case FIO_OPT_STR_MULTI: { |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 361 | fio_opt_str_fn *fn = o->cb; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 362 | const struct value_pair *vp; |
Jens Axboe | f085737 | 2007-03-19 13:15:23 +0100 | [diff] [blame] | 363 | struct value_pair posval[PARSE_MAX_VP]; |
Jens Axboe | ae2ddba | 2010-03-10 12:49:03 +0100 | [diff] [blame] | 364 | int i, all_skipped = 1; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 365 | |
Jens Axboe | f085737 | 2007-03-19 13:15:23 +0100 | [diff] [blame] | 366 | posval_sort(o, posval); |
| 367 | |
Jens Axboe | 5f6ddf1 | 2010-03-09 20:40:44 +0100 | [diff] [blame] | 368 | ret = 1; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 369 | for (i = 0; i < PARSE_MAX_VP; i++) { |
Jens Axboe | f085737 | 2007-03-19 13:15:23 +0100 | [diff] [blame] | 370 | vp = &posval[i]; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 371 | if (!vp->ival || vp->ival[0] == '\0') |
Jens Axboe | a3073f4 | 2010-03-05 10:06:15 +0100 | [diff] [blame] | 372 | continue; |
Jens Axboe | ae2ddba | 2010-03-10 12:49:03 +0100 | [diff] [blame] | 373 | all_skipped = 0; |
Jens Axboe | 808def7 | 2010-07-14 16:27:02 -0600 | [diff] [blame] | 374 | if (!strncmp(vp->ival, ptr, opt_len(ptr))) { |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 375 | ret = 0; |
Jens Axboe | 5f6ddf1 | 2010-03-09 20:40:44 +0100 | [diff] [blame] | 376 | if (o->roff1) { |
| 377 | if (vp->or) |
| 378 | *(unsigned int *) o->roff1 |= vp->oval; |
| 379 | else |
| 380 | *(unsigned int *) o->roff1 = vp->oval; |
| 381 | } else { |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 382 | if (!o->off1) |
Jens Axboe | 5f6ddf1 | 2010-03-09 20:40:44 +0100 | [diff] [blame] | 383 | continue; |
| 384 | val_store(ilp, vp->oval, o->off1, vp->or, data); |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 385 | } |
Jens Axboe | 5f6ddf1 | 2010-03-09 20:40:44 +0100 | [diff] [blame] | 386 | continue; |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
Jens Axboe | ae2ddba | 2010-03-10 12:49:03 +0100 | [diff] [blame] | 390 | if (ret && !all_skipped) |
Jens Axboe | b1ec1da | 2007-02-23 10:29:16 +0100 | [diff] [blame] | 391 | show_option_values(o); |
| 392 | else if (fn) |
| 393 | ret = fn(data, ptr); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 394 | break; |
| 395 | } |
Jens Axboe | e42b01e | 2011-05-05 12:05:10 -0600 | [diff] [blame] | 396 | case FIO_OPT_STR_VAL_TIME: |
| 397 | is_time = 1; |
Jens Axboe | f7fa265 | 2009-03-09 14:20:20 +0100 | [diff] [blame] | 398 | case FIO_OPT_INT: |
Jens Axboe | e42b01e | 2011-05-05 12:05:10 -0600 | [diff] [blame] | 399 | case FIO_OPT_STR_VAL: { |
| 400 | fio_opt_str_val_fn *fn = o->cb; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 401 | |
Jens Axboe | e42b01e | 2011-05-05 12:05:10 -0600 | [diff] [blame] | 402 | if (is_time) |
| 403 | ret = check_str_time(ptr, &ull); |
| 404 | else |
| 405 | ret = check_str_bytes(ptr, &ull, data); |
| 406 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 407 | if (ret) |
| 408 | break; |
| 409 | |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 410 | if (o->maxval && ull > o->maxval) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 411 | fprintf(stderr, "max value out of range: %lld" |
| 412 | " (%d max)\n", ull, o->maxval); |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 413 | return 1; |
| 414 | } |
| 415 | if (o->minval && ull < o->minval) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 416 | fprintf(stderr, "min value out of range: %lld" |
| 417 | " (%d min)\n", ull, o->minval); |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 418 | return 1; |
| 419 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 420 | |
| 421 | if (fn) |
| 422 | ret = fn(data, &ull); |
| 423 | else { |
Jens Axboe | e01b22b | 2009-06-09 15:43:25 +0200 | [diff] [blame] | 424 | if (o->type == FIO_OPT_INT) { |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 425 | if (first) { |
| 426 | if (o->roff1) |
Jens Axboe | 7758d4f | 2010-03-18 16:50:31 +0100 | [diff] [blame] | 427 | *(unsigned int *) o->roff1 = ull; |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 428 | else |
Jens Axboe | 5f6ddf1 | 2010-03-09 20:40:44 +0100 | [diff] [blame] | 429 | val_store(ilp, ull, o->off1, 0, data); |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 430 | } |
| 431 | if (!more) { |
| 432 | if (o->roff2) |
Jens Axboe | 7758d4f | 2010-03-18 16:50:31 +0100 | [diff] [blame] | 433 | *(unsigned int *) o->roff2 = ull; |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 434 | else if (o->off2) |
Jens Axboe | 5f6ddf1 | 2010-03-09 20:40:44 +0100 | [diff] [blame] | 435 | val_store(ilp, ull, o->off2, 0, data); |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 436 | } |
Jens Axboe | 75e6f36 | 2006-11-03 08:17:09 +0100 | [diff] [blame] | 437 | } else { |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 438 | if (first) { |
| 439 | if (o->roff1) |
| 440 | *(unsigned long long *) o->roff1 = ull; |
| 441 | else |
Jens Axboe | 5f6ddf1 | 2010-03-09 20:40:44 +0100 | [diff] [blame] | 442 | val_store(ullp, ull, o->off1, 0, data); |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 443 | } |
| 444 | if (!more) { |
| 445 | if (o->roff2) |
| 446 | *(unsigned long long *) o->roff2 = ull; |
| 447 | else if (o->off2) |
Jens Axboe | 5f6ddf1 | 2010-03-09 20:40:44 +0100 | [diff] [blame] | 448 | val_store(ullp, ull, o->off2, 0, data); |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 449 | } |
Jens Axboe | 75e6f36 | 2006-11-03 08:17:09 +0100 | [diff] [blame] | 450 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 451 | } |
| 452 | break; |
| 453 | } |
Yu-ju Hong | 8334919 | 2011-08-13 00:53:44 +0200 | [diff] [blame] | 454 | case FIO_OPT_FLOAT_LIST: { |
| 455 | |
| 456 | if (first) { |
| 457 | ul2 = 1; |
| 458 | ilp = td_var(data, o->off2); |
| 459 | *ilp = ul2; |
| 460 | } |
| 461 | if (curr >= o->maxlen) { |
| 462 | fprintf(stderr, "the list exceeding max length %d\n", |
| 463 | o->maxlen); |
| 464 | return 1; |
| 465 | } |
| 466 | if(!str_to_float(ptr, &uf)){ |
| 467 | fprintf(stderr, "not a floating point value: %s\n", |
| 468 | ptr); |
| 469 | return 1; |
| 470 | } |
| 471 | if (!isnan(o->maxfp) && uf > o->maxfp) { |
| 472 | fprintf(stderr, "value out of range: %f" |
| 473 | " (range max: %f)\n", uf, o->maxfp); |
| 474 | return 1; |
| 475 | } |
| 476 | if (!isnan(o->minfp) && uf < o->minfp) { |
| 477 | fprintf(stderr, "value out of range: %f" |
| 478 | " (range min: %f)\n", uf, o->minfp); |
| 479 | return 1; |
| 480 | } |
| 481 | |
| 482 | flp = td_var(data, o->off1); |
| 483 | flp[curr] = uf; |
| 484 | |
| 485 | break; |
| 486 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 487 | case FIO_OPT_STR_STORE: { |
| 488 | fio_opt_str_fn *fn = o->cb; |
| 489 | |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 490 | if (o->roff1) |
| 491 | cp = (char **) o->roff1; |
| 492 | else |
| 493 | cp = td_var(data, o->off1); |
| 494 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 495 | *cp = strdup(ptr); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 496 | if (fn) { |
| 497 | ret = fn(data, ptr); |
| 498 | if (ret) { |
| 499 | free(*cp); |
| 500 | *cp = NULL; |
| 501 | } |
| 502 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 503 | break; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 504 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 505 | case FIO_OPT_RANGE: { |
Jens Axboe | b765a37 | 2006-10-27 15:00:16 +0200 | [diff] [blame] | 506 | char tmp[128]; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 507 | char *p1, *p2; |
| 508 | |
Jens Axboe | 0bbab0e | 2006-11-02 09:18:36 +0100 | [diff] [blame] | 509 | strncpy(tmp, ptr, sizeof(tmp) - 1); |
Jens Axboe | b765a37 | 2006-10-27 15:00:16 +0200 | [diff] [blame] | 510 | |
Dave Engberg | 31d23f4 | 2011-07-23 21:07:13 +0200 | [diff] [blame] | 511 | /* Handle bsrange with separate read,write values: */ |
| 512 | p1 = strchr(tmp, ','); |
| 513 | if (p1) |
| 514 | *p1 = '\0'; |
| 515 | |
Jens Axboe | b765a37 | 2006-10-27 15:00:16 +0200 | [diff] [blame] | 516 | p1 = strchr(tmp, '-'); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 517 | if (!p1) { |
Jens Axboe | 0c9baf9 | 2007-01-11 15:59:26 +0100 | [diff] [blame] | 518 | p1 = strchr(tmp, ':'); |
| 519 | if (!p1) { |
| 520 | ret = 1; |
| 521 | break; |
| 522 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | p2 = p1 + 1; |
| 526 | *p1 = '\0'; |
Jens Axboe | b765a37 | 2006-10-27 15:00:16 +0200 | [diff] [blame] | 527 | p1 = tmp; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 528 | |
| 529 | ret = 1; |
Jens Axboe | d6978a3 | 2009-07-18 21:04:09 +0200 | [diff] [blame] | 530 | if (!check_range_bytes(p1, &ul1, data) && |
| 531 | !check_range_bytes(p2, &ul2, data)) { |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 532 | ret = 0; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 533 | if (ul1 > ul2) { |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 534 | unsigned long foo = ul1; |
| 535 | |
| 536 | ul1 = ul2; |
| 537 | ul2 = foo; |
| 538 | } |
| 539 | |
| 540 | if (first) { |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 541 | if (o->roff1) |
Jens Axboe | 7758d4f | 2010-03-18 16:50:31 +0100 | [diff] [blame] | 542 | *(unsigned int *) o->roff1 = ul1; |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 543 | else |
Jens Axboe | 5f6ddf1 | 2010-03-09 20:40:44 +0100 | [diff] [blame] | 544 | val_store(ilp, ul1, o->off1, 0, data); |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 545 | if (o->roff2) |
Jens Axboe | 7758d4f | 2010-03-18 16:50:31 +0100 | [diff] [blame] | 546 | *(unsigned int *) o->roff2 = ul2; |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 547 | else |
Jens Axboe | 5f6ddf1 | 2010-03-09 20:40:44 +0100 | [diff] [blame] | 548 | val_store(ilp, ul2, o->off2, 0, data); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 549 | } |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 550 | if (o->roff3 && o->roff4) { |
Jens Axboe | 7758d4f | 2010-03-18 16:50:31 +0100 | [diff] [blame] | 551 | *(unsigned int *) o->roff3 = ul1; |
| 552 | *(unsigned int *) o->roff4 = ul2; |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 553 | } else if (o->off3 && o->off4) { |
Jens Axboe | 5f6ddf1 | 2010-03-09 20:40:44 +0100 | [diff] [blame] | 554 | val_store(ilp, ul1, o->off3, 0, data); |
| 555 | val_store(ilp, ul2, o->off4, 0, data); |
Jens Axboe | 17abbe8 | 2006-11-06 11:23:10 +0100 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 559 | break; |
| 560 | } |
Jens Axboe | 74ba180 | 2011-07-15 09:09:15 +0200 | [diff] [blame] | 561 | case FIO_OPT_BOOL: |
| 562 | case FIO_OPT_STR_SET: { |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 563 | fio_opt_int_fn *fn = o->cb; |
| 564 | |
Jens Axboe | 74ba180 | 2011-07-15 09:09:15 +0200 | [diff] [blame] | 565 | if (ptr) |
| 566 | ret = check_int(ptr, &il); |
| 567 | else if (o->type == FIO_OPT_BOOL) |
| 568 | ret = 1; |
| 569 | else |
| 570 | il = 1; |
| 571 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 572 | if (ret) |
| 573 | break; |
| 574 | |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 575 | if (o->maxval && il > (int) o->maxval) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 576 | fprintf(stderr, "max value out of range: %d (%d max)\n", |
| 577 | il, o->maxval); |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 578 | return 1; |
| 579 | } |
| 580 | if (o->minval && il < o->minval) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 581 | fprintf(stderr, "min value out of range: %d (%d min)\n", |
| 582 | il, o->minval); |
Jens Axboe | db8e016 | 2007-01-10 13:41:26 +0100 | [diff] [blame] | 583 | return 1; |
| 584 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 585 | |
Jens Axboe | 76a43db | 2007-01-11 13:24:44 +0100 | [diff] [blame] | 586 | if (o->neg) |
| 587 | il = !il; |
| 588 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 589 | if (fn) |
| 590 | ret = fn(data, &il); |
| 591 | else { |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 592 | if (first) { |
| 593 | if (o->roff1) |
| 594 | *(unsigned int *)o->roff1 = il; |
| 595 | else |
Jens Axboe | 5f6ddf1 | 2010-03-09 20:40:44 +0100 | [diff] [blame] | 596 | val_store(ilp, il, o->off1, 0, data); |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 597 | } |
| 598 | if (!more) { |
| 599 | if (o->roff2) |
| 600 | *(unsigned int *) o->roff2 = il; |
| 601 | else if (o->off2) |
Jens Axboe | 5f6ddf1 | 2010-03-09 20:40:44 +0100 | [diff] [blame] | 602 | val_store(ilp, il, o->off2, 0, data); |
Jens Axboe | 02c6aad | 2010-03-04 13:49:11 +0100 | [diff] [blame] | 603 | } |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 604 | } |
| 605 | break; |
| 606 | } |
Jens Axboe | 15ca150 | 2008-04-07 09:26:02 +0200 | [diff] [blame] | 607 | case FIO_OPT_DEPRECATED: |
| 608 | fprintf(stdout, "Option %s is deprecated\n", o->name); |
| 609 | break; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 610 | default: |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 611 | fprintf(stderr, "Bad option type %u\n", o->type); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 612 | ret = 1; |
| 613 | } |
| 614 | |
Jens Axboe | 70a4c0c | 2009-07-01 09:24:05 +0200 | [diff] [blame] | 615 | if (ret) |
| 616 | return ret; |
| 617 | |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 618 | if (o->verify) { |
Jens Axboe | 70a4c0c | 2009-07-01 09:24:05 +0200 | [diff] [blame] | 619 | ret = o->verify(o, data); |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 620 | if (ret) { |
| 621 | fprintf(stderr,"Correct format for offending option\n"); |
| 622 | fprintf(stderr, "%20s: %s\n", o->name, o->help); |
| 623 | show_option_help(o, stderr); |
| 624 | } |
| 625 | } |
Jens Axboe | 70a4c0c | 2009-07-01 09:24:05 +0200 | [diff] [blame] | 626 | |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 627 | return ret; |
| 628 | } |
| 629 | |
Jens Axboe | 7c8f1a5 | 2009-06-09 12:28:57 +0200 | [diff] [blame] | 630 | 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] | 631 | { |
Jens Axboe | b62bdf2 | 2010-03-10 12:36:17 +0100 | [diff] [blame] | 632 | char *o_ptr, *ptr, *ptr2; |
| 633 | int ret, done; |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 634 | |
Jens Axboe | 7c8f1a5 | 2009-06-09 12:28:57 +0200 | [diff] [blame] | 635 | dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr); |
| 636 | |
Jens Axboe | b62bdf2 | 2010-03-10 12:36:17 +0100 | [diff] [blame] | 637 | o_ptr = ptr = NULL; |
Jens Axboe | 7c8f1a5 | 2009-06-09 12:28:57 +0200 | [diff] [blame] | 638 | if (__ptr) |
Jens Axboe | b62bdf2 | 2010-03-10 12:36:17 +0100 | [diff] [blame] | 639 | o_ptr = ptr = strdup(__ptr); |
Jens Axboe | a3d741f | 2008-02-27 18:32:33 +0100 | [diff] [blame] | 640 | |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 641 | /* |
Jens Axboe | b62bdf2 | 2010-03-10 12:36:17 +0100 | [diff] [blame] | 642 | * See if we have another set of parameters, hidden after a comma. |
| 643 | * Do this before parsing this round, to check if we should |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 644 | * copy set 1 options to set 2. |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 645 | */ |
Jens Axboe | b62bdf2 | 2010-03-10 12:36:17 +0100 | [diff] [blame] | 646 | done = 0; |
| 647 | ret = 1; |
| 648 | do { |
| 649 | int __ret; |
| 650 | |
| 651 | ptr2 = NULL; |
| 652 | if (ptr && |
| 653 | (o->type != FIO_OPT_STR_STORE) && |
Yu-ju Hong | 8334919 | 2011-08-13 00:53:44 +0200 | [diff] [blame] | 654 | (o->type != FIO_OPT_STR) && |
| 655 | (o->type != FIO_OPT_FLOAT_LIST)) { |
Jens Axboe | b62bdf2 | 2010-03-10 12:36:17 +0100 | [diff] [blame] | 656 | ptr2 = strchr(ptr, ','); |
| 657 | if (ptr2 && *(ptr2 + 1) == '\0') |
| 658 | *ptr2 = '\0'; |
| 659 | if (o->type != FIO_OPT_STR_MULTI) { |
| 660 | if (!ptr2) |
| 661 | ptr2 = strchr(ptr, ':'); |
| 662 | if (!ptr2) |
| 663 | ptr2 = strchr(ptr, '-'); |
| 664 | } |
Yu-ju Hong | 8334919 | 2011-08-13 00:53:44 +0200 | [diff] [blame] | 665 | } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) { |
| 666 | ptr2 = strchr(ptr, ':'); |
Jens Axboe | b62bdf2 | 2010-03-10 12:36:17 +0100 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /* |
| 670 | * Don't return early if parsing the first option fails - if |
| 671 | * we are doing multiple arguments, we can allow the first one |
| 672 | * being empty. |
| 673 | */ |
Yu-ju Hong | 8334919 | 2011-08-13 00:53:44 +0200 | [diff] [blame] | 674 | __ret = __handle_option(o, ptr, data, !done, !!ptr2, done); |
Jens Axboe | b62bdf2 | 2010-03-10 12:36:17 +0100 | [diff] [blame] | 675 | if (ret) |
| 676 | ret = __ret; |
| 677 | |
Jens Axboe | 0c9baf9 | 2007-01-11 15:59:26 +0100 | [diff] [blame] | 678 | if (!ptr2) |
Jens Axboe | b62bdf2 | 2010-03-10 12:36:17 +0100 | [diff] [blame] | 679 | break; |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 680 | |
Jens Axboe | b62bdf2 | 2010-03-10 12:36:17 +0100 | [diff] [blame] | 681 | ptr = ptr2 + 1; |
| 682 | done++; |
| 683 | } while (1); |
Jens Axboe | 787f7e9 | 2006-11-06 13:26:29 +0100 | [diff] [blame] | 684 | |
Jens Axboe | b62bdf2 | 2010-03-10 12:36:17 +0100 | [diff] [blame] | 685 | if (o_ptr) |
| 686 | free(o_ptr); |
| 687 | return ret; |
Jens Axboe | f90eff5 | 2006-11-06 11:08:21 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 690 | static struct fio_option *get_option(const char *opt, |
Jens Axboe | 07b3232 | 2010-03-05 09:48:44 +0100 | [diff] [blame] | 691 | struct fio_option *options, char **post) |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 692 | { |
| 693 | struct fio_option *o; |
| 694 | char *ret; |
| 695 | |
| 696 | ret = strchr(opt, '='); |
| 697 | if (ret) { |
| 698 | *post = ret; |
| 699 | *ret = '\0'; |
| 700 | ret = (char *) opt; |
| 701 | (*post)++; |
Jens Axboe | 43c129b | 2008-12-08 14:06:42 +0100 | [diff] [blame] | 702 | strip_blank_end(ret); |
Jens Axboe | 07b3232 | 2010-03-05 09:48:44 +0100 | [diff] [blame] | 703 | o = find_option(options, ret); |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 704 | } else { |
Jens Axboe | 07b3232 | 2010-03-05 09:48:44 +0100 | [diff] [blame] | 705 | o = find_option(options, opt); |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 706 | *post = NULL; |
| 707 | } |
| 708 | |
| 709 | return o; |
| 710 | } |
| 711 | |
| 712 | static int opt_cmp(const void *p1, const void *p2) |
| 713 | { |
| 714 | struct fio_option *o1, *o2; |
| 715 | char *s1, *s2, *foo; |
Jens Axboe | 8cdabc1 | 2008-11-19 12:31:43 +0100 | [diff] [blame] | 716 | int prio1, prio2; |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 717 | |
| 718 | s1 = strdup(*((char **) p1)); |
| 719 | s2 = strdup(*((char **) p2)); |
| 720 | |
Jens Axboe | 07b3232 | 2010-03-05 09:48:44 +0100 | [diff] [blame] | 721 | o1 = get_option(s1, fio_options, &foo); |
| 722 | o2 = get_option(s2, fio_options, &foo); |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 723 | |
Jens Axboe | 8cdabc1 | 2008-11-19 12:31:43 +0100 | [diff] [blame] | 724 | prio1 = prio2 = 0; |
| 725 | if (o1) |
| 726 | prio1 = o1->prio; |
| 727 | if (o2) |
| 728 | prio2 = o2->prio; |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 729 | |
| 730 | free(s1); |
| 731 | free(s2); |
Jens Axboe | 8cdabc1 | 2008-11-19 12:31:43 +0100 | [diff] [blame] | 732 | return prio2 - prio1; |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | void sort_options(char **opts, struct fio_option *options, int num_opts) |
| 736 | { |
| 737 | fio_options = options; |
| 738 | qsort(opts, num_opts, sizeof(char *), opt_cmp); |
| 739 | fio_options = NULL; |
| 740 | } |
| 741 | |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 742 | int parse_cmd_option(const char *opt, const char *val, |
Jens Axboe | 07b3232 | 2010-03-05 09:48:44 +0100 | [diff] [blame] | 743 | struct fio_option *options, void *data) |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 744 | { |
| 745 | struct fio_option *o; |
| 746 | |
Jens Axboe | 07b3232 | 2010-03-05 09:48:44 +0100 | [diff] [blame] | 747 | o = find_option(options, opt); |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 748 | if (!o) { |
Jens Axboe | 43c129b | 2008-12-08 14:06:42 +0100 | [diff] [blame] | 749 | fprintf(stderr, "Bad option <%s>\n", opt); |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 750 | return 1; |
| 751 | } |
| 752 | |
Jens Axboe | b1508cf | 2006-11-02 09:12:40 +0100 | [diff] [blame] | 753 | if (!handle_option(o, val, data)) |
| 754 | return 0; |
| 755 | |
| 756 | fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val); |
| 757 | return 1; |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 758 | } |
| 759 | |
Aaron Carroll | 88b5a39 | 2008-10-07 11:25:20 +0200 | [diff] [blame] | 760 | /* |
| 761 | * Return a copy of the input string with substrings of the form ${VARNAME} |
| 762 | * substituted with the value of the environment variable VARNAME. The |
| 763 | * substitution always occurs, even if VARNAME is empty or the corresponding |
| 764 | * environment variable undefined. |
| 765 | */ |
| 766 | static char *option_dup_subs(const char *opt) |
| 767 | { |
| 768 | char out[OPT_LEN_MAX+1]; |
| 769 | char in[OPT_LEN_MAX+1]; |
| 770 | char *outptr = out; |
| 771 | char *inptr = in; |
| 772 | char *ch1, *ch2, *env; |
| 773 | ssize_t nchr = OPT_LEN_MAX; |
| 774 | size_t envlen; |
| 775 | |
Jens Axboe | a0741cb | 2010-03-05 12:46:37 +0100 | [diff] [blame] | 776 | if (strlen(opt) + 1 > OPT_LEN_MAX) { |
Jens Axboe | 38789b5 | 2010-03-03 09:23:20 +0100 | [diff] [blame] | 777 | fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX); |
| 778 | return NULL; |
| 779 | } |
| 780 | |
Aaron Carroll | 88b5a39 | 2008-10-07 11:25:20 +0200 | [diff] [blame] | 781 | in[OPT_LEN_MAX] = '\0'; |
| 782 | strncpy(in, opt, OPT_LEN_MAX); |
| 783 | |
| 784 | while (*inptr && nchr > 0) { |
| 785 | if (inptr[0] == '$' && inptr[1] == '{') { |
| 786 | ch2 = strchr(inptr, '}'); |
| 787 | if (ch2 && inptr+1 < ch2) { |
| 788 | ch1 = inptr+2; |
| 789 | inptr = ch2+1; |
| 790 | *ch2 = '\0'; |
| 791 | |
| 792 | env = getenv(ch1); |
| 793 | if (env) { |
| 794 | envlen = strlen(env); |
| 795 | if (envlen <= nchr) { |
| 796 | memcpy(outptr, env, envlen); |
| 797 | outptr += envlen; |
| 798 | nchr -= envlen; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | continue; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | *outptr++ = *inptr++; |
| 807 | --nchr; |
| 808 | } |
| 809 | |
| 810 | *outptr = '\0'; |
| 811 | return strdup(out); |
| 812 | } |
| 813 | |
Jens Axboe | 07b3232 | 2010-03-05 09:48:44 +0100 | [diff] [blame] | 814 | int parse_option(const char *opt, struct fio_option *options, void *data) |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 815 | { |
Jens Axboe | b469282 | 2006-10-27 13:43:22 +0200 | [diff] [blame] | 816 | struct fio_option *o; |
Jens Axboe | 3b8b713 | 2008-06-10 19:46:23 +0200 | [diff] [blame] | 817 | char *post, *tmp; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 818 | |
Aaron Carroll | 88b5a39 | 2008-10-07 11:25:20 +0200 | [diff] [blame] | 819 | tmp = option_dup_subs(opt); |
Jens Axboe | 38789b5 | 2010-03-03 09:23:20 +0100 | [diff] [blame] | 820 | if (!tmp) |
| 821 | return 1; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 822 | |
Jens Axboe | 07b3232 | 2010-03-05 09:48:44 +0100 | [diff] [blame] | 823 | o = get_option(tmp, options, &post); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 824 | if (!o) { |
Jens Axboe | 43c129b | 2008-12-08 14:06:42 +0100 | [diff] [blame] | 825 | fprintf(stderr, "Bad option <%s>\n", tmp); |
Jens Axboe | 0401bca | 2007-03-29 11:00:43 +0200 | [diff] [blame] | 826 | free(tmp); |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 827 | return 1; |
| 828 | } |
| 829 | |
Jens Axboe | 0401bca | 2007-03-29 11:00:43 +0200 | [diff] [blame] | 830 | if (!handle_option(o, post, data)) { |
| 831 | free(tmp); |
Jens Axboe | b1508cf | 2006-11-02 09:12:40 +0100 | [diff] [blame] | 832 | return 0; |
Jens Axboe | 0401bca | 2007-03-29 11:00:43 +0200 | [diff] [blame] | 833 | } |
Jens Axboe | b1508cf | 2006-11-02 09:12:40 +0100 | [diff] [blame] | 834 | |
| 835 | fprintf(stderr, "fio: failed parsing %s\n", opt); |
Jens Axboe | 0401bca | 2007-03-29 11:00:43 +0200 | [diff] [blame] | 836 | free(tmp); |
Jens Axboe | b1508cf | 2006-11-02 09:12:40 +0100 | [diff] [blame] | 837 | return 1; |
Jens Axboe | e1f3650 | 2006-10-27 10:54:08 +0200 | [diff] [blame] | 838 | } |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 839 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 840 | /* |
| 841 | * Option match, levenshtein distance. Handy for not quite remembering what |
| 842 | * the option name is. |
| 843 | */ |
| 844 | static int string_distance(const char *s1, const char *s2) |
| 845 | { |
| 846 | unsigned int s1_len = strlen(s1); |
| 847 | unsigned int s2_len = strlen(s2); |
| 848 | unsigned int *p, *q, *r; |
| 849 | unsigned int i, j; |
| 850 | |
| 851 | p = malloc(sizeof(unsigned int) * (s2_len + 1)); |
| 852 | q = malloc(sizeof(unsigned int) * (s2_len + 1)); |
| 853 | |
| 854 | p[0] = 0; |
| 855 | for (i = 1; i <= s2_len; i++) |
| 856 | p[i] = p[i - 1] + 1; |
| 857 | |
| 858 | for (i = 1; i <= s1_len; i++) { |
| 859 | q[0] = p[0] + 1; |
| 860 | for (j = 1; j <= s2_len; j++) { |
| 861 | unsigned int sub = p[j - 1]; |
| 862 | |
| 863 | if (s1[i - 1] != s2[j - 1]) |
| 864 | sub++; |
| 865 | |
| 866 | q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub)); |
| 867 | } |
| 868 | r = p; |
| 869 | p = q; |
| 870 | q = r; |
| 871 | } |
| 872 | |
| 873 | i = p[s2_len]; |
| 874 | free(p); |
| 875 | free(q); |
| 876 | return i; |
| 877 | } |
| 878 | |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 879 | static struct fio_option *find_child(struct fio_option *options, |
| 880 | struct fio_option *o) |
| 881 | { |
| 882 | struct fio_option *__o; |
| 883 | |
Jens Axboe | fdf2874 | 2007-07-31 23:06:09 +0200 | [diff] [blame] | 884 | for (__o = options + 1; __o->name; __o++) |
| 885 | if (__o->parent && !strcmp(__o->parent, o->name)) |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 886 | return __o; |
| 887 | |
| 888 | return NULL; |
| 889 | } |
| 890 | |
Jens Axboe | 323d911 | 2008-03-01 15:12:14 +0100 | [diff] [blame] | 891 | static void __print_option(struct fio_option *o, struct fio_option *org, |
| 892 | int level) |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 893 | { |
| 894 | char name[256], *p; |
Jens Axboe | 323d911 | 2008-03-01 15:12:14 +0100 | [diff] [blame] | 895 | int depth; |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 896 | |
| 897 | if (!o) |
| 898 | return; |
Jens Axboe | ef9aff5 | 2007-07-31 22:56:53 +0200 | [diff] [blame] | 899 | if (!org) |
| 900 | org = o; |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 901 | |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 902 | p = name; |
Jens Axboe | 323d911 | 2008-03-01 15:12:14 +0100 | [diff] [blame] | 903 | depth = level; |
| 904 | while (depth--) |
| 905 | p += sprintf(p, "%s", " "); |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 906 | |
| 907 | sprintf(p, "%s", o->name); |
| 908 | |
| 909 | printf("%-24s: %s\n", name, o->help); |
Jens Axboe | 323d911 | 2008-03-01 15:12:14 +0100 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | static void print_option(struct fio_option *o) |
| 913 | { |
| 914 | struct fio_option *parent; |
| 915 | struct fio_option *__o; |
| 916 | unsigned int printed; |
| 917 | unsigned int level; |
| 918 | |
| 919 | __print_option(o, NULL, 0); |
| 920 | parent = o; |
| 921 | level = 0; |
| 922 | do { |
| 923 | level++; |
| 924 | printed = 0; |
| 925 | |
| 926 | while ((__o = find_child(o, parent)) != NULL) { |
| 927 | __print_option(__o, o, level); |
| 928 | o = __o; |
| 929 | printed++; |
| 930 | } |
| 931 | |
| 932 | parent = o; |
| 933 | } while (printed); |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 934 | } |
| 935 | |
Jens Axboe | 07b3232 | 2010-03-05 09:48:44 +0100 | [diff] [blame] | 936 | int show_cmd_help(struct fio_option *options, const char *name) |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 937 | { |
| 938 | struct fio_option *o, *closest; |
Jens Axboe | d091d09 | 2010-04-01 19:56:23 +0200 | [diff] [blame] | 939 | unsigned int best_dist = -1U; |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 940 | int found = 0; |
Jens Axboe | 320beef | 2007-03-01 10:44:12 +0100 | [diff] [blame] | 941 | int show_all = 0; |
| 942 | |
| 943 | if (!name || !strcmp(name, "all")) |
| 944 | show_all = 1; |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 945 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 946 | closest = NULL; |
| 947 | best_dist = -1; |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 948 | for (o = &options[0]; o->name; o++) { |
Jens Axboe | 320beef | 2007-03-01 10:44:12 +0100 | [diff] [blame] | 949 | int match = 0; |
| 950 | |
Jens Axboe | 15ca150 | 2008-04-07 09:26:02 +0200 | [diff] [blame] | 951 | if (o->type == FIO_OPT_DEPRECATED) |
| 952 | continue; |
Jens Axboe | 07b3232 | 2010-03-05 09:48:44 +0100 | [diff] [blame] | 953 | if (!exec_profile && o->prof_name) |
| 954 | continue; |
Jens Axboe | 15ca150 | 2008-04-07 09:26:02 +0200 | [diff] [blame] | 955 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 956 | if (name) { |
Jens Axboe | 7f9348f | 2007-03-15 14:09:28 +0100 | [diff] [blame] | 957 | if (!strcmp(name, o->name) || |
| 958 | (o->alias && !strcmp(name, o->alias))) |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 959 | match = 1; |
| 960 | else { |
| 961 | unsigned int dist; |
| 962 | |
| 963 | dist = string_distance(name, o->name); |
| 964 | if (dist < best_dist) { |
| 965 | best_dist = dist; |
| 966 | closest = o; |
| 967 | } |
| 968 | } |
| 969 | } |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 970 | |
| 971 | if (show_all || match) { |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 972 | found = 1; |
Jens Axboe | c167ded | 2007-03-14 13:02:53 +0100 | [diff] [blame] | 973 | if (match) |
Jens Axboe | 07b3232 | 2010-03-05 09:48:44 +0100 | [diff] [blame] | 974 | printf("%20s: %s\n", o->name, o->help); |
Jens Axboe | c167ded | 2007-03-14 13:02:53 +0100 | [diff] [blame] | 975 | if (show_all) { |
Jens Axboe | afdf935 | 2007-07-31 16:14:34 +0200 | [diff] [blame] | 976 | if (!o->parent) |
Jens Axboe | 323d911 | 2008-03-01 15:12:14 +0100 | [diff] [blame] | 977 | print_option(o); |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 978 | continue; |
Jens Axboe | c167ded | 2007-03-14 13:02:53 +0100 | [diff] [blame] | 979 | } |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 980 | } |
| 981 | |
Jens Axboe | 70df2f1 | 2007-01-11 14:04:47 +0100 | [diff] [blame] | 982 | if (!match) |
| 983 | continue; |
| 984 | |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 985 | show_option_help(o, stdout); |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 986 | } |
| 987 | |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 988 | if (found) |
| 989 | return 0; |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 990 | |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 991 | printf("No such command: %s", name); |
Jens Axboe | d091d09 | 2010-04-01 19:56:23 +0200 | [diff] [blame] | 992 | |
| 993 | /* |
| 994 | * Only print an appropriately close option, one where the edit |
| 995 | * distance isn't too big. Otherwise we get crazy matches. |
| 996 | */ |
| 997 | if (closest && best_dist < 3) { |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 998 | printf(" - showing closest match\n"); |
| 999 | printf("%20s: %s\n", closest->name, closest->help); |
Jens Axboe | d447a8c | 2009-07-17 22:26:15 +0200 | [diff] [blame] | 1000 | show_option_help(closest, stdout); |
Jens Axboe | 0e9f7fa | 2007-03-01 14:05:30 +0100 | [diff] [blame] | 1001 | } else |
| 1002 | printf("\n"); |
| 1003 | |
Jens Axboe | 29fc6af | 2007-01-09 21:22:02 +0100 | [diff] [blame] | 1004 | return 1; |
Jens Axboe | fd28ca4 | 2007-01-09 21:20:13 +0100 | [diff] [blame] | 1005 | } |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 1006 | |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 1007 | /* |
| 1008 | * Handle parsing of default parameters. |
| 1009 | */ |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 1010 | void fill_default_options(void *data, struct fio_option *options) |
| 1011 | { |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 1012 | struct fio_option *o; |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 1013 | |
Jens Axboe | a3d741f | 2008-02-27 18:32:33 +0100 | [diff] [blame] | 1014 | dprint(FD_PARSE, "filling default options\n"); |
| 1015 | |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 1016 | for (o = &options[0]; o->name; o++) |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 1017 | if (o->def) |
| 1018 | handle_option(o, o->def, data); |
Jens Axboe | ee73849 | 2007-01-10 11:23:16 +0100 | [diff] [blame] | 1019 | } |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 1020 | |
Jens Axboe | 9f988e2 | 2010-03-04 10:42:38 +0100 | [diff] [blame] | 1021 | void option_init(struct fio_option *o) |
| 1022 | { |
| 1023 | if (o->type == FIO_OPT_DEPRECATED) |
| 1024 | return; |
| 1025 | if (o->type == FIO_OPT_BOOL) { |
| 1026 | o->minval = 0; |
| 1027 | o->maxval = 1; |
| 1028 | } |
Yu-ju Hong | 8334919 | 2011-08-13 00:53:44 +0200 | [diff] [blame] | 1029 | if (o->type == FIO_OPT_FLOAT_LIST) { |
| 1030 | o->minfp = NAN; |
| 1031 | o->maxfp = NAN; |
| 1032 | } |
Jens Axboe | 9f988e2 | 2010-03-04 10:42:38 +0100 | [diff] [blame] | 1033 | if (o->type == FIO_OPT_STR_SET && o->def) { |
| 1034 | fprintf(stderr, "Option %s: string set option with" |
| 1035 | " default will always be true\n", o->name); |
| 1036 | } |
Jens Axboe | e2de69d | 2010-03-04 14:05:48 +0100 | [diff] [blame] | 1037 | if (!o->cb && (!o->off1 && !o->roff1)) { |
Jens Axboe | 9f988e2 | 2010-03-04 10:42:38 +0100 | [diff] [blame] | 1038 | fprintf(stderr, "Option %s: neither cb nor offset given\n", |
| 1039 | o->name); |
| 1040 | } |
Jens Axboe | 5f6ddf1 | 2010-03-09 20:40:44 +0100 | [diff] [blame] | 1041 | if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE || |
| 1042 | o->type == FIO_OPT_STR_MULTI) |
Jens Axboe | 9f988e2 | 2010-03-04 10:42:38 +0100 | [diff] [blame] | 1043 | return; |
Jens Axboe | e2de69d | 2010-03-04 14:05:48 +0100 | [diff] [blame] | 1044 | if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) || |
| 1045 | (o->roff1 || o->roff2 || o->roff3 || o->roff4))) { |
Jens Axboe | 9f988e2 | 2010-03-04 10:42:38 +0100 | [diff] [blame] | 1046 | fprintf(stderr, "Option %s: both cb and offset given\n", |
| 1047 | o->name); |
| 1048 | } |
| 1049 | } |
| 1050 | |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 1051 | /* |
| 1052 | * 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] | 1053 | * values and whether both callback and offsets are given. |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 1054 | */ |
| 1055 | void options_init(struct fio_option *options) |
| 1056 | { |
Jens Axboe | 4945ba1 | 2007-01-11 14:36:56 +0100 | [diff] [blame] | 1057 | struct fio_option *o; |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 1058 | |
Jens Axboe | a3d741f | 2008-02-27 18:32:33 +0100 | [diff] [blame] | 1059 | dprint(FD_PARSE, "init options\n"); |
| 1060 | |
Jens Axboe | 9f988e2 | 2010-03-04 10:42:38 +0100 | [diff] [blame] | 1061 | for (o = &options[0]; o->name; o++) |
| 1062 | option_init(o); |
Jens Axboe | 13335dd | 2007-01-10 13:14:02 +0100 | [diff] [blame] | 1063 | } |