[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 07/13] char: Add qemu_chr_parse_cli_dict/str()
From: |
Kevin Wolf |
Subject: |
[PATCH 07/13] char: Add qemu_chr_parse_cli_dict/str() |
Date: |
Thu, 12 Nov 2020 18:58:59 +0100 |
This adds a function that parses a command line definition of a
character device into ChardevOptions, which can then be passed to
qemu_chr_new_cli().
You can start both from a string (for actual CLI) or from a QDict, which
is not only the intermediate representation after calling the keyval
parser, but also what HMP handlers receive.
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
include/chardev/char.h | 30 ++++++++++++++++++++++++++++
chardev/char.c | 45 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 75 insertions(+)
diff --git a/include/chardev/char.h b/include/chardev/char.h
index c0944f5828..5cd46207f6 100644
--- a/include/chardev/char.h
+++ b/include/chardev/char.h
@@ -94,6 +94,36 @@ Chardev *qemu_chr_new_from_opts(QemuOpts *opts,
*/
Chardev *qemu_chr_new_cli(ChardevOptions *options, Error **errp);
+/**
+ * qemu_chr_parse_cli_dict:
+ * @args: Options defining a new character device
+ * @help: true if help should be printed instead of returning ChardevOptions
+ *
+ * Parses the given command line option QDict into ChardevOptions, using
+ * qemu_chr_translate_legacy_options() to maintain compatibility with
+ * legacy command line syntax.
+ *
+ * Returns: On successful conversion, a ChardevOptions object containing the
+ * requested options. NULL and @errp is unchanged if help was requested and
+ * printed. NULL and @errp is set in error cases.
+ */
+ChardevOptions *qemu_chr_parse_cli_dict(QDict *args, bool help,
+ Error **errp);
+
+/**
+ * qemu_chr_parse_cli_str:
+ * @optarg: Command line argument defining a new character device
+ *
+ * Parses the given command line option into ChardevOptions, using
+ * qemu_chr_translate_legacy_options() to maintain compatibility with
+ * legacy command line syntax.
+ *
+ * Returns: On successful conversion, a ChardevOptions object containing the
+ * requested options. NULL and @errp is unchanged if help was requested and
+ * printed. NULL and @errp is set in error cases.
+ */
+ChardevOptions *qemu_chr_parse_cli_str(const char *optarg, Error **errp);
+
/**
* qemu_chr_translate_legacy_options:
* @args: Character device creation options as returned by the keyval parser
diff --git a/chardev/char.c b/chardev/char.c
index 99feaae275..a5d6be9dc8 100644
--- a/chardev/char.c
+++ b/chardev/char.c
@@ -32,8 +32,10 @@
#include "chardev/char.h"
#include "qapi/error.h"
#include "qapi/qapi-commands-char.h"
+#include "qapi/qapi-visit-char.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qerror.h"
+#include "qapi/qobject-input-visitor.h"
#include "sysemu/replay.h"
#include "qemu/help_option.h"
#include "qemu/module.h"
@@ -1106,6 +1108,49 @@ Chardev *qemu_chr_new_cli(ChardevOptions *options, Error
**errp)
return chardev_new_qapi(options->id, options->backend, errp);
}
+ChardevOptions *qemu_chr_parse_cli_dict(QDict *args, bool help,
+ Error **errp)
+{
+ Visitor *v;
+ ChardevOptions *chr_options;
+
+ qemu_chr_translate_legacy_options(args);
+
+ if (help) {
+ if (qdict_haskey(args, "type")) {
+ /* TODO Print help based on the QAPI schema */
+ qemu_opts_print_help(&qemu_chardev_opts, true);
+ } else {
+ qemu_chr_print_types();
+ }
+ return NULL;
+ }
+
+ v = qobject_input_visitor_new_keyval(QOBJECT(args));
+ visit_type_ChardevOptions(v, NULL, &chr_options, errp);
+ visit_free(v);
+
+ return chr_options;
+}
+
+ChardevOptions *qemu_chr_parse_cli_str(const char *optarg, Error **errp)
+{
+ ERRP_GUARD();
+ QDict *args;
+ ChardevOptions *chr_options;
+ bool help;
+
+ args = keyval_parse(optarg, "backend", &help, errp);
+ if (!args) {
+ return NULL;
+ }
+
+ chr_options = qemu_chr_parse_cli_dict(args, help, errp);
+ qobject_unref(args);
+
+ return chr_options;
+}
+
ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend,
Error **errp)
{
--
2.28.0
- [PATCH 00/13] char: QAPIfy the command line parsing, Kevin Wolf, 2020/11/12
- [PATCH 01/13] char: Factor out qemu_chr_print_types(), Kevin Wolf, 2020/11/12
- [PATCH 03/13] char: Some QAPI aliases for CLI compatibility, Kevin Wolf, 2020/11/12
- [PATCH 02/13] char: Add ChardevOptions and qemu_chr_new_cli(), Kevin Wolf, 2020/11/12
- [PATCH 04/13] char: Add qemu_chr_translate_legacy_options(), Kevin Wolf, 2020/11/12
- [PATCH 05/13] char-socket: Implement compat code for CLI QAPIfication, Kevin Wolf, 2020/11/12
- [PATCH 06/13] char-udp: Implement compat code for CLI QAPIfication, Kevin Wolf, 2020/11/12
- [PATCH 07/13] char: Add qemu_chr_parse_cli_dict/str(),
Kevin Wolf <=
- [PATCH 08/13] char: Add mux option to ChardevOptions, Kevin Wolf, 2020/11/12
- [PATCH 09/13] qemu-storage-daemon: QAPIfy --chardev, Kevin Wolf, 2020/11/12
- [PATCH 10/13] char: Implement qemu_chr_new_from_opts() in terms of QAPI, Kevin Wolf, 2020/11/12
- [PATCH 11/13] hmp/char: Use qemu_chr_parse_cli_str() for chardev-change, Kevin Wolf, 2020/11/12
- [PATCH 12/13] char: Remove qemu_chr_parse_opts(), Kevin Wolf, 2020/11/12
- [PATCH 13/13] char: Remove ChardevClass.parse, Kevin Wolf, 2020/11/12