From 1e208225416ae4c98e2534616f3095c1677402b1 Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Tue, 22 Oct 2002 14:25:25 +0000 Subject: [PATCH] adding a grep command to --shell in xmllint for T.V. Raman Daniel * debugXML.c: adding a grep command to --shell in xmllint for T.V. Raman Daniel --- ChangeLog | 5 ++++ debugXML.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/ChangeLog b/ChangeLog index efa005a6..172e7471 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Tue Oct 22 16:25:18 CEST 2002 Daniel Veillard + + * debugXML.c: adding a grep command to --shell in xmllint + for T.V. Raman + Tue Oct 22 16:23:57 CEST 2002 Daniel Veillard * xmlcatalog.c: tried to fix some of the problem with --sgml diff --git a/debugXML.c b/debugXML.c index 200fe4e6..cbc5db07 100644 --- a/debugXML.c +++ b/debugXML.c @@ -1551,6 +1551,84 @@ xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, return (0); } +/** + * xmlShellGrep: + * @ctxt: the shell context + * @arg: the string or regular expression to find + * @node: a node + * @node2: unused + * + * Implements the XML shell function "grep" + * dumps informations about the node (namespace, attributes, content). + * + * Returns 0 + */ +int +xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, + char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED) +{ + if (!ctxt) + return (0); + if (node == NULL) + return (0); + if (arg == NULL) + return (0); +#ifdef LIBXML_REGEXP_ENABLED + if ((xmlStrchr((xmlChar *) arg, '?')) || + (xmlStrchr((xmlChar *) arg, '*')) || + (xmlStrchr((xmlChar *) arg, '.')) || + (xmlStrchr((xmlChar *) arg, '['))) { + } +#endif + while (node != NULL) { + if (node->type == XML_COMMENT_NODE) { + if (xmlStrstr(node->content, arg)) { + + fprintf(ctxt->output, "%s : ", xmlGetNodePath(node)); + xmlShellList(ctxt, NULL, node, NULL); + } + } else if (node->type == XML_TEXT_NODE) { + if (xmlStrstr(node->content, arg)) { + + fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent)); + xmlShellList(ctxt, NULL, node, NULL); + } + } + + /* + * Browse the full subtree, deep first + */ + + if ((node->type == XML_DOCUMENT_NODE) || + (node->type == XML_HTML_DOCUMENT_NODE)) { + node = ((xmlDocPtr) node)->children; + } else if ((node->children != NULL) + && (node->type != XML_ENTITY_REF_NODE)) { + /* deep first */ + node = node->children; + } else if (node->next != NULL) { + /* then siblings */ + node = node->next; + } else { + /* go up to parents->next if needed */ + while (node != NULL) { + if (node->parent != NULL) { + node = node->parent; + } + if (node->next != NULL) { + node = node->next; + break; + } + if (node->parent == NULL) { + node = NULL; + break; + } + } + } + } + return (0); +} + /** * xmlShellDir: * @ctxt: the shell context @@ -2102,6 +2180,7 @@ xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input, fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n"); fprintf(ctxt->output, "\tvalidate check the document for errors\n"); fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n"); + fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n"); } else if (!strcmp(command, "validate")) { xmlShellValidate(ctxt, arg, NULL, NULL); } else if (!strcmp(command, "load")) { @@ -2110,6 +2189,8 @@ xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input, xmlShellSave(ctxt, arg, NULL, NULL); } else if (!strcmp(command, "write")) { xmlShellWrite(ctxt, arg, NULL, NULL); + } else if (!strcmp(command, "grep")) { + xmlShellGrep(ctxt, arg, ctxt->node, NULL); } else if (!strcmp(command, "free")) { if (arg[0] == 0) { xmlMemShow(ctxt->output, 0);