[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH V6 11/27] qapi: list utility functions
From: |
Steve Sistare |
Subject: |
[PATCH V6 11/27] qapi: list utility functions |
Date: |
Fri, 6 Aug 2021 14:43:45 -0700 |
Generalize strList_from_comma_list() to take any delimiter character, rename
as strList_from_string(), and move it to qapi/util.c. Also add
strList_from_string() and QAPI_LIST_LENGTH().
No functional change.
Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
---
include/qapi/util.h | 28 ++++++++++++++++++++++++++++
monitor/hmp-cmds.c | 29 ++---------------------------
qapi/qapi-util.c | 37 +++++++++++++++++++++++++++++++++++++
3 files changed, 67 insertions(+), 27 deletions(-)
diff --git a/include/qapi/util.h b/include/qapi/util.h
index d7bfb30..83cc4d7 100644
--- a/include/qapi/util.h
+++ b/include/qapi/util.h
@@ -16,6 +16,8 @@ typedef struct QEnumLookup {
int size;
} QEnumLookup;
+struct strList;
+
const char *qapi_enum_lookup(const QEnumLookup *lookup, int val);
int qapi_enum_parse(const QEnumLookup *lookup, const char *buf,
int def, Error **errp);
@@ -25,6 +27,19 @@ bool qapi_bool_parse(const char *name, const char *value,
bool *obj,
int parse_qapi_name(const char *name, bool complete);
/*
+ * Produce and return a NULL-terminated array of strings from @args.
+ * All strings are g_strdup'd.
+ */
+char **strv_from_strList(const struct strList *args);
+
+/*
+ * Produce a strList from the character delimited string @in.
+ * All strings are g_strdup'd.
+ * A NULL or empty input string returns NULL.
+ */
+struct strList *strList_from_string(const char *in, char delim);
+
+/*
* For any GenericList @list, insert @element at the front.
*
* Note that this macro evaluates @element exactly once, so it is safe
@@ -50,4 +65,17 @@ int parse_qapi_name(const char *name, bool complete);
(tail) = &(*(tail))->next; \
} while (0)
+/*
+ * For any GenericList @list, return its length.
+ */
+#define QAPI_LIST_LENGTH(list) \
+ ({ \
+ int len = 0; \
+ typeof(list) elem; \
+ for (elem = list; elem != NULL; elem = elem->next) { \
+ len++; \
+ } \
+ len; \
+ })
+
#endif
diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
index 6aed6ac..da91a0a 100644
--- a/monitor/hmp-cmds.c
+++ b/monitor/hmp-cmds.c
@@ -43,6 +43,7 @@
#include "qapi/qapi-commands-run-state.h"
#include "qapi/qapi-commands-tpm.h"
#include "qapi/qapi-commands-ui.h"
+#include "qapi/util.h"
#include "qapi/qapi-visit-net.h"
#include "qapi/qapi-visit-migration.h"
#include "qapi/qmp/qdict.h"
@@ -70,32 +71,6 @@ void hmp_handle_error(Monitor *mon, Error *err)
}
}
-/*
- * Produce a strList from a comma separated list.
- * A NULL or empty input string return NULL.
- */
-static strList *strList_from_comma_list(const char *in)
-{
- strList *res = NULL;
- strList **tail = &res;
-
- while (in && in[0]) {
- char *comma = strchr(in, ',');
- char *value;
-
- if (comma) {
- value = g_strndup(in, comma - in);
- in = comma + 1; /* skip the , */
- } else {
- value = g_strdup(in);
- in = NULL;
- }
- QAPI_LIST_APPEND(tail, value);
- }
-
- return res;
-}
-
void hmp_info_name(Monitor *mon, const QDict *qdict)
{
NameInfo *info;
@@ -1170,7 +1145,7 @@ void hmp_announce_self(Monitor *mon, const QDict *qdict)
migrate_announce_params());
qapi_free_strList(params->interfaces);
- params->interfaces = strList_from_comma_list(interfaces_str);
+ params->interfaces = strList_from_string(interfaces_str, ',');
params->has_interfaces = params->interfaces != NULL;
params->id = g_strdup(id);
params->has_id = !!params->id;
diff --git a/qapi/qapi-util.c b/qapi/qapi-util.c
index 3c24bb3..1edbd04 100644
--- a/qapi/qapi-util.c
+++ b/qapi/qapi-util.c
@@ -14,6 +14,7 @@
#include "qapi/error.h"
#include "qemu/ctype.h"
#include "qapi/qmp/qerror.h"
+#include "qapi/qapi-builtin-types.h"
const char *qapi_enum_lookup(const QEnumLookup *lookup, int val)
{
@@ -109,3 +110,39 @@ int parse_qapi_name(const char *str, bool complete)
}
return p - str;
}
+
+char **strv_from_strList(const strList *args)
+{
+ const strList *arg;
+ int i = 0;
+ char **argv = g_malloc((QAPI_LIST_LENGTH(args) + 1) * sizeof(char *));
+
+ for (arg = args; arg != NULL; arg = arg->next) {
+ argv[i++] = g_strdup(arg->value);
+ }
+ argv[i] = NULL;
+
+ return argv;
+}
+
+strList *strList_from_string(const char *in, char delim)
+{
+ strList *res = NULL;
+ strList **tail = &res;
+
+ while (in && in[0]) {
+ char *next = strchr(in, delim);
+ char *value;
+
+ if (next) {
+ value = g_strndup(in, next - in);
+ in = next + 1; /* skip the delim */
+ } else {
+ value = g_strdup(in);
+ in = NULL;
+ }
+ QAPI_LIST_APPEND(tail, value);
+ }
+
+ return res;
+}
--
1.8.3.1
- [PATCH V6 00/27] Live Update, Steve Sistare, 2021/08/06
- [PATCH V6 02/27] migration: fix populate_vfio_info, Steve Sistare, 2021/08/06
- [PATCH V6 03/27] migration: qemu file wrappers, Steve Sistare, 2021/08/06
- [PATCH V6 04/27] migration: simplify savevm, Steve Sistare, 2021/08/06
- [PATCH V6 05/27] vl: start on wakeup request, Steve Sistare, 2021/08/06
- [PATCH V6 06/27] cpr: reboot mode, Steve Sistare, 2021/08/06
- [PATCH V6 07/27] cpr: reboot HMP interfaces, Steve Sistare, 2021/08/06
- [PATCH V6 08/27] memory: flat section iterator, Steve Sistare, 2021/08/06
- [PATCH V6 09/27] oslib: qemu_clear_cloexec, Steve Sistare, 2021/08/06
- [PATCH V6 10/27] machine: memfd-alloc option, Steve Sistare, 2021/08/06
- [PATCH V6 11/27] qapi: list utility functions,
Steve Sistare <=
- [PATCH V6 12/27] vl: helper to request re-exec, Steve Sistare, 2021/08/06
- [PATCH V6 13/27] cpr: preserve extra state, Steve Sistare, 2021/08/06
- [PATCH V6 14/27] cpr: restart mode, Steve Sistare, 2021/08/06
- [PATCH V6 15/27] cpr: restart HMP interfaces, Steve Sistare, 2021/08/06
- [PATCH V6 16/27] hostmem-memfd: cpr for memory-backend-memfd, Steve Sistare, 2021/08/06
- [PATCH V6 17/27] pci: export functions for cpr, Steve Sistare, 2021/08/06
- [PATCH V6 18/27] vfio-pci: refactor for cpr, Steve Sistare, 2021/08/06
- [PATCH V6 20/27] vfio-pci: cpr part 2 (msi), Steve Sistare, 2021/08/06