All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: Markus Armbruster <armbru@redhat.com>
Subject: [PULL 30/31] qemu-option: move help handling to get_opt_name_value
Date: Sat, 23 Jan 2021 09:31:27 -0500	[thread overview]
Message-ID: <20210123143128.1167797-31-pbonzini@redhat.com> (raw)
In-Reply-To: <20210123143128.1167797-1-pbonzini@redhat.com>

Right now, help options are parsed normally and then checked
specially in opt_validate, but only if coming from
qemu_opts_parse_noisily.  has_help_option does the check on its own.

opt_validate() has two callers: qemu_opt_set(), which passes null and is
therefore unaffected, and opts_do_parse(), which is affected.

opts_do_parse() is called by qemu_opts_do_parse(), which passes null and
is therefore unaffected, and opts_parse().

opts_parse() is called by qemu_opts_parse() and qemu_opts_set_defaults(),
which pass null and are therefore unaffected, and
qemu_opts_parse_noisily().

Move the check from opt_validate to the parsing workhorse of QemuOpts,
get_opt_name_value.  This will come in handy in the next patch, which
will raise a warning for "-object memory-backend-ram,share" ("flag" option
with no =on/=off part) but not for "-object memory-backend-ram,help".

As a result:

- opts_parse and opts_do_parse do not return an error anymore
  when help is requested; qemu_opts_parse_noisily does not have
  to work around that anymore.

- various crazy ways to request help are not recognized anymore:
  - "help=..."
  - "nohelp" (sugar for "help=off")
  - "?=..."
  - "no?" (sugar for "?=off")

- "help" would be recognized as help request even if there is a (foolishly
  named) parameter "help".  No such parameters exist, though.

Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 util/qemu-option.c | 38 +++++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 15 deletions(-)

diff --git a/util/qemu-option.c b/util/qemu-option.c
index 91f4120ce1..5f27d4369d 100644
--- a/util/qemu-option.c
+++ b/util/qemu-option.c
@@ -496,8 +496,7 @@ static QemuOpt *opt_create(QemuOpts *opts, const char *name, char *value,
     return opt;
 }
 
-static bool opt_validate(QemuOpt *opt, bool *help_wanted,
-                         Error **errp)
+static bool opt_validate(QemuOpt *opt, Error **errp)
 {
     const QemuOptDesc *desc;
     const QemuOptsList *list = opt->opts->list;
@@ -505,9 +504,6 @@ static bool opt_validate(QemuOpt *opt, bool *help_wanted,
     desc = find_desc_by_name(list->desc, opt->name);
     if (!desc && !opts_accepts_any(list)) {
         error_setg(errp, QERR_INVALID_PARAMETER, opt->name);
-        if (help_wanted && is_help_option(opt->name)) {
-            *help_wanted = true;
-        }
         return false;
     }
 
@@ -524,7 +520,7 @@ bool qemu_opt_set(QemuOpts *opts, const char *name, const char *value,
 {
     QemuOpt *opt = opt_create(opts, name, g_strdup(value), false);
 
-    if (!opt_validate(opt, NULL, errp)) {
+    if (!opt_validate(opt, errp)) {
         qemu_opt_del(opt);
         return false;
     }
@@ -760,10 +756,12 @@ void qemu_opts_print(QemuOpts *opts, const char *separator)
 
 static const char *get_opt_name_value(const char *params,
                                       const char *firstname,
+                                      bool *help_wanted,
                                       char **name, char **value)
 {
     const char *p;
     size_t len;
+    bool is_help = false;
 
     len = strcspn(params, "=,");
     if (params[len] != '=') {
@@ -780,6 +778,7 @@ static const char *get_opt_name_value(const char *params,
                 *value = g_strdup("off");
             } else {
                 *value = g_strdup("on");
+                is_help = is_help_option(*name);
             }
         }
     } else {
@@ -791,6 +790,9 @@ static const char *get_opt_name_value(const char *params,
     }
 
     assert(!*p || *p == ',');
+    if (help_wanted && is_help) {
+        *help_wanted = true;
+    }
     if (*p == ',') {
         p++;
     }
@@ -806,7 +808,12 @@ static bool opts_do_parse(QemuOpts *opts, const char *params,
     QemuOpt *opt;
 
     for (p = params; *p;) {
-        p = get_opt_name_value(p, firstname, &option, &value);
+        p = get_opt_name_value(p, firstname, help_wanted, &option, &value);
+        if (help_wanted && *help_wanted) {
+            g_free(option);
+            g_free(value);
+            return false;
+        }
         firstname = NULL;
 
         if (!strcmp(option, "id")) {
@@ -817,7 +824,7 @@ static bool opts_do_parse(QemuOpts *opts, const char *params,
 
         opt = opt_create(opts, option, value, prepend);
         g_free(option);
-        if (!opt_validate(opt, help_wanted, errp)) {
+        if (!opt_validate(opt, errp)) {
             qemu_opt_del(opt);
             return false;
         }
@@ -832,7 +839,7 @@ static char *opts_parse_id(const char *params)
     char *name, *value;
 
     for (p = params; *p;) {
-        p = get_opt_name_value(p, NULL, &name, &value);
+        p = get_opt_name_value(p, NULL, NULL, &name, &value);
         if (!strcmp(name, "id")) {
             g_free(name);
             return value;
@@ -848,11 +855,10 @@ bool has_help_option(const char *params)
 {
     const char *p;
     char *name, *value;
-    bool ret;
+    bool ret = false;
 
     for (p = params; *p;) {
-        p = get_opt_name_value(p, NULL, &name, &value);
-        ret = is_help_option(name);
+        p = get_opt_name_value(p, NULL, &ret, &name, &value);
         g_free(name);
         g_free(value);
         if (ret) {
@@ -937,11 +943,13 @@ QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params,
     QemuOpts *opts;
     bool help_wanted = false;
 
-    opts = opts_parse(list, params, permit_abbrev, false, &help_wanted, &err);
-    if (err) {
+    opts = opts_parse(list, params, permit_abbrev, false,
+                      opts_accepts_any(list) ? NULL : &help_wanted,
+                      &err);
+    if (!opts) {
+        assert(!!err + !!help_wanted == 1);
         if (help_wanted) {
             qemu_opts_print_help(list, true);
-            error_free(err);
         } else {
             error_report_err(err);
         }
-- 
2.26.2




  parent reply	other threads:[~2021-01-23 14:53 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-23 14:30 [PULL 00/31] Misc patches for 2020-01-21 Paolo Bonzini
2021-01-23 14:30 ` [PULL 01/31] runstate: cleanup reboot and panic actions Paolo Bonzini
2021-01-23 14:30 ` [PULL 02/31] configure: MinGW respect --bindir argument Paolo Bonzini
2021-01-23 14:31 ` [PULL 03/31] x86/cpu: Use max host physical address if -cpu max option is applied Paolo Bonzini
2021-01-23 14:31 ` [PULL 04/31] build-system: clean up TCG/TCI configury Paolo Bonzini
2021-02-06 18:01   ` Philippe Mathieu-Daudé
2021-02-06 19:08     ` Philippe Mathieu-Daudé
2021-01-23 14:31 ` [PULL 05/31] util/cacheflush: Fix error generated by clang Paolo Bonzini
2021-01-23 14:31 ` [PULL 06/31] softmmu/physmem: Silence GCC 10 maybe-uninitialized error Paolo Bonzini
2021-01-23 14:31 ` [PULL 07/31] ide: atapi: check logical block address and read size (CVE-2020-29443) Paolo Bonzini
2021-01-23 14:31 ` [PULL 08/31] build-sys: remove unused LIBS Paolo Bonzini
2021-01-23 14:31 ` [PULL 09/31] build-sys: set global arguments for cflags/ldflags Paolo Bonzini
2021-01-23 14:31 ` [PULL 10/31] build-sys: add libvhost-user missing dependencies Paolo Bonzini
2021-01-23 14:31 ` [PULL 11/31] slirp: update to git master Paolo Bonzini
2021-01-23 14:31 ` [PULL 12/31] meson: Declare have_virtfs_proxy_helper in main meson.build Paolo Bonzini
2021-01-23 14:31 ` [PULL 13/31] nsis: adjust for new MinGW paths Paolo Bonzini
2021-01-23 14:31 ` [PULL 14/31] meson: convert wixl detection to Meson Paolo Bonzini
2021-01-23 14:31 ` [PULL 15/31] meson: Summarize information related to directories first Paolo Bonzini
2021-01-23 14:31 ` [PULL 16/31] meson: Display host binaries information altogether Paolo Bonzini
2021-01-23 14:31 ` [PULL 17/31] meson: Summarize overall features altogether Paolo Bonzini
2021-01-23 14:31 ` [PULL 18/31] meson: Summarize compilation-related information altogether Paolo Bonzini
2021-01-23 14:31 ` [PULL 19/31] meson: Display accelerators and selected targets altogether Paolo Bonzini
2021-01-23 14:31 ` [PULL 20/31] meson: Display block layer information altogether Paolo Bonzini
2021-01-23 14:31 ` [PULL 21/31] meson: Display crypto-related " Paolo Bonzini
2021-01-23 14:31 ` [PULL 22/31] meson: Add a section header for library dependencies Paolo Bonzini
2021-01-23 14:31 ` [PULL 23/31] meson.build: Declare global edk2_targets / install_edk2_blobs variables Paolo Bonzini
2021-01-23 14:31 ` [PULL 24/31] meson.build: Detect bzip2 program Paolo Bonzini
2021-01-23 14:31 ` [PULL 25/31] acceptance: switch to QMP change-vnc-password command Paolo Bonzini
2021-01-23 14:31 ` [PULL 26/31] hmp: remove "change vnc TARGET" command Paolo Bonzini
2021-01-23 14:31 ` [PULL 27/31] qmp: remove deprecated "change" command Paolo Bonzini
2021-01-23 14:31 ` [PULL 28/31] vnc: support "-vnc help" Paolo Bonzini
2021-01-23 14:31 ` [PULL 29/31] qemu-option: clean up id vs. list->merge_lists Paolo Bonzini
2021-01-25  7:42   ` Markus Armbruster
2021-01-25  7:58     ` Paolo Bonzini
2021-01-23 14:31 ` Paolo Bonzini [this message]
2021-01-23 14:31 ` [PULL 31/31] qemu-option: warn for short-form boolean options Paolo Bonzini
2021-02-15 19:56   ` Peter Maydell
2021-02-15 23:14     ` Paolo Bonzini
2021-02-16  9:58       ` Peter Maydell
2021-02-16 10:43         ` Paolo Bonzini
2021-02-16 11:04           ` Peter Maydell
2021-02-16 11:23             ` Paolo Bonzini
2021-02-16 11:58               ` Peter Maydell
2021-02-16 13:30                 ` Paolo Bonzini
2021-02-16 13:36                   ` Peter Maydell
2021-02-16 13:43                     ` Paolo Bonzini
2021-02-16 14:11                       ` Peter Maydell
2021-02-16 14:45                         ` Paolo Bonzini
2021-02-16 14:51                           ` Peter Maydell
2021-02-16 14:58                             ` Paolo Bonzini
2021-02-16 13:53                     ` Daniel P. Berrangé
2021-01-23 14:57 ` [PULL 00/31] Misc patches for 2020-01-21 no-reply
2021-01-23 19:52 ` Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210123143128.1167797-31-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=armbru@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.