qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, armbru@redhat.com, dgilbert@redhat.com,
	pbonzini@redhat.com, marcandre.lureau@redhat.com
Subject: [PATCH 10/13] char: Implement qemu_chr_new_from_opts() in terms of QAPI
Date: Thu, 12 Nov 2020 18:59:02 +0100	[thread overview]
Message-ID: <20201112175905.404472-11-kwolf@redhat.com> (raw)
In-Reply-To: <20201112175905.404472-1-kwolf@redhat.com>

Instead of having a second parser, qemu_chr_new_from_opts() uses
qemu_chr_translate_legacy_options() and qemu_chr_new_cli() now.

This switches -chardev of the system emulator to use the QAPI generated
parser rather than the hand-written QemuOpts based parser. All existing
command line options should keep working, but it gains support for
anything that was previously only supported in QMP (e.g. vsock socket
addresses).

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 chardev/char.c | 116 +++++++++++++++++++------------------------------
 1 file changed, 45 insertions(+), 71 deletions(-)

diff --git a/chardev/char.c b/chardev/char.c
index 3bb6a743f7..4a444a0353 100644
--- a/chardev/char.c
+++ b/chardev/char.c
@@ -656,70 +656,6 @@ void qemu_chr_print_types(void)
     qemu_printf("Available chardev backend types: %s\n", str->str);
 }
 
-Chardev *qemu_chr_new_from_opts(QemuOpts *opts, GMainContext *context,
-                                Error **errp)
-{
-    const ChardevClass *cc;
-    Chardev *chr = NULL;
-    ChardevBackend *backend = NULL;
-    const char *name = chardev_alias_translate(qemu_opt_get(opts, "backend"));
-    const char *id = qemu_opts_id(opts);
-    char *bid = NULL;
-
-    if (name && is_help_option(name)) {
-        qemu_chr_print_types();
-        return NULL;
-    }
-
-    if (id == NULL) {
-        error_setg(errp, "chardev: no id specified");
-        return NULL;
-    }
-
-    backend = qemu_chr_parse_opts(opts, errp);
-    if (backend == NULL) {
-        return NULL;
-    }
-
-    cc = char_get_class(name, errp);
-    if (cc == NULL) {
-        goto out;
-    }
-
-    if (qemu_opt_get_bool(opts, "mux", 0)) {
-        bid = g_strdup_printf("%s-base", id);
-    }
-
-    chr = qemu_chardev_new(bid ? bid : id,
-                           object_class_get_name(OBJECT_CLASS(cc)),
-                           backend, context, errp);
-
-    if (chr == NULL) {
-        goto out;
-    }
-
-    if (bid) {
-        Chardev *mux;
-        qapi_free_ChardevBackend(backend);
-        backend = g_new0(ChardevBackend, 1);
-        backend->type = CHARDEV_BACKEND_KIND_MUX;
-        backend->u.mux.data = g_new0(ChardevMux, 1);
-        backend->u.mux.data->chardev = g_strdup(bid);
-        mux = qemu_chardev_new(id, TYPE_CHARDEV_MUX, backend, context, errp);
-        if (mux == NULL) {
-            object_unparent(OBJECT(chr));
-            chr = NULL;
-            goto out;
-        }
-        chr = mux;
-    }
-
-out:
-    qapi_free_ChardevBackend(backend);
-    g_free(bid);
-    return chr;
-}
-
 void qemu_chr_translate_legacy_options(QDict *args)
 {
     const ChardevClass *cc;
@@ -1065,7 +1001,7 @@ Chardev *qemu_chardev_new(const char *id, const char *typename,
 }
 
 static Chardev *chardev_new_qapi(const char *id, ChardevBackend *backend,
-                                 Error **errp)
+                                 GMainContext *context, Error **errp)
 {
     const ChardevClass *cc;
 
@@ -1075,7 +1011,7 @@ static Chardev *chardev_new_qapi(const char *id, ChardevBackend *backend,
     }
 
     return chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)),
-                       backend, NULL, errp);
+                       backend, context, errp);
 }
 
 ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
@@ -1084,7 +1020,7 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
     ChardevReturn *ret;
     Chardev *chr;
 
-    chr = chardev_new_qapi(id, backend, errp);
+    chr = chardev_new_qapi(id, backend, NULL, errp);
     if (!chr) {
         return NULL;
     }
@@ -1098,7 +1034,9 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
     return ret;
 }
 
-Chardev *qemu_chr_new_cli(ChardevOptions *options, Error **errp)
+static Chardev *qemu_chr_new_cli_gcontext(ChardevOptions *options,
+                                          GMainContext *context,
+                                          Error **errp)
 {
     Chardev *chr;
     char *bid = NULL;
@@ -1107,7 +1045,7 @@ Chardev *qemu_chr_new_cli(ChardevOptions *options, Error **errp)
         bid = g_strdup_printf("%s-base", options->id);
     }
 
-    chr = chardev_new_qapi(bid ?: options->id, options->backend, errp);
+    chr = chardev_new_qapi(bid ?: options->id, options->backend, context, errp);
     if (!chr) {
         goto out;
     }
@@ -1122,8 +1060,8 @@ Chardev *qemu_chr_new_cli(ChardevOptions *options, Error **errp)
             .u.mux.data = &mux_data,
         };
 
-        mux = qemu_chardev_new(options->id, TYPE_CHARDEV_MUX, &backend, NULL,
-                               errp);
+        mux = qemu_chardev_new(options->id, TYPE_CHARDEV_MUX, &backend,
+                               context, errp);
         if (mux == NULL) {
             object_unparent(OBJECT(chr));
             chr = NULL;
@@ -1137,6 +1075,11 @@ out:
     return chr;
 }
 
+Chardev *qemu_chr_new_cli(ChardevOptions *options, Error **errp)
+{
+    return qemu_chr_new_cli_gcontext(options, NULL, errp);
+}
+
 ChardevOptions *qemu_chr_parse_cli_dict(QDict *args, bool help,
                                         Error **errp)
 {
@@ -1180,6 +1123,37 @@ ChardevOptions *qemu_chr_parse_cli_str(const char *optarg, Error **errp)
     return chr_options;
 }
 
+Chardev *qemu_chr_new_from_opts(QemuOpts *opts, GMainContext *context,
+                                Error **errp)
+{
+    ChardevOptions *chr_options;
+    Chardev *chr;
+    QDict *args;
+    const char *name = qemu_opt_get(opts, "backend");
+    bool help;
+
+    args = qemu_opts_to_qdict(opts, NULL);
+
+    if (name && is_help_option(name)) {
+        qdict_del(args, "backend");
+        qdict_del(args, "type");
+        help = true;
+    } else {
+        help = qemu_opt_has_help_opt(opts);
+    }
+
+    chr_options = qemu_chr_parse_cli_dict(args, help, errp);
+    qobject_unref(args);
+
+    if (!chr_options) {
+        return NULL;
+    }
+
+    chr = qemu_chr_new_cli_gcontext(chr_options, context, errp);
+    qapi_free_ChardevOptions(chr_options);
+    return chr;
+}
+
 ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend,
                                   Error **errp)
 {
-- 
2.28.0



  parent reply	other threads:[~2020-11-12 18:07 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-12 17:58 [PATCH 00/13] char: QAPIfy the command line parsing Kevin Wolf
2020-11-12 17:58 ` [PATCH 01/13] char: Factor out qemu_chr_print_types() Kevin Wolf
2020-11-12 17:58 ` [PATCH 02/13] char: Add ChardevOptions and qemu_chr_new_cli() Kevin Wolf
2020-11-12 17:58 ` [PATCH 03/13] char: Some QAPI aliases for CLI compatibility Kevin Wolf
2020-11-12 17:58 ` [PATCH 04/13] char: Add qemu_chr_translate_legacy_options() Kevin Wolf
2020-11-12 17:58 ` [PATCH 05/13] char-socket: Implement compat code for CLI QAPIfication Kevin Wolf
2020-11-12 17:58 ` [PATCH 06/13] char-udp: " Kevin Wolf
2020-11-12 17:58 ` [PATCH 07/13] char: Add qemu_chr_parse_cli_dict/str() Kevin Wolf
2020-11-12 17:59 ` [PATCH 08/13] char: Add mux option to ChardevOptions Kevin Wolf
2020-11-13 11:50   ` Paolo Bonzini
2020-11-13 13:20     ` Kevin Wolf
2020-11-13 14:16       ` Paolo Bonzini
2020-11-12 17:59 ` [PATCH 09/13] qemu-storage-daemon: QAPIfy --chardev Kevin Wolf
2020-11-12 17:59 ` Kevin Wolf [this message]
2020-11-12 17:59 ` [PATCH 11/13] hmp/char: Use qemu_chr_parse_cli_str() for chardev-change Kevin Wolf
2020-11-13 18:44   ` Dr. David Alan Gilbert
2020-11-12 17:59 ` [PATCH 12/13] char: Remove qemu_chr_parse_opts() Kevin Wolf
2020-11-12 17:59 ` [PATCH 13/13] char: Remove ChardevClass.parse Kevin Wolf

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=20201112175905.404472-11-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=armbru@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=pbonzini@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).