tests: Move to kernel style SPDX license identifiers
[lttng-tools.git] / tests / regression / tools / mi / extract_xml.c
index 7edf1716eb979ad07da7ba35f48d8edddaa3b660..32c57e60a93c3f462069623f994ae2056ecf8e14 100644 (file)
@@ -1,17 +1,8 @@
 /*
- * Copyright (C) 2014 Jonathan Rajotte <jonathan.r.julien@gmail.com>
+ * Copyright (C) 2014 Jonathan Rajotte <jonathan.r.julien@gmail.com>
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License, version 2 only, as
- * published by the Free Software Foundation.
+ * SPDX-License-Identifier: GPL-2.0-only
  *
- * This program 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 General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 51
  */
 
 /*
  *     node;b;
  *     node;c;
  */
-#include <stdlib.h>
+#include <assert.h>
+#include <stdbool.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
-#include <assert.h>
 #include <unistd.h>
 
 #include <libxml/tree.h>
 #include <libxml/parser.h>
 #include <libxml/xpath.h>
 #include <libxml/xpathInternals.h>
+#include <common/defaults.h>
 
 #if defined(LIBXML_XPATH_ENABLED)
 
-
-int opt_verbose;
-int node_exist;
+static int opt_verbose;
+static int node_exist;
+static bool result = false;
 
 /**
  * print_xpath_nodes:
@@ -85,7 +78,7 @@ static int print_xpath_nodes(xmlDocPtr doc, xmlNodeSetPtr nodes, FILE *output)
                                        node_child_value_string = xmlNodeListGetString(doc,
                                                        cur->children, 1);
                                        if (node_exist) {
-                                               fprintf(output, "true\n");
+                                               result = true;
                                        } else if (opt_verbose) {
                                                fprintf(output, "%s;%s;\n", cur->name,
                                                                node_child_value_string);
@@ -97,7 +90,7 @@ static int print_xpath_nodes(xmlDocPtr doc, xmlNodeSetPtr nodes, FILE *output)
                                } else {
                                        /* We don't want to print non-final element */
                                        if (node_exist) {
-                                               fprintf(output, "true\n");
+                                               result = true;
                                        } else {
                                                fprintf(stderr, "ERR:%s\n",
                                                                "Xpath expression return non-final xml element");
@@ -107,7 +100,7 @@ static int print_xpath_nodes(xmlDocPtr doc, xmlNodeSetPtr nodes, FILE *output)
                                }
                        } else {
                                if (node_exist) {
-                                       fprintf(output, "true\n");
+                                       result = true;
                                } else {
                                        /* We don't want to print non-final element */
                                        fprintf(stderr, "ERR:%s\n",
@@ -120,7 +113,7 @@ static int print_xpath_nodes(xmlDocPtr doc, xmlNodeSetPtr nodes, FILE *output)
                } else {
                        cur = nodes->nodeTab[i];
                        if (node_exist) {
-                               fprintf(output, "true\n");
+                               result = true;
                        } else if (opt_verbose) {
                                fprintf(output, "%s;%s;\n", cur->parent->name, cur->content);
                        } else {
@@ -135,6 +128,31 @@ end:
        return ret;
 }
 
+static int register_lttng_namespace(xmlXPathContextPtr xpathCtx)
+{
+       int ret;
+       xmlChar *prefix;
+       xmlChar *ns = NULL;
+
+       prefix = xmlCharStrdup("lttng");
+       if (!prefix) {
+               ret = -1;
+               goto end;
+       }
+
+       ns = xmlCharStrdup(DEFAULT_LTTNG_MI_NAMESPACE);
+       if (!ns) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = xmlXPathRegisterNs(xpathCtx, prefix, ns);
+end:
+       xmlFree(prefix);
+       xmlFree(ns);
+       return ret;
+}
+
 /*
  * Extract element corresponding to xpath
  * xml_path     The path to the xml file
@@ -147,6 +165,7 @@ end:
  */
 static int extract_xpath(const char *xml_path, const xmlChar *xpath)
 {
+       int ret;
        xmlDocPtr doc = NULL;
        xmlXPathContextPtr xpathCtx = NULL;
        xmlXPathObjectPtr xpathObj = NULL;
@@ -169,6 +188,15 @@ static int extract_xpath(const char *xml_path, const xmlChar *xpath)
                return -1;
        }
 
+       /* Register the LTTng MI namespace */
+       ret = register_lttng_namespace(xpathCtx);
+       if (ret) {
+               fprintf(stderr, "ERR: Could not register lttng namespace\n");
+               xmlXPathFreeContext(xpathCtx);
+               xmlFreeDoc(doc);
+               return -1;
+       }
+
        /* Evaluate xpath expression */
        xpathObj = xmlXPathEvalExpression(xpath, xpathCtx);
        if (!xpathObj) {
@@ -185,6 +213,9 @@ static int extract_xpath(const char *xml_path, const xmlChar *xpath)
                xmlFreeDoc(doc);
                return -1;
        }
+       if (node_exist && result) {
+               fprintf(stdout, "true\n");
+       }
 
        /* Cleanup */
        xmlXPathFreeObject(xpathObj);
This page took 0.026738 seconds and 5 git commands to generate.