- add sources.
[platform/framework/web/crosswalk.git] / src / third_party / libxml / src / debugXML.c
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 #ifdef HAVE_STDLIB_H
16 #include <stdlib.h>
17 #endif
18 #ifdef HAVE_STRING_H
19 #include <string.h>
20 #endif
21 #include <libxml/xmlmemory.h>
22 #include <libxml/tree.h>
23 #include <libxml/parser.h>
24 #include <libxml/parserInternals.h>
25 #include <libxml/valid.h>
26 #include <libxml/debugXML.h>
27 #include <libxml/HTMLtree.h>
28 #include <libxml/HTMLparser.h>
29 #include <libxml/xmlerror.h>
30 #include <libxml/globals.h>
31 #include <libxml/xpathInternals.h>
32 #include <libxml/uri.h>
33 #ifdef LIBXML_SCHEMAS_ENABLED
34 #include <libxml/relaxng.h>
35 #endif
36
37 #define DUMP_TEXT_TYPE 1
38
39 typedef struct _xmlDebugCtxt xmlDebugCtxt;
40 typedef xmlDebugCtxt *xmlDebugCtxtPtr;
41 struct _xmlDebugCtxt {
42     FILE *output;               /* the output file */
43     char shift[101];            /* used for indenting */
44     int depth;                  /* current depth */
45     xmlDocPtr doc;              /* current document */
46     xmlNodePtr node;            /* current node */
47     xmlDictPtr dict;            /* the doc dictionnary */
48     int check;                  /* do just checkings */
49     int errors;                 /* number of errors found */
50     int nodict;                 /* if the document has no dictionnary */
51     int options;                /* options */
52 };
53
54 static void xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node);
55
56 static void
57 xmlCtxtDumpInitCtxt(xmlDebugCtxtPtr ctxt)
58 {
59     int i;
60
61     ctxt->depth = 0;
62     ctxt->check = 0;
63     ctxt->errors = 0;
64     ctxt->output = stdout;
65     ctxt->doc = NULL;
66     ctxt->node = NULL;
67     ctxt->dict = NULL;
68     ctxt->nodict = 0;
69     ctxt->options = 0;
70     for (i = 0; i < 100; i++)
71         ctxt->shift[i] = ' ';
72     ctxt->shift[100] = 0;
73 }
74
75 static void
76 xmlCtxtDumpCleanCtxt(xmlDebugCtxtPtr ctxt ATTRIBUTE_UNUSED)
77 {
78  /* remove the ATTRIBUTE_UNUSED when this is added */
79 }
80
81 /**
82  * xmlNsCheckScope:
83  * @node: the node
84  * @ns: the namespace node
85  *
86  * Check that a given namespace is in scope on a node.
87  *
88  * Returns 1 if in scope, -1 in case of argument error, 
89  *         -2 if the namespace is not in scope, and -3 if not on
90  *         an ancestor node.
91  */
92 static int
93 xmlNsCheckScope(xmlNodePtr node, xmlNsPtr ns)
94 {
95     xmlNsPtr cur;
96
97     if ((node == NULL) || (ns == NULL))
98         return(-1);
99
100     if ((node->type != XML_ELEMENT_NODE) &&
101         (node->type != XML_ATTRIBUTE_NODE) &&
102         (node->type != XML_DOCUMENT_NODE) &&
103         (node->type != XML_TEXT_NODE) &&
104         (node->type != XML_HTML_DOCUMENT_NODE) &&
105         (node->type != XML_XINCLUDE_START))
106         return(-2);
107
108     while ((node != NULL) &&
109            ((node->type == XML_ELEMENT_NODE) ||
110             (node->type == XML_ATTRIBUTE_NODE) ||
111             (node->type == XML_TEXT_NODE) ||
112             (node->type == XML_XINCLUDE_START))) {
113         if ((node->type == XML_ELEMENT_NODE) ||
114             (node->type == XML_XINCLUDE_START)) {
115             cur = node->nsDef;
116             while (cur != NULL) {
117                 if (cur == ns)
118                     return(1);
119                 if (xmlStrEqual(cur->prefix, ns->prefix))
120                     return(-2);
121                 cur = cur->next;
122             }
123         }
124         node = node->parent;
125     }
126     /* the xml namespace may be declared on the document node */
127     if ((node != NULL) &&
128         ((node->type == XML_DOCUMENT_NODE) ||
129          (node->type == XML_HTML_DOCUMENT_NODE))) {
130          xmlNsPtr oldNs = ((xmlDocPtr) node)->oldNs;
131          if (oldNs == ns)
132              return(1);
133     }
134     return(-3);
135 }
136
137 static void
138 xmlCtxtDumpSpaces(xmlDebugCtxtPtr ctxt)
139 {
140     if (ctxt->check)
141         return;
142     if ((ctxt->output != NULL) && (ctxt->depth > 0)) {
143         if (ctxt->depth < 50)
144             fprintf(ctxt->output, "%s", &ctxt->shift[100 - 2 * ctxt->depth]);
145         else
146             fprintf(ctxt->output, "%s", ctxt->shift);
147     }
148 }
149
150 /**
151  * xmlDebugErr:
152  * @ctxt:  a debug context
153  * @error:  the error code
154  *
155  * Handle a debug error.
156  */
157 static void
158 xmlDebugErr(xmlDebugCtxtPtr ctxt, int error, const char *msg)
159 {
160     ctxt->errors++;
161     __xmlRaiseError(NULL, NULL, NULL,
162                     NULL, ctxt->node, XML_FROM_CHECK,
163                     error, XML_ERR_ERROR, NULL, 0,
164                     NULL, NULL, NULL, 0, 0,
165                     "%s", msg);
166 }
167 static void
168 xmlDebugErr2(xmlDebugCtxtPtr ctxt, int error, const char *msg, int extra)
169 {
170     ctxt->errors++;
171     __xmlRaiseError(NULL, NULL, NULL,
172                     NULL, ctxt->node, XML_FROM_CHECK,
173                     error, XML_ERR_ERROR, NULL, 0,
174                     NULL, NULL, NULL, 0, 0,
175                     msg, extra);
176 }
177 static void
178 xmlDebugErr3(xmlDebugCtxtPtr ctxt, int error, const char *msg, const char *extra)
179 {
180     ctxt->errors++;
181     __xmlRaiseError(NULL, NULL, NULL,
182                     NULL, ctxt->node, XML_FROM_CHECK,
183                     error, XML_ERR_ERROR, NULL, 0,
184                     NULL, NULL, NULL, 0, 0,
185                     msg, extra);
186 }
187
188 /**
189  * xmlCtxtNsCheckScope:
190  * @ctxt: the debugging context
191  * @node: the node
192  * @ns: the namespace node
193  *
194  * Report if a given namespace is is not in scope.
195  */
196 static void
197 xmlCtxtNsCheckScope(xmlDebugCtxtPtr ctxt, xmlNodePtr node, xmlNsPtr ns)
198 {
199     int ret;
200
201     ret = xmlNsCheckScope(node, ns);
202     if (ret == -2) {
203         if (ns->prefix == NULL)
204             xmlDebugErr(ctxt, XML_CHECK_NS_SCOPE,
205                         "Reference to default namespace not in scope\n");
206         else
207             xmlDebugErr3(ctxt, XML_CHECK_NS_SCOPE,
208                          "Reference to namespace '%s' not in scope\n",
209                          (char *) ns->prefix);
210     }
211     if (ret == -3) {
212         if (ns->prefix == NULL)
213             xmlDebugErr(ctxt, XML_CHECK_NS_ANCESTOR,
214                         "Reference to default namespace not on ancestor\n");
215         else
216             xmlDebugErr3(ctxt, XML_CHECK_NS_ANCESTOR,
217                          "Reference to namespace '%s' not on ancestor\n",
218                          (char *) ns->prefix);
219     }
220 }
221
222 /**
223  * xmlCtxtCheckString:
224  * @ctxt: the debug context
225  * @str: the string
226  *
227  * Do debugging on the string, currently it just checks the UTF-8 content
228  */
229 static void
230 xmlCtxtCheckString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
231 {
232     if (str == NULL) return;
233     if (ctxt->check) {
234         if (!xmlCheckUTF8(str)) {
235             xmlDebugErr3(ctxt, XML_CHECK_NOT_UTF8,
236                          "String is not UTF-8 %s", (const char *) str);
237         }
238     }
239 }
240
241 /**
242  * xmlCtxtCheckName:
243  * @ctxt: the debug context
244  * @name: the name
245  *
246  * Do debugging on the name, for example the dictionnary status and
247  * conformance to the Name production.
248  */
249 static void
250 xmlCtxtCheckName(xmlDebugCtxtPtr ctxt, const xmlChar * name)
251 {
252     if (ctxt->check) {
253         if (name == NULL) {
254             xmlDebugErr(ctxt, XML_CHECK_NO_NAME, "Name is NULL");
255             return;
256         }
257         if (xmlValidateName(name, 0)) {
258             xmlDebugErr3(ctxt, XML_CHECK_NOT_NCNAME,
259                          "Name is not an NCName '%s'", (const char *) name);
260         }
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 dictionnary '%s'",
267                          (const char *) name);
268         }
269     }
270 }
271
272 static void
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             /* desactivated 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 dictionnary\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 #ifdef LIBXML_DOCB_ENABLED
411         case XML_DOCB_DOCUMENT_NODE:
412 #endif
413         case XML_DOCUMENT_NODE:
414         case XML_HTML_DOCUMENT_NODE:
415             break;
416     }
417 }
418
419 static void
420 xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
421 {
422     int i;
423
424     if (ctxt->check) {
425         return;
426     }
427     /* TODO: check UTF8 content of the string */
428     if (str == NULL) {
429         fprintf(ctxt->output, "(NULL)");
430         return;
431     }
432     for (i = 0; i < 40; i++)
433         if (str[i] == 0)
434             return;
435         else if (IS_BLANK_CH(str[i]))
436             fputc(' ', ctxt->output);
437         else if (str[i] >= 0x80)
438             fprintf(ctxt->output, "#%X", str[i]);
439         else
440             fputc(str[i], ctxt->output);
441     fprintf(ctxt->output, "...");
442 }
443
444 static void
445 xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
446 {
447     xmlCtxtDumpSpaces(ctxt);
448
449     if (dtd == NULL) {
450         if (!ctxt->check)
451             fprintf(ctxt->output, "DTD node is NULL\n");
452         return;
453     }
454
455     if (dtd->type != XML_DTD_NODE) {
456         xmlDebugErr(ctxt, XML_CHECK_NOT_DTD,
457                     "Node is not a DTD");
458         return;
459     }
460     if (!ctxt->check) {
461         if (dtd->name != NULL)
462             fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name);
463         else
464             fprintf(ctxt->output, "DTD");
465         if (dtd->ExternalID != NULL)
466             fprintf(ctxt->output, ", PUBLIC %s", (char *) dtd->ExternalID);
467         if (dtd->SystemID != NULL)
468             fprintf(ctxt->output, ", SYSTEM %s", (char *) dtd->SystemID);
469         fprintf(ctxt->output, "\n");
470     }
471     /*
472      * Do a bit of checking
473      */
474     xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd);
475 }
476
477 static void
478 xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr)
479 {
480     xmlCtxtDumpSpaces(ctxt);
481
482     if (attr == NULL) {
483         if (!ctxt->check)
484             fprintf(ctxt->output, "Attribute declaration is NULL\n");
485         return;
486     }
487     if (attr->type != XML_ATTRIBUTE_DECL) {
488         xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL,
489                     "Node is not an attribute declaration");
490         return;
491     }
492     if (attr->name != NULL) {
493         if (!ctxt->check)
494             fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name);
495     } else
496         xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
497                     "Node attribute declaration has no name");
498     if (attr->elem != NULL) {
499         if (!ctxt->check)
500             fprintf(ctxt->output, " for %s", (char *) attr->elem);
501     } else
502         xmlDebugErr(ctxt, XML_CHECK_NO_ELEM,
503                     "Node attribute declaration has no element name");
504     if (!ctxt->check) {
505         switch (attr->atype) {
506             case XML_ATTRIBUTE_CDATA:
507                 fprintf(ctxt->output, " CDATA");
508                 break;
509             case XML_ATTRIBUTE_ID:
510                 fprintf(ctxt->output, " ID");
511                 break;
512             case XML_ATTRIBUTE_IDREF:
513                 fprintf(ctxt->output, " IDREF");
514                 break;
515             case XML_ATTRIBUTE_IDREFS:
516                 fprintf(ctxt->output, " IDREFS");
517                 break;
518             case XML_ATTRIBUTE_ENTITY:
519                 fprintf(ctxt->output, " ENTITY");
520                 break;
521             case XML_ATTRIBUTE_ENTITIES:
522                 fprintf(ctxt->output, " ENTITIES");
523                 break;
524             case XML_ATTRIBUTE_NMTOKEN:
525                 fprintf(ctxt->output, " NMTOKEN");
526                 break;
527             case XML_ATTRIBUTE_NMTOKENS:
528                 fprintf(ctxt->output, " NMTOKENS");
529                 break;
530             case XML_ATTRIBUTE_ENUMERATION:
531                 fprintf(ctxt->output, " ENUMERATION");
532                 break;
533             case XML_ATTRIBUTE_NOTATION:
534                 fprintf(ctxt->output, " NOTATION ");
535                 break;
536         }
537         if (attr->tree != NULL) {
538             int indx;
539             xmlEnumerationPtr cur = attr->tree;
540
541             for (indx = 0; indx < 5; indx++) {
542                 if (indx != 0)
543                     fprintf(ctxt->output, "|%s", (char *) cur->name);
544                 else
545                     fprintf(ctxt->output, " (%s", (char *) cur->name);
546                 cur = cur->next;
547                 if (cur == NULL)
548                     break;
549             }
550             if (cur == NULL)
551                 fprintf(ctxt->output, ")");
552             else
553                 fprintf(ctxt->output, "...)");
554         }
555         switch (attr->def) {
556             case XML_ATTRIBUTE_NONE:
557                 break;
558             case XML_ATTRIBUTE_REQUIRED:
559                 fprintf(ctxt->output, " REQUIRED");
560                 break;
561             case XML_ATTRIBUTE_IMPLIED:
562                 fprintf(ctxt->output, " IMPLIED");
563                 break;
564             case XML_ATTRIBUTE_FIXED:
565                 fprintf(ctxt->output, " FIXED");
566                 break;
567         }
568         if (attr->defaultValue != NULL) {
569             fprintf(ctxt->output, "\"");
570             xmlCtxtDumpString(ctxt, attr->defaultValue);
571             fprintf(ctxt->output, "\"");
572         }
573         fprintf(ctxt->output, "\n");
574     }
575
576     /*
577      * Do a bit of checking
578      */
579     xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
580 }
581
582 static void
583 xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem)
584 {
585     xmlCtxtDumpSpaces(ctxt);
586
587     if (elem == NULL) {
588         if (!ctxt->check)
589             fprintf(ctxt->output, "Element declaration is NULL\n");
590         return;
591     }
592     if (elem->type != XML_ELEMENT_DECL) {
593         xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL,
594                     "Node is not an element declaration");
595         return;
596     }
597     if (elem->name != NULL) {
598         if (!ctxt->check) {
599             fprintf(ctxt->output, "ELEMDECL(");
600             xmlCtxtDumpString(ctxt, elem->name);
601             fprintf(ctxt->output, ")");
602         }
603     } else
604         xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
605                     "Element declaration has no name");
606     if (!ctxt->check) {
607         switch (elem->etype) {
608             case XML_ELEMENT_TYPE_UNDEFINED:
609                 fprintf(ctxt->output, ", UNDEFINED");
610                 break;
611             case XML_ELEMENT_TYPE_EMPTY:
612                 fprintf(ctxt->output, ", EMPTY");
613                 break;
614             case XML_ELEMENT_TYPE_ANY:
615                 fprintf(ctxt->output, ", ANY");
616                 break;
617             case XML_ELEMENT_TYPE_MIXED:
618                 fprintf(ctxt->output, ", MIXED ");
619                 break;
620             case XML_ELEMENT_TYPE_ELEMENT:
621                 fprintf(ctxt->output, ", MIXED ");
622                 break;
623         }
624         if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) {
625             char buf[5001];
626
627             buf[0] = 0;
628             xmlSnprintfElementContent(buf, 5000, elem->content, 1);
629             buf[5000] = 0;
630             fprintf(ctxt->output, "%s", buf);
631         }
632         fprintf(ctxt->output, "\n");
633     }
634
635     /*
636      * Do a bit of checking
637      */
638     xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem);
639 }
640
641 static void
642 xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
643 {
644     xmlCtxtDumpSpaces(ctxt);
645
646     if (ent == NULL) {
647         if (!ctxt->check)
648             fprintf(ctxt->output, "Entity declaration is NULL\n");
649         return;
650     }
651     if (ent->type != XML_ENTITY_DECL) {
652         xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL,
653                     "Node is not an entity declaration");
654         return;
655     }
656     if (ent->name != NULL) {
657         if (!ctxt->check) {
658             fprintf(ctxt->output, "ENTITYDECL(");
659             xmlCtxtDumpString(ctxt, ent->name);
660             fprintf(ctxt->output, ")");
661         }
662     } else
663         xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
664                     "Entity declaration has no name");
665     if (!ctxt->check) {
666         switch (ent->etype) {
667             case XML_INTERNAL_GENERAL_ENTITY:
668                 fprintf(ctxt->output, ", internal\n");
669                 break;
670             case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
671                 fprintf(ctxt->output, ", external parsed\n");
672                 break;
673             case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
674                 fprintf(ctxt->output, ", unparsed\n");
675                 break;
676             case XML_INTERNAL_PARAMETER_ENTITY:
677                 fprintf(ctxt->output, ", parameter\n");
678                 break;
679             case XML_EXTERNAL_PARAMETER_ENTITY:
680                 fprintf(ctxt->output, ", external parameter\n");
681                 break;
682             case XML_INTERNAL_PREDEFINED_ENTITY:
683                 fprintf(ctxt->output, ", predefined\n");
684                 break;
685         }
686         if (ent->ExternalID) {
687             xmlCtxtDumpSpaces(ctxt);
688             fprintf(ctxt->output, " ExternalID=%s\n",
689                     (char *) ent->ExternalID);
690         }
691         if (ent->SystemID) {
692             xmlCtxtDumpSpaces(ctxt);
693             fprintf(ctxt->output, " SystemID=%s\n",
694                     (char *) ent->SystemID);
695         }
696         if (ent->URI != NULL) {
697             xmlCtxtDumpSpaces(ctxt);
698             fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI);
699         }
700         if (ent->content) {
701             xmlCtxtDumpSpaces(ctxt);
702             fprintf(ctxt->output, " content=");
703             xmlCtxtDumpString(ctxt, ent->content);
704             fprintf(ctxt->output, "\n");
705         }
706     }
707
708     /*
709      * Do a bit of checking
710      */
711     xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent);
712 }
713
714 static void
715 xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
716 {
717     xmlCtxtDumpSpaces(ctxt);
718
719     if (ns == NULL) {
720         if (!ctxt->check)
721             fprintf(ctxt->output, "namespace node is NULL\n");
722         return;
723     }
724     if (ns->type != XML_NAMESPACE_DECL) {
725         xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL,
726                     "Node is not a namespace declaration");
727         return;
728     }
729     if (ns->href == NULL) {
730         if (ns->prefix != NULL)
731             xmlDebugErr3(ctxt, XML_CHECK_NO_HREF,
732                     "Incomplete namespace %s href=NULL\n",
733                     (char *) ns->prefix);
734         else
735             xmlDebugErr(ctxt, XML_CHECK_NO_HREF,
736                     "Incomplete default namespace href=NULL\n");
737     } else {
738         if (!ctxt->check) {
739             if (ns->prefix != NULL)
740                 fprintf(ctxt->output, "namespace %s href=",
741                         (char *) ns->prefix);
742             else
743                 fprintf(ctxt->output, "default namespace href=");
744
745             xmlCtxtDumpString(ctxt, ns->href);
746             fprintf(ctxt->output, "\n");
747         }
748     }
749 }
750
751 static void
752 xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
753 {
754     while (ns != NULL) {
755         xmlCtxtDumpNamespace(ctxt, ns);
756         ns = ns->next;
757     }
758 }
759
760 static void
761 xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
762 {
763     xmlCtxtDumpSpaces(ctxt);
764
765     if (ent == NULL) {
766         if (!ctxt->check)
767             fprintf(ctxt->output, "Entity is NULL\n");
768         return;
769     }
770     if (!ctxt->check) {
771         switch (ent->etype) {
772             case XML_INTERNAL_GENERAL_ENTITY:
773                 fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY ");
774                 break;
775             case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
776                 fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
777                 break;
778             case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
779                 fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
780                 break;
781             case XML_INTERNAL_PARAMETER_ENTITY:
782                 fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY ");
783                 break;
784             case XML_EXTERNAL_PARAMETER_ENTITY:
785                 fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY ");
786                 break;
787             default:
788                 fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype);
789         }
790         fprintf(ctxt->output, "%s\n", ent->name);
791         if (ent->ExternalID) {
792             xmlCtxtDumpSpaces(ctxt);
793             fprintf(ctxt->output, "ExternalID=%s\n",
794                     (char *) ent->ExternalID);
795         }
796         if (ent->SystemID) {
797             xmlCtxtDumpSpaces(ctxt);
798             fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID);
799         }
800         if (ent->URI) {
801             xmlCtxtDumpSpaces(ctxt);
802             fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI);
803         }
804         if (ent->content) {
805             xmlCtxtDumpSpaces(ctxt);
806             fprintf(ctxt->output, "content=");
807             xmlCtxtDumpString(ctxt, ent->content);
808             fprintf(ctxt->output, "\n");
809         }
810     }
811 }
812
813 /**
814  * xmlCtxtDumpAttr:
815  * @output:  the FILE * for the output
816  * @attr:  the attribute
817  * @depth:  the indentation level.
818  *
819  * Dumps debug information for the attribute
820  */
821 static void
822 xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
823 {
824     xmlCtxtDumpSpaces(ctxt);
825
826     if (attr == NULL) {
827         if (!ctxt->check)
828             fprintf(ctxt->output, "Attr is NULL");
829         return;
830     }
831     if (!ctxt->check) {
832         fprintf(ctxt->output, "ATTRIBUTE ");
833         xmlCtxtDumpString(ctxt, attr->name);
834         fprintf(ctxt->output, "\n");
835         if (attr->children != NULL) {
836             ctxt->depth++;
837             xmlCtxtDumpNodeList(ctxt, attr->children);
838             ctxt->depth--;
839         }
840     }
841     if (attr->name == NULL)
842         xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
843                     "Attribute has no name");
844
845     /*
846      * Do a bit of checking
847      */
848     xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
849 }
850
851 /**
852  * xmlCtxtDumpAttrList:
853  * @output:  the FILE * for the output
854  * @attr:  the attribute list
855  * @depth:  the indentation level.
856  *
857  * Dumps debug information for the attribute list
858  */
859 static void
860 xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
861 {
862     while (attr != NULL) {
863         xmlCtxtDumpAttr(ctxt, attr);
864         attr = attr->next;
865     }
866 }
867
868 /**
869  * xmlCtxtDumpOneNode:
870  * @output:  the FILE * for the output
871  * @node:  the node
872  * @depth:  the indentation level.
873  *
874  * Dumps debug information for the element node, it is not recursive
875  */
876 static void
877 xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
878 {
879     if (node == NULL) {
880         if (!ctxt->check) {
881             xmlCtxtDumpSpaces(ctxt);
882             fprintf(ctxt->output, "node is NULL\n");
883         }
884         return;
885     }
886     ctxt->node = node;
887
888     switch (node->type) {
889         case XML_ELEMENT_NODE:
890             if (!ctxt->check) {
891                 xmlCtxtDumpSpaces(ctxt);
892                 fprintf(ctxt->output, "ELEMENT ");
893                 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
894                     xmlCtxtDumpString(ctxt, node->ns->prefix);
895                     fprintf(ctxt->output, ":");
896                 }
897                 xmlCtxtDumpString(ctxt, node->name);
898                 fprintf(ctxt->output, "\n");
899             }
900             break;
901         case XML_ATTRIBUTE_NODE:
902             if (!ctxt->check)
903                 xmlCtxtDumpSpaces(ctxt);
904             fprintf(ctxt->output, "Error, ATTRIBUTE found here\n");
905             xmlCtxtGenericNodeCheck(ctxt, node);
906             return;
907         case XML_TEXT_NODE:
908             if (!ctxt->check) {
909                 xmlCtxtDumpSpaces(ctxt);
910                 if (node->name == (const xmlChar *) xmlStringTextNoenc)
911                     fprintf(ctxt->output, "TEXT no enc");
912                 else
913                     fprintf(ctxt->output, "TEXT");
914                 if (ctxt->options & DUMP_TEXT_TYPE) {
915                     if (node->content == (xmlChar *) &(node->properties))
916                         fprintf(ctxt->output, " compact\n");
917                     else if (xmlDictOwns(ctxt->dict, node->content) == 1)
918                         fprintf(ctxt->output, " interned\n");
919                     else
920                         fprintf(ctxt->output, "\n");
921                 } else
922                     fprintf(ctxt->output, "\n");
923             }
924             break;
925         case XML_CDATA_SECTION_NODE:
926             if (!ctxt->check) {
927                 xmlCtxtDumpSpaces(ctxt);
928                 fprintf(ctxt->output, "CDATA_SECTION\n");
929             }
930             break;
931         case XML_ENTITY_REF_NODE:
932             if (!ctxt->check) {
933                 xmlCtxtDumpSpaces(ctxt);
934                 fprintf(ctxt->output, "ENTITY_REF(%s)\n",
935                         (char *) node->name);
936             }
937             break;
938         case XML_ENTITY_NODE:
939             if (!ctxt->check) {
940                 xmlCtxtDumpSpaces(ctxt);
941                 fprintf(ctxt->output, "ENTITY\n");
942             }
943             break;
944         case XML_PI_NODE:
945             if (!ctxt->check) {
946                 xmlCtxtDumpSpaces(ctxt);
947                 fprintf(ctxt->output, "PI %s\n", (char *) node->name);
948             }
949             break;
950         case XML_COMMENT_NODE:
951             if (!ctxt->check) {
952                 xmlCtxtDumpSpaces(ctxt);
953                 fprintf(ctxt->output, "COMMENT\n");
954             }
955             break;
956         case XML_DOCUMENT_NODE:
957         case XML_HTML_DOCUMENT_NODE:
958             if (!ctxt->check) {
959                 xmlCtxtDumpSpaces(ctxt);
960             }
961             fprintf(ctxt->output, "Error, DOCUMENT found here\n");
962             xmlCtxtGenericNodeCheck(ctxt, node);
963             return;
964         case XML_DOCUMENT_TYPE_NODE:
965             if (!ctxt->check) {
966                 xmlCtxtDumpSpaces(ctxt);
967                 fprintf(ctxt->output, "DOCUMENT_TYPE\n");
968             }
969             break;
970         case XML_DOCUMENT_FRAG_NODE:
971             if (!ctxt->check) {
972                 xmlCtxtDumpSpaces(ctxt);
973                 fprintf(ctxt->output, "DOCUMENT_FRAG\n");
974             }
975             break;
976         case XML_NOTATION_NODE:
977             if (!ctxt->check) {
978                 xmlCtxtDumpSpaces(ctxt);
979                 fprintf(ctxt->output, "NOTATION\n");
980             }
981             break;
982         case XML_DTD_NODE:
983             xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
984             return;
985         case XML_ELEMENT_DECL:
986             xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
987             return;
988         case XML_ATTRIBUTE_DECL:
989             xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
990             return;
991         case XML_ENTITY_DECL:
992             xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
993             return;
994         case XML_NAMESPACE_DECL:
995             xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
996             return;
997         case XML_XINCLUDE_START:
998             if (!ctxt->check) {
999                 xmlCtxtDumpSpaces(ctxt);
1000                 fprintf(ctxt->output, "INCLUDE START\n");
1001             }
1002             return;
1003         case XML_XINCLUDE_END:
1004             if (!ctxt->check) {
1005                 xmlCtxtDumpSpaces(ctxt);
1006                 fprintf(ctxt->output, "INCLUDE END\n");
1007             }
1008             return;
1009         default:
1010             if (!ctxt->check)
1011                 xmlCtxtDumpSpaces(ctxt);
1012             xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1013                         "Unknown node type %d\n", node->type);
1014             return;
1015     }
1016     if (node->doc == NULL) {
1017         if (!ctxt->check) {
1018             xmlCtxtDumpSpaces(ctxt);
1019         }
1020         fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
1021     }
1022     ctxt->depth++;
1023     if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL))
1024         xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
1025     if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL))
1026         xmlCtxtDumpAttrList(ctxt, node->properties);
1027     if (node->type != XML_ENTITY_REF_NODE) {
1028         if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
1029             if (!ctxt->check) {
1030                 xmlCtxtDumpSpaces(ctxt);
1031                 fprintf(ctxt->output, "content=");
1032                 xmlCtxtDumpString(ctxt, node->content);
1033                 fprintf(ctxt->output, "\n");
1034             }
1035         }
1036     } else {
1037         xmlEntityPtr ent;
1038
1039         ent = xmlGetDocEntity(node->doc, node->name);
1040         if (ent != NULL)
1041             xmlCtxtDumpEntity(ctxt, ent);
1042     }
1043     ctxt->depth--;
1044
1045     /*
1046      * Do a bit of checking
1047      */
1048     xmlCtxtGenericNodeCheck(ctxt, node);
1049 }
1050
1051 /**
1052  * xmlCtxtDumpNode:
1053  * @output:  the FILE * for the output
1054  * @node:  the node
1055  * @depth:  the indentation level.
1056  *
1057  * Dumps debug information for the element node, it is recursive
1058  */
1059 static void
1060 xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1061 {
1062     if (node == NULL) {
1063         if (!ctxt->check) {
1064             xmlCtxtDumpSpaces(ctxt);
1065             fprintf(ctxt->output, "node is NULL\n");
1066         }
1067         return;
1068     }
1069     xmlCtxtDumpOneNode(ctxt, node);
1070     if ((node->type != XML_NAMESPACE_DECL) && 
1071         (node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
1072         ctxt->depth++;
1073         xmlCtxtDumpNodeList(ctxt, node->children);
1074         ctxt->depth--;
1075     }
1076 }
1077
1078 /**
1079  * xmlCtxtDumpNodeList:
1080  * @output:  the FILE * for the output
1081  * @node:  the node list
1082  * @depth:  the indentation level.
1083  *
1084  * Dumps debug information for the list of element node, it is recursive
1085  */
1086 static void
1087 xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1088 {
1089     while (node != NULL) {
1090         xmlCtxtDumpNode(ctxt, node);
1091         node = node->next;
1092     }
1093 }
1094
1095 static void
1096 xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1097 {
1098     if (doc == NULL) {
1099         if (!ctxt->check)
1100             fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1101         return;
1102     }
1103     ctxt->node = (xmlNodePtr) doc;
1104
1105     switch (doc->type) {
1106         case XML_ELEMENT_NODE:
1107             xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
1108                         "Misplaced ELEMENT node\n");
1109             break;
1110         case XML_ATTRIBUTE_NODE:
1111             xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
1112                         "Misplaced ATTRIBUTE node\n");
1113             break;
1114         case XML_TEXT_NODE:
1115             xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
1116                         "Misplaced TEXT node\n");
1117             break;
1118         case XML_CDATA_SECTION_NODE:
1119             xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
1120                         "Misplaced CDATA node\n");
1121             break;
1122         case XML_ENTITY_REF_NODE:
1123             xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
1124                         "Misplaced ENTITYREF node\n");
1125             break;
1126         case XML_ENTITY_NODE:
1127             xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
1128                         "Misplaced ENTITY node\n");
1129             break;
1130         case XML_PI_NODE:
1131             xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
1132                         "Misplaced PI node\n");
1133             break;
1134         case XML_COMMENT_NODE:
1135             xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
1136                         "Misplaced COMMENT node\n");
1137             break;
1138         case XML_DOCUMENT_NODE:
1139             if (!ctxt->check)
1140                 fprintf(ctxt->output, "DOCUMENT\n");
1141             break;
1142         case XML_HTML_DOCUMENT_NODE:
1143             if (!ctxt->check)
1144                 fprintf(ctxt->output, "HTML DOCUMENT\n");
1145             break;
1146         case XML_DOCUMENT_TYPE_NODE:
1147             xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
1148                         "Misplaced DOCTYPE node\n");
1149             break;
1150         case XML_DOCUMENT_FRAG_NODE:
1151             xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
1152                         "Misplaced FRAGMENT node\n");
1153             break;
1154         case XML_NOTATION_NODE:
1155             xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
1156                         "Misplaced NOTATION node\n");
1157             break;
1158         default:
1159             xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1160                         "Unknown node type %d\n", doc->type);
1161     }
1162 }
1163
1164 /**
1165  * xmlCtxtDumpDocumentHead:
1166  * @output:  the FILE * for the output
1167  * @doc:  the document
1168  *
1169  * Dumps debug information cncerning the document, not recursive
1170  */
1171 static void
1172 xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1173 {
1174     if (doc == NULL) return;
1175     xmlCtxtDumpDocHead(ctxt, doc);
1176     if (!ctxt->check) {
1177         if (doc->name != NULL) {
1178             fprintf(ctxt->output, "name=");
1179             xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
1180             fprintf(ctxt->output, "\n");
1181         }
1182         if (doc->version != NULL) {
1183             fprintf(ctxt->output, "version=");
1184             xmlCtxtDumpString(ctxt, doc->version);
1185             fprintf(ctxt->output, "\n");
1186         }
1187         if (doc->encoding != NULL) {
1188             fprintf(ctxt->output, "encoding=");
1189             xmlCtxtDumpString(ctxt, doc->encoding);
1190             fprintf(ctxt->output, "\n");
1191         }
1192         if (doc->URL != NULL) {
1193             fprintf(ctxt->output, "URL=");
1194             xmlCtxtDumpString(ctxt, doc->URL);
1195             fprintf(ctxt->output, "\n");
1196         }
1197         if (doc->standalone)
1198             fprintf(ctxt->output, "standalone=true\n");
1199     }
1200     if (doc->oldNs != NULL)
1201         xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
1202 }
1203
1204 /**
1205  * xmlCtxtDumpDocument:
1206  * @output:  the FILE * for the output
1207  * @doc:  the document
1208  *
1209  * Dumps debug information for the document, it's recursive
1210  */
1211 static void
1212 xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1213 {
1214     if (doc == NULL) {
1215         if (!ctxt->check)
1216             fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1217         return;
1218     }
1219     xmlCtxtDumpDocumentHead(ctxt, doc);
1220     if (((doc->type == XML_DOCUMENT_NODE) ||
1221          (doc->type == XML_HTML_DOCUMENT_NODE))
1222         && (doc->children != NULL)) {
1223         ctxt->depth++;
1224         xmlCtxtDumpNodeList(ctxt, doc->children);
1225         ctxt->depth--;
1226     }
1227 }
1228
1229 static void
1230 xmlCtxtDumpEntityCallback(xmlEntityPtr cur, xmlDebugCtxtPtr ctxt)
1231 {
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
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, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1291                     ctxt);
1292     } else
1293         fprintf(ctxt->output, "No entities in internal subset\n");
1294     if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
1295         xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1296             doc->extSubset->entities;
1297
1298         if (!ctxt->check)
1299             fprintf(ctxt->output, "Entities in external subset\n");
1300         xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1301                     ctxt);
1302     } else if (!ctxt->check)
1303         fprintf(ctxt->output, "No entities in external subset\n");
1304 }
1305
1306 /**
1307  * xmlCtxtDumpDTD:
1308  * @output:  the FILE * for the output
1309  * @dtd:  the DTD
1310  *
1311  * Dumps debug information for the DTD
1312  */
1313 static void
1314 xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
1315 {
1316     if (dtd == NULL) {
1317         if (!ctxt->check)
1318             fprintf(ctxt->output, "DTD is NULL\n");
1319         return;
1320     }
1321     xmlCtxtDumpDtdNode(ctxt, dtd);
1322     if (dtd->children == NULL)
1323         fprintf(ctxt->output, "    DTD is empty\n");
1324     else {
1325         ctxt->depth++;
1326         xmlCtxtDumpNodeList(ctxt, dtd->children);
1327         ctxt->depth--;
1328     }
1329 }
1330
1331 /************************************************************************
1332  *                                                                      *
1333  *                      Public entry points for dump                    *
1334  *                                                                      *
1335  ************************************************************************/
1336
1337 /**
1338  * xmlDebugDumpString:
1339  * @output:  the FILE * for the output
1340  * @str:  the string
1341  *
1342  * Dumps informations about the string, shorten it if necessary
1343  */
1344 void
1345 xmlDebugDumpString(FILE * output, const xmlChar * str)
1346 {
1347     int i;
1348
1349     if (output == NULL)
1350         output = stdout;
1351     if (str == NULL) {
1352         fprintf(output, "(NULL)");
1353         return;
1354     }
1355     for (i = 0; i < 40; i++)
1356         if (str[i] == 0)
1357             return;
1358         else if (IS_BLANK_CH(str[i]))
1359             fputc(' ', output);
1360         else if (str[i] >= 0x80)
1361             fprintf(output, "#%X", str[i]);
1362         else
1363             fputc(str[i], output);
1364     fprintf(output, "...");
1365 }
1366
1367 /**
1368  * xmlDebugDumpAttr:
1369  * @output:  the FILE * for the output
1370  * @attr:  the attribute
1371  * @depth:  the indentation level.
1372  *
1373  * Dumps debug information for the attribute
1374  */
1375 void
1376 xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
1377     xmlDebugCtxt ctxt;
1378
1379     if (output == NULL) return;
1380     xmlCtxtDumpInitCtxt(&ctxt);
1381     ctxt.output = output;
1382     ctxt.depth = depth;
1383     xmlCtxtDumpAttr(&ctxt, attr);
1384     xmlCtxtDumpCleanCtxt(&ctxt);
1385 }
1386
1387
1388 /**
1389  * xmlDebugDumpEntities:
1390  * @output:  the FILE * for the output
1391  * @doc:  the document
1392  *
1393  * Dumps debug information for all the entities in use by the document
1394  */
1395 void
1396 xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
1397 {
1398     xmlDebugCtxt ctxt;
1399
1400     if (output == NULL) return;
1401     xmlCtxtDumpInitCtxt(&ctxt);
1402     ctxt.output = output;
1403     xmlCtxtDumpEntities(&ctxt, doc);
1404     xmlCtxtDumpCleanCtxt(&ctxt);
1405 }
1406
1407 /**
1408  * xmlDebugDumpAttrList:
1409  * @output:  the FILE * for the output
1410  * @attr:  the attribute list
1411  * @depth:  the indentation level.
1412  *
1413  * Dumps debug information for the attribute list
1414  */
1415 void
1416 xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
1417 {
1418     xmlDebugCtxt ctxt;
1419
1420     if (output == NULL) return;
1421     xmlCtxtDumpInitCtxt(&ctxt);
1422     ctxt.output = output;
1423     ctxt.depth = depth;
1424     xmlCtxtDumpAttrList(&ctxt, attr);
1425     xmlCtxtDumpCleanCtxt(&ctxt);
1426 }
1427
1428 /**
1429  * xmlDebugDumpOneNode:
1430  * @output:  the FILE * for the output
1431  * @node:  the node
1432  * @depth:  the indentation level.
1433  *
1434  * Dumps debug information for the element node, it is not recursive
1435  */
1436 void
1437 xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
1438 {
1439     xmlDebugCtxt ctxt;
1440
1441     if (output == NULL) return;
1442     xmlCtxtDumpInitCtxt(&ctxt);
1443     ctxt.output = output;
1444     ctxt.depth = depth;
1445     xmlCtxtDumpOneNode(&ctxt, node);
1446     xmlCtxtDumpCleanCtxt(&ctxt);
1447 }
1448
1449 /**
1450  * xmlDebugDumpNode:
1451  * @output:  the FILE * for the output
1452  * @node:  the node
1453  * @depth:  the indentation level.
1454  *
1455  * Dumps debug information for the element node, it is recursive
1456  */
1457 void
1458 xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
1459 {
1460     xmlDebugCtxt ctxt;
1461
1462     if (output == NULL)
1463         output = stdout;
1464     xmlCtxtDumpInitCtxt(&ctxt);
1465     ctxt.output = output;
1466     ctxt.depth = depth;
1467     xmlCtxtDumpNode(&ctxt, node);
1468     xmlCtxtDumpCleanCtxt(&ctxt);
1469 }
1470
1471 /**
1472  * xmlDebugDumpNodeList:
1473  * @output:  the FILE * for the output
1474  * @node:  the node list
1475  * @depth:  the indentation level.
1476  *
1477  * Dumps debug information for the list of element node, it is recursive
1478  */
1479 void
1480 xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
1481 {
1482     xmlDebugCtxt ctxt;
1483
1484     if (output == NULL)
1485         output = stdout;
1486     xmlCtxtDumpInitCtxt(&ctxt);
1487     ctxt.output = output;
1488     ctxt.depth = depth;
1489     xmlCtxtDumpNodeList(&ctxt, node);
1490     xmlCtxtDumpCleanCtxt(&ctxt);
1491 }
1492
1493 /**
1494  * xmlDebugDumpDocumentHead:
1495  * @output:  the FILE * for the output
1496  * @doc:  the document
1497  *
1498  * Dumps debug information cncerning the document, not recursive
1499  */
1500 void
1501 xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
1502 {
1503     xmlDebugCtxt ctxt;
1504
1505     if (output == NULL)
1506         output = stdout;
1507     xmlCtxtDumpInitCtxt(&ctxt);
1508     ctxt.options |= DUMP_TEXT_TYPE;
1509     ctxt.output = output;
1510     xmlCtxtDumpDocumentHead(&ctxt, doc);
1511     xmlCtxtDumpCleanCtxt(&ctxt);
1512 }
1513
1514 /**
1515  * xmlDebugDumpDocument:
1516  * @output:  the FILE * for the output
1517  * @doc:  the document
1518  *
1519  * Dumps debug information for the document, it's recursive
1520  */
1521 void
1522 xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
1523 {
1524     xmlDebugCtxt ctxt;
1525
1526     if (output == NULL)
1527         output = stdout;
1528     xmlCtxtDumpInitCtxt(&ctxt);
1529     ctxt.options |= DUMP_TEXT_TYPE;
1530     ctxt.output = output;
1531     xmlCtxtDumpDocument(&ctxt, doc);
1532     xmlCtxtDumpCleanCtxt(&ctxt);
1533 }
1534
1535 /**
1536  * xmlDebugDumpDTD:
1537  * @output:  the FILE * for the output
1538  * @dtd:  the DTD
1539  *
1540  * Dumps debug information for the DTD
1541  */
1542 void
1543 xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
1544 {
1545     xmlDebugCtxt ctxt;
1546
1547     if (output == NULL)
1548         output = stdout;
1549     xmlCtxtDumpInitCtxt(&ctxt);
1550     ctxt.options |= DUMP_TEXT_TYPE;
1551     ctxt.output = output;
1552     xmlCtxtDumpDTD(&ctxt, dtd);
1553     xmlCtxtDumpCleanCtxt(&ctxt);
1554 }
1555
1556 /************************************************************************
1557  *                                                                      *
1558  *                      Public entry points for checkings               *
1559  *                                                                      *
1560  ************************************************************************/
1561
1562 /**
1563  * xmlDebugCheckDocument:
1564  * @output:  the FILE * for the output
1565  * @doc:  the document
1566  *
1567  * Check the document for potential content problems, and output
1568  * the errors to @output
1569  *
1570  * Returns the number of errors found
1571  */
1572 int
1573 xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
1574 {
1575     xmlDebugCtxt ctxt;
1576
1577     if (output == NULL)
1578         output = stdout;
1579     xmlCtxtDumpInitCtxt(&ctxt);
1580     ctxt.output = output;
1581     ctxt.check = 1;
1582     xmlCtxtDumpDocument(&ctxt, doc);
1583     xmlCtxtDumpCleanCtxt(&ctxt);
1584     return(ctxt.errors);
1585 }
1586
1587 /************************************************************************
1588  *                                                                      *
1589  *                      Helpers for Shell                               *
1590  *                                                                      *
1591  ************************************************************************/
1592
1593 /**
1594  * xmlLsCountNode:
1595  * @node:  the node to count
1596  *
1597  * Count the children of @node.
1598  *
1599  * Returns the number of children of @node.
1600  */
1601 int
1602 xmlLsCountNode(xmlNodePtr node) {
1603     int ret = 0;
1604     xmlNodePtr list = NULL;
1605     
1606     if (node == NULL)
1607         return(0);
1608
1609     switch (node->type) {
1610         case XML_ELEMENT_NODE:
1611             list = node->children;
1612             break;
1613         case XML_DOCUMENT_NODE:
1614         case XML_HTML_DOCUMENT_NODE:
1615 #ifdef LIBXML_DOCB_ENABLED
1616         case XML_DOCB_DOCUMENT_NODE:
1617 #endif
1618             list = ((xmlDocPtr) node)->children;
1619             break;
1620         case XML_ATTRIBUTE_NODE:
1621             list = ((xmlAttrPtr) node)->children;
1622             break;
1623         case XML_TEXT_NODE:
1624         case XML_CDATA_SECTION_NODE:
1625         case XML_PI_NODE:
1626         case XML_COMMENT_NODE:
1627             if (node->content != NULL) {
1628                 ret = xmlStrlen(node->content);
1629             }
1630             break;
1631         case XML_ENTITY_REF_NODE:
1632         case XML_DOCUMENT_TYPE_NODE:
1633         case XML_ENTITY_NODE:
1634         case XML_DOCUMENT_FRAG_NODE:
1635         case XML_NOTATION_NODE:
1636         case XML_DTD_NODE:
1637         case XML_ELEMENT_DECL:
1638         case XML_ATTRIBUTE_DECL:
1639         case XML_ENTITY_DECL:
1640         case XML_NAMESPACE_DECL:
1641         case XML_XINCLUDE_START:
1642         case XML_XINCLUDE_END:
1643             ret = 1;
1644             break;
1645     }
1646     for (;list != NULL;ret++) 
1647         list = list->next;
1648     return(ret);
1649 }
1650
1651 /**
1652  * xmlLsOneNode:
1653  * @output:  the FILE * for the output
1654  * @node:  the node to dump
1655  *
1656  * Dump to @output the type and name of @node.
1657  */
1658 void
1659 xmlLsOneNode(FILE *output, xmlNodePtr node) {
1660     if (output == NULL) return;
1661     if (node == NULL) {
1662         fprintf(output, "NULL\n");
1663         return;
1664     }
1665     switch (node->type) {
1666         case XML_ELEMENT_NODE:
1667             fprintf(output, "-");
1668             break;
1669         case XML_ATTRIBUTE_NODE:
1670             fprintf(output, "a");
1671             break;
1672         case XML_TEXT_NODE:
1673             fprintf(output, "t");
1674             break;
1675         case XML_CDATA_SECTION_NODE:
1676             fprintf(output, "C");
1677             break;
1678         case XML_ENTITY_REF_NODE:
1679             fprintf(output, "e");
1680             break;
1681         case XML_ENTITY_NODE:
1682             fprintf(output, "E");
1683             break;
1684         case XML_PI_NODE:
1685             fprintf(output, "p");
1686             break;
1687         case XML_COMMENT_NODE:
1688             fprintf(output, "c");
1689             break;
1690         case XML_DOCUMENT_NODE:
1691             fprintf(output, "d");
1692             break;
1693         case XML_HTML_DOCUMENT_NODE:
1694             fprintf(output, "h");
1695             break;
1696         case XML_DOCUMENT_TYPE_NODE:
1697             fprintf(output, "T");
1698             break;
1699         case XML_DOCUMENT_FRAG_NODE:
1700             fprintf(output, "F");
1701             break;
1702         case XML_NOTATION_NODE:
1703             fprintf(output, "N");
1704             break;
1705         case XML_NAMESPACE_DECL:
1706             fprintf(output, "n");
1707             break;
1708         default:
1709             fprintf(output, "?");
1710     }
1711     if (node->type != XML_NAMESPACE_DECL) {
1712         if (node->properties != NULL)
1713             fprintf(output, "a");
1714         else    
1715             fprintf(output, "-");
1716         if (node->nsDef != NULL) 
1717             fprintf(output, "n");
1718         else    
1719             fprintf(output, "-");
1720     }
1721
1722     fprintf(output, " %8d ", xmlLsCountNode(node));
1723
1724     switch (node->type) {
1725         case XML_ELEMENT_NODE:
1726             if (node->name != NULL)
1727                 fprintf(output, "%s", (const char *) node->name);
1728             break;
1729         case XML_ATTRIBUTE_NODE:
1730             if (node->name != NULL)
1731                 fprintf(output, "%s", (const char *) node->name);
1732             break;
1733         case XML_TEXT_NODE:
1734             if (node->content != NULL) {
1735                 xmlDebugDumpString(output, node->content);
1736             }
1737             break;
1738         case XML_CDATA_SECTION_NODE:
1739             break;
1740         case XML_ENTITY_REF_NODE:
1741             if (node->name != NULL)
1742                 fprintf(output, "%s", (const char *) node->name);
1743             break;
1744         case XML_ENTITY_NODE:
1745             if (node->name != NULL)
1746                 fprintf(output, "%s", (const char *) node->name);
1747             break;
1748         case XML_PI_NODE:
1749             if (node->name != NULL)
1750                 fprintf(output, "%s", (const char *) node->name);
1751             break;
1752         case XML_COMMENT_NODE:
1753             break;
1754         case XML_DOCUMENT_NODE:
1755             break;
1756         case XML_HTML_DOCUMENT_NODE:
1757             break;
1758         case XML_DOCUMENT_TYPE_NODE:
1759             break;
1760         case XML_DOCUMENT_FRAG_NODE:
1761             break;
1762         case XML_NOTATION_NODE:
1763             break;
1764         case XML_NAMESPACE_DECL: {
1765             xmlNsPtr ns = (xmlNsPtr) node;
1766
1767             if (ns->prefix == NULL)
1768                 fprintf(output, "default -> %s", (char *)ns->href);
1769             else
1770                 fprintf(output, "%s -> %s", (char *)ns->prefix,
1771                         (char *)ns->href);
1772             break;
1773         }
1774         default:
1775             if (node->name != NULL)
1776                 fprintf(output, "%s", (const char *) node->name);
1777     }
1778     fprintf(output, "\n");
1779 }
1780
1781 /**
1782  * xmlBoolToText:
1783  * @boolval: a bool to turn into text
1784  *
1785  * Convenient way to turn bool into text 
1786  *
1787  * Returns a pointer to either "True" or "False"
1788  */
1789 const char *
1790 xmlBoolToText(int boolval)
1791 {
1792     if (boolval)
1793         return("True");
1794     else
1795         return("False");
1796 }
1797
1798 #ifdef LIBXML_XPATH_ENABLED
1799 /****************************************************************
1800  *                                                              *
1801  *              The XML shell related functions                 *
1802  *                                                              *
1803  ****************************************************************/
1804
1805
1806
1807 /*
1808  * TODO: Improvement/cleanups for the XML shell
1809  *     - allow to shell out an editor on a subpart
1810  *     - cleanup function registrations (with help) and calling
1811  *     - provide registration routines
1812  */
1813
1814 /**
1815  * xmlShellPrintXPathError:
1816  * @errorType: valid xpath error id
1817  * @arg: the argument that cause xpath to fail
1818  *
1819  * Print the xpath error to libxml default error channel
1820  */
1821 void
1822 xmlShellPrintXPathError(int errorType, const char *arg)
1823 {
1824     const char *default_arg = "Result";
1825
1826     if (!arg)
1827         arg = default_arg;
1828
1829     switch (errorType) {
1830         case XPATH_UNDEFINED:
1831             xmlGenericError(xmlGenericErrorContext,
1832                             "%s: no such node\n", arg);
1833             break;
1834
1835         case XPATH_BOOLEAN:
1836             xmlGenericError(xmlGenericErrorContext,
1837                             "%s is a Boolean\n", arg);
1838             break;
1839         case XPATH_NUMBER:
1840             xmlGenericError(xmlGenericErrorContext,
1841                             "%s is a number\n", arg);
1842             break;
1843         case XPATH_STRING:
1844             xmlGenericError(xmlGenericErrorContext,
1845                             "%s is a string\n", arg);
1846             break;
1847         case XPATH_POINT:
1848             xmlGenericError(xmlGenericErrorContext,
1849                             "%s is a point\n", arg);
1850             break;
1851         case XPATH_RANGE:
1852             xmlGenericError(xmlGenericErrorContext,
1853                             "%s is a range\n", arg);
1854             break;
1855         case XPATH_LOCATIONSET:
1856             xmlGenericError(xmlGenericErrorContext,
1857                             "%s is a range\n", arg);
1858             break;
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
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
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
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
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
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
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
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
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
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 informations about the node (namespace, attributes, content).
2188  *
2189  * Returns 0
2190  */
2191 static int
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 informations about the node (namespace, attributes, content).
2266  *
2267  * Returns 0
2268  */
2269 int
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 informations about the node (namespace, attributes, content).
2300  *
2301  * Returns 0
2302  */
2303 static int
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
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,
2361             (xmlRelaxNGValidityErrorFunc) fprintf,
2362             (xmlRelaxNGValidityWarningFunc) fprintf,
2363             stderr);
2364     relaxngschemas = xmlRelaxNGParse(ctxt);
2365     xmlRelaxNGFreeParserCtxt(ctxt);
2366     if (relaxngschemas == NULL) {
2367         xmlGenericError(xmlGenericErrorContext,
2368                 "Relax-NG schema %s failed to compile\n", schemas);
2369         return(-1);
2370     }
2371     vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2372     xmlRelaxNGSetValidErrors(vctxt,
2373             (xmlRelaxNGValidityErrorFunc) fprintf,
2374             (xmlRelaxNGValidityWarningFunc) fprintf,
2375             stderr);
2376     ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2377     if (ret == 0) {
2378         fprintf(stderr, "%s validates\n", sctxt->filename);
2379     } else if (ret > 0) {
2380         fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2381     } else {
2382         fprintf(stderr, "%s validation generated an internal error\n",
2383                sctxt->filename);
2384     }
2385     xmlRelaxNGFreeValidCtxt(vctxt);
2386     if (relaxngschemas != NULL)
2387         xmlRelaxNGFree(relaxngschemas);
2388     return(0);
2389 }
2390 #endif
2391
2392 #ifdef LIBXML_OUTPUT_ENABLED
2393 /**
2394  * xmlShellCat:
2395  * @ctxt:  the shell context
2396  * @arg:  unused
2397  * @node:  a node
2398  * @node2:  unused
2399  *
2400  * Implements the XML shell function "cat"
2401  * dumps the serialization node content (XML or HTML).
2402  *
2403  * Returns 0
2404  */
2405 int
2406 xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2407             xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2408 {
2409     if (!ctxt)
2410         return (0);
2411     if (node == NULL) {
2412         fprintf(ctxt->output, "NULL\n");
2413         return (0);
2414     }    
2415     if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2416 #ifdef LIBXML_HTML_ENABLED
2417         if (node->type == XML_HTML_DOCUMENT_NODE)
2418             htmlDocDump(ctxt->output, (htmlDocPtr) node);
2419         else
2420             htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
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 #endif /* LIBXML_HTML_ENABLED */
2427     } else {
2428         if (node->type == XML_DOCUMENT_NODE)
2429             xmlDocDump(ctxt->output, (xmlDocPtr) node);
2430         else
2431             xmlElemDump(ctxt->output, ctxt->doc, node);
2432     }
2433     fprintf(ctxt->output, "\n");
2434     return (0);
2435 }
2436 #endif /* LIBXML_OUTPUT_ENABLED */
2437
2438 /**
2439  * xmlShellLoad:
2440  * @ctxt:  the shell context
2441  * @filename:  the file name
2442  * @node:  unused
2443  * @node2:  unused
2444  *
2445  * Implements the XML shell function "load"
2446  * loads a new document specified by the filename
2447  *
2448  * Returns 0 or -1 if loading failed
2449  */
2450 int
2451 xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2452              xmlNodePtr node ATTRIBUTE_UNUSED,
2453              xmlNodePtr node2 ATTRIBUTE_UNUSED)
2454 {
2455     xmlDocPtr doc;
2456     int html = 0;
2457
2458     if ((ctxt == NULL) || (filename == NULL)) return(-1);
2459     if (ctxt->doc != NULL)
2460         html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
2461
2462     if (html) {
2463 #ifdef LIBXML_HTML_ENABLED
2464         doc = htmlParseFile(filename, NULL);
2465 #else
2466         fprintf(ctxt->output, "HTML support not compiled in\n");
2467         doc = NULL;
2468 #endif /* LIBXML_HTML_ENABLED */
2469     } else {
2470         doc = xmlReadFile(filename,NULL,0);
2471     }
2472     if (doc != NULL) {
2473         if (ctxt->loaded == 1) {
2474             xmlFreeDoc(ctxt->doc);
2475         }
2476         ctxt->loaded = 1;
2477 #ifdef LIBXML_XPATH_ENABLED
2478         xmlXPathFreeContext(ctxt->pctxt);
2479 #endif /* LIBXML_XPATH_ENABLED */
2480         xmlFree(ctxt->filename);
2481         ctxt->doc = doc;
2482         ctxt->node = (xmlNodePtr) doc;
2483 #ifdef LIBXML_XPATH_ENABLED
2484         ctxt->pctxt = xmlXPathNewContext(doc);
2485 #endif /* LIBXML_XPATH_ENABLED */
2486         ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
2487     } else
2488         return (-1);
2489     return (0);
2490 }
2491
2492 #ifdef LIBXML_OUTPUT_ENABLED
2493 /**
2494  * xmlShellWrite:
2495  * @ctxt:  the shell context
2496  * @filename:  the file name
2497  * @node:  a node in the tree
2498  * @node2:  unused
2499  *
2500  * Implements the XML shell function "write"
2501  * Write the current node to the filename, it saves the serialization
2502  * of the subtree under the @node specified
2503  *
2504  * Returns 0 or -1 in case of error
2505  */
2506 int
2507 xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
2508               xmlNodePtr node2 ATTRIBUTE_UNUSED)
2509 {
2510     if (node == NULL)
2511         return (-1);
2512     if ((filename == NULL) || (filename[0] == 0)) {
2513         return (-1);
2514     }
2515 #ifdef W_OK
2516     if (access((char *) filename, W_OK)) {
2517         xmlGenericError(xmlGenericErrorContext,
2518                         "Cannot write to %s\n", filename);
2519         return (-1);
2520     }
2521 #endif
2522     switch (node->type) {
2523         case XML_DOCUMENT_NODE:
2524             if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2525                 xmlGenericError(xmlGenericErrorContext,
2526                                 "Failed to write to %s\n", filename);
2527                 return (-1);
2528             }
2529             break;
2530         case XML_HTML_DOCUMENT_NODE:
2531 #ifdef LIBXML_HTML_ENABLED
2532             if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2533                 xmlGenericError(xmlGenericErrorContext,
2534                                 "Failed to write to %s\n", filename);
2535                 return (-1);
2536             }
2537 #else
2538             if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2539                 xmlGenericError(xmlGenericErrorContext,
2540                                 "Failed to write to %s\n", filename);
2541                 return (-1);
2542             }
2543 #endif /* LIBXML_HTML_ENABLED */
2544             break;
2545         default:{
2546                 FILE *f;
2547
2548                 f = fopen((char *) filename, "w");
2549                 if (f == NULL) {
2550                     xmlGenericError(xmlGenericErrorContext,
2551                                     "Failed to write to %s\n", filename);
2552                     return (-1);
2553                 }
2554                 xmlElemDump(f, ctxt->doc, node);
2555                 fclose(f);
2556             }
2557     }
2558     return (0);
2559 }
2560
2561 /**
2562  * xmlShellSave:
2563  * @ctxt:  the shell context
2564  * @filename:  the file name (optional)
2565  * @node:  unused
2566  * @node2:  unused
2567  *
2568  * Implements the XML shell function "save"
2569  * Write the current document to the filename, or it's original name
2570  *
2571  * Returns 0 or -1 in case of error
2572  */
2573 int
2574 xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2575              xmlNodePtr node ATTRIBUTE_UNUSED,
2576              xmlNodePtr node2 ATTRIBUTE_UNUSED)
2577 {
2578     if ((ctxt == NULL) || (ctxt->doc == NULL))
2579         return (-1);
2580     if ((filename == NULL) || (filename[0] == 0))
2581         filename = ctxt->filename;
2582     if (filename == NULL)
2583         return (-1);
2584 #ifdef W_OK
2585     if (access((char *) filename, W_OK)) {
2586         xmlGenericError(xmlGenericErrorContext,
2587                         "Cannot save to %s\n", filename);
2588         return (-1);
2589     }
2590 #endif
2591     switch (ctxt->doc->type) {
2592         case XML_DOCUMENT_NODE:
2593             if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2594                 xmlGenericError(xmlGenericErrorContext,
2595                                 "Failed to save to %s\n", filename);
2596             }
2597             break;
2598         case XML_HTML_DOCUMENT_NODE:
2599 #ifdef LIBXML_HTML_ENABLED
2600             if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2601                 xmlGenericError(xmlGenericErrorContext,
2602                                 "Failed to save to %s\n", filename);
2603             }
2604 #else
2605             if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2606                 xmlGenericError(xmlGenericErrorContext,
2607                                 "Failed to save to %s\n", filename);
2608             }
2609 #endif /* LIBXML_HTML_ENABLED */
2610             break;
2611         default:
2612             xmlGenericError(xmlGenericErrorContext,
2613             "To save to subparts of a document use the 'write' command\n");
2614             return (-1);
2615
2616     }
2617     return (0);
2618 }
2619 #endif /* LIBXML_OUTPUT_ENABLED */
2620
2621 #ifdef LIBXML_VALID_ENABLED
2622 /**
2623  * xmlShellValidate:
2624  * @ctxt:  the shell context
2625  * @dtd:  the DTD URI (optional)
2626  * @node:  unused
2627  * @node2:  unused
2628  *
2629  * Implements the XML shell function "validate"
2630  * Validate the document, if a DTD path is provided, then the validation
2631  * is done against the given DTD.
2632  *
2633  * Returns 0 or -1 in case of error
2634  */
2635 int
2636 xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2637                  xmlNodePtr node ATTRIBUTE_UNUSED,
2638                  xmlNodePtr node2 ATTRIBUTE_UNUSED)
2639 {
2640     xmlValidCtxt vctxt;
2641     int res = -1;
2642
2643     if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
2644     vctxt.userData = stderr;
2645     vctxt.error = (xmlValidityErrorFunc) fprintf;
2646     vctxt.warning = (xmlValidityWarningFunc) fprintf;
2647
2648     if ((dtd == NULL) || (dtd[0] == 0)) {
2649         res = xmlValidateDocument(&vctxt, ctxt->doc);
2650     } else {
2651         xmlDtdPtr subset;
2652
2653         subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2654         if (subset != NULL) {
2655             res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2656
2657             xmlFreeDtd(subset);
2658         }
2659     }
2660     return (res);
2661 }
2662 #endif /* LIBXML_VALID_ENABLED */
2663
2664 /**
2665  * xmlShellDu:
2666  * @ctxt:  the shell context
2667  * @arg:  unused
2668  * @tree:  a node defining a subtree
2669  * @node2:  unused
2670  *
2671  * Implements the XML shell function "du"
2672  * show the structure of the subtree under node @tree
2673  * If @tree is null, the command works on the current node.
2674  *
2675  * Returns 0 or -1 in case of error
2676  */
2677 int
2678 xmlShellDu(xmlShellCtxtPtr ctxt,
2679            char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2680            xmlNodePtr node2 ATTRIBUTE_UNUSED)
2681 {
2682     xmlNodePtr node;
2683     int indent = 0, i;
2684
2685     if (!ctxt)
2686         return (-1);
2687
2688     if (tree == NULL)
2689         return (-1);
2690     node = tree;
2691     while (node != NULL) {
2692         if ((node->type == XML_DOCUMENT_NODE) ||
2693             (node->type == XML_HTML_DOCUMENT_NODE)) {
2694             fprintf(ctxt->output, "/\n");
2695         } else if (node->type == XML_ELEMENT_NODE) {
2696             for (i = 0; i < indent; i++)
2697                 fprintf(ctxt->output, "  ");
2698             fprintf(ctxt->output, "%s\n", node->name);
2699         } else {
2700         }
2701
2702         /*
2703          * Browse the full subtree, deep first
2704          */
2705
2706         if ((node->type == XML_DOCUMENT_NODE) ||
2707             (node->type == XML_HTML_DOCUMENT_NODE)) {
2708             node = ((xmlDocPtr) node)->children;
2709         } else if ((node->children != NULL)
2710                    && (node->type != XML_ENTITY_REF_NODE)) {
2711             /* deep first */
2712             node = node->children;
2713             indent++;
2714         } else if ((node != tree) && (node->next != NULL)) {
2715             /* then siblings */
2716             node = node->next;
2717         } else if (node != tree) {
2718             /* go up to parents->next if needed */
2719             while (node != tree) {
2720                 if (node->parent != NULL) {
2721                     node = node->parent;
2722                     indent--;
2723                 }
2724                 if ((node != tree) && (node->next != NULL)) {
2725                     node = node->next;
2726                     break;
2727                 }
2728                 if (node->parent == NULL) {
2729                     node = NULL;
2730                     break;
2731                 }
2732                 if (node == tree) {
2733                     node = NULL;
2734                     break;
2735                 }
2736             }
2737             /* exit condition */
2738             if (node == tree)
2739                 node = NULL;
2740         } else
2741             node = NULL;
2742     }
2743     return (0);
2744 }
2745
2746 /**
2747  * xmlShellPwd:
2748  * @ctxt:  the shell context
2749  * @buffer:  the output buffer
2750  * @node:  a node 
2751  * @node2:  unused
2752  *
2753  * Implements the XML shell function "pwd"
2754  * Show the full path from the root to the node, if needed building
2755  * thumblers when similar elements exists at a given ancestor level.
2756  * The output is compatible with XPath commands.
2757  *
2758  * Returns 0 or -1 in case of error
2759  */
2760 int
2761 xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2762             xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2763 {
2764     xmlChar *path;
2765
2766     if ((node == NULL) || (buffer == NULL))
2767         return (-1);
2768
2769     path = xmlGetNodePath(node);
2770     if (path == NULL)
2771         return (-1);
2772
2773     /*
2774      * This test prevents buffer overflow, because this routine
2775      * is only called by xmlShell, in which the second argument is
2776      * 500 chars long.
2777      * It is a dirty hack before a cleaner solution is found.
2778      * Documentation should mention that the second argument must
2779      * be at least 500 chars long, and could be stripped if too long.
2780      */
2781     snprintf(buffer, 499, "%s", path);
2782     buffer[499] = '0';
2783     xmlFree(path);
2784
2785     return (0);
2786 }
2787
2788 /**
2789  * xmlShell:
2790  * @doc:  the initial document
2791  * @filename:  the output buffer
2792  * @input:  the line reading function
2793  * @output:  the output FILE*, defaults to stdout if NULL
2794  *
2795  * Implements the XML shell 
2796  * This allow to load, validate, view, modify and save a document
2797  * using a environment similar to a UNIX commandline.
2798  */
2799 void
2800 xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
2801          FILE * output)
2802 {
2803     char prompt[500] = "/ > ";
2804     char *cmdline = NULL, *cur;
2805     char command[100];
2806     char arg[400];
2807     int i;
2808     xmlShellCtxtPtr ctxt;
2809     xmlXPathObjectPtr list;
2810
2811     if (doc == NULL)
2812         return;
2813     if (filename == NULL)
2814         return;
2815     if (input == NULL)
2816         return;
2817     if (output == NULL)
2818         output = stdout;
2819     ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
2820     if (ctxt == NULL)
2821         return;
2822     ctxt->loaded = 0;
2823     ctxt->doc = doc;
2824     ctxt->input = input;
2825     ctxt->output = output;
2826     ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
2827     ctxt->node = (xmlNodePtr) ctxt->doc;
2828
2829 #ifdef LIBXML_XPATH_ENABLED
2830     ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2831     if (ctxt->pctxt == NULL) {
2832         xmlFree(ctxt);
2833         return;
2834     }
2835 #endif /* LIBXML_XPATH_ENABLED */
2836     while (1) {
2837         if (ctxt->node == (xmlNodePtr) ctxt->doc)
2838             snprintf(prompt, sizeof(prompt), "%s > ", "/");
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 informations 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, "\tquit         leave shell\n");
2914 #ifdef LIBXML_OUTPUT_ENABLED
2915                   fprintf(ctxt->output, "\tsave [name]  save this document to name or the original name\n");
2916                   fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
2917 #endif /* LIBXML_OUTPUT_ENABLED */
2918 #ifdef LIBXML_VALID_ENABLED
2919                   fprintf(ctxt->output, "\tvalidate     check the document for errors\n");
2920 #endif /* LIBXML_VALID_ENABLED */
2921 #ifdef LIBXML_SCHEMAS_ENABLED
2922                   fprintf(ctxt->output, "\trelaxng rng  validate the document agaisnt the Relax-NG schemas\n");
2923 #endif
2924                   fprintf(ctxt->output, "\tgrep string  search for a string in the subtree\n");
2925 #ifdef LIBXML_VALID_ENABLED
2926         } else if (!strcmp(command, "validate")) {
2927             xmlShellValidate(ctxt, arg, NULL, NULL);
2928 #endif /* LIBXML_VALID_ENABLED */
2929         } else if (!strcmp(command, "load")) {
2930             xmlShellLoad(ctxt, arg, NULL, NULL);
2931 #ifdef LIBXML_SCHEMAS_ENABLED
2932         } else if (!strcmp(command, "relaxng")) {
2933             xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2934 #endif
2935 #ifdef LIBXML_OUTPUT_ENABLED
2936         } else if (!strcmp(command, "save")) {
2937             xmlShellSave(ctxt, arg, NULL, NULL);
2938         } else if (!strcmp(command, "write")) {
2939             if ((arg == NULL) || (arg[0] == 0))
2940                 xmlGenericError(xmlGenericErrorContext,
2941                         "Write command requires a filename argument\n");
2942             else
2943                 xmlShellWrite(ctxt, arg, NULL, NULL);
2944 #endif /* LIBXML_OUTPUT_ENABLED */
2945         } else if (!strcmp(command, "grep")) {
2946             xmlShellGrep(ctxt, arg, ctxt->node, NULL);
2947         } else if (!strcmp(command, "free")) {
2948             if (arg[0] == 0) {
2949                 xmlMemShow(ctxt->output, 0);
2950             } else {
2951                 int len = 0;
2952
2953                 sscanf(arg, "%d", &len);
2954                 xmlMemShow(ctxt->output, len);
2955             }
2956         } else if (!strcmp(command, "pwd")) {
2957             char dir[500];
2958
2959             if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
2960                 fprintf(ctxt->output, "%s\n", dir);
2961         } else if (!strcmp(command, "du")) {
2962             xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2963         } else if (!strcmp(command, "base")) {
2964             xmlShellBase(ctxt, NULL, ctxt->node, NULL);
2965         } else if (!strcmp(command, "set")) {
2966             xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
2967 #ifdef LIBXML_XPATH_ENABLED
2968         } else if (!strcmp(command, "setns")) {
2969             if (arg[0] == 0) {
2970                 xmlGenericError(xmlGenericErrorContext,
2971                                 "setns: prefix=[nsuri] required\n");
2972             } else {
2973                 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
2974             }
2975         } else if (!strcmp(command, "setrootns")) {
2976             xmlNodePtr root;
2977
2978             root = xmlDocGetRootElement(ctxt->doc);
2979             xmlShellRegisterRootNamespaces(ctxt, NULL, root, NULL);
2980         } else if (!strcmp(command, "xpath")) {
2981             if (arg[0] == 0) {
2982                 xmlGenericError(xmlGenericErrorContext,
2983                                 "xpath: expression required\n");
2984             } else {
2985                 ctxt->pctxt->node = ctxt->node;
2986                 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2987                 xmlXPathDebugDumpObject(ctxt->output, list, 0);
2988                 xmlXPathFreeObject(list);
2989             }
2990 #endif /* LIBXML_XPATH_ENABLED */
2991 #ifdef LIBXML_TREE_ENABLED
2992         } else if (!strcmp(command, "setbase")) {
2993             xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
2994 #endif
2995         } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
2996             int dir = (!strcmp(command, "dir"));
2997
2998             if (arg[0] == 0) {
2999                 if (dir)
3000                     xmlShellDir(ctxt, NULL, ctxt->node, NULL);
3001                 else
3002                     xmlShellList(ctxt, NULL, ctxt->node, NULL);
3003             } else {
3004                 ctxt->pctxt->node = ctxt->node;
3005 #ifdef LIBXML_XPATH_ENABLED
3006                 ctxt->pctxt->node = ctxt->node;
3007                 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3008 #else
3009                 list = NULL;
3010 #endif /* LIBXML_XPATH_ENABLED */
3011                 if (list != NULL) {
3012                     switch (list->type) {
3013                         case XPATH_UNDEFINED:
3014                             xmlGenericError(xmlGenericErrorContext,
3015                                             "%s: no such node\n", arg);
3016                             break;
3017                         case XPATH_NODESET:{
3018                                 int indx;
3019
3020                                 if (list->nodesetval == NULL)
3021                                     break;
3022
3023                                 for (indx = 0;
3024                                      indx < list->nodesetval->nodeNr;
3025                                      indx++) {
3026                                     if (dir)
3027                                         xmlShellDir(ctxt, NULL,
3028                                                     list->nodesetval->
3029                                                     nodeTab[indx], NULL);
3030                                     else
3031                                         xmlShellList(ctxt, NULL,
3032                                                      list->nodesetval->
3033                                                      nodeTab[indx], NULL);
3034                                 }
3035                                 break;
3036                             }
3037                         case XPATH_BOOLEAN:
3038                             xmlGenericError(xmlGenericErrorContext,
3039                                             "%s is a Boolean\n", arg);
3040                             break;
3041                         case XPATH_NUMBER:
3042                             xmlGenericError(xmlGenericErrorContext,
3043                                             "%s is a number\n", arg);
3044                             break;
3045                         case XPATH_STRING:
3046                             xmlGenericError(xmlGenericErrorContext,
3047                                             "%s is a string\n", arg);
3048                             break;
3049                         case XPATH_POINT:
3050                             xmlGenericError(xmlGenericErrorContext,
3051                                             "%s is a point\n", arg);
3052                             break;
3053                         case XPATH_RANGE:
3054                             xmlGenericError(xmlGenericErrorContext,
3055                                             "%s is a range\n", arg);
3056                             break;
3057                         case XPATH_LOCATIONSET:
3058                             xmlGenericError(xmlGenericErrorContext,
3059                                             "%s is a range\n", arg);
3060                             break;
3061                         case XPATH_USERS:
3062                             xmlGenericError(xmlGenericErrorContext,
3063                                             "%s is user-defined\n", arg);
3064                             break;
3065                         case XPATH_XSLT_TREE:
3066                             xmlGenericError(xmlGenericErrorContext,
3067                                             "%s is an XSLT value tree\n",
3068                                             arg);
3069                             break;
3070                     }
3071 #ifdef LIBXML_XPATH_ENABLED
3072                     xmlXPathFreeObject(list);
3073 #endif
3074                 } else {
3075                     xmlGenericError(xmlGenericErrorContext,
3076                                     "%s: no such node\n", arg);
3077                 }
3078                 ctxt->pctxt->node = NULL;
3079             }
3080         } else if (!strcmp(command, "cd")) {
3081             if (arg[0] == 0) {
3082                 ctxt->node = (xmlNodePtr) ctxt->doc;
3083             } else {
3084 #ifdef LIBXML_XPATH_ENABLED
3085                 ctxt->pctxt->node = ctxt->node;
3086                 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3087 #else
3088                 list = NULL;
3089 #endif /* LIBXML_XPATH_ENABLED */
3090                 if (list != NULL) {
3091                     switch (list->type) {
3092                         case XPATH_UNDEFINED:
3093                             xmlGenericError(xmlGenericErrorContext,
3094                                             "%s: no such node\n", arg);
3095                             break;
3096                         case XPATH_NODESET:
3097                             if (list->nodesetval != NULL) {
3098                                 if (list->nodesetval->nodeNr == 1) {
3099                                     ctxt->node = list->nodesetval->nodeTab[0];
3100                                     if ((ctxt->node != NULL) &&
3101                                         (ctxt->node->type ==
3102                                          XML_NAMESPACE_DECL)) {
3103                                         xmlGenericError(xmlGenericErrorContext,
3104                                                     "cannot cd to namespace\n");
3105                                         ctxt->node = NULL;
3106                                     }
3107                                 } else
3108                                     xmlGenericError(xmlGenericErrorContext,
3109                                                     "%s is a %d Node Set\n",
3110                                                     arg,
3111                                                     list->nodesetval->nodeNr);
3112                             } else
3113                                 xmlGenericError(xmlGenericErrorContext,
3114                                                 "%s is an empty Node Set\n",
3115                                                 arg);
3116                             break;
3117                         case XPATH_BOOLEAN:
3118                             xmlGenericError(xmlGenericErrorContext,
3119                                             "%s is a Boolean\n", arg);
3120                             break;
3121                         case XPATH_NUMBER:
3122                             xmlGenericError(xmlGenericErrorContext,
3123                                             "%s is a number\n", arg);
3124                             break;
3125                         case XPATH_STRING:
3126                             xmlGenericError(xmlGenericErrorContext,
3127                                             "%s is a string\n", arg);
3128                             break;
3129                         case XPATH_POINT:
3130                             xmlGenericError(xmlGenericErrorContext,
3131                                             "%s is a point\n", arg);
3132                             break;
3133                         case XPATH_RANGE:
3134                             xmlGenericError(xmlGenericErrorContext,
3135                                             "%s is a range\n", arg);
3136                             break;
3137                         case XPATH_LOCATIONSET:
3138                             xmlGenericError(xmlGenericErrorContext,
3139                                             "%s is a range\n", arg);
3140                             break;
3141                         case XPATH_USERS:
3142                             xmlGenericError(xmlGenericErrorContext,
3143                                             "%s is user-defined\n", arg);
3144                             break;
3145                         case XPATH_XSLT_TREE:
3146                             xmlGenericError(xmlGenericErrorContext,
3147                                             "%s is an XSLT value tree\n",
3148                                             arg);
3149                             break;
3150                     }
3151 #ifdef LIBXML_XPATH_ENABLED
3152                     xmlXPathFreeObject(list);
3153 #endif
3154                 } else {
3155                     xmlGenericError(xmlGenericErrorContext,
3156                                     "%s: no such node\n", arg);
3157                 }
3158                 ctxt->pctxt->node = NULL;
3159             }
3160 #ifdef LIBXML_OUTPUT_ENABLED
3161         } else if (!strcmp(command, "cat")) {
3162             if (arg[0] == 0) {
3163                 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3164             } else {
3165                 ctxt->pctxt->node = ctxt->node;
3166 #ifdef LIBXML_XPATH_ENABLED
3167                 ctxt->pctxt->node = ctxt->node;
3168                 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3169 #else
3170                 list = NULL;
3171 #endif /* LIBXML_XPATH_ENABLED */
3172                 if (list != NULL) {
3173                     switch (list->type) {
3174                         case XPATH_UNDEFINED:
3175                             xmlGenericError(xmlGenericErrorContext,
3176                                             "%s: no such node\n", arg);
3177                             break;
3178                         case XPATH_NODESET:{
3179                                 int indx;
3180
3181                                 if (list->nodesetval == NULL)
3182                                     break;
3183
3184                                 for (indx = 0;
3185                                      indx < list->nodesetval->nodeNr;
3186                                      indx++) {
3187                                     if (i > 0)
3188                                         fprintf(ctxt->output, " -------\n");
3189                                     xmlShellCat(ctxt, NULL,
3190                                                 list->nodesetval->
3191                                                 nodeTab[indx], NULL);
3192                                 }
3193                                 break;
3194                             }
3195                         case XPATH_BOOLEAN:
3196                             xmlGenericError(xmlGenericErrorContext,
3197                                             "%s is a Boolean\n", arg);
3198                             break;
3199                         case XPATH_NUMBER:
3200                             xmlGenericError(xmlGenericErrorContext,
3201                                             "%s is a number\n", arg);
3202                             break;
3203                         case XPATH_STRING:
3204                             xmlGenericError(xmlGenericErrorContext,
3205                                             "%s is a string\n", arg);
3206                             break;
3207                         case XPATH_POINT:
3208                             xmlGenericError(xmlGenericErrorContext,
3209                                             "%s is a point\n", arg);
3210                             break;
3211                         case XPATH_RANGE:
3212                             xmlGenericError(xmlGenericErrorContext,
3213                                             "%s is a range\n", arg);
3214                             break;
3215                         case XPATH_LOCATIONSET:
3216                             xmlGenericError(xmlGenericErrorContext,
3217                                             "%s is a range\n", arg);
3218                             break;
3219                         case XPATH_USERS:
3220                             xmlGenericError(xmlGenericErrorContext,
3221                                             "%s is user-defined\n", arg);
3222                             break;
3223                         case XPATH_XSLT_TREE:
3224                             xmlGenericError(xmlGenericErrorContext,
3225                                             "%s is an XSLT value tree\n",
3226                                             arg);
3227                             break;
3228                     }
3229 #ifdef LIBXML_XPATH_ENABLED
3230                     xmlXPathFreeObject(list);
3231 #endif
3232                 } else {
3233                     xmlGenericError(xmlGenericErrorContext,
3234                                     "%s: no such node\n", arg);
3235                 }
3236                 ctxt->pctxt->node = NULL;
3237             }
3238 #endif /* LIBXML_OUTPUT_ENABLED */
3239         } else {
3240             xmlGenericError(xmlGenericErrorContext,
3241                             "Unknown command %s\n", command);
3242         }
3243         free(cmdline);          /* not xmlFree here ! */
3244         cmdline = NULL;
3245     }
3246 #ifdef LIBXML_XPATH_ENABLED
3247     xmlXPathFreeContext(ctxt->pctxt);
3248 #endif /* LIBXML_XPATH_ENABLED */
3249     if (ctxt->loaded) {
3250         xmlFreeDoc(ctxt->doc);
3251     }
3252     if (ctxt->filename != NULL)
3253         xmlFree(ctxt->filename);
3254     xmlFree(ctxt);
3255     if (cmdline != NULL)
3256         free(cmdline);          /* not xmlFree here ! */
3257 }
3258
3259 #endif /* LIBXML_XPATH_ENABLED */
3260 #define bottom_debugXML
3261 #include "elfgcchack.h"
3262 #endif /* LIBXML_DEBUG_ENABLED */