| 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 "qemu/osdep.h" |
| 27 | |
| 28 | #include "qapi/error.h" |
| 29 | #include "qemu/error-report.h" |
| 30 | #include "qobject/qbool.h" |
| 31 | #include "qobject/qdict.h" |
| 32 | #include "qobject/qnum.h" |
| 33 | #include "qobject/qstring.h" |
| 34 | #include "qapi/qmp/qerror.h" |
| 35 | #include "qemu/option_int.h" |
| 36 | #include "qemu/cutils.h" |
| 37 | #include "qemu/id.h" |
| 38 | #include "qemu/help_option.h" |
| 39 | |
| 40 | /* |
| 41 | * Extracts the name of an option from the parameter string (@p points at the |
| 42 | * first byte of the option name) |
| 43 | * |
| 44 | * The option name is @len characters long and is copied into @option. The |
| 45 | * caller is responsible for free'ing @option when no longer required. |
| 46 | * |
| 47 | * The return value is the position of the delimiter/zero byte after the option |
| 48 | * name in @p. |
| 49 | */ |
| 50 | static const char *get_opt_name(const char *p, char **option, size_t len) |
| 51 | { |
| 52 | *option = g_strndup(p, len); |
| 53 | return p + len; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Extracts the value of an option from the parameter string p (p points at the |
| 58 | * first byte of the option value) |
| 59 | * |
| 60 | * This function is comparable to get_opt_name with the difference that the |
| 61 | * delimiter is fixed to be comma which starts a new option. To specify an |
| 62 | * option value that contains commas, double each comma. |
| 63 | */ |
| 64 | const char *get_opt_value(const char *p, char **value) |
| 65 | { |
| 66 | size_t capacity = 0, length; |
| 67 | const char *offset; |
| 68 | |
| 69 | *value = NULL; |
| 70 | while (1) { |
| 71 | offset = qemu_strchrnul(p, ','); |
| 72 | length = offset - p; |
| 73 | if (*offset != '\0' && *(offset + 1) == ',') { |
| 74 | length++; |
| 75 | } |
| 76 | *value = g_renew(char, *value, capacity + length + 1); |
| 77 | strncpy(*value + capacity, p, length); |
| 78 | (*value)[capacity + length] = '\0'; |
| 79 | capacity += length; |
| 80 | if (*offset == '\0' || |
| 81 | *(offset + 1) != ',') { |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | p += (offset - p) + 2; |
| 86 | } |
| 87 | |
| 88 | return offset; |
| 89 | } |
| 90 | |
| 91 | static bool parse_option_number(const char *name, const char *value, |
| 92 | uint64_t *ret, Error **errp) |
| 93 | { |
| 94 | uint64_t number; |
| 95 | int err; |
| 96 | |
| 97 | err = qemu_strtou64(value, NULL, 0, &number); |
| 98 | if (err == -ERANGE) { |
| 99 | error_setg(errp, "Value '%s' is too large for parameter '%s'", |
| 100 | value, name); |
| 101 | return false; |
| 102 | } |
| 103 | if (err) { |
| 104 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number"); |
| 105 | return false; |
| 106 | } |
| 107 | *ret = number; |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc, |
| 112 | const char *name) |
| 113 | { |
| 114 | int i; |
| 115 | |
| 116 | for (i = 0; desc[i].name != NULL; i++) { |
| 117 | if (strcmp(desc[i].name, name) == 0) { |
| 118 | return &desc[i]; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | static const char *find_default_by_name(QemuOpts *opts, const char *name) |
| 126 | { |
| 127 | const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name); |
| 128 | |
| 129 | return desc ? desc->def_value_str : NULL; |
| 130 | } |
| 131 | |
| 132 | bool parse_option_size(const char *name, const char *value, |
| 133 | uint64_t *ret, Error **errp) |
| 134 | { |
| 135 | uint64_t size; |
| 136 | int err; |
| 137 | |
| 138 | err = qemu_strtosz(value, NULL, &size); |
| 139 | if (err == -ERANGE) { |
| 140 | error_setg(errp, "Value '%s' is out of range for parameter '%s'", |
| 141 | value, name); |
| 142 | return false; |
| 143 | } |
| 144 | if (err) { |
| 145 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, |
| 146 | "a non-negative number below 2^64"); |
| 147 | error_append_hint(errp, "Optional suffix k, M, G, T, P or E means" |
| 148 | " kilo-, mega-, giga-, tera-, peta-\n" |
| 149 | "and exabytes, respectively.\n"); |
| 150 | return false; |
| 151 | } |
| 152 | *ret = size; |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | static const char *opt_type_to_string(enum QemuOptType type) |
| 157 | { |
| 158 | switch (type) { |
| 159 | case QEMU_OPT_STRING: |
| 160 | return "str"; |
| 161 | case QEMU_OPT_BOOL: |
| 162 | return "bool (on/off)"; |
| 163 | case QEMU_OPT_NUMBER: |
| 164 | return "num"; |
| 165 | case QEMU_OPT_SIZE: |
| 166 | return "size"; |
| 167 | } |
| 168 | |
| 169 | g_assert_not_reached(); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Print the list of options available in the given list. If |
| 174 | * @print_caption is true, a caption (including the list name, if it |
| 175 | * exists) is printed. The options itself will be indented, so |
| 176 | * @print_caption should only be set to false if the caller prints its |
| 177 | * own custom caption (so that the indentation makes sense). |
| 178 | */ |
| 179 | void qemu_opts_print_help(QemuOptsList *list, bool print_caption) |
| 180 | { |
| 181 | QemuOptDesc *desc; |
| 182 | int i; |
| 183 | GPtrArray *array = g_ptr_array_new(); |
| 184 | |
| 185 | assert(list); |
| 186 | desc = list->desc; |
| 187 | while (desc && desc->name) { |
| 188 | GString *str = g_string_new(NULL); |
| 189 | g_string_append_printf(str, " %s=<%s>", desc->name, |
| 190 | opt_type_to_string(desc->type)); |
| 191 | if (desc->help) { |
| 192 | if (str->len < 24) { |
| 193 | g_string_append_printf(str, "%*s", 24 - (int)str->len, ""); |
| 194 | } |
| 195 | g_string_append_printf(str, " - %s", desc->help); |
| 196 | } |
| 197 | g_ptr_array_add(array, g_string_free(str, false)); |
| 198 | desc++; |
| 199 | } |
| 200 | |
| 201 | g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0); |
| 202 | if (print_caption && array->len > 0) { |
| 203 | if (list->name) { |
| 204 | printf("%s options:\n", list->name); |
| 205 | } else { |
| 206 | printf("Options:\n"); |
| 207 | } |
| 208 | } else if (array->len == 0) { |
| 209 | if (list->name) { |
| 210 | printf("There are no options for %s.\n", list->name); |
| 211 | } else { |
| 212 | printf("No options available.\n"); |
| 213 | } |
| 214 | } |
| 215 | for (i = 0; i < array->len; i++) { |
| 216 | printf("%s\n", (char *)array->pdata[i]); |
| 217 | } |
| 218 | g_ptr_array_set_free_func(array, g_free); |
| 219 | g_ptr_array_free(array, true); |
| 220 | |
| 221 | } |
| 222 | /* ------------------------------------------------------------------ */ |
| 223 | |
| 224 | QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name) |
| 225 | { |
| 226 | QemuOpt *opt; |
| 227 | |
| 228 | QTAILQ_FOREACH_REVERSE(opt, &opts->head, next) { |
| 229 | if (strcmp(opt->name, name) != 0) |
| 230 | continue; |
| 231 | return opt; |
| 232 | } |
| 233 | return NULL; |
| 234 | } |
| 235 | |
| 236 | static void qemu_opt_del(QemuOpt *opt) |
| 237 | { |
| 238 | QTAILQ_REMOVE(&opt->opts->head, opt, next); |
| 239 | g_free(opt->name); |
| 240 | g_free(opt->str); |
| 241 | g_free(opt); |
| 242 | } |
| 243 | |
| 244 | /* qemu_opt_set allows many settings for the same option. |
| 245 | * This function deletes all settings for an option. |
| 246 | */ |
| 247 | static void qemu_opt_del_all(QemuOpts *opts, const char *name) |
| 248 | { |
| 249 | QemuOpt *opt, *next_opt; |
| 250 | |
| 251 | QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next_opt) { |
| 252 | if (!strcmp(opt->name, name)) { |
| 253 | qemu_opt_del(opt); |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | const char *qemu_opt_get(QemuOpts *opts, const char *name) |
| 259 | { |
| 260 | QemuOpt *opt; |
| 261 | |
| 262 | if (opts == NULL) { |
| 263 | return NULL; |
| 264 | } |
| 265 | |
| 266 | opt = qemu_opt_find(opts, name); |
| 267 | if (!opt) { |
| 268 | return find_default_by_name(opts, name); |
| 269 | } |
| 270 | |
| 271 | return opt->str; |
| 272 | } |
| 273 | |
| 274 | bool qemu_opt_has_any(QemuOpts *opts, const char * const *names) |
| 275 | { |
| 276 | int it; |
| 277 | |
| 278 | for (it = 0; names[it]; it++) { |
| 279 | if (qemu_opt_get(opts, names[it])) { |
| 280 | return true; |
| 281 | } |
| 282 | } |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | void qemu_opt_iter_init(QemuOptsIter *iter, QemuOpts *opts, const char *name) |
| 288 | { |
| 289 | iter->opts = opts; |
| 290 | iter->opt = QTAILQ_FIRST(&opts->head); |
| 291 | iter->name = name; |
| 292 | } |
| 293 | |
| 294 | const char *qemu_opt_iter_next(QemuOptsIter *iter) |
| 295 | { |
| 296 | QemuOpt *ret = iter->opt; |
| 297 | if (iter->name) { |
| 298 | while (ret && !g_str_equal(iter->name, ret->name)) { |
| 299 | ret = QTAILQ_NEXT(ret, next); |
| 300 | } |
| 301 | } |
| 302 | iter->opt = ret ? QTAILQ_NEXT(ret, next) : NULL; |
| 303 | return ret ? ret->str : NULL; |
| 304 | } |
| 305 | |
| 306 | /* Get a known option (or its default) and remove it from the list |
| 307 | * all in one action. Return a malloced string of the option value. |
| 308 | * Result must be freed by caller with g_free(). |
| 309 | */ |
| 310 | char *qemu_opt_get_del(QemuOpts *opts, const char *name) |
| 311 | { |
| 312 | QemuOpt *opt; |
| 313 | char *str; |
| 314 | |
| 315 | if (opts == NULL) { |
| 316 | return NULL; |
| 317 | } |
| 318 | |
| 319 | opt = qemu_opt_find(opts, name); |
| 320 | if (!opt) { |
| 321 | return g_strdup(find_default_by_name(opts, name)); |
| 322 | } |
| 323 | str = opt->str; |
| 324 | opt->str = NULL; |
| 325 | qemu_opt_del_all(opts, name); |
| 326 | return str; |
| 327 | } |
| 328 | |
| 329 | bool qemu_opt_has_help_opt(QemuOpts *opts) |
| 330 | { |
| 331 | QemuOpt *opt; |
| 332 | |
| 333 | QTAILQ_FOREACH_REVERSE(opt, &opts->head, next) { |
| 334 | if (is_help_option(opt->name)) { |
| 335 | return true; |
| 336 | } |
| 337 | } |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | static bool qemu_opt_get_bool_helper(QemuOpts *opts, const char *name, |
| 342 | bool defval, bool del) |
| 343 | { |
| 344 | QemuOpt *opt; |
| 345 | const char *def_val; |
| 346 | bool ret = defval; |
| 347 | |
| 348 | if (opts == NULL) { |
| 349 | return ret; |
| 350 | } |
| 351 | |
| 352 | opt = qemu_opt_find(opts, name); |
| 353 | if (opt == NULL) { |
| 354 | def_val = find_default_by_name(opts, name); |
| 355 | if (def_val) { |
| 356 | qapi_bool_parse(name, def_val, &ret, &error_abort); |
| 357 | } |
| 358 | return ret; |
| 359 | } |
| 360 | assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL); |
| 361 | ret = opt->value.boolean; |
| 362 | if (del) { |
| 363 | qemu_opt_del_all(opts, name); |
| 364 | } |
| 365 | return ret; |
| 366 | } |
| 367 | |
| 368 | bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval) |
| 369 | { |
| 370 | return qemu_opt_get_bool_helper(opts, name, defval, false); |
| 371 | } |
| 372 | |
| 373 | bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval) |
| 374 | { |
| 375 | return qemu_opt_get_bool_helper(opts, name, defval, true); |
| 376 | } |
| 377 | |
| 378 | static uint64_t qemu_opt_get_number_helper(QemuOpts *opts, const char *name, |
| 379 | uint64_t defval, bool del) |
| 380 | { |
| 381 | QemuOpt *opt; |
| 382 | const char *def_val; |
| 383 | uint64_t ret = defval; |
| 384 | |
| 385 | if (opts == NULL) { |
| 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | opt = qemu_opt_find(opts, name); |
| 390 | if (opt == NULL) { |
| 391 | def_val = find_default_by_name(opts, name); |
| 392 | if (def_val) { |
| 393 | parse_option_number(name, def_val, &ret, &error_abort); |
| 394 | } |
| 395 | return ret; |
| 396 | } |
| 397 | assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER); |
| 398 | ret = opt->value.uint; |
| 399 | if (del) { |
| 400 | qemu_opt_del_all(opts, name); |
| 401 | } |
| 402 | return ret; |
| 403 | } |
| 404 | |
| 405 | uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval) |
| 406 | { |
| 407 | return qemu_opt_get_number_helper(opts, name, defval, false); |
| 408 | } |
| 409 | |
| 410 | uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name, |
| 411 | uint64_t defval) |
| 412 | { |
| 413 | return qemu_opt_get_number_helper(opts, name, defval, true); |
| 414 | } |
| 415 | |
| 416 | static uint64_t qemu_opt_get_size_helper(QemuOpts *opts, const char *name, |
| 417 | uint64_t defval, bool del) |
| 418 | { |
| 419 | QemuOpt *opt; |
| 420 | const char *def_val; |
| 421 | uint64_t ret = defval; |
| 422 | |
| 423 | if (opts == NULL) { |
| 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | opt = qemu_opt_find(opts, name); |
| 428 | if (opt == NULL) { |
| 429 | def_val = find_default_by_name(opts, name); |
| 430 | if (def_val) { |
| 431 | parse_option_size(name, def_val, &ret, &error_abort); |
| 432 | } |
| 433 | return ret; |
| 434 | } |
| 435 | assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE); |
| 436 | ret = opt->value.uint; |
| 437 | if (del) { |
| 438 | qemu_opt_del_all(opts, name); |
| 439 | } |
| 440 | return ret; |
| 441 | } |
| 442 | |
| 443 | uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval) |
| 444 | { |
| 445 | return qemu_opt_get_size_helper(opts, name, defval, false); |
| 446 | } |
| 447 | |
| 448 | uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name, |
| 449 | uint64_t defval) |
| 450 | { |
| 451 | return qemu_opt_get_size_helper(opts, name, defval, true); |
| 452 | } |
| 453 | |
| 454 | static bool qemu_opt_parse(QemuOpt *opt, Error **errp) |
| 455 | { |
| 456 | if (opt->desc == NULL) |
| 457 | return true; |
| 458 | |
| 459 | switch (opt->desc->type) { |
| 460 | case QEMU_OPT_STRING: |
| 461 | /* nothing */ |
| 462 | return true; |
| 463 | case QEMU_OPT_BOOL: |
| 464 | return qapi_bool_parse(opt->name, opt->str, &opt->value.boolean, errp); |
| 465 | case QEMU_OPT_NUMBER: |
| 466 | return parse_option_number(opt->name, opt->str, &opt->value.uint, |
| 467 | errp); |
| 468 | case QEMU_OPT_SIZE: |
| 469 | return parse_option_size(opt->name, opt->str, &opt->value.uint, |
| 470 | errp); |
| 471 | default: |
| 472 | abort(); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | static bool opts_accepts_any(const QemuOptsList *list) |
| 477 | { |
| 478 | return list->desc[0].name == NULL; |
| 479 | } |
| 480 | |
| 481 | int qemu_opt_unset(QemuOpts *opts, const char *name) |
| 482 | { |
| 483 | QemuOpt *opt = qemu_opt_find(opts, name); |
| 484 | |
| 485 | assert(opts_accepts_any(opts->list)); |
| 486 | |
| 487 | if (opt == NULL) { |
| 488 | return -1; |
| 489 | } else { |
| 490 | qemu_opt_del(opt); |
| 491 | return 0; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | static QemuOpt *opt_create(QemuOpts *opts, const char *name, char *value) |
| 496 | { |
| 497 | QemuOpt *opt = g_malloc0(sizeof(*opt)); |
| 498 | |
| 499 | opt->name = g_strdup(name); |
| 500 | opt->str = value; |
| 501 | opt->opts = opts; |
| 502 | QTAILQ_INSERT_TAIL(&opts->head, opt, next); |
| 503 | |
| 504 | return opt; |
| 505 | } |
| 506 | |
| 507 | static bool opt_validate(QemuOpt *opt, Error **errp) |
| 508 | { |
| 509 | const QemuOptDesc *desc; |
| 510 | const QemuOptsList *list = opt->opts->list; |
| 511 | |
| 512 | desc = find_desc_by_name(list->desc, opt->name); |
| 513 | if (!desc && !opts_accepts_any(list)) { |
| 514 | error_setg(errp, "Invalid parameter '%s'", opt->name); |
| 515 | return false; |
| 516 | } |
| 517 | |
| 518 | opt->desc = desc; |
| 519 | if (!qemu_opt_parse(opt, errp)) { |
| 520 | return false; |
| 521 | } |
| 522 | |
| 523 | return true; |
| 524 | } |
| 525 | |
| 526 | bool qemu_opt_set(QemuOpts *opts, const char *name, const char *value, |
| 527 | Error **errp) |
| 528 | { |
| 529 | QemuOpt *opt = opt_create(opts, name, g_strdup(value)); |
| 530 | |
| 531 | if (!opt_validate(opt, errp)) { |
| 532 | qemu_opt_del(opt); |
| 533 | return false; |
| 534 | } |
| 535 | return true; |
| 536 | } |
| 537 | |
| 538 | bool qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val, |
| 539 | Error **errp) |
| 540 | { |
| 541 | QemuOpt *opt; |
| 542 | const QemuOptDesc *desc; |
| 543 | const QemuOptsList *list = opts->list; |
| 544 | |
| 545 | desc = find_desc_by_name(list->desc, name); |
| 546 | if (!desc && !opts_accepts_any(list)) { |
| 547 | error_setg(errp, "Invalid parameter '%s'", name); |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | opt = g_malloc0(sizeof(*opt)); |
| 552 | opt->name = g_strdup(name); |
| 553 | opt->opts = opts; |
| 554 | opt->desc = desc; |
| 555 | opt->value.boolean = !!val; |
| 556 | opt->str = g_strdup(val ? "on" : "off"); |
| 557 | QTAILQ_INSERT_TAIL(&opts->head, opt, next); |
| 558 | return true; |
| 559 | } |
| 560 | |
| 561 | bool qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val, |
| 562 | Error **errp) |
| 563 | { |
| 564 | QemuOpt *opt; |
| 565 | const QemuOptDesc *desc; |
| 566 | const QemuOptsList *list = opts->list; |
| 567 | |
| 568 | desc = find_desc_by_name(list->desc, name); |
| 569 | if (!desc && !opts_accepts_any(list)) { |
| 570 | error_setg(errp, "Invalid parameter '%s'", name); |
| 571 | return false; |
| 572 | } |
| 573 | |
| 574 | opt = g_malloc0(sizeof(*opt)); |
| 575 | opt->name = g_strdup(name); |
| 576 | opt->opts = opts; |
| 577 | opt->desc = desc; |
| 578 | opt->value.uint = val; |
| 579 | opt->str = g_strdup_printf("%" PRId64, val); |
| 580 | QTAILQ_INSERT_TAIL(&opts->head, opt, next); |
| 581 | return true; |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * For each member of @opts, call @func(@opaque, name, value, @errp). |
| 586 | * @func() may store an Error through @errp, but must return non-zero then. |
| 587 | * When @func() returns non-zero, break the loop and return that value. |
| 588 | * Return zero when the loop completes. |
| 589 | */ |
| 590 | int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque, |
| 591 | Error **errp) |
| 592 | { |
| 593 | QemuOpt *opt; |
| 594 | int rc; |
| 595 | |
| 596 | QTAILQ_FOREACH(opt, &opts->head, next) { |
| 597 | rc = func(opaque, opt->name, opt->str, errp); |
| 598 | if (rc) { |
| 599 | return rc; |
| 600 | } |
| 601 | assert(!errp || !*errp); |
| 602 | } |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id) |
| 607 | { |
| 608 | QemuOpts *opts; |
| 609 | |
| 610 | QTAILQ_FOREACH(opts, &list->head, next) { |
| 611 | if (!opts->id && !id) { |
| 612 | return opts; |
| 613 | } |
| 614 | if (opts->id && id && !strcmp(opts->id, id)) { |
| 615 | return opts; |
| 616 | } |
| 617 | } |
| 618 | return NULL; |
| 619 | } |
| 620 | |
| 621 | QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, |
| 622 | int fail_if_exists, Error **errp) |
| 623 | { |
| 624 | QemuOpts *opts = NULL; |
| 625 | |
| 626 | if (list->merge_lists) { |
| 627 | if (id) { |
| 628 | error_setg(errp, "Invalid parameter 'id'"); |
| 629 | return NULL; |
| 630 | } |
| 631 | opts = qemu_opts_find(list, NULL); |
| 632 | if (opts) { |
| 633 | return opts; |
| 634 | } |
| 635 | } else if (id) { |
| 636 | assert(fail_if_exists); |
| 637 | if (!id_wellformed(id)) { |
| 638 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id", |
| 639 | "an identifier"); |
| 640 | error_append_hint(errp, "Identifiers consist of letters, digits, " |
| 641 | "'-', '.', '_', starting with a letter.\n"); |
| 642 | return NULL; |
| 643 | } |
| 644 | opts = qemu_opts_find(list, id); |
| 645 | if (opts != NULL) { |
| 646 | error_setg(errp, "Duplicate ID '%s' for %s", id, list->name); |
| 647 | return NULL; |
| 648 | } |
| 649 | } |
| 650 | opts = g_malloc0(sizeof(*opts)); |
| 651 | opts->id = g_strdup(id); |
| 652 | opts->list = list; |
| 653 | loc_save(&opts->loc); |
| 654 | QTAILQ_INIT(&opts->head); |
| 655 | QTAILQ_INSERT_TAIL(&list->head, opts, next); |
| 656 | return opts; |
| 657 | } |
| 658 | |
| 659 | void qemu_opts_reset(QemuOptsList *list) |
| 660 | { |
| 661 | QemuOpts *opts, *next_opts; |
| 662 | |
| 663 | QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) { |
| 664 | qemu_opts_del(opts); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | void qemu_opts_loc_restore(QemuOpts *opts) |
| 669 | { |
| 670 | loc_restore(&opts->loc); |
| 671 | } |
| 672 | |
| 673 | const char *qemu_opts_id(QemuOpts *opts) |
| 674 | { |
| 675 | return opts->id; |
| 676 | } |
| 677 | |
| 678 | /* The id string will be g_free()d by qemu_opts_del */ |
| 679 | void qemu_opts_set_id(QemuOpts *opts, char *id) |
| 680 | { |
| 681 | opts->id = id; |
| 682 | } |
| 683 | |
| 684 | void qemu_opts_del(QemuOpts *opts) |
| 685 | { |
| 686 | QemuOpt *opt; |
| 687 | |
| 688 | if (opts == NULL) { |
| 689 | return; |
| 690 | } |
| 691 | |
| 692 | for (;;) { |
| 693 | opt = QTAILQ_FIRST(&opts->head); |
| 694 | if (opt == NULL) |
| 695 | break; |
| 696 | qemu_opt_del(opt); |
| 697 | } |
| 698 | QTAILQ_REMOVE(&opts->list->head, opts, next); |
| 699 | g_free(opts->id); |
| 700 | g_free(opts); |
| 701 | } |
| 702 | |
| 703 | /* print value, escaping any commas in value */ |
| 704 | static void escaped_print(const char *value) |
| 705 | { |
| 706 | const char *ptr; |
| 707 | |
| 708 | for (ptr = value; *ptr; ++ptr) { |
| 709 | if (*ptr == ',') { |
| 710 | putchar(','); |
| 711 | } |
| 712 | putchar(*ptr); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | void qemu_opts_print(QemuOpts *opts, const char *separator) |
| 717 | { |
| 718 | QemuOpt *opt; |
| 719 | QemuOptDesc *desc = opts->list->desc; |
| 720 | const char *sep = ""; |
| 721 | |
| 722 | if (opts->id) { |
| 723 | printf("id=%s", opts->id); /* passed id_wellformed -> no commas */ |
| 724 | sep = separator; |
| 725 | } |
| 726 | |
| 727 | if (desc[0].name == NULL) { |
| 728 | QTAILQ_FOREACH(opt, &opts->head, next) { |
| 729 | printf("%s%s=", sep, opt->name); |
| 730 | escaped_print(opt->str); |
| 731 | sep = separator; |
| 732 | } |
| 733 | return; |
| 734 | } |
| 735 | for (; desc && desc->name; desc++) { |
| 736 | const char *value; |
| 737 | opt = qemu_opt_find(opts, desc->name); |
| 738 | |
| 739 | value = opt ? opt->str : desc->def_value_str; |
| 740 | if (!value) { |
| 741 | continue; |
| 742 | } |
| 743 | if (desc->type == QEMU_OPT_STRING) { |
| 744 | printf("%s%s=", sep, desc->name); |
| 745 | escaped_print(value); |
| 746 | } else if ((desc->type == QEMU_OPT_SIZE || |
| 747 | desc->type == QEMU_OPT_NUMBER) && opt) { |
| 748 | printf("%s%s=%" PRId64, sep, desc->name, opt->value.uint); |
| 749 | } else { |
| 750 | printf("%s%s=%s", sep, desc->name, value); |
| 751 | } |
| 752 | sep = separator; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | static const char *get_opt_name_value(const char *params, |
| 757 | const char *firstname, |
| 758 | bool warn_on_flag, |
| 759 | bool *help_wanted, |
| 760 | char **name, char **value) |
| 761 | { |
| 762 | const char *p; |
| 763 | const char *prefix = ""; |
| 764 | size_t len; |
| 765 | bool is_help = false; |
| 766 | |
| 767 | len = strcspn(params, "=,"); |
| 768 | if (params[len] != '=') { |
| 769 | /* found "foo,more" */ |
| 770 | if (firstname) { |
| 771 | /* implicitly named first option */ |
| 772 | *name = g_strdup(firstname); |
| 773 | p = get_opt_value(params, value); |
| 774 | } else { |
| 775 | /* option without value, must be a flag */ |
| 776 | p = get_opt_name(params, name, len); |
| 777 | if (strncmp(*name, "no", 2) == 0) { |
| 778 | memmove(*name, *name + 2, strlen(*name + 2) + 1); |
| 779 | *value = g_strdup("off"); |
| 780 | prefix = "no"; |
| 781 | } else { |
| 782 | *value = g_strdup("on"); |
| 783 | is_help = is_help_option(*name); |
| 784 | } |
| 785 | if (!is_help && warn_on_flag) { |
| 786 | warn_report("short-form boolean option '%s%s' deprecated", prefix, *name); |
| 787 | if (g_str_equal(*name, "delay")) { |
| 788 | error_printf("Please use nodelay=%s instead\n", prefix[0] ? "on" : "off"); |
| 789 | } else { |
| 790 | error_printf("Please use %s=%s instead\n", *name, *value); |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | } else { |
| 795 | /* found "foo=bar,more" */ |
| 796 | p = get_opt_name(params, name, len); |
| 797 | assert(*p == '='); |
| 798 | p++; |
| 799 | p = get_opt_value(p, value); |
| 800 | } |
| 801 | |
| 802 | assert(!*p || *p == ','); |
| 803 | if (help_wanted && is_help) { |
| 804 | *help_wanted = true; |
| 805 | } |
| 806 | if (*p == ',') { |
| 807 | p++; |
| 808 | } |
| 809 | return p; |
| 810 | } |
| 811 | |
| 812 | static bool opts_do_parse(QemuOpts *opts, const char *params, |
| 813 | const char *firstname, |
| 814 | bool warn_on_flag, bool *help_wanted, Error **errp) |
| 815 | { |
| 816 | char *option, *value; |
| 817 | const char *p; |
| 818 | QemuOpt *opt; |
| 819 | |
| 820 | for (p = params; *p;) { |
| 821 | p = get_opt_name_value(p, firstname, warn_on_flag, help_wanted, &option, &value); |
| 822 | if (help_wanted && *help_wanted) { |
| 823 | g_free(option); |
| 824 | g_free(value); |
| 825 | return false; |
| 826 | } |
| 827 | firstname = NULL; |
| 828 | |
| 829 | if (!strcmp(option, "id")) { |
| 830 | g_free(option); |
| 831 | g_free(value); |
| 832 | continue; |
| 833 | } |
| 834 | |
| 835 | opt = opt_create(opts, option, value); |
| 836 | g_free(option); |
| 837 | if (!opt_validate(opt, errp)) { |
| 838 | qemu_opt_del(opt); |
| 839 | return false; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | return true; |
| 844 | } |
| 845 | |
| 846 | static char *opts_parse_id(const char *params) |
| 847 | { |
| 848 | const char *p; |
| 849 | char *name, *value; |
| 850 | |
| 851 | for (p = params; *p;) { |
| 852 | p = get_opt_name_value(p, NULL, false, NULL, &name, &value); |
| 853 | if (!strcmp(name, "id")) { |
| 854 | g_free(name); |
| 855 | return value; |
| 856 | } |
| 857 | g_free(name); |
| 858 | g_free(value); |
| 859 | } |
| 860 | |
| 861 | return NULL; |
| 862 | } |
| 863 | |
| 864 | bool has_help_option(const char *params) |
| 865 | { |
| 866 | const char *p; |
| 867 | char *name, *value; |
| 868 | bool ret = false; |
| 869 | |
| 870 | for (p = params; *p;) { |
| 871 | p = get_opt_name_value(p, NULL, false, &ret, &name, &value); |
| 872 | g_free(name); |
| 873 | g_free(value); |
| 874 | if (ret) { |
| 875 | return true; |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | return false; |
| 880 | } |
| 881 | |
| 882 | /** |
| 883 | * Store options parsed from @params into @opts. |
| 884 | * If @firstname is non-null, the first key=value in @params may omit |
| 885 | * key=, and is treated as if key was @firstname. |
| 886 | * On error, store an error object through @errp if non-null. |
| 887 | */ |
| 888 | bool qemu_opts_do_parse(QemuOpts *opts, const char *params, |
| 889 | const char *firstname, Error **errp) |
| 890 | { |
| 891 | return opts_do_parse(opts, params, firstname, false, NULL, errp); |
| 892 | } |
| 893 | |
| 894 | static QemuOpts *opts_parse(QemuOptsList *list, const char *params, |
| 895 | bool permit_abbrev, |
| 896 | bool warn_on_flag, bool *help_wanted, Error **errp) |
| 897 | { |
| 898 | const char *firstname; |
| 899 | char *id = opts_parse_id(params); |
| 900 | QemuOpts *opts; |
| 901 | |
| 902 | assert(!permit_abbrev || list->implied_opt_name); |
| 903 | firstname = permit_abbrev ? list->implied_opt_name : NULL; |
| 904 | |
| 905 | opts = qemu_opts_create(list, id, !list->merge_lists, errp); |
| 906 | g_free(id); |
| 907 | if (opts == NULL) { |
| 908 | return NULL; |
| 909 | } |
| 910 | |
| 911 | if (!opts_do_parse(opts, params, firstname, |
| 912 | warn_on_flag, help_wanted, errp)) { |
| 913 | qemu_opts_del(opts); |
| 914 | return NULL; |
| 915 | } |
| 916 | |
| 917 | return opts; |
| 918 | } |
| 919 | |
| 920 | /** |
| 921 | * Create a QemuOpts in @list and with options parsed from @params. |
| 922 | * If @permit_abbrev, the first key=value in @params may omit key=, |
| 923 | * and is treated as if key was @list->implied_opt_name. |
| 924 | * On error, store an error object through @errp if non-null. |
| 925 | * Return the new QemuOpts on success, null pointer on error. |
| 926 | */ |
| 927 | QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, |
| 928 | bool permit_abbrev, Error **errp) |
| 929 | { |
| 930 | return opts_parse(list, params, permit_abbrev, false, NULL, errp); |
| 931 | } |
| 932 | |
| 933 | /** |
| 934 | * Create a QemuOpts in @list and with options parsed from @params. |
| 935 | * If @permit_abbrev, the first key=value in @params may omit key=, |
| 936 | * and is treated as if key was @list->implied_opt_name. |
| 937 | * Report errors with error_report_err(). This is inappropriate in |
| 938 | * QMP context. Do not use this function there! |
| 939 | * Return the new QemuOpts on success, null pointer on error. |
| 940 | */ |
| 941 | QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params, |
| 942 | bool permit_abbrev) |
| 943 | { |
| 944 | Error *err = NULL; |
| 945 | QemuOpts *opts; |
| 946 | bool help_wanted = false; |
| 947 | |
| 948 | opts = opts_parse(list, params, permit_abbrev, true, |
| 949 | opts_accepts_any(list) ? NULL : &help_wanted, |
| 950 | &err); |
| 951 | if (!opts) { |
| 952 | assert(!!err + !!help_wanted == 1); |
| 953 | if (help_wanted) { |
| 954 | qemu_opts_print_help(list, true); |
| 955 | } else { |
| 956 | error_report_err(err); |
| 957 | } |
| 958 | } |
| 959 | return opts; |
| 960 | } |
| 961 | |
| 962 | static bool qemu_opts_from_qdict_entry(QemuOpts *opts, |
| 963 | const QDictEntry *entry, |
| 964 | Error **errp) |
| 965 | { |
| 966 | const char *key = qdict_entry_key(entry); |
| 967 | QObject *obj = qdict_entry_value(entry); |
| 968 | char buf[32]; |
| 969 | g_autofree char *tmp = NULL; |
| 970 | const char *value; |
| 971 | |
| 972 | if (!strcmp(key, "id")) { |
| 973 | return true; |
| 974 | } |
| 975 | |
| 976 | switch (qobject_type(obj)) { |
| 977 | case QTYPE_QSTRING: |
| 978 | value = qstring_get_str(qobject_to(QString, obj)); |
| 979 | break; |
| 980 | case QTYPE_QNUM: |
| 981 | tmp = qnum_to_string(qobject_to(QNum, obj)); |
| 982 | value = tmp; |
| 983 | break; |
| 984 | case QTYPE_QBOOL: |
| 985 | pstrcpy(buf, sizeof(buf), |
| 986 | qbool_get_bool(qobject_to(QBool, obj)) ? "on" : "off"); |
| 987 | value = buf; |
| 988 | break; |
| 989 | default: |
| 990 | return true; |
| 991 | } |
| 992 | |
| 993 | return qemu_opt_set(opts, key, value, errp); |
| 994 | } |
| 995 | |
| 996 | /* |
| 997 | * Create QemuOpts from a QDict. |
| 998 | * Use value of key "id" as ID if it exists and is a QString. Only |
| 999 | * QStrings, QNums and QBools are copied. Entries with other types |
| 1000 | * are silently ignored. |
| 1001 | */ |
| 1002 | QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict, |
| 1003 | Error **errp) |
| 1004 | { |
| 1005 | QemuOpts *opts; |
| 1006 | const QDictEntry *entry; |
| 1007 | |
| 1008 | opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1, errp); |
| 1009 | if (!opts) { |
| 1010 | return NULL; |
| 1011 | } |
| 1012 | |
| 1013 | for (entry = qdict_first(qdict); |
| 1014 | entry; |
| 1015 | entry = qdict_next(qdict, entry)) { |
| 1016 | if (!qemu_opts_from_qdict_entry(opts, entry, errp)) { |
| 1017 | qemu_opts_del(opts); |
| 1018 | return NULL; |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | return opts; |
| 1023 | } |
| 1024 | |
| 1025 | /* |
| 1026 | * Adds all QDict entries to the QemuOpts that can be added and removes them |
| 1027 | * from the QDict. When this function returns, the QDict contains only those |
| 1028 | * entries that couldn't be added to the QemuOpts. |
| 1029 | */ |
| 1030 | bool qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp) |
| 1031 | { |
| 1032 | const QDictEntry *entry, *next; |
| 1033 | |
| 1034 | entry = qdict_first(qdict); |
| 1035 | |
| 1036 | while (entry != NULL) { |
| 1037 | next = qdict_next(qdict, entry); |
| 1038 | |
| 1039 | if (opts_accepts_any(opts->list) || |
| 1040 | find_desc_by_name(opts->list->desc, entry->key)) { |
| 1041 | if (!qemu_opts_from_qdict_entry(opts, entry, errp)) { |
| 1042 | return false; |
| 1043 | } |
| 1044 | qdict_del(qdict, entry->key); |
| 1045 | } |
| 1046 | |
| 1047 | entry = next; |
| 1048 | } |
| 1049 | |
| 1050 | return true; |
| 1051 | } |
| 1052 | |
| 1053 | /* |
| 1054 | * Convert from QemuOpts to QDict. The QDict values are of type QString. |
| 1055 | * |
| 1056 | * If @list is given, only add those options to the QDict that are contained in |
| 1057 | * the list. If @del is true, any options added to the QDict are removed from |
| 1058 | * the QemuOpts, otherwise they remain there. |
| 1059 | * |
| 1060 | * If two options in @opts have the same name, they are processed in order |
| 1061 | * so that the last one wins (consistent with the reverse iteration in |
| 1062 | * qemu_opt_find()), but all of them are deleted if @del is true. |
| 1063 | * |
| 1064 | * TODO We'll want to use types appropriate for opt->desc->type, but |
| 1065 | * this is enough for now. |
| 1066 | */ |
| 1067 | QDict *qemu_opts_to_qdict_filtered(QemuOpts *opts, QDict *qdict, |
| 1068 | QemuOptsList *list, bool del) |
| 1069 | { |
| 1070 | QemuOpt *opt, *next; |
| 1071 | |
| 1072 | if (!qdict) { |
| 1073 | qdict = qdict_new(); |
| 1074 | } |
| 1075 | if (opts->id) { |
| 1076 | qdict_put_str(qdict, "id", opts->id); |
| 1077 | } |
| 1078 | QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next) { |
| 1079 | if (list) { |
| 1080 | QemuOptDesc *desc; |
| 1081 | bool found = false; |
| 1082 | for (desc = list->desc; desc->name; desc++) { |
| 1083 | if (!strcmp(desc->name, opt->name)) { |
| 1084 | found = true; |
| 1085 | break; |
| 1086 | } |
| 1087 | } |
| 1088 | if (!found) { |
| 1089 | continue; |
| 1090 | } |
| 1091 | } |
| 1092 | qdict_put_str(qdict, opt->name, opt->str); |
| 1093 | if (del) { |
| 1094 | qemu_opt_del(opt); |
| 1095 | } |
| 1096 | } |
| 1097 | return qdict; |
| 1098 | } |
| 1099 | |
| 1100 | /* Copy all options in a QemuOpts to the given QDict. See |
| 1101 | * qemu_opts_to_qdict_filtered() for details. */ |
| 1102 | QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict) |
| 1103 | { |
| 1104 | return qemu_opts_to_qdict_filtered(opts, qdict, NULL, false); |
| 1105 | } |
| 1106 | |
| 1107 | /* Validate parsed opts against descriptions where no |
| 1108 | * descriptions were provided in the QemuOptsList. |
| 1109 | */ |
| 1110 | bool qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp) |
| 1111 | { |
| 1112 | QemuOpt *opt; |
| 1113 | |
| 1114 | assert(opts_accepts_any(opts->list)); |
| 1115 | |
| 1116 | QTAILQ_FOREACH(opt, &opts->head, next) { |
| 1117 | opt->desc = find_desc_by_name(desc, opt->name); |
| 1118 | if (!opt->desc) { |
| 1119 | error_setg(errp, "Invalid parameter '%s'", opt->name); |
| 1120 | return false; |
| 1121 | } |
| 1122 | |
| 1123 | if (!qemu_opt_parse(opt, errp)) { |
| 1124 | return false; |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | return true; |
| 1129 | } |
| 1130 | |
| 1131 | /** |
| 1132 | * For each member of @list, call @func(@opaque, member, @errp). |
| 1133 | * Call it with the current location temporarily set to the member's. |
| 1134 | * @func() may store an Error through @errp, but must return non-zero then. |
| 1135 | * When @func() returns non-zero, break the loop and return that value. |
| 1136 | * Return zero when the loop completes. |
| 1137 | */ |
| 1138 | int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, |
| 1139 | void *opaque, Error **errp) |
| 1140 | { |
| 1141 | Location loc; |
| 1142 | QemuOpts *opts, *next; |
| 1143 | int rc = 0; |
| 1144 | |
| 1145 | loc_push_none(&loc); |
| 1146 | QTAILQ_FOREACH_SAFE(opts, &list->head, next, next) { |
| 1147 | loc_restore(&opts->loc); |
| 1148 | rc = func(opaque, opts, errp); |
| 1149 | if (rc) { |
| 1150 | break; |
| 1151 | } |
| 1152 | assert(!errp || !*errp); |
| 1153 | } |
| 1154 | loc_pop(&loc); |
| 1155 | return rc; |
| 1156 | } |
| 1157 | |
| 1158 | static size_t count_opts_list(QemuOptsList *list) |
| 1159 | { |
| 1160 | QemuOptDesc *desc = NULL; |
| 1161 | size_t num_opts = 0; |
| 1162 | |
| 1163 | if (!list) { |
| 1164 | return 0; |
| 1165 | } |
| 1166 | |
| 1167 | desc = list->desc; |
| 1168 | while (desc && desc->name) { |
| 1169 | num_opts++; |
| 1170 | desc++; |
| 1171 | } |
| 1172 | |
| 1173 | return num_opts; |
| 1174 | } |
| 1175 | |
| 1176 | void qemu_opts_free(QemuOptsList *list) |
| 1177 | { |
| 1178 | g_free(list); |
| 1179 | } |
| 1180 | |
| 1181 | /* Realloc dst option list and append options from an option list (list) |
| 1182 | * to it. dst could be NULL or a malloced list. |
| 1183 | * The lifetime of dst must be shorter than the input list because the |
| 1184 | * QemuOptDesc->name, ->help, and ->def_value_str strings are shared. |
| 1185 | */ |
| 1186 | QemuOptsList *qemu_opts_append(QemuOptsList *dst, |
| 1187 | QemuOptsList *list) |
| 1188 | { |
| 1189 | size_t num_opts, num_dst_opts; |
| 1190 | QemuOptDesc *desc; |
| 1191 | bool need_init = false; |
| 1192 | bool need_head_update; |
| 1193 | |
| 1194 | if (!list) { |
| 1195 | return dst; |
| 1196 | } |
| 1197 | |
| 1198 | /* If dst is NULL, after realloc, some area of dst should be initialized |
| 1199 | * before adding options to it. |
| 1200 | */ |
| 1201 | if (!dst) { |
| 1202 | need_init = true; |
| 1203 | need_head_update = true; |
| 1204 | } else { |
| 1205 | /* Moreover, even if dst is not NULL, the realloc may move it to a |
| 1206 | * different address in which case we may get a stale tail pointer |
| 1207 | * in dst->head. */ |
| 1208 | need_head_update = QTAILQ_EMPTY(&dst->head); |
| 1209 | } |
| 1210 | |
| 1211 | num_opts = count_opts_list(dst); |
| 1212 | num_dst_opts = num_opts; |
| 1213 | num_opts += count_opts_list(list); |
| 1214 | dst = g_realloc(dst, sizeof(QemuOptsList) + |
| 1215 | (num_opts + 1) * sizeof(QemuOptDesc)); |
| 1216 | if (need_init) { |
| 1217 | dst->name = NULL; |
| 1218 | dst->implied_opt_name = NULL; |
| 1219 | dst->merge_lists = false; |
| 1220 | } |
| 1221 | if (need_head_update) { |
| 1222 | QTAILQ_INIT(&dst->head); |
| 1223 | } |
| 1224 | dst->desc[num_dst_opts].name = NULL; |
| 1225 | |
| 1226 | /* append list->desc to dst->desc */ |
| 1227 | if (list) { |
| 1228 | desc = list->desc; |
| 1229 | while (desc && desc->name) { |
| 1230 | if (find_desc_by_name(dst->desc, desc->name) == NULL) { |
| 1231 | dst->desc[num_dst_opts++] = *desc; |
| 1232 | dst->desc[num_dst_opts].name = NULL; |
| 1233 | } |
| 1234 | desc++; |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | return dst; |
| 1239 | } |