[libvirt] [PATCHv2 9/9] qemu: Move qemuParseKeywords(Free) to the monitor code

Peter Krempa pkrempa at redhat.com
Mon Jun 17 12:55:34 UTC 2019


The only user is now in qemu_monitor_json.c to re-parse the command line
format into keyvalue pairs for use in QMP command construction.

Move and rename the functions.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 po/POTFILES                   |   1 -
 src/qemu/Makefile.inc.am      |   2 -
 src/qemu/qemu_monitor_json.c  | 124 +++++++++++++++++++++++++++-
 src/qemu/qemu_parse_command.c | 149 ----------------------------------
 src/qemu/qemu_parse_command.h |  37 ---------
 5 files changed, 120 insertions(+), 193 deletions(-)
 delete mode 100644 src/qemu/qemu_parse_command.c
 delete mode 100644 src/qemu/qemu_parse_command.h

diff --git a/po/POTFILES b/po/POTFILES
index 9dd4ee7d99..ccfc873a89 100644
--- a/po/POTFILES
+++ b/po/POTFILES
@@ -140,7 +140,6 @@ src/qemu/qemu_migration_params.c
 src/qemu/qemu_monitor.c
 src/qemu/qemu_monitor_json.c
 src/qemu/qemu_monitor_text.c
-src/qemu/qemu_parse_command.c
 src/qemu/qemu_process.c
 src/qemu/qemu_qapi.c
 src/remote/remote_client_bodies.h
diff --git a/src/qemu/Makefile.inc.am b/src/qemu/Makefile.inc.am
index fd32a90d56..254ba07dc0 100644
--- a/src/qemu/Makefile.inc.am
+++ b/src/qemu/Makefile.inc.am
@@ -13,8 +13,6 @@ QEMU_DRIVER_SOURCES = \
 	qemu/qemu_capabilities.h \
 	qemu/qemu_command.c \
 	qemu/qemu_command.h \
-	qemu/qemu_parse_command.c \
-	qemu/qemu_parse_command.h \
 	qemu/qemu_domain.c \
 	qemu/qemu_domain.h \
 	qemu/qemu_domain_address.c \
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 2dcd65d86f..2ec41a77e2 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -31,7 +31,6 @@
 #include "qemu_monitor_text.h"
 #include "qemu_monitor_json.h"
 #include "qemu_alias.h"
-#include "qemu_parse_command.h"
 #include "qemu_capabilities.h"
 #include "viralloc.h"
 #include "virlog.h"
@@ -555,6 +554,123 @@ qemuMonitorJSONMakeCommand(const char *cmdname,
 }


+static void
+qemuMonitorJSONParseKeywordsFree(int nkeywords,
+                                 char **keywords,
+                                 char **values)
+{
+    size_t i;
+    for (i = 0; i < nkeywords; i++) {
+        VIR_FREE(keywords[i]);
+        VIR_FREE(values[i]);
+    }
+    VIR_FREE(keywords);
+    VIR_FREE(values);
+}
+
+
+/*
+ * Takes a string containing a set of key=value,key=value,key...
+ * parameters and splits them up, returning two arrays with
+ * the individual keys and values. If allowEmptyValue is nonzero,
+ * the "=value" part is optional and if a key with no value is found,
+ * NULL is be placed into corresponding place in retvalues.
+ */
+static int
+qemuMonitorJSONParseKeywords(const char *str,
+                             char ***retkeywords,
+                             char ***retvalues,
+                             int *retnkeywords,
+                             int allowEmptyValue)
+{
+    int keywordCount = 0;
+    int keywordAlloc = 0;
+    char **keywords = NULL;
+    char **values = NULL;
+    const char *start = str;
+    const char *end;
+
+    *retkeywords = NULL;
+    *retvalues = NULL;
+    *retnkeywords = 0;
+    end = start + strlen(str);
+
+    while (start) {
+        const char *separator;
+        const char *endmark;
+        char *keyword;
+        char *value = NULL;
+
+        endmark = start;
+        do {
+            /* QEMU accepts ',,' as an escape for a literal comma;
+             * skip past those here while searching for the end of the
+             * value, then strip them down below */
+            endmark = strchr(endmark, ',');
+        } while (endmark && endmark[1] == ',' && (endmark += 2));
+        if (!endmark)
+            endmark = end;
+        if (!(separator = strchr(start, '=')))
+            separator = end;
+
+        if (separator >= endmark) {
+            if (!allowEmptyValue) {
+                virReportError(VIR_ERR_INTERNAL_ERROR,
+                               _("malformed keyword arguments in '%s'"), str);
+                goto error;
+            }
+            separator = endmark;
+        }
+
+        if (VIR_STRNDUP(keyword, start, separator - start) < 0)
+            goto error;
+
+        if (separator < endmark) {
+            separator++;
+            if (VIR_STRNDUP(value, separator, endmark - separator) < 0) {
+                VIR_FREE(keyword);
+                goto error;
+            }
+            if (strchr(value, ',')) {
+                char *p = strchr(value, ',') + 1;
+                char *q = p + 1;
+                while (*q) {
+                    if (*q == ',')
+                        q++;
+                    *p++ = *q++;
+                }
+                *p = '\0';
+            }
+        }
+
+        if (keywordAlloc == keywordCount) {
+            if (VIR_REALLOC_N(keywords, keywordAlloc + 10) < 0 ||
+                VIR_REALLOC_N(values, keywordAlloc + 10) < 0) {
+                VIR_FREE(keyword);
+                VIR_FREE(value);
+                goto error;
+            }
+            keywordAlloc += 10;
+        }
+
+        keywords[keywordCount] = keyword;
+        values[keywordCount] = value;
+        keywordCount++;
+
+        start = endmark < end ? endmark + 1 : NULL;
+    }
+
+    *retkeywords = keywords;
+    *retvalues = values;
+    *retnkeywords = keywordCount;
+    return 0;
+
+ error:
+    qemuMonitorJSONParseKeywordsFree(keywordCount, keywords, values);
+    return -1;
+}
+
+
 static virJSONValuePtr
 qemuMonitorJSONKeywordStringToJSON(const char *str, const char *firstkeyword)
 {
@@ -567,7 +683,7 @@ qemuMonitorJSONKeywordStringToJSON(const char *str, const char *firstkeyword)
     if (!(ret = virJSONValueNewObject()))
         return NULL;

-    if (qemuParseKeywords(str, &keywords, &values, &nkeywords, 1) < 0)
+    if (qemuMonitorJSONParseKeywords(str, &keywords, &values, &nkeywords, 1) < 0)
         goto error;

     for (i = 0; i < nkeywords; i++) {
@@ -588,11 +704,11 @@ qemuMonitorJSONKeywordStringToJSON(const char *str, const char *firstkeyword)
         }
     }

-    qemuParseKeywordsFree(nkeywords, keywords, values);
+    qemuMonitorJSONParseKeywordsFree(nkeywords, keywords, values);
     return ret;

  error:
-    qemuParseKeywordsFree(nkeywords, keywords, values);
+    qemuMonitorJSONParseKeywordsFree(nkeywords, keywords, values);
     virJSONValueFree(ret);
     return NULL;
 }
diff --git a/src/qemu/qemu_parse_command.c b/src/qemu/qemu_parse_command.c
deleted file mode 100644
index b49aa92fb3..0000000000
--- a/src/qemu/qemu_parse_command.c
+++ /dev/null
@@ -1,149 +0,0 @@
-/*
- * qemu_parse_command.c: QEMU command parser
- *
- * Copyright (C) 2006-2016 Red Hat, Inc.
- * Copyright (C) 2006 Daniel P. Berrange
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library.  If not, see
- * <http://www.gnu.org/licenses/>.
- */
-
-#include <config.h>
-
-#include "qemu_parse_command.h"
-#include "viralloc.h"
-#include "virlog.h"
-#include "virstring.h"
-#include "virerror.h"
-
-#define VIR_FROM_THIS VIR_FROM_QEMU
-
-VIR_LOG_INIT("qemu.qemu_parse_command");
-
-
-void
-qemuParseKeywordsFree(int nkeywords,
-                      char **keywords,
-                      char **values)
-{
-    size_t i;
-    for (i = 0; i < nkeywords; i++) {
-        VIR_FREE(keywords[i]);
-        VIR_FREE(values[i]);
-    }
-    VIR_FREE(keywords);
-    VIR_FREE(values);
-}
-
-
-/*
- * Takes a string containing a set of key=value,key=value,key...
- * parameters and splits them up, returning two arrays with
- * the individual keys and values. If allowEmptyValue is nonzero,
- * the "=value" part is optional and if a key with no value is found,
- * NULL is be placed into corresponding place in retvalues.
- */
-int
-qemuParseKeywords(const char *str,
-                  char ***retkeywords,
-                  char ***retvalues,
-                  int *retnkeywords,
-                  int allowEmptyValue)
-{
-    int keywordCount = 0;
-    int keywordAlloc = 0;
-    char **keywords = NULL;
-    char **values = NULL;
-    const char *start = str;
-    const char *end;
-
-    *retkeywords = NULL;
-    *retvalues = NULL;
-    *retnkeywords = 0;
-    end = start + strlen(str);
-
-    while (start) {
-        const char *separator;
-        const char *endmark;
-        char *keyword;
-        char *value = NULL;
-
-        endmark = start;
-        do {
-            /* QEMU accepts ',,' as an escape for a literal comma;
-             * skip past those here while searching for the end of the
-             * value, then strip them down below */
-            endmark = strchr(endmark, ',');
-        } while (endmark && endmark[1] == ',' && (endmark += 2));
-        if (!endmark)
-            endmark = end;
-        if (!(separator = strchr(start, '=')))
-            separator = end;
-
-        if (separator >= endmark) {
-            if (!allowEmptyValue) {
-                virReportError(VIR_ERR_INTERNAL_ERROR,
-                               _("malformed keyword arguments in '%s'"), str);
-                goto error;
-            }
-            separator = endmark;
-        }
-
-        if (VIR_STRNDUP(keyword, start, separator - start) < 0)
-            goto error;
-
-        if (separator < endmark) {
-            separator++;
-            if (VIR_STRNDUP(value, separator, endmark - separator) < 0) {
-                VIR_FREE(keyword);
-                goto error;
-            }
-            if (strchr(value, ',')) {
-                char *p = strchr(value, ',') + 1;
-                char *q = p + 1;
-                while (*q) {
-                    if (*q == ',')
-                        q++;
-                    *p++ = *q++;
-                }
-                *p = '\0';
-            }
-        }
-
-        if (keywordAlloc == keywordCount) {
-            if (VIR_REALLOC_N(keywords, keywordAlloc + 10) < 0 ||
-                VIR_REALLOC_N(values, keywordAlloc + 10) < 0) {
-                VIR_FREE(keyword);
-                VIR_FREE(value);
-                goto error;
-            }
-            keywordAlloc += 10;
-        }
-
-        keywords[keywordCount] = keyword;
-        values[keywordCount] = value;
-        keywordCount++;
-
-        start = endmark < end ? endmark + 1 : NULL;
-    }
-
-    *retkeywords = keywords;
-    *retvalues = values;
-    *retnkeywords = keywordCount;
-    return 0;
-
- error:
-    qemuParseKeywordsFree(keywordCount, keywords, values);
-    return -1;
-}
diff --git a/src/qemu/qemu_parse_command.h b/src/qemu/qemu_parse_command.h
deleted file mode 100644
index e7af48e95b..0000000000
--- a/src/qemu/qemu_parse_command.h
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * qemu_parse_command.h: QEMU command parser
- *
- * Copyright (C) 2006-2016 Red Hat, Inc.
- * Copyright (C) 2006 Daniel P. Berrange
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library.  If not, see
- * <http://www.gnu.org/licenses/>.
- */
-
-#ifndef LIBVIRT_QEMU_PARSE_COMMAND_H
-# define LIBVIRT_QEMU_PARSE_COMMAND_H
-
-void
-qemuParseKeywordsFree(int nkeywords,
-                     char **keywords,
-                     char **values);
-
-int
-qemuParseKeywords(const char *str,
-                  char ***retkeywords,
-                  char ***retvalues,
-                  int *retnkeywords,
-                  int allowEmptyValue);
-
-#endif /* LIBVIRT_QEMU_PARSE_COMMAND_H */
-- 
2.21.0




More information about the libvir-list mailing list