Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Commandline option parsing functions |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * Copyright (c) 2009 Kevin Wolf <kwolf@redhat.com> |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | * THE SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | #include "qemu-common.h" |
| 30 | #include "qemu-option.h" |
| 31 | |
| 32 | /* |
| 33 | * Extracts the name of an option from the parameter string (p points at the |
| 34 | * first byte of the option name) |
| 35 | * |
| 36 | * The option name is delimited by delim (usually , or =) or the string end |
| 37 | * and is copied into buf. If the option name is longer than buf_size, it is |
| 38 | * truncated. buf is always zero terminated. |
| 39 | * |
| 40 | * The return value is the position of the delimiter/zero byte after the option |
| 41 | * name in p. |
| 42 | */ |
| 43 | const char *get_opt_name(char *buf, int buf_size, const char *p, char delim) |
| 44 | { |
| 45 | char *q; |
| 46 | |
| 47 | q = buf; |
| 48 | while (*p != '\0' && *p != delim) { |
| 49 | if (q && (q - buf) < buf_size - 1) |
| 50 | *q++ = *p; |
| 51 | p++; |
| 52 | } |
| 53 | if (q) |
| 54 | *q = '\0'; |
| 55 | |
| 56 | return p; |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Extracts the value of an option from the parameter string p (p points at the |
| 61 | * first byte of the option value) |
| 62 | * |
| 63 | * This function is comparable to get_opt_name with the difference that the |
| 64 | * delimiter is fixed to be comma which starts a new option. To specify an |
| 65 | * option value that contains commas, double each comma. |
| 66 | */ |
| 67 | const char *get_opt_value(char *buf, int buf_size, const char *p) |
| 68 | { |
| 69 | char *q; |
| 70 | |
| 71 | q = buf; |
| 72 | while (*p != '\0') { |
| 73 | if (*p == ',') { |
| 74 | if (*(p + 1) != ',') |
| 75 | break; |
| 76 | p++; |
| 77 | } |
| 78 | if (q && (q - buf) < buf_size - 1) |
| 79 | *q++ = *p; |
| 80 | p++; |
| 81 | } |
| 82 | if (q) |
| 83 | *q = '\0'; |
| 84 | |
| 85 | return p; |
| 86 | } |
| 87 | |
Gerd Hoffmann | 62c5802 | 2009-07-22 16:43:00 +0200 | [diff] [blame] | 88 | int get_next_param_value(char *buf, int buf_size, |
| 89 | const char *tag, const char **pstr) |
| 90 | { |
| 91 | const char *p; |
| 92 | char option[128]; |
| 93 | |
| 94 | p = *pstr; |
| 95 | for(;;) { |
| 96 | p = get_opt_name(option, sizeof(option), p, '='); |
| 97 | if (*p != '=') |
| 98 | break; |
| 99 | p++; |
| 100 | if (!strcmp(tag, option)) { |
| 101 | *pstr = get_opt_value(buf, buf_size, p); |
| 102 | if (**pstr == ',') { |
| 103 | (*pstr)++; |
| 104 | } |
| 105 | return strlen(buf); |
| 106 | } else { |
| 107 | p = get_opt_value(NULL, 0, p); |
| 108 | } |
| 109 | if (*p != ',') |
| 110 | break; |
| 111 | p++; |
| 112 | } |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int get_param_value(char *buf, int buf_size, |
| 117 | const char *tag, const char *str) |
| 118 | { |
| 119 | return get_next_param_value(buf, buf_size, tag, &str); |
| 120 | } |
| 121 | |
| 122 | int check_params(char *buf, int buf_size, |
| 123 | const char * const *params, const char *str) |
| 124 | { |
| 125 | const char *p; |
| 126 | int i; |
| 127 | |
| 128 | p = str; |
| 129 | while (*p != '\0') { |
| 130 | p = get_opt_name(buf, buf_size, p, '='); |
| 131 | if (*p != '=') { |
| 132 | return -1; |
| 133 | } |
| 134 | p++; |
| 135 | for (i = 0; params[i] != NULL; i++) { |
| 136 | if (!strcmp(params[i], buf)) { |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | if (params[i] == NULL) { |
| 141 | return -1; |
| 142 | } |
| 143 | p = get_opt_value(NULL, 0, p); |
| 144 | if (*p != ',') { |
| 145 | break; |
| 146 | } |
| 147 | p++; |
| 148 | } |
| 149 | return 0; |
| 150 | } |
| 151 | |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 152 | /* |
| 153 | * Searches an option list for an option with the given name |
| 154 | */ |
| 155 | QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list, |
| 156 | const char *name) |
| 157 | { |
| 158 | while (list && list->name) { |
| 159 | if (!strcmp(list->name, name)) { |
| 160 | return list; |
| 161 | } |
| 162 | list++; |
| 163 | } |
| 164 | |
| 165 | return NULL; |
| 166 | } |
| 167 | |
Gerd Hoffmann | 67b1355 | 2009-07-22 16:43:01 +0200 | [diff] [blame] | 168 | static int parse_option_bool(const char *name, const char *value, int *ret) |
| 169 | { |
| 170 | if (value != NULL) { |
| 171 | if (!strcmp(value, "on")) { |
| 172 | *ret = 1; |
| 173 | } else if (!strcmp(value, "off")) { |
| 174 | *ret = 0; |
| 175 | } else { |
| 176 | fprintf(stderr, "Option '%s': Use 'on' or 'off'\n", name); |
| 177 | return -1; |
| 178 | } |
| 179 | } else { |
| 180 | *ret = 1; |
| 181 | } |
| 182 | return 0; |
| 183 | } |
| 184 | |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 185 | static int parse_option_number(const char *name, const char *value, uint64_t *ret) |
| 186 | { |
| 187 | char *postfix; |
| 188 | uint64_t number; |
| 189 | |
| 190 | if (value != NULL) { |
| 191 | number = strtoull(value, &postfix, 0); |
| 192 | if (*postfix != '\0') { |
| 193 | fprintf(stderr, "Option '%s' needs a number as parameter\n", name); |
| 194 | return -1; |
| 195 | } |
Gerd Hoffmann | 21c9f4c | 2009-07-29 10:39:59 +0200 | [diff] [blame] | 196 | *ret = number; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 197 | } else { |
| 198 | fprintf(stderr, "Option '%s' needs a parameter\n", name); |
| 199 | return -1; |
| 200 | } |
| 201 | return 0; |
| 202 | } |
| 203 | |
Gerd Hoffmann | 7695019 | 2009-07-22 16:43:02 +0200 | [diff] [blame] | 204 | static int parse_option_size(const char *name, const char *value, uint64_t *ret) |
| 205 | { |
| 206 | char *postfix; |
| 207 | double sizef; |
| 208 | |
| 209 | if (value != NULL) { |
| 210 | sizef = strtod(value, &postfix); |
| 211 | switch (*postfix) { |
| 212 | case 'T': |
| 213 | sizef *= 1024; |
| 214 | case 'G': |
| 215 | sizef *= 1024; |
| 216 | case 'M': |
| 217 | sizef *= 1024; |
| 218 | case 'K': |
| 219 | case 'k': |
| 220 | sizef *= 1024; |
| 221 | case 'b': |
| 222 | case '\0': |
| 223 | *ret = (uint64_t) sizef; |
| 224 | break; |
| 225 | default: |
| 226 | fprintf(stderr, "Option '%s' needs size as parameter\n", name); |
| 227 | fprintf(stderr, "You may use k, M, G or T suffixes for " |
| 228 | "kilobytes, megabytes, gigabytes and terabytes.\n"); |
| 229 | return -1; |
| 230 | } |
| 231 | } else { |
| 232 | fprintf(stderr, "Option '%s' needs a parameter\n", name); |
| 233 | return -1; |
| 234 | } |
| 235 | return 0; |
| 236 | } |
| 237 | |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 238 | /* |
| 239 | * Sets the value of a parameter in a given option list. The parsing of the |
| 240 | * value depends on the type of option: |
| 241 | * |
| 242 | * OPT_FLAG (uses value.n): |
| 243 | * If no value is given, the flag is set to 1. |
| 244 | * Otherwise the value must be "on" (set to 1) or "off" (set to 0) |
| 245 | * |
| 246 | * OPT_STRING (uses value.s): |
| 247 | * value is strdup()ed and assigned as option value |
| 248 | * |
| 249 | * OPT_SIZE (uses value.n): |
| 250 | * The value is converted to an integer. Suffixes for kilobytes etc. are |
| 251 | * allowed (powers of 1024). |
| 252 | * |
| 253 | * Returns 0 on succes, -1 in error cases |
| 254 | */ |
| 255 | int set_option_parameter(QEMUOptionParameter *list, const char *name, |
| 256 | const char *value) |
| 257 | { |
Gerd Hoffmann | 67b1355 | 2009-07-22 16:43:01 +0200 | [diff] [blame] | 258 | int flag; |
| 259 | |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 260 | // Find a matching parameter |
| 261 | list = get_option_parameter(list, name); |
| 262 | if (list == NULL) { |
| 263 | fprintf(stderr, "Unknown option '%s'\n", name); |
| 264 | return -1; |
| 265 | } |
| 266 | |
| 267 | // Process parameter |
| 268 | switch (list->type) { |
| 269 | case OPT_FLAG: |
Mark McLoughlin | 3df04ac | 2009-09-23 11:24:05 +0100 | [diff] [blame] | 270 | if (parse_option_bool(name, value, &flag) == -1) |
Gerd Hoffmann | 67b1355 | 2009-07-22 16:43:01 +0200 | [diff] [blame] | 271 | return -1; |
| 272 | list->value.n = flag; |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 273 | break; |
| 274 | |
| 275 | case OPT_STRING: |
| 276 | if (value != NULL) { |
Jean-Christophe DUBOIS | d4c3fdd | 2009-09-03 00:37:00 +0200 | [diff] [blame] | 277 | list->value.s = qemu_strdup(value); |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 278 | } else { |
| 279 | fprintf(stderr, "Option '%s' needs a parameter\n", name); |
| 280 | return -1; |
| 281 | } |
| 282 | break; |
| 283 | |
| 284 | case OPT_SIZE: |
Mark McLoughlin | 3df04ac | 2009-09-23 11:24:05 +0100 | [diff] [blame] | 285 | if (parse_option_size(name, value, &list->value.n) == -1) |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 286 | return -1; |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 287 | break; |
Gerd Hoffmann | 7695019 | 2009-07-22 16:43:02 +0200 | [diff] [blame] | 288 | |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 289 | default: |
| 290 | fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name); |
| 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Sets the given parameter to an integer instead of a string. |
| 299 | * This function cannot be used to set string options. |
| 300 | * |
| 301 | * Returns 0 on success, -1 in error cases |
| 302 | */ |
| 303 | int set_option_parameter_int(QEMUOptionParameter *list, const char *name, |
| 304 | uint64_t value) |
| 305 | { |
| 306 | // Find a matching parameter |
| 307 | list = get_option_parameter(list, name); |
| 308 | if (list == NULL) { |
| 309 | fprintf(stderr, "Unknown option '%s'\n", name); |
| 310 | return -1; |
| 311 | } |
| 312 | |
| 313 | // Process parameter |
| 314 | switch (list->type) { |
| 315 | case OPT_FLAG: |
| 316 | case OPT_NUMBER: |
| 317 | case OPT_SIZE: |
| 318 | list->value.n = value; |
| 319 | break; |
| 320 | |
| 321 | default: |
| 322 | return -1; |
| 323 | } |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * Frees a option list. If it contains strings, the strings are freed as well. |
| 330 | */ |
| 331 | void free_option_parameters(QEMUOptionParameter *list) |
| 332 | { |
| 333 | QEMUOptionParameter *cur = list; |
| 334 | |
| 335 | while (cur && cur->name) { |
| 336 | if (cur->type == OPT_STRING) { |
Jean-Christophe DUBOIS | d4c3fdd | 2009-09-03 00:37:00 +0200 | [diff] [blame] | 337 | qemu_free(cur->value.s); |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 338 | } |
| 339 | cur++; |
| 340 | } |
| 341 | |
Jean-Christophe DUBOIS | d4c3fdd | 2009-09-03 00:37:00 +0200 | [diff] [blame] | 342 | qemu_free(list); |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Parses a parameter string (param) into an option list (dest). |
| 347 | * |
| 348 | * list is the templace is. If dest is NULL, a new copy of list is created for |
| 349 | * it. If list is NULL, this function fails. |
| 350 | * |
| 351 | * A parameter string consists of one or more parameters, separated by commas. |
| 352 | * Each parameter consists of its name and possibly of a value. In the latter |
| 353 | * case, the value is delimited by an = character. To specify a value which |
| 354 | * contains commas, double each comma so it won't be recognized as the end of |
| 355 | * the parameter. |
| 356 | * |
| 357 | * For more details of the parsing see above. |
| 358 | * |
| 359 | * Returns a pointer to the first element of dest (or the newly allocated copy) |
| 360 | * or NULL in error cases |
| 361 | */ |
| 362 | QEMUOptionParameter *parse_option_parameters(const char *param, |
| 363 | QEMUOptionParameter *list, QEMUOptionParameter *dest) |
| 364 | { |
| 365 | QEMUOptionParameter *cur; |
| 366 | QEMUOptionParameter *allocated = NULL; |
| 367 | char name[256]; |
| 368 | char value[256]; |
| 369 | char *param_delim, *value_delim; |
| 370 | char next_delim; |
| 371 | size_t num_options; |
| 372 | |
| 373 | if (list == NULL) { |
| 374 | return NULL; |
| 375 | } |
| 376 | |
| 377 | if (dest == NULL) { |
| 378 | // Count valid options |
| 379 | num_options = 0; |
| 380 | cur = list; |
| 381 | while (cur->name) { |
| 382 | num_options++; |
| 383 | cur++; |
| 384 | } |
| 385 | |
| 386 | // Create a copy of the option list to fill in values |
| 387 | dest = qemu_mallocz((num_options + 1) * sizeof(QEMUOptionParameter)); |
| 388 | allocated = dest; |
| 389 | memcpy(dest, list, (num_options + 1) * sizeof(QEMUOptionParameter)); |
| 390 | } |
| 391 | |
| 392 | while (*param) { |
| 393 | |
| 394 | // Find parameter name and value in the string |
| 395 | param_delim = strchr(param, ','); |
| 396 | value_delim = strchr(param, '='); |
| 397 | |
| 398 | if (value_delim && (value_delim < param_delim || !param_delim)) { |
| 399 | next_delim = '='; |
| 400 | } else { |
| 401 | next_delim = ','; |
| 402 | value_delim = NULL; |
| 403 | } |
| 404 | |
| 405 | param = get_opt_name(name, sizeof(name), param, next_delim); |
| 406 | if (value_delim) { |
| 407 | param = get_opt_value(value, sizeof(value), param + 1); |
| 408 | } |
| 409 | if (*param != '\0') { |
| 410 | param++; |
| 411 | } |
| 412 | |
| 413 | // Set the parameter |
| 414 | if (set_option_parameter(dest, name, value_delim ? value : NULL)) { |
| 415 | goto fail; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | return dest; |
| 420 | |
| 421 | fail: |
| 422 | // Only free the list if it was newly allocated |
| 423 | free_option_parameters(allocated); |
| 424 | return NULL; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Prints all options of a list that have a value to stdout |
| 429 | */ |
| 430 | void print_option_parameters(QEMUOptionParameter *list) |
| 431 | { |
| 432 | while (list && list->name) { |
| 433 | switch (list->type) { |
| 434 | case OPT_STRING: |
| 435 | if (list->value.s != NULL) { |
| 436 | printf("%s='%s' ", list->name, list->value.s); |
| 437 | } |
| 438 | break; |
| 439 | case OPT_FLAG: |
| 440 | printf("%s=%s ", list->name, list->value.n ? "on" : "off"); |
| 441 | break; |
| 442 | case OPT_SIZE: |
| 443 | case OPT_NUMBER: |
| 444 | printf("%s=%" PRId64 " ", list->name, list->value.n); |
| 445 | break; |
| 446 | default: |
| 447 | printf("%s=(unkown type) ", list->name); |
| 448 | break; |
| 449 | } |
| 450 | list++; |
| 451 | } |
| 452 | } |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 453 | |
| 454 | /* |
| 455 | * Prints an overview of all available options |
| 456 | */ |
| 457 | void print_option_help(QEMUOptionParameter *list) |
| 458 | { |
| 459 | printf("Supported options:\n"); |
| 460 | while (list && list->name) { |
| 461 | printf("%-16s %s\n", list->name, |
| 462 | list->help ? list->help : "No description available"); |
| 463 | list++; |
| 464 | } |
| 465 | } |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 466 | |
| 467 | /* ------------------------------------------------------------------ */ |
| 468 | |
| 469 | struct QemuOpt { |
| 470 | const char *name; |
| 471 | const char *str; |
| 472 | |
| 473 | QemuOptDesc *desc; |
| 474 | union { |
Juan Quintela | 1f5c177 | 2009-09-21 13:08:34 +0200 | [diff] [blame] | 475 | int boolean; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 476 | uint64_t uint; |
| 477 | } value; |
| 478 | |
| 479 | QemuOpts *opts; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 480 | QTAILQ_ENTRY(QemuOpt) next; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 481 | }; |
| 482 | |
| 483 | struct QemuOpts { |
| 484 | const char *id; |
| 485 | QemuOptsList *list; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 486 | QTAILQ_HEAD(, QemuOpt) head; |
| 487 | QTAILQ_ENTRY(QemuOpts) next; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 488 | }; |
| 489 | |
| 490 | static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name) |
| 491 | { |
| 492 | QemuOpt *opt; |
| 493 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 494 | QTAILQ_FOREACH(opt, &opts->head, next) { |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 495 | if (strcmp(opt->name, name) != 0) |
| 496 | continue; |
| 497 | return opt; |
| 498 | } |
| 499 | return NULL; |
| 500 | } |
| 501 | |
| 502 | const char *qemu_opt_get(QemuOpts *opts, const char *name) |
| 503 | { |
| 504 | QemuOpt *opt = qemu_opt_find(opts, name); |
| 505 | return opt ? opt->str : NULL; |
| 506 | } |
| 507 | |
| 508 | int qemu_opt_get_bool(QemuOpts *opts, const char *name, int defval) |
| 509 | { |
| 510 | QemuOpt *opt = qemu_opt_find(opts, name); |
| 511 | |
| 512 | if (opt == NULL) |
| 513 | return defval; |
| 514 | assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL); |
Juan Quintela | 1f5c177 | 2009-09-21 13:08:34 +0200 | [diff] [blame] | 515 | return opt->value.boolean; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval) |
| 519 | { |
| 520 | QemuOpt *opt = qemu_opt_find(opts, name); |
| 521 | |
| 522 | if (opt == NULL) |
| 523 | return defval; |
| 524 | assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER); |
| 525 | return opt->value.uint; |
| 526 | } |
| 527 | |
| 528 | uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval) |
| 529 | { |
| 530 | QemuOpt *opt = qemu_opt_find(opts, name); |
| 531 | |
| 532 | if (opt == NULL) |
| 533 | return defval; |
| 534 | assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE); |
| 535 | return opt->value.uint; |
| 536 | } |
| 537 | |
| 538 | static int qemu_opt_parse(QemuOpt *opt) |
| 539 | { |
| 540 | if (opt->desc == NULL) |
| 541 | return 0; |
| 542 | switch (opt->desc->type) { |
| 543 | case QEMU_OPT_STRING: |
| 544 | /* nothing */ |
| 545 | return 0; |
| 546 | case QEMU_OPT_BOOL: |
Juan Quintela | 1f5c177 | 2009-09-21 13:08:34 +0200 | [diff] [blame] | 547 | return parse_option_bool(opt->name, opt->str, &opt->value.boolean); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 548 | case QEMU_OPT_NUMBER: |
| 549 | return parse_option_number(opt->name, opt->str, &opt->value.uint); |
| 550 | case QEMU_OPT_SIZE: |
| 551 | return parse_option_size(opt->name, opt->str, &opt->value.uint); |
| 552 | default: |
| 553 | abort(); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | static void qemu_opt_del(QemuOpt *opt) |
| 558 | { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 559 | QTAILQ_REMOVE(&opt->opts->head, opt, next); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 560 | qemu_free((/* !const */ char*)opt->name); |
| 561 | qemu_free((/* !const */ char*)opt->str); |
| 562 | qemu_free(opt); |
| 563 | } |
| 564 | |
| 565 | int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) |
| 566 | { |
| 567 | QemuOpt *opt; |
| 568 | |
| 569 | opt = qemu_opt_find(opts, name); |
| 570 | if (!opt) { |
| 571 | QemuOptDesc *desc = opts->list->desc; |
| 572 | int i; |
| 573 | |
| 574 | for (i = 0; desc[i].name != NULL; i++) { |
| 575 | if (strcmp(desc[i].name, name) == 0) { |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | if (desc[i].name == NULL) { |
| 580 | if (i == 0) { |
| 581 | /* empty list -> allow any */; |
| 582 | } else { |
| 583 | fprintf(stderr, "option \"%s\" is not valid for %s\n", |
| 584 | name, opts->list->name); |
| 585 | return -1; |
| 586 | } |
| 587 | } |
| 588 | opt = qemu_mallocz(sizeof(*opt)); |
| 589 | opt->name = qemu_strdup(name); |
| 590 | opt->opts = opts; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 591 | QTAILQ_INSERT_TAIL(&opts->head, opt, next); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 592 | if (desc[i].name != NULL) { |
| 593 | opt->desc = desc+i; |
| 594 | } |
| 595 | } |
| 596 | qemu_free((/* !const */ char*)opt->str); |
| 597 | opt->str = NULL; |
| 598 | if (value) { |
| 599 | opt->str = qemu_strdup(value); |
| 600 | } |
| 601 | if (qemu_opt_parse(opt) < 0) { |
| 602 | fprintf(stderr, "Failed to parse \"%s\" for \"%s.%s\"\n", opt->str, |
| 603 | opts->list->name, opt->name); |
| 604 | qemu_opt_del(opt); |
| 605 | return -1; |
| 606 | } |
| 607 | return 0; |
| 608 | } |
| 609 | |
Gerd Hoffmann | 4802607 | 2009-07-31 12:25:32 +0200 | [diff] [blame] | 610 | int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque, |
| 611 | int abort_on_failure) |
| 612 | { |
| 613 | QemuOpt *opt; |
| 614 | int rc = 0; |
| 615 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 616 | QTAILQ_FOREACH(opt, &opts->head, next) { |
Gerd Hoffmann | 4802607 | 2009-07-31 12:25:32 +0200 | [diff] [blame] | 617 | rc = func(opt->name, opt->str, opaque); |
| 618 | if (abort_on_failure && rc != 0) |
| 619 | break; |
| 620 | } |
| 621 | return rc; |
| 622 | } |
| 623 | |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 624 | QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id) |
| 625 | { |
| 626 | QemuOpts *opts; |
| 627 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 628 | QTAILQ_FOREACH(opts, &list->head, next) { |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 629 | if (!opts->id) { |
| 630 | continue; |
| 631 | } |
| 632 | if (strcmp(opts->id, id) != 0) { |
| 633 | continue; |
| 634 | } |
| 635 | return opts; |
| 636 | } |
| 637 | return NULL; |
| 638 | } |
| 639 | |
| 640 | QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists) |
| 641 | { |
| 642 | QemuOpts *opts = NULL; |
| 643 | |
| 644 | if (id) { |
| 645 | opts = qemu_opts_find(list, id); |
| 646 | if (opts != NULL) { |
| 647 | if (fail_if_exists) { |
| 648 | fprintf(stderr, "tried to create id \"%s\" twice for \"%s\"\n", |
| 649 | id, list->name); |
| 650 | return NULL; |
| 651 | } else { |
| 652 | return opts; |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | opts = qemu_mallocz(sizeof(*opts)); |
| 657 | if (id) { |
| 658 | opts->id = qemu_strdup(id); |
| 659 | } |
| 660 | opts->list = list; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 661 | QTAILQ_INIT(&opts->head); |
| 662 | QTAILQ_INSERT_TAIL(&list->head, opts, next); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 663 | return opts; |
| 664 | } |
| 665 | |
| 666 | int qemu_opts_set(QemuOptsList *list, const char *id, |
| 667 | const char *name, const char *value) |
| 668 | { |
| 669 | QemuOpts *opts; |
| 670 | |
| 671 | opts = qemu_opts_create(list, id, 1); |
| 672 | if (opts == NULL) { |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 673 | return -1; |
| 674 | } |
| 675 | return qemu_opt_set(opts, name, value); |
| 676 | } |
| 677 | |
Gerd Hoffmann | 4802607 | 2009-07-31 12:25:32 +0200 | [diff] [blame] | 678 | const char *qemu_opts_id(QemuOpts *opts) |
| 679 | { |
| 680 | return opts->id; |
| 681 | } |
| 682 | |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 683 | void qemu_opts_del(QemuOpts *opts) |
| 684 | { |
| 685 | QemuOpt *opt; |
| 686 | |
| 687 | for (;;) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 688 | opt = QTAILQ_FIRST(&opts->head); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 689 | if (opt == NULL) |
| 690 | break; |
| 691 | qemu_opt_del(opt); |
| 692 | } |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 693 | QTAILQ_REMOVE(&opts->list->head, opts, next); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 694 | qemu_free(opts); |
| 695 | } |
| 696 | |
| 697 | int qemu_opts_print(QemuOpts *opts, void *dummy) |
| 698 | { |
| 699 | QemuOpt *opt; |
| 700 | |
| 701 | fprintf(stderr, "%s: %s:", opts->list->name, |
| 702 | opts->id ? opts->id : "<noid>"); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 703 | QTAILQ_FOREACH(opt, &opts->head, next) { |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 704 | fprintf(stderr, " %s=\"%s\"", opt->name, opt->str); |
| 705 | } |
| 706 | fprintf(stderr, "\n"); |
| 707 | return 0; |
| 708 | } |
| 709 | |
Gerd Hoffmann | 96729cb | 2009-09-10 10:58:33 +0200 | [diff] [blame] | 710 | int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname) |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 711 | { |
Gerd Hoffmann | 96729cb | 2009-09-10 10:58:33 +0200 | [diff] [blame] | 712 | char option[128], value[128]; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 713 | const char *p,*pe,*pc; |
| 714 | |
Mark McLoughlin | 2cfa571 | 2009-10-06 12:17:02 +0100 | [diff] [blame^] | 715 | for (p = params; *p != '\0'; p++) { |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 716 | pe = strchr(p, '='); |
| 717 | pc = strchr(p, ','); |
| 718 | if (!pe || (pc && pc < pe)) { |
| 719 | /* found "foo,more" */ |
| 720 | if (p == params && firstname) { |
| 721 | /* implicitly named first option */ |
| 722 | pstrcpy(option, sizeof(option), firstname); |
| 723 | p = get_opt_value(value, sizeof(value), p); |
| 724 | } else { |
| 725 | /* option without value, probably a flag */ |
| 726 | p = get_opt_name(option, sizeof(option), p, ','); |
Gerd Hoffmann | 96729cb | 2009-09-10 10:58:33 +0200 | [diff] [blame] | 727 | if (strncmp(option, "no", 2) == 0) { |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 728 | memmove(option, option+2, strlen(option+2)+1); |
| 729 | pstrcpy(value, sizeof(value), "off"); |
| 730 | } else { |
| 731 | pstrcpy(value, sizeof(value), "on"); |
| 732 | } |
| 733 | } |
| 734 | } else { |
| 735 | /* found "foo=bar,more" */ |
| 736 | p = get_opt_name(option, sizeof(option), p, '='); |
| 737 | if (*p != '=') { |
| 738 | break; |
| 739 | } |
| 740 | p++; |
| 741 | p = get_opt_value(value, sizeof(value), p); |
| 742 | } |
| 743 | if (strcmp(option, "id") != 0) { |
| 744 | /* store and parse */ |
Mark McLoughlin | 3df04ac | 2009-09-23 11:24:05 +0100 | [diff] [blame] | 745 | if (qemu_opt_set(opts, option, value) == -1) { |
Gerd Hoffmann | 96729cb | 2009-09-10 10:58:33 +0200 | [diff] [blame] | 746 | return -1; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | if (*p != ',') { |
| 750 | break; |
| 751 | } |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 752 | } |
Gerd Hoffmann | 96729cb | 2009-09-10 10:58:33 +0200 | [diff] [blame] | 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *firstname) |
| 757 | { |
| 758 | char value[128], *id = NULL; |
| 759 | const char *p; |
| 760 | QemuOpts *opts; |
| 761 | |
| 762 | if (strncmp(params, "id=", 3) == 0) { |
| 763 | get_opt_value(value, sizeof(value), params+3); |
| 764 | id = qemu_strdup(value); |
| 765 | } else if ((p = strstr(params, ",id=")) != NULL) { |
| 766 | get_opt_value(value, sizeof(value), p+4); |
| 767 | id = qemu_strdup(value); |
| 768 | } |
| 769 | opts = qemu_opts_create(list, id, 1); |
| 770 | if (opts == NULL) |
| 771 | return NULL; |
| 772 | |
| 773 | if (qemu_opts_do_parse(opts, params, firstname) != 0) { |
| 774 | qemu_opts_del(opts); |
| 775 | return NULL; |
| 776 | } |
| 777 | |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 778 | return opts; |
| 779 | } |
| 780 | |
| 781 | int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque, |
| 782 | int abort_on_failure) |
| 783 | { |
| 784 | QemuOpts *opts; |
| 785 | int rc = 0; |
| 786 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 787 | QTAILQ_FOREACH(opts, &list->head, next) { |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 788 | rc = func(opts, opaque); |
| 789 | if (abort_on_failure && rc != 0) |
| 790 | break; |
| 791 | } |
| 792 | return rc; |
| 793 | } |