[Libvirt-cim] [PATCH V2 12/48] Fix xml parsing algorithm for parse_graphics_device()

Xu Wang cngesaint at gmail.com
Mon Oct 28 02:45:41 UTC 2013


Signed-off-by: Xu Wang <gesaint at linux.vnet.ibm.com>
---
 libxkutil/device_parsing.c |  152 ++++++++++++++++++++++++++++++++++---------
 1 files changed, 120 insertions(+), 32 deletions(-)

diff --git a/libxkutil/device_parsing.c b/libxkutil/device_parsing.c
index b8fcc7b..4225153 100644
--- a/libxkutil/device_parsing.c
+++ b/libxkutil/device_parsing.c
@@ -1504,17 +1504,6 @@ static int parse_mem_device(xmlNode *node, struct virt_device **vdevs)
         return ret;
 }
 
-static char *get_attr_value_default(xmlNode *node, char *attrname,
-                                    const char *default_value)
-{
-        char *ret = get_attr_value(node, attrname);
-
-        if (ret == NULL && default_value != NULL)
-                ret = strdup(default_value);
-
-        return ret;
-}
-
 static int parse_console_device(xmlNode *node, struct virt_device **vdevs)
 {
         struct virt_device *vdev = NULL;
@@ -1790,28 +1779,92 @@ static int parse_graphics_device(xmlNode *node, struct virt_device **vdevs)
 {
         struct virt_device *vdev = NULL;
         struct graphics_device *gdev = NULL;
-        xmlNode *child = NULL;
         int ret;
 
+        CU_DEBUG("Enter parse_graphics_device().");
+
         vdev = calloc(1, sizeof(*vdev));
-        if (vdev == NULL)
+        if (vdev == NULL) {
+                CU_DEBUG("calloc failed.");
                 goto err;
+        }
 
         gdev = &(vdev->dev.graphics);
 
-        gdev->type = get_attr_value(node, "type");
-        if (gdev->type == NULL)
+        gdev->others = parse_data_to_others(gdev->others,
+                                            node,
+                                            0,
+                                            BAD_CAST "devices");
+        if (gdev->others == NULL) {
+                CU_DEBUG("parse data to others failed.");
                 goto err;
+        }
+
+        /* fetch out <graphics> tag from others. It will be removed
+         * after others management finished. */
+        fetch_from_others(&gdev->others,
+                          -1,
+                          "graphics",
+                          TYPE_NODE,
+                          -1,
+                          "devices");
 
+        fetch_from_others(&gdev->others,
+                          -1,
+                          "serial",
+                          TYPE_NODE,
+                          -1,
+                          "devices");
+
+        gdev->type = fetch_from_others(&gdev->others,
+                                       -1,
+                                       "type",
+                                       TYPE_PROP,
+                                       -1,
+                                       (char *)node->name);
+                        
+        if (gdev->type == NULL) {
+                CU_DEBUG("no type");
+                goto err;
+        }
         CU_DEBUG("graphics device type = %s", gdev->type);
 
         if (STREQC(gdev->type, "vnc")) {
-                gdev->dev.vnc.port = get_attr_value_default(node, "port",
-                                                            "-1");
-                gdev->dev.vnc.host = get_attr_value_default(node, "listen",
-                                                            "127.0.0.1");
-                gdev->dev.vnc.keymap = get_attr_value(node, "keymap");
-                gdev->dev.vnc.passwd = get_attr_value(node, "passwd");
+                gdev->dev.vnc.port = fetch_from_others(&gdev->others,
+                                                       -1,
+                                                       "port",
+                                                       TYPE_PROP,
+                                                       -1,
+                                                       (char *)node->name);
+
+                if (gdev->dev.vnc.port == NULL) {
+                        gdev->dev.vnc.port = strdup("-1");
+                }
+
+                gdev->dev.vnc.host = fetch_from_others(&gdev->others,
+                                                       -1,
+                                                       "listen",
+                                                       TYPE_PROP,
+                                                       -1,
+                                                       (char *)node->name);
+
+                if (gdev->dev.vnc.host == NULL) {
+                        gdev->dev.vnc.host = strdup("127.0.0.1");
+                }
+
+                gdev->dev.vnc.keymap = fetch_from_others(&gdev->others,
+                                                         -1,
+                                                         "keymap",
+                                                         TYPE_PROP,
+                                                         -1,
+                                                         (char *)node->name);
+
+                gdev->dev.vnc.passwd = fetch_from_others(&gdev->others,
+                                                         -1,
+                                                         "passwd",
+                                                         TYPE_PROP,
+                                                         -1,
+                                                         (char *)node->name);
 
                 if (gdev->dev.vnc.port == NULL || gdev->dev.vnc.host == NULL) {
                         CU_DEBUG("Error vnc port '%p' host '%p'",
@@ -1820,9 +1873,26 @@ static int parse_graphics_device(xmlNode *node, struct virt_device **vdevs)
                 }
         }
         else if (STREQC(gdev->type, "sdl")) {
-                gdev->dev.sdl.display = get_attr_value(node, "display");
-                gdev->dev.sdl.xauth = get_attr_value(node, "xauth");
-                gdev->dev.sdl.fullscreen = get_attr_value(node, "fullscreen");
+                gdev->dev.sdl.display = fetch_from_others(&gdev->others,
+                                                          -1,
+                                                          "display",
+                                                          TYPE_PROP,
+                                                          -1,
+                                                          (char *)node->name);
+
+                gdev->dev.sdl.xauth = fetch_from_others(&gdev->others,
+                                                        -1,
+                                                        "xauth",
+                                                        TYPE_PROP,
+                                                        -1,
+                                                        (char *)node->name);
+
+                gdev->dev.sdl.fullscreen = fetch_from_others(&gdev->others,
+                                                             -1,
+                                                             "fullscreen",
+                                                             TYPE_PROP,
+                                                             -1,
+                                                             (char *)node->name);
         }
         else if (STREQC(gdev->type, "pty")) {
                 if (node->name == NULL)
@@ -1833,14 +1903,32 @@ static int parse_graphics_device(xmlNode *node, struct virt_device **vdevs)
                 free(gdev->type);
                 gdev->type = strdup((char *)node->name);
 
-                for (child = node->children; child != NULL; 
-                        child = child->next) {
-                        if (XSTREQ(child->name, "source")) 
-                                gdev->dev.vnc.host = get_attr_value(child, "path");
-                        else if (XSTREQ(child->name, "target")) {
-                                gdev->dev.vnc.port =
-                                        get_attr_value(child, "port");
-                        }
+                if (seek_in_others(&gdev->others,
+                                   -1,
+                                   "source",
+                                   TYPE_NODE,
+                                   -1,
+                                   (char *)node->name)) {
+                        gdev->dev.vnc.host = fetch_from_others(&gdev->others,
+                                                               -1,
+                                                               "path",
+                                                               TYPE_PROP,
+                                                               -1,
+                                                               "source");
+                }
+
+                if (seek_in_others(&gdev->others,
+                                   -1,
+                                   "target",
+                                   TYPE_NODE,
+                                   -1,
+                                   (char *)node->name)) {
+                        gdev->dev.vnc.port = fetch_from_others(&gdev->others,
+                                                               -1,
+                                                               "port",
+                                                               TYPE_PROP,
+                                                               -1,
+                                                               "target");
                 }
         }
         else {
-- 
1.7.1




More information about the Libvirt-cim mailing list