/*
- * 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
*/
/*
- * Usage: extract_xml [-v] xml_path xpath_expression
+ * Usage: extract_xml [-v|-e] xml_path xpath_expression
* Evaluate XPath expression and prints result node set.
* args[1] path to the xml file
* args[2] xpath expression to extract
+ * If -e look if node exist return "true" else nothing
* If -v is set the name of the node will appear with his value delimited by
* a semicolon(;)
* Ex:
* 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)
+static int opt_verbose;
+static int node_exist;
+static bool result = false;
-int opt_verbose;
/**
* print_xpath_nodes:
* nodes: the nodes set.
*/
static int print_xpath_nodes(xmlDocPtr doc, xmlNodeSetPtr nodes, FILE *output)
{
- int ret;
+ int ret = 0;
int size;
int i;
if (xmlNodeIsText(cur->children)) {
node_child_value_string = xmlNodeListGetString(doc,
cur->children, 1);
- if (opt_verbose) {
+ if (node_exist) {
+ result = true;
+ } else if (opt_verbose) {
fprintf(output, "%s;%s;\n", cur->name,
node_child_value_string);
} else {
node_child_value_string);
}
xmlFree(node_child_value_string);
+ } else {
+ /* We don't want to print non-final element */
+ if (node_exist) {
+ result = true;
+ } else {
+ fprintf(stderr, "ERR:%s\n",
+ "Xpath expression return non-final xml element");
+ ret = -1;
+ goto end;
+ }
+ }
+ } else {
+ if (node_exist) {
+ result = true;
} else {
/* We don't want to print non-final element */
fprintf(stderr, "ERR:%s\n",
ret = -1;
goto end;
}
- } else {
- /* We don't want to print non-final element */
- fprintf(stderr, "ERR:%s\n",
- "Xpath expression return non-final xml element");
- ret = -1;
- goto end;
}
} else {
cur = nodes->nodeTab[i];
- if (opt_verbose) {
+ if (node_exist) {
+ result = true;
+ } else if (opt_verbose) {
fprintf(output, "%s;%s;\n", cur->parent->name, cur->content);
} else {
fprintf(output, "%s\n", cur->content);
-
}
}
}
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
*/
static int extract_xpath(const char *xml_path, const xmlChar *xpath)
{
+ int ret;
xmlDocPtr doc = NULL;
xmlXPathContextPtr xpathCtx = NULL;
xmlXPathObjectPtr xpathObj = NULL;
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) {
xmlFreeDoc(doc);
return -1;
}
+ if (node_exist && result) {
+ fprintf(stdout, "true\n");
+ }
/* Cleanup */
xmlXPathFreeObject(xpathObj);
int opt;
/* Parse command line and process file */
- while ((opt = getopt(argc, argv, "v")) != -1) {
+ while ((opt = getopt(argc, argv, "ve")) != -1) {
switch (opt) {
case 'v':
opt_verbose = 1;
break;
+ case 'e':
+ node_exist = 1;
+ break;
default:
abort();
}