qemu-storage-daemon: Remove QemuOpts from --object parser
cover qemu-storage-daemon: Remove QemuOpts from --object parser
This replaces the QemuOpts-based help code for --object in the storage daemon with code based on the keyval parser.
Kevin Wolf (4):
keyval: Parse help options
qom: Factor out helpers from user_creatable_print_help()
qom: Add user_creatable_print_help_from_qdict()
qemu-storage-daemon: Remove QemuOpts from --object parser
include/qemu/option.h | 2 +- include/qom/object_interfaces.h | 9 ++ qapi/qobject-input-visitor.c | 2 +- qom/object_interfaces.c | 99 ++++++++++------- storage-daemon/qemu-storage-daemon.c | 15 +-- tests/test-keyval.c | 157 ++++++++++++++++----------- util/keyval.c | 28 ++++- 7 files changed, 196 insertions(+), 116 deletions(-)
Signature · 1 line
2.25.4
PATCH 1/4 keyval: Parse help options
This adds a new parameter 'help' to keyval_parse() that enables parsing of help options. If NULL is passed, the function behaves the same as before. But if a bool pointer is given, it contains the information whether an option "help" without value was given (which would otherwise either result in an error or be interpreted as the value for an implied key).
--- include/qemu/option.h | 2 +- qapi/qobject-input-visitor.c | 2 +- storage-daemon/qemu-storage-daemon.c | 2 +- tests/test-keyval.c | 157 ++++++++++++++++----------- util/keyval.c | 28 ++++- 5 files changed, 123 insertions(+), 68 deletions(-)
include/qemu/option.h +1 -1
@@ -149,6 +149,6 @@ void qemu_opts_free(QemuOptsList *list);QemuOptsList *qemu_opts_append (QemuOptsList *dst,QemuOptsList *list);QDict *keyval_parse (const char *params,const char *implied_key, -Error **errp); +bool *help,Error **errp);#endif
qapi/qobject-input-visitor.c +1 -1
@@ -757,7 +757,7 @@ Visitor *qobject_input_visitor_new_str(const char *str,assert (args); v =qobject_input_visitor_new (QOBJECT (args)); }else { - args =keyval_parse (str, implied_key, errp); + args =keyval_parse (str, implied_key,NULL , errp);if (!args) {return NULL ; }
storage-daemon/qemu-storage-daemon.c +1 -1
@@ -278,7 +278,7 @@ static void process_options(int argc, char *argv[]) }qemu_opts_del (opts); - args =keyval_parse (optarg,"qom-type" , &error_fatal); + args =keyval_parse (optarg,"qom-type" ,NULL , &error_fatal);user_creatable_add_dict (args, true, &error_fatal);qobject_unref (args);break ;
tests/test-keyval.c +95 -62
455 lines not on this page · raw message
util/keyval.c +25 -3
@@ -166,7 +166,7 @@ static QObject *keyval_parse_put(QDict *cur,* On failure, return NULL. */ static const char *keyval_parse_one (QDict *qdict,const char *params, -const char *implied_key, +const char *implied_key,bool *help,Error **errp) {const char *key, *key_end, *s, *end;
@@ -179,6 +179,16 @@ static const char *keyval_parse_one(QDict *qdict, const char *params, key = params; len =strcspn (params,"=," ); + +if (help && key[len] !='=' && !strncmp (key,"help" , len)) { + *help = true; + s = key + len; +if (key[len] !='\0' ) { + s++; + } +return s; + } +if (implied_key && len && key[len] !='=' ) {/* Desugar implied key */ key = implied_key;
@@ -388,21 +398,33 @@ static QObject *keyval_listify(QDict *cur, GSList *key_of_cur, Error **errp)/* * Parse @params in QEMU's traditional KEY=VALUE,... syntax. +* * If @implied_key, the first KEY= can be omitted. @implied_key is * implied then, and VALUE can't be empty or contain ',' or '='. +* +* If @help is given, an option "help" without a value isn't added to +* the resulting dictionary, but instead sets @help to true. If no +* help option is found, @help is false on return. All other options +* are parsed and returned normally so that context specific help can +* be printed. +* * On success, return a dictionary of the parsed keys and values. * On failure, store an error through @errp and return NULL. */ QDict *keyval_parse (const char *params,const char *implied_key, -Error **errp) +bool *help,Error **errp) {QDict *qdict =qdict_new ();QObject *listified;const char *s; +if (help) { + *help = false; + } + s = params;while (*s) { - s =keyval_parse_one (qdict, s, implied_key, errp); + s =keyval_parse_one (qdict, s, implied_key, help, errp);if (!s) {qobject_unref (qdict);return NULL ;
Signature · 1 line
2.25.4
Re: [PATCH 1/4] keyval: Parse help options
Quoted 11 lines · Kevin Wolf
This adds a new parameter 'help' to
keyval_parse()that enables parsing of help options. If NULL is passed, the function behaves the same as before. But if a bool pointer is given, it contains the information whether an option "help" without value was given (which would otherwise either result in an error or be interpreted as the value for an implied key).Signed-off-by Kevin Wolf <[email protected]>---
+++
b/util/keyval.c
Might be nice to see this before the testsuite changes by tweaking the git orderfile.
Quoted 10 lines
util/keyval.c +1
-1@@ -166,7 +166,7 @@ static QObject *keyval_parse_put(QDict *cur,* On failure, return NULL. */ static const char *keyval_parse_one (QDict *qdict,const char *params, -const char *implied_key, +const char *implied_key,bool *help,Error **errp) {const char *key, *key_end, *s, *end;
util/keyval.c static const char *keyval_parse_one(QDict *qdict, const char *params, · line 183
Quoted 5 lines
util/keyval.c +2
-0@@ -179,6 +179,16 @@ static const char *keyval_parse_one(QDict *qdict, const char *params, key = params; len =strcspn (params,"=," ); + +if (help && key[len] !='=' && !strncmp (key,"help" , len)) {
What if the user typed "help,," to get "help," as the value of the implied key?
Quoted 38 lines
util/keyval.c +18
-1+ *help = true; + s = key + len; +if (key[len] !='\0' ) { + s++; + } +return s; + } +if (implied_key && len && key[len] !='=' ) {/* Desugar implied key */ key = implied_key;@@ -388,21 +398,33 @@ static QObject *keyval_listify(QDict *cur, GSList *key_of_cur, Error **errp)/* * Parse @params in QEMU's traditional KEY=VALUE,... syntax. +* * If @implied_key, the first KEY= can be omitted. @implied_key is * implied then, and VALUE can't be empty or contain ',' or '='. +* +* If @help is given, an option "help" without a value isn't added to +* the resulting dictionary, but instead sets @help to true. If no +* help option is found, @help is false on return. All other options +* are parsed and returned normally so that context specific help can +* be printed. +* * On success, return a dictionary of the parsed keys and values. * On failure, store an error through @errp and return NULL. */ QDict *keyval_parse (const char *params,const char *implied_key, -Error **errp) +bool *help,Error **errp) {QDict *qdict =qdict_new ();QObject *listified;const char *s; +if (help) {
util/keyval.c static QObject *keyval_listify(QDict *cur, GSList *key_of_cur, Error **errp) · line 430
Quoted 10 lines
util/keyval.c +4
-1+ *help = false; + } + s = params;while (*s) { - s =keyval_parse_one (qdict, s, implied_key, errp); + s =keyval_parse_one (qdict, s, implied_key, help, errp);if (!s) {qobject_unref (qdict);return NULL ;
I like it, but wonder if you are missing one corner case.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Re: [PATCH 1/4] keyval: Parse help options
Am 29.09.2020 um 19:46 hat Eric Blake geschrieben:
Quoted 8 lines · Kevin Wolf
Quoted 7 lines
This adds a new parameter 'help' to
keyval_parse()that enables parsing of help options. If NULL is passed, the function behaves the same as before. But if a bool pointer is given, it contains the information whether an option "help" without value was given (which would otherwise either result in an error or be interpreted as the value for an implied key).
Quoted 7 lines
Quoted 4 lines
Signed-off-by Kevin Wolf <[email protected]>---
+++
b/util/keyval.cMight be nice to see this before the testsuite changes by tweaking the git orderfile.
What does your git orderfile look like? I don't know how to exclude tests/ from file type patterns like *.c.
Kevin
Re: [PATCH 1/4] keyval: Parse help options
Quoted 12 lines · Kevin Wolf
Am 29.09.2020 um 19:46 hat Eric Blake geschrieben:
Quoted 11 lines · Kevin Wolf
Quoted 9 lines
This adds a new parameter 'help' to
keyval_parse()that enables parsing of help options. If NULL is passed, the function behaves the same as before. But if a bool pointer is given, it contains the information whether an option "help" without value was given (which would otherwise either result in an error or be interpreted as the value for an implied key).Signed-off-by Kevin Wolf <[email protected]>---
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
+++
b/util/keyval.cMight be nice to see this before the testsuite changes by tweaking the git orderfile.
What does your git orderfile look like? I don't know how to exclude tests/ from file type patterns like *.c.
You can start with scripts/git.orderfile, and temporarily add:
# decoding tree specification *.decode
+# Key files that I want first for this patch
+util/*.c
+
# code *.c
or similar. It's not a show-stopper if you don't, and I concede that remembering to do it (and then to revert back to the usual afterwords) is not trivial.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Re: [PATCH 1/4] keyval: Parse help options
Am 30.09.2020 um 15:42 hat Eric Blake geschrieben:
Quoted 32 lines · Kevin Wolf
Quoted 12 lines
Am 29.09.2020 um 19:46 hat Eric Blake geschrieben:
Quoted 11 lines · Kevin Wolf
Quoted 9 lines
This adds a new parameter 'help' to
keyval_parse()that enables parsing of help options. If NULL is passed, the function behaves the same as before. But if a bool pointer is given, it contains the information whether an option "help" without value was given (which would otherwise either result in an error or be interpreted as the value for an implied key).Signed-off-by Kevin Wolf <[email protected]>---
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
+++
b/util/keyval.cMight be nice to see this before the testsuite changes by tweaking the git orderfile.
What does your git orderfile look like? I don't know how to exclude tests/ from file type patterns like *.c.
You can start with
scripts/git.orderfile, and temporarily add:# decoding tree specification *.decode(quoted diff) +3
-0+# Key files that I want first for this patch +util/*.c + # code *.c
Quoted 3 lines
or similar. It's not a show-stopper if you don't, and I concede that remembering to do it (and then to revert back to the usual afterwords) is not trivial.
Ah, I see. I never did per-patch/series orderfiles, I just have my generic one that does things like headers before implementation, and documentation and QAPI schema changes before both.
Kevin
Re: [PATCH 1/4] keyval: Parse help options
In reply to Eric BlakeQuoted 27 lines · Eric Blake
Quoted 11 lines · Kevin Wolf
This adds a new parameter 'help' to
keyval_parse()that enables parsing of help options. If NULL is passed, the function behaves the same as before. But if a bool pointer is given, it contains the information whether an option "help" without value was given (which would otherwise either result in an error or be interpreted as the value for an implied key).Signed-off-by Kevin Wolf <[email protected]>---
+++
b/util/keyval.cMight be nice to see this before the testsuite changes by tweaking the git orderfile.
Quoted 11 lines
util/keyval.c +1
-1@@ -166,7 +166,7 @@ static QObject *keyval_parse_put(QDict *cur,* On failure, return NULL. */ static const char *keyval_parse_one (QDict *qdict,const char *params, -const char *implied_key, +const char *implied_key,bool *help,Error **errp) {const char *key, *key_end, *s, *end;@@ -179,6 +179,16 @@ static const char *keyval_parse_one(QDict *qdict, const char *params,
Quoted 7 lines
util/keyval.c static const char *keyval_parse_one(QDict *qdict, const char *params, · line 183
Quoted 4 lines
util/keyval.c +2
-0key = params; len =strcspn (params,"=," ); + +if (help && key[len] !='=' && !strncmp (key,"help" , len)) {What if the user typed "help,," to get "help," as the value of the implied key?
The value of an implied key cannot contain ','. This is intentional.
test-keyval.c:
/* Implied key with empty value (qemu_opts_parse() accepts this) */
qdict = keyval_parse(",", "implied", &err);
error_free_or_abort(&err);
g_assert(!qdict);
/* Likewise (qemu_opts_parse(): implied key with comma value) */
qdict = keyval_parse(",,,a=1", "implied", &err);
error_free_or_abort(&err);
g_assert(!qdict);Grammar:
* val-no-key = / [^=,]* /
Aside: should be + instead of *. Doc bug. I'll fix it.
[...]
PATCH 2/4 qom: Factor out helpers from user_creatable_print_help()
In reply to Kevin WolfThis creates separate helper functions for printing a list of user creatable object types and for printing a list of properties of a given type. This allows using these parts without having a QemuOpts.
--- qom/object_interfaces.c | 90 ++++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 38 deletions(-)
qom/object_interfaces.c +52 -38
@@ -214,54 +214,68 @@ char *object_property_help(const char *name, const char *type,return g_string_free (str, false); } -bool user_creatable_print_help (const char *type,QemuOpts *opts) +static void user_creatable_print_types (void ) +{ +GSList *l, *list; + +printf ("List of user creatable objects:\n" ); + list =object_class_get_list_sorted (TYPE_USER_CREATABLE , false); +for (l = list; l !=NULL ; l = l->next) { +ObjectClass *oc =OBJECT_CLASS (l->data); +printf (" %s\n" ,object_class_get_name (oc)); + } +g_slist_free (list); +} + +static bool user_creatable_print_type_properites (const char *type) {ObjectClass *klass; +ObjectPropertyIterator iter; +ObjectProperty *prop; +GPtrArray *array; +int i; -if (is_help_option (type)) { -GSList *l, *list; + klass =object_class_by_name (type); +if (!klass) { +return false; + } -printf ("List of user creatable objects:\n" ); - list =object_class_get_list_sorted (TYPE_USER_CREATABLE , false); -for (l = list; l !=NULL ; l = l->next) { -ObjectClass *oc =OBJECT_CLASS (l->data); -printf (" %s\n" ,object_class_get_name (oc)); + array =g_ptr_array_new (); +object_class_property_iter_init (&iter, klass); +while ((prop =object_property_iter_next (&iter))) { +if (!prop->set) { +continue ; } -g_slist_free (list); -return true; + +g_ptr_array_add (array, +object_property_help (prop->name, prop->type, + prop->defval, prop->description)); } +g_ptr_array_sort (array, (GCompareFunc )qemu_pstrcmp0); +if (array->len >0 ) { +printf ("%s options:\n" , type); + }else { +printf ("There are no options for %s.\n" , type); + } +for (i =0 ; i < array->len; i++) { +printf ("%s\n" , (char *)array->pdata[i]); + } +g_ptr_array_set_free_func (array, g_free); +g_ptr_array_free (array, true); +return true; +} - klass =object_class_by_name (type); -if (klass &&qemu_opt_has_help_opt (opts)) { -ObjectPropertyIterator iter; -ObjectProperty *prop; -GPtrArray *array =g_ptr_array_new (); -int i; - -object_class_property_iter_init (&iter, klass); -while ((prop =object_property_iter_next (&iter))) { -if (!prop->set) { -continue ; - } - -g_ptr_array_add (array, -object_property_help (prop->name, prop->type, - prop->defval, prop->description)); - } -g_ptr_array_sort (array, (GCompareFunc )qemu_pstrcmp0); -if (array->len >0 ) { -printf ("%s options:\n" , type); - }else { -printf ("There are no options for %s.\n" , type); - } -for (i =0 ; i < array->len; i++) { -printf ("%s\n" , (char *)array->pdata[i]); - } -g_ptr_array_set_free_func (array, g_free); -g_ptr_array_free (array, true); +bool user_creatable_print_help (const char *type,QemuOpts *opts) +{ +if (is_help_option (type)) { +user_creatable_print_types ();return true; } +if (qemu_opt_has_help_opt (opts)) { +return user_creatable_print_type_properites (type); + } +return false; }
Signature · 1 line
2.25.4
Re: [PATCH 2/4] qom: Factor out helpers from user_creatable_print_help()
Quoted 9 lines · Kevin Wolf
This creates separate helper functions for printing a list of user creatable object types and for printing a list of properties of a given type. This allows using these parts without having a QemuOpts.
Signed-off-by Kevin Wolf <[email protected]>--- qom/object_interfaces.c | 90 ++++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 38 deletions(-)
Awkward diff as presented; ignoring whitespace makes it easier.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
PATCH 3/4 qom: Add user_creatable_print_help_from_qdict()
In reply to Kevin WolfThis adds a function that, given a QDict of non-help options, prints help for user creatable objects.
--- include/qom/object_interfaces.h | 9 +++++++++ qom/object_interfaces.c | 9 +++++++++ 2 files changed, 18 insertions(+)
include/qom/object_interfaces.h +9 -0
@@ -161,6 +161,15 @@ int user_creatable_add_opts_foreach(void *opaque,*/ bool user_creatable_print_help (const char *type,QemuOpts *opts); +/** +* user_creatable_print_help_from_qdict: +* @args: options to create +* +* Prints help considering the other options given in @args (if "qom-type" is +* given and valid, print properties for the type, otherwise print valid types) +*/ +void user_creatable_print_help_from_qdict (QDict *args); +/** * user_creatable_del: * @id: the unique ID for the object
qom/object_interfaces.c +9 -0
@@ -279,6 +279,15 @@ bool user_creatable_print_help(const char *type, QemuOpts *opts)return false; } +void user_creatable_print_help_from_qdict (QDict *args) +{ +const char *type =qdict_get_try_str (args,"qom-type" ); + +if (!type || !user_creatable_print_type_properites (type)) { +user_creatable_print_types (); + } +} +bool user_creatable_del (const char *id,Error **errp) {Object *container;
Signature · 1 line
2.25.4
Re: [PATCH 3/4] qom: Add user_creatable_print_help_from_qdict()
Quoted 9 lines · Kevin Wolf
This adds a function that, given a QDict of non-help options, prints help for user creatable objects.
Signed-off-by Kevin Wolf <[email protected]>--- include/qom/object_interfaces.h | 9 +++++++++ qom/object_interfaces.c | 9 +++++++++ 2 files changed, 18 insertions(+)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
PATCH 4/4 qemu-storage-daemon: Remove QemuOpts from --object parser
In reply to Kevin WolfThe command line parser for --object parses the input twice: Once into QemuOpts just for detecting help options, and then again into a QDict using the keyval parser for actually creating the object.
Now that the keyval parser can also detect help options, we can simplify this and remove the QemuOpts part.
--- storage-daemon/qemu-storage-daemon.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-)
storage-daemon/qemu-storage-daemon.c +4 -11
@@ -264,21 +264,14 @@ static void process_options(int argc, char *argv[]) }case OPTION_OBJECT : { -QemuOpts *opts; -const char *type;QDict *args; +bool help; -/* FIXME The keyval parser rejects 'help' arguments, so we must -* unconditionall try QemuOpts first. */ - opts =qemu_opts_parse (&qemu_object_opts, - optarg, true, &error_fatal); - type =qemu_opt_get (opts,"qom-type" ); -if (type &&user_creatable_print_help (type, opts)) { + args =keyval_parse (optarg,"qom-type" , &help, &error_fatal); +if (help) { +user_creatable_print_help_from_qdict (args);exit (EXIT_SUCCESS ); } -qemu_opts_del (opts); - - args =keyval_parse (optarg,"qom-type" ,NULL , &error_fatal);user_creatable_add_dict (args, true, &error_fatal);qobject_unref (args);break ;
Signature · 1 line
2.25.4
Quoted 17 lines · Kevin Wolf
The command line parser for --object parses the input twice: Once into QemuOpts just for detecting help options, and then again into a QDict using the keyval parser for actually creating the object.
Now that the keyval parser can also detect help options, we can simplify this and remove the QemuOpts part.
Signed-off-by Kevin Wolf <[email protected]>--- storage-daemon/qemu-storage-daemon.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-)storage-daemon/qemu-storage-daemon.c +0
-0
storage-daemon/qemu-storage-daemon.c static void process_options(int argc, char *argv[]) · line 272
Quoted 10 lines
storage-daemon/qemu-storage-daemon.c +1
-4@@ -264,21 +264,14 @@ static void process_options(int argc, char *argv[]) }case OPTION_OBJECT : { -QemuOpts *opts; -const char *type;QDict *args; +bool help; -/* FIXME The keyval parser rejects 'help' arguments, so we must -* unconditionall try QemuOpts first. */
And you're fixing a typo by deleting it ;)
storage-daemon/qemu-storage-daemon.c static void process_options(int argc, char *argv[]) · line 274
Quoted 9 lines
storage-daemon/qemu-storage-daemon.c +3
-4- opts =qemu_opts_parse (&qemu_object_opts, - optarg, true, &error_fatal); - type =qemu_opt_get (opts,"qom-type" ); -if (type &&user_creatable_print_help (type, opts)) { + args =keyval_parse (optarg,"qom-type" , &help, &error_fatal); +if (help) { +user_creatable_print_help_from_qdict (args);exit (EXIT_SUCCESS ); }
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org