1 /* 2 * Commandline option parsing functions 3 * 4 * Copyright (c) 2003-2008 Fabrice Bellard 5 * Copyright (c) 2009 Kevin Wolf <kwolf@redhat.com> 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated documentation files (the "Software"), to deal 9 * in the Software without restriction, including without limitation the rights 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 * copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 * THE SOFTWARE. 24 */ 25 26 #include <stdio.h> 27 #include <string.h> 28 29 #include "qemu-common.h" 30 #include "qemu/error-report.h" 31 #include "qapi/qmp/types.h" 32 #include "qapi/error.h" 33 #include "qapi/qmp/qerror.h" 34 #include "qemu/option_int.h" 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 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 static void parse_option_bool(const char *name, const char *value, bool *ret, 127 Error **errp) 128 { 129 if (value != NULL) { 130 if (!strcmp(value, "on")) { 131 *ret = 1; 132 } else if (!strcmp(value, "off")) { 133 *ret = 0; 134 } else { 135 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 136 name, "'on' or 'off'"); 137 } 138 } else { 139 *ret = 1; 140 } 141 } 142 143 static void parse_option_number(const char *name, const char *value, 144 uint64_t *ret, Error **errp) 145 { 146 char *postfix; 147 uint64_t number; 148 149 if (value != NULL) { 150 number = strtoull(value, &postfix, 0); 151 if (*postfix != '\0') { 152 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number"); 153 return; 154 } 155 *ret = number; 156 } else { 157 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number"); 158 } 159 } 160 161 static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc, 162 const char *name) 163 { 164 int i; 165 166 for (i = 0; desc[i].name != NULL; i++) { 167 if (strcmp(desc[i].name, name) == 0) { 168 return &desc[i]; 169 } 170 } 171 172 return NULL; 173 } 174 175 void parse_option_size(const char *name, const char *value, 176 uint64_t *ret, Error **errp) 177 { 178 char *postfix; 179 double sizef; 180 181 if (value != NULL) { 182 sizef = strtod(value, &postfix); 183 if (sizef < 0 || sizef > UINT64_MAX) { 184 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, 185 "a non-negative number below 2^64"); 186 return; 187 } 188 switch (*postfix) { 189 case 'T': 190 sizef *= 1024; 191 /* fall through */ 192 case 'G': 193 sizef *= 1024; 194 /* fall through */ 195 case 'M': 196 sizef *= 1024; 197 /* fall through */ 198 case 'K': 199 case 'k': 200 sizef *= 1024; 201 /* fall through */ 202 case 'b': 203 case '\0': 204 *ret = (uint64_t) sizef; 205 break; 206 default: 207 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size"); 208 #if 0 /* conversion from qerror_report() to error_set() broke this: */ 209 error_printf_unless_qmp("You may use k, M, G or T suffixes for " 210 "kilobytes, megabytes, gigabytes and terabytes.\n"); 211 #endif 212 return; 213 } 214 } else { 215 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size"); 216 } 217 } 218 219 bool has_help_option(const char *param) 220 { 221 size_t buflen = strlen(param) + 1; 222 char *buf = g_malloc(buflen); 223 const char *p = param; 224 bool result = false; 225 226 while (*p) { 227 p = get_opt_value(buf, buflen, p); 228 if (*p) { 229 p++; 230 } 231 232 if (is_help_option(buf)) { 233 result = true; 234 goto out; 235 } 236 } 237 238 out: 239 g_free(buf); 240 return result; 241 } 242 243 bool is_valid_option_list(const char *param) 244 { 245 size_t buflen = strlen(param) + 1; 246 char *buf = g_malloc(buflen); 247 const char *p = param; 248 bool result = true; 249 250 while (*p) { 251 p = get_opt_value(buf, buflen, p); 252 if (*p && !*++p) { 253 result = false; 254 goto out; 255 } 256 257 if (!*buf || *buf == ',') { 258 result = false; 259 goto out; 260 } 261 } 262 263 out: 264 g_free(buf); 265 return result; 266 } 267 268 void qemu_opts_print_help(QemuOptsList *list) 269 { 270 QemuOptDesc *desc; 271 272 assert(list); 273 desc = list->desc; 274 printf("Supported options:\n"); 275 while (desc && desc->name) { 276 printf("%-16s %s\n", desc->name, 277 desc->help ? desc->help : "No description available"); 278 desc++; 279 } 280 } 281 /* ------------------------------------------------------------------ */ 282 283 QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name) 284 { 285 QemuOpt *opt; 286 287 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) { 288 if (strcmp(opt->name, name) != 0) 289 continue; 290 return opt; 291 } 292 return NULL; 293 } 294 295 static void qemu_opt_del(QemuOpt *opt) 296 { 297 QTAILQ_REMOVE(&opt->opts->head, opt, next); 298 g_free(opt->name); 299 g_free(opt->str); 300 g_free(opt); 301 } 302 303 /* qemu_opt_set allows many settings for the same option. 304 * This function deletes all settings for an option. 305 */ 306 static void qemu_opt_del_all(QemuOpts *opts, const char *name) 307 { 308 QemuOpt *opt, *next_opt; 309 310 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next_opt) { 311 if (!strcmp(opt->name, name)) { 312 qemu_opt_del(opt); 313 } 314 } 315 } 316 317 const char *qemu_opt_get(QemuOpts *opts, const char *name) 318 { 319 QemuOpt *opt; 320 321 if (opts == NULL) { 322 return NULL; 323 } 324 325 opt = qemu_opt_find(opts, name); 326 if (!opt) { 327 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name); 328 if (desc && desc->def_value_str) { 329 return desc->def_value_str; 330 } 331 } 332 return opt ? opt->str : NULL; 333 } 334 335 /* Get a known option (or its default) and remove it from the list 336 * all in one action. Return a malloced string of the option value. 337 * Result must be freed by caller with g_free(). 338 */ 339 char *qemu_opt_get_del(QemuOpts *opts, const char *name) 340 { 341 QemuOpt *opt; 342 const QemuOptDesc *desc; 343 char *str = NULL; 344 345 if (opts == NULL) { 346 return NULL; 347 } 348 349 opt = qemu_opt_find(opts, name); 350 if (!opt) { 351 desc = find_desc_by_name(opts->list->desc, name); 352 if (desc && desc->def_value_str) { 353 str = g_strdup(desc->def_value_str); 354 } 355 return str; 356 } 357 str = opt->str; 358 opt->str = NULL; 359 qemu_opt_del_all(opts, name); 360 return str; 361 } 362 363 bool qemu_opt_has_help_opt(QemuOpts *opts) 364 { 365 QemuOpt *opt; 366 367 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) { 368 if (is_help_option(opt->name)) { 369 return true; 370 } 371 } 372 return false; 373 } 374 375 static bool qemu_opt_get_bool_helper(QemuOpts *opts, const char *name, 376 bool defval, bool del) 377 { 378 QemuOpt *opt; 379 bool ret = defval; 380 381 if (opts == NULL) { 382 return ret; 383 } 384 385 opt = qemu_opt_find(opts, name); 386 if (opt == NULL) { 387 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name); 388 if (desc && desc->def_value_str) { 389 parse_option_bool(name, desc->def_value_str, &ret, &error_abort); 390 } 391 return ret; 392 } 393 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL); 394 ret = opt->value.boolean; 395 if (del) { 396 qemu_opt_del_all(opts, name); 397 } 398 return ret; 399 } 400 401 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval) 402 { 403 return qemu_opt_get_bool_helper(opts, name, defval, false); 404 } 405 406 bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval) 407 { 408 return qemu_opt_get_bool_helper(opts, name, defval, true); 409 } 410 411 static uint64_t qemu_opt_get_number_helper(QemuOpts *opts, const char *name, 412 uint64_t defval, bool del) 413 { 414 QemuOpt *opt; 415 uint64_t ret = defval; 416 417 if (opts == NULL) { 418 return ret; 419 } 420 421 opt = qemu_opt_find(opts, name); 422 if (opt == NULL) { 423 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name); 424 if (desc && desc->def_value_str) { 425 parse_option_number(name, desc->def_value_str, &ret, &error_abort); 426 } 427 return ret; 428 } 429 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER); 430 ret = opt->value.uint; 431 if (del) { 432 qemu_opt_del_all(opts, name); 433 } 434 return ret; 435 } 436 437 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval) 438 { 439 return qemu_opt_get_number_helper(opts, name, defval, false); 440 } 441 442 uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name, 443 uint64_t defval) 444 { 445 return qemu_opt_get_number_helper(opts, name, defval, true); 446 } 447 448 static uint64_t qemu_opt_get_size_helper(QemuOpts *opts, const char *name, 449 uint64_t defval, bool del) 450 { 451 QemuOpt *opt; 452 uint64_t ret = defval; 453 454 if (opts == NULL) { 455 return ret; 456 } 457 458 opt = qemu_opt_find(opts, name); 459 if (opt == NULL) { 460 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name); 461 if (desc && desc->def_value_str) { 462 parse_option_size(name, desc->def_value_str, &ret, &error_abort); 463 } 464 return ret; 465 } 466 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE); 467 ret = opt->value.uint; 468 if (del) { 469 qemu_opt_del_all(opts, name); 470 } 471 return ret; 472 } 473 474 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval) 475 { 476 return qemu_opt_get_size_helper(opts, name, defval, false); 477 } 478 479 uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name, 480 uint64_t defval) 481 { 482 return qemu_opt_get_size_helper(opts, name, defval, true); 483 } 484 485 static void qemu_opt_parse(QemuOpt *opt, Error **errp) 486 { 487 if (opt->desc == NULL) 488 return; 489 490 switch (opt->desc->type) { 491 case QEMU_OPT_STRING: 492 /* nothing */ 493 return; 494 case QEMU_OPT_BOOL: 495 parse_option_bool(opt->name, opt->str, &opt->value.boolean, errp); 496 break; 497 case QEMU_OPT_NUMBER: 498 parse_option_number(opt->name, opt->str, &opt->value.uint, errp); 499 break; 500 case QEMU_OPT_SIZE: 501 parse_option_size(opt->name, opt->str, &opt->value.uint, errp); 502 break; 503 default: 504 abort(); 505 } 506 } 507 508 static bool opts_accepts_any(const QemuOpts *opts) 509 { 510 return opts->list->desc[0].name == NULL; 511 } 512 513 int qemu_opt_unset(QemuOpts *opts, const char *name) 514 { 515 QemuOpt *opt = qemu_opt_find(opts, name); 516 517 assert(opts_accepts_any(opts)); 518 519 if (opt == NULL) { 520 return -1; 521 } else { 522 qemu_opt_del(opt); 523 return 0; 524 } 525 } 526 527 static void opt_set(QemuOpts *opts, const char *name, const char *value, 528 bool prepend, Error **errp) 529 { 530 QemuOpt *opt; 531 const QemuOptDesc *desc; 532 Error *local_err = NULL; 533 534 desc = find_desc_by_name(opts->list->desc, name); 535 if (!desc && !opts_accepts_any(opts)) { 536 error_setg(errp, QERR_INVALID_PARAMETER, name); 537 return; 538 } 539 540 opt = g_malloc0(sizeof(*opt)); 541 opt->name = g_strdup(name); 542 opt->opts = opts; 543 if (prepend) { 544 QTAILQ_INSERT_HEAD(&opts->head, opt, next); 545 } else { 546 QTAILQ_INSERT_TAIL(&opts->head, opt, next); 547 } 548 opt->desc = desc; 549 opt->str = g_strdup(value); 550 qemu_opt_parse(opt, &local_err); 551 if (local_err) { 552 error_propagate(errp, local_err); 553 qemu_opt_del(opt); 554 } 555 } 556 557 void qemu_opt_set(QemuOpts *opts, const char *name, const char *value, 558 Error **errp) 559 { 560 opt_set(opts, name, value, false, errp); 561 } 562 563 void qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val, 564 Error **errp) 565 { 566 QemuOpt *opt; 567 const QemuOptDesc *desc = opts->list->desc; 568 569 opt = g_malloc0(sizeof(*opt)); 570 opt->desc = find_desc_by_name(desc, name); 571 if (!opt->desc && !opts_accepts_any(opts)) { 572 error_setg(errp, QERR_INVALID_PARAMETER, name); 573 g_free(opt); 574 return; 575 } 576 577 opt->name = g_strdup(name); 578 opt->opts = opts; 579 opt->value.boolean = !!val; 580 opt->str = g_strdup(val ? "on" : "off"); 581 QTAILQ_INSERT_TAIL(&opts->head, opt, next); 582 } 583 584 void qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val, 585 Error **errp) 586 { 587 QemuOpt *opt; 588 const QemuOptDesc *desc = opts->list->desc; 589 590 opt = g_malloc0(sizeof(*opt)); 591 opt->desc = find_desc_by_name(desc, name); 592 if (!opt->desc && !opts_accepts_any(opts)) { 593 error_setg(errp, QERR_INVALID_PARAMETER, name); 594 g_free(opt); 595 return; 596 } 597 598 opt->name = g_strdup(name); 599 opt->opts = opts; 600 opt->value.uint = val; 601 opt->str = g_strdup_printf("%" PRId64, val); 602 QTAILQ_INSERT_TAIL(&opts->head, opt, next); 603 } 604 605 /** 606 * For each member of @opts, call @func(@opaque, name, value, @errp). 607 * @func() may store an Error through @errp, but must return non-zero then. 608 * When @func() returns non-zero, break the loop and return that value. 609 * Return zero when the loop completes. 610 */ 611 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque, 612 Error **errp) 613 { 614 QemuOpt *opt; 615 int rc; 616 617 QTAILQ_FOREACH(opt, &opts->head, next) { 618 rc = func(opaque, opt->name, opt->str, errp); 619 if (rc) { 620 return rc; 621 } 622 assert(!errp || !*errp); 623 } 624 return 0; 625 } 626 627 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id) 628 { 629 QemuOpts *opts; 630 631 QTAILQ_FOREACH(opts, &list->head, next) { 632 if (!opts->id && !id) { 633 return opts; 634 } 635 if (opts->id && id && !strcmp(opts->id, id)) { 636 return opts; 637 } 638 } 639 return NULL; 640 } 641 642 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, 643 int fail_if_exists, Error **errp) 644 { 645 QemuOpts *opts = NULL; 646 647 if (id) { 648 if (!id_wellformed(id)) { 649 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id", 650 "an identifier"); 651 #if 0 /* conversion from qerror_report() to error_set() broke this: */ 652 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n"); 653 #endif 654 return NULL; 655 } 656 opts = qemu_opts_find(list, id); 657 if (opts != NULL) { 658 if (fail_if_exists && !list->merge_lists) { 659 error_setg(errp, "Duplicate ID '%s' for %s", id, list->name); 660 return NULL; 661 } else { 662 return opts; 663 } 664 } 665 } else if (list->merge_lists) { 666 opts = qemu_opts_find(list, NULL); 667 if (opts) { 668 return opts; 669 } 670 } 671 opts = g_malloc0(sizeof(*opts)); 672 opts->id = g_strdup(id); 673 opts->list = list; 674 loc_save(&opts->loc); 675 QTAILQ_INIT(&opts->head); 676 QTAILQ_INSERT_TAIL(&list->head, opts, next); 677 return opts; 678 } 679 680 void qemu_opts_reset(QemuOptsList *list) 681 { 682 QemuOpts *opts, *next_opts; 683 684 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) { 685 qemu_opts_del(opts); 686 } 687 } 688 689 void qemu_opts_loc_restore(QemuOpts *opts) 690 { 691 loc_restore(&opts->loc); 692 } 693 694 void qemu_opts_set(QemuOptsList *list, const char *id, 695 const char *name, const char *value, Error **errp) 696 { 697 QemuOpts *opts; 698 Error *local_err = NULL; 699 700 opts = qemu_opts_create(list, id, 1, &local_err); 701 if (local_err) { 702 error_propagate(errp, local_err); 703 return; 704 } 705 qemu_opt_set(opts, name, value, errp); 706 } 707 708 const char *qemu_opts_id(QemuOpts *opts) 709 { 710 return opts->id; 711 } 712 713 /* The id string will be g_free()d by qemu_opts_del */ 714 void qemu_opts_set_id(QemuOpts *opts, char *id) 715 { 716 opts->id = id; 717 } 718 719 void qemu_opts_del(QemuOpts *opts) 720 { 721 QemuOpt *opt; 722 723 if (opts == NULL) { 724 return; 725 } 726 727 for (;;) { 728 opt = QTAILQ_FIRST(&opts->head); 729 if (opt == NULL) 730 break; 731 qemu_opt_del(opt); 732 } 733 QTAILQ_REMOVE(&opts->list->head, opts, next); 734 g_free(opts->id); 735 g_free(opts); 736 } 737 738 /* print value, escaping any commas in value */ 739 static void escaped_print(const char *value) 740 { 741 const char *ptr; 742 743 for (ptr = value; *ptr; ++ptr) { 744 if (*ptr == ',') { 745 putchar(','); 746 } 747 putchar(*ptr); 748 } 749 } 750 751 void qemu_opts_print(QemuOpts *opts, const char *separator) 752 { 753 QemuOpt *opt; 754 QemuOptDesc *desc = opts->list->desc; 755 const char *sep = ""; 756 757 if (opts->id) { 758 printf("id=%s", opts->id); /* passed id_wellformed -> no commas */ 759 sep = separator; 760 } 761 762 if (desc[0].name == NULL) { 763 QTAILQ_FOREACH(opt, &opts->head, next) { 764 printf("%s%s=", sep, opt->name); 765 escaped_print(opt->str); 766 sep = separator; 767 } 768 return; 769 } 770 for (; desc && desc->name; desc++) { 771 const char *value; 772 QemuOpt *opt = qemu_opt_find(opts, desc->name); 773 774 value = opt ? opt->str : desc->def_value_str; 775 if (!value) { 776 continue; 777 } 778 if (desc->type == QEMU_OPT_STRING) { 779 printf("%s%s=", sep, desc->name); 780 escaped_print(value); 781 } else if ((desc->type == QEMU_OPT_SIZE || 782 desc->type == QEMU_OPT_NUMBER) && opt) { 783 printf("%s%s=%" PRId64, sep, desc->name, opt->value.uint); 784 } else { 785 printf("%s%s=%s", sep, desc->name, value); 786 } 787 sep = separator; 788 } 789 } 790 791 static void opts_do_parse(QemuOpts *opts, const char *params, 792 const char *firstname, bool prepend, Error **errp) 793 { 794 char option[128], value[1024]; 795 const char *p,*pe,*pc; 796 Error *local_err = NULL; 797 798 for (p = params; *p != '\0'; p++) { 799 pe = strchr(p, '='); 800 pc = strchr(p, ','); 801 if (!pe || (pc && pc < pe)) { 802 /* found "foo,more" */ 803 if (p == params && firstname) { 804 /* implicitly named first option */ 805 pstrcpy(option, sizeof(option), firstname); 806 p = get_opt_value(value, sizeof(value), p); 807 } else { 808 /* option without value, probably a flag */ 809 p = get_opt_name(option, sizeof(option), p, ','); 810 if (strncmp(option, "no", 2) == 0) { 811 memmove(option, option+2, strlen(option+2)+1); 812 pstrcpy(value, sizeof(value), "off"); 813 } else { 814 pstrcpy(value, sizeof(value), "on"); 815 } 816 } 817 } else { 818 /* found "foo=bar,more" */ 819 p = get_opt_name(option, sizeof(option), p, '='); 820 if (*p != '=') { 821 break; 822 } 823 p++; 824 p = get_opt_value(value, sizeof(value), p); 825 } 826 if (strcmp(option, "id") != 0) { 827 /* store and parse */ 828 opt_set(opts, option, value, prepend, &local_err); 829 if (local_err) { 830 error_propagate(errp, local_err); 831 return; 832 } 833 } 834 if (*p != ',') { 835 break; 836 } 837 } 838 } 839 840 /** 841 * Store options parsed from @params into @opts. 842 * If @firstname is non-null, the first key=value in @params may omit 843 * key=, and is treated as if key was @firstname. 844 * On error, store an error object through @errp if non-null. 845 */ 846 void qemu_opts_do_parse(QemuOpts *opts, const char *params, 847 const char *firstname, Error **errp) 848 { 849 opts_do_parse(opts, params, firstname, false, errp); 850 } 851 852 static QemuOpts *opts_parse(QemuOptsList *list, const char *params, 853 bool permit_abbrev, bool defaults, Error **errp) 854 { 855 const char *firstname; 856 char value[1024], *id = NULL; 857 const char *p; 858 QemuOpts *opts; 859 Error *local_err = NULL; 860 861 assert(!permit_abbrev || list->implied_opt_name); 862 firstname = permit_abbrev ? list->implied_opt_name : NULL; 863 864 if (strncmp(params, "id=", 3) == 0) { 865 get_opt_value(value, sizeof(value), params+3); 866 id = value; 867 } else if ((p = strstr(params, ",id=")) != NULL) { 868 get_opt_value(value, sizeof(value), p+4); 869 id = value; 870 } 871 872 /* 873 * This code doesn't work for defaults && !list->merge_lists: when 874 * params has no id=, and list has an element with !opts->id, it 875 * appends a new element instead of returning the existing opts. 876 * However, we got no use for this case. Guard against possible 877 * (if unlikely) future misuse: 878 */ 879 assert(!defaults || list->merge_lists); 880 opts = qemu_opts_create(list, id, !defaults, &local_err); 881 if (opts == NULL) { 882 error_propagate(errp, local_err); 883 return NULL; 884 } 885 886 opts_do_parse(opts, params, firstname, defaults, &local_err); 887 if (local_err) { 888 error_propagate(errp, local_err); 889 qemu_opts_del(opts); 890 return NULL; 891 } 892 893 return opts; 894 } 895 896 /** 897 * Create a QemuOpts in @list and with options parsed from @params. 898 * If @permit_abbrev, the first key=value in @params may omit key=, 899 * and is treated as if key was @list->implied_opt_name. 900 * On error, store an error object through @errp if non-null. 901 * Return the new QemuOpts on success, null pointer on error. 902 */ 903 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, 904 bool permit_abbrev, Error **errp) 905 { 906 return opts_parse(list, params, permit_abbrev, false, errp); 907 } 908 909 /** 910 * Create a QemuOpts in @list and with options parsed from @params. 911 * If @permit_abbrev, the first key=value in @params may omit key=, 912 * and is treated as if key was @list->implied_opt_name. 913 * Report errors with error_report_err(). This is inappropriate in 914 * QMP context. Do not use this function there! 915 * Return the new QemuOpts on success, null pointer on error. 916 */ 917 QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params, 918 bool permit_abbrev) 919 { 920 Error *err = NULL; 921 QemuOpts *opts; 922 923 opts = opts_parse(list, params, permit_abbrev, false, &err); 924 if (err) { 925 error_report_err(err); 926 } 927 return opts; 928 } 929 930 void qemu_opts_set_defaults(QemuOptsList *list, const char *params, 931 int permit_abbrev) 932 { 933 QemuOpts *opts; 934 935 opts = opts_parse(list, params, permit_abbrev, true, NULL); 936 assert(opts); 937 } 938 939 typedef struct OptsFromQDictState { 940 QemuOpts *opts; 941 Error **errp; 942 } OptsFromQDictState; 943 944 static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque) 945 { 946 OptsFromQDictState *state = opaque; 947 char buf[32]; 948 const char *value; 949 int n; 950 951 if (!strcmp(key, "id") || *state->errp) { 952 return; 953 } 954 955 switch (qobject_type(obj)) { 956 case QTYPE_QSTRING: 957 value = qstring_get_str(qobject_to_qstring(obj)); 958 break; 959 case QTYPE_QINT: 960 n = snprintf(buf, sizeof(buf), "%" PRId64, 961 qint_get_int(qobject_to_qint(obj))); 962 assert(n < sizeof(buf)); 963 value = buf; 964 break; 965 case QTYPE_QFLOAT: 966 n = snprintf(buf, sizeof(buf), "%.17g", 967 qfloat_get_double(qobject_to_qfloat(obj))); 968 assert(n < sizeof(buf)); 969 value = buf; 970 break; 971 case QTYPE_QBOOL: 972 pstrcpy(buf, sizeof(buf), 973 qbool_get_bool(qobject_to_qbool(obj)) ? "on" : "off"); 974 value = buf; 975 break; 976 default: 977 return; 978 } 979 980 qemu_opt_set(state->opts, key, value, state->errp); 981 } 982 983 /* 984 * Create QemuOpts from a QDict. 985 * Use value of key "id" as ID if it exists and is a QString. 986 * Only QStrings, QInts, QFloats and QBools are copied. Entries with 987 * other types are silently ignored. 988 */ 989 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict, 990 Error **errp) 991 { 992 OptsFromQDictState state; 993 Error *local_err = NULL; 994 QemuOpts *opts; 995 996 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1, 997 &local_err); 998 if (local_err) { 999 error_propagate(errp, local_err); 1000 return NULL; 1001 } 1002 1003 assert(opts != NULL); 1004 1005 state.errp = &local_err; 1006 state.opts = opts; 1007 qdict_iter(qdict, qemu_opts_from_qdict_1, &state); 1008 if (local_err) { 1009 error_propagate(errp, local_err); 1010 qemu_opts_del(opts); 1011 return NULL; 1012 } 1013 1014 return opts; 1015 } 1016 1017 /* 1018 * Adds all QDict entries to the QemuOpts that can be added and removes them 1019 * from the QDict. When this function returns, the QDict contains only those 1020 * entries that couldn't be added to the QemuOpts. 1021 */ 1022 void qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp) 1023 { 1024 const QDictEntry *entry, *next; 1025 1026 entry = qdict_first(qdict); 1027 1028 while (entry != NULL) { 1029 Error *local_err = NULL; 1030 OptsFromQDictState state = { 1031 .errp = &local_err, 1032 .opts = opts, 1033 }; 1034 1035 next = qdict_next(qdict, entry); 1036 1037 if (find_desc_by_name(opts->list->desc, entry->key)) { 1038 qemu_opts_from_qdict_1(entry->key, entry->value, &state); 1039 if (local_err) { 1040 error_propagate(errp, local_err); 1041 return; 1042 } else { 1043 qdict_del(qdict, entry->key); 1044 } 1045 } 1046 1047 entry = next; 1048 } 1049 } 1050 1051 /* 1052 * Convert from QemuOpts to QDict. 1053 * The QDict values are of type QString. 1054 * TODO We'll want to use types appropriate for opt->desc->type, but 1055 * this is enough for now. 1056 */ 1057 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict) 1058 { 1059 QemuOpt *opt; 1060 QObject *val; 1061 1062 if (!qdict) { 1063 qdict = qdict_new(); 1064 } 1065 if (opts->id) { 1066 qdict_put(qdict, "id", qstring_from_str(opts->id)); 1067 } 1068 QTAILQ_FOREACH(opt, &opts->head, next) { 1069 val = QOBJECT(qstring_from_str(opt->str)); 1070 qdict_put_obj(qdict, opt->name, val); 1071 } 1072 return qdict; 1073 } 1074 1075 /* Validate parsed opts against descriptions where no 1076 * descriptions were provided in the QemuOptsList. 1077 */ 1078 void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp) 1079 { 1080 QemuOpt *opt; 1081 Error *local_err = NULL; 1082 1083 assert(opts_accepts_any(opts)); 1084 1085 QTAILQ_FOREACH(opt, &opts->head, next) { 1086 opt->desc = find_desc_by_name(desc, opt->name); 1087 if (!opt->desc) { 1088 error_setg(errp, QERR_INVALID_PARAMETER, opt->name); 1089 return; 1090 } 1091 1092 qemu_opt_parse(opt, &local_err); 1093 if (local_err) { 1094 error_propagate(errp, local_err); 1095 return; 1096 } 1097 } 1098 } 1099 1100 /** 1101 * For each member of @list, call @func(@opaque, member, @errp). 1102 * Call it with the current location temporarily set to the member's. 1103 * @func() may store an Error through @errp, but must return non-zero then. 1104 * When @func() returns non-zero, break the loop and return that value. 1105 * Return zero when the loop completes. 1106 */ 1107 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, 1108 void *opaque, Error **errp) 1109 { 1110 Location loc; 1111 QemuOpts *opts; 1112 int rc; 1113 1114 loc_push_none(&loc); 1115 QTAILQ_FOREACH(opts, &list->head, next) { 1116 loc_restore(&opts->loc); 1117 rc = func(opaque, opts, errp); 1118 if (rc) { 1119 return rc; 1120 } 1121 assert(!errp || !*errp); 1122 } 1123 loc_pop(&loc); 1124 return 0; 1125 } 1126 1127 static size_t count_opts_list(QemuOptsList *list) 1128 { 1129 QemuOptDesc *desc = NULL; 1130 size_t num_opts = 0; 1131 1132 if (!list) { 1133 return 0; 1134 } 1135 1136 desc = list->desc; 1137 while (desc && desc->name) { 1138 num_opts++; 1139 desc++; 1140 } 1141 1142 return num_opts; 1143 } 1144 1145 void qemu_opts_free(QemuOptsList *list) 1146 { 1147 g_free(list); 1148 } 1149 1150 /* Realloc dst option list and append options from an option list (list) 1151 * to it. dst could be NULL or a malloced list. 1152 * The lifetime of dst must be shorter than the input list because the 1153 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared. 1154 */ 1155 QemuOptsList *qemu_opts_append(QemuOptsList *dst, 1156 QemuOptsList *list) 1157 { 1158 size_t num_opts, num_dst_opts; 1159 QemuOptDesc *desc; 1160 bool need_init = false; 1161 bool need_head_update; 1162 1163 if (!list) { 1164 return dst; 1165 } 1166 1167 /* If dst is NULL, after realloc, some area of dst should be initialized 1168 * before adding options to it. 1169 */ 1170 if (!dst) { 1171 need_init = true; 1172 need_head_update = true; 1173 } else { 1174 /* Moreover, even if dst is not NULL, the realloc may move it to a 1175 * different address in which case we may get a stale tail pointer 1176 * in dst->head. */ 1177 need_head_update = QTAILQ_EMPTY(&dst->head); 1178 } 1179 1180 num_opts = count_opts_list(dst); 1181 num_dst_opts = num_opts; 1182 num_opts += count_opts_list(list); 1183 dst = g_realloc(dst, sizeof(QemuOptsList) + 1184 (num_opts + 1) * sizeof(QemuOptDesc)); 1185 if (need_init) { 1186 dst->name = NULL; 1187 dst->implied_opt_name = NULL; 1188 dst->merge_lists = false; 1189 } 1190 if (need_head_update) { 1191 QTAILQ_INIT(&dst->head); 1192 } 1193 dst->desc[num_dst_opts].name = NULL; 1194 1195 /* append list->desc to dst->desc */ 1196 if (list) { 1197 desc = list->desc; 1198 while (desc && desc->name) { 1199 if (find_desc_by_name(dst->desc, desc->name) == NULL) { 1200 dst->desc[num_dst_opts++] = *desc; 1201 dst->desc[num_dst_opts].name = NULL; 1202 } 1203 desc++; 1204 } 1205 } 1206 1207 return dst; 1208 } 1209