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