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