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