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