• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * debugXML.c : This is a set of routines used for debugging the tree
3  *              produced by the XML parser.
4  *
5  * See Copyright for the status of this software.
6  *
7  * Daniel Veillard <daniel@veillard.com>
8  */
9 
10 #define IN_LIBXML
11 #include "libxml.h"
12 #ifdef LIBXML_DEBUG_ENABLED
13 
14 #include <string.h>
15 #include <stdlib.h>
16 
17 #include <libxml/xmlmemory.h>
18 #include <libxml/tree.h>
19 #include <libxml/parser.h>
20 #include <libxml/parserInternals.h>
21 #include <libxml/valid.h>
22 #include <libxml/debugXML.h>
23 #include <libxml/HTMLtree.h>
24 #include <libxml/HTMLparser.h>
25 #include <libxml/xmlerror.h>
26 #include <libxml/globals.h>
27 #include <libxml/xpathInternals.h>
28 #include <libxml/uri.h>
29 #ifdef LIBXML_SCHEMAS_ENABLED
30 #include <libxml/relaxng.h>
31 #endif
32 
33 #include "private/error.h"
34 
35 #define DUMP_TEXT_TYPE 1
36 
37 typedef struct _xmlDebugCtxt xmlDebugCtxt;
38 typedef xmlDebugCtxt *xmlDebugCtxtPtr;
39 struct _xmlDebugCtxt {
40     FILE *output;               /* the output file */
41     char shift[101];            /* used for indenting */
42     int depth;                  /* current depth */
43     xmlDocPtr doc;              /* current document */
44     xmlNodePtr node;		/* current node */
45     xmlDictPtr dict;		/* the doc dictionary */
46     int check;                  /* do just checkings */
47     int errors;                 /* number of errors found */
48     int nodict;			/* if the document has no dictionary */
49     int options;		/* options */
50 };
51 
52 static void xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node);
53 
54 static void
xmlCtxtDumpInitCtxt(xmlDebugCtxtPtr ctxt)55 xmlCtxtDumpInitCtxt(xmlDebugCtxtPtr ctxt)
56 {
57     int i;
58 
59     ctxt->depth = 0;
60     ctxt->check = 0;
61     ctxt->errors = 0;
62     ctxt->output = stdout;
63     ctxt->doc = NULL;
64     ctxt->node = NULL;
65     ctxt->dict = NULL;
66     ctxt->nodict = 0;
67     ctxt->options = 0;
68     for (i = 0; i < 100; i++)
69         ctxt->shift[i] = ' ';
70     ctxt->shift[100] = 0;
71 }
72 
73 static void
xmlCtxtDumpCleanCtxt(xmlDebugCtxtPtr ctxt ATTRIBUTE_UNUSED)74 xmlCtxtDumpCleanCtxt(xmlDebugCtxtPtr ctxt ATTRIBUTE_UNUSED)
75 {
76  /* remove the ATTRIBUTE_UNUSED when this is added */
77 }
78 
79 /**
80  * xmlNsCheckScope:
81  * @node: the node
82  * @ns: the namespace node
83  *
84  * Check that a given namespace is in scope on a node.
85  *
86  * Returns 1 if in scope, -1 in case of argument error,
87  *         -2 if the namespace is not in scope, and -3 if not on
88  *         an ancestor node.
89  */
90 static int
xmlNsCheckScope(xmlNodePtr node,xmlNsPtr ns)91 xmlNsCheckScope(xmlNodePtr node, xmlNsPtr ns)
92 {
93     xmlNsPtr cur;
94 
95     if ((node == NULL) || (ns == NULL))
96         return(-1);
97 
98     if ((node->type != XML_ELEMENT_NODE) &&
99 	(node->type != XML_ATTRIBUTE_NODE) &&
100 	(node->type != XML_DOCUMENT_NODE) &&
101 	(node->type != XML_TEXT_NODE) &&
102 	(node->type != XML_HTML_DOCUMENT_NODE) &&
103 	(node->type != XML_XINCLUDE_START))
104 	return(-2);
105 
106     while ((node != NULL) &&
107            ((node->type == XML_ELEMENT_NODE) ||
108             (node->type == XML_ATTRIBUTE_NODE) ||
109             (node->type == XML_TEXT_NODE) ||
110 	    (node->type == XML_XINCLUDE_START))) {
111 	if ((node->type == XML_ELEMENT_NODE) ||
112 	    (node->type == XML_XINCLUDE_START)) {
113 	    cur = node->nsDef;
114 	    while (cur != NULL) {
115 	        if (cur == ns)
116 		    return(1);
117 		if (xmlStrEqual(cur->prefix, ns->prefix))
118 		    return(-2);
119 		cur = cur->next;
120 	    }
121 	}
122 	node = node->parent;
123     }
124     /* the xml namespace may be declared on the document node */
125     if ((node != NULL) &&
126         ((node->type == XML_DOCUMENT_NODE) ||
127 	 (node->type == XML_HTML_DOCUMENT_NODE))) {
128 	 xmlNsPtr oldNs = ((xmlDocPtr) node)->oldNs;
129 	 if (oldNs == ns)
130 	     return(1);
131     }
132     return(-3);
133 }
134 
135 static void
xmlCtxtDumpSpaces(xmlDebugCtxtPtr ctxt)136 xmlCtxtDumpSpaces(xmlDebugCtxtPtr ctxt)
137 {
138     if (ctxt->check)
139         return;
140     if ((ctxt->output != NULL) && (ctxt->depth > 0)) {
141         if (ctxt->depth < 50)
142             fprintf(ctxt->output, "%s", &ctxt->shift[100 - 2 * ctxt->depth]);
143         else
144             fprintf(ctxt->output, "%s", ctxt->shift);
145     }
146 }
147 
148 /**
149  * xmlDebugErr:
150  * @ctxt:  a debug context
151  * @error:  the error code
152  *
153  * Handle a debug error.
154  */
155 static void
xmlDebugErr(xmlDebugCtxtPtr ctxt,int error,const char * msg)156 xmlDebugErr(xmlDebugCtxtPtr ctxt, int error, const char *msg)
157 {
158     ctxt->errors++;
159     __xmlRaiseError(NULL, NULL, NULL,
160 		    NULL, ctxt->node, XML_FROM_CHECK,
161 		    error, XML_ERR_ERROR, NULL, 0,
162 		    NULL, NULL, NULL, 0, 0,
163 		    "%s", msg);
164 }
165 static void LIBXML_ATTR_FORMAT(3,0)
xmlDebugErr2(xmlDebugCtxtPtr ctxt,int error,const char * msg,int extra)166 xmlDebugErr2(xmlDebugCtxtPtr ctxt, int error, const char *msg, int extra)
167 {
168     ctxt->errors++;
169     __xmlRaiseError(NULL, NULL, NULL,
170 		    NULL, ctxt->node, XML_FROM_CHECK,
171 		    error, XML_ERR_ERROR, NULL, 0,
172 		    NULL, NULL, NULL, 0, 0,
173 		    msg, extra);
174 }
175 static void LIBXML_ATTR_FORMAT(3,0)
xmlDebugErr3(xmlDebugCtxtPtr ctxt,int error,const char * msg,const char * extra)176 xmlDebugErr3(xmlDebugCtxtPtr ctxt, int error, const char *msg, const char *extra)
177 {
178     ctxt->errors++;
179     __xmlRaiseError(NULL, NULL, NULL,
180 		    NULL, ctxt->node, XML_FROM_CHECK,
181 		    error, XML_ERR_ERROR, NULL, 0,
182 		    NULL, NULL, NULL, 0, 0,
183 		    msg, extra);
184 }
185 
186 /**
187  * xmlCtxtNsCheckScope:
188  * @ctxt: the debugging context
189  * @node: the node
190  * @ns: the namespace node
191  *
192  * Report if a given namespace is is not in scope.
193  */
194 static void
xmlCtxtNsCheckScope(xmlDebugCtxtPtr ctxt,xmlNodePtr node,xmlNsPtr ns)195 xmlCtxtNsCheckScope(xmlDebugCtxtPtr ctxt, xmlNodePtr node, xmlNsPtr ns)
196 {
197     int ret;
198 
199     ret = xmlNsCheckScope(node, ns);
200     if (ret == -2) {
201         if (ns->prefix == NULL)
202 	    xmlDebugErr(ctxt, XML_CHECK_NS_SCOPE,
203 			"Reference to default namespace not in scope\n");
204 	else
205 	    xmlDebugErr3(ctxt, XML_CHECK_NS_SCOPE,
206 			 "Reference to namespace '%s' not in scope\n",
207 			 (char *) ns->prefix);
208     }
209     if (ret == -3) {
210         if (ns->prefix == NULL)
211 	    xmlDebugErr(ctxt, XML_CHECK_NS_ANCESTOR,
212 			"Reference to default namespace not on ancestor\n");
213 	else
214 	    xmlDebugErr3(ctxt, XML_CHECK_NS_ANCESTOR,
215 			 "Reference to namespace '%s' not on ancestor\n",
216 			 (char *) ns->prefix);
217     }
218 }
219 
220 /**
221  * xmlCtxtCheckString:
222  * @ctxt: the debug context
223  * @str: the string
224  *
225  * Do debugging on the string, currently it just checks the UTF-8 content
226  */
227 static void
xmlCtxtCheckString(xmlDebugCtxtPtr ctxt,const xmlChar * str)228 xmlCtxtCheckString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
229 {
230     if (str == NULL) return;
231     if (ctxt->check) {
232         if (!xmlCheckUTF8(str)) {
233 	    xmlDebugErr3(ctxt, XML_CHECK_NOT_UTF8,
234 			 "String is not UTF-8 %s", (const char *) str);
235 	}
236     }
237 }
238 
239 /**
240  * xmlCtxtCheckName:
241  * @ctxt: the debug context
242  * @name: the name
243  *
244  * Do debugging on the name, for example the dictionary status and
245  * conformance to the Name production.
246  */
247 static void
xmlCtxtCheckName(xmlDebugCtxtPtr ctxt,const xmlChar * name)248 xmlCtxtCheckName(xmlDebugCtxtPtr ctxt, const xmlChar * name)
249 {
250     if (ctxt->check) {
251 	if (name == NULL) {
252 	    xmlDebugErr(ctxt, XML_CHECK_NO_NAME, "Name is NULL");
253 	    return;
254 	}
255 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
256         if (xmlValidateName(name, 0)) {
257 	    xmlDebugErr3(ctxt, XML_CHECK_NOT_NCNAME,
258 			 "Name is not an NCName '%s'", (const char *) name);
259 	}
260 #endif
261 	if ((ctxt->dict != NULL) &&
262 	    (!xmlDictOwns(ctxt->dict, name)) &&
263             ((ctxt->doc == NULL) ||
264              ((ctxt->doc->parseFlags & (XML_PARSE_SAX1 | XML_PARSE_NODICT)) == 0))) {
265 	    xmlDebugErr3(ctxt, XML_CHECK_OUTSIDE_DICT,
266 			 "Name is not from the document dictionary '%s'",
267 			 (const char *) name);
268 	}
269     }
270 }
271 
272 static void
xmlCtxtGenericNodeCheck(xmlDebugCtxtPtr ctxt,xmlNodePtr node)273 xmlCtxtGenericNodeCheck(xmlDebugCtxtPtr ctxt, xmlNodePtr node) {
274     xmlDocPtr doc;
275     xmlDictPtr dict;
276 
277     doc = node->doc;
278 
279     if (node->parent == NULL)
280         xmlDebugErr(ctxt, XML_CHECK_NO_PARENT,
281 	            "Node has no parent\n");
282     if (node->doc == NULL) {
283         xmlDebugErr(ctxt, XML_CHECK_NO_DOC,
284 	            "Node has no doc\n");
285         dict = NULL;
286     } else {
287 	dict = doc->dict;
288 	if ((dict == NULL) && (ctxt->nodict == 0)) {
289 #if 0
290             /* deactivated right now as it raises too many errors */
291 	    if (doc->type == XML_DOCUMENT_NODE)
292 		xmlDebugErr(ctxt, XML_CHECK_NO_DICT,
293 			    "Document has no dictionary\n");
294 #endif
295 	    ctxt->nodict = 1;
296 	}
297 	if (ctxt->doc == NULL)
298 	    ctxt->doc = doc;
299 
300 	if (ctxt->dict == NULL) {
301 	    ctxt->dict = dict;
302 	}
303     }
304     if ((node->parent != NULL) && (node->doc != node->parent->doc) &&
305         (!xmlStrEqual(node->name, BAD_CAST "pseudoroot")))
306         xmlDebugErr(ctxt, XML_CHECK_WRONG_DOC,
307 	            "Node doc differs from parent's one\n");
308     if (node->prev == NULL) {
309         if (node->type == XML_ATTRIBUTE_NODE) {
310 	    if ((node->parent != NULL) &&
311 	        (node != (xmlNodePtr) node->parent->properties))
312 		xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
313                     "Attr has no prev and not first of attr list\n");
314 
315         } else if ((node->parent != NULL) && (node->parent->children != node))
316 	    xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
317                     "Node has no prev and not first of parent list\n");
318     } else {
319         if (node->prev->next != node)
320 	    xmlDebugErr(ctxt, XML_CHECK_WRONG_PREV,
321                         "Node prev->next : back link wrong\n");
322     }
323     if (node->next == NULL) {
324 	if ((node->parent != NULL) && (node->type != XML_ATTRIBUTE_NODE) &&
325 	    (node->parent->last != node) &&
326 	    (node->parent->type == XML_ELEMENT_NODE))
327 	    xmlDebugErr(ctxt, XML_CHECK_NO_NEXT,
328                     "Node has no next and not last of parent list\n");
329     } else {
330         if (node->next->prev != node)
331 	    xmlDebugErr(ctxt, XML_CHECK_WRONG_NEXT,
332                     "Node next->prev : forward link wrong\n");
333         if (node->next->parent != node->parent)
334 	    xmlDebugErr(ctxt, XML_CHECK_WRONG_PARENT,
335                     "Node next->prev : forward link wrong\n");
336     }
337     if (node->type == XML_ELEMENT_NODE) {
338         xmlNsPtr ns;
339 
340 	ns = node->nsDef;
341 	while (ns != NULL) {
342 	    xmlCtxtNsCheckScope(ctxt, node, ns);
343 	    ns = ns->next;
344 	}
345 	if (node->ns != NULL)
346 	    xmlCtxtNsCheckScope(ctxt, node, node->ns);
347     } else if (node->type == XML_ATTRIBUTE_NODE) {
348 	if (node->ns != NULL)
349 	    xmlCtxtNsCheckScope(ctxt, node, node->ns);
350     }
351 
352     if ((node->type != XML_ELEMENT_NODE) &&
353 	(node->type != XML_ATTRIBUTE_NODE) &&
354 	(node->type != XML_ELEMENT_DECL) &&
355 	(node->type != XML_ATTRIBUTE_DECL) &&
356 	(node->type != XML_DTD_NODE) &&
357 	(node->type != XML_HTML_DOCUMENT_NODE) &&
358 	(node->type != XML_DOCUMENT_NODE)) {
359 	if (node->content != NULL)
360 	    xmlCtxtCheckString(ctxt, (const xmlChar *) node->content);
361     }
362     switch (node->type) {
363         case XML_ELEMENT_NODE:
364         case XML_ATTRIBUTE_NODE:
365 	    xmlCtxtCheckName(ctxt, node->name);
366 	    break;
367         case XML_TEXT_NODE:
368 	    if ((node->name == xmlStringText) ||
369 	        (node->name == xmlStringTextNoenc))
370 		break;
371 	    /* some case of entity substitution can lead to this */
372 	    if ((ctxt->dict != NULL) &&
373 	        (node->name == xmlDictLookup(ctxt->dict, BAD_CAST "nbktext",
374 		                             7)))
375 		break;
376 
377 	    xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
378 			 "Text node has wrong name '%s'",
379 			 (const char *) node->name);
380 	    break;
381         case XML_COMMENT_NODE:
382 	    if (node->name == xmlStringComment)
383 		break;
384 	    xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
385 			 "Comment node has wrong name '%s'",
386 			 (const char *) node->name);
387 	    break;
388         case XML_PI_NODE:
389 	    xmlCtxtCheckName(ctxt, node->name);
390 	    break;
391         case XML_CDATA_SECTION_NODE:
392 	    if (node->name == NULL)
393 		break;
394 	    xmlDebugErr3(ctxt, XML_CHECK_NAME_NOT_NULL,
395 			 "CData section has non NULL name '%s'",
396 			 (const char *) node->name);
397 	    break;
398         case XML_ENTITY_REF_NODE:
399         case XML_ENTITY_NODE:
400         case XML_DOCUMENT_TYPE_NODE:
401         case XML_DOCUMENT_FRAG_NODE:
402         case XML_NOTATION_NODE:
403         case XML_DTD_NODE:
404         case XML_ELEMENT_DECL:
405         case XML_ATTRIBUTE_DECL:
406         case XML_ENTITY_DECL:
407         case XML_NAMESPACE_DECL:
408         case XML_XINCLUDE_START:
409         case XML_XINCLUDE_END:
410         case XML_DOCUMENT_NODE:
411         case XML_HTML_DOCUMENT_NODE:
412 	    break;
413     }
414 }
415 
416 static void
xmlCtxtDumpString(xmlDebugCtxtPtr ctxt,const xmlChar * str)417 xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
418 {
419     int i;
420 
421     if (ctxt->check) {
422         return;
423     }
424     /* TODO: check UTF8 content of the string */
425     if (str == NULL) {
426         fprintf(ctxt->output, "(NULL)");
427         return;
428     }
429     for (i = 0; i < 40; i++)
430         if (str[i] == 0)
431             return;
432         else if (IS_BLANK_CH(str[i]))
433             fputc(' ', ctxt->output);
434         else if (str[i] >= 0x80)
435             fprintf(ctxt->output, "#%X", str[i]);
436         else
437             fputc(str[i], ctxt->output);
438     fprintf(ctxt->output, "...");
439 }
440 
441 static void
xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt,xmlDtdPtr dtd)442 xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
443 {
444     xmlCtxtDumpSpaces(ctxt);
445 
446     if (dtd == NULL) {
447         if (!ctxt->check)
448             fprintf(ctxt->output, "DTD node is NULL\n");
449         return;
450     }
451 
452     if (dtd->type != XML_DTD_NODE) {
453 	xmlDebugErr(ctxt, XML_CHECK_NOT_DTD,
454 	            "Node is not a DTD");
455         return;
456     }
457     if (!ctxt->check) {
458         if (dtd->name != NULL)
459             fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name);
460         else
461             fprintf(ctxt->output, "DTD");
462         if (dtd->ExternalID != NULL)
463             fprintf(ctxt->output, ", PUBLIC %s", (char *) dtd->ExternalID);
464         if (dtd->SystemID != NULL)
465             fprintf(ctxt->output, ", SYSTEM %s", (char *) dtd->SystemID);
466         fprintf(ctxt->output, "\n");
467     }
468     /*
469      * Do a bit of checking
470      */
471     xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd);
472 }
473 
474 static void
xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt,xmlAttributePtr attr)475 xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr)
476 {
477     xmlCtxtDumpSpaces(ctxt);
478 
479     if (attr == NULL) {
480         if (!ctxt->check)
481             fprintf(ctxt->output, "Attribute declaration is NULL\n");
482         return;
483     }
484     if (attr->type != XML_ATTRIBUTE_DECL) {
485 	xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL,
486 	            "Node is not an attribute declaration");
487         return;
488     }
489     if (attr->name != NULL) {
490         if (!ctxt->check)
491             fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name);
492     } else
493 	xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
494 	            "Node attribute declaration has no name");
495     if (attr->elem != NULL) {
496         if (!ctxt->check)
497             fprintf(ctxt->output, " for %s", (char *) attr->elem);
498     } else
499 	xmlDebugErr(ctxt, XML_CHECK_NO_ELEM,
500 	            "Node attribute declaration has no element name");
501     if (!ctxt->check) {
502         switch (attr->atype) {
503             case XML_ATTRIBUTE_CDATA:
504                 fprintf(ctxt->output, " CDATA");
505                 break;
506             case XML_ATTRIBUTE_ID:
507                 fprintf(ctxt->output, " ID");
508                 break;
509             case XML_ATTRIBUTE_IDREF:
510                 fprintf(ctxt->output, " IDREF");
511                 break;
512             case XML_ATTRIBUTE_IDREFS:
513                 fprintf(ctxt->output, " IDREFS");
514                 break;
515             case XML_ATTRIBUTE_ENTITY:
516                 fprintf(ctxt->output, " ENTITY");
517                 break;
518             case XML_ATTRIBUTE_ENTITIES:
519                 fprintf(ctxt->output, " ENTITIES");
520                 break;
521             case XML_ATTRIBUTE_NMTOKEN:
522                 fprintf(ctxt->output, " NMTOKEN");
523                 break;
524             case XML_ATTRIBUTE_NMTOKENS:
525                 fprintf(ctxt->output, " NMTOKENS");
526                 break;
527             case XML_ATTRIBUTE_ENUMERATION:
528                 fprintf(ctxt->output, " ENUMERATION");
529                 break;
530             case XML_ATTRIBUTE_NOTATION:
531                 fprintf(ctxt->output, " NOTATION ");
532                 break;
533         }
534         if (attr->tree != NULL) {
535             int indx;
536             xmlEnumerationPtr cur = attr->tree;
537 
538             for (indx = 0; indx < 5; indx++) {
539                 if (indx != 0)
540                     fprintf(ctxt->output, "|%s", (char *) cur->name);
541                 else
542                     fprintf(ctxt->output, " (%s", (char *) cur->name);
543                 cur = cur->next;
544                 if (cur == NULL)
545                     break;
546             }
547             if (cur == NULL)
548                 fprintf(ctxt->output, ")");
549             else
550                 fprintf(ctxt->output, "...)");
551         }
552         switch (attr->def) {
553             case XML_ATTRIBUTE_NONE:
554                 break;
555             case XML_ATTRIBUTE_REQUIRED:
556                 fprintf(ctxt->output, " REQUIRED");
557                 break;
558             case XML_ATTRIBUTE_IMPLIED:
559                 fprintf(ctxt->output, " IMPLIED");
560                 break;
561             case XML_ATTRIBUTE_FIXED:
562                 fprintf(ctxt->output, " FIXED");
563                 break;
564         }
565         if (attr->defaultValue != NULL) {
566             fprintf(ctxt->output, "\"");
567             xmlCtxtDumpString(ctxt, attr->defaultValue);
568             fprintf(ctxt->output, "\"");
569         }
570         fprintf(ctxt->output, "\n");
571     }
572 
573     /*
574      * Do a bit of checking
575      */
576     xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
577 }
578 
579 static void
xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt,xmlElementPtr elem)580 xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem)
581 {
582     xmlCtxtDumpSpaces(ctxt);
583 
584     if (elem == NULL) {
585         if (!ctxt->check)
586             fprintf(ctxt->output, "Element declaration is NULL\n");
587         return;
588     }
589     if (elem->type != XML_ELEMENT_DECL) {
590 	xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL,
591 	            "Node is not an element declaration");
592         return;
593     }
594     if (elem->name != NULL) {
595         if (!ctxt->check) {
596             fprintf(ctxt->output, "ELEMDECL(");
597             xmlCtxtDumpString(ctxt, elem->name);
598             fprintf(ctxt->output, ")");
599         }
600     } else
601 	xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
602 	            "Element declaration has no name");
603     if (!ctxt->check) {
604         switch (elem->etype) {
605             case XML_ELEMENT_TYPE_UNDEFINED:
606                 fprintf(ctxt->output, ", UNDEFINED");
607                 break;
608             case XML_ELEMENT_TYPE_EMPTY:
609                 fprintf(ctxt->output, ", EMPTY");
610                 break;
611             case XML_ELEMENT_TYPE_ANY:
612                 fprintf(ctxt->output, ", ANY");
613                 break;
614             case XML_ELEMENT_TYPE_MIXED:
615                 fprintf(ctxt->output, ", MIXED ");
616                 break;
617             case XML_ELEMENT_TYPE_ELEMENT:
618                 fprintf(ctxt->output, ", MIXED ");
619                 break;
620         }
621         if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) {
622             char buf[5001];
623 
624             buf[0] = 0;
625             xmlSnprintfElementContent(buf, 5000, elem->content, 1);
626             buf[5000] = 0;
627             fprintf(ctxt->output, "%s", buf);
628         }
629         fprintf(ctxt->output, "\n");
630     }
631 
632     /*
633      * Do a bit of checking
634      */
635     xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem);
636 }
637 
638 static void
xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt,xmlEntityPtr ent)639 xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
640 {
641     xmlCtxtDumpSpaces(ctxt);
642 
643     if (ent == NULL) {
644         if (!ctxt->check)
645             fprintf(ctxt->output, "Entity declaration is NULL\n");
646         return;
647     }
648     if (ent->type != XML_ENTITY_DECL) {
649 	xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL,
650 	            "Node is not an entity declaration");
651         return;
652     }
653     if (ent->name != NULL) {
654         if (!ctxt->check) {
655             fprintf(ctxt->output, "ENTITYDECL(");
656             xmlCtxtDumpString(ctxt, ent->name);
657             fprintf(ctxt->output, ")");
658         }
659     } else
660 	xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
661 	            "Entity declaration has no name");
662     if (!ctxt->check) {
663         switch (ent->etype) {
664             case XML_INTERNAL_GENERAL_ENTITY:
665                 fprintf(ctxt->output, ", internal\n");
666                 break;
667             case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
668                 fprintf(ctxt->output, ", external parsed\n");
669                 break;
670             case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
671                 fprintf(ctxt->output, ", unparsed\n");
672                 break;
673             case XML_INTERNAL_PARAMETER_ENTITY:
674                 fprintf(ctxt->output, ", parameter\n");
675                 break;
676             case XML_EXTERNAL_PARAMETER_ENTITY:
677                 fprintf(ctxt->output, ", external parameter\n");
678                 break;
679             case XML_INTERNAL_PREDEFINED_ENTITY:
680                 fprintf(ctxt->output, ", predefined\n");
681                 break;
682         }
683         if (ent->ExternalID) {
684             xmlCtxtDumpSpaces(ctxt);
685             fprintf(ctxt->output, " ExternalID=%s\n",
686                     (char *) ent->ExternalID);
687         }
688         if (ent->SystemID) {
689             xmlCtxtDumpSpaces(ctxt);
690             fprintf(ctxt->output, " SystemID=%s\n",
691                     (char *) ent->SystemID);
692         }
693         if (ent->URI != NULL) {
694             xmlCtxtDumpSpaces(ctxt);
695             fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI);
696         }
697         if (ent->content) {
698             xmlCtxtDumpSpaces(ctxt);
699             fprintf(ctxt->output, " content=");
700             xmlCtxtDumpString(ctxt, ent->content);
701             fprintf(ctxt->output, "\n");
702         }
703     }
704 
705     /*
706      * Do a bit of checking
707      */
708     xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent);
709 }
710 
711 static void
xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt,xmlNsPtr ns)712 xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
713 {
714     xmlCtxtDumpSpaces(ctxt);
715 
716     if (ns == NULL) {
717         if (!ctxt->check)
718             fprintf(ctxt->output, "namespace node is NULL\n");
719         return;
720     }
721     if (ns->type != XML_NAMESPACE_DECL) {
722 	xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL,
723 	            "Node is not a namespace declaration");
724         return;
725     }
726     if (ns->href == NULL) {
727         if (ns->prefix != NULL)
728 	    xmlDebugErr3(ctxt, XML_CHECK_NO_HREF,
729                     "Incomplete namespace %s href=NULL\n",
730                     (char *) ns->prefix);
731         else
732 	    xmlDebugErr(ctxt, XML_CHECK_NO_HREF,
733                     "Incomplete default namespace href=NULL\n");
734     } else {
735         if (!ctxt->check) {
736             if (ns->prefix != NULL)
737                 fprintf(ctxt->output, "namespace %s href=",
738                         (char *) ns->prefix);
739             else
740                 fprintf(ctxt->output, "default namespace href=");
741 
742             xmlCtxtDumpString(ctxt, ns->href);
743             fprintf(ctxt->output, "\n");
744         }
745     }
746 }
747 
748 static void
xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt,xmlNsPtr ns)749 xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
750 {
751     while (ns != NULL) {
752         xmlCtxtDumpNamespace(ctxt, ns);
753         ns = ns->next;
754     }
755 }
756 
757 static void
xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt,xmlEntityPtr ent)758 xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
759 {
760     xmlCtxtDumpSpaces(ctxt);
761 
762     if (ent == NULL) {
763         if (!ctxt->check)
764             fprintf(ctxt->output, "Entity is NULL\n");
765         return;
766     }
767     if (!ctxt->check) {
768         switch (ent->etype) {
769             case XML_INTERNAL_GENERAL_ENTITY:
770                 fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY ");
771                 break;
772             case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
773                 fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
774                 break;
775             case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
776                 fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
777                 break;
778             case XML_INTERNAL_PARAMETER_ENTITY:
779                 fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY ");
780                 break;
781             case XML_EXTERNAL_PARAMETER_ENTITY:
782                 fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY ");
783                 break;
784             default:
785                 fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype);
786         }
787         fprintf(ctxt->output, "%s\n", ent->name);
788         if (ent->ExternalID) {
789             xmlCtxtDumpSpaces(ctxt);
790             fprintf(ctxt->output, "ExternalID=%s\n",
791                     (char *) ent->ExternalID);
792         }
793         if (ent->SystemID) {
794             xmlCtxtDumpSpaces(ctxt);
795             fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID);
796         }
797         if (ent->URI) {
798             xmlCtxtDumpSpaces(ctxt);
799             fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI);
800         }
801         if (ent->content) {
802             xmlCtxtDumpSpaces(ctxt);
803             fprintf(ctxt->output, "content=");
804             xmlCtxtDumpString(ctxt, ent->content);
805             fprintf(ctxt->output, "\n");
806         }
807     }
808 }
809 
810 /**
811  * xmlCtxtDumpAttr:
812  * @output:  the FILE * for the output
813  * @attr:  the attribute
814  * @depth:  the indentation level.
815  *
816  * Dumps debug information for the attribute
817  */
818 static void
xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt,xmlAttrPtr attr)819 xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
820 {
821     xmlCtxtDumpSpaces(ctxt);
822 
823     if (attr == NULL) {
824         if (!ctxt->check)
825             fprintf(ctxt->output, "Attr is NULL");
826         return;
827     }
828     if (!ctxt->check) {
829         fprintf(ctxt->output, "ATTRIBUTE ");
830 	xmlCtxtDumpString(ctxt, attr->name);
831         fprintf(ctxt->output, "\n");
832         if (attr->children != NULL) {
833             ctxt->depth++;
834             xmlCtxtDumpNodeList(ctxt, attr->children);
835             ctxt->depth--;
836         }
837     }
838     if (attr->name == NULL)
839 	xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
840 	            "Attribute has no name");
841 
842     /*
843      * Do a bit of checking
844      */
845     xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
846 }
847 
848 /**
849  * xmlCtxtDumpAttrList:
850  * @output:  the FILE * for the output
851  * @attr:  the attribute list
852  * @depth:  the indentation level.
853  *
854  * Dumps debug information for the attribute list
855  */
856 static void
xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt,xmlAttrPtr attr)857 xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
858 {
859     while (attr != NULL) {
860         xmlCtxtDumpAttr(ctxt, attr);
861         attr = attr->next;
862     }
863 }
864 
865 /**
866  * xmlCtxtDumpOneNode:
867  * @output:  the FILE * for the output
868  * @node:  the node
869  * @depth:  the indentation level.
870  *
871  * Dumps debug information for the element node, it is not recursive
872  */
873 static void
xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt,xmlNodePtr node)874 xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
875 {
876     if (node == NULL) {
877         if (!ctxt->check) {
878             xmlCtxtDumpSpaces(ctxt);
879             fprintf(ctxt->output, "node is NULL\n");
880         }
881         return;
882     }
883     ctxt->node = node;
884 
885     switch (node->type) {
886         case XML_ELEMENT_NODE:
887             if (!ctxt->check) {
888                 xmlCtxtDumpSpaces(ctxt);
889                 fprintf(ctxt->output, "ELEMENT ");
890                 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
891                     xmlCtxtDumpString(ctxt, node->ns->prefix);
892                     fprintf(ctxt->output, ":");
893                 }
894                 xmlCtxtDumpString(ctxt, node->name);
895                 fprintf(ctxt->output, "\n");
896             }
897             break;
898         case XML_ATTRIBUTE_NODE:
899             if (!ctxt->check)
900                 xmlCtxtDumpSpaces(ctxt);
901             fprintf(ctxt->output, "Error, ATTRIBUTE found here\n");
902             xmlCtxtGenericNodeCheck(ctxt, node);
903             return;
904         case XML_TEXT_NODE:
905             if (!ctxt->check) {
906                 xmlCtxtDumpSpaces(ctxt);
907                 if (node->name == (const xmlChar *) xmlStringTextNoenc)
908                     fprintf(ctxt->output, "TEXT no enc");
909                 else
910                     fprintf(ctxt->output, "TEXT");
911 		if (ctxt->options & DUMP_TEXT_TYPE) {
912 		    if (node->content == (xmlChar *) &(node->properties))
913 			fprintf(ctxt->output, " compact\n");
914 		    else if (xmlDictOwns(ctxt->dict, node->content) == 1)
915 			fprintf(ctxt->output, " interned\n");
916 		    else
917 			fprintf(ctxt->output, "\n");
918 		} else
919 		    fprintf(ctxt->output, "\n");
920             }
921             break;
922         case XML_CDATA_SECTION_NODE:
923             if (!ctxt->check) {
924                 xmlCtxtDumpSpaces(ctxt);
925                 fprintf(ctxt->output, "CDATA_SECTION\n");
926             }
927             break;
928         case XML_ENTITY_REF_NODE:
929             if (!ctxt->check) {
930                 xmlCtxtDumpSpaces(ctxt);
931                 fprintf(ctxt->output, "ENTITY_REF(%s)\n",
932                         (char *) node->name);
933             }
934             break;
935         case XML_ENTITY_NODE:
936             if (!ctxt->check) {
937                 xmlCtxtDumpSpaces(ctxt);
938                 fprintf(ctxt->output, "ENTITY\n");
939             }
940             break;
941         case XML_PI_NODE:
942             if (!ctxt->check) {
943                 xmlCtxtDumpSpaces(ctxt);
944                 fprintf(ctxt->output, "PI %s\n", (char *) node->name);
945             }
946             break;
947         case XML_COMMENT_NODE:
948             if (!ctxt->check) {
949                 xmlCtxtDumpSpaces(ctxt);
950                 fprintf(ctxt->output, "COMMENT\n");
951             }
952             break;
953         case XML_DOCUMENT_NODE:
954         case XML_HTML_DOCUMENT_NODE:
955             if (!ctxt->check) {
956                 xmlCtxtDumpSpaces(ctxt);
957             }
958             fprintf(ctxt->output, "Error, DOCUMENT found here\n");
959             xmlCtxtGenericNodeCheck(ctxt, node);
960             return;
961         case XML_DOCUMENT_TYPE_NODE:
962             if (!ctxt->check) {
963                 xmlCtxtDumpSpaces(ctxt);
964                 fprintf(ctxt->output, "DOCUMENT_TYPE\n");
965             }
966             break;
967         case XML_DOCUMENT_FRAG_NODE:
968             if (!ctxt->check) {
969                 xmlCtxtDumpSpaces(ctxt);
970                 fprintf(ctxt->output, "DOCUMENT_FRAG\n");
971             }
972             break;
973         case XML_NOTATION_NODE:
974             if (!ctxt->check) {
975                 xmlCtxtDumpSpaces(ctxt);
976                 fprintf(ctxt->output, "NOTATION\n");
977             }
978             break;
979         case XML_DTD_NODE:
980             xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
981             return;
982         case XML_ELEMENT_DECL:
983             xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
984             return;
985         case XML_ATTRIBUTE_DECL:
986             xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
987             return;
988         case XML_ENTITY_DECL:
989             xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
990             return;
991         case XML_NAMESPACE_DECL:
992             xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
993             return;
994         case XML_XINCLUDE_START:
995             if (!ctxt->check) {
996                 xmlCtxtDumpSpaces(ctxt);
997                 fprintf(ctxt->output, "INCLUDE START\n");
998             }
999             return;
1000         case XML_XINCLUDE_END:
1001             if (!ctxt->check) {
1002                 xmlCtxtDumpSpaces(ctxt);
1003                 fprintf(ctxt->output, "INCLUDE END\n");
1004             }
1005             return;
1006         default:
1007             if (!ctxt->check)
1008                 xmlCtxtDumpSpaces(ctxt);
1009 	    xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1010 	                "Unknown node type %d\n", node->type);
1011             return;
1012     }
1013     if (node->doc == NULL) {
1014         if (!ctxt->check) {
1015             xmlCtxtDumpSpaces(ctxt);
1016         }
1017         fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
1018     }
1019     ctxt->depth++;
1020     if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL))
1021         xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
1022     if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL))
1023         xmlCtxtDumpAttrList(ctxt, node->properties);
1024     if (node->type != XML_ENTITY_REF_NODE) {
1025         if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
1026             if (!ctxt->check) {
1027                 xmlCtxtDumpSpaces(ctxt);
1028                 fprintf(ctxt->output, "content=");
1029                 xmlCtxtDumpString(ctxt, node->content);
1030                 fprintf(ctxt->output, "\n");
1031             }
1032         }
1033     } else {
1034         xmlEntityPtr ent;
1035 
1036         ent = xmlGetDocEntity(node->doc, node->name);
1037         if (ent != NULL)
1038             xmlCtxtDumpEntity(ctxt, ent);
1039     }
1040     ctxt->depth--;
1041 
1042     /*
1043      * Do a bit of checking
1044      */
1045     xmlCtxtGenericNodeCheck(ctxt, node);
1046 }
1047 
1048 /**
1049  * xmlCtxtDumpNode:
1050  * @output:  the FILE * for the output
1051  * @node:  the node
1052  * @depth:  the indentation level.
1053  *
1054  * Dumps debug information for the element node, it is recursive
1055  */
1056 static void
xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt,xmlNodePtr node)1057 xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1058 {
1059     if (node == NULL) {
1060         if (!ctxt->check) {
1061             xmlCtxtDumpSpaces(ctxt);
1062             fprintf(ctxt->output, "node is NULL\n");
1063         }
1064         return;
1065     }
1066     xmlCtxtDumpOneNode(ctxt, node);
1067     if ((node->type != XML_NAMESPACE_DECL) &&
1068         (node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
1069         ctxt->depth++;
1070         xmlCtxtDumpNodeList(ctxt, node->children);
1071         ctxt->depth--;
1072     }
1073 }
1074 
1075 /**
1076  * xmlCtxtDumpNodeList:
1077  * @output:  the FILE * for the output
1078  * @node:  the node list
1079  * @depth:  the indentation level.
1080  *
1081  * Dumps debug information for the list of element node, it is recursive
1082  */
1083 static void
xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt,xmlNodePtr node)1084 xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1085 {
1086     while (node != NULL) {
1087         xmlCtxtDumpNode(ctxt, node);
1088         node = node->next;
1089     }
1090 }
1091 
1092 static void
xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt,xmlDocPtr doc)1093 xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1094 {
1095     if (doc == NULL) {
1096         if (!ctxt->check)
1097             fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1098         return;
1099     }
1100     ctxt->node = (xmlNodePtr) doc;
1101 
1102     switch (doc->type) {
1103         case XML_ELEMENT_NODE:
1104 	    xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
1105 	                "Misplaced ELEMENT node\n");
1106             break;
1107         case XML_ATTRIBUTE_NODE:
1108 	    xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
1109 	                "Misplaced ATTRIBUTE node\n");
1110             break;
1111         case XML_TEXT_NODE:
1112 	    xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
1113 	                "Misplaced TEXT node\n");
1114             break;
1115         case XML_CDATA_SECTION_NODE:
1116 	    xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
1117 	                "Misplaced CDATA node\n");
1118             break;
1119         case XML_ENTITY_REF_NODE:
1120 	    xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
1121 	                "Misplaced ENTITYREF node\n");
1122             break;
1123         case XML_ENTITY_NODE:
1124 	    xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
1125 	                "Misplaced ENTITY node\n");
1126             break;
1127         case XML_PI_NODE:
1128 	    xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
1129 	                "Misplaced PI node\n");
1130             break;
1131         case XML_COMMENT_NODE:
1132 	    xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
1133 	                "Misplaced COMMENT node\n");
1134             break;
1135         case XML_DOCUMENT_NODE:
1136 	    if (!ctxt->check)
1137 		fprintf(ctxt->output, "DOCUMENT\n");
1138             break;
1139         case XML_HTML_DOCUMENT_NODE:
1140 	    if (!ctxt->check)
1141 		fprintf(ctxt->output, "HTML DOCUMENT\n");
1142             break;
1143         case XML_DOCUMENT_TYPE_NODE:
1144 	    xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
1145 	                "Misplaced DOCTYPE node\n");
1146             break;
1147         case XML_DOCUMENT_FRAG_NODE:
1148 	    xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
1149 	                "Misplaced FRAGMENT node\n");
1150             break;
1151         case XML_NOTATION_NODE:
1152 	    xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
1153 	                "Misplaced NOTATION node\n");
1154             break;
1155         default:
1156 	    xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1157 	                "Unknown node type %d\n", doc->type);
1158     }
1159 }
1160 
1161 /**
1162  * xmlCtxtDumpDocumentHead:
1163  * @output:  the FILE * for the output
1164  * @doc:  the document
1165  *
1166  * Dumps debug information concerning the document, not recursive
1167  */
1168 static void
xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt,xmlDocPtr doc)1169 xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1170 {
1171     if (doc == NULL) return;
1172     xmlCtxtDumpDocHead(ctxt, doc);
1173     if (!ctxt->check) {
1174         if (doc->name != NULL) {
1175             fprintf(ctxt->output, "name=");
1176             xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
1177             fprintf(ctxt->output, "\n");
1178         }
1179         if (doc->version != NULL) {
1180             fprintf(ctxt->output, "version=");
1181             xmlCtxtDumpString(ctxt, doc->version);
1182             fprintf(ctxt->output, "\n");
1183         }
1184         if (doc->encoding != NULL) {
1185             fprintf(ctxt->output, "encoding=");
1186             xmlCtxtDumpString(ctxt, doc->encoding);
1187             fprintf(ctxt->output, "\n");
1188         }
1189         if (doc->URL != NULL) {
1190             fprintf(ctxt->output, "URL=");
1191             xmlCtxtDumpString(ctxt, doc->URL);
1192             fprintf(ctxt->output, "\n");
1193         }
1194         if (doc->standalone)
1195             fprintf(ctxt->output, "standalone=true\n");
1196     }
1197     if (doc->oldNs != NULL)
1198         xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
1199 }
1200 
1201 /**
1202  * xmlCtxtDumpDocument:
1203  * @output:  the FILE * for the output
1204  * @doc:  the document
1205  *
1206  * Dumps debug information for the document, it's recursive
1207  */
1208 static void
xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt,xmlDocPtr doc)1209 xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1210 {
1211     if (doc == NULL) {
1212         if (!ctxt->check)
1213             fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1214         return;
1215     }
1216     xmlCtxtDumpDocumentHead(ctxt, doc);
1217     if (((doc->type == XML_DOCUMENT_NODE) ||
1218          (doc->type == XML_HTML_DOCUMENT_NODE))
1219         && (doc->children != NULL)) {
1220         ctxt->depth++;
1221         xmlCtxtDumpNodeList(ctxt, doc->children);
1222         ctxt->depth--;
1223     }
1224 }
1225 
1226 static void
xmlCtxtDumpEntityCallback(void * payload,void * data,const xmlChar * name ATTRIBUTE_UNUSED)1227 xmlCtxtDumpEntityCallback(void *payload, void *data,
1228                           const xmlChar *name ATTRIBUTE_UNUSED)
1229 {
1230     xmlEntityPtr cur = (xmlEntityPtr) payload;
1231     xmlDebugCtxtPtr ctxt = (xmlDebugCtxtPtr) data;
1232     if (cur == NULL) {
1233         if (!ctxt->check)
1234             fprintf(ctxt->output, "Entity is NULL");
1235         return;
1236     }
1237     if (!ctxt->check) {
1238         fprintf(ctxt->output, "%s : ", (char *) cur->name);
1239         switch (cur->etype) {
1240             case XML_INTERNAL_GENERAL_ENTITY:
1241                 fprintf(ctxt->output, "INTERNAL GENERAL, ");
1242                 break;
1243             case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1244                 fprintf(ctxt->output, "EXTERNAL PARSED, ");
1245                 break;
1246             case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1247                 fprintf(ctxt->output, "EXTERNAL UNPARSED, ");
1248                 break;
1249             case XML_INTERNAL_PARAMETER_ENTITY:
1250                 fprintf(ctxt->output, "INTERNAL PARAMETER, ");
1251                 break;
1252             case XML_EXTERNAL_PARAMETER_ENTITY:
1253                 fprintf(ctxt->output, "EXTERNAL PARAMETER, ");
1254                 break;
1255             default:
1256 		xmlDebugErr2(ctxt, XML_CHECK_ENTITY_TYPE,
1257 			     "Unknown entity type %d\n", cur->etype);
1258         }
1259         if (cur->ExternalID != NULL)
1260             fprintf(ctxt->output, "ID \"%s\"", (char *) cur->ExternalID);
1261         if (cur->SystemID != NULL)
1262             fprintf(ctxt->output, "SYSTEM \"%s\"", (char *) cur->SystemID);
1263         if (cur->orig != NULL)
1264             fprintf(ctxt->output, "\n orig \"%s\"", (char *) cur->orig);
1265         if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL))
1266             fprintf(ctxt->output, "\n content \"%s\"",
1267                     (char *) cur->content);
1268         fprintf(ctxt->output, "\n");
1269     }
1270 }
1271 
1272 /**
1273  * xmlCtxtDumpEntities:
1274  * @output:  the FILE * for the output
1275  * @doc:  the document
1276  *
1277  * Dumps debug information for all the entities in use by the document
1278  */
1279 static void
xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt,xmlDocPtr doc)1280 xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1281 {
1282     if (doc == NULL) return;
1283     xmlCtxtDumpDocHead(ctxt, doc);
1284     if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
1285         xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1286             doc->intSubset->entities;
1287 
1288         if (!ctxt->check)
1289             fprintf(ctxt->output, "Entities in internal subset\n");
1290         xmlHashScan(table, xmlCtxtDumpEntityCallback, ctxt);
1291     } else
1292         fprintf(ctxt->output, "No entities in internal subset\n");
1293     if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
1294         xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1295             doc->extSubset->entities;
1296 
1297         if (!ctxt->check)
1298             fprintf(ctxt->output, "Entities in external subset\n");
1299         xmlHashScan(table, xmlCtxtDumpEntityCallback, ctxt);
1300     } else if (!ctxt->check)
1301         fprintf(ctxt->output, "No entities in external subset\n");
1302 }
1303 
1304 /**
1305  * xmlCtxtDumpDTD:
1306  * @output:  the FILE * for the output
1307  * @dtd:  the DTD
1308  *
1309  * Dumps debug information for the DTD
1310  */
1311 static void
xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt,xmlDtdPtr dtd)1312 xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
1313 {
1314     if (dtd == NULL) {
1315         if (!ctxt->check)
1316             fprintf(ctxt->output, "DTD is NULL\n");
1317         return;
1318     }
1319     xmlCtxtDumpDtdNode(ctxt, dtd);
1320     if (dtd->children == NULL)
1321         fprintf(ctxt->output, "    DTD is empty\n");
1322     else {
1323         ctxt->depth++;
1324         xmlCtxtDumpNodeList(ctxt, dtd->children);
1325         ctxt->depth--;
1326     }
1327 }
1328 
1329 /************************************************************************
1330  *									*
1331  *			Public entry points for dump			*
1332  *									*
1333  ************************************************************************/
1334 
1335 /**
1336  * xmlDebugDumpString:
1337  * @output:  the FILE * for the output
1338  * @str:  the string
1339  *
1340  * Dumps information about the string, shorten it if necessary
1341  */
1342 void
xmlDebugDumpString(FILE * output,const xmlChar * str)1343 xmlDebugDumpString(FILE * output, const xmlChar * str)
1344 {
1345     int i;
1346 
1347     if (output == NULL)
1348 	output = stdout;
1349     if (str == NULL) {
1350         fprintf(output, "(NULL)");
1351         return;
1352     }
1353     for (i = 0; i < 40; i++)
1354         if (str[i] == 0)
1355             return;
1356         else if (IS_BLANK_CH(str[i]))
1357             fputc(' ', output);
1358         else if (str[i] >= 0x80)
1359             fprintf(output, "#%X", str[i]);
1360         else
1361             fputc(str[i], output);
1362     fprintf(output, "...");
1363 }
1364 
1365 /**
1366  * xmlDebugDumpAttr:
1367  * @output:  the FILE * for the output
1368  * @attr:  the attribute
1369  * @depth:  the indentation level.
1370  *
1371  * Dumps debug information for the attribute
1372  */
1373 void
xmlDebugDumpAttr(FILE * output,xmlAttrPtr attr,int depth)1374 xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
1375     xmlDebugCtxt ctxt;
1376 
1377     if (output == NULL) return;
1378     xmlCtxtDumpInitCtxt(&ctxt);
1379     ctxt.output = output;
1380     ctxt.depth = depth;
1381     xmlCtxtDumpAttr(&ctxt, attr);
1382     xmlCtxtDumpCleanCtxt(&ctxt);
1383 }
1384 
1385 
1386 /**
1387  * xmlDebugDumpEntities:
1388  * @output:  the FILE * for the output
1389  * @doc:  the document
1390  *
1391  * Dumps debug information for all the entities in use by the document
1392  */
1393 void
xmlDebugDumpEntities(FILE * output,xmlDocPtr doc)1394 xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
1395 {
1396     xmlDebugCtxt ctxt;
1397 
1398     if (output == NULL) return;
1399     xmlCtxtDumpInitCtxt(&ctxt);
1400     ctxt.output = output;
1401     xmlCtxtDumpEntities(&ctxt, doc);
1402     xmlCtxtDumpCleanCtxt(&ctxt);
1403 }
1404 
1405 /**
1406  * xmlDebugDumpAttrList:
1407  * @output:  the FILE * for the output
1408  * @attr:  the attribute list
1409  * @depth:  the indentation level.
1410  *
1411  * Dumps debug information for the attribute list
1412  */
1413 void
xmlDebugDumpAttrList(FILE * output,xmlAttrPtr attr,int depth)1414 xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
1415 {
1416     xmlDebugCtxt ctxt;
1417 
1418     if (output == NULL) return;
1419     xmlCtxtDumpInitCtxt(&ctxt);
1420     ctxt.output = output;
1421     ctxt.depth = depth;
1422     xmlCtxtDumpAttrList(&ctxt, attr);
1423     xmlCtxtDumpCleanCtxt(&ctxt);
1424 }
1425 
1426 /**
1427  * xmlDebugDumpOneNode:
1428  * @output:  the FILE * for the output
1429  * @node:  the node
1430  * @depth:  the indentation level.
1431  *
1432  * Dumps debug information for the element node, it is not recursive
1433  */
1434 void
xmlDebugDumpOneNode(FILE * output,xmlNodePtr node,int depth)1435 xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
1436 {
1437     xmlDebugCtxt ctxt;
1438 
1439     if (output == NULL) return;
1440     xmlCtxtDumpInitCtxt(&ctxt);
1441     ctxt.output = output;
1442     ctxt.depth = depth;
1443     xmlCtxtDumpOneNode(&ctxt, node);
1444     xmlCtxtDumpCleanCtxt(&ctxt);
1445 }
1446 
1447 /**
1448  * xmlDebugDumpNode:
1449  * @output:  the FILE * for the output
1450  * @node:  the node
1451  * @depth:  the indentation level.
1452  *
1453  * Dumps debug information for the element node, it is recursive
1454  */
1455 void
xmlDebugDumpNode(FILE * output,xmlNodePtr node,int depth)1456 xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
1457 {
1458     xmlDebugCtxt ctxt;
1459 
1460     if (output == NULL)
1461 	output = stdout;
1462     xmlCtxtDumpInitCtxt(&ctxt);
1463     ctxt.output = output;
1464     ctxt.depth = depth;
1465     xmlCtxtDumpNode(&ctxt, node);
1466     xmlCtxtDumpCleanCtxt(&ctxt);
1467 }
1468 
1469 /**
1470  * xmlDebugDumpNodeList:
1471  * @output:  the FILE * for the output
1472  * @node:  the node list
1473  * @depth:  the indentation level.
1474  *
1475  * Dumps debug information for the list of element node, it is recursive
1476  */
1477 void
xmlDebugDumpNodeList(FILE * output,xmlNodePtr node,int depth)1478 xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
1479 {
1480     xmlDebugCtxt ctxt;
1481 
1482     if (output == NULL)
1483 	output = stdout;
1484     xmlCtxtDumpInitCtxt(&ctxt);
1485     ctxt.output = output;
1486     ctxt.depth = depth;
1487     xmlCtxtDumpNodeList(&ctxt, node);
1488     xmlCtxtDumpCleanCtxt(&ctxt);
1489 }
1490 
1491 /**
1492  * xmlDebugDumpDocumentHead:
1493  * @output:  the FILE * for the output
1494  * @doc:  the document
1495  *
1496  * Dumps debug information concerning the document, not recursive
1497  */
1498 void
xmlDebugDumpDocumentHead(FILE * output,xmlDocPtr doc)1499 xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
1500 {
1501     xmlDebugCtxt ctxt;
1502 
1503     if (output == NULL)
1504 	output = stdout;
1505     xmlCtxtDumpInitCtxt(&ctxt);
1506     ctxt.options |= DUMP_TEXT_TYPE;
1507     ctxt.output = output;
1508     xmlCtxtDumpDocumentHead(&ctxt, doc);
1509     xmlCtxtDumpCleanCtxt(&ctxt);
1510 }
1511 
1512 /**
1513  * xmlDebugDumpDocument:
1514  * @output:  the FILE * for the output
1515  * @doc:  the document
1516  *
1517  * Dumps debug information for the document, it's recursive
1518  */
1519 void
xmlDebugDumpDocument(FILE * output,xmlDocPtr doc)1520 xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
1521 {
1522     xmlDebugCtxt ctxt;
1523 
1524     if (output == NULL)
1525 	output = stdout;
1526     xmlCtxtDumpInitCtxt(&ctxt);
1527     ctxt.options |= DUMP_TEXT_TYPE;
1528     ctxt.output = output;
1529     xmlCtxtDumpDocument(&ctxt, doc);
1530     xmlCtxtDumpCleanCtxt(&ctxt);
1531 }
1532 
1533 /**
1534  * xmlDebugDumpDTD:
1535  * @output:  the FILE * for the output
1536  * @dtd:  the DTD
1537  *
1538  * Dumps debug information for the DTD
1539  */
1540 void
xmlDebugDumpDTD(FILE * output,xmlDtdPtr dtd)1541 xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
1542 {
1543     xmlDebugCtxt ctxt;
1544 
1545     if (output == NULL)
1546 	output = stdout;
1547     xmlCtxtDumpInitCtxt(&ctxt);
1548     ctxt.options |= DUMP_TEXT_TYPE;
1549     ctxt.output = output;
1550     xmlCtxtDumpDTD(&ctxt, dtd);
1551     xmlCtxtDumpCleanCtxt(&ctxt);
1552 }
1553 
1554 /************************************************************************
1555  *									*
1556  *			Public entry points for checkings		*
1557  *									*
1558  ************************************************************************/
1559 
1560 /**
1561  * xmlDebugCheckDocument:
1562  * @output:  the FILE * for the output
1563  * @doc:  the document
1564  *
1565  * Check the document for potential content problems, and output
1566  * the errors to @output
1567  *
1568  * Returns the number of errors found
1569  */
1570 int
xmlDebugCheckDocument(FILE * output,xmlDocPtr doc)1571 xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
1572 {
1573     xmlDebugCtxt ctxt;
1574 
1575     if (output == NULL)
1576 	output = stdout;
1577     xmlCtxtDumpInitCtxt(&ctxt);
1578     ctxt.output = output;
1579     ctxt.check = 1;
1580     xmlCtxtDumpDocument(&ctxt, doc);
1581     xmlCtxtDumpCleanCtxt(&ctxt);
1582     return(ctxt.errors);
1583 }
1584 
1585 /************************************************************************
1586  *									*
1587  *			Helpers for Shell				*
1588  *									*
1589  ************************************************************************/
1590 
1591 /**
1592  * xmlLsCountNode:
1593  * @node:  the node to count
1594  *
1595  * Count the children of @node.
1596  *
1597  * Returns the number of children of @node.
1598  */
1599 int
xmlLsCountNode(xmlNodePtr node)1600 xmlLsCountNode(xmlNodePtr node) {
1601     int ret = 0;
1602     xmlNodePtr list = NULL;
1603 
1604     if (node == NULL)
1605 	return(0);
1606 
1607     switch (node->type) {
1608 	case XML_ELEMENT_NODE:
1609 	    list = node->children;
1610 	    break;
1611 	case XML_DOCUMENT_NODE:
1612 	case XML_HTML_DOCUMENT_NODE:
1613 	    list = ((xmlDocPtr) node)->children;
1614 	    break;
1615 	case XML_ATTRIBUTE_NODE:
1616 	    list = ((xmlAttrPtr) node)->children;
1617 	    break;
1618 	case XML_TEXT_NODE:
1619 	case XML_CDATA_SECTION_NODE:
1620 	case XML_PI_NODE:
1621 	case XML_COMMENT_NODE:
1622 	    if (node->content != NULL) {
1623 		ret = xmlStrlen(node->content);
1624             }
1625 	    break;
1626 	case XML_ENTITY_REF_NODE:
1627 	case XML_DOCUMENT_TYPE_NODE:
1628 	case XML_ENTITY_NODE:
1629 	case XML_DOCUMENT_FRAG_NODE:
1630 	case XML_NOTATION_NODE:
1631 	case XML_DTD_NODE:
1632         case XML_ELEMENT_DECL:
1633         case XML_ATTRIBUTE_DECL:
1634         case XML_ENTITY_DECL:
1635 	case XML_NAMESPACE_DECL:
1636 	case XML_XINCLUDE_START:
1637 	case XML_XINCLUDE_END:
1638 	    ret = 1;
1639 	    break;
1640     }
1641     for (;list != NULL;ret++)
1642         list = list->next;
1643     return(ret);
1644 }
1645 
1646 /**
1647  * xmlLsOneNode:
1648  * @output:  the FILE * for the output
1649  * @node:  the node to dump
1650  *
1651  * Dump to @output the type and name of @node.
1652  */
1653 void
xmlLsOneNode(FILE * output,xmlNodePtr node)1654 xmlLsOneNode(FILE *output, xmlNodePtr node) {
1655     if (output == NULL) return;
1656     if (node == NULL) {
1657 	fprintf(output, "NULL\n");
1658 	return;
1659     }
1660     switch (node->type) {
1661 	case XML_ELEMENT_NODE:
1662 	    fprintf(output, "-");
1663 	    break;
1664 	case XML_ATTRIBUTE_NODE:
1665 	    fprintf(output, "a");
1666 	    break;
1667 	case XML_TEXT_NODE:
1668 	    fprintf(output, "t");
1669 	    break;
1670 	case XML_CDATA_SECTION_NODE:
1671 	    fprintf(output, "C");
1672 	    break;
1673 	case XML_ENTITY_REF_NODE:
1674 	    fprintf(output, "e");
1675 	    break;
1676 	case XML_ENTITY_NODE:
1677 	    fprintf(output, "E");
1678 	    break;
1679 	case XML_PI_NODE:
1680 	    fprintf(output, "p");
1681 	    break;
1682 	case XML_COMMENT_NODE:
1683 	    fprintf(output, "c");
1684 	    break;
1685 	case XML_DOCUMENT_NODE:
1686 	    fprintf(output, "d");
1687 	    break;
1688 	case XML_HTML_DOCUMENT_NODE:
1689 	    fprintf(output, "h");
1690 	    break;
1691 	case XML_DOCUMENT_TYPE_NODE:
1692 	    fprintf(output, "T");
1693 	    break;
1694 	case XML_DOCUMENT_FRAG_NODE:
1695 	    fprintf(output, "F");
1696 	    break;
1697 	case XML_NOTATION_NODE:
1698 	    fprintf(output, "N");
1699 	    break;
1700 	case XML_NAMESPACE_DECL:
1701 	    fprintf(output, "n");
1702 	    break;
1703 	default:
1704 	    fprintf(output, "?");
1705     }
1706     if (node->type != XML_NAMESPACE_DECL) {
1707 	if (node->properties != NULL)
1708 	    fprintf(output, "a");
1709 	else
1710 	    fprintf(output, "-");
1711 	if (node->nsDef != NULL)
1712 	    fprintf(output, "n");
1713 	else
1714 	    fprintf(output, "-");
1715     }
1716 
1717     fprintf(output, " %8d ", xmlLsCountNode(node));
1718 
1719     switch (node->type) {
1720 	case XML_ELEMENT_NODE:
1721 	    if (node->name != NULL) {
1722                 if ((node->ns != NULL) && (node->ns->prefix != NULL))
1723                     fprintf(output, "%s:", node->ns->prefix);
1724 		fprintf(output, "%s", (const char *) node->name);
1725             }
1726 	    break;
1727 	case XML_ATTRIBUTE_NODE:
1728 	    if (node->name != NULL)
1729 		fprintf(output, "%s", (const char *) node->name);
1730 	    break;
1731 	case XML_TEXT_NODE:
1732 	    if (node->content != NULL) {
1733 		xmlDebugDumpString(output, node->content);
1734             }
1735 	    break;
1736 	case XML_CDATA_SECTION_NODE:
1737 	    break;
1738 	case XML_ENTITY_REF_NODE:
1739 	    if (node->name != NULL)
1740 		fprintf(output, "%s", (const char *) node->name);
1741 	    break;
1742 	case XML_ENTITY_NODE:
1743 	    if (node->name != NULL)
1744 		fprintf(output, "%s", (const char *) node->name);
1745 	    break;
1746 	case XML_PI_NODE:
1747 	    if (node->name != NULL)
1748 		fprintf(output, "%s", (const char *) node->name);
1749 	    break;
1750 	case XML_COMMENT_NODE:
1751 	    break;
1752 	case XML_DOCUMENT_NODE:
1753 	    break;
1754 	case XML_HTML_DOCUMENT_NODE:
1755 	    break;
1756 	case XML_DOCUMENT_TYPE_NODE:
1757 	    break;
1758 	case XML_DOCUMENT_FRAG_NODE:
1759 	    break;
1760 	case XML_NOTATION_NODE:
1761 	    break;
1762 	case XML_NAMESPACE_DECL: {
1763 	    xmlNsPtr ns = (xmlNsPtr) node;
1764 
1765 	    if (ns->prefix == NULL)
1766 		fprintf(output, "default -> %s", (char *)ns->href);
1767 	    else
1768 		fprintf(output, "%s -> %s", (char *)ns->prefix,
1769 			(char *)ns->href);
1770 	    break;
1771 	}
1772 	default:
1773 	    if (node->name != NULL)
1774 		fprintf(output, "%s", (const char *) node->name);
1775     }
1776     fprintf(output, "\n");
1777 }
1778 
1779 /**
1780  * xmlBoolToText:
1781  * @boolval: a bool to turn into text
1782  *
1783  * Convenient way to turn bool into text
1784  *
1785  * Returns a pointer to either "True" or "False"
1786  */
1787 const char *
xmlBoolToText(int boolval)1788 xmlBoolToText(int boolval)
1789 {
1790     if (boolval)
1791         return("True");
1792     else
1793         return("False");
1794 }
1795 
1796 #ifdef LIBXML_XPATH_ENABLED
1797 /****************************************************************
1798  *								*
1799  *		The XML shell related functions			*
1800  *								*
1801  ****************************************************************/
1802 
1803 
1804 
1805 /*
1806  * TODO: Improvement/cleanups for the XML shell
1807  *     - allow to shell out an editor on a subpart
1808  *     - cleanup function registrations (with help) and calling
1809  *     - provide registration routines
1810  */
1811 
1812 /**
1813  * xmlShellPrintXPathError:
1814  * @errorType: valid xpath error id
1815  * @arg: the argument that cause xpath to fail
1816  *
1817  * Print the xpath error to libxml default error channel
1818  */
1819 void
xmlShellPrintXPathError(int errorType,const char * arg)1820 xmlShellPrintXPathError(int errorType, const char *arg)
1821 {
1822     const char *default_arg = "Result";
1823 
1824     if (!arg)
1825         arg = default_arg;
1826 
1827     switch (errorType) {
1828         case XPATH_UNDEFINED:
1829             xmlGenericError(xmlGenericErrorContext,
1830                             "%s: no such node\n", arg);
1831             break;
1832 
1833         case XPATH_BOOLEAN:
1834             xmlGenericError(xmlGenericErrorContext,
1835                             "%s is a Boolean\n", arg);
1836             break;
1837         case XPATH_NUMBER:
1838             xmlGenericError(xmlGenericErrorContext,
1839                             "%s is a number\n", arg);
1840             break;
1841         case XPATH_STRING:
1842             xmlGenericError(xmlGenericErrorContext,
1843                             "%s is a string\n", arg);
1844             break;
1845 #ifdef LIBXML_XPTR_LOCS_ENABLED
1846         case XPATH_POINT:
1847             xmlGenericError(xmlGenericErrorContext,
1848                             "%s is a point\n", arg);
1849             break;
1850         case XPATH_RANGE:
1851             xmlGenericError(xmlGenericErrorContext,
1852                             "%s is a range\n", arg);
1853             break;
1854         case XPATH_LOCATIONSET:
1855             xmlGenericError(xmlGenericErrorContext,
1856                             "%s is a range\n", arg);
1857             break;
1858 #endif /* LIBXML_XPTR_LOCS_ENABLED */
1859         case XPATH_USERS:
1860             xmlGenericError(xmlGenericErrorContext,
1861                             "%s is user-defined\n", arg);
1862             break;
1863         case XPATH_XSLT_TREE:
1864             xmlGenericError(xmlGenericErrorContext,
1865                             "%s is an XSLT value tree\n", arg);
1866             break;
1867     }
1868 #if 0
1869     xmlGenericError(xmlGenericErrorContext,
1870                     "Try casting the result string function (xpath builtin)\n",
1871                     arg);
1872 #endif
1873 }
1874 
1875 
1876 #ifdef LIBXML_OUTPUT_ENABLED
1877 /**
1878  * xmlShellPrintNodeCtxt:
1879  * @ctxt : a non-null shell context
1880  * @node : a non-null node to print to the output FILE
1881  *
1882  * Print node to the output FILE
1883  */
1884 static void
xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)1885 xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1886 {
1887     FILE *fp;
1888 
1889     if (!node)
1890         return;
1891     if (ctxt == NULL)
1892 	fp = stdout;
1893     else
1894 	fp = ctxt->output;
1895 
1896     if (node->type == XML_DOCUMENT_NODE)
1897         xmlDocDump(fp, (xmlDocPtr) node);
1898     else if (node->type == XML_ATTRIBUTE_NODE)
1899         xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
1900     else
1901         xmlElemDump(fp, node->doc, node);
1902 
1903     fprintf(fp, "\n");
1904 }
1905 
1906 /**
1907  * xmlShellPrintNode:
1908  * @node : a non-null node to print to the output FILE
1909  *
1910  * Print node to the output FILE
1911  */
1912 void
xmlShellPrintNode(xmlNodePtr node)1913 xmlShellPrintNode(xmlNodePtr node)
1914 {
1915     xmlShellPrintNodeCtxt(NULL, node);
1916 }
1917 #endif /* LIBXML_OUTPUT_ENABLED */
1918 
1919 /**
1920  * xmlShellPrintXPathResultCtxt:
1921  * @ctxt: a valid shell context
1922  * @list: a valid result generated by an xpath evaluation
1923  *
1924  * Prints result to the output FILE
1925  */
1926 static void
xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)1927 xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
1928 {
1929     if (!ctxt)
1930        return;
1931 
1932     if (list != NULL) {
1933         switch (list->type) {
1934             case XPATH_NODESET:{
1935 #ifdef LIBXML_OUTPUT_ENABLED
1936                     int indx;
1937 
1938                     if (list->nodesetval) {
1939                         for (indx = 0; indx < list->nodesetval->nodeNr;
1940                              indx++) {
1941                             xmlShellPrintNodeCtxt(ctxt,
1942 				    list->nodesetval->nodeTab[indx]);
1943                         }
1944                     } else {
1945                         xmlGenericError(xmlGenericErrorContext,
1946                                         "Empty node set\n");
1947                     }
1948                     break;
1949 #else
1950 		    xmlGenericError(xmlGenericErrorContext,
1951 				    "Node set\n");
1952 #endif /* LIBXML_OUTPUT_ENABLED */
1953                 }
1954             case XPATH_BOOLEAN:
1955                 xmlGenericError(xmlGenericErrorContext,
1956                                 "Is a Boolean:%s\n",
1957                                 xmlBoolToText(list->boolval));
1958                 break;
1959             case XPATH_NUMBER:
1960                 xmlGenericError(xmlGenericErrorContext,
1961                                 "Is a number:%0g\n", list->floatval);
1962                 break;
1963             case XPATH_STRING:
1964                 xmlGenericError(xmlGenericErrorContext,
1965                                 "Is a string:%s\n", list->stringval);
1966                 break;
1967 
1968             default:
1969                 xmlShellPrintXPathError(list->type, NULL);
1970         }
1971     }
1972 }
1973 
1974 /**
1975  * xmlShellPrintXPathResult:
1976  * @list: a valid result generated by an xpath evaluation
1977  *
1978  * Prints result to the output FILE
1979  */
1980 void
xmlShellPrintXPathResult(xmlXPathObjectPtr list)1981 xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1982 {
1983     xmlShellPrintXPathResultCtxt(NULL, list);
1984 }
1985 
1986 /**
1987  * xmlShellList:
1988  * @ctxt:  the shell context
1989  * @arg:  unused
1990  * @node:  a node
1991  * @node2:  unused
1992  *
1993  * Implements the XML shell function "ls"
1994  * Does an Unix like listing of the given node (like a directory)
1995  *
1996  * Returns 0
1997  */
1998 int
xmlShellList(xmlShellCtxtPtr ctxt,char * arg ATTRIBUTE_UNUSED,xmlNodePtr node,xmlNodePtr node2 ATTRIBUTE_UNUSED)1999 xmlShellList(xmlShellCtxtPtr ctxt,
2000              char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2001              xmlNodePtr node2 ATTRIBUTE_UNUSED)
2002 {
2003     xmlNodePtr cur;
2004     if (!ctxt)
2005         return (0);
2006     if (node == NULL) {
2007 	fprintf(ctxt->output, "NULL\n");
2008 	return (0);
2009     }
2010     if ((node->type == XML_DOCUMENT_NODE) ||
2011         (node->type == XML_HTML_DOCUMENT_NODE)) {
2012         cur = ((xmlDocPtr) node)->children;
2013     } else if (node->type == XML_NAMESPACE_DECL) {
2014         xmlLsOneNode(ctxt->output, node);
2015         return (0);
2016     } else if (node->children != NULL) {
2017         cur = node->children;
2018     } else {
2019         xmlLsOneNode(ctxt->output, node);
2020         return (0);
2021     }
2022     while (cur != NULL) {
2023         xmlLsOneNode(ctxt->output, cur);
2024         cur = cur->next;
2025     }
2026     return (0);
2027 }
2028 
2029 /**
2030  * xmlShellBase:
2031  * @ctxt:  the shell context
2032  * @arg:  unused
2033  * @node:  a node
2034  * @node2:  unused
2035  *
2036  * Implements the XML shell function "base"
2037  * dumps the current XML base of the node
2038  *
2039  * Returns 0
2040  */
2041 int
xmlShellBase(xmlShellCtxtPtr ctxt,char * arg ATTRIBUTE_UNUSED,xmlNodePtr node,xmlNodePtr node2 ATTRIBUTE_UNUSED)2042 xmlShellBase(xmlShellCtxtPtr ctxt,
2043              char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2044              xmlNodePtr node2 ATTRIBUTE_UNUSED)
2045 {
2046     xmlChar *base;
2047     if (!ctxt)
2048         return 0;
2049     if (node == NULL) {
2050 	fprintf(ctxt->output, "NULL\n");
2051 	return (0);
2052     }
2053 
2054     base = xmlNodeGetBase(node->doc, node);
2055 
2056     if (base == NULL) {
2057         fprintf(ctxt->output, " No base found !!!\n");
2058     } else {
2059         fprintf(ctxt->output, "%s\n", base);
2060         xmlFree(base);
2061     }
2062     return (0);
2063 }
2064 
2065 #ifdef LIBXML_TREE_ENABLED
2066 /**
2067  * xmlShellSetBase:
2068  * @ctxt:  the shell context
2069  * @arg:  the new base
2070  * @node:  a node
2071  * @node2:  unused
2072  *
2073  * Implements the XML shell function "setbase"
2074  * change the current XML base of the node
2075  *
2076  * Returns 0
2077  */
2078 static int
xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,char * arg ATTRIBUTE_UNUSED,xmlNodePtr node,xmlNodePtr node2 ATTRIBUTE_UNUSED)2079 xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2080              char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2081              xmlNodePtr node2 ATTRIBUTE_UNUSED)
2082 {
2083     xmlNodeSetBase(node, (xmlChar*) arg);
2084     return (0);
2085 }
2086 #endif
2087 
2088 #ifdef LIBXML_XPATH_ENABLED
2089 /**
2090  * xmlShellRegisterNamespace:
2091  * @ctxt:  the shell context
2092  * @arg:  a string in prefix=nsuri format
2093  * @node:  unused
2094  * @node2:  unused
2095  *
2096  * Implements the XML shell function "setns"
2097  * register/unregister a prefix=namespace pair
2098  * on the XPath context
2099  *
2100  * Returns 0 on success and a negative value otherwise.
2101  */
2102 static int
xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt,char * arg,xmlNodePtr node ATTRIBUTE_UNUSED,xmlNodePtr node2 ATTRIBUTE_UNUSED)2103 xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
2104       xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2105 {
2106     xmlChar* nsListDup;
2107     xmlChar* prefix;
2108     xmlChar* href;
2109     xmlChar* next;
2110 
2111     nsListDup = xmlStrdup((xmlChar *) arg);
2112     next = nsListDup;
2113     while(next != NULL) {
2114 	/* skip spaces */
2115 	/*while((*next) == ' ') next++;*/
2116 	if((*next) == '\0') break;
2117 
2118 	/* find prefix */
2119 	prefix = next;
2120 	next = (xmlChar*)xmlStrchr(next, '=');
2121 	if(next == NULL) {
2122 	    fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
2123 	    xmlFree(nsListDup);
2124 	    return(-1);
2125 	}
2126 	*(next++) = '\0';
2127 
2128 	/* find href */
2129 	href = next;
2130 	next = (xmlChar*)xmlStrchr(next, ' ');
2131 	if(next != NULL) {
2132 	    *(next++) = '\0';
2133 	}
2134 
2135 	/* do register namespace */
2136 	if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
2137 	    fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
2138 	    xmlFree(nsListDup);
2139 	    return(-1);
2140 	}
2141     }
2142 
2143     xmlFree(nsListDup);
2144     return(0);
2145 }
2146 /**
2147  * xmlShellRegisterRootNamespaces:
2148  * @ctxt:  the shell context
2149  * @arg:  unused
2150  * @node:  the root element
2151  * @node2:  unused
2152  *
2153  * Implements the XML shell function "setrootns"
2154  * which registers all namespaces declarations found on the root element.
2155  *
2156  * Returns 0 on success and a negative value otherwise.
2157  */
2158 static int
xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt,char * arg ATTRIBUTE_UNUSED,xmlNodePtr root,xmlNodePtr node2 ATTRIBUTE_UNUSED)2159 xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2160       xmlNodePtr root, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2161 {
2162     xmlNsPtr ns;
2163 
2164     if ((root == NULL) || (root->type != XML_ELEMENT_NODE) ||
2165         (root->nsDef == NULL) || (ctxt == NULL) || (ctxt->pctxt == NULL))
2166 	return(-1);
2167     ns = root->nsDef;
2168     while (ns != NULL) {
2169         if (ns->prefix == NULL)
2170 	    xmlXPathRegisterNs(ctxt->pctxt, BAD_CAST "defaultns", ns->href);
2171 	else
2172 	    xmlXPathRegisterNs(ctxt->pctxt, ns->prefix, ns->href);
2173         ns = ns->next;
2174     }
2175     return(0);
2176 }
2177 #endif
2178 
2179 /**
2180  * xmlShellGrep:
2181  * @ctxt:  the shell context
2182  * @arg:  the string or regular expression to find
2183  * @node:  a node
2184  * @node2:  unused
2185  *
2186  * Implements the XML shell function "grep"
2187  * dumps information about the node (namespace, attributes, content).
2188  *
2189  * Returns 0
2190  */
2191 static int
xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,char * arg,xmlNodePtr node,xmlNodePtr node2 ATTRIBUTE_UNUSED)2192 xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2193             char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2194 {
2195     if (!ctxt)
2196         return (0);
2197     if (node == NULL)
2198 	return (0);
2199     if (arg == NULL)
2200 	return (0);
2201 #ifdef LIBXML_REGEXP_ENABLED
2202     if ((xmlStrchr((xmlChar *) arg, '?')) ||
2203 	(xmlStrchr((xmlChar *) arg, '*')) ||
2204 	(xmlStrchr((xmlChar *) arg, '.')) ||
2205 	(xmlStrchr((xmlChar *) arg, '['))) {
2206     }
2207 #endif
2208     while (node != NULL) {
2209         if (node->type == XML_COMMENT_NODE) {
2210 	    if (xmlStrstr(node->content, (xmlChar *) arg)) {
2211 
2212 		fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
2213                 xmlShellList(ctxt, NULL, node, NULL);
2214 	    }
2215         } else if (node->type == XML_TEXT_NODE) {
2216 	    if (xmlStrstr(node->content, (xmlChar *) arg)) {
2217 
2218 		fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
2219                 xmlShellList(ctxt, NULL, node->parent, NULL);
2220 	    }
2221         }
2222 
2223         /*
2224          * Browse the full subtree, deep first
2225          */
2226 
2227         if ((node->type == XML_DOCUMENT_NODE) ||
2228             (node->type == XML_HTML_DOCUMENT_NODE)) {
2229             node = ((xmlDocPtr) node)->children;
2230         } else if ((node->children != NULL)
2231                    && (node->type != XML_ENTITY_REF_NODE)) {
2232             /* deep first */
2233             node = node->children;
2234         } else if (node->next != NULL) {
2235             /* then siblings */
2236             node = node->next;
2237         } else {
2238             /* go up to parents->next if needed */
2239             while (node != NULL) {
2240                 if (node->parent != NULL) {
2241                     node = node->parent;
2242                 }
2243                 if (node->next != NULL) {
2244                     node = node->next;
2245                     break;
2246                 }
2247                 if (node->parent == NULL) {
2248                     node = NULL;
2249                     break;
2250                 }
2251             }
2252 	}
2253     }
2254     return (0);
2255 }
2256 
2257 /**
2258  * xmlShellDir:
2259  * @ctxt:  the shell context
2260  * @arg:  unused
2261  * @node:  a node
2262  * @node2:  unused
2263  *
2264  * Implements the XML shell function "dir"
2265  * dumps information about the node (namespace, attributes, content).
2266  *
2267  * Returns 0
2268  */
2269 int
xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,char * arg ATTRIBUTE_UNUSED,xmlNodePtr node,xmlNodePtr node2 ATTRIBUTE_UNUSED)2270 xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2271             char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2272             xmlNodePtr node2 ATTRIBUTE_UNUSED)
2273 {
2274     if (!ctxt)
2275         return (0);
2276     if (node == NULL) {
2277 	fprintf(ctxt->output, "NULL\n");
2278 	return (0);
2279     }
2280     if ((node->type == XML_DOCUMENT_NODE) ||
2281         (node->type == XML_HTML_DOCUMENT_NODE)) {
2282         xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
2283     } else if (node->type == XML_ATTRIBUTE_NODE) {
2284         xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
2285     } else {
2286         xmlDebugDumpOneNode(ctxt->output, node, 0);
2287     }
2288     return (0);
2289 }
2290 
2291 /**
2292  * xmlShellSetContent:
2293  * @ctxt:  the shell context
2294  * @value:  the content as a string
2295  * @node:  a node
2296  * @node2:  unused
2297  *
2298  * Implements the XML shell function "dir"
2299  * dumps information about the node (namespace, attributes, content).
2300  *
2301  * Returns 0
2302  */
2303 static int
xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,char * value,xmlNodePtr node,xmlNodePtr node2 ATTRIBUTE_UNUSED)2304 xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2305             char *value, xmlNodePtr node,
2306             xmlNodePtr node2 ATTRIBUTE_UNUSED)
2307 {
2308     xmlNodePtr results;
2309     xmlParserErrors ret;
2310 
2311     if (!ctxt)
2312         return (0);
2313     if (node == NULL) {
2314 	fprintf(ctxt->output, "NULL\n");
2315 	return (0);
2316     }
2317     if (value == NULL) {
2318         fprintf(ctxt->output, "NULL\n");
2319 	return (0);
2320     }
2321 
2322     ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
2323     if (ret == XML_ERR_OK) {
2324 	if (node->children != NULL) {
2325 	    xmlFreeNodeList(node->children);
2326 	    node->children = NULL;
2327 	    node->last = NULL;
2328 	}
2329 	xmlAddChildList(node, results);
2330     } else {
2331         fprintf(ctxt->output, "failed to parse content\n");
2332     }
2333     return (0);
2334 }
2335 
2336 #ifdef LIBXML_SCHEMAS_ENABLED
2337 /**
2338  * xmlShellRNGValidate:
2339  * @ctxt:  the shell context
2340  * @schemas:  the path to the Relax-NG schemas
2341  * @node:  a node
2342  * @node2:  unused
2343  *
2344  * Implements the XML shell function "relaxng"
2345  * validating the instance against a Relax-NG schemas
2346  *
2347  * Returns 0
2348  */
2349 static int
xmlShellRNGValidate(xmlShellCtxtPtr sctxt,char * schemas,xmlNodePtr node ATTRIBUTE_UNUSED,xmlNodePtr node2 ATTRIBUTE_UNUSED)2350 xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
2351             xmlNodePtr node ATTRIBUTE_UNUSED,
2352 	    xmlNodePtr node2 ATTRIBUTE_UNUSED)
2353 {
2354     xmlRelaxNGPtr relaxngschemas;
2355     xmlRelaxNGParserCtxtPtr ctxt;
2356     xmlRelaxNGValidCtxtPtr vctxt;
2357     int ret;
2358 
2359     ctxt = xmlRelaxNGNewParserCtxt(schemas);
2360     xmlRelaxNGSetParserErrors(ctxt, xmlGenericError, xmlGenericError, NULL);
2361     relaxngschemas = xmlRelaxNGParse(ctxt);
2362     xmlRelaxNGFreeParserCtxt(ctxt);
2363     if (relaxngschemas == NULL) {
2364 	xmlGenericError(xmlGenericErrorContext,
2365 		"Relax-NG schema %s failed to compile\n", schemas);
2366 	return(-1);
2367     }
2368     vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2369     xmlRelaxNGSetValidErrors(vctxt, xmlGenericError, xmlGenericError, NULL);
2370     ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2371     if (ret == 0) {
2372 	fprintf(stderr, "%s validates\n", sctxt->filename);
2373     } else if (ret > 0) {
2374 	fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2375     } else {
2376 	fprintf(stderr, "%s validation generated an internal error\n",
2377 	       sctxt->filename);
2378     }
2379     xmlRelaxNGFreeValidCtxt(vctxt);
2380     if (relaxngschemas != NULL)
2381 	xmlRelaxNGFree(relaxngschemas);
2382     return(0);
2383 }
2384 #endif
2385 
2386 #ifdef LIBXML_OUTPUT_ENABLED
2387 /**
2388  * xmlShellCat:
2389  * @ctxt:  the shell context
2390  * @arg:  unused
2391  * @node:  a node
2392  * @node2:  unused
2393  *
2394  * Implements the XML shell function "cat"
2395  * dumps the serialization node content (XML or HTML).
2396  *
2397  * Returns 0
2398  */
2399 int
xmlShellCat(xmlShellCtxtPtr ctxt,char * arg ATTRIBUTE_UNUSED,xmlNodePtr node,xmlNodePtr node2 ATTRIBUTE_UNUSED)2400 xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2401             xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2402 {
2403     if (!ctxt)
2404         return (0);
2405     if (node == NULL) {
2406 	fprintf(ctxt->output, "NULL\n");
2407 	return (0);
2408     }
2409     if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2410 #ifdef LIBXML_HTML_ENABLED
2411         if (node->type == XML_HTML_DOCUMENT_NODE)
2412             htmlDocDump(ctxt->output, (htmlDocPtr) node);
2413         else
2414             htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
2415 #else
2416         if (node->type == XML_DOCUMENT_NODE)
2417             xmlDocDump(ctxt->output, (xmlDocPtr) node);
2418         else
2419             xmlElemDump(ctxt->output, ctxt->doc, node);
2420 #endif /* LIBXML_HTML_ENABLED */
2421     } else {
2422         if (node->type == XML_DOCUMENT_NODE)
2423             xmlDocDump(ctxt->output, (xmlDocPtr) node);
2424         else
2425             xmlElemDump(ctxt->output, ctxt->doc, node);
2426     }
2427     fprintf(ctxt->output, "\n");
2428     return (0);
2429 }
2430 #endif /* LIBXML_OUTPUT_ENABLED */
2431 
2432 /**
2433  * xmlShellLoad:
2434  * @ctxt:  the shell context
2435  * @filename:  the file name
2436  * @node:  unused
2437  * @node2:  unused
2438  *
2439  * Implements the XML shell function "load"
2440  * loads a new document specified by the filename
2441  *
2442  * Returns 0 or -1 if loading failed
2443  */
2444 int
xmlShellLoad(xmlShellCtxtPtr ctxt,char * filename,xmlNodePtr node ATTRIBUTE_UNUSED,xmlNodePtr node2 ATTRIBUTE_UNUSED)2445 xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2446              xmlNodePtr node ATTRIBUTE_UNUSED,
2447              xmlNodePtr node2 ATTRIBUTE_UNUSED)
2448 {
2449     xmlDocPtr doc;
2450     int html = 0;
2451 
2452     if ((ctxt == NULL) || (filename == NULL)) return(-1);
2453     if (ctxt->doc != NULL)
2454         html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
2455 
2456     if (html) {
2457 #ifdef LIBXML_HTML_ENABLED
2458         doc = htmlParseFile(filename, NULL);
2459 #else
2460         fprintf(ctxt->output, "HTML support not compiled in\n");
2461         doc = NULL;
2462 #endif /* LIBXML_HTML_ENABLED */
2463     } else {
2464         doc = xmlReadFile(filename,NULL,0);
2465     }
2466     if (doc != NULL) {
2467         if (ctxt->loaded == 1) {
2468             xmlFreeDoc(ctxt->doc);
2469         }
2470         ctxt->loaded = 1;
2471 #ifdef LIBXML_XPATH_ENABLED
2472         xmlXPathFreeContext(ctxt->pctxt);
2473 #endif /* LIBXML_XPATH_ENABLED */
2474         xmlFree(ctxt->filename);
2475         ctxt->doc = doc;
2476         ctxt->node = (xmlNodePtr) doc;
2477 #ifdef LIBXML_XPATH_ENABLED
2478         ctxt->pctxt = xmlXPathNewContext(doc);
2479 #endif /* LIBXML_XPATH_ENABLED */
2480         ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
2481     } else
2482         return (-1);
2483     return (0);
2484 }
2485 
2486 #ifdef LIBXML_OUTPUT_ENABLED
2487 /**
2488  * xmlShellWrite:
2489  * @ctxt:  the shell context
2490  * @filename:  the file name
2491  * @node:  a node in the tree
2492  * @node2:  unused
2493  *
2494  * Implements the XML shell function "write"
2495  * Write the current node to the filename, it saves the serialization
2496  * of the subtree under the @node specified
2497  *
2498  * Returns 0 or -1 in case of error
2499  */
2500 int
xmlShellWrite(xmlShellCtxtPtr ctxt,char * filename,xmlNodePtr node,xmlNodePtr node2 ATTRIBUTE_UNUSED)2501 xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
2502               xmlNodePtr node2 ATTRIBUTE_UNUSED)
2503 {
2504     if (node == NULL)
2505         return (-1);
2506     if ((filename == NULL) || (filename[0] == 0)) {
2507         return (-1);
2508     }
2509 #ifdef W_OK
2510     if (access((char *) filename, W_OK)) {
2511         xmlGenericError(xmlGenericErrorContext,
2512                         "Cannot write to %s\n", filename);
2513         return (-1);
2514     }
2515 #endif
2516     switch (node->type) {
2517         case XML_DOCUMENT_NODE:
2518             if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2519                 xmlGenericError(xmlGenericErrorContext,
2520                                 "Failed to write to %s\n", filename);
2521                 return (-1);
2522             }
2523             break;
2524         case XML_HTML_DOCUMENT_NODE:
2525 #ifdef LIBXML_HTML_ENABLED
2526             if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2527                 xmlGenericError(xmlGenericErrorContext,
2528                                 "Failed to write to %s\n", filename);
2529                 return (-1);
2530             }
2531 #else
2532             if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2533                 xmlGenericError(xmlGenericErrorContext,
2534                                 "Failed to write to %s\n", filename);
2535                 return (-1);
2536             }
2537 #endif /* LIBXML_HTML_ENABLED */
2538             break;
2539         default:{
2540                 FILE *f;
2541 
2542                 f = fopen((char *) filename, "w");
2543                 if (f == NULL) {
2544                     xmlGenericError(xmlGenericErrorContext,
2545                                     "Failed to write to %s\n", filename);
2546                     return (-1);
2547                 }
2548                 xmlElemDump(f, ctxt->doc, node);
2549                 fclose(f);
2550             }
2551     }
2552     return (0);
2553 }
2554 
2555 /**
2556  * xmlShellSave:
2557  * @ctxt:  the shell context
2558  * @filename:  the file name (optional)
2559  * @node:  unused
2560  * @node2:  unused
2561  *
2562  * Implements the XML shell function "save"
2563  * Write the current document to the filename, or it's original name
2564  *
2565  * Returns 0 or -1 in case of error
2566  */
2567 int
xmlShellSave(xmlShellCtxtPtr ctxt,char * filename,xmlNodePtr node ATTRIBUTE_UNUSED,xmlNodePtr node2 ATTRIBUTE_UNUSED)2568 xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2569              xmlNodePtr node ATTRIBUTE_UNUSED,
2570              xmlNodePtr node2 ATTRIBUTE_UNUSED)
2571 {
2572     if ((ctxt == NULL) || (ctxt->doc == NULL))
2573         return (-1);
2574     if ((filename == NULL) || (filename[0] == 0))
2575         filename = ctxt->filename;
2576     if (filename == NULL)
2577         return (-1);
2578 #ifdef W_OK
2579     if (access((char *) filename, W_OK)) {
2580         xmlGenericError(xmlGenericErrorContext,
2581                         "Cannot save to %s\n", filename);
2582         return (-1);
2583     }
2584 #endif
2585     switch (ctxt->doc->type) {
2586         case XML_DOCUMENT_NODE:
2587             if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2588                 xmlGenericError(xmlGenericErrorContext,
2589                                 "Failed to save to %s\n", filename);
2590             }
2591             break;
2592         case XML_HTML_DOCUMENT_NODE:
2593 #ifdef LIBXML_HTML_ENABLED
2594             if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2595                 xmlGenericError(xmlGenericErrorContext,
2596                                 "Failed to save to %s\n", filename);
2597             }
2598 #else
2599             if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2600                 xmlGenericError(xmlGenericErrorContext,
2601                                 "Failed to save to %s\n", filename);
2602             }
2603 #endif /* LIBXML_HTML_ENABLED */
2604             break;
2605         default:
2606             xmlGenericError(xmlGenericErrorContext,
2607 	    "To save to subparts of a document use the 'write' command\n");
2608             return (-1);
2609 
2610     }
2611     return (0);
2612 }
2613 #endif /* LIBXML_OUTPUT_ENABLED */
2614 
2615 #ifdef LIBXML_VALID_ENABLED
2616 /**
2617  * xmlShellValidate:
2618  * @ctxt:  the shell context
2619  * @dtd:  the DTD URI (optional)
2620  * @node:  unused
2621  * @node2:  unused
2622  *
2623  * Implements the XML shell function "validate"
2624  * Validate the document, if a DTD path is provided, then the validation
2625  * is done against the given DTD.
2626  *
2627  * Returns 0 or -1 in case of error
2628  */
2629 int
xmlShellValidate(xmlShellCtxtPtr ctxt,char * dtd,xmlNodePtr node ATTRIBUTE_UNUSED,xmlNodePtr node2 ATTRIBUTE_UNUSED)2630 xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2631                  xmlNodePtr node ATTRIBUTE_UNUSED,
2632                  xmlNodePtr node2 ATTRIBUTE_UNUSED)
2633 {
2634     xmlValidCtxt vctxt;
2635     int res = -1;
2636 
2637     if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
2638     memset(&vctxt, 0, sizeof(vctxt));
2639     vctxt.error = xmlGenericError;
2640     vctxt.warning = xmlGenericError;
2641 
2642     if ((dtd == NULL) || (dtd[0] == 0)) {
2643         res = xmlValidateDocument(&vctxt, ctxt->doc);
2644     } else {
2645         xmlDtdPtr subset;
2646 
2647         subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2648         if (subset != NULL) {
2649             res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2650 
2651             xmlFreeDtd(subset);
2652         }
2653     }
2654     return (res);
2655 }
2656 #endif /* LIBXML_VALID_ENABLED */
2657 
2658 /**
2659  * xmlShellDu:
2660  * @ctxt:  the shell context
2661  * @arg:  unused
2662  * @tree:  a node defining a subtree
2663  * @node2:  unused
2664  *
2665  * Implements the XML shell function "du"
2666  * show the structure of the subtree under node @tree
2667  * If @tree is null, the command works on the current node.
2668  *
2669  * Returns 0 or -1 in case of error
2670  */
2671 int
xmlShellDu(xmlShellCtxtPtr ctxt,char * arg ATTRIBUTE_UNUSED,xmlNodePtr tree,xmlNodePtr node2 ATTRIBUTE_UNUSED)2672 xmlShellDu(xmlShellCtxtPtr ctxt,
2673            char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2674            xmlNodePtr node2 ATTRIBUTE_UNUSED)
2675 {
2676     xmlNodePtr node;
2677     int indent = 0, i;
2678 
2679     if (!ctxt)
2680 	return (-1);
2681 
2682     if (tree == NULL)
2683         return (-1);
2684     node = tree;
2685     while (node != NULL) {
2686         if ((node->type == XML_DOCUMENT_NODE) ||
2687             (node->type == XML_HTML_DOCUMENT_NODE)) {
2688             fprintf(ctxt->output, "/\n");
2689         } else if (node->type == XML_ELEMENT_NODE) {
2690             for (i = 0; i < indent; i++)
2691                 fprintf(ctxt->output, "  ");
2692             if ((node->ns) && (node->ns->prefix))
2693                 fprintf(ctxt->output, "%s:", node->ns->prefix);
2694             fprintf(ctxt->output, "%s\n", node->name);
2695         } else {
2696         }
2697 
2698         /*
2699          * Browse the full subtree, deep first
2700          */
2701 
2702         if ((node->type == XML_DOCUMENT_NODE) ||
2703             (node->type == XML_HTML_DOCUMENT_NODE)) {
2704             node = ((xmlDocPtr) node)->children;
2705         } else if ((node->children != NULL)
2706                    && (node->type != XML_ENTITY_REF_NODE)) {
2707             /* deep first */
2708             node = node->children;
2709             indent++;
2710         } else if ((node != tree) && (node->next != NULL)) {
2711             /* then siblings */
2712             node = node->next;
2713         } else if (node != tree) {
2714             /* go up to parents->next if needed */
2715             while (node != tree) {
2716                 if (node->parent != NULL) {
2717                     node = node->parent;
2718                     indent--;
2719                 }
2720                 if ((node != tree) && (node->next != NULL)) {
2721                     node = node->next;
2722                     break;
2723                 }
2724                 if (node->parent == NULL) {
2725                     node = NULL;
2726                     break;
2727                 }
2728                 if (node == tree) {
2729                     node = NULL;
2730                     break;
2731                 }
2732             }
2733             /* exit condition */
2734             if (node == tree)
2735                 node = NULL;
2736         } else
2737             node = NULL;
2738     }
2739     return (0);
2740 }
2741 
2742 /**
2743  * xmlShellPwd:
2744  * @ctxt:  the shell context
2745  * @buffer:  the output buffer
2746  * @node:  a node
2747  * @node2:  unused
2748  *
2749  * Implements the XML shell function "pwd"
2750  * Show the full path from the root to the node, if needed building
2751  * thumblers when similar elements exists at a given ancestor level.
2752  * The output is compatible with XPath commands.
2753  *
2754  * Returns 0 or -1 in case of error
2755  */
2756 int
xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,char * buffer,xmlNodePtr node,xmlNodePtr node2 ATTRIBUTE_UNUSED)2757 xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2758             xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2759 {
2760     xmlChar *path;
2761 
2762     if ((node == NULL) || (buffer == NULL))
2763         return (-1);
2764 
2765     path = xmlGetNodePath(node);
2766     if (path == NULL)
2767 	return (-1);
2768 
2769     /*
2770      * This test prevents buffer overflow, because this routine
2771      * is only called by xmlShell, in which the second argument is
2772      * 500 chars long.
2773      * It is a dirty hack before a cleaner solution is found.
2774      * Documentation should mention that the second argument must
2775      * be at least 500 chars long, and could be stripped if too long.
2776      */
2777     snprintf(buffer, 499, "%s", path);
2778     buffer[499] = '0';
2779     xmlFree(path);
2780 
2781     return (0);
2782 }
2783 
2784 /**
2785  * xmlShell:
2786  * @doc:  the initial document
2787  * @filename:  the output buffer
2788  * @input:  the line reading function
2789  * @output:  the output FILE*, defaults to stdout if NULL
2790  *
2791  * Implements the XML shell
2792  * This allow to load, validate, view, modify and save a document
2793  * using a environment similar to a UNIX commandline.
2794  */
2795 void
xmlShell(xmlDocPtr doc,char * filename,xmlShellReadlineFunc input,FILE * output)2796 xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
2797          FILE * output)
2798 {
2799     char prompt[500] = "/ > ";
2800     char *cmdline = NULL, *cur;
2801     char command[100];
2802     char arg[400];
2803     int i;
2804     xmlShellCtxtPtr ctxt;
2805     xmlXPathObjectPtr list;
2806 
2807     if (doc == NULL)
2808         return;
2809     if (filename == NULL)
2810         return;
2811     if (input == NULL)
2812         return;
2813     if (output == NULL)
2814         output = stdout;
2815     ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
2816     if (ctxt == NULL)
2817         return;
2818     ctxt->loaded = 0;
2819     ctxt->doc = doc;
2820     ctxt->input = input;
2821     ctxt->output = output;
2822     ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
2823     ctxt->node = (xmlNodePtr) ctxt->doc;
2824 
2825 #ifdef LIBXML_XPATH_ENABLED
2826     ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2827     if (ctxt->pctxt == NULL) {
2828         xmlFree(ctxt);
2829         return;
2830     }
2831 #endif /* LIBXML_XPATH_ENABLED */
2832     while (1) {
2833         if (ctxt->node == (xmlNodePtr) ctxt->doc)
2834             snprintf(prompt, sizeof(prompt), "%s > ", "/");
2835         else if ((ctxt->node != NULL) && (ctxt->node->name) &&
2836                  (ctxt->node->ns) && (ctxt->node->ns->prefix))
2837             snprintf(prompt, sizeof(prompt), "%s:%s > ",
2838                      (ctxt->node->ns->prefix), ctxt->node->name);
2839         else if ((ctxt->node != NULL) && (ctxt->node->name))
2840             snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
2841         else
2842             snprintf(prompt, sizeof(prompt), "? > ");
2843         prompt[sizeof(prompt) - 1] = 0;
2844 
2845         /*
2846          * Get a new command line
2847          */
2848         cmdline = ctxt->input(prompt);
2849         if (cmdline == NULL)
2850             break;
2851 
2852         /*
2853          * Parse the command itself
2854          */
2855         cur = cmdline;
2856         while ((*cur == ' ') || (*cur == '\t'))
2857             cur++;
2858         i = 0;
2859         while ((*cur != ' ') && (*cur != '\t') &&
2860                (*cur != '\n') && (*cur != '\r')) {
2861             if (*cur == 0)
2862                 break;
2863             command[i++] = *cur++;
2864         }
2865         command[i] = 0;
2866         if (i == 0)
2867             continue;
2868 
2869         /*
2870          * Parse the argument
2871          */
2872         while ((*cur == ' ') || (*cur == '\t'))
2873             cur++;
2874         i = 0;
2875         while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2876             if (*cur == 0)
2877                 break;
2878             arg[i++] = *cur++;
2879         }
2880         arg[i] = 0;
2881 
2882         /*
2883          * start interpreting the command
2884          */
2885         if (!strcmp(command, "exit"))
2886             break;
2887         if (!strcmp(command, "quit"))
2888             break;
2889         if (!strcmp(command, "bye"))
2890             break;
2891 		if (!strcmp(command, "help")) {
2892 		  fprintf(ctxt->output, "\tbase         display XML base of the node\n");
2893 		  fprintf(ctxt->output, "\tsetbase URI  change the XML base of the node\n");
2894 		  fprintf(ctxt->output, "\tbye          leave shell\n");
2895 		  fprintf(ctxt->output, "\tcat [node]   display node or current node\n");
2896 		  fprintf(ctxt->output, "\tcd [path]    change directory to path or to root\n");
2897 		  fprintf(ctxt->output, "\tdir [path]   dumps information about the node (namespace, attributes, content)\n");
2898 		  fprintf(ctxt->output, "\tdu [path]    show the structure of the subtree under path or the current node\n");
2899 		  fprintf(ctxt->output, "\texit         leave shell\n");
2900 		  fprintf(ctxt->output, "\thelp         display this help\n");
2901 		  fprintf(ctxt->output, "\tfree         display memory usage\n");
2902 		  fprintf(ctxt->output, "\tload [name]  load a new document with name\n");
2903 		  fprintf(ctxt->output, "\tls [path]    list contents of path or the current directory\n");
2904 		  fprintf(ctxt->output, "\tset xml_fragment replace the current node content with the fragment parsed in context\n");
2905 #ifdef LIBXML_XPATH_ENABLED
2906 		  fprintf(ctxt->output, "\txpath expr   evaluate the XPath expression in that context and print the result\n");
2907 		  fprintf(ctxt->output, "\tsetns nsreg  register a namespace to a prefix in the XPath evaluation context\n");
2908 		  fprintf(ctxt->output, "\t             format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
2909 		  fprintf(ctxt->output, "\tsetrootns    register all namespace found on the root element\n");
2910 		  fprintf(ctxt->output, "\t             the default namespace if any uses 'defaultns' prefix\n");
2911 #endif /* LIBXML_XPATH_ENABLED */
2912 		  fprintf(ctxt->output, "\tpwd          display current working directory\n");
2913 		  fprintf(ctxt->output, "\twhereis      display absolute path of [path] or current working directory\n");
2914 		  fprintf(ctxt->output, "\tquit         leave shell\n");
2915 #ifdef LIBXML_OUTPUT_ENABLED
2916 		  fprintf(ctxt->output, "\tsave [name]  save this document to name or the original name\n");
2917 		  fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
2918 #endif /* LIBXML_OUTPUT_ENABLED */
2919 #ifdef LIBXML_VALID_ENABLED
2920 		  fprintf(ctxt->output, "\tvalidate     check the document for errors\n");
2921 #endif /* LIBXML_VALID_ENABLED */
2922 #ifdef LIBXML_SCHEMAS_ENABLED
2923 		  fprintf(ctxt->output, "\trelaxng rng  validate the document against the Relax-NG schemas\n");
2924 #endif
2925 		  fprintf(ctxt->output, "\tgrep string  search for a string in the subtree\n");
2926 #ifdef LIBXML_VALID_ENABLED
2927         } else if (!strcmp(command, "validate")) {
2928             xmlShellValidate(ctxt, arg, NULL, NULL);
2929 #endif /* LIBXML_VALID_ENABLED */
2930         } else if (!strcmp(command, "load")) {
2931             xmlShellLoad(ctxt, arg, NULL, NULL);
2932 #ifdef LIBXML_SCHEMAS_ENABLED
2933         } else if (!strcmp(command, "relaxng")) {
2934             xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2935 #endif
2936 #ifdef LIBXML_OUTPUT_ENABLED
2937         } else if (!strcmp(command, "save")) {
2938             xmlShellSave(ctxt, arg, NULL, NULL);
2939         } else if (!strcmp(command, "write")) {
2940 	    if (arg[0] == 0)
2941 		xmlGenericError(xmlGenericErrorContext,
2942                         "Write command requires a filename argument\n");
2943 	    else
2944 		xmlShellWrite(ctxt, arg, ctxt->node, NULL);
2945 #endif /* LIBXML_OUTPUT_ENABLED */
2946         } else if (!strcmp(command, "grep")) {
2947             xmlShellGrep(ctxt, arg, ctxt->node, NULL);
2948         } else if (!strcmp(command, "free")) {
2949             if (arg[0] == 0) {
2950                 xmlMemShow(ctxt->output, 0);
2951             } else {
2952                 int len = 0;
2953 
2954                 sscanf(arg, "%d", &len);
2955                 xmlMemShow(ctxt->output, len);
2956             }
2957         } else if (!strcmp(command, "pwd")) {
2958             char dir[500];
2959 
2960             if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
2961                 fprintf(ctxt->output, "%s\n", dir);
2962         } else if (!strcmp(command, "du")) {
2963             if (arg[0] == 0) {
2964                 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2965             } else {
2966                 ctxt->pctxt->node = ctxt->node;
2967 #ifdef LIBXML_XPATH_ENABLED
2968                 ctxt->pctxt->node = ctxt->node;
2969                 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2970 #else
2971                 list = NULL;
2972 #endif /* LIBXML_XPATH_ENABLED */
2973                 if (list != NULL) {
2974                     switch (list->type) {
2975                         case XPATH_UNDEFINED:
2976                             xmlGenericError(xmlGenericErrorContext,
2977                                             "%s: no such node\n", arg);
2978                             break;
2979                         case XPATH_NODESET:{
2980                             int indx;
2981 
2982                             if (list->nodesetval == NULL)
2983                                 break;
2984 
2985                             for (indx = 0;
2986                                  indx < list->nodesetval->nodeNr;
2987                                  indx++)
2988                                 xmlShellDu(ctxt, NULL,
2989                                            list->nodesetval->
2990                                            nodeTab[indx], NULL);
2991                             break;
2992                         }
2993                         case XPATH_BOOLEAN:
2994                             xmlGenericError(xmlGenericErrorContext,
2995                                             "%s is a Boolean\n", arg);
2996                             break;
2997                         case XPATH_NUMBER:
2998                             xmlGenericError(xmlGenericErrorContext,
2999                                             "%s is a number\n", arg);
3000                             break;
3001                         case XPATH_STRING:
3002                             xmlGenericError(xmlGenericErrorContext,
3003                                             "%s is a string\n", arg);
3004                             break;
3005 #ifdef LIBXML_XPTR_LOCS_ENABLED
3006                         case XPATH_POINT:
3007                             xmlGenericError(xmlGenericErrorContext,
3008                                             "%s is a point\n", arg);
3009                             break;
3010                         case XPATH_RANGE:
3011                             xmlGenericError(xmlGenericErrorContext,
3012                                             "%s is a range\n", arg);
3013                             break;
3014                         case XPATH_LOCATIONSET:
3015                             xmlGenericError(xmlGenericErrorContext,
3016                                             "%s is a range\n", arg);
3017                             break;
3018 #endif /* LIBXML_XPTR_LOCS_ENABLED */
3019                         case XPATH_USERS:
3020                             xmlGenericError(xmlGenericErrorContext,
3021                                             "%s is user-defined\n", arg);
3022                             break;
3023                         case XPATH_XSLT_TREE:
3024                             xmlGenericError(xmlGenericErrorContext,
3025                                             "%s is an XSLT value tree\n",
3026                                             arg);
3027                             break;
3028                     }
3029 #ifdef LIBXML_XPATH_ENABLED
3030                     xmlXPathFreeObject(list);
3031 #endif
3032                 } else {
3033                     xmlGenericError(xmlGenericErrorContext,
3034                                     "%s: no such node\n", arg);
3035                 }
3036                 ctxt->pctxt->node = NULL;
3037             }
3038         } else if (!strcmp(command, "base")) {
3039             xmlShellBase(ctxt, NULL, ctxt->node, NULL);
3040         } else if (!strcmp(command, "set")) {
3041 	    xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
3042 #ifdef LIBXML_XPATH_ENABLED
3043         } else if (!strcmp(command, "setns")) {
3044             if (arg[0] == 0) {
3045 		xmlGenericError(xmlGenericErrorContext,
3046 				"setns: prefix=[nsuri] required\n");
3047             } else {
3048                 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
3049             }
3050         } else if (!strcmp(command, "setrootns")) {
3051 	    xmlNodePtr root;
3052 
3053 	    root = xmlDocGetRootElement(ctxt->doc);
3054 	    xmlShellRegisterRootNamespaces(ctxt, NULL, root, NULL);
3055         } else if (!strcmp(command, "xpath")) {
3056             if (arg[0] == 0) {
3057 		xmlGenericError(xmlGenericErrorContext,
3058 				"xpath: expression required\n");
3059 	    } else {
3060                 ctxt->pctxt->node = ctxt->node;
3061                 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3062 		xmlXPathDebugDumpObject(ctxt->output, list, 0);
3063 		xmlXPathFreeObject(list);
3064 	    }
3065 #endif /* LIBXML_XPATH_ENABLED */
3066 #ifdef LIBXML_TREE_ENABLED
3067         } else if (!strcmp(command, "setbase")) {
3068             xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
3069 #endif
3070         } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
3071             int dir = (!strcmp(command, "dir"));
3072 
3073             if (arg[0] == 0) {
3074                 if (dir)
3075                     xmlShellDir(ctxt, NULL, ctxt->node, NULL);
3076                 else
3077                     xmlShellList(ctxt, NULL, ctxt->node, NULL);
3078             } else {
3079                 ctxt->pctxt->node = ctxt->node;
3080 #ifdef LIBXML_XPATH_ENABLED
3081                 ctxt->pctxt->node = ctxt->node;
3082                 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3083 #else
3084                 list = NULL;
3085 #endif /* LIBXML_XPATH_ENABLED */
3086                 if (list != NULL) {
3087                     switch (list->type) {
3088                         case XPATH_UNDEFINED:
3089                             xmlGenericError(xmlGenericErrorContext,
3090                                             "%s: no such node\n", arg);
3091                             break;
3092                         case XPATH_NODESET:{
3093                                 int indx;
3094 
3095 				if (list->nodesetval == NULL)
3096 				    break;
3097 
3098                                 for (indx = 0;
3099                                      indx < list->nodesetval->nodeNr;
3100                                      indx++) {
3101                                     if (dir)
3102                                         xmlShellDir(ctxt, NULL,
3103                                                     list->nodesetval->
3104                                                     nodeTab[indx], NULL);
3105                                     else
3106                                         xmlShellList(ctxt, NULL,
3107                                                      list->nodesetval->
3108                                                      nodeTab[indx], NULL);
3109                                 }
3110                                 break;
3111                             }
3112                         case XPATH_BOOLEAN:
3113                             xmlGenericError(xmlGenericErrorContext,
3114                                             "%s is a Boolean\n", arg);
3115                             break;
3116                         case XPATH_NUMBER:
3117                             xmlGenericError(xmlGenericErrorContext,
3118                                             "%s is a number\n", arg);
3119                             break;
3120                         case XPATH_STRING:
3121                             xmlGenericError(xmlGenericErrorContext,
3122                                             "%s is a string\n", arg);
3123                             break;
3124 #ifdef LIBXML_XPTR_LOCS_ENABLED
3125                         case XPATH_POINT:
3126                             xmlGenericError(xmlGenericErrorContext,
3127                                             "%s is a point\n", arg);
3128                             break;
3129                         case XPATH_RANGE:
3130                             xmlGenericError(xmlGenericErrorContext,
3131                                             "%s is a range\n", arg);
3132                             break;
3133                         case XPATH_LOCATIONSET:
3134                             xmlGenericError(xmlGenericErrorContext,
3135                                             "%s is a range\n", arg);
3136                             break;
3137 #endif /* LIBXML_XPTR_LOCS_ENABLED */
3138                         case XPATH_USERS:
3139                             xmlGenericError(xmlGenericErrorContext,
3140                                             "%s is user-defined\n", arg);
3141                             break;
3142                         case XPATH_XSLT_TREE:
3143                             xmlGenericError(xmlGenericErrorContext,
3144                                             "%s is an XSLT value tree\n",
3145                                             arg);
3146                             break;
3147                     }
3148 #ifdef LIBXML_XPATH_ENABLED
3149                     xmlXPathFreeObject(list);
3150 #endif
3151                 } else {
3152                     xmlGenericError(xmlGenericErrorContext,
3153                                     "%s: no such node\n", arg);
3154                 }
3155                 ctxt->pctxt->node = NULL;
3156             }
3157         } else if (!strcmp(command, "whereis")) {
3158             char dir[500];
3159 
3160             if (arg[0] == 0) {
3161                 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
3162                     fprintf(ctxt->output, "%s\n", dir);
3163             } else {
3164                 ctxt->pctxt->node = ctxt->node;
3165 #ifdef LIBXML_XPATH_ENABLED
3166                 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3167 #else
3168                 list = NULL;
3169 #endif /* LIBXML_XPATH_ENABLED */
3170                 if (list != NULL) {
3171                     switch (list->type) {
3172                         case XPATH_UNDEFINED:
3173                             xmlGenericError(xmlGenericErrorContext,
3174                                             "%s: no such node\n", arg);
3175                             break;
3176                         case XPATH_NODESET:{
3177                                 int indx;
3178 
3179 				if (list->nodesetval == NULL)
3180 				    break;
3181 
3182                                 for (indx = 0;
3183                                      indx < list->nodesetval->nodeNr;
3184                                      indx++) {
3185                                     if (!xmlShellPwd(ctxt, dir, list->nodesetval->
3186                                                      nodeTab[indx], NULL))
3187                                         fprintf(ctxt->output, "%s\n", dir);
3188                                 }
3189                                 break;
3190                             }
3191                         case XPATH_BOOLEAN:
3192                             xmlGenericError(xmlGenericErrorContext,
3193                                             "%s is a Boolean\n", arg);
3194                             break;
3195                         case XPATH_NUMBER:
3196                             xmlGenericError(xmlGenericErrorContext,
3197                                             "%s is a number\n", arg);
3198                             break;
3199                         case XPATH_STRING:
3200                             xmlGenericError(xmlGenericErrorContext,
3201                                             "%s is a string\n", arg);
3202                             break;
3203 #ifdef LIBXML_XPTR_LOCS_ENABLED
3204                         case XPATH_POINT:
3205                             xmlGenericError(xmlGenericErrorContext,
3206                                             "%s is a point\n", arg);
3207                             break;
3208                         case XPATH_RANGE:
3209                             xmlGenericError(xmlGenericErrorContext,
3210                                             "%s is a range\n", arg);
3211                             break;
3212                         case XPATH_LOCATIONSET:
3213                             xmlGenericError(xmlGenericErrorContext,
3214                                             "%s is a range\n", arg);
3215                             break;
3216 #endif /* LIBXML_XPTR_LOCS_ENABLED */
3217                         case XPATH_USERS:
3218                             xmlGenericError(xmlGenericErrorContext,
3219                                             "%s is user-defined\n", arg);
3220                             break;
3221                         case XPATH_XSLT_TREE:
3222                             xmlGenericError(xmlGenericErrorContext,
3223                                             "%s is an XSLT value tree\n",
3224                                             arg);
3225                             break;
3226                     }
3227 #ifdef LIBXML_XPATH_ENABLED
3228                     xmlXPathFreeObject(list);
3229 #endif
3230                 } else {
3231                     xmlGenericError(xmlGenericErrorContext,
3232                                     "%s: no such node\n", arg);
3233                 }
3234                 ctxt->pctxt->node = NULL;
3235             }
3236         } else if (!strcmp(command, "cd")) {
3237             if (arg[0] == 0) {
3238                 ctxt->node = (xmlNodePtr) ctxt->doc;
3239             } else {
3240 #ifdef LIBXML_XPATH_ENABLED
3241                 int l;
3242 
3243                 ctxt->pctxt->node = ctxt->node;
3244 		l = strlen(arg);
3245 		if ((l >= 2) && (arg[l - 1] == '/'))
3246 		    arg[l - 1] = 0;
3247                 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3248 #else
3249                 list = NULL;
3250 #endif /* LIBXML_XPATH_ENABLED */
3251                 if (list != NULL) {
3252                     switch (list->type) {
3253                         case XPATH_UNDEFINED:
3254                             xmlGenericError(xmlGenericErrorContext,
3255                                             "%s: no such node\n", arg);
3256                             break;
3257                         case XPATH_NODESET:
3258                             if (list->nodesetval != NULL) {
3259 				if (list->nodesetval->nodeNr == 1) {
3260 				    ctxt->node = list->nodesetval->nodeTab[0];
3261 				    if ((ctxt->node != NULL) &&
3262 				        (ctxt->node->type ==
3263 					 XML_NAMESPACE_DECL)) {
3264 					xmlGenericError(xmlGenericErrorContext,
3265 						    "cannot cd to namespace\n");
3266 					ctxt->node = NULL;
3267 				    }
3268 				} else
3269 				    xmlGenericError(xmlGenericErrorContext,
3270 						    "%s is a %d Node Set\n",
3271 						    arg,
3272 						    list->nodesetval->nodeNr);
3273                             } else
3274                                 xmlGenericError(xmlGenericErrorContext,
3275                                                 "%s is an empty Node Set\n",
3276                                                 arg);
3277                             break;
3278                         case XPATH_BOOLEAN:
3279                             xmlGenericError(xmlGenericErrorContext,
3280                                             "%s is a Boolean\n", arg);
3281                             break;
3282                         case XPATH_NUMBER:
3283                             xmlGenericError(xmlGenericErrorContext,
3284                                             "%s is a number\n", arg);
3285                             break;
3286                         case XPATH_STRING:
3287                             xmlGenericError(xmlGenericErrorContext,
3288                                             "%s is a string\n", arg);
3289                             break;
3290 #ifdef LIBXML_XPTR_LOCS_ENABLED
3291                         case XPATH_POINT:
3292                             xmlGenericError(xmlGenericErrorContext,
3293                                             "%s is a point\n", arg);
3294                             break;
3295                         case XPATH_RANGE:
3296                             xmlGenericError(xmlGenericErrorContext,
3297                                             "%s is a range\n", arg);
3298                             break;
3299                         case XPATH_LOCATIONSET:
3300                             xmlGenericError(xmlGenericErrorContext,
3301                                             "%s is a range\n", arg);
3302                             break;
3303 #endif /* LIBXML_XPTR_LOCS_ENABLED */
3304                         case XPATH_USERS:
3305                             xmlGenericError(xmlGenericErrorContext,
3306                                             "%s is user-defined\n", arg);
3307                             break;
3308                         case XPATH_XSLT_TREE:
3309                             xmlGenericError(xmlGenericErrorContext,
3310                                             "%s is an XSLT value tree\n",
3311                                             arg);
3312                             break;
3313                     }
3314 #ifdef LIBXML_XPATH_ENABLED
3315                     xmlXPathFreeObject(list);
3316 #endif
3317                 } else {
3318                     xmlGenericError(xmlGenericErrorContext,
3319                                     "%s: no such node\n", arg);
3320                 }
3321                 ctxt->pctxt->node = NULL;
3322             }
3323 #ifdef LIBXML_OUTPUT_ENABLED
3324         } else if (!strcmp(command, "cat")) {
3325             if (arg[0] == 0) {
3326                 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3327             } else {
3328                 ctxt->pctxt->node = ctxt->node;
3329 #ifdef LIBXML_XPATH_ENABLED
3330                 ctxt->pctxt->node = ctxt->node;
3331                 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3332 #else
3333                 list = NULL;
3334 #endif /* LIBXML_XPATH_ENABLED */
3335                 if (list != NULL) {
3336                     switch (list->type) {
3337                         case XPATH_UNDEFINED:
3338                             xmlGenericError(xmlGenericErrorContext,
3339                                             "%s: no such node\n", arg);
3340                             break;
3341                         case XPATH_NODESET:{
3342                                 int indx;
3343 
3344 				if (list->nodesetval == NULL)
3345 				    break;
3346 
3347                                 for (indx = 0;
3348                                      indx < list->nodesetval->nodeNr;
3349                                      indx++) {
3350                                     if (i > 0)
3351                                         fprintf(ctxt->output, " -------\n");
3352                                     xmlShellCat(ctxt, NULL,
3353                                                 list->nodesetval->
3354                                                 nodeTab[indx], NULL);
3355                                 }
3356                                 break;
3357                             }
3358                         case XPATH_BOOLEAN:
3359                             xmlGenericError(xmlGenericErrorContext,
3360                                             "%s is a Boolean\n", arg);
3361                             break;
3362                         case XPATH_NUMBER:
3363                             xmlGenericError(xmlGenericErrorContext,
3364                                             "%s is a number\n", arg);
3365                             break;
3366                         case XPATH_STRING:
3367                             xmlGenericError(xmlGenericErrorContext,
3368                                             "%s is a string\n", arg);
3369                             break;
3370 #ifdef LIBXML_XPTR_LOCS_ENABLED
3371                         case XPATH_POINT:
3372                             xmlGenericError(xmlGenericErrorContext,
3373                                             "%s is a point\n", arg);
3374                             break;
3375                         case XPATH_RANGE:
3376                             xmlGenericError(xmlGenericErrorContext,
3377                                             "%s is a range\n", arg);
3378                             break;
3379                         case XPATH_LOCATIONSET:
3380                             xmlGenericError(xmlGenericErrorContext,
3381                                             "%s is a range\n", arg);
3382                             break;
3383 #endif /* LIBXML_XPTR_LOCS_ENABLED */
3384                         case XPATH_USERS:
3385                             xmlGenericError(xmlGenericErrorContext,
3386                                             "%s is user-defined\n", arg);
3387                             break;
3388                         case XPATH_XSLT_TREE:
3389                             xmlGenericError(xmlGenericErrorContext,
3390                                             "%s is an XSLT value tree\n",
3391                                             arg);
3392                             break;
3393                     }
3394 #ifdef LIBXML_XPATH_ENABLED
3395                     xmlXPathFreeObject(list);
3396 #endif
3397                 } else {
3398                     xmlGenericError(xmlGenericErrorContext,
3399                                     "%s: no such node\n", arg);
3400                 }
3401                 ctxt->pctxt->node = NULL;
3402             }
3403 #endif /* LIBXML_OUTPUT_ENABLED */
3404         } else {
3405             xmlGenericError(xmlGenericErrorContext,
3406                             "Unknown command %s\n", command);
3407         }
3408         free(cmdline);          /* not xmlFree here ! */
3409 	cmdline = NULL;
3410     }
3411 #ifdef LIBXML_XPATH_ENABLED
3412     xmlXPathFreeContext(ctxt->pctxt);
3413 #endif /* LIBXML_XPATH_ENABLED */
3414     if (ctxt->loaded) {
3415         xmlFreeDoc(ctxt->doc);
3416     }
3417     if (ctxt->filename != NULL)
3418         xmlFree(ctxt->filename);
3419     xmlFree(ctxt);
3420     if (cmdline != NULL)
3421         free(cmdline);          /* not xmlFree here ! */
3422 }
3423 
3424 #endif /* LIBXML_XPATH_ENABLED */
3425 
3426 #endif /* LIBXML_DEBUG_ENABLED */
3427