Blame debugXML.c

Packit 423ecb
/*
Packit 423ecb
 * debugXML.c : This is a set of routines used for debugging the tree
Packit 423ecb
 *              produced by the XML parser.
Packit 423ecb
 *
Packit 423ecb
 * See Copyright for the status of this software.
Packit 423ecb
 *
Packit 423ecb
 * Daniel Veillard <daniel@veillard.com>
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
#define IN_LIBXML
Packit 423ecb
#include "libxml.h"
Packit 423ecb
#ifdef LIBXML_DEBUG_ENABLED
Packit 423ecb
Packit 423ecb
#include <string.h>
Packit 423ecb
#ifdef HAVE_STDLIB_H
Packit 423ecb
#include <stdlib.h>
Packit 423ecb
#endif
Packit 423ecb
#ifdef HAVE_STRING_H
Packit 423ecb
#include <string.h>
Packit 423ecb
#endif
Packit 423ecb
#include <libxml/xmlmemory.h>
Packit 423ecb
#include <libxml/tree.h>
Packit 423ecb
#include <libxml/parser.h>
Packit 423ecb
#include <libxml/parserInternals.h>
Packit 423ecb
#include <libxml/valid.h>
Packit 423ecb
#include <libxml/debugXML.h>
Packit 423ecb
#include <libxml/HTMLtree.h>
Packit 423ecb
#include <libxml/HTMLparser.h>
Packit 423ecb
#include <libxml/xmlerror.h>
Packit 423ecb
#include <libxml/globals.h>
Packit 423ecb
#include <libxml/xpathInternals.h>
Packit 423ecb
#include <libxml/uri.h>
Packit 423ecb
#ifdef LIBXML_SCHEMAS_ENABLED
Packit 423ecb
#include <libxml/relaxng.h>
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
#define DUMP_TEXT_TYPE 1
Packit 423ecb
Packit 423ecb
typedef struct _xmlDebugCtxt xmlDebugCtxt;
Packit 423ecb
typedef xmlDebugCtxt *xmlDebugCtxtPtr;
Packit 423ecb
struct _xmlDebugCtxt {
Packit 423ecb
    FILE *output;               /* the output file */
Packit 423ecb
    char shift[101];            /* used for indenting */
Packit 423ecb
    int depth;                  /* current depth */
Packit 423ecb
    xmlDocPtr doc;              /* current document */
Packit 423ecb
    xmlNodePtr node;		/* current node */
Packit 423ecb
    xmlDictPtr dict;		/* the doc dictionary */
Packit 423ecb
    int check;                  /* do just checkings */
Packit 423ecb
    int errors;                 /* number of errors found */
Packit 423ecb
    int nodict;			/* if the document has no dictionary */
Packit 423ecb
    int options;		/* options */
Packit 423ecb
};
Packit 423ecb
Packit 423ecb
static void xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node);
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpInitCtxt(xmlDebugCtxtPtr ctxt)
Packit 423ecb
{
Packit 423ecb
    int i;
Packit 423ecb
Packit 423ecb
    ctxt->depth = 0;
Packit 423ecb
    ctxt->check = 0;
Packit 423ecb
    ctxt->errors = 0;
Packit 423ecb
    ctxt->output = stdout;
Packit 423ecb
    ctxt->doc = NULL;
Packit 423ecb
    ctxt->node = NULL;
Packit 423ecb
    ctxt->dict = NULL;
Packit 423ecb
    ctxt->nodict = 0;
Packit 423ecb
    ctxt->options = 0;
Packit 423ecb
    for (i = 0; i < 100; i++)
Packit 423ecb
        ctxt->shift[i] = ' ';
Packit 423ecb
    ctxt->shift[100] = 0;
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpCleanCtxt(xmlDebugCtxtPtr ctxt ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
 /* remove the ATTRIBUTE_UNUSED when this is added */
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlNsCheckScope:
Packit 423ecb
 * @node: the node
Packit 423ecb
 * @ns: the namespace node
Packit 423ecb
 *
Packit 423ecb
 * Check that a given namespace is in scope on a node.
Packit 423ecb
 *
Packit 423ecb
 * Returns 1 if in scope, -1 in case of argument error,
Packit 423ecb
 *         -2 if the namespace is not in scope, and -3 if not on
Packit 423ecb
 *         an ancestor node.
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
xmlNsCheckScope(xmlNodePtr node, xmlNsPtr ns)
Packit 423ecb
{
Packit 423ecb
    xmlNsPtr cur;
Packit 423ecb
Packit 423ecb
    if ((node == NULL) || (ns == NULL))
Packit 423ecb
        return(-1);
Packit 423ecb
Packit 423ecb
    if ((node->type != XML_ELEMENT_NODE) &&
Packit 423ecb
	(node->type != XML_ATTRIBUTE_NODE) &&
Packit 423ecb
	(node->type != XML_DOCUMENT_NODE) &&
Packit 423ecb
	(node->type != XML_TEXT_NODE) &&
Packit 423ecb
	(node->type != XML_HTML_DOCUMENT_NODE) &&
Packit 423ecb
	(node->type != XML_XINCLUDE_START))
Packit 423ecb
	return(-2);
Packit 423ecb
Packit 423ecb
    while ((node != NULL) &&
Packit 423ecb
           ((node->type == XML_ELEMENT_NODE) ||
Packit 423ecb
            (node->type == XML_ATTRIBUTE_NODE) ||
Packit 423ecb
            (node->type == XML_TEXT_NODE) ||
Packit 423ecb
	    (node->type == XML_XINCLUDE_START))) {
Packit 423ecb
	if ((node->type == XML_ELEMENT_NODE) ||
Packit 423ecb
	    (node->type == XML_XINCLUDE_START)) {
Packit 423ecb
	    cur = node->nsDef;
Packit 423ecb
	    while (cur != NULL) {
Packit 423ecb
	        if (cur == ns)
Packit 423ecb
		    return(1);
Packit 423ecb
		if (xmlStrEqual(cur->prefix, ns->prefix))
Packit 423ecb
		    return(-2);
Packit 423ecb
		cur = cur->next;
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
	node = node->parent;
Packit 423ecb
    }
Packit 423ecb
    /* the xml namespace may be declared on the document node */
Packit 423ecb
    if ((node != NULL) &&
Packit 423ecb
        ((node->type == XML_DOCUMENT_NODE) ||
Packit 423ecb
	 (node->type == XML_HTML_DOCUMENT_NODE))) {
Packit 423ecb
	 xmlNsPtr oldNs = ((xmlDocPtr) node)->oldNs;
Packit 423ecb
	 if (oldNs == ns)
Packit 423ecb
	     return(1);
Packit 423ecb
    }
Packit 423ecb
    return(-3);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpSpaces(xmlDebugCtxtPtr ctxt)
Packit 423ecb
{
Packit 423ecb
    if (ctxt->check)
Packit 423ecb
        return;
Packit 423ecb
    if ((ctxt->output != NULL) && (ctxt->depth > 0)) {
Packit 423ecb
        if (ctxt->depth < 50)
Packit 423ecb
            fprintf(ctxt->output, "%s", &ctxt->shift[100 - 2 * ctxt->depth]);
Packit 423ecb
        else
Packit 423ecb
            fprintf(ctxt->output, "%s", ctxt->shift);
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDebugErr:
Packit 423ecb
 * @ctxt:  a debug context
Packit 423ecb
 * @error:  the error code
Packit 423ecb
 *
Packit 423ecb
 * Handle a debug error.
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlDebugErr(xmlDebugCtxtPtr ctxt, int error, const char *msg)
Packit 423ecb
{
Packit 423ecb
    ctxt->errors++;
Packit 423ecb
    __xmlRaiseError(NULL, NULL, NULL,
Packit 423ecb
		    NULL, ctxt->node, XML_FROM_CHECK,
Packit 423ecb
		    error, XML_ERR_ERROR, NULL, 0,
Packit 423ecb
		    NULL, NULL, NULL, 0, 0,
Packit 423ecb
		    "%s", msg);
Packit 423ecb
}
Packit 423ecb
static void LIBXML_ATTR_FORMAT(3,0)
Packit 423ecb
xmlDebugErr2(xmlDebugCtxtPtr ctxt, int error, const char *msg, int extra)
Packit 423ecb
{
Packit 423ecb
    ctxt->errors++;
Packit 423ecb
    __xmlRaiseError(NULL, NULL, NULL,
Packit 423ecb
		    NULL, ctxt->node, XML_FROM_CHECK,
Packit 423ecb
		    error, XML_ERR_ERROR, NULL, 0,
Packit 423ecb
		    NULL, NULL, NULL, 0, 0,
Packit 423ecb
		    msg, extra);
Packit 423ecb
}
Packit 423ecb
static void LIBXML_ATTR_FORMAT(3,0)
Packit 423ecb
xmlDebugErr3(xmlDebugCtxtPtr ctxt, int error, const char *msg, const char *extra)
Packit 423ecb
{
Packit 423ecb
    ctxt->errors++;
Packit 423ecb
    __xmlRaiseError(NULL, NULL, NULL,
Packit 423ecb
		    NULL, ctxt->node, XML_FROM_CHECK,
Packit 423ecb
		    error, XML_ERR_ERROR, NULL, 0,
Packit 423ecb
		    NULL, NULL, NULL, 0, 0,
Packit 423ecb
		    msg, extra);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCtxtNsCheckScope:
Packit 423ecb
 * @ctxt: the debugging context
Packit 423ecb
 * @node: the node
Packit 423ecb
 * @ns: the namespace node
Packit 423ecb
 *
Packit 423ecb
 * Report if a given namespace is is not in scope.
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlCtxtNsCheckScope(xmlDebugCtxtPtr ctxt, xmlNodePtr node, xmlNsPtr ns)
Packit 423ecb
{
Packit 423ecb
    int ret;
Packit 423ecb
Packit 423ecb
    ret = xmlNsCheckScope(node, ns);
Packit 423ecb
    if (ret == -2) {
Packit 423ecb
        if (ns->prefix == NULL)
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_NS_SCOPE,
Packit 423ecb
			"Reference to default namespace not in scope\n");
Packit 423ecb
	else
Packit 423ecb
	    xmlDebugErr3(ctxt, XML_CHECK_NS_SCOPE,
Packit 423ecb
			 "Reference to namespace '%s' not in scope\n",
Packit 423ecb
			 (char *) ns->prefix);
Packit 423ecb
    }
Packit 423ecb
    if (ret == -3) {
Packit 423ecb
        if (ns->prefix == NULL)
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_NS_ANCESTOR,
Packit 423ecb
			"Reference to default namespace not on ancestor\n");
Packit 423ecb
	else
Packit 423ecb
	    xmlDebugErr3(ctxt, XML_CHECK_NS_ANCESTOR,
Packit 423ecb
			 "Reference to namespace '%s' not on ancestor\n",
Packit 423ecb
			 (char *) ns->prefix);
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCtxtCheckString:
Packit 423ecb
 * @ctxt: the debug context
Packit 423ecb
 * @str: the string
Packit 423ecb
 *
Packit 423ecb
 * Do debugging on the string, currently it just checks the UTF-8 content
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlCtxtCheckString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
Packit 423ecb
{
Packit 423ecb
    if (str == NULL) return;
Packit 423ecb
    if (ctxt->check) {
Packit 423ecb
        if (!xmlCheckUTF8(str)) {
Packit 423ecb
	    xmlDebugErr3(ctxt, XML_CHECK_NOT_UTF8,
Packit 423ecb
			 "String is not UTF-8 %s", (const char *) str);
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCtxtCheckName:
Packit 423ecb
 * @ctxt: the debug context
Packit 423ecb
 * @name: the name
Packit 423ecb
 *
Packit 423ecb
 * Do debugging on the name, for example the dictionary status and
Packit 423ecb
 * conformance to the Name production.
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlCtxtCheckName(xmlDebugCtxtPtr ctxt, const xmlChar * name)
Packit 423ecb
{
Packit 423ecb
    if (ctxt->check) {
Packit 423ecb
	if (name == NULL) {
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_NO_NAME, "Name is NULL");
Packit 423ecb
	    return;
Packit 423ecb
	}
Packit 423ecb
#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Packit 423ecb
        if (xmlValidateName(name, 0)) {
Packit 423ecb
	    xmlDebugErr3(ctxt, XML_CHECK_NOT_NCNAME,
Packit 423ecb
			 "Name is not an NCName '%s'", (const char *) name);
Packit 423ecb
	}
Packit 423ecb
#endif
Packit 423ecb
	if ((ctxt->dict != NULL) &&
Packit 423ecb
	    (!xmlDictOwns(ctxt->dict, name)) &&
Packit 423ecb
            ((ctxt->doc == NULL) ||
Packit 423ecb
             ((ctxt->doc->parseFlags & (XML_PARSE_SAX1 | XML_PARSE_NODICT)) == 0))) {
Packit 423ecb
	    xmlDebugErr3(ctxt, XML_CHECK_OUTSIDE_DICT,
Packit 423ecb
			 "Name is not from the document dictionary '%s'",
Packit 423ecb
			 (const char *) name);
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
xmlCtxtGenericNodeCheck(xmlDebugCtxtPtr ctxt, xmlNodePtr node) {
Packit 423ecb
    xmlDocPtr doc;
Packit 423ecb
    xmlDictPtr dict;
Packit 423ecb
Packit 423ecb
    doc = node->doc;
Packit 423ecb
Packit 423ecb
    if (node->parent == NULL)
Packit 423ecb
        xmlDebugErr(ctxt, XML_CHECK_NO_PARENT,
Packit 423ecb
	            "Node has no parent\n");
Packit 423ecb
    if (node->doc == NULL) {
Packit 423ecb
        xmlDebugErr(ctxt, XML_CHECK_NO_DOC,
Packit 423ecb
	            "Node has no doc\n");
Packit 423ecb
        dict = NULL;
Packit 423ecb
    } else {
Packit 423ecb
	dict = doc->dict;
Packit 423ecb
	if ((dict == NULL) && (ctxt->nodict == 0)) {
Packit 423ecb
#if 0
Packit 423ecb
            /* desactivated right now as it raises too many errors */
Packit 423ecb
	    if (doc->type == XML_DOCUMENT_NODE)
Packit 423ecb
		xmlDebugErr(ctxt, XML_CHECK_NO_DICT,
Packit 423ecb
			    "Document has no dictionary\n");
Packit 423ecb
#endif
Packit 423ecb
	    ctxt->nodict = 1;
Packit 423ecb
	}
Packit 423ecb
	if (ctxt->doc == NULL)
Packit 423ecb
	    ctxt->doc = doc;
Packit 423ecb
Packit 423ecb
	if (ctxt->dict == NULL) {
Packit 423ecb
	    ctxt->dict = dict;
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    if ((node->parent != NULL) && (node->doc != node->parent->doc) &&
Packit 423ecb
        (!xmlStrEqual(node->name, BAD_CAST "pseudoroot")))
Packit 423ecb
        xmlDebugErr(ctxt, XML_CHECK_WRONG_DOC,
Packit 423ecb
	            "Node doc differs from parent's one\n");
Packit 423ecb
    if (node->prev == NULL) {
Packit 423ecb
        if (node->type == XML_ATTRIBUTE_NODE) {
Packit 423ecb
	    if ((node->parent != NULL) &&
Packit 423ecb
	        (node != (xmlNodePtr) node->parent->properties))
Packit 423ecb
		xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
Packit 423ecb
                    "Attr has no prev and not first of attr list\n");
Packit 423ecb
Packit 423ecb
        } else if ((node->parent != NULL) && (node->parent->children != node))
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
Packit 423ecb
                    "Node has no prev and not first of parent list\n");
Packit 423ecb
    } else {
Packit 423ecb
        if (node->prev->next != node)
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_WRONG_PREV,
Packit 423ecb
                        "Node prev->next : back link wrong\n");
Packit 423ecb
    }
Packit 423ecb
    if (node->next == NULL) {
Packit 423ecb
	if ((node->parent != NULL) && (node->type != XML_ATTRIBUTE_NODE) &&
Packit 423ecb
	    (node->parent->last != node) &&
Packit 423ecb
	    (node->parent->type == XML_ELEMENT_NODE))
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_NO_NEXT,
Packit 423ecb
                    "Node has no next and not last of parent list\n");
Packit 423ecb
    } else {
Packit 423ecb
        if (node->next->prev != node)
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_WRONG_NEXT,
Packit 423ecb
                    "Node next->prev : forward link wrong\n");
Packit 423ecb
        if (node->next->parent != node->parent)
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_WRONG_PARENT,
Packit 423ecb
                    "Node next->prev : forward link wrong\n");
Packit 423ecb
    }
Packit 423ecb
    if (node->type == XML_ELEMENT_NODE) {
Packit 423ecb
        xmlNsPtr ns;
Packit 423ecb
Packit 423ecb
	ns = node->nsDef;
Packit 423ecb
	while (ns != NULL) {
Packit 423ecb
	    xmlCtxtNsCheckScope(ctxt, node, ns);
Packit 423ecb
	    ns = ns->next;
Packit 423ecb
	}
Packit 423ecb
	if (node->ns != NULL)
Packit 423ecb
	    xmlCtxtNsCheckScope(ctxt, node, node->ns);
Packit 423ecb
    } else if (node->type == XML_ATTRIBUTE_NODE) {
Packit 423ecb
	if (node->ns != NULL)
Packit 423ecb
	    xmlCtxtNsCheckScope(ctxt, node, node->ns);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    if ((node->type != XML_ELEMENT_NODE) &&
Packit 423ecb
	(node->type != XML_ATTRIBUTE_NODE) &&
Packit 423ecb
	(node->type != XML_ELEMENT_DECL) &&
Packit 423ecb
	(node->type != XML_ATTRIBUTE_DECL) &&
Packit 423ecb
	(node->type != XML_DTD_NODE) &&
Packit 423ecb
	(node->type != XML_HTML_DOCUMENT_NODE) &&
Packit 423ecb
	(node->type != XML_DOCUMENT_NODE)) {
Packit 423ecb
	if (node->content != NULL)
Packit 423ecb
	    xmlCtxtCheckString(ctxt, (const xmlChar *) node->content);
Packit 423ecb
    }
Packit 423ecb
    switch (node->type) {
Packit 423ecb
        case XML_ELEMENT_NODE:
Packit 423ecb
        case XML_ATTRIBUTE_NODE:
Packit 423ecb
	    xmlCtxtCheckName(ctxt, node->name);
Packit 423ecb
	    break;
Packit 423ecb
        case XML_TEXT_NODE:
Packit 423ecb
	    if ((node->name == xmlStringText) ||
Packit 423ecb
	        (node->name == xmlStringTextNoenc))
Packit 423ecb
		break;
Packit 423ecb
	    /* some case of entity substitution can lead to this */
Packit 423ecb
	    if ((ctxt->dict != NULL) &&
Packit 423ecb
	        (node->name == xmlDictLookup(ctxt->dict, BAD_CAST "nbktext",
Packit 423ecb
		                             7)))
Packit 423ecb
		break;
Packit 423ecb
Packit 423ecb
	    xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
Packit 423ecb
			 "Text node has wrong name '%s'",
Packit 423ecb
			 (const char *) node->name);
Packit 423ecb
	    break;
Packit 423ecb
        case XML_COMMENT_NODE:
Packit 423ecb
	    if (node->name == xmlStringComment)
Packit 423ecb
		break;
Packit 423ecb
	    xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
Packit 423ecb
			 "Comment node has wrong name '%s'",
Packit 423ecb
			 (const char *) node->name);
Packit 423ecb
	    break;
Packit 423ecb
        case XML_PI_NODE:
Packit 423ecb
	    xmlCtxtCheckName(ctxt, node->name);
Packit 423ecb
	    break;
Packit 423ecb
        case XML_CDATA_SECTION_NODE:
Packit 423ecb
	    if (node->name == NULL)
Packit 423ecb
		break;
Packit 423ecb
	    xmlDebugErr3(ctxt, XML_CHECK_NAME_NOT_NULL,
Packit 423ecb
			 "CData section has non NULL name '%s'",
Packit 423ecb
			 (const char *) node->name);
Packit 423ecb
	    break;
Packit 423ecb
        case XML_ENTITY_REF_NODE:
Packit 423ecb
        case XML_ENTITY_NODE:
Packit 423ecb
        case XML_DOCUMENT_TYPE_NODE:
Packit 423ecb
        case XML_DOCUMENT_FRAG_NODE:
Packit 423ecb
        case XML_NOTATION_NODE:
Packit 423ecb
        case XML_DTD_NODE:
Packit 423ecb
        case XML_ELEMENT_DECL:
Packit 423ecb
        case XML_ATTRIBUTE_DECL:
Packit 423ecb
        case XML_ENTITY_DECL:
Packit 423ecb
        case XML_NAMESPACE_DECL:
Packit 423ecb
        case XML_XINCLUDE_START:
Packit 423ecb
        case XML_XINCLUDE_END:
Packit 423ecb
#ifdef LIBXML_DOCB_ENABLED
Packit 423ecb
        case XML_DOCB_DOCUMENT_NODE:
Packit 423ecb
#endif
Packit 423ecb
        case XML_DOCUMENT_NODE:
Packit 423ecb
        case XML_HTML_DOCUMENT_NODE:
Packit 423ecb
	    break;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
Packit 423ecb
{
Packit 423ecb
    int i;
Packit 423ecb
Packit 423ecb
    if (ctxt->check) {
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    /* TODO: check UTF8 content of the string */
Packit 423ecb
    if (str == NULL) {
Packit 423ecb
        fprintf(ctxt->output, "(NULL)");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    for (i = 0; i < 40; i++)
Packit 423ecb
        if (str[i] == 0)
Packit 423ecb
            return;
Packit 423ecb
        else if (IS_BLANK_CH(str[i]))
Packit 423ecb
            fputc(' ', ctxt->output);
Packit 423ecb
        else if (str[i] >= 0x80)
Packit 423ecb
            fprintf(ctxt->output, "#%X", str[i]);
Packit 423ecb
        else
Packit 423ecb
            fputc(str[i], ctxt->output);
Packit 423ecb
    fprintf(ctxt->output, "...");
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
Packit 423ecb
{
Packit 423ecb
    xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
Packit 423ecb
    if (dtd == NULL) {
Packit 423ecb
        if (!ctxt->check)
Packit 423ecb
            fprintf(ctxt->output, "DTD node is NULL\n");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    if (dtd->type != XML_DTD_NODE) {
Packit 423ecb
	xmlDebugErr(ctxt, XML_CHECK_NOT_DTD,
Packit 423ecb
	            "Node is not a DTD");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    if (!ctxt->check) {
Packit 423ecb
        if (dtd->name != NULL)
Packit 423ecb
            fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name);
Packit 423ecb
        else
Packit 423ecb
            fprintf(ctxt->output, "DTD");
Packit 423ecb
        if (dtd->ExternalID != NULL)
Packit 423ecb
            fprintf(ctxt->output, ", PUBLIC %s", (char *) dtd->ExternalID);
Packit 423ecb
        if (dtd->SystemID != NULL)
Packit 423ecb
            fprintf(ctxt->output, ", SYSTEM %s", (char *) dtd->SystemID);
Packit 423ecb
        fprintf(ctxt->output, "\n");
Packit 423ecb
    }
Packit 423ecb
    /*
Packit 423ecb
     * Do a bit of checking
Packit 423ecb
     */
Packit 423ecb
    xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr)
Packit 423ecb
{
Packit 423ecb
    xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
Packit 423ecb
    if (attr == NULL) {
Packit 423ecb
        if (!ctxt->check)
Packit 423ecb
            fprintf(ctxt->output, "Attribute declaration is NULL\n");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    if (attr->type != XML_ATTRIBUTE_DECL) {
Packit 423ecb
	xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL,
Packit 423ecb
	            "Node is not an attribute declaration");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    if (attr->name != NULL) {
Packit 423ecb
        if (!ctxt->check)
Packit 423ecb
            fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name);
Packit 423ecb
    } else
Packit 423ecb
	xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
Packit 423ecb
	            "Node attribute declaration has no name");
Packit 423ecb
    if (attr->elem != NULL) {
Packit 423ecb
        if (!ctxt->check)
Packit 423ecb
            fprintf(ctxt->output, " for %s", (char *) attr->elem);
Packit 423ecb
    } else
Packit 423ecb
	xmlDebugErr(ctxt, XML_CHECK_NO_ELEM,
Packit 423ecb
	            "Node attribute declaration has no element name");
Packit 423ecb
    if (!ctxt->check) {
Packit 423ecb
        switch (attr->atype) {
Packit 423ecb
            case XML_ATTRIBUTE_CDATA:
Packit 423ecb
                fprintf(ctxt->output, " CDATA");
Packit 423ecb
                break;
Packit 423ecb
            case XML_ATTRIBUTE_ID:
Packit 423ecb
                fprintf(ctxt->output, " ID");
Packit 423ecb
                break;
Packit 423ecb
            case XML_ATTRIBUTE_IDREF:
Packit 423ecb
                fprintf(ctxt->output, " IDREF");
Packit 423ecb
                break;
Packit 423ecb
            case XML_ATTRIBUTE_IDREFS:
Packit 423ecb
                fprintf(ctxt->output, " IDREFS");
Packit 423ecb
                break;
Packit 423ecb
            case XML_ATTRIBUTE_ENTITY:
Packit 423ecb
                fprintf(ctxt->output, " ENTITY");
Packit 423ecb
                break;
Packit 423ecb
            case XML_ATTRIBUTE_ENTITIES:
Packit 423ecb
                fprintf(ctxt->output, " ENTITIES");
Packit 423ecb
                break;
Packit 423ecb
            case XML_ATTRIBUTE_NMTOKEN:
Packit 423ecb
                fprintf(ctxt->output, " NMTOKEN");
Packit 423ecb
                break;
Packit 423ecb
            case XML_ATTRIBUTE_NMTOKENS:
Packit 423ecb
                fprintf(ctxt->output, " NMTOKENS");
Packit 423ecb
                break;
Packit 423ecb
            case XML_ATTRIBUTE_ENUMERATION:
Packit 423ecb
                fprintf(ctxt->output, " ENUMERATION");
Packit 423ecb
                break;
Packit 423ecb
            case XML_ATTRIBUTE_NOTATION:
Packit 423ecb
                fprintf(ctxt->output, " NOTATION ");
Packit 423ecb
                break;
Packit 423ecb
        }
Packit 423ecb
        if (attr->tree != NULL) {
Packit 423ecb
            int indx;
Packit 423ecb
            xmlEnumerationPtr cur = attr->tree;
Packit 423ecb
Packit 423ecb
            for (indx = 0; indx < 5; indx++) {
Packit 423ecb
                if (indx != 0)
Packit 423ecb
                    fprintf(ctxt->output, "|%s", (char *) cur->name);
Packit 423ecb
                else
Packit 423ecb
                    fprintf(ctxt->output, " (%s", (char *) cur->name);
Packit 423ecb
                cur = cur->next;
Packit 423ecb
                if (cur == NULL)
Packit 423ecb
                    break;
Packit 423ecb
            }
Packit 423ecb
            if (cur == NULL)
Packit 423ecb
                fprintf(ctxt->output, ")");
Packit 423ecb
            else
Packit 423ecb
                fprintf(ctxt->output, "...)");
Packit 423ecb
        }
Packit 423ecb
        switch (attr->def) {
Packit 423ecb
            case XML_ATTRIBUTE_NONE:
Packit 423ecb
                break;
Packit 423ecb
            case XML_ATTRIBUTE_REQUIRED:
Packit 423ecb
                fprintf(ctxt->output, " REQUIRED");
Packit 423ecb
                break;
Packit 423ecb
            case XML_ATTRIBUTE_IMPLIED:
Packit 423ecb
                fprintf(ctxt->output, " IMPLIED");
Packit 423ecb
                break;
Packit 423ecb
            case XML_ATTRIBUTE_FIXED:
Packit 423ecb
                fprintf(ctxt->output, " FIXED");
Packit 423ecb
                break;
Packit 423ecb
        }
Packit 423ecb
        if (attr->defaultValue != NULL) {
Packit 423ecb
            fprintf(ctxt->output, "\"");
Packit 423ecb
            xmlCtxtDumpString(ctxt, attr->defaultValue);
Packit 423ecb
            fprintf(ctxt->output, "\"");
Packit 423ecb
        }
Packit 423ecb
        fprintf(ctxt->output, "\n");
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * Do a bit of checking
Packit 423ecb
     */
Packit 423ecb
    xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem)
Packit 423ecb
{
Packit 423ecb
    xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
Packit 423ecb
    if (elem == NULL) {
Packit 423ecb
        if (!ctxt->check)
Packit 423ecb
            fprintf(ctxt->output, "Element declaration is NULL\n");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    if (elem->type != XML_ELEMENT_DECL) {
Packit 423ecb
	xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL,
Packit 423ecb
	            "Node is not an element declaration");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    if (elem->name != NULL) {
Packit 423ecb
        if (!ctxt->check) {
Packit 423ecb
            fprintf(ctxt->output, "ELEMDECL(");
Packit 423ecb
            xmlCtxtDumpString(ctxt, elem->name);
Packit 423ecb
            fprintf(ctxt->output, ")");
Packit 423ecb
        }
Packit 423ecb
    } else
Packit 423ecb
	xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
Packit 423ecb
	            "Element declaration has no name");
Packit 423ecb
    if (!ctxt->check) {
Packit 423ecb
        switch (elem->etype) {
Packit 423ecb
            case XML_ELEMENT_TYPE_UNDEFINED:
Packit 423ecb
                fprintf(ctxt->output, ", UNDEFINED");
Packit 423ecb
                break;
Packit 423ecb
            case XML_ELEMENT_TYPE_EMPTY:
Packit 423ecb
                fprintf(ctxt->output, ", EMPTY");
Packit 423ecb
                break;
Packit 423ecb
            case XML_ELEMENT_TYPE_ANY:
Packit 423ecb
                fprintf(ctxt->output, ", ANY");
Packit 423ecb
                break;
Packit 423ecb
            case XML_ELEMENT_TYPE_MIXED:
Packit 423ecb
                fprintf(ctxt->output, ", MIXED ");
Packit 423ecb
                break;
Packit 423ecb
            case XML_ELEMENT_TYPE_ELEMENT:
Packit 423ecb
                fprintf(ctxt->output, ", MIXED ");
Packit 423ecb
                break;
Packit 423ecb
        }
Packit 423ecb
        if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) {
Packit 423ecb
            char buf[5001];
Packit 423ecb
Packit 423ecb
            buf[0] = 0;
Packit 423ecb
            xmlSnprintfElementContent(buf, 5000, elem->content, 1);
Packit 423ecb
            buf[5000] = 0;
Packit 423ecb
            fprintf(ctxt->output, "%s", buf);
Packit 423ecb
        }
Packit 423ecb
        fprintf(ctxt->output, "\n");
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * Do a bit of checking
Packit 423ecb
     */
Packit 423ecb
    xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
Packit 423ecb
{
Packit 423ecb
    xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
Packit 423ecb
    if (ent == NULL) {
Packit 423ecb
        if (!ctxt->check)
Packit 423ecb
            fprintf(ctxt->output, "Entity declaration is NULL\n");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    if (ent->type != XML_ENTITY_DECL) {
Packit 423ecb
	xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL,
Packit 423ecb
	            "Node is not an entity declaration");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    if (ent->name != NULL) {
Packit 423ecb
        if (!ctxt->check) {
Packit 423ecb
            fprintf(ctxt->output, "ENTITYDECL(");
Packit 423ecb
            xmlCtxtDumpString(ctxt, ent->name);
Packit 423ecb
            fprintf(ctxt->output, ")");
Packit 423ecb
        }
Packit 423ecb
    } else
Packit 423ecb
	xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
Packit 423ecb
	            "Entity declaration has no name");
Packit 423ecb
    if (!ctxt->check) {
Packit 423ecb
        switch (ent->etype) {
Packit 423ecb
            case XML_INTERNAL_GENERAL_ENTITY:
Packit 423ecb
                fprintf(ctxt->output, ", internal\n");
Packit 423ecb
                break;
Packit 423ecb
            case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
Packit 423ecb
                fprintf(ctxt->output, ", external parsed\n");
Packit 423ecb
                break;
Packit 423ecb
            case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
Packit 423ecb
                fprintf(ctxt->output, ", unparsed\n");
Packit 423ecb
                break;
Packit 423ecb
            case XML_INTERNAL_PARAMETER_ENTITY:
Packit 423ecb
                fprintf(ctxt->output, ", parameter\n");
Packit 423ecb
                break;
Packit 423ecb
            case XML_EXTERNAL_PARAMETER_ENTITY:
Packit 423ecb
                fprintf(ctxt->output, ", external parameter\n");
Packit 423ecb
                break;
Packit 423ecb
            case XML_INTERNAL_PREDEFINED_ENTITY:
Packit 423ecb
                fprintf(ctxt->output, ", predefined\n");
Packit 423ecb
                break;
Packit 423ecb
        }
Packit 423ecb
        if (ent->ExternalID) {
Packit 423ecb
            xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
            fprintf(ctxt->output, " ExternalID=%s\n",
Packit 423ecb
                    (char *) ent->ExternalID);
Packit 423ecb
        }
Packit 423ecb
        if (ent->SystemID) {
Packit 423ecb
            xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
            fprintf(ctxt->output, " SystemID=%s\n",
Packit 423ecb
                    (char *) ent->SystemID);
Packit 423ecb
        }
Packit 423ecb
        if (ent->URI != NULL) {
Packit 423ecb
            xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
            fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI);
Packit 423ecb
        }
Packit 423ecb
        if (ent->content) {
Packit 423ecb
            xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
            fprintf(ctxt->output, " content=");
Packit 423ecb
            xmlCtxtDumpString(ctxt, ent->content);
Packit 423ecb
            fprintf(ctxt->output, "\n");
Packit 423ecb
        }
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * Do a bit of checking
Packit 423ecb
     */
Packit 423ecb
    xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
Packit 423ecb
{
Packit 423ecb
    xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
Packit 423ecb
    if (ns == NULL) {
Packit 423ecb
        if (!ctxt->check)
Packit 423ecb
            fprintf(ctxt->output, "namespace node is NULL\n");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    if (ns->type != XML_NAMESPACE_DECL) {
Packit 423ecb
	xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL,
Packit 423ecb
	            "Node is not a namespace declaration");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    if (ns->href == NULL) {
Packit 423ecb
        if (ns->prefix != NULL)
Packit 423ecb
	    xmlDebugErr3(ctxt, XML_CHECK_NO_HREF,
Packit 423ecb
                    "Incomplete namespace %s href=NULL\n",
Packit 423ecb
                    (char *) ns->prefix);
Packit 423ecb
        else
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_NO_HREF,
Packit 423ecb
                    "Incomplete default namespace href=NULL\n");
Packit 423ecb
    } else {
Packit 423ecb
        if (!ctxt->check) {
Packit 423ecb
            if (ns->prefix != NULL)
Packit 423ecb
                fprintf(ctxt->output, "namespace %s href=",
Packit 423ecb
                        (char *) ns->prefix);
Packit 423ecb
            else
Packit 423ecb
                fprintf(ctxt->output, "default namespace href=");
Packit 423ecb
Packit 423ecb
            xmlCtxtDumpString(ctxt, ns->href);
Packit 423ecb
            fprintf(ctxt->output, "\n");
Packit 423ecb
        }
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
Packit 423ecb
{
Packit 423ecb
    while (ns != NULL) {
Packit 423ecb
        xmlCtxtDumpNamespace(ctxt, ns);
Packit 423ecb
        ns = ns->next;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
Packit 423ecb
{
Packit 423ecb
    xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
Packit 423ecb
    if (ent == NULL) {
Packit 423ecb
        if (!ctxt->check)
Packit 423ecb
            fprintf(ctxt->output, "Entity is NULL\n");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    if (!ctxt->check) {
Packit 423ecb
        switch (ent->etype) {
Packit 423ecb
            case XML_INTERNAL_GENERAL_ENTITY:
Packit 423ecb
                fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY ");
Packit 423ecb
                break;
Packit 423ecb
            case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
Packit 423ecb
                fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
Packit 423ecb
                break;
Packit 423ecb
            case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
Packit 423ecb
                fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
Packit 423ecb
                break;
Packit 423ecb
            case XML_INTERNAL_PARAMETER_ENTITY:
Packit 423ecb
                fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY ");
Packit 423ecb
                break;
Packit 423ecb
            case XML_EXTERNAL_PARAMETER_ENTITY:
Packit 423ecb
                fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY ");
Packit 423ecb
                break;
Packit 423ecb
            default:
Packit 423ecb
                fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype);
Packit 423ecb
        }
Packit 423ecb
        fprintf(ctxt->output, "%s\n", ent->name);
Packit 423ecb
        if (ent->ExternalID) {
Packit 423ecb
            xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
            fprintf(ctxt->output, "ExternalID=%s\n",
Packit 423ecb
                    (char *) ent->ExternalID);
Packit 423ecb
        }
Packit 423ecb
        if (ent->SystemID) {
Packit 423ecb
            xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
            fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID);
Packit 423ecb
        }
Packit 423ecb
        if (ent->URI) {
Packit 423ecb
            xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
            fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI);
Packit 423ecb
        }
Packit 423ecb
        if (ent->content) {
Packit 423ecb
            xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
            fprintf(ctxt->output, "content=");
Packit 423ecb
            xmlCtxtDumpString(ctxt, ent->content);
Packit 423ecb
            fprintf(ctxt->output, "\n");
Packit 423ecb
        }
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCtxtDumpAttr:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @attr:  the attribute
Packit 423ecb
 * @depth:  the indentation level.
Packit 423ecb
 *
Packit 423ecb
 * Dumps debug information for the attribute
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
Packit 423ecb
{
Packit 423ecb
    xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
Packit 423ecb
    if (attr == NULL) {
Packit 423ecb
        if (!ctxt->check)
Packit 423ecb
            fprintf(ctxt->output, "Attr is NULL");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    if (!ctxt->check) {
Packit 423ecb
        fprintf(ctxt->output, "ATTRIBUTE ");
Packit 423ecb
	xmlCtxtDumpString(ctxt, attr->name);
Packit 423ecb
        fprintf(ctxt->output, "\n");
Packit 423ecb
        if (attr->children != NULL) {
Packit 423ecb
            ctxt->depth++;
Packit 423ecb
            xmlCtxtDumpNodeList(ctxt, attr->children);
Packit 423ecb
            ctxt->depth--;
Packit 423ecb
        }
Packit 423ecb
    }
Packit 423ecb
    if (attr->name == NULL)
Packit 423ecb
	xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
Packit 423ecb
	            "Attribute has no name");
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * Do a bit of checking
Packit 423ecb
     */
Packit 423ecb
    xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCtxtDumpAttrList:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @attr:  the attribute list
Packit 423ecb
 * @depth:  the indentation level.
Packit 423ecb
 *
Packit 423ecb
 * Dumps debug information for the attribute list
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
Packit 423ecb
{
Packit 423ecb
    while (attr != NULL) {
Packit 423ecb
        xmlCtxtDumpAttr(ctxt, attr);
Packit 423ecb
        attr = attr->next;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCtxtDumpOneNode:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @node:  the node
Packit 423ecb
 * @depth:  the indentation level.
Packit 423ecb
 *
Packit 423ecb
 * Dumps debug information for the element node, it is not recursive
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
Packit 423ecb
{
Packit 423ecb
    if (node == NULL) {
Packit 423ecb
        if (!ctxt->check) {
Packit 423ecb
            xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
            fprintf(ctxt->output, "node is NULL\n");
Packit 423ecb
        }
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    ctxt->node = node;
Packit 423ecb
Packit 423ecb
    switch (node->type) {
Packit 423ecb
        case XML_ELEMENT_NODE:
Packit 423ecb
            if (!ctxt->check) {
Packit 423ecb
                xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
                fprintf(ctxt->output, "ELEMENT ");
Packit 423ecb
                if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
Packit 423ecb
                    xmlCtxtDumpString(ctxt, node->ns->prefix);
Packit 423ecb
                    fprintf(ctxt->output, ":");
Packit 423ecb
                }
Packit 423ecb
                xmlCtxtDumpString(ctxt, node->name);
Packit 423ecb
                fprintf(ctxt->output, "\n");
Packit 423ecb
            }
Packit 423ecb
            break;
Packit 423ecb
        case XML_ATTRIBUTE_NODE:
Packit 423ecb
            if (!ctxt->check)
Packit 423ecb
                xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
            fprintf(ctxt->output, "Error, ATTRIBUTE found here\n");
Packit 423ecb
            xmlCtxtGenericNodeCheck(ctxt, node);
Packit 423ecb
            return;
Packit 423ecb
        case XML_TEXT_NODE:
Packit 423ecb
            if (!ctxt->check) {
Packit 423ecb
                xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
                if (node->name == (const xmlChar *) xmlStringTextNoenc)
Packit 423ecb
                    fprintf(ctxt->output, "TEXT no enc");
Packit 423ecb
                else
Packit 423ecb
                    fprintf(ctxt->output, "TEXT");
Packit 423ecb
		if (ctxt->options & DUMP_TEXT_TYPE) {
Packit 423ecb
		    if (node->content == (xmlChar *) &(node->properties))
Packit 423ecb
			fprintf(ctxt->output, " compact\n");
Packit 423ecb
		    else if (xmlDictOwns(ctxt->dict, node->content) == 1)
Packit 423ecb
			fprintf(ctxt->output, " interned\n");
Packit 423ecb
		    else
Packit 423ecb
			fprintf(ctxt->output, "\n");
Packit 423ecb
		} else
Packit 423ecb
		    fprintf(ctxt->output, "\n");
Packit 423ecb
            }
Packit 423ecb
            break;
Packit 423ecb
        case XML_CDATA_SECTION_NODE:
Packit 423ecb
            if (!ctxt->check) {
Packit 423ecb
                xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
                fprintf(ctxt->output, "CDATA_SECTION\n");
Packit 423ecb
            }
Packit 423ecb
            break;
Packit 423ecb
        case XML_ENTITY_REF_NODE:
Packit 423ecb
            if (!ctxt->check) {
Packit 423ecb
                xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
                fprintf(ctxt->output, "ENTITY_REF(%s)\n",
Packit 423ecb
                        (char *) node->name);
Packit 423ecb
            }
Packit 423ecb
            break;
Packit 423ecb
        case XML_ENTITY_NODE:
Packit 423ecb
            if (!ctxt->check) {
Packit 423ecb
                xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
                fprintf(ctxt->output, "ENTITY\n");
Packit 423ecb
            }
Packit 423ecb
            break;
Packit 423ecb
        case XML_PI_NODE:
Packit 423ecb
            if (!ctxt->check) {
Packit 423ecb
                xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
                fprintf(ctxt->output, "PI %s\n", (char *) node->name);
Packit 423ecb
            }
Packit 423ecb
            break;
Packit 423ecb
        case XML_COMMENT_NODE:
Packit 423ecb
            if (!ctxt->check) {
Packit 423ecb
                xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
                fprintf(ctxt->output, "COMMENT\n");
Packit 423ecb
            }
Packit 423ecb
            break;
Packit 423ecb
        case XML_DOCUMENT_NODE:
Packit 423ecb
        case XML_HTML_DOCUMENT_NODE:
Packit 423ecb
            if (!ctxt->check) {
Packit 423ecb
                xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
            }
Packit 423ecb
            fprintf(ctxt->output, "Error, DOCUMENT found here\n");
Packit 423ecb
            xmlCtxtGenericNodeCheck(ctxt, node);
Packit 423ecb
            return;
Packit 423ecb
        case XML_DOCUMENT_TYPE_NODE:
Packit 423ecb
            if (!ctxt->check) {
Packit 423ecb
                xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
                fprintf(ctxt->output, "DOCUMENT_TYPE\n");
Packit 423ecb
            }
Packit 423ecb
            break;
Packit 423ecb
        case XML_DOCUMENT_FRAG_NODE:
Packit 423ecb
            if (!ctxt->check) {
Packit 423ecb
                xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
                fprintf(ctxt->output, "DOCUMENT_FRAG\n");
Packit 423ecb
            }
Packit 423ecb
            break;
Packit 423ecb
        case XML_NOTATION_NODE:
Packit 423ecb
            if (!ctxt->check) {
Packit 423ecb
                xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
                fprintf(ctxt->output, "NOTATION\n");
Packit 423ecb
            }
Packit 423ecb
            break;
Packit 423ecb
        case XML_DTD_NODE:
Packit 423ecb
            xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
Packit 423ecb
            return;
Packit 423ecb
        case XML_ELEMENT_DECL:
Packit 423ecb
            xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
Packit 423ecb
            return;
Packit 423ecb
        case XML_ATTRIBUTE_DECL:
Packit 423ecb
            xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
Packit 423ecb
            return;
Packit 423ecb
        case XML_ENTITY_DECL:
Packit 423ecb
            xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
Packit 423ecb
            return;
Packit 423ecb
        case XML_NAMESPACE_DECL:
Packit 423ecb
            xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
Packit 423ecb
            return;
Packit 423ecb
        case XML_XINCLUDE_START:
Packit 423ecb
            if (!ctxt->check) {
Packit 423ecb
                xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
                fprintf(ctxt->output, "INCLUDE START\n");
Packit 423ecb
            }
Packit 423ecb
            return;
Packit 423ecb
        case XML_XINCLUDE_END:
Packit 423ecb
            if (!ctxt->check) {
Packit 423ecb
                xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
                fprintf(ctxt->output, "INCLUDE END\n");
Packit 423ecb
            }
Packit 423ecb
            return;
Packit 423ecb
        default:
Packit 423ecb
            if (!ctxt->check)
Packit 423ecb
                xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
	    xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
Packit 423ecb
	                "Unknown node type %d\n", node->type);
Packit 423ecb
            return;
Packit 423ecb
    }
Packit 423ecb
    if (node->doc == NULL) {
Packit 423ecb
        if (!ctxt->check) {
Packit 423ecb
            xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
        }
Packit 423ecb
        fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
Packit 423ecb
    }
Packit 423ecb
    ctxt->depth++;
Packit 423ecb
    if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL))
Packit 423ecb
        xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
Packit 423ecb
    if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL))
Packit 423ecb
        xmlCtxtDumpAttrList(ctxt, node->properties);
Packit 423ecb
    if (node->type != XML_ENTITY_REF_NODE) {
Packit 423ecb
        if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
Packit 423ecb
            if (!ctxt->check) {
Packit 423ecb
                xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
                fprintf(ctxt->output, "content=");
Packit 423ecb
                xmlCtxtDumpString(ctxt, node->content);
Packit 423ecb
                fprintf(ctxt->output, "\n");
Packit 423ecb
            }
Packit 423ecb
        }
Packit 423ecb
    } else {
Packit 423ecb
        xmlEntityPtr ent;
Packit 423ecb
Packit 423ecb
        ent = xmlGetDocEntity(node->doc, node->name);
Packit 423ecb
        if (ent != NULL)
Packit 423ecb
            xmlCtxtDumpEntity(ctxt, ent);
Packit 423ecb
    }
Packit 423ecb
    ctxt->depth--;
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * Do a bit of checking
Packit 423ecb
     */
Packit 423ecb
    xmlCtxtGenericNodeCheck(ctxt, node);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCtxtDumpNode:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @node:  the node
Packit 423ecb
 * @depth:  the indentation level.
Packit 423ecb
 *
Packit 423ecb
 * Dumps debug information for the element node, it is recursive
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
Packit 423ecb
{
Packit 423ecb
    if (node == NULL) {
Packit 423ecb
        if (!ctxt->check) {
Packit 423ecb
            xmlCtxtDumpSpaces(ctxt);
Packit 423ecb
            fprintf(ctxt->output, "node is NULL\n");
Packit 423ecb
        }
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    xmlCtxtDumpOneNode(ctxt, node);
Packit 423ecb
    if ((node->type != XML_NAMESPACE_DECL) &&
Packit 423ecb
        (node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
Packit 423ecb
        ctxt->depth++;
Packit 423ecb
        xmlCtxtDumpNodeList(ctxt, node->children);
Packit 423ecb
        ctxt->depth--;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCtxtDumpNodeList:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @node:  the node list
Packit 423ecb
 * @depth:  the indentation level.
Packit 423ecb
 *
Packit 423ecb
 * Dumps debug information for the list of element node, it is recursive
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
Packit 423ecb
{
Packit 423ecb
    while (node != NULL) {
Packit 423ecb
        xmlCtxtDumpNode(ctxt, node);
Packit 423ecb
        node = node->next;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
Packit 423ecb
{
Packit 423ecb
    if (doc == NULL) {
Packit 423ecb
        if (!ctxt->check)
Packit 423ecb
            fprintf(ctxt->output, "DOCUMENT == NULL !\n");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    ctxt->node = (xmlNodePtr) doc;
Packit 423ecb
Packit 423ecb
    switch (doc->type) {
Packit 423ecb
        case XML_ELEMENT_NODE:
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
Packit 423ecb
	                "Misplaced ELEMENT node\n");
Packit 423ecb
            break;
Packit 423ecb
        case XML_ATTRIBUTE_NODE:
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
Packit 423ecb
	                "Misplaced ATTRIBUTE node\n");
Packit 423ecb
            break;
Packit 423ecb
        case XML_TEXT_NODE:
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
Packit 423ecb
	                "Misplaced TEXT node\n");
Packit 423ecb
            break;
Packit 423ecb
        case XML_CDATA_SECTION_NODE:
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
Packit 423ecb
	                "Misplaced CDATA node\n");
Packit 423ecb
            break;
Packit 423ecb
        case XML_ENTITY_REF_NODE:
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
Packit 423ecb
	                "Misplaced ENTITYREF node\n");
Packit 423ecb
            break;
Packit 423ecb
        case XML_ENTITY_NODE:
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
Packit 423ecb
	                "Misplaced ENTITY node\n");
Packit 423ecb
            break;
Packit 423ecb
        case XML_PI_NODE:
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
Packit 423ecb
	                "Misplaced PI node\n");
Packit 423ecb
            break;
Packit 423ecb
        case XML_COMMENT_NODE:
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
Packit 423ecb
	                "Misplaced COMMENT node\n");
Packit 423ecb
            break;
Packit 423ecb
        case XML_DOCUMENT_NODE:
Packit 423ecb
	    if (!ctxt->check)
Packit 423ecb
		fprintf(ctxt->output, "DOCUMENT\n");
Packit 423ecb
            break;
Packit 423ecb
        case XML_HTML_DOCUMENT_NODE:
Packit 423ecb
	    if (!ctxt->check)
Packit 423ecb
		fprintf(ctxt->output, "HTML DOCUMENT\n");
Packit 423ecb
            break;
Packit 423ecb
        case XML_DOCUMENT_TYPE_NODE:
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
Packit 423ecb
	                "Misplaced DOCTYPE node\n");
Packit 423ecb
            break;
Packit 423ecb
        case XML_DOCUMENT_FRAG_NODE:
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
Packit 423ecb
	                "Misplaced FRAGMENT node\n");
Packit 423ecb
            break;
Packit 423ecb
        case XML_NOTATION_NODE:
Packit 423ecb
	    xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
Packit 423ecb
	                "Misplaced NOTATION node\n");
Packit 423ecb
            break;
Packit 423ecb
        default:
Packit 423ecb
	    xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
Packit 423ecb
	                "Unknown node type %d\n", doc->type);
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCtxtDumpDocumentHead:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @doc:  the document
Packit 423ecb
 *
Packit 423ecb
 * Dumps debug information cncerning the document, not recursive
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
Packit 423ecb
{
Packit 423ecb
    if (doc == NULL) return;
Packit 423ecb
    xmlCtxtDumpDocHead(ctxt, doc);
Packit 423ecb
    if (!ctxt->check) {
Packit 423ecb
        if (doc->name != NULL) {
Packit 423ecb
            fprintf(ctxt->output, "name=");
Packit 423ecb
            xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
Packit 423ecb
            fprintf(ctxt->output, "\n");
Packit 423ecb
        }
Packit 423ecb
        if (doc->version != NULL) {
Packit 423ecb
            fprintf(ctxt->output, "version=");
Packit 423ecb
            xmlCtxtDumpString(ctxt, doc->version);
Packit 423ecb
            fprintf(ctxt->output, "\n");
Packit 423ecb
        }
Packit 423ecb
        if (doc->encoding != NULL) {
Packit 423ecb
            fprintf(ctxt->output, "encoding=");
Packit 423ecb
            xmlCtxtDumpString(ctxt, doc->encoding);
Packit 423ecb
            fprintf(ctxt->output, "\n");
Packit 423ecb
        }
Packit 423ecb
        if (doc->URL != NULL) {
Packit 423ecb
            fprintf(ctxt->output, "URL=");
Packit 423ecb
            xmlCtxtDumpString(ctxt, doc->URL);
Packit 423ecb
            fprintf(ctxt->output, "\n");
Packit 423ecb
        }
Packit 423ecb
        if (doc->standalone)
Packit 423ecb
            fprintf(ctxt->output, "standalone=true\n");
Packit 423ecb
    }
Packit 423ecb
    if (doc->oldNs != NULL)
Packit 423ecb
        xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCtxtDumpDocument:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @doc:  the document
Packit 423ecb
 *
Packit 423ecb
 * Dumps debug information for the document, it's recursive
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
Packit 423ecb
{
Packit 423ecb
    if (doc == NULL) {
Packit 423ecb
        if (!ctxt->check)
Packit 423ecb
            fprintf(ctxt->output, "DOCUMENT == NULL !\n");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    xmlCtxtDumpDocumentHead(ctxt, doc);
Packit 423ecb
    if (((doc->type == XML_DOCUMENT_NODE) ||
Packit 423ecb
         (doc->type == XML_HTML_DOCUMENT_NODE))
Packit 423ecb
        && (doc->children != NULL)) {
Packit 423ecb
        ctxt->depth++;
Packit 423ecb
        xmlCtxtDumpNodeList(ctxt, doc->children);
Packit 423ecb
        ctxt->depth--;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpEntityCallback(xmlEntityPtr cur, xmlDebugCtxtPtr ctxt)
Packit 423ecb
{
Packit 423ecb
    if (cur == NULL) {
Packit 423ecb
        if (!ctxt->check)
Packit 423ecb
            fprintf(ctxt->output, "Entity is NULL");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    if (!ctxt->check) {
Packit 423ecb
        fprintf(ctxt->output, "%s : ", (char *) cur->name);
Packit 423ecb
        switch (cur->etype) {
Packit 423ecb
            case XML_INTERNAL_GENERAL_ENTITY:
Packit 423ecb
                fprintf(ctxt->output, "INTERNAL GENERAL, ");
Packit 423ecb
                break;
Packit 423ecb
            case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
Packit 423ecb
                fprintf(ctxt->output, "EXTERNAL PARSED, ");
Packit 423ecb
                break;
Packit 423ecb
            case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
Packit 423ecb
                fprintf(ctxt->output, "EXTERNAL UNPARSED, ");
Packit 423ecb
                break;
Packit 423ecb
            case XML_INTERNAL_PARAMETER_ENTITY:
Packit 423ecb
                fprintf(ctxt->output, "INTERNAL PARAMETER, ");
Packit 423ecb
                break;
Packit 423ecb
            case XML_EXTERNAL_PARAMETER_ENTITY:
Packit 423ecb
                fprintf(ctxt->output, "EXTERNAL PARAMETER, ");
Packit 423ecb
                break;
Packit 423ecb
            default:
Packit 423ecb
		xmlDebugErr2(ctxt, XML_CHECK_ENTITY_TYPE,
Packit 423ecb
			     "Unknown entity type %d\n", cur->etype);
Packit 423ecb
        }
Packit 423ecb
        if (cur->ExternalID != NULL)
Packit 423ecb
            fprintf(ctxt->output, "ID \"%s\"", (char *) cur->ExternalID);
Packit 423ecb
        if (cur->SystemID != NULL)
Packit 423ecb
            fprintf(ctxt->output, "SYSTEM \"%s\"", (char *) cur->SystemID);
Packit 423ecb
        if (cur->orig != NULL)
Packit 423ecb
            fprintf(ctxt->output, "\n orig \"%s\"", (char *) cur->orig);
Packit 423ecb
        if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL))
Packit 423ecb
            fprintf(ctxt->output, "\n content \"%s\"",
Packit 423ecb
                    (char *) cur->content);
Packit 423ecb
        fprintf(ctxt->output, "\n");
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCtxtDumpEntities:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @doc:  the document
Packit 423ecb
 *
Packit 423ecb
 * Dumps debug information for all the entities in use by the document
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
Packit 423ecb
{
Packit 423ecb
    if (doc == NULL) return;
Packit 423ecb
    xmlCtxtDumpDocHead(ctxt, doc);
Packit 423ecb
    if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
Packit 423ecb
        xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
Packit 423ecb
            doc->intSubset->entities;
Packit 423ecb
Packit 423ecb
        if (!ctxt->check)
Packit 423ecb
            fprintf(ctxt->output, "Entities in internal subset\n");
Packit 423ecb
        xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
Packit 423ecb
                    ctxt);
Packit 423ecb
    } else
Packit 423ecb
        fprintf(ctxt->output, "No entities in internal subset\n");
Packit 423ecb
    if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
Packit 423ecb
        xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
Packit 423ecb
            doc->extSubset->entities;
Packit 423ecb
Packit 423ecb
        if (!ctxt->check)
Packit 423ecb
            fprintf(ctxt->output, "Entities in external subset\n");
Packit 423ecb
        xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
Packit 423ecb
                    ctxt);
Packit 423ecb
    } else if (!ctxt->check)
Packit 423ecb
        fprintf(ctxt->output, "No entities in external subset\n");
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlCtxtDumpDTD:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @dtd:  the DTD
Packit 423ecb
 *
Packit 423ecb
 * Dumps debug information for the DTD
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
Packit 423ecb
{
Packit 423ecb
    if (dtd == NULL) {
Packit 423ecb
        if (!ctxt->check)
Packit 423ecb
            fprintf(ctxt->output, "DTD is NULL\n");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    xmlCtxtDumpDtdNode(ctxt, dtd);
Packit 423ecb
    if (dtd->children == NULL)
Packit 423ecb
        fprintf(ctxt->output, "    DTD is empty\n");
Packit 423ecb
    else {
Packit 423ecb
        ctxt->depth++;
Packit 423ecb
        xmlCtxtDumpNodeList(ctxt, dtd->children);
Packit 423ecb
        ctxt->depth--;
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *			Public entry points for dump			*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDebugDumpString:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @str:  the string
Packit 423ecb
 *
Packit 423ecb
 * Dumps informations about the string, shorten it if necessary
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlDebugDumpString(FILE * output, const xmlChar * str)
Packit 423ecb
{
Packit 423ecb
    int i;
Packit 423ecb
Packit 423ecb
    if (output == NULL)
Packit 423ecb
	output = stdout;
Packit 423ecb
    if (str == NULL) {
Packit 423ecb
        fprintf(output, "(NULL)");
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
    for (i = 0; i < 40; i++)
Packit 423ecb
        if (str[i] == 0)
Packit 423ecb
            return;
Packit 423ecb
        else if (IS_BLANK_CH(str[i]))
Packit 423ecb
            fputc(' ', output);
Packit 423ecb
        else if (str[i] >= 0x80)
Packit 423ecb
            fprintf(output, "#%X", str[i]);
Packit 423ecb
        else
Packit 423ecb
            fputc(str[i], output);
Packit 423ecb
    fprintf(output, "...");
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDebugDumpAttr:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @attr:  the attribute
Packit 423ecb
 * @depth:  the indentation level.
Packit 423ecb
 *
Packit 423ecb
 * Dumps debug information for the attribute
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
Packit 423ecb
    xmlDebugCtxt ctxt;
Packit 423ecb
Packit 423ecb
    if (output == NULL) return;
Packit 423ecb
    xmlCtxtDumpInitCtxt(&ctxt);
Packit 423ecb
    ctxt.output = output;
Packit 423ecb
    ctxt.depth = depth;
Packit 423ecb
    xmlCtxtDumpAttr(&ctxt, attr);
Packit 423ecb
    xmlCtxtDumpCleanCtxt(&ctxt);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDebugDumpEntities:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @doc:  the document
Packit 423ecb
 *
Packit 423ecb
 * Dumps debug information for all the entities in use by the document
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
Packit 423ecb
{
Packit 423ecb
    xmlDebugCtxt ctxt;
Packit 423ecb
Packit 423ecb
    if (output == NULL) return;
Packit 423ecb
    xmlCtxtDumpInitCtxt(&ctxt);
Packit 423ecb
    ctxt.output = output;
Packit 423ecb
    xmlCtxtDumpEntities(&ctxt, doc);
Packit 423ecb
    xmlCtxtDumpCleanCtxt(&ctxt);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDebugDumpAttrList:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @attr:  the attribute list
Packit 423ecb
 * @depth:  the indentation level.
Packit 423ecb
 *
Packit 423ecb
 * Dumps debug information for the attribute list
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
Packit 423ecb
{
Packit 423ecb
    xmlDebugCtxt ctxt;
Packit 423ecb
Packit 423ecb
    if (output == NULL) return;
Packit 423ecb
    xmlCtxtDumpInitCtxt(&ctxt);
Packit 423ecb
    ctxt.output = output;
Packit 423ecb
    ctxt.depth = depth;
Packit 423ecb
    xmlCtxtDumpAttrList(&ctxt, attr);
Packit 423ecb
    xmlCtxtDumpCleanCtxt(&ctxt);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDebugDumpOneNode:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @node:  the node
Packit 423ecb
 * @depth:  the indentation level.
Packit 423ecb
 *
Packit 423ecb
 * Dumps debug information for the element node, it is not recursive
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
Packit 423ecb
{
Packit 423ecb
    xmlDebugCtxt ctxt;
Packit 423ecb
Packit 423ecb
    if (output == NULL) return;
Packit 423ecb
    xmlCtxtDumpInitCtxt(&ctxt);
Packit 423ecb
    ctxt.output = output;
Packit 423ecb
    ctxt.depth = depth;
Packit 423ecb
    xmlCtxtDumpOneNode(&ctxt, node);
Packit 423ecb
    xmlCtxtDumpCleanCtxt(&ctxt);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDebugDumpNode:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @node:  the node
Packit 423ecb
 * @depth:  the indentation level.
Packit 423ecb
 *
Packit 423ecb
 * Dumps debug information for the element node, it is recursive
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
Packit 423ecb
{
Packit 423ecb
    xmlDebugCtxt ctxt;
Packit 423ecb
Packit 423ecb
    if (output == NULL)
Packit 423ecb
	output = stdout;
Packit 423ecb
    xmlCtxtDumpInitCtxt(&ctxt);
Packit 423ecb
    ctxt.output = output;
Packit 423ecb
    ctxt.depth = depth;
Packit 423ecb
    xmlCtxtDumpNode(&ctxt, node);
Packit 423ecb
    xmlCtxtDumpCleanCtxt(&ctxt);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDebugDumpNodeList:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @node:  the node list
Packit 423ecb
 * @depth:  the indentation level.
Packit 423ecb
 *
Packit 423ecb
 * Dumps debug information for the list of element node, it is recursive
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
Packit 423ecb
{
Packit 423ecb
    xmlDebugCtxt ctxt;
Packit 423ecb
Packit 423ecb
    if (output == NULL)
Packit 423ecb
	output = stdout;
Packit 423ecb
    xmlCtxtDumpInitCtxt(&ctxt);
Packit 423ecb
    ctxt.output = output;
Packit 423ecb
    ctxt.depth = depth;
Packit 423ecb
    xmlCtxtDumpNodeList(&ctxt, node);
Packit 423ecb
    xmlCtxtDumpCleanCtxt(&ctxt);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDebugDumpDocumentHead:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @doc:  the document
Packit 423ecb
 *
Packit 423ecb
 * Dumps debug information cncerning the document, not recursive
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
Packit 423ecb
{
Packit 423ecb
    xmlDebugCtxt ctxt;
Packit 423ecb
Packit 423ecb
    if (output == NULL)
Packit 423ecb
	output = stdout;
Packit 423ecb
    xmlCtxtDumpInitCtxt(&ctxt);
Packit 423ecb
    ctxt.options |= DUMP_TEXT_TYPE;
Packit 423ecb
    ctxt.output = output;
Packit 423ecb
    xmlCtxtDumpDocumentHead(&ctxt, doc);
Packit 423ecb
    xmlCtxtDumpCleanCtxt(&ctxt);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDebugDumpDocument:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @doc:  the document
Packit 423ecb
 *
Packit 423ecb
 * Dumps debug information for the document, it's recursive
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
Packit 423ecb
{
Packit 423ecb
    xmlDebugCtxt ctxt;
Packit 423ecb
Packit 423ecb
    if (output == NULL)
Packit 423ecb
	output = stdout;
Packit 423ecb
    xmlCtxtDumpInitCtxt(&ctxt);
Packit 423ecb
    ctxt.options |= DUMP_TEXT_TYPE;
Packit 423ecb
    ctxt.output = output;
Packit 423ecb
    xmlCtxtDumpDocument(&ctxt, doc);
Packit 423ecb
    xmlCtxtDumpCleanCtxt(&ctxt);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDebugDumpDTD:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @dtd:  the DTD
Packit 423ecb
 *
Packit 423ecb
 * Dumps debug information for the DTD
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
Packit 423ecb
{
Packit 423ecb
    xmlDebugCtxt ctxt;
Packit 423ecb
Packit 423ecb
    if (output == NULL)
Packit 423ecb
	output = stdout;
Packit 423ecb
    xmlCtxtDumpInitCtxt(&ctxt);
Packit 423ecb
    ctxt.options |= DUMP_TEXT_TYPE;
Packit 423ecb
    ctxt.output = output;
Packit 423ecb
    xmlCtxtDumpDTD(&ctxt, dtd);
Packit 423ecb
    xmlCtxtDumpCleanCtxt(&ctxt);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *			Public entry points for checkings		*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlDebugCheckDocument:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @doc:  the document
Packit 423ecb
 *
Packit 423ecb
 * Check the document for potential content problems, and output
Packit 423ecb
 * the errors to @output
Packit 423ecb
 *
Packit 423ecb
 * Returns the number of errors found
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
Packit 423ecb
{
Packit 423ecb
    xmlDebugCtxt ctxt;
Packit 423ecb
Packit 423ecb
    if (output == NULL)
Packit 423ecb
	output = stdout;
Packit 423ecb
    xmlCtxtDumpInitCtxt(&ctxt);
Packit 423ecb
    ctxt.output = output;
Packit 423ecb
    ctxt.check = 1;
Packit 423ecb
    xmlCtxtDumpDocument(&ctxt, doc);
Packit 423ecb
    xmlCtxtDumpCleanCtxt(&ctxt);
Packit 423ecb
    return(ctxt.errors);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/************************************************************************
Packit 423ecb
 *									*
Packit 423ecb
 *			Helpers for Shell				*
Packit 423ecb
 *									*
Packit 423ecb
 ************************************************************************/
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlLsCountNode:
Packit 423ecb
 * @node:  the node to count
Packit 423ecb
 *
Packit 423ecb
 * Count the children of @node.
Packit 423ecb
 *
Packit 423ecb
 * Returns the number of children of @node.
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlLsCountNode(xmlNodePtr node) {
Packit 423ecb
    int ret = 0;
Packit 423ecb
    xmlNodePtr list = NULL;
Packit 423ecb
Packit 423ecb
    if (node == NULL)
Packit 423ecb
	return(0);
Packit 423ecb
Packit 423ecb
    switch (node->type) {
Packit 423ecb
	case XML_ELEMENT_NODE:
Packit 423ecb
	    list = node->children;
Packit 423ecb
	    break;
Packit 423ecb
	case XML_DOCUMENT_NODE:
Packit 423ecb
	case XML_HTML_DOCUMENT_NODE:
Packit 423ecb
#ifdef LIBXML_DOCB_ENABLED
Packit 423ecb
	case XML_DOCB_DOCUMENT_NODE:
Packit 423ecb
#endif
Packit 423ecb
	    list = ((xmlDocPtr) node)->children;
Packit 423ecb
	    break;
Packit 423ecb
	case XML_ATTRIBUTE_NODE:
Packit 423ecb
	    list = ((xmlAttrPtr) node)->children;
Packit 423ecb
	    break;
Packit 423ecb
	case XML_TEXT_NODE:
Packit 423ecb
	case XML_CDATA_SECTION_NODE:
Packit 423ecb
	case XML_PI_NODE:
Packit 423ecb
	case XML_COMMENT_NODE:
Packit 423ecb
	    if (node->content != NULL) {
Packit 423ecb
		ret = xmlStrlen(node->content);
Packit 423ecb
            }
Packit 423ecb
	    break;
Packit 423ecb
	case XML_ENTITY_REF_NODE:
Packit 423ecb
	case XML_DOCUMENT_TYPE_NODE:
Packit 423ecb
	case XML_ENTITY_NODE:
Packit 423ecb
	case XML_DOCUMENT_FRAG_NODE:
Packit 423ecb
	case XML_NOTATION_NODE:
Packit 423ecb
	case XML_DTD_NODE:
Packit 423ecb
        case XML_ELEMENT_DECL:
Packit 423ecb
        case XML_ATTRIBUTE_DECL:
Packit 423ecb
        case XML_ENTITY_DECL:
Packit 423ecb
	case XML_NAMESPACE_DECL:
Packit 423ecb
	case XML_XINCLUDE_START:
Packit 423ecb
	case XML_XINCLUDE_END:
Packit 423ecb
	    ret = 1;
Packit 423ecb
	    break;
Packit 423ecb
    }
Packit 423ecb
    for (;list != NULL;ret++)
Packit 423ecb
        list = list->next;
Packit 423ecb
    return(ret);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlLsOneNode:
Packit 423ecb
 * @output:  the FILE * for the output
Packit 423ecb
 * @node:  the node to dump
Packit 423ecb
 *
Packit 423ecb
 * Dump to @output the type and name of @node.
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlLsOneNode(FILE *output, xmlNodePtr node) {
Packit 423ecb
    if (output == NULL) return;
Packit 423ecb
    if (node == NULL) {
Packit 423ecb
	fprintf(output, "NULL\n");
Packit 423ecb
	return;
Packit 423ecb
    }
Packit 423ecb
    switch (node->type) {
Packit 423ecb
	case XML_ELEMENT_NODE:
Packit 423ecb
	    fprintf(output, "-");
Packit 423ecb
	    break;
Packit 423ecb
	case XML_ATTRIBUTE_NODE:
Packit 423ecb
	    fprintf(output, "a");
Packit 423ecb
	    break;
Packit 423ecb
	case XML_TEXT_NODE:
Packit 423ecb
	    fprintf(output, "t");
Packit 423ecb
	    break;
Packit 423ecb
	case XML_CDATA_SECTION_NODE:
Packit 423ecb
	    fprintf(output, "C");
Packit 423ecb
	    break;
Packit 423ecb
	case XML_ENTITY_REF_NODE:
Packit 423ecb
	    fprintf(output, "e");
Packit 423ecb
	    break;
Packit 423ecb
	case XML_ENTITY_NODE:
Packit 423ecb
	    fprintf(output, "E");
Packit 423ecb
	    break;
Packit 423ecb
	case XML_PI_NODE:
Packit 423ecb
	    fprintf(output, "p");
Packit 423ecb
	    break;
Packit 423ecb
	case XML_COMMENT_NODE:
Packit 423ecb
	    fprintf(output, "c");
Packit 423ecb
	    break;
Packit 423ecb
	case XML_DOCUMENT_NODE:
Packit 423ecb
	    fprintf(output, "d");
Packit 423ecb
	    break;
Packit 423ecb
	case XML_HTML_DOCUMENT_NODE:
Packit 423ecb
	    fprintf(output, "h");
Packit 423ecb
	    break;
Packit 423ecb
	case XML_DOCUMENT_TYPE_NODE:
Packit 423ecb
	    fprintf(output, "T");
Packit 423ecb
	    break;
Packit 423ecb
	case XML_DOCUMENT_FRAG_NODE:
Packit 423ecb
	    fprintf(output, "F");
Packit 423ecb
	    break;
Packit 423ecb
	case XML_NOTATION_NODE:
Packit 423ecb
	    fprintf(output, "N");
Packit 423ecb
	    break;
Packit 423ecb
	case XML_NAMESPACE_DECL:
Packit 423ecb
	    fprintf(output, "n");
Packit 423ecb
	    break;
Packit 423ecb
	default:
Packit 423ecb
	    fprintf(output, "?");
Packit 423ecb
    }
Packit 423ecb
    if (node->type != XML_NAMESPACE_DECL) {
Packit 423ecb
	if (node->properties != NULL)
Packit 423ecb
	    fprintf(output, "a");
Packit 423ecb
	else
Packit 423ecb
	    fprintf(output, "-");
Packit 423ecb
	if (node->nsDef != NULL)
Packit 423ecb
	    fprintf(output, "n");
Packit 423ecb
	else
Packit 423ecb
	    fprintf(output, "-");
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    fprintf(output, " %8d ", xmlLsCountNode(node));
Packit 423ecb
Packit 423ecb
    switch (node->type) {
Packit 423ecb
	case XML_ELEMENT_NODE:
Packit 423ecb
	    if (node->name != NULL) {
Packit 423ecb
                if ((node->ns != NULL) && (node->ns->prefix != NULL))
Packit 423ecb
                    fprintf(output, "%s:", node->ns->prefix);
Packit 423ecb
		fprintf(output, "%s", (const char *) node->name);
Packit 423ecb
            }
Packit 423ecb
	    break;
Packit 423ecb
	case XML_ATTRIBUTE_NODE:
Packit 423ecb
	    if (node->name != NULL)
Packit 423ecb
		fprintf(output, "%s", (const char *) node->name);
Packit 423ecb
	    break;
Packit 423ecb
	case XML_TEXT_NODE:
Packit 423ecb
	    if (node->content != NULL) {
Packit 423ecb
		xmlDebugDumpString(output, node->content);
Packit 423ecb
            }
Packit 423ecb
	    break;
Packit 423ecb
	case XML_CDATA_SECTION_NODE:
Packit 423ecb
	    break;
Packit 423ecb
	case XML_ENTITY_REF_NODE:
Packit 423ecb
	    if (node->name != NULL)
Packit 423ecb
		fprintf(output, "%s", (const char *) node->name);
Packit 423ecb
	    break;
Packit 423ecb
	case XML_ENTITY_NODE:
Packit 423ecb
	    if (node->name != NULL)
Packit 423ecb
		fprintf(output, "%s", (const char *) node->name);
Packit 423ecb
	    break;
Packit 423ecb
	case XML_PI_NODE:
Packit 423ecb
	    if (node->name != NULL)
Packit 423ecb
		fprintf(output, "%s", (const char *) node->name);
Packit 423ecb
	    break;
Packit 423ecb
	case XML_COMMENT_NODE:
Packit 423ecb
	    break;
Packit 423ecb
	case XML_DOCUMENT_NODE:
Packit 423ecb
	    break;
Packit 423ecb
	case XML_HTML_DOCUMENT_NODE:
Packit 423ecb
	    break;
Packit 423ecb
	case XML_DOCUMENT_TYPE_NODE:
Packit 423ecb
	    break;
Packit 423ecb
	case XML_DOCUMENT_FRAG_NODE:
Packit 423ecb
	    break;
Packit 423ecb
	case XML_NOTATION_NODE:
Packit 423ecb
	    break;
Packit 423ecb
	case XML_NAMESPACE_DECL: {
Packit 423ecb
	    xmlNsPtr ns = (xmlNsPtr) node;
Packit 423ecb
Packit 423ecb
	    if (ns->prefix == NULL)
Packit 423ecb
		fprintf(output, "default -> %s", (char *)ns->href);
Packit 423ecb
	    else
Packit 423ecb
		fprintf(output, "%s -> %s", (char *)ns->prefix,
Packit 423ecb
			(char *)ns->href);
Packit 423ecb
	    break;
Packit 423ecb
	}
Packit 423ecb
	default:
Packit 423ecb
	    if (node->name != NULL)
Packit 423ecb
		fprintf(output, "%s", (const char *) node->name);
Packit 423ecb
    }
Packit 423ecb
    fprintf(output, "\n");
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlBoolToText:
Packit 423ecb
 * @boolval: a bool to turn into text
Packit 423ecb
 *
Packit 423ecb
 * Convenient way to turn bool into text
Packit 423ecb
 *
Packit 423ecb
 * Returns a pointer to either "True" or "False"
Packit 423ecb
 */
Packit 423ecb
const char *
Packit 423ecb
xmlBoolToText(int boolval)
Packit 423ecb
{
Packit 423ecb
    if (boolval)
Packit 423ecb
        return("True");
Packit 423ecb
    else
Packit 423ecb
        return("False");
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_XPATH_ENABLED
Packit 423ecb
/****************************************************************
Packit 423ecb
 *								*
Packit 423ecb
 *		The XML shell related functions			*
Packit 423ecb
 *								*
Packit 423ecb
 ****************************************************************/
Packit 423ecb
Packit 423ecb
Packit 423ecb
Packit 423ecb
/*
Packit 423ecb
 * TODO: Improvement/cleanups for the XML shell
Packit 423ecb
 *     - allow to shell out an editor on a subpart
Packit 423ecb
 *     - cleanup function registrations (with help) and calling
Packit 423ecb
 *     - provide registration routines
Packit 423ecb
 */
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlShellPrintXPathError:
Packit 423ecb
 * @errorType: valid xpath error id
Packit 423ecb
 * @arg: the argument that cause xpath to fail
Packit 423ecb
 *
Packit 423ecb
 * Print the xpath error to libxml default error channel
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlShellPrintXPathError(int errorType, const char *arg)
Packit 423ecb
{
Packit 423ecb
    const char *default_arg = "Result";
Packit 423ecb
Packit 423ecb
    if (!arg)
Packit 423ecb
        arg = default_arg;
Packit 423ecb
Packit 423ecb
    switch (errorType) {
Packit 423ecb
        case XPATH_UNDEFINED:
Packit 423ecb
            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                            "%s: no such node\n", arg);
Packit 423ecb
            break;
Packit 423ecb
Packit 423ecb
        case XPATH_BOOLEAN:
Packit 423ecb
            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                            "%s is a Boolean\n", arg);
Packit 423ecb
            break;
Packit 423ecb
        case XPATH_NUMBER:
Packit 423ecb
            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                            "%s is a number\n", arg);
Packit 423ecb
            break;
Packit 423ecb
        case XPATH_STRING:
Packit 423ecb
            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                            "%s is a string\n", arg);
Packit 423ecb
            break;
Packit 423ecb
        case XPATH_POINT:
Packit 423ecb
            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                            "%s is a point\n", arg);
Packit 423ecb
            break;
Packit 423ecb
        case XPATH_RANGE:
Packit 423ecb
            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                            "%s is a range\n", arg);
Packit 423ecb
            break;
Packit 423ecb
        case XPATH_LOCATIONSET:
Packit 423ecb
            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                            "%s is a range\n", arg);
Packit 423ecb
            break;
Packit 423ecb
        case XPATH_USERS:
Packit 423ecb
            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                            "%s is user-defined\n", arg);
Packit 423ecb
            break;
Packit 423ecb
        case XPATH_XSLT_TREE:
Packit 423ecb
            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                            "%s is an XSLT value tree\n", arg);
Packit 423ecb
            break;
Packit 423ecb
    }
Packit 423ecb
#if 0
Packit 423ecb
    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                    "Try casting the result string function (xpath builtin)\n",
Packit 423ecb
                    arg);
Packit 423ecb
#endif
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_OUTPUT_ENABLED
Packit 423ecb
/**
Packit 423ecb
 * xmlShellPrintNodeCtxt:
Packit 423ecb
 * @ctxt : a non-null shell context
Packit 423ecb
 * @node : a non-null node to print to the output FILE
Packit 423ecb
 *
Packit 423ecb
 * Print node to the output FILE
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
Packit 423ecb
{
Packit 423ecb
    FILE *fp;
Packit 423ecb
Packit 423ecb
    if (!node)
Packit 423ecb
        return;
Packit 423ecb
    if (ctxt == NULL)
Packit 423ecb
	fp = stdout;
Packit 423ecb
    else
Packit 423ecb
	fp = ctxt->output;
Packit 423ecb
Packit 423ecb
    if (node->type == XML_DOCUMENT_NODE)
Packit 423ecb
        xmlDocDump(fp, (xmlDocPtr) node);
Packit 423ecb
    else if (node->type == XML_ATTRIBUTE_NODE)
Packit 423ecb
        xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
Packit 423ecb
    else
Packit 423ecb
        xmlElemDump(fp, node->doc, node);
Packit 423ecb
Packit 423ecb
    fprintf(fp, "\n");
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlShellPrintNode:
Packit 423ecb
 * @node : a non-null node to print to the output FILE
Packit 423ecb
 *
Packit 423ecb
 * Print node to the output FILE
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlShellPrintNode(xmlNodePtr node)
Packit 423ecb
{
Packit 423ecb
    xmlShellPrintNodeCtxt(NULL, node);
Packit 423ecb
}
Packit 423ecb
#endif /* LIBXML_OUTPUT_ENABLED */
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlShellPrintXPathResultCtxt:
Packit 423ecb
 * @ctxt: a valid shell context
Packit 423ecb
 * @list: a valid result generated by an xpath evaluation
Packit 423ecb
 *
Packit 423ecb
 * Prints result to the output FILE
Packit 423ecb
 */
Packit 423ecb
static void
Packit 423ecb
xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
Packit 423ecb
{
Packit 423ecb
    if (!ctxt)
Packit 423ecb
       return;
Packit 423ecb
Packit 423ecb
    if (list != NULL) {
Packit 423ecb
        switch (list->type) {
Packit 423ecb
            case XPATH_NODESET:{
Packit 423ecb
#ifdef LIBXML_OUTPUT_ENABLED
Packit 423ecb
                    int indx;
Packit 423ecb
Packit 423ecb
                    if (list->nodesetval) {
Packit 423ecb
                        for (indx = 0; indx < list->nodesetval->nodeNr;
Packit 423ecb
                             indx++) {
Packit 423ecb
                            xmlShellPrintNodeCtxt(ctxt,
Packit 423ecb
				    list->nodesetval->nodeTab[indx]);
Packit 423ecb
                        }
Packit 423ecb
                    } else {
Packit 423ecb
                        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                        "Empty node set\n");
Packit 423ecb
                    }
Packit 423ecb
                    break;
Packit 423ecb
#else
Packit 423ecb
		    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
				    "Node set\n");
Packit 423ecb
#endif /* LIBXML_OUTPUT_ENABLED */
Packit 423ecb
                }
Packit 423ecb
            case XPATH_BOOLEAN:
Packit 423ecb
                xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                "Is a Boolean:%s\n",
Packit 423ecb
                                xmlBoolToText(list->boolval));
Packit 423ecb
                break;
Packit 423ecb
            case XPATH_NUMBER:
Packit 423ecb
                xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                "Is a number:%0g\n", list->floatval);
Packit 423ecb
                break;
Packit 423ecb
            case XPATH_STRING:
Packit 423ecb
                xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                "Is a string:%s\n", list->stringval);
Packit 423ecb
                break;
Packit 423ecb
Packit 423ecb
            default:
Packit 423ecb
                xmlShellPrintXPathError(list->type, NULL);
Packit 423ecb
        }
Packit 423ecb
    }
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlShellPrintXPathResult:
Packit 423ecb
 * @list: a valid result generated by an xpath evaluation
Packit 423ecb
 *
Packit 423ecb
 * Prints result to the output FILE
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlShellPrintXPathResult(xmlXPathObjectPtr list)
Packit 423ecb
{
Packit 423ecb
    xmlShellPrintXPathResultCtxt(NULL, list);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlShellList:
Packit 423ecb
 * @ctxt:  the shell context
Packit 423ecb
 * @arg:  unused
Packit 423ecb
 * @node:  a node
Packit 423ecb
 * @node2:  unused
Packit 423ecb
 *
Packit 423ecb
 * Implements the XML shell function "ls"
Packit 423ecb
 * Does an Unix like listing of the given node (like a directory)
Packit 423ecb
 *
Packit 423ecb
 * Returns 0
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlShellList(xmlShellCtxtPtr ctxt,
Packit 423ecb
             char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
Packit 423ecb
             xmlNodePtr node2 ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
    xmlNodePtr cur;
Packit 423ecb
    if (!ctxt)
Packit 423ecb
        return (0);
Packit 423ecb
    if (node == NULL) {
Packit 423ecb
	fprintf(ctxt->output, "NULL\n");
Packit 423ecb
	return (0);
Packit 423ecb
    }
Packit 423ecb
    if ((node->type == XML_DOCUMENT_NODE) ||
Packit 423ecb
        (node->type == XML_HTML_DOCUMENT_NODE)) {
Packit 423ecb
        cur = ((xmlDocPtr) node)->children;
Packit 423ecb
    } else if (node->type == XML_NAMESPACE_DECL) {
Packit 423ecb
        xmlLsOneNode(ctxt->output, node);
Packit 423ecb
        return (0);
Packit 423ecb
    } else if (node->children != NULL) {
Packit 423ecb
        cur = node->children;
Packit 423ecb
    } else {
Packit 423ecb
        xmlLsOneNode(ctxt->output, node);
Packit 423ecb
        return (0);
Packit 423ecb
    }
Packit 423ecb
    while (cur != NULL) {
Packit 423ecb
        xmlLsOneNode(ctxt->output, cur);
Packit 423ecb
        cur = cur->next;
Packit 423ecb
    }
Packit 423ecb
    return (0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlShellBase:
Packit 423ecb
 * @ctxt:  the shell context
Packit 423ecb
 * @arg:  unused
Packit 423ecb
 * @node:  a node
Packit 423ecb
 * @node2:  unused
Packit 423ecb
 *
Packit 423ecb
 * Implements the XML shell function "base"
Packit 423ecb
 * dumps the current XML base of the node
Packit 423ecb
 *
Packit 423ecb
 * Returns 0
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlShellBase(xmlShellCtxtPtr ctxt,
Packit 423ecb
             char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
Packit 423ecb
             xmlNodePtr node2 ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
    xmlChar *base;
Packit 423ecb
    if (!ctxt)
Packit 423ecb
        return 0;
Packit 423ecb
    if (node == NULL) {
Packit 423ecb
	fprintf(ctxt->output, "NULL\n");
Packit 423ecb
	return (0);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    base = xmlNodeGetBase(node->doc, node);
Packit 423ecb
Packit 423ecb
    if (base == NULL) {
Packit 423ecb
        fprintf(ctxt->output, " No base found !!!\n");
Packit 423ecb
    } else {
Packit 423ecb
        fprintf(ctxt->output, "%s\n", base);
Packit 423ecb
        xmlFree(base);
Packit 423ecb
    }
Packit 423ecb
    return (0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_TREE_ENABLED
Packit 423ecb
/**
Packit 423ecb
 * xmlShellSetBase:
Packit 423ecb
 * @ctxt:  the shell context
Packit 423ecb
 * @arg:  the new base
Packit 423ecb
 * @node:  a node
Packit 423ecb
 * @node2:  unused
Packit 423ecb
 *
Packit 423ecb
 * Implements the XML shell function "setbase"
Packit 423ecb
 * change the current XML base of the node
Packit 423ecb
 *
Packit 423ecb
 * Returns 0
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
Packit 423ecb
             char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
Packit 423ecb
             xmlNodePtr node2 ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
    xmlNodeSetBase(node, (xmlChar*) arg);
Packit 423ecb
    return (0);
Packit 423ecb
}
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_XPATH_ENABLED
Packit 423ecb
/**
Packit 423ecb
 * xmlShellRegisterNamespace:
Packit 423ecb
 * @ctxt:  the shell context
Packit 423ecb
 * @arg:  a string in prefix=nsuri format
Packit 423ecb
 * @node:  unused
Packit 423ecb
 * @node2:  unused
Packit 423ecb
 *
Packit 423ecb
 * Implements the XML shell function "setns"
Packit 423ecb
 * register/unregister a prefix=namespace pair
Packit 423ecb
 * on the XPath context
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 on success and a negative value otherwise.
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
Packit 423ecb
      xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
    xmlChar* nsListDup;
Packit 423ecb
    xmlChar* prefix;
Packit 423ecb
    xmlChar* href;
Packit 423ecb
    xmlChar* next;
Packit 423ecb
Packit 423ecb
    nsListDup = xmlStrdup((xmlChar *) arg);
Packit 423ecb
    next = nsListDup;
Packit 423ecb
    while(next != NULL) {
Packit 423ecb
	/* skip spaces */
Packit 423ecb
	/*while((*next) == ' ') next++;*/
Packit 423ecb
	if((*next) == '\0') break;
Packit 423ecb
Packit 423ecb
	/* find prefix */
Packit 423ecb
	prefix = next;
Packit 423ecb
	next = (xmlChar*)xmlStrchr(next, '=');
Packit 423ecb
	if(next == NULL) {
Packit 423ecb
	    fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
Packit 423ecb
	    xmlFree(nsListDup);
Packit 423ecb
	    return(-1);
Packit 423ecb
	}
Packit 423ecb
	*(next++) = '\0';
Packit 423ecb
Packit 423ecb
	/* find href */
Packit 423ecb
	href = next;
Packit 423ecb
	next = (xmlChar*)xmlStrchr(next, ' ');
Packit 423ecb
	if(next != NULL) {
Packit 423ecb
	    *(next++) = '\0';
Packit 423ecb
	}
Packit 423ecb
Packit 423ecb
	/* do register namespace */
Packit 423ecb
	if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
Packit 423ecb
	    fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
Packit 423ecb
	    xmlFree(nsListDup);
Packit 423ecb
	    return(-1);
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    xmlFree(nsListDup);
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
/**
Packit 423ecb
 * xmlShellRegisterRootNamespaces:
Packit 423ecb
 * @ctxt:  the shell context
Packit 423ecb
 * @arg:  unused
Packit 423ecb
 * @node:  the root element
Packit 423ecb
 * @node2:  unused
Packit 423ecb
 *
Packit 423ecb
 * Implements the XML shell function "setrootns"
Packit 423ecb
 * which registers all namespaces declarations found on the root element.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 on success and a negative value otherwise.
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
Packit 423ecb
      xmlNodePtr root, xmlNodePtr node2 ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
    xmlNsPtr ns;
Packit 423ecb
Packit 423ecb
    if ((root == NULL) || (root->type != XML_ELEMENT_NODE) ||
Packit 423ecb
        (root->nsDef == NULL) || (ctxt == NULL) || (ctxt->pctxt == NULL))
Packit 423ecb
	return(-1);
Packit 423ecb
    ns = root->nsDef;
Packit 423ecb
    while (ns != NULL) {
Packit 423ecb
        if (ns->prefix == NULL)
Packit 423ecb
	    xmlXPathRegisterNs(ctxt->pctxt, BAD_CAST "defaultns", ns->href);
Packit 423ecb
	else
Packit 423ecb
	    xmlXPathRegisterNs(ctxt->pctxt, ns->prefix, ns->href);
Packit 423ecb
        ns = ns->next;
Packit 423ecb
    }
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlShellGrep:
Packit 423ecb
 * @ctxt:  the shell context
Packit 423ecb
 * @arg:  the string or regular expression to find
Packit 423ecb
 * @node:  a node
Packit 423ecb
 * @node2:  unused
Packit 423ecb
 *
Packit 423ecb
 * Implements the XML shell function "grep"
Packit 423ecb
 * dumps informations about the node (namespace, attributes, content).
Packit 423ecb
 *
Packit 423ecb
 * Returns 0
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
Packit 423ecb
            char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
    if (!ctxt)
Packit 423ecb
        return (0);
Packit 423ecb
    if (node == NULL)
Packit 423ecb
	return (0);
Packit 423ecb
    if (arg == NULL)
Packit 423ecb
	return (0);
Packit 423ecb
#ifdef LIBXML_REGEXP_ENABLED
Packit 423ecb
    if ((xmlStrchr((xmlChar *) arg, '?')) ||
Packit 423ecb
	(xmlStrchr((xmlChar *) arg, '*')) ||
Packit 423ecb
	(xmlStrchr((xmlChar *) arg, '.')) ||
Packit 423ecb
	(xmlStrchr((xmlChar *) arg, '['))) {
Packit 423ecb
    }
Packit 423ecb
#endif
Packit 423ecb
    while (node != NULL) {
Packit 423ecb
        if (node->type == XML_COMMENT_NODE) {
Packit 423ecb
	    if (xmlStrstr(node->content, (xmlChar *) arg)) {
Packit 423ecb
Packit 423ecb
		fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
Packit 423ecb
                xmlShellList(ctxt, NULL, node, NULL);
Packit 423ecb
	    }
Packit 423ecb
        } else if (node->type == XML_TEXT_NODE) {
Packit 423ecb
	    if (xmlStrstr(node->content, (xmlChar *) arg)) {
Packit 423ecb
Packit 423ecb
		fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
Packit 423ecb
                xmlShellList(ctxt, NULL, node->parent, NULL);
Packit 423ecb
	    }
Packit 423ecb
        }
Packit 423ecb
Packit 423ecb
        /*
Packit 423ecb
         * Browse the full subtree, deep first
Packit 423ecb
         */
Packit 423ecb
Packit 423ecb
        if ((node->type == XML_DOCUMENT_NODE) ||
Packit 423ecb
            (node->type == XML_HTML_DOCUMENT_NODE)) {
Packit 423ecb
            node = ((xmlDocPtr) node)->children;
Packit 423ecb
        } else if ((node->children != NULL)
Packit 423ecb
                   && (node->type != XML_ENTITY_REF_NODE)) {
Packit 423ecb
            /* deep first */
Packit 423ecb
            node = node->children;
Packit 423ecb
        } else if (node->next != NULL) {
Packit 423ecb
            /* then siblings */
Packit 423ecb
            node = node->next;
Packit 423ecb
        } else {
Packit 423ecb
            /* go up to parents->next if needed */
Packit 423ecb
            while (node != NULL) {
Packit 423ecb
                if (node->parent != NULL) {
Packit 423ecb
                    node = node->parent;
Packit 423ecb
                }
Packit 423ecb
                if (node->next != NULL) {
Packit 423ecb
                    node = node->next;
Packit 423ecb
                    break;
Packit 423ecb
                }
Packit 423ecb
                if (node->parent == NULL) {
Packit 423ecb
                    node = NULL;
Packit 423ecb
                    break;
Packit 423ecb
                }
Packit 423ecb
            }
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    return (0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlShellDir:
Packit 423ecb
 * @ctxt:  the shell context
Packit 423ecb
 * @arg:  unused
Packit 423ecb
 * @node:  a node
Packit 423ecb
 * @node2:  unused
Packit 423ecb
 *
Packit 423ecb
 * Implements the XML shell function "dir"
Packit 423ecb
 * dumps informations about the node (namespace, attributes, content).
Packit 423ecb
 *
Packit 423ecb
 * Returns 0
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
Packit 423ecb
            char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
Packit 423ecb
            xmlNodePtr node2 ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
    if (!ctxt)
Packit 423ecb
        return (0);
Packit 423ecb
    if (node == NULL) {
Packit 423ecb
	fprintf(ctxt->output, "NULL\n");
Packit 423ecb
	return (0);
Packit 423ecb
    }
Packit 423ecb
    if ((node->type == XML_DOCUMENT_NODE) ||
Packit 423ecb
        (node->type == XML_HTML_DOCUMENT_NODE)) {
Packit 423ecb
        xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
Packit 423ecb
    } else if (node->type == XML_ATTRIBUTE_NODE) {
Packit 423ecb
        xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
Packit 423ecb
    } else {
Packit 423ecb
        xmlDebugDumpOneNode(ctxt->output, node, 0);
Packit 423ecb
    }
Packit 423ecb
    return (0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlShellSetContent:
Packit 423ecb
 * @ctxt:  the shell context
Packit 423ecb
 * @value:  the content as a string
Packit 423ecb
 * @node:  a node
Packit 423ecb
 * @node2:  unused
Packit 423ecb
 *
Packit 423ecb
 * Implements the XML shell function "dir"
Packit 423ecb
 * dumps informations about the node (namespace, attributes, content).
Packit 423ecb
 *
Packit 423ecb
 * Returns 0
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
Packit 423ecb
            char *value, xmlNodePtr node,
Packit 423ecb
            xmlNodePtr node2 ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
    xmlNodePtr results;
Packit 423ecb
    xmlParserErrors ret;
Packit 423ecb
Packit 423ecb
    if (!ctxt)
Packit 423ecb
        return (0);
Packit 423ecb
    if (node == NULL) {
Packit 423ecb
	fprintf(ctxt->output, "NULL\n");
Packit 423ecb
	return (0);
Packit 423ecb
    }
Packit 423ecb
    if (value == NULL) {
Packit 423ecb
        fprintf(ctxt->output, "NULL\n");
Packit 423ecb
	return (0);
Packit 423ecb
    }
Packit 423ecb
Packit 423ecb
    ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
Packit 423ecb
    if (ret == XML_ERR_OK) {
Packit 423ecb
	if (node->children != NULL) {
Packit 423ecb
	    xmlFreeNodeList(node->children);
Packit 423ecb
	    node->children = NULL;
Packit 423ecb
	    node->last = NULL;
Packit 423ecb
	}
Packit 423ecb
	xmlAddChildList(node, results);
Packit 423ecb
    } else {
Packit 423ecb
        fprintf(ctxt->output, "failed to parse content\n");
Packit 423ecb
    }
Packit 423ecb
    return (0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_SCHEMAS_ENABLED
Packit 423ecb
/**
Packit 423ecb
 * xmlShellRNGValidate:
Packit 423ecb
 * @ctxt:  the shell context
Packit 423ecb
 * @schemas:  the path to the Relax-NG schemas
Packit 423ecb
 * @node:  a node
Packit 423ecb
 * @node2:  unused
Packit 423ecb
 *
Packit 423ecb
 * Implements the XML shell function "relaxng"
Packit 423ecb
 * validating the instance against a Relax-NG schemas
Packit 423ecb
 *
Packit 423ecb
 * Returns 0
Packit 423ecb
 */
Packit 423ecb
static int
Packit 423ecb
xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
Packit 423ecb
            xmlNodePtr node ATTRIBUTE_UNUSED,
Packit 423ecb
	    xmlNodePtr node2 ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
    xmlRelaxNGPtr relaxngschemas;
Packit 423ecb
    xmlRelaxNGParserCtxtPtr ctxt;
Packit 423ecb
    xmlRelaxNGValidCtxtPtr vctxt;
Packit 423ecb
    int ret;
Packit 423ecb
Packit 423ecb
    ctxt = xmlRelaxNGNewParserCtxt(schemas);
Packit 423ecb
    xmlRelaxNGSetParserErrors(ctxt,
Packit 423ecb
	    (xmlRelaxNGValidityErrorFunc) fprintf,
Packit 423ecb
	    (xmlRelaxNGValidityWarningFunc) fprintf,
Packit 423ecb
	    stderr);
Packit 423ecb
    relaxngschemas = xmlRelaxNGParse(ctxt);
Packit 423ecb
    xmlRelaxNGFreeParserCtxt(ctxt);
Packit 423ecb
    if (relaxngschemas == NULL) {
Packit 423ecb
	xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
		"Relax-NG schema %s failed to compile\n", schemas);
Packit 423ecb
	return(-1);
Packit 423ecb
    }
Packit 423ecb
    vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
Packit 423ecb
    xmlRelaxNGSetValidErrors(vctxt,
Packit 423ecb
	    (xmlRelaxNGValidityErrorFunc) fprintf,
Packit 423ecb
	    (xmlRelaxNGValidityWarningFunc) fprintf,
Packit 423ecb
	    stderr);
Packit 423ecb
    ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
Packit 423ecb
    if (ret == 0) {
Packit 423ecb
	fprintf(stderr, "%s validates\n", sctxt->filename);
Packit 423ecb
    } else if (ret > 0) {
Packit 423ecb
	fprintf(stderr, "%s fails to validate\n", sctxt->filename);
Packit 423ecb
    } else {
Packit 423ecb
	fprintf(stderr, "%s validation generated an internal error\n",
Packit 423ecb
	       sctxt->filename);
Packit 423ecb
    }
Packit 423ecb
    xmlRelaxNGFreeValidCtxt(vctxt);
Packit 423ecb
    if (relaxngschemas != NULL)
Packit 423ecb
	xmlRelaxNGFree(relaxngschemas);
Packit 423ecb
    return(0);
Packit 423ecb
}
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_OUTPUT_ENABLED
Packit 423ecb
/**
Packit 423ecb
 * xmlShellCat:
Packit 423ecb
 * @ctxt:  the shell context
Packit 423ecb
 * @arg:  unused
Packit 423ecb
 * @node:  a node
Packit 423ecb
 * @node2:  unused
Packit 423ecb
 *
Packit 423ecb
 * Implements the XML shell function "cat"
Packit 423ecb
 * dumps the serialization node content (XML or HTML).
Packit 423ecb
 *
Packit 423ecb
 * Returns 0
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
Packit 423ecb
            xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
    if (!ctxt)
Packit 423ecb
        return (0);
Packit 423ecb
    if (node == NULL) {
Packit 423ecb
	fprintf(ctxt->output, "NULL\n");
Packit 423ecb
	return (0);
Packit 423ecb
    }
Packit 423ecb
    if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
Packit 423ecb
#ifdef LIBXML_HTML_ENABLED
Packit 423ecb
        if (node->type == XML_HTML_DOCUMENT_NODE)
Packit 423ecb
            htmlDocDump(ctxt->output, (htmlDocPtr) node);
Packit 423ecb
        else
Packit 423ecb
            htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
Packit 423ecb
#else
Packit 423ecb
        if (node->type == XML_DOCUMENT_NODE)
Packit 423ecb
            xmlDocDump(ctxt->output, (xmlDocPtr) node);
Packit 423ecb
        else
Packit 423ecb
            xmlElemDump(ctxt->output, ctxt->doc, node);
Packit 423ecb
#endif /* LIBXML_HTML_ENABLED */
Packit 423ecb
    } else {
Packit 423ecb
        if (node->type == XML_DOCUMENT_NODE)
Packit 423ecb
            xmlDocDump(ctxt->output, (xmlDocPtr) node);
Packit 423ecb
        else
Packit 423ecb
            xmlElemDump(ctxt->output, ctxt->doc, node);
Packit 423ecb
    }
Packit 423ecb
    fprintf(ctxt->output, "\n");
Packit 423ecb
    return (0);
Packit 423ecb
}
Packit 423ecb
#endif /* LIBXML_OUTPUT_ENABLED */
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlShellLoad:
Packit 423ecb
 * @ctxt:  the shell context
Packit 423ecb
 * @filename:  the file name
Packit 423ecb
 * @node:  unused
Packit 423ecb
 * @node2:  unused
Packit 423ecb
 *
Packit 423ecb
 * Implements the XML shell function "load"
Packit 423ecb
 * loads a new document specified by the filename
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 or -1 if loading failed
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
Packit 423ecb
             xmlNodePtr node ATTRIBUTE_UNUSED,
Packit 423ecb
             xmlNodePtr node2 ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
    xmlDocPtr doc;
Packit 423ecb
    int html = 0;
Packit 423ecb
Packit 423ecb
    if ((ctxt == NULL) || (filename == NULL)) return(-1);
Packit 423ecb
    if (ctxt->doc != NULL)
Packit 423ecb
        html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Packit 423ecb
Packit 423ecb
    if (html) {
Packit 423ecb
#ifdef LIBXML_HTML_ENABLED
Packit 423ecb
        doc = htmlParseFile(filename, NULL);
Packit 423ecb
#else
Packit 423ecb
        fprintf(ctxt->output, "HTML support not compiled in\n");
Packit 423ecb
        doc = NULL;
Packit 423ecb
#endif /* LIBXML_HTML_ENABLED */
Packit 423ecb
    } else {
Packit 423ecb
        doc = xmlReadFile(filename,NULL,0);
Packit 423ecb
    }
Packit 423ecb
    if (doc != NULL) {
Packit 423ecb
        if (ctxt->loaded == 1) {
Packit 423ecb
            xmlFreeDoc(ctxt->doc);
Packit 423ecb
        }
Packit 423ecb
        ctxt->loaded = 1;
Packit 423ecb
#ifdef LIBXML_XPATH_ENABLED
Packit 423ecb
        xmlXPathFreeContext(ctxt->pctxt);
Packit 423ecb
#endif /* LIBXML_XPATH_ENABLED */
Packit 423ecb
        xmlFree(ctxt->filename);
Packit 423ecb
        ctxt->doc = doc;
Packit 423ecb
        ctxt->node = (xmlNodePtr) doc;
Packit 423ecb
#ifdef LIBXML_XPATH_ENABLED
Packit 423ecb
        ctxt->pctxt = xmlXPathNewContext(doc);
Packit 423ecb
#endif /* LIBXML_XPATH_ENABLED */
Packit 423ecb
        ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
Packit 423ecb
    } else
Packit 423ecb
        return (-1);
Packit 423ecb
    return (0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_OUTPUT_ENABLED
Packit 423ecb
/**
Packit 423ecb
 * xmlShellWrite:
Packit 423ecb
 * @ctxt:  the shell context
Packit 423ecb
 * @filename:  the file name
Packit 423ecb
 * @node:  a node in the tree
Packit 423ecb
 * @node2:  unused
Packit 423ecb
 *
Packit 423ecb
 * Implements the XML shell function "write"
Packit 423ecb
 * Write the current node to the filename, it saves the serialization
Packit 423ecb
 * of the subtree under the @node specified
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 or -1 in case of error
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Packit 423ecb
              xmlNodePtr node2 ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
    if (node == NULL)
Packit 423ecb
        return (-1);
Packit 423ecb
    if ((filename == NULL) || (filename[0] == 0)) {
Packit 423ecb
        return (-1);
Packit 423ecb
    }
Packit 423ecb
#ifdef W_OK
Packit 423ecb
    if (access((char *) filename, W_OK)) {
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                        "Cannot write to %s\n", filename);
Packit 423ecb
        return (-1);
Packit 423ecb
    }
Packit 423ecb
#endif
Packit 423ecb
    switch (node->type) {
Packit 423ecb
        case XML_DOCUMENT_NODE:
Packit 423ecb
            if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
Packit 423ecb
                xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                "Failed to write to %s\n", filename);
Packit 423ecb
                return (-1);
Packit 423ecb
            }
Packit 423ecb
            break;
Packit 423ecb
        case XML_HTML_DOCUMENT_NODE:
Packit 423ecb
#ifdef LIBXML_HTML_ENABLED
Packit 423ecb
            if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
Packit 423ecb
                xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                "Failed to write to %s\n", filename);
Packit 423ecb
                return (-1);
Packit 423ecb
            }
Packit 423ecb
#else
Packit 423ecb
            if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
Packit 423ecb
                xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                "Failed to write to %s\n", filename);
Packit 423ecb
                return (-1);
Packit 423ecb
            }
Packit 423ecb
#endif /* LIBXML_HTML_ENABLED */
Packit 423ecb
            break;
Packit 423ecb
        default:{
Packit 423ecb
                FILE *f;
Packit 423ecb
Packit 423ecb
                f = fopen((char *) filename, "w");
Packit 423ecb
                if (f == NULL) {
Packit 423ecb
                    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                    "Failed to write to %s\n", filename);
Packit 423ecb
                    return (-1);
Packit 423ecb
                }
Packit 423ecb
                xmlElemDump(f, ctxt->doc, node);
Packit 423ecb
                fclose(f);
Packit 423ecb
            }
Packit 423ecb
    }
Packit 423ecb
    return (0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlShellSave:
Packit 423ecb
 * @ctxt:  the shell context
Packit 423ecb
 * @filename:  the file name (optional)
Packit 423ecb
 * @node:  unused
Packit 423ecb
 * @node2:  unused
Packit 423ecb
 *
Packit 423ecb
 * Implements the XML shell function "save"
Packit 423ecb
 * Write the current document to the filename, or it's original name
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 or -1 in case of error
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
Packit 423ecb
             xmlNodePtr node ATTRIBUTE_UNUSED,
Packit 423ecb
             xmlNodePtr node2 ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
    if ((ctxt == NULL) || (ctxt->doc == NULL))
Packit 423ecb
        return (-1);
Packit 423ecb
    if ((filename == NULL) || (filename[0] == 0))
Packit 423ecb
        filename = ctxt->filename;
Packit 423ecb
    if (filename == NULL)
Packit 423ecb
        return (-1);
Packit 423ecb
#ifdef W_OK
Packit 423ecb
    if (access((char *) filename, W_OK)) {
Packit 423ecb
        xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                        "Cannot save to %s\n", filename);
Packit 423ecb
        return (-1);
Packit 423ecb
    }
Packit 423ecb
#endif
Packit 423ecb
    switch (ctxt->doc->type) {
Packit 423ecb
        case XML_DOCUMENT_NODE:
Packit 423ecb
            if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
Packit 423ecb
                xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                "Failed to save to %s\n", filename);
Packit 423ecb
            }
Packit 423ecb
            break;
Packit 423ecb
        case XML_HTML_DOCUMENT_NODE:
Packit 423ecb
#ifdef LIBXML_HTML_ENABLED
Packit 423ecb
            if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
Packit 423ecb
                xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                "Failed to save to %s\n", filename);
Packit 423ecb
            }
Packit 423ecb
#else
Packit 423ecb
            if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
Packit 423ecb
                xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                "Failed to save to %s\n", filename);
Packit 423ecb
            }
Packit 423ecb
#endif /* LIBXML_HTML_ENABLED */
Packit 423ecb
            break;
Packit 423ecb
        default:
Packit 423ecb
            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
	    "To save to subparts of a document use the 'write' command\n");
Packit 423ecb
            return (-1);
Packit 423ecb
Packit 423ecb
    }
Packit 423ecb
    return (0);
Packit 423ecb
}
Packit 423ecb
#endif /* LIBXML_OUTPUT_ENABLED */
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_VALID_ENABLED
Packit 423ecb
/**
Packit 423ecb
 * xmlShellValidate:
Packit 423ecb
 * @ctxt:  the shell context
Packit 423ecb
 * @dtd:  the DTD URI (optional)
Packit 423ecb
 * @node:  unused
Packit 423ecb
 * @node2:  unused
Packit 423ecb
 *
Packit 423ecb
 * Implements the XML shell function "validate"
Packit 423ecb
 * Validate the document, if a DTD path is provided, then the validation
Packit 423ecb
 * is done against the given DTD.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 or -1 in case of error
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
Packit 423ecb
                 xmlNodePtr node ATTRIBUTE_UNUSED,
Packit 423ecb
                 xmlNodePtr node2 ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
    xmlValidCtxt vctxt;
Packit 423ecb
    int res = -1;
Packit 423ecb
Packit 423ecb
    if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
Packit 423ecb
    vctxt.userData = stderr;
Packit 423ecb
    vctxt.error = (xmlValidityErrorFunc) fprintf;
Packit 423ecb
    vctxt.warning = (xmlValidityWarningFunc) fprintf;
Packit 423ecb
Packit 423ecb
    if ((dtd == NULL) || (dtd[0] == 0)) {
Packit 423ecb
        res = xmlValidateDocument(&vctxt, ctxt->doc);
Packit 423ecb
    } else {
Packit 423ecb
        xmlDtdPtr subset;
Packit 423ecb
Packit 423ecb
        subset = xmlParseDTD(NULL, (xmlChar *) dtd);
Packit 423ecb
        if (subset != NULL) {
Packit 423ecb
            res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
Packit 423ecb
Packit 423ecb
            xmlFreeDtd(subset);
Packit 423ecb
        }
Packit 423ecb
    }
Packit 423ecb
    return (res);
Packit 423ecb
}
Packit 423ecb
#endif /* LIBXML_VALID_ENABLED */
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlShellDu:
Packit 423ecb
 * @ctxt:  the shell context
Packit 423ecb
 * @arg:  unused
Packit 423ecb
 * @tree:  a node defining a subtree
Packit 423ecb
 * @node2:  unused
Packit 423ecb
 *
Packit 423ecb
 * Implements the XML shell function "du"
Packit 423ecb
 * show the structure of the subtree under node @tree
Packit 423ecb
 * If @tree is null, the command works on the current node.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 or -1 in case of error
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlShellDu(xmlShellCtxtPtr ctxt,
Packit 423ecb
           char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
Packit 423ecb
           xmlNodePtr node2 ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
    xmlNodePtr node;
Packit 423ecb
    int indent = 0, i;
Packit 423ecb
Packit 423ecb
    if (!ctxt)
Packit 423ecb
	return (-1);
Packit 423ecb
Packit 423ecb
    if (tree == NULL)
Packit 423ecb
        return (-1);
Packit 423ecb
    node = tree;
Packit 423ecb
    while (node != NULL) {
Packit 423ecb
        if ((node->type == XML_DOCUMENT_NODE) ||
Packit 423ecb
            (node->type == XML_HTML_DOCUMENT_NODE)) {
Packit 423ecb
            fprintf(ctxt->output, "/\n");
Packit 423ecb
        } else if (node->type == XML_ELEMENT_NODE) {
Packit 423ecb
            for (i = 0; i < indent; i++)
Packit 423ecb
                fprintf(ctxt->output, "  ");
Packit 423ecb
            if ((node->ns) && (node->ns->prefix))
Packit 423ecb
                fprintf(ctxt->output, "%s:", node->ns->prefix);
Packit 423ecb
            fprintf(ctxt->output, "%s\n", node->name);
Packit 423ecb
        } else {
Packit 423ecb
        }
Packit 423ecb
Packit 423ecb
        /*
Packit 423ecb
         * Browse the full subtree, deep first
Packit 423ecb
         */
Packit 423ecb
Packit 423ecb
        if ((node->type == XML_DOCUMENT_NODE) ||
Packit 423ecb
            (node->type == XML_HTML_DOCUMENT_NODE)) {
Packit 423ecb
            node = ((xmlDocPtr) node)->children;
Packit 423ecb
        } else if ((node->children != NULL)
Packit 423ecb
                   && (node->type != XML_ENTITY_REF_NODE)) {
Packit 423ecb
            /* deep first */
Packit 423ecb
            node = node->children;
Packit 423ecb
            indent++;
Packit 423ecb
        } else if ((node != tree) && (node->next != NULL)) {
Packit 423ecb
            /* then siblings */
Packit 423ecb
            node = node->next;
Packit 423ecb
        } else if (node != tree) {
Packit 423ecb
            /* go up to parents->next if needed */
Packit 423ecb
            while (node != tree) {
Packit 423ecb
                if (node->parent != NULL) {
Packit 423ecb
                    node = node->parent;
Packit 423ecb
                    indent--;
Packit 423ecb
                }
Packit 423ecb
                if ((node != tree) && (node->next != NULL)) {
Packit 423ecb
                    node = node->next;
Packit 423ecb
                    break;
Packit 423ecb
                }
Packit 423ecb
                if (node->parent == NULL) {
Packit 423ecb
                    node = NULL;
Packit 423ecb
                    break;
Packit 423ecb
                }
Packit 423ecb
                if (node == tree) {
Packit 423ecb
                    node = NULL;
Packit 423ecb
                    break;
Packit 423ecb
                }
Packit 423ecb
            }
Packit 423ecb
            /* exit condition */
Packit 423ecb
            if (node == tree)
Packit 423ecb
                node = NULL;
Packit 423ecb
        } else
Packit 423ecb
            node = NULL;
Packit 423ecb
    }
Packit 423ecb
    return (0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlShellPwd:
Packit 423ecb
 * @ctxt:  the shell context
Packit 423ecb
 * @buffer:  the output buffer
Packit 423ecb
 * @node:  a node
Packit 423ecb
 * @node2:  unused
Packit 423ecb
 *
Packit 423ecb
 * Implements the XML shell function "pwd"
Packit 423ecb
 * Show the full path from the root to the node, if needed building
Packit 423ecb
 * thumblers when similar elements exists at a given ancestor level.
Packit 423ecb
 * The output is compatible with XPath commands.
Packit 423ecb
 *
Packit 423ecb
 * Returns 0 or -1 in case of error
Packit 423ecb
 */
Packit 423ecb
int
Packit 423ecb
xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
Packit 423ecb
            xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
Packit 423ecb
{
Packit 423ecb
    xmlChar *path;
Packit 423ecb
Packit 423ecb
    if ((node == NULL) || (buffer == NULL))
Packit 423ecb
        return (-1);
Packit 423ecb
Packit 423ecb
    path = xmlGetNodePath(node);
Packit 423ecb
    if (path == NULL)
Packit 423ecb
	return (-1);
Packit 423ecb
Packit 423ecb
    /*
Packit 423ecb
     * This test prevents buffer overflow, because this routine
Packit 423ecb
     * is only called by xmlShell, in which the second argument is
Packit 423ecb
     * 500 chars long.
Packit 423ecb
     * It is a dirty hack before a cleaner solution is found.
Packit 423ecb
     * Documentation should mention that the second argument must
Packit 423ecb
     * be at least 500 chars long, and could be stripped if too long.
Packit 423ecb
     */
Packit 423ecb
    snprintf(buffer, 499, "%s", path);
Packit 423ecb
    buffer[499] = '0';
Packit 423ecb
    xmlFree(path);
Packit 423ecb
Packit 423ecb
    return (0);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
/**
Packit 423ecb
 * xmlShell:
Packit 423ecb
 * @doc:  the initial document
Packit 423ecb
 * @filename:  the output buffer
Packit 423ecb
 * @input:  the line reading function
Packit 423ecb
 * @output:  the output FILE*, defaults to stdout if NULL
Packit 423ecb
 *
Packit 423ecb
 * Implements the XML shell
Packit 423ecb
 * This allow to load, validate, view, modify and save a document
Packit 423ecb
 * using a environment similar to a UNIX commandline.
Packit 423ecb
 */
Packit 423ecb
void
Packit 423ecb
xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Packit 423ecb
         FILE * output)
Packit 423ecb
{
Packit 423ecb
    char prompt[500] = "/ > ";
Packit 423ecb
    char *cmdline = NULL, *cur;
Packit 423ecb
    char command[100];
Packit 423ecb
    char arg[400];
Packit 423ecb
    int i;
Packit 423ecb
    xmlShellCtxtPtr ctxt;
Packit 423ecb
    xmlXPathObjectPtr list;
Packit 423ecb
Packit 423ecb
    if (doc == NULL)
Packit 423ecb
        return;
Packit 423ecb
    if (filename == NULL)
Packit 423ecb
        return;
Packit 423ecb
    if (input == NULL)
Packit 423ecb
        return;
Packit 423ecb
    if (output == NULL)
Packit 423ecb
        output = stdout;
Packit 423ecb
    ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Packit 423ecb
    if (ctxt == NULL)
Packit 423ecb
        return;
Packit 423ecb
    ctxt->loaded = 0;
Packit 423ecb
    ctxt->doc = doc;
Packit 423ecb
    ctxt->input = input;
Packit 423ecb
    ctxt->output = output;
Packit 423ecb
    ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Packit 423ecb
    ctxt->node = (xmlNodePtr) ctxt->doc;
Packit 423ecb
Packit 423ecb
#ifdef LIBXML_XPATH_ENABLED
Packit 423ecb
    ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
Packit 423ecb
    if (ctxt->pctxt == NULL) {
Packit 423ecb
        xmlFree(ctxt);
Packit 423ecb
        return;
Packit 423ecb
    }
Packit 423ecb
#endif /* LIBXML_XPATH_ENABLED */
Packit 423ecb
    while (1) {
Packit 423ecb
        if (ctxt->node == (xmlNodePtr) ctxt->doc)
Packit 423ecb
            snprintf(prompt, sizeof(prompt), "%s > ", "/");
Packit 423ecb
        else if ((ctxt->node != NULL) && (ctxt->node->name) &&
Packit 423ecb
                 (ctxt->node->ns) && (ctxt->node->ns->prefix))
Packit 423ecb
            snprintf(prompt, sizeof(prompt), "%s:%s > ",
Packit 423ecb
                     (ctxt->node->ns->prefix), ctxt->node->name);
Packit 423ecb
        else if ((ctxt->node != NULL) && (ctxt->node->name))
Packit 423ecb
            snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Packit 423ecb
        else
Packit 423ecb
            snprintf(prompt, sizeof(prompt), "? > ");
Packit 423ecb
        prompt[sizeof(prompt) - 1] = 0;
Packit 423ecb
Packit 423ecb
        /*
Packit 423ecb
         * Get a new command line
Packit 423ecb
         */
Packit 423ecb
        cmdline = ctxt->input(prompt);
Packit 423ecb
        if (cmdline == NULL)
Packit 423ecb
            break;
Packit 423ecb
Packit 423ecb
        /*
Packit 423ecb
         * Parse the command itself
Packit 423ecb
         */
Packit 423ecb
        cur = cmdline;
Packit 423ecb
        while ((*cur == ' ') || (*cur == '\t'))
Packit 423ecb
            cur++;
Packit 423ecb
        i = 0;
Packit 423ecb
        while ((*cur != ' ') && (*cur != '\t') &&
Packit 423ecb
               (*cur != '\n') && (*cur != '\r')) {
Packit 423ecb
            if (*cur == 0)
Packit 423ecb
                break;
Packit 423ecb
            command[i++] = *cur++;
Packit 423ecb
        }
Packit 423ecb
        command[i] = 0;
Packit 423ecb
        if (i == 0)
Packit 423ecb
            continue;
Packit 423ecb
Packit 423ecb
        /*
Packit 423ecb
         * Parse the argument
Packit 423ecb
         */
Packit 423ecb
        while ((*cur == ' ') || (*cur == '\t'))
Packit 423ecb
            cur++;
Packit 423ecb
        i = 0;
Packit 423ecb
        while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
Packit 423ecb
            if (*cur == 0)
Packit 423ecb
                break;
Packit 423ecb
            arg[i++] = *cur++;
Packit 423ecb
        }
Packit 423ecb
        arg[i] = 0;
Packit 423ecb
Packit 423ecb
        /*
Packit 423ecb
         * start interpreting the command
Packit 423ecb
         */
Packit 423ecb
        if (!strcmp(command, "exit"))
Packit 423ecb
            break;
Packit 423ecb
        if (!strcmp(command, "quit"))
Packit 423ecb
            break;
Packit 423ecb
        if (!strcmp(command, "bye"))
Packit 423ecb
            break;
Packit 423ecb
		if (!strcmp(command, "help")) {
Packit 423ecb
		  fprintf(ctxt->output, "\tbase         display XML base of the node\n");
Packit 423ecb
		  fprintf(ctxt->output, "\tsetbase URI  change the XML base of the node\n");
Packit 423ecb
		  fprintf(ctxt->output, "\tbye          leave shell\n");
Packit 423ecb
		  fprintf(ctxt->output, "\tcat [node]   display node or current node\n");
Packit 423ecb
		  fprintf(ctxt->output, "\tcd [path]    change directory to path or to root\n");
Packit 423ecb
		  fprintf(ctxt->output, "\tdir [path]   dumps informations about the node (namespace, attributes, content)\n");
Packit 423ecb
		  fprintf(ctxt->output, "\tdu [path]    show the structure of the subtree under path or the current node\n");
Packit 423ecb
		  fprintf(ctxt->output, "\texit         leave shell\n");
Packit 423ecb
		  fprintf(ctxt->output, "\thelp         display this help\n");
Packit 423ecb
		  fprintf(ctxt->output, "\tfree         display memory usage\n");
Packit 423ecb
		  fprintf(ctxt->output, "\tload [name]  load a new document with name\n");
Packit 423ecb
		  fprintf(ctxt->output, "\tls [path]    list contents of path or the current directory\n");
Packit 423ecb
		  fprintf(ctxt->output, "\tset xml_fragment replace the current node content with the fragment parsed in context\n");
Packit 423ecb
#ifdef LIBXML_XPATH_ENABLED
Packit 423ecb
		  fprintf(ctxt->output, "\txpath expr   evaluate the XPath expression in that context and print the result\n");
Packit 423ecb
		  fprintf(ctxt->output, "\tsetns nsreg  register a namespace to a prefix in the XPath evaluation context\n");
Packit 423ecb
		  fprintf(ctxt->output, "\t             format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
Packit 423ecb
		  fprintf(ctxt->output, "\tsetrootns    register all namespace found on the root element\n");
Packit 423ecb
		  fprintf(ctxt->output, "\t             the default namespace if any uses 'defaultns' prefix\n");
Packit 423ecb
#endif /* LIBXML_XPATH_ENABLED */
Packit 423ecb
		  fprintf(ctxt->output, "\tpwd          display current working directory\n");
Packit 423ecb
		  fprintf(ctxt->output, "\twhereis      display absolute path of [path] or current working directory\n");
Packit 423ecb
		  fprintf(ctxt->output, "\tquit         leave shell\n");
Packit 423ecb
#ifdef LIBXML_OUTPUT_ENABLED
Packit 423ecb
		  fprintf(ctxt->output, "\tsave [name]  save this document to name or the original name\n");
Packit 423ecb
		  fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
Packit 423ecb
#endif /* LIBXML_OUTPUT_ENABLED */
Packit 423ecb
#ifdef LIBXML_VALID_ENABLED
Packit 423ecb
		  fprintf(ctxt->output, "\tvalidate     check the document for errors\n");
Packit 423ecb
#endif /* LIBXML_VALID_ENABLED */
Packit 423ecb
#ifdef LIBXML_SCHEMAS_ENABLED
Packit 423ecb
		  fprintf(ctxt->output, "\trelaxng rng  validate the document against the Relax-NG schemas\n");
Packit 423ecb
#endif
Packit 423ecb
		  fprintf(ctxt->output, "\tgrep string  search for a string in the subtree\n");
Packit 423ecb
#ifdef LIBXML_VALID_ENABLED
Packit 423ecb
        } else if (!strcmp(command, "validate")) {
Packit 423ecb
            xmlShellValidate(ctxt, arg, NULL, NULL);
Packit 423ecb
#endif /* LIBXML_VALID_ENABLED */
Packit 423ecb
        } else if (!strcmp(command, "load")) {
Packit 423ecb
            xmlShellLoad(ctxt, arg, NULL, NULL);
Packit 423ecb
#ifdef LIBXML_SCHEMAS_ENABLED
Packit 423ecb
        } else if (!strcmp(command, "relaxng")) {
Packit 423ecb
            xmlShellRNGValidate(ctxt, arg, NULL, NULL);
Packit 423ecb
#endif
Packit 423ecb
#ifdef LIBXML_OUTPUT_ENABLED
Packit 423ecb
        } else if (!strcmp(command, "save")) {
Packit 423ecb
            xmlShellSave(ctxt, arg, NULL, NULL);
Packit 423ecb
        } else if (!strcmp(command, "write")) {
Packit 423ecb
	    if (arg[0] == 0)
Packit 423ecb
		xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                        "Write command requires a filename argument\n");
Packit 423ecb
	    else
Packit 423ecb
		xmlShellWrite(ctxt, arg, ctxt->node, NULL);
Packit 423ecb
#endif /* LIBXML_OUTPUT_ENABLED */
Packit 423ecb
        } else if (!strcmp(command, "grep")) {
Packit 423ecb
            xmlShellGrep(ctxt, arg, ctxt->node, NULL);
Packit 423ecb
        } else if (!strcmp(command, "free")) {
Packit 423ecb
            if (arg[0] == 0) {
Packit 423ecb
                xmlMemShow(ctxt->output, 0);
Packit 423ecb
            } else {
Packit 423ecb
                int len = 0;
Packit 423ecb
Packit 423ecb
                sscanf(arg, "%d", &len;;
Packit 423ecb
                xmlMemShow(ctxt->output, len);
Packit 423ecb
            }
Packit 423ecb
        } else if (!strcmp(command, "pwd")) {
Packit 423ecb
            char dir[500];
Packit 423ecb
Packit 423ecb
            if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
Packit 423ecb
                fprintf(ctxt->output, "%s\n", dir);
Packit 423ecb
        } else if (!strcmp(command, "du")) {
Packit 423ecb
            if (arg[0] == 0) {
Packit 423ecb
                xmlShellDu(ctxt, NULL, ctxt->node, NULL);
Packit 423ecb
            } else {
Packit 423ecb
                ctxt->pctxt->node = ctxt->node;
Packit 423ecb
#ifdef LIBXML_XPATH_ENABLED
Packit 423ecb
                ctxt->pctxt->node = ctxt->node;
Packit 423ecb
                list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Packit 423ecb
#else
Packit 423ecb
                list = NULL;
Packit 423ecb
#endif /* LIBXML_XPATH_ENABLED */
Packit 423ecb
                if (list != NULL) {
Packit 423ecb
                    switch (list->type) {
Packit 423ecb
                        case XPATH_UNDEFINED:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s: no such node\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_NODESET:{
Packit 423ecb
                            int indx;
Packit 423ecb
Packit 423ecb
                            if (list->nodesetval == NULL)
Packit 423ecb
                                break;
Packit 423ecb
Packit 423ecb
                            for (indx = 0;
Packit 423ecb
                                 indx < list->nodesetval->nodeNr;
Packit 423ecb
                                 indx++)
Packit 423ecb
                                xmlShellDu(ctxt, NULL,
Packit 423ecb
                                           list->nodesetval->
Packit 423ecb
                                           nodeTab[indx], NULL);
Packit 423ecb
                            break;
Packit 423ecb
                        }
Packit 423ecb
                        case XPATH_BOOLEAN:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a Boolean\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_NUMBER:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a number\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_STRING:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a string\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_POINT:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a point\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_RANGE:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a range\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_LOCATIONSET:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a range\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_USERS:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is user-defined\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_XSLT_TREE:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is an XSLT value tree\n",
Packit 423ecb
                                            arg);
Packit 423ecb
                            break;
Packit 423ecb
                    }
Packit 423ecb
#ifdef LIBXML_XPATH_ENABLED
Packit 423ecb
                    xmlXPathFreeObject(list);
Packit 423ecb
#endif
Packit 423ecb
                } else {
Packit 423ecb
                    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                    "%s: no such node\n", arg);
Packit 423ecb
                }
Packit 423ecb
                ctxt->pctxt->node = NULL;
Packit 423ecb
            }
Packit 423ecb
        } else if (!strcmp(command, "base")) {
Packit 423ecb
            xmlShellBase(ctxt, NULL, ctxt->node, NULL);
Packit 423ecb
        } else if (!strcmp(command, "set")) {
Packit 423ecb
	    xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
Packit 423ecb
#ifdef LIBXML_XPATH_ENABLED
Packit 423ecb
        } else if (!strcmp(command, "setns")) {
Packit 423ecb
            if (arg[0] == 0) {
Packit 423ecb
		xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
				"setns: prefix=[nsuri] required\n");
Packit 423ecb
            } else {
Packit 423ecb
                xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
Packit 423ecb
            }
Packit 423ecb
        } else if (!strcmp(command, "setrootns")) {
Packit 423ecb
	    xmlNodePtr root;
Packit 423ecb
Packit 423ecb
	    root = xmlDocGetRootElement(ctxt->doc);
Packit 423ecb
	    xmlShellRegisterRootNamespaces(ctxt, NULL, root, NULL);
Packit 423ecb
        } else if (!strcmp(command, "xpath")) {
Packit 423ecb
            if (arg[0] == 0) {
Packit 423ecb
		xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
				"xpath: expression required\n");
Packit 423ecb
	    } else {
Packit 423ecb
                ctxt->pctxt->node = ctxt->node;
Packit 423ecb
                list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Packit 423ecb
		xmlXPathDebugDumpObject(ctxt->output, list, 0);
Packit 423ecb
		xmlXPathFreeObject(list);
Packit 423ecb
	    }
Packit 423ecb
#endif /* LIBXML_XPATH_ENABLED */
Packit 423ecb
#ifdef LIBXML_TREE_ENABLED
Packit 423ecb
        } else if (!strcmp(command, "setbase")) {
Packit 423ecb
            xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
Packit 423ecb
#endif
Packit 423ecb
        } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
Packit 423ecb
            int dir = (!strcmp(command, "dir"));
Packit 423ecb
Packit 423ecb
            if (arg[0] == 0) {
Packit 423ecb
                if (dir)
Packit 423ecb
                    xmlShellDir(ctxt, NULL, ctxt->node, NULL);
Packit 423ecb
                else
Packit 423ecb
                    xmlShellList(ctxt, NULL, ctxt->node, NULL);
Packit 423ecb
            } else {
Packit 423ecb
                ctxt->pctxt->node = ctxt->node;
Packit 423ecb
#ifdef LIBXML_XPATH_ENABLED
Packit 423ecb
                ctxt->pctxt->node = ctxt->node;
Packit 423ecb
                list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Packit 423ecb
#else
Packit 423ecb
                list = NULL;
Packit 423ecb
#endif /* LIBXML_XPATH_ENABLED */
Packit 423ecb
                if (list != NULL) {
Packit 423ecb
                    switch (list->type) {
Packit 423ecb
                        case XPATH_UNDEFINED:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s: no such node\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_NODESET:{
Packit 423ecb
                                int indx;
Packit 423ecb
Packit 423ecb
				if (list->nodesetval == NULL)
Packit 423ecb
				    break;
Packit 423ecb
Packit 423ecb
                                for (indx = 0;
Packit 423ecb
                                     indx < list->nodesetval->nodeNr;
Packit 423ecb
                                     indx++) {
Packit 423ecb
                                    if (dir)
Packit 423ecb
                                        xmlShellDir(ctxt, NULL,
Packit 423ecb
                                                    list->nodesetval->
Packit 423ecb
                                                    nodeTab[indx], NULL);
Packit 423ecb
                                    else
Packit 423ecb
                                        xmlShellList(ctxt, NULL,
Packit 423ecb
                                                     list->nodesetval->
Packit 423ecb
                                                     nodeTab[indx], NULL);
Packit 423ecb
                                }
Packit 423ecb
                                break;
Packit 423ecb
                            }
Packit 423ecb
                        case XPATH_BOOLEAN:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a Boolean\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_NUMBER:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a number\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_STRING:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a string\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_POINT:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a point\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_RANGE:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a range\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_LOCATIONSET:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a range\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_USERS:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is user-defined\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_XSLT_TREE:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is an XSLT value tree\n",
Packit 423ecb
                                            arg);
Packit 423ecb
                            break;
Packit 423ecb
                    }
Packit 423ecb
#ifdef LIBXML_XPATH_ENABLED
Packit 423ecb
                    xmlXPathFreeObject(list);
Packit 423ecb
#endif
Packit 423ecb
                } else {
Packit 423ecb
                    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                    "%s: no such node\n", arg);
Packit 423ecb
                }
Packit 423ecb
                ctxt->pctxt->node = NULL;
Packit 423ecb
            }
Packit 423ecb
        } else if (!strcmp(command, "whereis")) {
Packit 423ecb
            char dir[500];
Packit 423ecb
Packit 423ecb
            if (arg[0] == 0) {
Packit 423ecb
                if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
Packit 423ecb
                    fprintf(ctxt->output, "%s\n", dir);
Packit 423ecb
            } else {
Packit 423ecb
                ctxt->pctxt->node = ctxt->node;
Packit 423ecb
#ifdef LIBXML_XPATH_ENABLED
Packit 423ecb
                list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Packit 423ecb
#else
Packit 423ecb
                list = NULL;
Packit 423ecb
#endif /* LIBXML_XPATH_ENABLED */
Packit 423ecb
                if (list != NULL) {
Packit 423ecb
                    switch (list->type) {
Packit 423ecb
                        case XPATH_UNDEFINED:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s: no such node\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_NODESET:{
Packit 423ecb
                                int indx;
Packit 423ecb
Packit 423ecb
				if (list->nodesetval == NULL)
Packit 423ecb
				    break;
Packit 423ecb
Packit 423ecb
                                for (indx = 0;
Packit 423ecb
                                     indx < list->nodesetval->nodeNr;
Packit 423ecb
                                     indx++) {
Packit 423ecb
                                    if (!xmlShellPwd(ctxt, dir, list->nodesetval->
Packit 423ecb
                                                     nodeTab[indx], NULL))
Packit 423ecb
                                        fprintf(ctxt->output, "%s\n", dir);
Packit 423ecb
                                }
Packit 423ecb
                                break;
Packit 423ecb
                            }
Packit 423ecb
                        case XPATH_BOOLEAN:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a Boolean\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_NUMBER:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a number\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_STRING:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a string\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_POINT:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a point\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_RANGE:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a range\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_LOCATIONSET:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a range\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_USERS:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is user-defined\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_XSLT_TREE:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is an XSLT value tree\n",
Packit 423ecb
                                            arg);
Packit 423ecb
                            break;
Packit 423ecb
                    }
Packit 423ecb
#ifdef LIBXML_XPATH_ENABLED
Packit 423ecb
                    xmlXPathFreeObject(list);
Packit 423ecb
#endif
Packit 423ecb
                } else {
Packit 423ecb
                    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                    "%s: no such node\n", arg);
Packit 423ecb
                }
Packit 423ecb
                ctxt->pctxt->node = NULL;
Packit 423ecb
            }
Packit 423ecb
        } else if (!strcmp(command, "cd")) {
Packit 423ecb
            if (arg[0] == 0) {
Packit 423ecb
                ctxt->node = (xmlNodePtr) ctxt->doc;
Packit 423ecb
            } else {
Packit 423ecb
#ifdef LIBXML_XPATH_ENABLED
Packit 423ecb
                int l;
Packit 423ecb
Packit 423ecb
                ctxt->pctxt->node = ctxt->node;
Packit 423ecb
		l = strlen(arg);
Packit 423ecb
		if ((l >= 2) && (arg[l - 1] == '/'))
Packit 423ecb
		    arg[l - 1] = 0;
Packit 423ecb
                list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Packit 423ecb
#else
Packit 423ecb
                list = NULL;
Packit 423ecb
#endif /* LIBXML_XPATH_ENABLED */
Packit 423ecb
                if (list != NULL) {
Packit 423ecb
                    switch (list->type) {
Packit 423ecb
                        case XPATH_UNDEFINED:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s: no such node\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_NODESET:
Packit 423ecb
                            if (list->nodesetval != NULL) {
Packit 423ecb
				if (list->nodesetval->nodeNr == 1) {
Packit 423ecb
				    ctxt->node = list->nodesetval->nodeTab[0];
Packit 423ecb
				    if ((ctxt->node != NULL) &&
Packit 423ecb
				        (ctxt->node->type ==
Packit 423ecb
					 XML_NAMESPACE_DECL)) {
Packit 423ecb
					xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
						    "cannot cd to namespace\n");
Packit 423ecb
					ctxt->node = NULL;
Packit 423ecb
				    }
Packit 423ecb
				} else
Packit 423ecb
				    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
						    "%s is a %d Node Set\n",
Packit 423ecb
						    arg,
Packit 423ecb
						    list->nodesetval->nodeNr);
Packit 423ecb
                            } else
Packit 423ecb
                                xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                                "%s is an empty Node Set\n",
Packit 423ecb
                                                arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_BOOLEAN:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a Boolean\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_NUMBER:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a number\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_STRING:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a string\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_POINT:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a point\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_RANGE:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a range\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_LOCATIONSET:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a range\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_USERS:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is user-defined\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_XSLT_TREE:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is an XSLT value tree\n",
Packit 423ecb
                                            arg);
Packit 423ecb
                            break;
Packit 423ecb
                    }
Packit 423ecb
#ifdef LIBXML_XPATH_ENABLED
Packit 423ecb
                    xmlXPathFreeObject(list);
Packit 423ecb
#endif
Packit 423ecb
                } else {
Packit 423ecb
                    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                    "%s: no such node\n", arg);
Packit 423ecb
                }
Packit 423ecb
                ctxt->pctxt->node = NULL;
Packit 423ecb
            }
Packit 423ecb
#ifdef LIBXML_OUTPUT_ENABLED
Packit 423ecb
        } else if (!strcmp(command, "cat")) {
Packit 423ecb
            if (arg[0] == 0) {
Packit 423ecb
                xmlShellCat(ctxt, NULL, ctxt->node, NULL);
Packit 423ecb
            } else {
Packit 423ecb
                ctxt->pctxt->node = ctxt->node;
Packit 423ecb
#ifdef LIBXML_XPATH_ENABLED
Packit 423ecb
                ctxt->pctxt->node = ctxt->node;
Packit 423ecb
                list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Packit 423ecb
#else
Packit 423ecb
                list = NULL;
Packit 423ecb
#endif /* LIBXML_XPATH_ENABLED */
Packit 423ecb
                if (list != NULL) {
Packit 423ecb
                    switch (list->type) {
Packit 423ecb
                        case XPATH_UNDEFINED:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s: no such node\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_NODESET:{
Packit 423ecb
                                int indx;
Packit 423ecb
Packit 423ecb
				if (list->nodesetval == NULL)
Packit 423ecb
				    break;
Packit 423ecb
Packit 423ecb
                                for (indx = 0;
Packit 423ecb
                                     indx < list->nodesetval->nodeNr;
Packit 423ecb
                                     indx++) {
Packit 423ecb
                                    if (i > 0)
Packit 423ecb
                                        fprintf(ctxt->output, " -------\n");
Packit 423ecb
                                    xmlShellCat(ctxt, NULL,
Packit 423ecb
                                                list->nodesetval->
Packit 423ecb
                                                nodeTab[indx], NULL);
Packit 423ecb
                                }
Packit 423ecb
                                break;
Packit 423ecb
                            }
Packit 423ecb
                        case XPATH_BOOLEAN:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a Boolean\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_NUMBER:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a number\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_STRING:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a string\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_POINT:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a point\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_RANGE:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a range\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_LOCATIONSET:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is a range\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_USERS:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is user-defined\n", arg);
Packit 423ecb
                            break;
Packit 423ecb
                        case XPATH_XSLT_TREE:
Packit 423ecb
                            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                            "%s is an XSLT value tree\n",
Packit 423ecb
                                            arg);
Packit 423ecb
                            break;
Packit 423ecb
                    }
Packit 423ecb
#ifdef LIBXML_XPATH_ENABLED
Packit 423ecb
                    xmlXPathFreeObject(list);
Packit 423ecb
#endif
Packit 423ecb
                } else {
Packit 423ecb
                    xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                                    "%s: no such node\n", arg);
Packit 423ecb
                }
Packit 423ecb
                ctxt->pctxt->node = NULL;
Packit 423ecb
            }
Packit 423ecb
#endif /* LIBXML_OUTPUT_ENABLED */
Packit 423ecb
        } else {
Packit 423ecb
            xmlGenericError(xmlGenericErrorContext,
Packit 423ecb
                            "Unknown command %s\n", command);
Packit 423ecb
        }
Packit 423ecb
        free(cmdline);          /* not xmlFree here ! */
Packit 423ecb
	cmdline = NULL;
Packit 423ecb
    }
Packit 423ecb
#ifdef LIBXML_XPATH_ENABLED
Packit 423ecb
    xmlXPathFreeContext(ctxt->pctxt);
Packit 423ecb
#endif /* LIBXML_XPATH_ENABLED */
Packit 423ecb
    if (ctxt->loaded) {
Packit 423ecb
        xmlFreeDoc(ctxt->doc);
Packit 423ecb
    }
Packit 423ecb
    if (ctxt->filename != NULL)
Packit 423ecb
        xmlFree(ctxt->filename);
Packit 423ecb
    xmlFree(ctxt);
Packit 423ecb
    if (cmdline != NULL)
Packit 423ecb
        free(cmdline);          /* not xmlFree here ! */
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#endif /* LIBXML_XPATH_ENABLED */
Packit 423ecb
#define bottom_debugXML
Packit 423ecb
#include "elfgcchack.h"
Packit 423ecb
#endif /* LIBXML_DEBUG_ENABLED */