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" |
Markus Armbruster | 827b081 | 2010-02-18 19:46:49 +0100 | [diff] [blame] | 30 | #include "qemu-error.h" |
Markus Armbruster | 01e7f18 | 2010-02-10 20:15:29 +0100 | [diff] [blame] | 31 | #include "qemu-objects.h" |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 32 | #include "qemu-option.h" |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 33 | #include "error.h" |
Markus Armbruster | 975b63a | 2010-03-25 17:22:32 +0100 | [diff] [blame] | 34 | #include "qerror.h" |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * Extracts the name of an option from the parameter string (p points at the |
| 38 | * first byte of the option name) |
| 39 | * |
| 40 | * The option name is delimited by delim (usually , or =) or the string end |
| 41 | * and is copied into buf. If the option name is longer than buf_size, it is |
| 42 | * truncated. buf is always zero terminated. |
| 43 | * |
| 44 | * The return value is the position of the delimiter/zero byte after the option |
| 45 | * name in p. |
| 46 | */ |
| 47 | const char *get_opt_name(char *buf, int buf_size, const char *p, char delim) |
| 48 | { |
| 49 | char *q; |
| 50 | |
| 51 | q = buf; |
| 52 | while (*p != '\0' && *p != delim) { |
| 53 | if (q && (q - buf) < buf_size - 1) |
| 54 | *q++ = *p; |
| 55 | p++; |
| 56 | } |
| 57 | if (q) |
| 58 | *q = '\0'; |
| 59 | |
| 60 | return p; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Extracts the value of an option from the parameter string p (p points at the |
| 65 | * first byte of the option value) |
| 66 | * |
| 67 | * This function is comparable to get_opt_name with the difference that the |
| 68 | * delimiter is fixed to be comma which starts a new option. To specify an |
| 69 | * option value that contains commas, double each comma. |
| 70 | */ |
| 71 | const char *get_opt_value(char *buf, int buf_size, const char *p) |
| 72 | { |
| 73 | char *q; |
| 74 | |
| 75 | q = buf; |
| 76 | while (*p != '\0') { |
| 77 | if (*p == ',') { |
| 78 | if (*(p + 1) != ',') |
| 79 | break; |
| 80 | p++; |
| 81 | } |
| 82 | if (q && (q - buf) < buf_size - 1) |
| 83 | *q++ = *p; |
| 84 | p++; |
| 85 | } |
| 86 | if (q) |
| 87 | *q = '\0'; |
| 88 | |
| 89 | return p; |
| 90 | } |
| 91 | |
Gerd Hoffmann | 62c5802 | 2009-07-22 16:43:00 +0200 | [diff] [blame] | 92 | int get_next_param_value(char *buf, int buf_size, |
| 93 | const char *tag, const char **pstr) |
| 94 | { |
| 95 | const char *p; |
| 96 | char option[128]; |
| 97 | |
| 98 | p = *pstr; |
| 99 | for(;;) { |
| 100 | p = get_opt_name(option, sizeof(option), p, '='); |
| 101 | if (*p != '=') |
| 102 | break; |
| 103 | p++; |
| 104 | if (!strcmp(tag, option)) { |
| 105 | *pstr = get_opt_value(buf, buf_size, p); |
| 106 | if (**pstr == ',') { |
| 107 | (*pstr)++; |
| 108 | } |
| 109 | return strlen(buf); |
| 110 | } else { |
| 111 | p = get_opt_value(NULL, 0, p); |
| 112 | } |
| 113 | if (*p != ',') |
| 114 | break; |
| 115 | p++; |
| 116 | } |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | int get_param_value(char *buf, int buf_size, |
| 121 | const char *tag, const char *str) |
| 122 | { |
| 123 | return get_next_param_value(buf, buf_size, tag, &str); |
| 124 | } |
| 125 | |
| 126 | int check_params(char *buf, int buf_size, |
| 127 | const char * const *params, const char *str) |
| 128 | { |
| 129 | const char *p; |
| 130 | int i; |
| 131 | |
| 132 | p = str; |
| 133 | while (*p != '\0') { |
| 134 | p = get_opt_name(buf, buf_size, p, '='); |
| 135 | if (*p != '=') { |
| 136 | return -1; |
| 137 | } |
| 138 | p++; |
| 139 | for (i = 0; params[i] != NULL; i++) { |
| 140 | if (!strcmp(params[i], buf)) { |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | if (params[i] == NULL) { |
| 145 | return -1; |
| 146 | } |
| 147 | p = get_opt_value(NULL, 0, p); |
| 148 | if (*p != ',') { |
| 149 | break; |
| 150 | } |
| 151 | p++; |
| 152 | } |
| 153 | return 0; |
| 154 | } |
| 155 | |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 156 | /* |
| 157 | * Searches an option list for an option with the given name |
| 158 | */ |
| 159 | QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list, |
| 160 | const char *name) |
| 161 | { |
| 162 | while (list && list->name) { |
| 163 | if (!strcmp(list->name, name)) { |
| 164 | return list; |
| 165 | } |
| 166 | list++; |
| 167 | } |
| 168 | |
| 169 | return NULL; |
| 170 | } |
| 171 | |
M. Mohan Kumar | f02b77c | 2011-10-25 12:10:39 +0530 | [diff] [blame] | 172 | static int parse_option_bool(const char *name, const char *value, bool *ret) |
Gerd Hoffmann | 67b1355 | 2009-07-22 16:43:01 +0200 | [diff] [blame] | 173 | { |
| 174 | if (value != NULL) { |
| 175 | if (!strcmp(value, "on")) { |
| 176 | *ret = 1; |
| 177 | } else if (!strcmp(value, "off")) { |
| 178 | *ret = 0; |
| 179 | } else { |
Markus Armbruster | c64f27d | 2010-03-25 17:22:34 +0100 | [diff] [blame] | 180 | qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "'on' or 'off'"); |
Gerd Hoffmann | 67b1355 | 2009-07-22 16:43:01 +0200 | [diff] [blame] | 181 | return -1; |
| 182 | } |
| 183 | } else { |
| 184 | *ret = 1; |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
Luiz Capitulino | 2f39df5 | 2012-03-21 14:37:44 -0300 | [diff] [blame^] | 189 | static void parse_option_number(const char *name, const char *value, |
| 190 | uint64_t *ret, Error **errp) |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 191 | { |
| 192 | char *postfix; |
| 193 | uint64_t number; |
| 194 | |
| 195 | if (value != NULL) { |
| 196 | number = strtoull(value, &postfix, 0); |
| 197 | if (*postfix != '\0') { |
Luiz Capitulino | 2f39df5 | 2012-03-21 14:37:44 -0300 | [diff] [blame^] | 198 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number"); |
| 199 | return; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 200 | } |
Gerd Hoffmann | 21c9f4c | 2009-07-29 10:39:59 +0200 | [diff] [blame] | 201 | *ret = number; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 202 | } else { |
Luiz Capitulino | 2f39df5 | 2012-03-21 14:37:44 -0300 | [diff] [blame^] | 203 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number"); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 204 | } |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Gerd Hoffmann | 7695019 | 2009-07-22 16:43:02 +0200 | [diff] [blame] | 207 | static int parse_option_size(const char *name, const char *value, uint64_t *ret) |
| 208 | { |
| 209 | char *postfix; |
| 210 | double sizef; |
| 211 | |
| 212 | if (value != NULL) { |
| 213 | sizef = strtod(value, &postfix); |
| 214 | switch (*postfix) { |
| 215 | case 'T': |
| 216 | sizef *= 1024; |
Stefan Weil | 0b0404b | 2012-01-09 18:29:51 +0100 | [diff] [blame] | 217 | /* fall through */ |
Gerd Hoffmann | 7695019 | 2009-07-22 16:43:02 +0200 | [diff] [blame] | 218 | case 'G': |
| 219 | sizef *= 1024; |
Stefan Weil | 0b0404b | 2012-01-09 18:29:51 +0100 | [diff] [blame] | 220 | /* fall through */ |
Gerd Hoffmann | 7695019 | 2009-07-22 16:43:02 +0200 | [diff] [blame] | 221 | case 'M': |
| 222 | sizef *= 1024; |
Stefan Weil | 0b0404b | 2012-01-09 18:29:51 +0100 | [diff] [blame] | 223 | /* fall through */ |
Gerd Hoffmann | 7695019 | 2009-07-22 16:43:02 +0200 | [diff] [blame] | 224 | case 'K': |
| 225 | case 'k': |
| 226 | sizef *= 1024; |
Stefan Weil | 0b0404b | 2012-01-09 18:29:51 +0100 | [diff] [blame] | 227 | /* fall through */ |
Gerd Hoffmann | 7695019 | 2009-07-22 16:43:02 +0200 | [diff] [blame] | 228 | case 'b': |
| 229 | case '\0': |
| 230 | *ret = (uint64_t) sizef; |
| 231 | break; |
| 232 | default: |
Markus Armbruster | c64f27d | 2010-03-25 17:22:34 +0100 | [diff] [blame] | 233 | qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size"); |
| 234 | error_printf_unless_qmp("You may use k, M, G or T suffixes for " |
Gerd Hoffmann | 7695019 | 2009-07-22 16:43:02 +0200 | [diff] [blame] | 235 | "kilobytes, megabytes, gigabytes and terabytes.\n"); |
| 236 | return -1; |
| 237 | } |
| 238 | } else { |
Markus Armbruster | c64f27d | 2010-03-25 17:22:34 +0100 | [diff] [blame] | 239 | qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size"); |
Gerd Hoffmann | 7695019 | 2009-07-22 16:43:02 +0200 | [diff] [blame] | 240 | return -1; |
| 241 | } |
| 242 | return 0; |
| 243 | } |
| 244 | |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 245 | /* |
| 246 | * Sets the value of a parameter in a given option list. The parsing of the |
| 247 | * value depends on the type of option: |
| 248 | * |
| 249 | * OPT_FLAG (uses value.n): |
| 250 | * If no value is given, the flag is set to 1. |
| 251 | * Otherwise the value must be "on" (set to 1) or "off" (set to 0) |
| 252 | * |
| 253 | * OPT_STRING (uses value.s): |
| 254 | * value is strdup()ed and assigned as option value |
| 255 | * |
| 256 | * OPT_SIZE (uses value.n): |
| 257 | * The value is converted to an integer. Suffixes for kilobytes etc. are |
| 258 | * allowed (powers of 1024). |
| 259 | * |
| 260 | * Returns 0 on succes, -1 in error cases |
| 261 | */ |
| 262 | int set_option_parameter(QEMUOptionParameter *list, const char *name, |
| 263 | const char *value) |
| 264 | { |
M. Mohan Kumar | f02b77c | 2011-10-25 12:10:39 +0530 | [diff] [blame] | 265 | bool flag; |
Gerd Hoffmann | 67b1355 | 2009-07-22 16:43:01 +0200 | [diff] [blame] | 266 | |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 267 | // Find a matching parameter |
| 268 | list = get_option_parameter(list, name); |
| 269 | if (list == NULL) { |
| 270 | fprintf(stderr, "Unknown option '%s'\n", name); |
| 271 | return -1; |
| 272 | } |
| 273 | |
| 274 | // Process parameter |
| 275 | switch (list->type) { |
| 276 | case OPT_FLAG: |
Mark McLoughlin | 3df04ac | 2009-09-23 11:24:05 +0100 | [diff] [blame] | 277 | if (parse_option_bool(name, value, &flag) == -1) |
Gerd Hoffmann | 67b1355 | 2009-07-22 16:43:01 +0200 | [diff] [blame] | 278 | return -1; |
| 279 | list->value.n = flag; |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 280 | break; |
| 281 | |
| 282 | case OPT_STRING: |
| 283 | if (value != NULL) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 284 | list->value.s = g_strdup(value); |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 285 | } else { |
| 286 | fprintf(stderr, "Option '%s' needs a parameter\n", name); |
| 287 | return -1; |
| 288 | } |
| 289 | break; |
| 290 | |
| 291 | case OPT_SIZE: |
Mark McLoughlin | 3df04ac | 2009-09-23 11:24:05 +0100 | [diff] [blame] | 292 | if (parse_option_size(name, value, &list->value.n) == -1) |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 293 | return -1; |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 294 | break; |
Gerd Hoffmann | 7695019 | 2009-07-22 16:43:02 +0200 | [diff] [blame] | 295 | |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 296 | default: |
| 297 | fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name); |
| 298 | return -1; |
| 299 | } |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Sets the given parameter to an integer instead of a string. |
| 306 | * This function cannot be used to set string options. |
| 307 | * |
| 308 | * Returns 0 on success, -1 in error cases |
| 309 | */ |
| 310 | int set_option_parameter_int(QEMUOptionParameter *list, const char *name, |
| 311 | uint64_t value) |
| 312 | { |
| 313 | // Find a matching parameter |
| 314 | list = get_option_parameter(list, name); |
| 315 | if (list == NULL) { |
| 316 | fprintf(stderr, "Unknown option '%s'\n", name); |
| 317 | return -1; |
| 318 | } |
| 319 | |
| 320 | // Process parameter |
| 321 | switch (list->type) { |
| 322 | case OPT_FLAG: |
| 323 | case OPT_NUMBER: |
| 324 | case OPT_SIZE: |
| 325 | list->value.n = value; |
| 326 | break; |
| 327 | |
| 328 | default: |
| 329 | return -1; |
| 330 | } |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Frees a option list. If it contains strings, the strings are freed as well. |
| 337 | */ |
| 338 | void free_option_parameters(QEMUOptionParameter *list) |
| 339 | { |
| 340 | QEMUOptionParameter *cur = list; |
| 341 | |
| 342 | while (cur && cur->name) { |
| 343 | if (cur->type == OPT_STRING) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 344 | g_free(cur->value.s); |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 345 | } |
| 346 | cur++; |
| 347 | } |
| 348 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 349 | g_free(list); |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /* |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 353 | * Count valid options in list |
| 354 | */ |
| 355 | static size_t count_option_parameters(QEMUOptionParameter *list) |
| 356 | { |
| 357 | size_t num_options = 0; |
| 358 | |
| 359 | while (list && list->name) { |
| 360 | num_options++; |
| 361 | list++; |
| 362 | } |
| 363 | |
| 364 | return num_options; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * Append an option list (list) to an option list (dest). |
| 369 | * |
| 370 | * If dest is NULL, a new copy of list is created. |
| 371 | * |
| 372 | * Returns a pointer to the first element of dest (or the newly allocated copy) |
| 373 | */ |
| 374 | QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest, |
| 375 | QEMUOptionParameter *list) |
| 376 | { |
| 377 | size_t num_options, num_dest_options; |
| 378 | |
| 379 | num_options = count_option_parameters(dest); |
| 380 | num_dest_options = num_options; |
| 381 | |
| 382 | num_options += count_option_parameters(list); |
| 383 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 384 | dest = g_realloc(dest, (num_options + 1) * sizeof(QEMUOptionParameter)); |
Kevin Wolf | bd69fe8 | 2010-06-11 10:19:41 +0200 | [diff] [blame] | 385 | dest[num_dest_options].name = NULL; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 386 | |
| 387 | while (list && list->name) { |
| 388 | if (get_option_parameter(dest, list->name) == NULL) { |
| 389 | dest[num_dest_options++] = *list; |
| 390 | dest[num_dest_options].name = NULL; |
| 391 | } |
| 392 | list++; |
| 393 | } |
| 394 | |
| 395 | return dest; |
| 396 | } |
| 397 | |
| 398 | /* |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 399 | * Parses a parameter string (param) into an option list (dest). |
| 400 | * |
Stefan Hajnoczi | 0e72e75 | 2010-12-07 09:35:54 +0000 | [diff] [blame] | 401 | * list is the template option list. If dest is NULL, a new copy of list is |
| 402 | * created. If list is NULL, this function fails. |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 403 | * |
| 404 | * A parameter string consists of one or more parameters, separated by commas. |
| 405 | * Each parameter consists of its name and possibly of a value. In the latter |
| 406 | * case, the value is delimited by an = character. To specify a value which |
| 407 | * contains commas, double each comma so it won't be recognized as the end of |
| 408 | * the parameter. |
| 409 | * |
| 410 | * For more details of the parsing see above. |
| 411 | * |
| 412 | * Returns a pointer to the first element of dest (or the newly allocated copy) |
| 413 | * or NULL in error cases |
| 414 | */ |
| 415 | QEMUOptionParameter *parse_option_parameters(const char *param, |
| 416 | QEMUOptionParameter *list, QEMUOptionParameter *dest) |
| 417 | { |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 418 | QEMUOptionParameter *allocated = NULL; |
| 419 | char name[256]; |
| 420 | char value[256]; |
| 421 | char *param_delim, *value_delim; |
| 422 | char next_delim; |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 423 | |
| 424 | if (list == NULL) { |
| 425 | return NULL; |
| 426 | } |
| 427 | |
| 428 | if (dest == NULL) { |
Stefan Hajnoczi | 898c257 | 2010-12-07 09:35:53 +0000 | [diff] [blame] | 429 | dest = allocated = append_option_parameters(NULL, list); |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | while (*param) { |
| 433 | |
| 434 | // Find parameter name and value in the string |
| 435 | param_delim = strchr(param, ','); |
| 436 | value_delim = strchr(param, '='); |
| 437 | |
| 438 | if (value_delim && (value_delim < param_delim || !param_delim)) { |
| 439 | next_delim = '='; |
| 440 | } else { |
| 441 | next_delim = ','; |
| 442 | value_delim = NULL; |
| 443 | } |
| 444 | |
| 445 | param = get_opt_name(name, sizeof(name), param, next_delim); |
| 446 | if (value_delim) { |
| 447 | param = get_opt_value(value, sizeof(value), param + 1); |
| 448 | } |
| 449 | if (*param != '\0') { |
| 450 | param++; |
| 451 | } |
| 452 | |
| 453 | // Set the parameter |
| 454 | if (set_option_parameter(dest, name, value_delim ? value : NULL)) { |
| 455 | goto fail; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | return dest; |
| 460 | |
| 461 | fail: |
| 462 | // Only free the list if it was newly allocated |
| 463 | free_option_parameters(allocated); |
| 464 | return NULL; |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * Prints all options of a list that have a value to stdout |
| 469 | */ |
| 470 | void print_option_parameters(QEMUOptionParameter *list) |
| 471 | { |
| 472 | while (list && list->name) { |
| 473 | switch (list->type) { |
| 474 | case OPT_STRING: |
| 475 | if (list->value.s != NULL) { |
| 476 | printf("%s='%s' ", list->name, list->value.s); |
| 477 | } |
| 478 | break; |
| 479 | case OPT_FLAG: |
| 480 | printf("%s=%s ", list->name, list->value.n ? "on" : "off"); |
| 481 | break; |
| 482 | case OPT_SIZE: |
| 483 | case OPT_NUMBER: |
| 484 | printf("%s=%" PRId64 " ", list->name, list->value.n); |
| 485 | break; |
| 486 | default: |
Dong Xu Wang | 07f3507 | 2011-11-22 18:06:26 +0800 | [diff] [blame] | 487 | printf("%s=(unknown type) ", list->name); |
Kevin Wolf | d3f2436 | 2009-05-18 16:42:09 +0200 | [diff] [blame] | 488 | break; |
| 489 | } |
| 490 | list++; |
| 491 | } |
| 492 | } |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 493 | |
| 494 | /* |
| 495 | * Prints an overview of all available options |
| 496 | */ |
| 497 | void print_option_help(QEMUOptionParameter *list) |
| 498 | { |
| 499 | printf("Supported options:\n"); |
| 500 | while (list && list->name) { |
| 501 | printf("%-16s %s\n", list->name, |
| 502 | list->help ? list->help : "No description available"); |
| 503 | list++; |
| 504 | } |
| 505 | } |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 506 | |
| 507 | /* ------------------------------------------------------------------ */ |
| 508 | |
| 509 | struct QemuOpt { |
| 510 | const char *name; |
| 511 | const char *str; |
| 512 | |
Blue Swirl | 238431a | 2010-02-21 16:01:30 +0000 | [diff] [blame] | 513 | const QemuOptDesc *desc; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 514 | union { |
M. Mohan Kumar | f02b77c | 2011-10-25 12:10:39 +0530 | [diff] [blame] | 515 | bool boolean; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 516 | uint64_t uint; |
| 517 | } value; |
| 518 | |
| 519 | QemuOpts *opts; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 520 | QTAILQ_ENTRY(QemuOpt) next; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 521 | }; |
| 522 | |
| 523 | struct QemuOpts { |
Jan Kiszka | b09417b | 2009-12-01 15:24:18 +0100 | [diff] [blame] | 524 | char *id; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 525 | QemuOptsList *list; |
Markus Armbruster | 827b081 | 2010-02-18 19:46:49 +0100 | [diff] [blame] | 526 | Location loc; |
Mark McLoughlin | dc9ca4b | 2009-10-06 12:17:04 +0100 | [diff] [blame] | 527 | QTAILQ_HEAD(QemuOptHead, QemuOpt) head; |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 528 | QTAILQ_ENTRY(QemuOpts) next; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 529 | }; |
| 530 | |
| 531 | static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name) |
| 532 | { |
| 533 | QemuOpt *opt; |
| 534 | |
Mark McLoughlin | dc9ca4b | 2009-10-06 12:17:04 +0100 | [diff] [blame] | 535 | QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) { |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 536 | if (strcmp(opt->name, name) != 0) |
| 537 | continue; |
| 538 | return opt; |
| 539 | } |
| 540 | return NULL; |
| 541 | } |
| 542 | |
| 543 | const char *qemu_opt_get(QemuOpts *opts, const char *name) |
| 544 | { |
| 545 | QemuOpt *opt = qemu_opt_find(opts, name); |
| 546 | return opt ? opt->str : NULL; |
| 547 | } |
| 548 | |
M. Mohan Kumar | f02b77c | 2011-10-25 12:10:39 +0530 | [diff] [blame] | 549 | bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval) |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 550 | { |
| 551 | QemuOpt *opt = qemu_opt_find(opts, name); |
| 552 | |
| 553 | if (opt == NULL) |
| 554 | return defval; |
| 555 | assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL); |
Juan Quintela | 1f5c177 | 2009-09-21 13:08:34 +0200 | [diff] [blame] | 556 | return opt->value.boolean; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval) |
| 560 | { |
| 561 | QemuOpt *opt = qemu_opt_find(opts, name); |
| 562 | |
| 563 | if (opt == NULL) |
| 564 | return defval; |
| 565 | assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER); |
| 566 | return opt->value.uint; |
| 567 | } |
| 568 | |
| 569 | uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval) |
| 570 | { |
| 571 | QemuOpt *opt = qemu_opt_find(opts, name); |
| 572 | |
| 573 | if (opt == NULL) |
| 574 | return defval; |
| 575 | assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE); |
| 576 | return opt->value.uint; |
| 577 | } |
| 578 | |
| 579 | static int qemu_opt_parse(QemuOpt *opt) |
| 580 | { |
Luiz Capitulino | 2f39df5 | 2012-03-21 14:37:44 -0300 | [diff] [blame^] | 581 | Error *local_err = NULL; |
| 582 | |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 583 | if (opt->desc == NULL) |
| 584 | return 0; |
Luiz Capitulino | 2f39df5 | 2012-03-21 14:37:44 -0300 | [diff] [blame^] | 585 | |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 586 | switch (opt->desc->type) { |
| 587 | case QEMU_OPT_STRING: |
| 588 | /* nothing */ |
| 589 | return 0; |
| 590 | case QEMU_OPT_BOOL: |
Juan Quintela | 1f5c177 | 2009-09-21 13:08:34 +0200 | [diff] [blame] | 591 | return parse_option_bool(opt->name, opt->str, &opt->value.boolean); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 592 | case QEMU_OPT_NUMBER: |
Luiz Capitulino | 2f39df5 | 2012-03-21 14:37:44 -0300 | [diff] [blame^] | 593 | parse_option_number(opt->name, opt->str, &opt->value.uint, |
| 594 | &local_err); |
| 595 | break; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 596 | case QEMU_OPT_SIZE: |
| 597 | return parse_option_size(opt->name, opt->str, &opt->value.uint); |
| 598 | default: |
| 599 | abort(); |
| 600 | } |
Luiz Capitulino | 2f39df5 | 2012-03-21 14:37:44 -0300 | [diff] [blame^] | 601 | |
| 602 | if (error_is_set(&local_err)) { |
| 603 | qerror_report_err(local_err); |
| 604 | error_free(local_err); |
| 605 | return -1; |
| 606 | } |
| 607 | |
| 608 | return 0; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | static void qemu_opt_del(QemuOpt *opt) |
| 612 | { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 613 | QTAILQ_REMOVE(&opt->opts->head, opt, next); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 614 | g_free((/* !const */ char*)opt->name); |
| 615 | g_free((/* !const */ char*)opt->str); |
| 616 | g_free(opt); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 617 | } |
| 618 | |
Jan Kiszka | 4f6dd9a | 2012-01-27 19:54:54 +0100 | [diff] [blame] | 619 | static int opt_set(QemuOpts *opts, const char *name, const char *value, |
| 620 | bool prepend) |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 621 | { |
| 622 | QemuOpt *opt; |
Blue Swirl | 238431a | 2010-02-21 16:01:30 +0000 | [diff] [blame] | 623 | const QemuOptDesc *desc = opts->list->desc; |
Mark McLoughlin | dc9ca4b | 2009-10-06 12:17:04 +0100 | [diff] [blame] | 624 | int i; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 625 | |
Mark McLoughlin | dc9ca4b | 2009-10-06 12:17:04 +0100 | [diff] [blame] | 626 | for (i = 0; desc[i].name != NULL; i++) { |
| 627 | if (strcmp(desc[i].name, name) == 0) { |
| 628 | break; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 629 | } |
| 630 | } |
Mark McLoughlin | dc9ca4b | 2009-10-06 12:17:04 +0100 | [diff] [blame] | 631 | if (desc[i].name == NULL) { |
| 632 | if (i == 0) { |
| 633 | /* empty list -> allow any */; |
| 634 | } else { |
Markus Armbruster | c64f27d | 2010-03-25 17:22:34 +0100 | [diff] [blame] | 635 | qerror_report(QERR_INVALID_PARAMETER, name); |
Mark McLoughlin | dc9ca4b | 2009-10-06 12:17:04 +0100 | [diff] [blame] | 636 | return -1; |
| 637 | } |
| 638 | } |
| 639 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 640 | opt = g_malloc0(sizeof(*opt)); |
| 641 | opt->name = g_strdup(name); |
Mark McLoughlin | dc9ca4b | 2009-10-06 12:17:04 +0100 | [diff] [blame] | 642 | opt->opts = opts; |
Jan Kiszka | 4f6dd9a | 2012-01-27 19:54:54 +0100 | [diff] [blame] | 643 | if (prepend) { |
| 644 | QTAILQ_INSERT_HEAD(&opts->head, opt, next); |
| 645 | } else { |
| 646 | QTAILQ_INSERT_TAIL(&opts->head, opt, next); |
| 647 | } |
Mark McLoughlin | dc9ca4b | 2009-10-06 12:17:04 +0100 | [diff] [blame] | 648 | if (desc[i].name != NULL) { |
| 649 | opt->desc = desc+i; |
| 650 | } |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 651 | if (value) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 652 | opt->str = g_strdup(value); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 653 | } |
| 654 | if (qemu_opt_parse(opt) < 0) { |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 655 | qemu_opt_del(opt); |
| 656 | return -1; |
| 657 | } |
| 658 | return 0; |
| 659 | } |
| 660 | |
Jan Kiszka | 4f6dd9a | 2012-01-27 19:54:54 +0100 | [diff] [blame] | 661 | int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) |
| 662 | { |
| 663 | return opt_set(opts, name, value, false); |
| 664 | } |
| 665 | |
M. Mohan Kumar | f02b77c | 2011-10-25 12:10:39 +0530 | [diff] [blame] | 666 | int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val) |
| 667 | { |
| 668 | QemuOpt *opt; |
| 669 | const QemuOptDesc *desc = opts->list->desc; |
| 670 | int i; |
| 671 | |
| 672 | for (i = 0; desc[i].name != NULL; i++) { |
| 673 | if (strcmp(desc[i].name, name) == 0) { |
| 674 | break; |
| 675 | } |
| 676 | } |
| 677 | if (desc[i].name == NULL) { |
| 678 | if (i == 0) { |
| 679 | /* empty list -> allow any */; |
| 680 | } else { |
| 681 | qerror_report(QERR_INVALID_PARAMETER, name); |
| 682 | return -1; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | opt = g_malloc0(sizeof(*opt)); |
| 687 | opt->name = g_strdup(name); |
| 688 | opt->opts = opts; |
| 689 | QTAILQ_INSERT_TAIL(&opts->head, opt, next); |
| 690 | if (desc[i].name != NULL) { |
| 691 | opt->desc = desc+i; |
| 692 | } |
| 693 | opt->value.boolean = !!val; |
| 694 | return 0; |
| 695 | } |
| 696 | |
Gerd Hoffmann | 4802607 | 2009-07-31 12:25:32 +0200 | [diff] [blame] | 697 | int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque, |
| 698 | int abort_on_failure) |
| 699 | { |
| 700 | QemuOpt *opt; |
| 701 | int rc = 0; |
| 702 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 703 | QTAILQ_FOREACH(opt, &opts->head, next) { |
Gerd Hoffmann | 4802607 | 2009-07-31 12:25:32 +0200 | [diff] [blame] | 704 | rc = func(opt->name, opt->str, opaque); |
| 705 | if (abort_on_failure && rc != 0) |
| 706 | break; |
| 707 | } |
| 708 | return rc; |
| 709 | } |
| 710 | |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 711 | QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id) |
| 712 | { |
| 713 | QemuOpts *opts; |
| 714 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 715 | QTAILQ_FOREACH(opts, &list->head, next) { |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 716 | if (!opts->id) { |
Jan Kiszka | 4f6dd9a | 2012-01-27 19:54:54 +0100 | [diff] [blame] | 717 | if (!id) { |
| 718 | return opts; |
| 719 | } |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 720 | continue; |
| 721 | } |
| 722 | if (strcmp(opts->id, id) != 0) { |
| 723 | continue; |
| 724 | } |
| 725 | return opts; |
| 726 | } |
| 727 | return NULL; |
| 728 | } |
| 729 | |
Markus Armbruster | b560a9a | 2010-06-08 13:54:26 +0200 | [diff] [blame] | 730 | static int id_wellformed(const char *id) |
| 731 | { |
| 732 | int i; |
| 733 | |
| 734 | if (!qemu_isalpha(id[0])) { |
| 735 | return 0; |
| 736 | } |
| 737 | for (i = 1; id[i]; i++) { |
| 738 | if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) { |
| 739 | return 0; |
| 740 | } |
| 741 | } |
| 742 | return 1; |
| 743 | } |
| 744 | |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 745 | QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, |
| 746 | int fail_if_exists, Error **errp) |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 747 | { |
| 748 | QemuOpts *opts = NULL; |
| 749 | |
| 750 | if (id) { |
Markus Armbruster | b560a9a | 2010-06-08 13:54:26 +0200 | [diff] [blame] | 751 | if (!id_wellformed(id)) { |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 752 | error_set(errp,QERR_INVALID_PARAMETER_VALUE, "id", "an identifier"); |
Markus Armbruster | b560a9a | 2010-06-08 13:54:26 +0200 | [diff] [blame] | 753 | error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n"); |
| 754 | return NULL; |
| 755 | } |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 756 | opts = qemu_opts_find(list, id); |
| 757 | if (opts != NULL) { |
Peter Maydell | da93318 | 2012-02-08 05:41:37 +0000 | [diff] [blame] | 758 | if (fail_if_exists && !list->merge_lists) { |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 759 | error_set(errp, QERR_DUPLICATE_ID, id, list->name); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 760 | return NULL; |
| 761 | } else { |
| 762 | return opts; |
| 763 | } |
| 764 | } |
Peter Maydell | da93318 | 2012-02-08 05:41:37 +0000 | [diff] [blame] | 765 | } else if (list->merge_lists) { |
| 766 | opts = qemu_opts_find(list, NULL); |
| 767 | if (opts) { |
| 768 | return opts; |
| 769 | } |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 770 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 771 | opts = g_malloc0(sizeof(*opts)); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 772 | if (id) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 773 | opts->id = g_strdup(id); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 774 | } |
| 775 | opts->list = list; |
Markus Armbruster | 827b081 | 2010-02-18 19:46:49 +0100 | [diff] [blame] | 776 | loc_save(&opts->loc); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 777 | QTAILQ_INIT(&opts->head); |
| 778 | QTAILQ_INSERT_TAIL(&list->head, opts, next); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 779 | return opts; |
| 780 | } |
| 781 | |
Markus Armbruster | bb67ab0 | 2010-06-01 10:47:34 +0200 | [diff] [blame] | 782 | void qemu_opts_reset(QemuOptsList *list) |
| 783 | { |
| 784 | QemuOpts *opts, *next_opts; |
| 785 | |
| 786 | QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) { |
| 787 | qemu_opts_del(opts); |
| 788 | } |
| 789 | } |
| 790 | |
Markus Armbruster | 94ac726 | 2010-05-27 21:06:04 +0200 | [diff] [blame] | 791 | void qemu_opts_loc_restore(QemuOpts *opts) |
| 792 | { |
| 793 | loc_restore(&opts->loc); |
| 794 | } |
| 795 | |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 796 | int qemu_opts_set(QemuOptsList *list, const char *id, |
| 797 | const char *name, const char *value) |
| 798 | { |
| 799 | QemuOpts *opts; |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 800 | Error *local_err = NULL; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 801 | |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 802 | opts = qemu_opts_create(list, id, 1, &local_err); |
| 803 | if (error_is_set(&local_err)) { |
| 804 | qerror_report_err(local_err); |
| 805 | error_free(local_err); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 806 | return -1; |
| 807 | } |
| 808 | return qemu_opt_set(opts, name, value); |
| 809 | } |
| 810 | |
Gerd Hoffmann | 4802607 | 2009-07-31 12:25:32 +0200 | [diff] [blame] | 811 | const char *qemu_opts_id(QemuOpts *opts) |
| 812 | { |
| 813 | return opts->id; |
| 814 | } |
| 815 | |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 816 | void qemu_opts_del(QemuOpts *opts) |
| 817 | { |
| 818 | QemuOpt *opt; |
| 819 | |
| 820 | for (;;) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 821 | opt = QTAILQ_FIRST(&opts->head); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 822 | if (opt == NULL) |
| 823 | break; |
| 824 | qemu_opt_del(opt); |
| 825 | } |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 826 | QTAILQ_REMOVE(&opts->list->head, opts, next); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 827 | g_free(opts->id); |
| 828 | g_free(opts); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | int qemu_opts_print(QemuOpts *opts, void *dummy) |
| 832 | { |
| 833 | QemuOpt *opt; |
| 834 | |
| 835 | fprintf(stderr, "%s: %s:", opts->list->name, |
| 836 | opts->id ? opts->id : "<noid>"); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 837 | QTAILQ_FOREACH(opt, &opts->head, next) { |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 838 | fprintf(stderr, " %s=\"%s\"", opt->name, opt->str); |
| 839 | } |
| 840 | fprintf(stderr, "\n"); |
| 841 | return 0; |
| 842 | } |
| 843 | |
Jan Kiszka | 4f6dd9a | 2012-01-27 19:54:54 +0100 | [diff] [blame] | 844 | static int opts_do_parse(QemuOpts *opts, const char *params, |
| 845 | const char *firstname, bool prepend) |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 846 | { |
Gerd Hoffmann | d318ff9 | 2009-12-10 11:11:08 +0100 | [diff] [blame] | 847 | char option[128], value[1024]; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 848 | const char *p,*pe,*pc; |
| 849 | |
Mark McLoughlin | 2cfa571 | 2009-10-06 12:17:02 +0100 | [diff] [blame] | 850 | for (p = params; *p != '\0'; p++) { |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 851 | pe = strchr(p, '='); |
| 852 | pc = strchr(p, ','); |
| 853 | if (!pe || (pc && pc < pe)) { |
| 854 | /* found "foo,more" */ |
| 855 | if (p == params && firstname) { |
| 856 | /* implicitly named first option */ |
| 857 | pstrcpy(option, sizeof(option), firstname); |
| 858 | p = get_opt_value(value, sizeof(value), p); |
| 859 | } else { |
| 860 | /* option without value, probably a flag */ |
| 861 | p = get_opt_name(option, sizeof(option), p, ','); |
Gerd Hoffmann | 96729cb | 2009-09-10 10:58:33 +0200 | [diff] [blame] | 862 | if (strncmp(option, "no", 2) == 0) { |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 863 | memmove(option, option+2, strlen(option+2)+1); |
| 864 | pstrcpy(value, sizeof(value), "off"); |
| 865 | } else { |
| 866 | pstrcpy(value, sizeof(value), "on"); |
| 867 | } |
| 868 | } |
| 869 | } else { |
| 870 | /* found "foo=bar,more" */ |
| 871 | p = get_opt_name(option, sizeof(option), p, '='); |
| 872 | if (*p != '=') { |
| 873 | break; |
| 874 | } |
| 875 | p++; |
| 876 | p = get_opt_value(value, sizeof(value), p); |
| 877 | } |
| 878 | if (strcmp(option, "id") != 0) { |
| 879 | /* store and parse */ |
Jan Kiszka | 4f6dd9a | 2012-01-27 19:54:54 +0100 | [diff] [blame] | 880 | if (opt_set(opts, option, value, prepend) == -1) { |
Gerd Hoffmann | 96729cb | 2009-09-10 10:58:33 +0200 | [diff] [blame] | 881 | return -1; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | if (*p != ',') { |
| 885 | break; |
| 886 | } |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 887 | } |
Gerd Hoffmann | 96729cb | 2009-09-10 10:58:33 +0200 | [diff] [blame] | 888 | return 0; |
| 889 | } |
| 890 | |
Jan Kiszka | 4f6dd9a | 2012-01-27 19:54:54 +0100 | [diff] [blame] | 891 | int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname) |
| 892 | { |
| 893 | return opts_do_parse(opts, params, firstname, false); |
| 894 | } |
| 895 | |
| 896 | static QemuOpts *opts_parse(QemuOptsList *list, const char *params, |
| 897 | int permit_abbrev, bool defaults) |
Gerd Hoffmann | 96729cb | 2009-09-10 10:58:33 +0200 | [diff] [blame] | 898 | { |
Markus Armbruster | 8212c64 | 2010-02-10 19:52:18 +0100 | [diff] [blame] | 899 | const char *firstname; |
Gerd Hoffmann | d318ff9 | 2009-12-10 11:11:08 +0100 | [diff] [blame] | 900 | char value[1024], *id = NULL; |
Gerd Hoffmann | 96729cb | 2009-09-10 10:58:33 +0200 | [diff] [blame] | 901 | const char *p; |
| 902 | QemuOpts *opts; |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 903 | Error *local_err = NULL; |
Gerd Hoffmann | 96729cb | 2009-09-10 10:58:33 +0200 | [diff] [blame] | 904 | |
Markus Armbruster | 8212c64 | 2010-02-10 19:52:18 +0100 | [diff] [blame] | 905 | assert(!permit_abbrev || list->implied_opt_name); |
| 906 | firstname = permit_abbrev ? list->implied_opt_name : NULL; |
| 907 | |
Gerd Hoffmann | 96729cb | 2009-09-10 10:58:33 +0200 | [diff] [blame] | 908 | if (strncmp(params, "id=", 3) == 0) { |
| 909 | get_opt_value(value, sizeof(value), params+3); |
Jan Kiszka | d510c5c | 2010-04-29 18:24:43 +0200 | [diff] [blame] | 910 | id = value; |
Gerd Hoffmann | 96729cb | 2009-09-10 10:58:33 +0200 | [diff] [blame] | 911 | } else if ((p = strstr(params, ",id=")) != NULL) { |
| 912 | get_opt_value(value, sizeof(value), p+4); |
Jan Kiszka | d510c5c | 2010-04-29 18:24:43 +0200 | [diff] [blame] | 913 | id = value; |
Gerd Hoffmann | 96729cb | 2009-09-10 10:58:33 +0200 | [diff] [blame] | 914 | } |
Jan Kiszka | 4f6dd9a | 2012-01-27 19:54:54 +0100 | [diff] [blame] | 915 | if (defaults) { |
| 916 | if (!id && !QTAILQ_EMPTY(&list->head)) { |
| 917 | opts = qemu_opts_find(list, NULL); |
| 918 | } else { |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 919 | opts = qemu_opts_create(list, id, 0, &local_err); |
Jan Kiszka | 4f6dd9a | 2012-01-27 19:54:54 +0100 | [diff] [blame] | 920 | } |
| 921 | } else { |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 922 | opts = qemu_opts_create(list, id, 1, &local_err); |
Jan Kiszka | 4f6dd9a | 2012-01-27 19:54:54 +0100 | [diff] [blame] | 923 | } |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 924 | if (opts == NULL) { |
| 925 | if (error_is_set(&local_err)) { |
| 926 | qerror_report_err(local_err); |
| 927 | error_free(local_err); |
| 928 | } |
Gerd Hoffmann | 96729cb | 2009-09-10 10:58:33 +0200 | [diff] [blame] | 929 | return NULL; |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 930 | } |
Gerd Hoffmann | 96729cb | 2009-09-10 10:58:33 +0200 | [diff] [blame] | 931 | |
Jan Kiszka | 4f6dd9a | 2012-01-27 19:54:54 +0100 | [diff] [blame] | 932 | if (opts_do_parse(opts, params, firstname, defaults) != 0) { |
Gerd Hoffmann | 96729cb | 2009-09-10 10:58:33 +0200 | [diff] [blame] | 933 | qemu_opts_del(opts); |
| 934 | return NULL; |
| 935 | } |
| 936 | |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 937 | return opts; |
| 938 | } |
| 939 | |
Jan Kiszka | 4f6dd9a | 2012-01-27 19:54:54 +0100 | [diff] [blame] | 940 | QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, |
| 941 | int permit_abbrev) |
| 942 | { |
| 943 | return opts_parse(list, params, permit_abbrev, false); |
| 944 | } |
| 945 | |
| 946 | void qemu_opts_set_defaults(QemuOptsList *list, const char *params, |
| 947 | int permit_abbrev) |
| 948 | { |
| 949 | QemuOpts *opts; |
| 950 | |
| 951 | opts = opts_parse(list, params, permit_abbrev, true); |
| 952 | assert(opts); |
| 953 | } |
| 954 | |
Markus Armbruster | 01e7f18 | 2010-02-10 20:15:29 +0100 | [diff] [blame] | 955 | static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque) |
| 956 | { |
| 957 | char buf[32]; |
| 958 | const char *value; |
| 959 | int n; |
| 960 | |
| 961 | if (!strcmp(key, "id")) { |
| 962 | return; |
| 963 | } |
| 964 | |
| 965 | switch (qobject_type(obj)) { |
| 966 | case QTYPE_QSTRING: |
| 967 | value = qstring_get_str(qobject_to_qstring(obj)); |
| 968 | break; |
| 969 | case QTYPE_QINT: |
| 970 | n = snprintf(buf, sizeof(buf), "%" PRId64, |
| 971 | qint_get_int(qobject_to_qint(obj))); |
| 972 | assert(n < sizeof(buf)); |
| 973 | value = buf; |
| 974 | break; |
| 975 | case QTYPE_QFLOAT: |
| 976 | n = snprintf(buf, sizeof(buf), "%.17g", |
| 977 | qfloat_get_double(qobject_to_qfloat(obj))); |
| 978 | assert(n < sizeof(buf)); |
| 979 | value = buf; |
| 980 | break; |
| 981 | case QTYPE_QBOOL: |
Blue Swirl | d35215f | 2010-03-18 20:48:19 +0000 | [diff] [blame] | 982 | pstrcpy(buf, sizeof(buf), |
| 983 | qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off"); |
Markus Armbruster | 01e7f18 | 2010-02-10 20:15:29 +0100 | [diff] [blame] | 984 | value = buf; |
| 985 | break; |
| 986 | default: |
| 987 | return; |
| 988 | } |
| 989 | qemu_opt_set(opaque, key, value); |
| 990 | } |
| 991 | |
| 992 | /* |
| 993 | * Create QemuOpts from a QDict. |
| 994 | * Use value of key "id" as ID if it exists and is a QString. |
| 995 | * Only QStrings, QInts, QFloats and QBools are copied. Entries with |
| 996 | * other types are silently ignored. |
| 997 | */ |
| 998 | QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict) |
| 999 | { |
| 1000 | QemuOpts *opts; |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 1001 | Error *local_err = NULL; |
Markus Armbruster | 01e7f18 | 2010-02-10 20:15:29 +0100 | [diff] [blame] | 1002 | |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 1003 | opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1, |
| 1004 | &local_err); |
| 1005 | if (error_is_set(&local_err)) { |
| 1006 | qerror_report_err(local_err); |
| 1007 | error_free(local_err); |
Markus Armbruster | 01e7f18 | 2010-02-10 20:15:29 +0100 | [diff] [blame] | 1008 | return NULL; |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 1009 | } |
Markus Armbruster | 01e7f18 | 2010-02-10 20:15:29 +0100 | [diff] [blame] | 1010 | |
Luiz Capitulino | 8be7e7e | 2012-03-20 15:51:57 -0300 | [diff] [blame] | 1011 | assert(opts != NULL); |
Markus Armbruster | 01e7f18 | 2010-02-10 20:15:29 +0100 | [diff] [blame] | 1012 | qdict_iter(qdict, qemu_opts_from_qdict_1, opts); |
| 1013 | return opts; |
| 1014 | } |
| 1015 | |
| 1016 | /* |
| 1017 | * Convert from QemuOpts to QDict. |
| 1018 | * The QDict values are of type QString. |
| 1019 | * TODO We'll want to use types appropriate for opt->desc->type, but |
| 1020 | * this is enough for now. |
| 1021 | */ |
| 1022 | QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict) |
| 1023 | { |
| 1024 | QemuOpt *opt; |
| 1025 | QObject *val; |
| 1026 | |
| 1027 | if (!qdict) { |
| 1028 | qdict = qdict_new(); |
| 1029 | } |
| 1030 | if (opts->id) { |
| 1031 | qdict_put(qdict, "id", qstring_from_str(opts->id)); |
| 1032 | } |
| 1033 | QTAILQ_FOREACH(opt, &opts->head, next) { |
| 1034 | val = QOBJECT(qstring_from_str(opt->str)); |
| 1035 | qdict_put_obj(qdict, opt->name, val); |
| 1036 | } |
| 1037 | return qdict; |
| 1038 | } |
| 1039 | |
Mark McLoughlin | 5dc519e | 2009-10-06 12:17:03 +0100 | [diff] [blame] | 1040 | /* Validate parsed opts against descriptions where no |
| 1041 | * descriptions were provided in the QemuOptsList. |
| 1042 | */ |
Blue Swirl | 238431a | 2010-02-21 16:01:30 +0000 | [diff] [blame] | 1043 | int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc) |
Mark McLoughlin | 5dc519e | 2009-10-06 12:17:03 +0100 | [diff] [blame] | 1044 | { |
| 1045 | QemuOpt *opt; |
| 1046 | |
| 1047 | assert(opts->list->desc[0].name == NULL); |
| 1048 | |
| 1049 | QTAILQ_FOREACH(opt, &opts->head, next) { |
| 1050 | int i; |
| 1051 | |
| 1052 | for (i = 0; desc[i].name != NULL; i++) { |
| 1053 | if (strcmp(desc[i].name, opt->name) == 0) { |
| 1054 | break; |
| 1055 | } |
| 1056 | } |
| 1057 | if (desc[i].name == NULL) { |
Markus Armbruster | db716e9 | 2010-03-25 17:22:37 +0100 | [diff] [blame] | 1058 | qerror_report(QERR_INVALID_PARAMETER, opt->name); |
Mark McLoughlin | 5dc519e | 2009-10-06 12:17:03 +0100 | [diff] [blame] | 1059 | return -1; |
| 1060 | } |
| 1061 | |
| 1062 | opt->desc = &desc[i]; |
| 1063 | |
| 1064 | if (qemu_opt_parse(opt) < 0) { |
| 1065 | return -1; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | return 0; |
| 1070 | } |
| 1071 | |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 1072 | int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque, |
| 1073 | int abort_on_failure) |
| 1074 | { |
Markus Armbruster | 827b081 | 2010-02-18 19:46:49 +0100 | [diff] [blame] | 1075 | Location loc; |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 1076 | QemuOpts *opts; |
| 1077 | int rc = 0; |
| 1078 | |
Markus Armbruster | 827b081 | 2010-02-18 19:46:49 +0100 | [diff] [blame] | 1079 | loc_push_none(&loc); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 1080 | QTAILQ_FOREACH(opts, &list->head, next) { |
Markus Armbruster | 827b081 | 2010-02-18 19:46:49 +0100 | [diff] [blame] | 1081 | loc_restore(&opts->loc); |
Markus Armbruster | 4a2594d | 2010-01-29 19:48:56 +0100 | [diff] [blame] | 1082 | rc |= func(opts, opaque); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 1083 | if (abort_on_failure && rc != 0) |
| 1084 | break; |
| 1085 | } |
Markus Armbruster | 827b081 | 2010-02-18 19:46:49 +0100 | [diff] [blame] | 1086 | loc_pop(&loc); |
Gerd Hoffmann | e27c88f | 2009-07-22 16:43:03 +0200 | [diff] [blame] | 1087 | return rc; |
| 1088 | } |