include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / libs / xml2 / debugXML.c
blob3bb19304ff35141f2bdb8ab160c43ed18242e4f7
1 /*
2 * debugXML.c : This is a set of routines used for debugging the tree
3 * produced by the XML parser.
5 * See Copyright for the status of this software.
7 * Daniel Veillard <daniel@veillard.com>
8 */
10 #define IN_LIBXML
11 #include "libxml.h"
12 #ifdef LIBXML_DEBUG_ENABLED
14 #include <string.h>
15 #include <stdlib.h>
17 #include <libxml/xmlmemory.h>
18 #include <libxml/tree.h>
19 #include <libxml/parser.h>
20 #include <libxml/parserInternals.h>
21 #include <libxml/valid.h>
22 #include <libxml/debugXML.h>
23 #include <libxml/HTMLtree.h>
24 #include <libxml/HTMLparser.h>
25 #include <libxml/xmlerror.h>
26 #include <libxml/globals.h>
27 #include <libxml/xpathInternals.h>
28 #include <libxml/uri.h>
29 #ifdef LIBXML_SCHEMAS_ENABLED
30 #include <libxml/relaxng.h>
31 #endif
33 #include "private/error.h"
35 #define DUMP_TEXT_TYPE 1
37 typedef struct _xmlDebugCtxt xmlDebugCtxt;
38 typedef xmlDebugCtxt *xmlDebugCtxtPtr;
39 struct _xmlDebugCtxt {
40 FILE *output; /* the output file */
41 char shift[101]; /* used for indenting */
42 int depth; /* current depth */
43 xmlDocPtr doc; /* current document */
44 xmlNodePtr node; /* current node */
45 xmlDictPtr dict; /* the doc dictionary */
46 int check; /* do just checkings */
47 int errors; /* number of errors found */
48 int nodict; /* if the document has no dictionary */
49 int options; /* options */
52 static void xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node);
54 static void
55 xmlCtxtDumpInitCtxt(xmlDebugCtxtPtr ctxt)
57 int i;
59 ctxt->depth = 0;
60 ctxt->check = 0;
61 ctxt->errors = 0;
62 ctxt->output = stdout;
63 ctxt->doc = NULL;
64 ctxt->node = NULL;
65 ctxt->dict = NULL;
66 ctxt->nodict = 0;
67 ctxt->options = 0;
68 for (i = 0; i < 100; i++)
69 ctxt->shift[i] = ' ';
70 ctxt->shift[100] = 0;
73 static void
74 xmlCtxtDumpCleanCtxt(xmlDebugCtxtPtr ctxt ATTRIBUTE_UNUSED)
76 /* remove the ATTRIBUTE_UNUSED when this is added */
79 /**
80 * xmlNsCheckScope:
81 * @node: the node
82 * @ns: the namespace node
84 * Check that a given namespace is in scope on a node.
86 * Returns 1 if in scope, -1 in case of argument error,
87 * -2 if the namespace is not in scope, and -3 if not on
88 * an ancestor node.
90 static int
91 xmlNsCheckScope(xmlNodePtr node, xmlNsPtr ns)
93 xmlNsPtr cur;
95 if ((node == NULL) || (ns == NULL))
96 return(-1);
98 if ((node->type != XML_ELEMENT_NODE) &&
99 (node->type != XML_ATTRIBUTE_NODE) &&
100 (node->type != XML_DOCUMENT_NODE) &&
101 (node->type != XML_TEXT_NODE) &&
102 (node->type != XML_HTML_DOCUMENT_NODE) &&
103 (node->type != XML_XINCLUDE_START))
104 return(-2);
106 while ((node != NULL) &&
107 ((node->type == XML_ELEMENT_NODE) ||
108 (node->type == XML_ATTRIBUTE_NODE) ||
109 (node->type == XML_TEXT_NODE) ||
110 (node->type == XML_XINCLUDE_START))) {
111 if ((node->type == XML_ELEMENT_NODE) ||
112 (node->type == XML_XINCLUDE_START)) {
113 cur = node->nsDef;
114 while (cur != NULL) {
115 if (cur == ns)
116 return(1);
117 if (xmlStrEqual(cur->prefix, ns->prefix))
118 return(-2);
119 cur = cur->next;
122 node = node->parent;
124 /* the xml namespace may be declared on the document node */
125 if ((node != NULL) &&
126 ((node->type == XML_DOCUMENT_NODE) ||
127 (node->type == XML_HTML_DOCUMENT_NODE))) {
128 xmlNsPtr oldNs = ((xmlDocPtr) node)->oldNs;
129 if (oldNs == ns)
130 return(1);
132 return(-3);
135 static void
136 xmlCtxtDumpSpaces(xmlDebugCtxtPtr ctxt)
138 if (ctxt->check)
139 return;
140 if ((ctxt->output != NULL) && (ctxt->depth > 0)) {
141 if (ctxt->depth < 50)
142 fprintf(ctxt->output, "%s", &ctxt->shift[100 - 2 * ctxt->depth]);
143 else
144 fprintf(ctxt->output, "%s", ctxt->shift);
149 * xmlDebugErr:
150 * @ctxt: a debug context
151 * @error: the error code
153 * Handle a debug error.
155 static void
156 xmlDebugErr(xmlDebugCtxtPtr ctxt, int error, const char *msg)
158 ctxt->errors++;
159 __xmlRaiseError(NULL, NULL, NULL,
160 NULL, ctxt->node, XML_FROM_CHECK,
161 error, XML_ERR_ERROR, NULL, 0,
162 NULL, NULL, NULL, 0, 0,
163 "%s", msg);
165 static void LIBXML_ATTR_FORMAT(3,0)
166 xmlDebugErr2(xmlDebugCtxtPtr ctxt, int error, const char *msg, int extra)
168 ctxt->errors++;
169 __xmlRaiseError(NULL, NULL, NULL,
170 NULL, ctxt->node, XML_FROM_CHECK,
171 error, XML_ERR_ERROR, NULL, 0,
172 NULL, NULL, NULL, 0, 0,
173 msg, extra);
175 static void LIBXML_ATTR_FORMAT(3,0)
176 xmlDebugErr3(xmlDebugCtxtPtr ctxt, int error, const char *msg, const char *extra)
178 ctxt->errors++;
179 __xmlRaiseError(NULL, NULL, NULL,
180 NULL, ctxt->node, XML_FROM_CHECK,
181 error, XML_ERR_ERROR, NULL, 0,
182 NULL, NULL, NULL, 0, 0,
183 msg, extra);
187 * xmlCtxtNsCheckScope:
188 * @ctxt: the debugging context
189 * @node: the node
190 * @ns: the namespace node
192 * Report if a given namespace is is not in scope.
194 static void
195 xmlCtxtNsCheckScope(xmlDebugCtxtPtr ctxt, xmlNodePtr node, xmlNsPtr ns)
197 int ret;
199 ret = xmlNsCheckScope(node, ns);
200 if (ret == -2) {
201 if (ns->prefix == NULL)
202 xmlDebugErr(ctxt, XML_CHECK_NS_SCOPE,
203 "Reference to default namespace not in scope\n");
204 else
205 xmlDebugErr3(ctxt, XML_CHECK_NS_SCOPE,
206 "Reference to namespace '%s' not in scope\n",
207 (char *) ns->prefix);
209 if (ret == -3) {
210 if (ns->prefix == NULL)
211 xmlDebugErr(ctxt, XML_CHECK_NS_ANCESTOR,
212 "Reference to default namespace not on ancestor\n");
213 else
214 xmlDebugErr3(ctxt, XML_CHECK_NS_ANCESTOR,
215 "Reference to namespace '%s' not on ancestor\n",
216 (char *) ns->prefix);
221 * xmlCtxtCheckString:
222 * @ctxt: the debug context
223 * @str: the string
225 * Do debugging on the string, currently it just checks the UTF-8 content
227 static void
228 xmlCtxtCheckString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
230 if (str == NULL) return;
231 if (ctxt->check) {
232 if (!xmlCheckUTF8(str)) {
233 xmlDebugErr3(ctxt, XML_CHECK_NOT_UTF8,
234 "String is not UTF-8 %s", (const char *) str);
240 * xmlCtxtCheckName:
241 * @ctxt: the debug context
242 * @name: the name
244 * Do debugging on the name, for example the dictionary status and
245 * conformance to the Name production.
247 static void
248 xmlCtxtCheckName(xmlDebugCtxtPtr ctxt, const xmlChar * name)
250 if (ctxt->check) {
251 if (name == NULL) {
252 xmlDebugErr(ctxt, XML_CHECK_NO_NAME, "Name is NULL");
253 return;
255 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
256 if (xmlValidateName(name, 0)) {
257 xmlDebugErr3(ctxt, XML_CHECK_NOT_NCNAME,
258 "Name is not an NCName '%s'", (const char *) name);
260 #endif
261 if ((ctxt->dict != NULL) &&
262 (!xmlDictOwns(ctxt->dict, name)) &&
263 ((ctxt->doc == NULL) ||
264 ((ctxt->doc->parseFlags & (XML_PARSE_SAX1 | XML_PARSE_NODICT)) == 0))) {
265 xmlDebugErr3(ctxt, XML_CHECK_OUTSIDE_DICT,
266 "Name is not from the document dictionary '%s'",
267 (const char *) name);
272 static void
273 xmlCtxtGenericNodeCheck(xmlDebugCtxtPtr ctxt, xmlNodePtr node) {
274 xmlDocPtr doc;
275 xmlDictPtr dict;
277 doc = node->doc;
279 if (node->parent == NULL)
280 xmlDebugErr(ctxt, XML_CHECK_NO_PARENT,
281 "Node has no parent\n");
282 if (node->doc == NULL) {
283 xmlDebugErr(ctxt, XML_CHECK_NO_DOC,
284 "Node has no doc\n");
285 dict = NULL;
286 } else {
287 dict = doc->dict;
288 if ((dict == NULL) && (ctxt->nodict == 0)) {
289 #if 0
290 /* deactivated right now as it raises too many errors */
291 if (doc->type == XML_DOCUMENT_NODE)
292 xmlDebugErr(ctxt, XML_CHECK_NO_DICT,
293 "Document has no dictionary\n");
294 #endif
295 ctxt->nodict = 1;
297 if (ctxt->doc == NULL)
298 ctxt->doc = doc;
300 if (ctxt->dict == NULL) {
301 ctxt->dict = dict;
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");
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");
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");
337 if (node->type == XML_ELEMENT_NODE) {
338 xmlNsPtr ns;
340 ns = node->nsDef;
341 while (ns != NULL) {
342 xmlCtxtNsCheckScope(ctxt, node, ns);
343 ns = ns->next;
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);
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);
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;
377 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
378 "Text node has wrong name '%s'",
379 (const char *) node->name);
380 break;
381 case XML_COMMENT_NODE:
382 if (node->name == xmlStringComment)
383 break;
384 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
385 "Comment node has wrong name '%s'",
386 (const char *) node->name);
387 break;
388 case XML_PI_NODE:
389 xmlCtxtCheckName(ctxt, node->name);
390 break;
391 case XML_CDATA_SECTION_NODE:
392 if (node->name == NULL)
393 break;
394 xmlDebugErr3(ctxt, XML_CHECK_NAME_NOT_NULL,
395 "CData section has non NULL name '%s'",
396 (const char *) node->name);
397 break;
398 case XML_ENTITY_REF_NODE:
399 case XML_ENTITY_NODE:
400 case XML_DOCUMENT_TYPE_NODE:
401 case XML_DOCUMENT_FRAG_NODE:
402 case XML_NOTATION_NODE:
403 case XML_DTD_NODE:
404 case XML_ELEMENT_DECL:
405 case XML_ATTRIBUTE_DECL:
406 case XML_ENTITY_DECL:
407 case XML_NAMESPACE_DECL:
408 case XML_XINCLUDE_START:
409 case XML_XINCLUDE_END:
410 case XML_DOCUMENT_NODE:
411 case XML_HTML_DOCUMENT_NODE:
412 break;
416 static void
417 xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
419 int i;
421 if (ctxt->check) {
422 return;
424 /* TODO: check UTF8 content of the string */
425 if (str == NULL) {
426 fprintf(ctxt->output, "(NULL)");
427 return;
429 for (i = 0; i < 40; i++)
430 if (str[i] == 0)
431 return;
432 else if (IS_BLANK_CH(str[i]))
433 fputc(' ', ctxt->output);
434 else if (str[i] >= 0x80)
435 fprintf(ctxt->output, "#%X", str[i]);
436 else
437 fputc(str[i], ctxt->output);
438 fprintf(ctxt->output, "...");
441 static void
442 xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
444 xmlCtxtDumpSpaces(ctxt);
446 if (dtd == NULL) {
447 if (!ctxt->check)
448 fprintf(ctxt->output, "DTD node is NULL\n");
449 return;
452 if (dtd->type != XML_DTD_NODE) {
453 xmlDebugErr(ctxt, XML_CHECK_NOT_DTD,
454 "Node is not a DTD");
455 return;
457 if (!ctxt->check) {
458 if (dtd->name != NULL)
459 fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name);
460 else
461 fprintf(ctxt->output, "DTD");
462 if (dtd->ExternalID != NULL)
463 fprintf(ctxt->output, ", PUBLIC %s", (char *) dtd->ExternalID);
464 if (dtd->SystemID != NULL)
465 fprintf(ctxt->output, ", SYSTEM %s", (char *) dtd->SystemID);
466 fprintf(ctxt->output, "\n");
469 * Do a bit of checking
471 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd);
474 static void
475 xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr)
477 xmlCtxtDumpSpaces(ctxt);
479 if (attr == NULL) {
480 if (!ctxt->check)
481 fprintf(ctxt->output, "Attribute declaration is NULL\n");
482 return;
484 if (attr->type != XML_ATTRIBUTE_DECL) {
485 xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL,
486 "Node is not an attribute declaration");
487 return;
489 if (attr->name != NULL) {
490 if (!ctxt->check)
491 fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name);
492 } else
493 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
494 "Node attribute declaration has no name");
495 if (attr->elem != NULL) {
496 if (!ctxt->check)
497 fprintf(ctxt->output, " for %s", (char *) attr->elem);
498 } else
499 xmlDebugErr(ctxt, XML_CHECK_NO_ELEM,
500 "Node attribute declaration has no element name");
501 if (!ctxt->check) {
502 switch (attr->atype) {
503 case XML_ATTRIBUTE_CDATA:
504 fprintf(ctxt->output, " CDATA");
505 break;
506 case XML_ATTRIBUTE_ID:
507 fprintf(ctxt->output, " ID");
508 break;
509 case XML_ATTRIBUTE_IDREF:
510 fprintf(ctxt->output, " IDREF");
511 break;
512 case XML_ATTRIBUTE_IDREFS:
513 fprintf(ctxt->output, " IDREFS");
514 break;
515 case XML_ATTRIBUTE_ENTITY:
516 fprintf(ctxt->output, " ENTITY");
517 break;
518 case XML_ATTRIBUTE_ENTITIES:
519 fprintf(ctxt->output, " ENTITIES");
520 break;
521 case XML_ATTRIBUTE_NMTOKEN:
522 fprintf(ctxt->output, " NMTOKEN");
523 break;
524 case XML_ATTRIBUTE_NMTOKENS:
525 fprintf(ctxt->output, " NMTOKENS");
526 break;
527 case XML_ATTRIBUTE_ENUMERATION:
528 fprintf(ctxt->output, " ENUMERATION");
529 break;
530 case XML_ATTRIBUTE_NOTATION:
531 fprintf(ctxt->output, " NOTATION ");
532 break;
534 if (attr->tree != NULL) {
535 int indx;
536 xmlEnumerationPtr cur = attr->tree;
538 for (indx = 0; indx < 5; indx++) {
539 if (indx != 0)
540 fprintf(ctxt->output, "|%s", (char *) cur->name);
541 else
542 fprintf(ctxt->output, " (%s", (char *) cur->name);
543 cur = cur->next;
544 if (cur == NULL)
545 break;
547 if (cur == NULL)
548 fprintf(ctxt->output, ")");
549 else
550 fprintf(ctxt->output, "...)");
552 switch (attr->def) {
553 case XML_ATTRIBUTE_NONE:
554 break;
555 case XML_ATTRIBUTE_REQUIRED:
556 fprintf(ctxt->output, " REQUIRED");
557 break;
558 case XML_ATTRIBUTE_IMPLIED:
559 fprintf(ctxt->output, " IMPLIED");
560 break;
561 case XML_ATTRIBUTE_FIXED:
562 fprintf(ctxt->output, " FIXED");
563 break;
565 if (attr->defaultValue != NULL) {
566 fprintf(ctxt->output, "\"");
567 xmlCtxtDumpString(ctxt, attr->defaultValue);
568 fprintf(ctxt->output, "\"");
570 fprintf(ctxt->output, "\n");
574 * Do a bit of checking
576 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
579 static void
580 xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem)
582 xmlCtxtDumpSpaces(ctxt);
584 if (elem == NULL) {
585 if (!ctxt->check)
586 fprintf(ctxt->output, "Element declaration is NULL\n");
587 return;
589 if (elem->type != XML_ELEMENT_DECL) {
590 xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL,
591 "Node is not an element declaration");
592 return;
594 if (elem->name != NULL) {
595 if (!ctxt->check) {
596 fprintf(ctxt->output, "ELEMDECL(");
597 xmlCtxtDumpString(ctxt, elem->name);
598 fprintf(ctxt->output, ")");
600 } else
601 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
602 "Element declaration has no name");
603 if (!ctxt->check) {
604 switch (elem->etype) {
605 case XML_ELEMENT_TYPE_UNDEFINED:
606 fprintf(ctxt->output, ", UNDEFINED");
607 break;
608 case XML_ELEMENT_TYPE_EMPTY:
609 fprintf(ctxt->output, ", EMPTY");
610 break;
611 case XML_ELEMENT_TYPE_ANY:
612 fprintf(ctxt->output, ", ANY");
613 break;
614 case XML_ELEMENT_TYPE_MIXED:
615 fprintf(ctxt->output, ", MIXED ");
616 break;
617 case XML_ELEMENT_TYPE_ELEMENT:
618 fprintf(ctxt->output, ", MIXED ");
619 break;
621 if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) {
622 char buf[5001];
624 buf[0] = 0;
625 xmlSnprintfElementContent(buf, 5000, elem->content, 1);
626 buf[5000] = 0;
627 fprintf(ctxt->output, "%s", buf);
629 fprintf(ctxt->output, "\n");
633 * Do a bit of checking
635 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem);
638 static void
639 xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
641 xmlCtxtDumpSpaces(ctxt);
643 if (ent == NULL) {
644 if (!ctxt->check)
645 fprintf(ctxt->output, "Entity declaration is NULL\n");
646 return;
648 if (ent->type != XML_ENTITY_DECL) {
649 xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL,
650 "Node is not an entity declaration");
651 return;
653 if (ent->name != NULL) {
654 if (!ctxt->check) {
655 fprintf(ctxt->output, "ENTITYDECL(");
656 xmlCtxtDumpString(ctxt, ent->name);
657 fprintf(ctxt->output, ")");
659 } else
660 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
661 "Entity declaration has no name");
662 if (!ctxt->check) {
663 switch (ent->etype) {
664 case XML_INTERNAL_GENERAL_ENTITY:
665 fprintf(ctxt->output, ", internal\n");
666 break;
667 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
668 fprintf(ctxt->output, ", external parsed\n");
669 break;
670 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
671 fprintf(ctxt->output, ", unparsed\n");
672 break;
673 case XML_INTERNAL_PARAMETER_ENTITY:
674 fprintf(ctxt->output, ", parameter\n");
675 break;
676 case XML_EXTERNAL_PARAMETER_ENTITY:
677 fprintf(ctxt->output, ", external parameter\n");
678 break;
679 case XML_INTERNAL_PREDEFINED_ENTITY:
680 fprintf(ctxt->output, ", predefined\n");
681 break;
683 if (ent->ExternalID) {
684 xmlCtxtDumpSpaces(ctxt);
685 fprintf(ctxt->output, " ExternalID=%s\n",
686 (char *) ent->ExternalID);
688 if (ent->SystemID) {
689 xmlCtxtDumpSpaces(ctxt);
690 fprintf(ctxt->output, " SystemID=%s\n",
691 (char *) ent->SystemID);
693 if (ent->URI != NULL) {
694 xmlCtxtDumpSpaces(ctxt);
695 fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI);
697 if (ent->content) {
698 xmlCtxtDumpSpaces(ctxt);
699 fprintf(ctxt->output, " content=");
700 xmlCtxtDumpString(ctxt, ent->content);
701 fprintf(ctxt->output, "\n");
706 * Do a bit of checking
708 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent);
711 static void
712 xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
714 xmlCtxtDumpSpaces(ctxt);
716 if (ns == NULL) {
717 if (!ctxt->check)
718 fprintf(ctxt->output, "namespace node is NULL\n");
719 return;
721 if (ns->type != XML_NAMESPACE_DECL) {
722 xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL,
723 "Node is not a namespace declaration");
724 return;
726 if (ns->href == NULL) {
727 if (ns->prefix != NULL)
728 xmlDebugErr3(ctxt, XML_CHECK_NO_HREF,
729 "Incomplete namespace %s href=NULL\n",
730 (char *) ns->prefix);
731 else
732 xmlDebugErr(ctxt, XML_CHECK_NO_HREF,
733 "Incomplete default namespace href=NULL\n");
734 } else {
735 if (!ctxt->check) {
736 if (ns->prefix != NULL)
737 fprintf(ctxt->output, "namespace %s href=",
738 (char *) ns->prefix);
739 else
740 fprintf(ctxt->output, "default namespace href=");
742 xmlCtxtDumpString(ctxt, ns->href);
743 fprintf(ctxt->output, "\n");
748 static void
749 xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
751 while (ns != NULL) {
752 xmlCtxtDumpNamespace(ctxt, ns);
753 ns = ns->next;
757 static void
758 xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
760 xmlCtxtDumpSpaces(ctxt);
762 if (ent == NULL) {
763 if (!ctxt->check)
764 fprintf(ctxt->output, "Entity is NULL\n");
765 return;
767 if (!ctxt->check) {
768 switch (ent->etype) {
769 case XML_INTERNAL_GENERAL_ENTITY:
770 fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY ");
771 break;
772 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
773 fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
774 break;
775 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
776 fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
777 break;
778 case XML_INTERNAL_PARAMETER_ENTITY:
779 fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY ");
780 break;
781 case XML_EXTERNAL_PARAMETER_ENTITY:
782 fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY ");
783 break;
784 default:
785 fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype);
787 fprintf(ctxt->output, "%s\n", ent->name);
788 if (ent->ExternalID) {
789 xmlCtxtDumpSpaces(ctxt);
790 fprintf(ctxt->output, "ExternalID=%s\n",
791 (char *) ent->ExternalID);
793 if (ent->SystemID) {
794 xmlCtxtDumpSpaces(ctxt);
795 fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID);
797 if (ent->URI) {
798 xmlCtxtDumpSpaces(ctxt);
799 fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI);
801 if (ent->content) {
802 xmlCtxtDumpSpaces(ctxt);
803 fprintf(ctxt->output, "content=");
804 xmlCtxtDumpString(ctxt, ent->content);
805 fprintf(ctxt->output, "\n");
811 * xmlCtxtDumpAttr:
812 * @output: the FILE * for the output
813 * @attr: the attribute
814 * @depth: the indentation level.
816 * Dumps debug information for the attribute
818 static void
819 xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
821 xmlCtxtDumpSpaces(ctxt);
823 if (attr == NULL) {
824 if (!ctxt->check)
825 fprintf(ctxt->output, "Attr is NULL");
826 return;
828 if (!ctxt->check) {
829 fprintf(ctxt->output, "ATTRIBUTE ");
830 xmlCtxtDumpString(ctxt, attr->name);
831 fprintf(ctxt->output, "\n");
832 if (attr->children != NULL) {
833 ctxt->depth++;
834 xmlCtxtDumpNodeList(ctxt, attr->children);
835 ctxt->depth--;
838 if (attr->name == NULL)
839 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
840 "Attribute has no name");
843 * Do a bit of checking
845 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
849 * xmlCtxtDumpAttrList:
850 * @output: the FILE * for the output
851 * @attr: the attribute list
852 * @depth: the indentation level.
854 * Dumps debug information for the attribute list
856 static void
857 xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
859 while (attr != NULL) {
860 xmlCtxtDumpAttr(ctxt, attr);
861 attr = attr->next;
866 * xmlCtxtDumpOneNode:
867 * @output: the FILE * for the output
868 * @node: the node
869 * @depth: the indentation level.
871 * Dumps debug information for the element node, it is not recursive
873 static void
874 xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
876 if (node == NULL) {
877 if (!ctxt->check) {
878 xmlCtxtDumpSpaces(ctxt);
879 fprintf(ctxt->output, "node is NULL\n");
881 return;
883 ctxt->node = node;
885 switch (node->type) {
886 case XML_ELEMENT_NODE:
887 if (!ctxt->check) {
888 xmlCtxtDumpSpaces(ctxt);
889 fprintf(ctxt->output, "ELEMENT ");
890 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
891 xmlCtxtDumpString(ctxt, node->ns->prefix);
892 fprintf(ctxt->output, ":");
894 xmlCtxtDumpString(ctxt, node->name);
895 fprintf(ctxt->output, "\n");
897 break;
898 case XML_ATTRIBUTE_NODE:
899 if (!ctxt->check)
900 xmlCtxtDumpSpaces(ctxt);
901 fprintf(ctxt->output, "Error, ATTRIBUTE found here\n");
902 xmlCtxtGenericNodeCheck(ctxt, node);
903 return;
904 case XML_TEXT_NODE:
905 if (!ctxt->check) {
906 xmlCtxtDumpSpaces(ctxt);
907 if (node->name == (const xmlChar *) xmlStringTextNoenc)
908 fprintf(ctxt->output, "TEXT no enc");
909 else
910 fprintf(ctxt->output, "TEXT");
911 if (ctxt->options & DUMP_TEXT_TYPE) {
912 if (node->content == (xmlChar *) &(node->properties))
913 fprintf(ctxt->output, " compact\n");
914 else if (xmlDictOwns(ctxt->dict, node->content) == 1)
915 fprintf(ctxt->output, " interned\n");
916 else
917 fprintf(ctxt->output, "\n");
918 } else
919 fprintf(ctxt->output, "\n");
921 break;
922 case XML_CDATA_SECTION_NODE:
923 if (!ctxt->check) {
924 xmlCtxtDumpSpaces(ctxt);
925 fprintf(ctxt->output, "CDATA_SECTION\n");
927 break;
928 case XML_ENTITY_REF_NODE:
929 if (!ctxt->check) {
930 xmlCtxtDumpSpaces(ctxt);
931 fprintf(ctxt->output, "ENTITY_REF(%s)\n",
932 (char *) node->name);
934 break;
935 case XML_ENTITY_NODE:
936 if (!ctxt->check) {
937 xmlCtxtDumpSpaces(ctxt);
938 fprintf(ctxt->output, "ENTITY\n");
940 break;
941 case XML_PI_NODE:
942 if (!ctxt->check) {
943 xmlCtxtDumpSpaces(ctxt);
944 fprintf(ctxt->output, "PI %s\n", (char *) node->name);
946 break;
947 case XML_COMMENT_NODE:
948 if (!ctxt->check) {
949 xmlCtxtDumpSpaces(ctxt);
950 fprintf(ctxt->output, "COMMENT\n");
952 break;
953 case XML_DOCUMENT_NODE:
954 case XML_HTML_DOCUMENT_NODE:
955 if (!ctxt->check) {
956 xmlCtxtDumpSpaces(ctxt);
958 fprintf(ctxt->output, "Error, DOCUMENT found here\n");
959 xmlCtxtGenericNodeCheck(ctxt, node);
960 return;
961 case XML_DOCUMENT_TYPE_NODE:
962 if (!ctxt->check) {
963 xmlCtxtDumpSpaces(ctxt);
964 fprintf(ctxt->output, "DOCUMENT_TYPE\n");
966 break;
967 case XML_DOCUMENT_FRAG_NODE:
968 if (!ctxt->check) {
969 xmlCtxtDumpSpaces(ctxt);
970 fprintf(ctxt->output, "DOCUMENT_FRAG\n");
972 break;
973 case XML_NOTATION_NODE:
974 if (!ctxt->check) {
975 xmlCtxtDumpSpaces(ctxt);
976 fprintf(ctxt->output, "NOTATION\n");
978 break;
979 case XML_DTD_NODE:
980 xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
981 return;
982 case XML_ELEMENT_DECL:
983 xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
984 return;
985 case XML_ATTRIBUTE_DECL:
986 xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
987 return;
988 case XML_ENTITY_DECL:
989 xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
990 return;
991 case XML_NAMESPACE_DECL:
992 xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
993 return;
994 case XML_XINCLUDE_START:
995 if (!ctxt->check) {
996 xmlCtxtDumpSpaces(ctxt);
997 fprintf(ctxt->output, "INCLUDE START\n");
999 return;
1000 case XML_XINCLUDE_END:
1001 if (!ctxt->check) {
1002 xmlCtxtDumpSpaces(ctxt);
1003 fprintf(ctxt->output, "INCLUDE END\n");
1005 return;
1006 default:
1007 if (!ctxt->check)
1008 xmlCtxtDumpSpaces(ctxt);
1009 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1010 "Unknown node type %d\n", node->type);
1011 return;
1013 if (node->doc == NULL) {
1014 if (!ctxt->check) {
1015 xmlCtxtDumpSpaces(ctxt);
1017 fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
1019 ctxt->depth++;
1020 if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL))
1021 xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
1022 if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL))
1023 xmlCtxtDumpAttrList(ctxt, node->properties);
1024 if (node->type != XML_ENTITY_REF_NODE) {
1025 if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
1026 if (!ctxt->check) {
1027 xmlCtxtDumpSpaces(ctxt);
1028 fprintf(ctxt->output, "content=");
1029 xmlCtxtDumpString(ctxt, node->content);
1030 fprintf(ctxt->output, "\n");
1033 } else {
1034 xmlEntityPtr ent;
1036 ent = xmlGetDocEntity(node->doc, node->name);
1037 if (ent != NULL)
1038 xmlCtxtDumpEntity(ctxt, ent);
1040 ctxt->depth--;
1043 * Do a bit of checking
1045 xmlCtxtGenericNodeCheck(ctxt, node);
1049 * xmlCtxtDumpNode:
1050 * @output: the FILE * for the output
1051 * @node: the node
1052 * @depth: the indentation level.
1054 * Dumps debug information for the element node, it is recursive
1056 static void
1057 xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1059 if (node == NULL) {
1060 if (!ctxt->check) {
1061 xmlCtxtDumpSpaces(ctxt);
1062 fprintf(ctxt->output, "node is NULL\n");
1064 return;
1066 xmlCtxtDumpOneNode(ctxt, node);
1067 if ((node->type != XML_NAMESPACE_DECL) &&
1068 (node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
1069 ctxt->depth++;
1070 xmlCtxtDumpNodeList(ctxt, node->children);
1071 ctxt->depth--;
1076 * xmlCtxtDumpNodeList:
1077 * @output: the FILE * for the output
1078 * @node: the node list
1079 * @depth: the indentation level.
1081 * Dumps debug information for the list of element node, it is recursive
1083 static void
1084 xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1086 while (node != NULL) {
1087 xmlCtxtDumpNode(ctxt, node);
1088 node = node->next;
1092 static void
1093 xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1095 if (doc == NULL) {
1096 if (!ctxt->check)
1097 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1098 return;
1100 ctxt->node = (xmlNodePtr) doc;
1102 switch (doc->type) {
1103 case XML_ELEMENT_NODE:
1104 xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
1105 "Misplaced ELEMENT node\n");
1106 break;
1107 case XML_ATTRIBUTE_NODE:
1108 xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
1109 "Misplaced ATTRIBUTE node\n");
1110 break;
1111 case XML_TEXT_NODE:
1112 xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
1113 "Misplaced TEXT node\n");
1114 break;
1115 case XML_CDATA_SECTION_NODE:
1116 xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
1117 "Misplaced CDATA node\n");
1118 break;
1119 case XML_ENTITY_REF_NODE:
1120 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
1121 "Misplaced ENTITYREF node\n");
1122 break;
1123 case XML_ENTITY_NODE:
1124 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
1125 "Misplaced ENTITY node\n");
1126 break;
1127 case XML_PI_NODE:
1128 xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
1129 "Misplaced PI node\n");
1130 break;
1131 case XML_COMMENT_NODE:
1132 xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
1133 "Misplaced COMMENT node\n");
1134 break;
1135 case XML_DOCUMENT_NODE:
1136 if (!ctxt->check)
1137 fprintf(ctxt->output, "DOCUMENT\n");
1138 break;
1139 case XML_HTML_DOCUMENT_NODE:
1140 if (!ctxt->check)
1141 fprintf(ctxt->output, "HTML DOCUMENT\n");
1142 break;
1143 case XML_DOCUMENT_TYPE_NODE:
1144 xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
1145 "Misplaced DOCTYPE node\n");
1146 break;
1147 case XML_DOCUMENT_FRAG_NODE:
1148 xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
1149 "Misplaced FRAGMENT node\n");
1150 break;
1151 case XML_NOTATION_NODE:
1152 xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
1153 "Misplaced NOTATION node\n");
1154 break;
1155 default:
1156 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1157 "Unknown node type %d\n", doc->type);
1162 * xmlCtxtDumpDocumentHead:
1163 * @output: the FILE * for the output
1164 * @doc: the document
1166 * Dumps debug information concerning the document, not recursive
1168 static void
1169 xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1171 if (doc == NULL) return;
1172 xmlCtxtDumpDocHead(ctxt, doc);
1173 if (!ctxt->check) {
1174 if (doc->name != NULL) {
1175 fprintf(ctxt->output, "name=");
1176 xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
1177 fprintf(ctxt->output, "\n");
1179 if (doc->version != NULL) {
1180 fprintf(ctxt->output, "version=");
1181 xmlCtxtDumpString(ctxt, doc->version);
1182 fprintf(ctxt->output, "\n");
1184 if (doc->encoding != NULL) {
1185 fprintf(ctxt->output, "encoding=");
1186 xmlCtxtDumpString(ctxt, doc->encoding);
1187 fprintf(ctxt->output, "\n");
1189 if (doc->URL != NULL) {
1190 fprintf(ctxt->output, "URL=");
1191 xmlCtxtDumpString(ctxt, doc->URL);
1192 fprintf(ctxt->output, "\n");
1194 if (doc->standalone)
1195 fprintf(ctxt->output, "standalone=true\n");
1197 if (doc->oldNs != NULL)
1198 xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
1202 * xmlCtxtDumpDocument:
1203 * @output: the FILE * for the output
1204 * @doc: the document
1206 * Dumps debug information for the document, it's recursive
1208 static void
1209 xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1211 if (doc == NULL) {
1212 if (!ctxt->check)
1213 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1214 return;
1216 xmlCtxtDumpDocumentHead(ctxt, doc);
1217 if (((doc->type == XML_DOCUMENT_NODE) ||
1218 (doc->type == XML_HTML_DOCUMENT_NODE))
1219 && (doc->children != NULL)) {
1220 ctxt->depth++;
1221 xmlCtxtDumpNodeList(ctxt, doc->children);
1222 ctxt->depth--;
1226 static void
1227 xmlCtxtDumpEntityCallback(void *payload, void *data,
1228 const xmlChar *name ATTRIBUTE_UNUSED)
1230 xmlEntityPtr cur = (xmlEntityPtr) payload;
1231 xmlDebugCtxtPtr ctxt = (xmlDebugCtxtPtr) data;
1232 if (cur == NULL) {
1233 if (!ctxt->check)
1234 fprintf(ctxt->output, "Entity is NULL");
1235 return;
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);
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");
1273 * xmlCtxtDumpEntities:
1274 * @output: the FILE * for the output
1275 * @doc: the document
1277 * Dumps debug information for all the entities in use by the document
1279 static void
1280 xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
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;
1288 if (!ctxt->check)
1289 fprintf(ctxt->output, "Entities in internal subset\n");
1290 xmlHashScan(table, xmlCtxtDumpEntityCallback, ctxt);
1291 } else
1292 fprintf(ctxt->output, "No entities in internal subset\n");
1293 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
1294 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1295 doc->extSubset->entities;
1297 if (!ctxt->check)
1298 fprintf(ctxt->output, "Entities in external subset\n");
1299 xmlHashScan(table, xmlCtxtDumpEntityCallback, ctxt);
1300 } else if (!ctxt->check)
1301 fprintf(ctxt->output, "No entities in external subset\n");
1305 * xmlCtxtDumpDTD:
1306 * @output: the FILE * for the output
1307 * @dtd: the DTD
1309 * Dumps debug information for the DTD
1311 static void
1312 xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
1314 if (dtd == NULL) {
1315 if (!ctxt->check)
1316 fprintf(ctxt->output, "DTD is NULL\n");
1317 return;
1319 xmlCtxtDumpDtdNode(ctxt, dtd);
1320 if (dtd->children == NULL)
1321 fprintf(ctxt->output, " DTD is empty\n");
1322 else {
1323 ctxt->depth++;
1324 xmlCtxtDumpNodeList(ctxt, dtd->children);
1325 ctxt->depth--;
1329 /************************************************************************
1331 * Public entry points for dump *
1333 ************************************************************************/
1336 * xmlDebugDumpString:
1337 * @output: the FILE * for the output
1338 * @str: the string
1340 * Dumps information about the string, shorten it if necessary
1342 void
1343 xmlDebugDumpString(FILE * output, const xmlChar * str)
1345 int i;
1347 if (output == NULL)
1348 output = stdout;
1349 if (str == NULL) {
1350 fprintf(output, "(NULL)");
1351 return;
1353 for (i = 0; i < 40; i++)
1354 if (str[i] == 0)
1355 return;
1356 else if (IS_BLANK_CH(str[i]))
1357 fputc(' ', output);
1358 else if (str[i] >= 0x80)
1359 fprintf(output, "#%X", str[i]);
1360 else
1361 fputc(str[i], output);
1362 fprintf(output, "...");
1366 * xmlDebugDumpAttr:
1367 * @output: the FILE * for the output
1368 * @attr: the attribute
1369 * @depth: the indentation level.
1371 * Dumps debug information for the attribute
1373 void
1374 xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
1375 xmlDebugCtxt ctxt;
1377 if (output == NULL) return;
1378 xmlCtxtDumpInitCtxt(&ctxt);
1379 ctxt.output = output;
1380 ctxt.depth = depth;
1381 xmlCtxtDumpAttr(&ctxt, attr);
1382 xmlCtxtDumpCleanCtxt(&ctxt);
1387 * xmlDebugDumpEntities:
1388 * @output: the FILE * for the output
1389 * @doc: the document
1391 * Dumps debug information for all the entities in use by the document
1393 void
1394 xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
1396 xmlDebugCtxt ctxt;
1398 if (output == NULL) return;
1399 xmlCtxtDumpInitCtxt(&ctxt);
1400 ctxt.output = output;
1401 xmlCtxtDumpEntities(&ctxt, doc);
1402 xmlCtxtDumpCleanCtxt(&ctxt);
1406 * xmlDebugDumpAttrList:
1407 * @output: the FILE * for the output
1408 * @attr: the attribute list
1409 * @depth: the indentation level.
1411 * Dumps debug information for the attribute list
1413 void
1414 xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
1416 xmlDebugCtxt ctxt;
1418 if (output == NULL) return;
1419 xmlCtxtDumpInitCtxt(&ctxt);
1420 ctxt.output = output;
1421 ctxt.depth = depth;
1422 xmlCtxtDumpAttrList(&ctxt, attr);
1423 xmlCtxtDumpCleanCtxt(&ctxt);
1427 * xmlDebugDumpOneNode:
1428 * @output: the FILE * for the output
1429 * @node: the node
1430 * @depth: the indentation level.
1432 * Dumps debug information for the element node, it is not recursive
1434 void
1435 xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
1437 xmlDebugCtxt ctxt;
1439 if (output == NULL) return;
1440 xmlCtxtDumpInitCtxt(&ctxt);
1441 ctxt.output = output;
1442 ctxt.depth = depth;
1443 xmlCtxtDumpOneNode(&ctxt, node);
1444 xmlCtxtDumpCleanCtxt(&ctxt);
1448 * xmlDebugDumpNode:
1449 * @output: the FILE * for the output
1450 * @node: the node
1451 * @depth: the indentation level.
1453 * Dumps debug information for the element node, it is recursive
1455 void
1456 xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
1458 xmlDebugCtxt ctxt;
1460 if (output == NULL)
1461 output = stdout;
1462 xmlCtxtDumpInitCtxt(&ctxt);
1463 ctxt.output = output;
1464 ctxt.depth = depth;
1465 xmlCtxtDumpNode(&ctxt, node);
1466 xmlCtxtDumpCleanCtxt(&ctxt);
1470 * xmlDebugDumpNodeList:
1471 * @output: the FILE * for the output
1472 * @node: the node list
1473 * @depth: the indentation level.
1475 * Dumps debug information for the list of element node, it is recursive
1477 void
1478 xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
1480 xmlDebugCtxt ctxt;
1482 if (output == NULL)
1483 output = stdout;
1484 xmlCtxtDumpInitCtxt(&ctxt);
1485 ctxt.output = output;
1486 ctxt.depth = depth;
1487 xmlCtxtDumpNodeList(&ctxt, node);
1488 xmlCtxtDumpCleanCtxt(&ctxt);
1492 * xmlDebugDumpDocumentHead:
1493 * @output: the FILE * for the output
1494 * @doc: the document
1496 * Dumps debug information concerning the document, not recursive
1498 void
1499 xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
1501 xmlDebugCtxt ctxt;
1503 if (output == NULL)
1504 output = stdout;
1505 xmlCtxtDumpInitCtxt(&ctxt);
1506 ctxt.options |= DUMP_TEXT_TYPE;
1507 ctxt.output = output;
1508 xmlCtxtDumpDocumentHead(&ctxt, doc);
1509 xmlCtxtDumpCleanCtxt(&ctxt);
1513 * xmlDebugDumpDocument:
1514 * @output: the FILE * for the output
1515 * @doc: the document
1517 * Dumps debug information for the document, it's recursive
1519 void
1520 xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
1522 xmlDebugCtxt ctxt;
1524 if (output == NULL)
1525 output = stdout;
1526 xmlCtxtDumpInitCtxt(&ctxt);
1527 ctxt.options |= DUMP_TEXT_TYPE;
1528 ctxt.output = output;
1529 xmlCtxtDumpDocument(&ctxt, doc);
1530 xmlCtxtDumpCleanCtxt(&ctxt);
1534 * xmlDebugDumpDTD:
1535 * @output: the FILE * for the output
1536 * @dtd: the DTD
1538 * Dumps debug information for the DTD
1540 void
1541 xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
1543 xmlDebugCtxt ctxt;
1545 if (output == NULL)
1546 output = stdout;
1547 xmlCtxtDumpInitCtxt(&ctxt);
1548 ctxt.options |= DUMP_TEXT_TYPE;
1549 ctxt.output = output;
1550 xmlCtxtDumpDTD(&ctxt, dtd);
1551 xmlCtxtDumpCleanCtxt(&ctxt);
1554 /************************************************************************
1556 * Public entry points for checkings *
1558 ************************************************************************/
1561 * xmlDebugCheckDocument:
1562 * @output: the FILE * for the output
1563 * @doc: the document
1565 * Check the document for potential content problems, and output
1566 * the errors to @output
1568 * Returns the number of errors found
1571 xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
1573 xmlDebugCtxt ctxt;
1575 if (output == NULL)
1576 output = stdout;
1577 xmlCtxtDumpInitCtxt(&ctxt);
1578 ctxt.output = output;
1579 ctxt.check = 1;
1580 xmlCtxtDumpDocument(&ctxt, doc);
1581 xmlCtxtDumpCleanCtxt(&ctxt);
1582 return(ctxt.errors);
1585 /************************************************************************
1587 * Helpers for Shell *
1589 ************************************************************************/
1592 * xmlLsCountNode:
1593 * @node: the node to count
1595 * Count the children of @node.
1597 * Returns the number of children of @node.
1600 xmlLsCountNode(xmlNodePtr node) {
1601 int ret = 0;
1602 xmlNodePtr list = NULL;
1604 if (node == NULL)
1605 return(0);
1607 switch (node->type) {
1608 case XML_ELEMENT_NODE:
1609 list = node->children;
1610 break;
1611 case XML_DOCUMENT_NODE:
1612 case XML_HTML_DOCUMENT_NODE:
1613 list = ((xmlDocPtr) node)->children;
1614 break;
1615 case XML_ATTRIBUTE_NODE:
1616 list = ((xmlAttrPtr) node)->children;
1617 break;
1618 case XML_TEXT_NODE:
1619 case XML_CDATA_SECTION_NODE:
1620 case XML_PI_NODE:
1621 case XML_COMMENT_NODE:
1622 if (node->content != NULL) {
1623 ret = xmlStrlen(node->content);
1625 break;
1626 case XML_ENTITY_REF_NODE:
1627 case XML_DOCUMENT_TYPE_NODE:
1628 case XML_ENTITY_NODE:
1629 case XML_DOCUMENT_FRAG_NODE:
1630 case XML_NOTATION_NODE:
1631 case XML_DTD_NODE:
1632 case XML_ELEMENT_DECL:
1633 case XML_ATTRIBUTE_DECL:
1634 case XML_ENTITY_DECL:
1635 case XML_NAMESPACE_DECL:
1636 case XML_XINCLUDE_START:
1637 case XML_XINCLUDE_END:
1638 ret = 1;
1639 break;
1641 for (;list != NULL;ret++)
1642 list = list->next;
1643 return(ret);
1647 * xmlLsOneNode:
1648 * @output: the FILE * for the output
1649 * @node: the node to dump
1651 * Dump to @output the type and name of @node.
1653 void
1654 xmlLsOneNode(FILE *output, xmlNodePtr node) {
1655 if (output == NULL) return;
1656 if (node == NULL) {
1657 fprintf(output, "NULL\n");
1658 return;
1660 switch (node->type) {
1661 case XML_ELEMENT_NODE:
1662 fprintf(output, "-");
1663 break;
1664 case XML_ATTRIBUTE_NODE:
1665 fprintf(output, "a");
1666 break;
1667 case XML_TEXT_NODE:
1668 fprintf(output, "t");
1669 break;
1670 case XML_CDATA_SECTION_NODE:
1671 fprintf(output, "C");
1672 break;
1673 case XML_ENTITY_REF_NODE:
1674 fprintf(output, "e");
1675 break;
1676 case XML_ENTITY_NODE:
1677 fprintf(output, "E");
1678 break;
1679 case XML_PI_NODE:
1680 fprintf(output, "p");
1681 break;
1682 case XML_COMMENT_NODE:
1683 fprintf(output, "c");
1684 break;
1685 case XML_DOCUMENT_NODE:
1686 fprintf(output, "d");
1687 break;
1688 case XML_HTML_DOCUMENT_NODE:
1689 fprintf(output, "h");
1690 break;
1691 case XML_DOCUMENT_TYPE_NODE:
1692 fprintf(output, "T");
1693 break;
1694 case XML_DOCUMENT_FRAG_NODE:
1695 fprintf(output, "F");
1696 break;
1697 case XML_NOTATION_NODE:
1698 fprintf(output, "N");
1699 break;
1700 case XML_NAMESPACE_DECL:
1701 fprintf(output, "n");
1702 break;
1703 default:
1704 fprintf(output, "?");
1706 if (node->type != XML_NAMESPACE_DECL) {
1707 if (node->properties != NULL)
1708 fprintf(output, "a");
1709 else
1710 fprintf(output, "-");
1711 if (node->nsDef != NULL)
1712 fprintf(output, "n");
1713 else
1714 fprintf(output, "-");
1717 fprintf(output, " %8d ", xmlLsCountNode(node));
1719 switch (node->type) {
1720 case XML_ELEMENT_NODE:
1721 if (node->name != NULL) {
1722 if ((node->ns != NULL) && (node->ns->prefix != NULL))
1723 fprintf(output, "%s:", node->ns->prefix);
1724 fprintf(output, "%s", (const char *) node->name);
1726 break;
1727 case XML_ATTRIBUTE_NODE:
1728 if (node->name != NULL)
1729 fprintf(output, "%s", (const char *) node->name);
1730 break;
1731 case XML_TEXT_NODE:
1732 if (node->content != NULL) {
1733 xmlDebugDumpString(output, node->content);
1735 break;
1736 case XML_CDATA_SECTION_NODE:
1737 break;
1738 case XML_ENTITY_REF_NODE:
1739 if (node->name != NULL)
1740 fprintf(output, "%s", (const char *) node->name);
1741 break;
1742 case XML_ENTITY_NODE:
1743 if (node->name != NULL)
1744 fprintf(output, "%s", (const char *) node->name);
1745 break;
1746 case XML_PI_NODE:
1747 if (node->name != NULL)
1748 fprintf(output, "%s", (const char *) node->name);
1749 break;
1750 case XML_COMMENT_NODE:
1751 break;
1752 case XML_DOCUMENT_NODE:
1753 break;
1754 case XML_HTML_DOCUMENT_NODE:
1755 break;
1756 case XML_DOCUMENT_TYPE_NODE:
1757 break;
1758 case XML_DOCUMENT_FRAG_NODE:
1759 break;
1760 case XML_NOTATION_NODE:
1761 break;
1762 case XML_NAMESPACE_DECL: {
1763 xmlNsPtr ns = (xmlNsPtr) node;
1765 if (ns->prefix == NULL)
1766 fprintf(output, "default -> %s", (char *)ns->href);
1767 else
1768 fprintf(output, "%s -> %s", (char *)ns->prefix,
1769 (char *)ns->href);
1770 break;
1772 default:
1773 if (node->name != NULL)
1774 fprintf(output, "%s", (const char *) node->name);
1776 fprintf(output, "\n");
1780 * xmlBoolToText:
1781 * @boolval: a bool to turn into text
1783 * Convenient way to turn bool into text
1785 * Returns a pointer to either "True" or "False"
1787 const char *
1788 xmlBoolToText(int boolval)
1790 if (boolval)
1791 return("True");
1792 else
1793 return("False");
1796 #ifdef LIBXML_XPATH_ENABLED
1797 /****************************************************************
1799 * The XML shell related functions *
1801 ****************************************************************/
1806 * TODO: Improvement/cleanups for the XML shell
1807 * - allow to shell out an editor on a subpart
1808 * - cleanup function registrations (with help) and calling
1809 * - provide registration routines
1813 * xmlShellPrintXPathError:
1814 * @errorType: valid xpath error id
1815 * @arg: the argument that cause xpath to fail
1817 * Print the xpath error to libxml default error channel
1819 void
1820 xmlShellPrintXPathError(int errorType, const char *arg)
1822 const char *default_arg = "Result";
1824 if (!arg)
1825 arg = default_arg;
1827 switch (errorType) {
1828 case XPATH_UNDEFINED:
1829 xmlGenericError(xmlGenericErrorContext,
1830 "%s: no such node\n", arg);
1831 break;
1833 case XPATH_BOOLEAN:
1834 xmlGenericError(xmlGenericErrorContext,
1835 "%s is a Boolean\n", arg);
1836 break;
1837 case XPATH_NUMBER:
1838 xmlGenericError(xmlGenericErrorContext,
1839 "%s is a number\n", arg);
1840 break;
1841 case XPATH_STRING:
1842 xmlGenericError(xmlGenericErrorContext,
1843 "%s is a string\n", arg);
1844 break;
1845 #ifdef LIBXML_XPTR_LOCS_ENABLED
1846 case XPATH_POINT:
1847 xmlGenericError(xmlGenericErrorContext,
1848 "%s is a point\n", arg);
1849 break;
1850 case XPATH_RANGE:
1851 xmlGenericError(xmlGenericErrorContext,
1852 "%s is a range\n", arg);
1853 break;
1854 case XPATH_LOCATIONSET:
1855 xmlGenericError(xmlGenericErrorContext,
1856 "%s is a range\n", arg);
1857 break;
1858 #endif /* LIBXML_XPTR_LOCS_ENABLED */
1859 case XPATH_USERS:
1860 xmlGenericError(xmlGenericErrorContext,
1861 "%s is user-defined\n", arg);
1862 break;
1863 case XPATH_XSLT_TREE:
1864 xmlGenericError(xmlGenericErrorContext,
1865 "%s is an XSLT value tree\n", arg);
1866 break;
1868 #if 0
1869 xmlGenericError(xmlGenericErrorContext,
1870 "Try casting the result string function (xpath builtin)\n",
1871 arg);
1872 #endif
1876 #ifdef LIBXML_OUTPUT_ENABLED
1878 * xmlShellPrintNodeCtxt:
1879 * @ctxt : a non-null shell context
1880 * @node : a non-null node to print to the output FILE
1882 * Print node to the output FILE
1884 static void
1885 xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1887 FILE *fp;
1889 if (!node)
1890 return;
1891 if (ctxt == NULL)
1892 fp = stdout;
1893 else
1894 fp = ctxt->output;
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);
1903 fprintf(fp, "\n");
1907 * xmlShellPrintNode:
1908 * @node : a non-null node to print to the output FILE
1910 * Print node to the output FILE
1912 void
1913 xmlShellPrintNode(xmlNodePtr node)
1915 xmlShellPrintNodeCtxt(NULL, node);
1917 #endif /* LIBXML_OUTPUT_ENABLED */
1920 * xmlShellPrintXPathResultCtxt:
1921 * @ctxt: a valid shell context
1922 * @list: a valid result generated by an xpath evaluation
1924 * Prints result to the output FILE
1926 static void
1927 xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
1929 if (!ctxt)
1930 return;
1932 if (list != NULL) {
1933 switch (list->type) {
1934 case XPATH_NODESET:{
1935 #ifdef LIBXML_OUTPUT_ENABLED
1936 int indx;
1938 if (list->nodesetval) {
1939 for (indx = 0; indx < list->nodesetval->nodeNr;
1940 indx++) {
1941 xmlShellPrintNodeCtxt(ctxt,
1942 list->nodesetval->nodeTab[indx]);
1944 } else {
1945 xmlGenericError(xmlGenericErrorContext,
1946 "Empty node set\n");
1948 break;
1949 #else
1950 xmlGenericError(xmlGenericErrorContext,
1951 "Node set\n");
1952 #endif /* LIBXML_OUTPUT_ENABLED */
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;
1968 default:
1969 xmlShellPrintXPathError(list->type, NULL);
1975 * xmlShellPrintXPathResult:
1976 * @list: a valid result generated by an xpath evaluation
1978 * Prints result to the output FILE
1980 void
1981 xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1983 xmlShellPrintXPathResultCtxt(NULL, list);
1987 * xmlShellList:
1988 * @ctxt: the shell context
1989 * @arg: unused
1990 * @node: a node
1991 * @node2: unused
1993 * Implements the XML shell function "ls"
1994 * Does an Unix like listing of the given node (like a directory)
1996 * Returns 0
1999 xmlShellList(xmlShellCtxtPtr ctxt,
2000 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2001 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2003 xmlNodePtr cur;
2004 if (!ctxt)
2005 return (0);
2006 if (node == NULL) {
2007 fprintf(ctxt->output, "NULL\n");
2008 return (0);
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);
2022 while (cur != NULL) {
2023 xmlLsOneNode(ctxt->output, cur);
2024 cur = cur->next;
2026 return (0);
2030 * xmlShellBase:
2031 * @ctxt: the shell context
2032 * @arg: unused
2033 * @node: a node
2034 * @node2: unused
2036 * Implements the XML shell function "base"
2037 * dumps the current XML base of the node
2039 * Returns 0
2042 xmlShellBase(xmlShellCtxtPtr ctxt,
2043 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2044 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2046 xmlChar *base;
2047 if (!ctxt)
2048 return 0;
2049 if (node == NULL) {
2050 fprintf(ctxt->output, "NULL\n");
2051 return (0);
2054 base = xmlNodeGetBase(node->doc, node);
2056 if (base == NULL) {
2057 fprintf(ctxt->output, " No base found !!!\n");
2058 } else {
2059 fprintf(ctxt->output, "%s\n", base);
2060 xmlFree(base);
2062 return (0);
2065 #ifdef LIBXML_TREE_ENABLED
2067 * xmlShellSetBase:
2068 * @ctxt: the shell context
2069 * @arg: the new base
2070 * @node: a node
2071 * @node2: unused
2073 * Implements the XML shell function "setbase"
2074 * change the current XML base of the node
2076 * Returns 0
2078 static int
2079 xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2080 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2081 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2083 xmlNodeSetBase(node, (xmlChar*) arg);
2084 return (0);
2086 #endif
2088 #ifdef LIBXML_XPATH_ENABLED
2090 * xmlShellRegisterNamespace:
2091 * @ctxt: the shell context
2092 * @arg: a string in prefix=nsuri format
2093 * @node: unused
2094 * @node2: unused
2096 * Implements the XML shell function "setns"
2097 * register/unregister a prefix=namespace pair
2098 * on the XPath context
2100 * Returns 0 on success and a negative value otherwise.
2102 static int
2103 xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
2104 xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2106 xmlChar* nsListDup;
2107 xmlChar* prefix;
2108 xmlChar* href;
2109 xmlChar* next;
2111 nsListDup = xmlStrdup((xmlChar *) arg);
2112 next = nsListDup;
2113 while(next != NULL) {
2114 /* skip spaces */
2115 /*while((*next) == ' ') next++;*/
2116 if((*next) == '\0') break;
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);
2126 *(next++) = '\0';
2128 /* find href */
2129 href = next;
2130 next = (xmlChar*)xmlStrchr(next, ' ');
2131 if(next != NULL) {
2132 *(next++) = '\0';
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);
2143 xmlFree(nsListDup);
2144 return(0);
2147 * xmlShellRegisterRootNamespaces:
2148 * @ctxt: the shell context
2149 * @arg: unused
2150 * @node: the root element
2151 * @node2: unused
2153 * Implements the XML shell function "setrootns"
2154 * which registers all namespaces declarations found on the root element.
2156 * Returns 0 on success and a negative value otherwise.
2158 static int
2159 xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2160 xmlNodePtr root, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2162 xmlNsPtr ns;
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;
2175 return(0);
2177 #endif
2180 * xmlShellGrep:
2181 * @ctxt: the shell context
2182 * @arg: the string or regular expression to find
2183 * @node: a node
2184 * @node2: unused
2186 * Implements the XML shell function "grep"
2187 * dumps information about the node (namespace, attributes, content).
2189 * Returns 0
2191 static int
2192 xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2193 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
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, '['))) {
2207 #endif
2208 while (node != NULL) {
2209 if (node->type == XML_COMMENT_NODE) {
2210 if (xmlStrstr(node->content, (xmlChar *) arg)) {
2212 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
2213 xmlShellList(ctxt, NULL, node, NULL);
2215 } else if (node->type == XML_TEXT_NODE) {
2216 if (xmlStrstr(node->content, (xmlChar *) arg)) {
2218 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
2219 xmlShellList(ctxt, NULL, node->parent, NULL);
2224 * Browse the full subtree, deep first
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;
2243 if (node->next != NULL) {
2244 node = node->next;
2245 break;
2247 if (node->parent == NULL) {
2248 node = NULL;
2249 break;
2254 return (0);
2258 * xmlShellDir:
2259 * @ctxt: the shell context
2260 * @arg: unused
2261 * @node: a node
2262 * @node2: unused
2264 * Implements the XML shell function "dir"
2265 * dumps information about the node (namespace, attributes, content).
2267 * Returns 0
2270 xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2271 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2272 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2274 if (!ctxt)
2275 return (0);
2276 if (node == NULL) {
2277 fprintf(ctxt->output, "NULL\n");
2278 return (0);
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);
2288 return (0);
2292 * xmlShellSetContent:
2293 * @ctxt: the shell context
2294 * @value: the content as a string
2295 * @node: a node
2296 * @node2: unused
2298 * Implements the XML shell function "dir"
2299 * dumps information about the node (namespace, attributes, content).
2301 * Returns 0
2303 static int
2304 xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2305 char *value, xmlNodePtr node,
2306 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2308 xmlNodePtr results;
2309 xmlParserErrors ret;
2311 if (!ctxt)
2312 return (0);
2313 if (node == NULL) {
2314 fprintf(ctxt->output, "NULL\n");
2315 return (0);
2317 if (value == NULL) {
2318 fprintf(ctxt->output, "NULL\n");
2319 return (0);
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;
2329 xmlAddChildList(node, results);
2330 } else {
2331 fprintf(ctxt->output, "failed to parse content\n");
2333 return (0);
2336 #ifdef LIBXML_SCHEMAS_ENABLED
2338 * xmlShellRNGValidate:
2339 * @ctxt: the shell context
2340 * @schemas: the path to the Relax-NG schemas
2341 * @node: a node
2342 * @node2: unused
2344 * Implements the XML shell function "relaxng"
2345 * validating the instance against a Relax-NG schemas
2347 * Returns 0
2349 static int
2350 xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
2351 xmlNodePtr node ATTRIBUTE_UNUSED,
2352 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2354 xmlRelaxNGPtr relaxngschemas;
2355 xmlRelaxNGParserCtxtPtr ctxt;
2356 xmlRelaxNGValidCtxtPtr vctxt;
2357 int ret;
2359 ctxt = xmlRelaxNGNewParserCtxt(schemas);
2360 xmlRelaxNGSetParserErrors(ctxt, xmlGenericError, xmlGenericError, NULL);
2361 relaxngschemas = xmlRelaxNGParse(ctxt);
2362 xmlRelaxNGFreeParserCtxt(ctxt);
2363 if (relaxngschemas == NULL) {
2364 xmlGenericError(xmlGenericErrorContext,
2365 "Relax-NG schema %s failed to compile\n", schemas);
2366 return(-1);
2368 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2369 xmlRelaxNGSetValidErrors(vctxt, xmlGenericError, xmlGenericError, NULL);
2370 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2371 if (ret == 0) {
2372 fprintf(stderr, "%s validates\n", sctxt->filename);
2373 } else if (ret > 0) {
2374 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2375 } else {
2376 fprintf(stderr, "%s validation generated an internal error\n",
2377 sctxt->filename);
2379 xmlRelaxNGFreeValidCtxt(vctxt);
2380 if (relaxngschemas != NULL)
2381 xmlRelaxNGFree(relaxngschemas);
2382 return(0);
2384 #endif
2386 #ifdef LIBXML_OUTPUT_ENABLED
2388 * xmlShellCat:
2389 * @ctxt: the shell context
2390 * @arg: unused
2391 * @node: a node
2392 * @node2: unused
2394 * Implements the XML shell function "cat"
2395 * dumps the serialization node content (XML or HTML).
2397 * Returns 0
2400 xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2401 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2403 if (!ctxt)
2404 return (0);
2405 if (node == NULL) {
2406 fprintf(ctxt->output, "NULL\n");
2407 return (0);
2409 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2410 #ifdef LIBXML_HTML_ENABLED
2411 if (node->type == XML_HTML_DOCUMENT_NODE)
2412 htmlDocDump(ctxt->output, (htmlDocPtr) node);
2413 else
2414 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
2415 #else
2416 if (node->type == XML_DOCUMENT_NODE)
2417 xmlDocDump(ctxt->output, (xmlDocPtr) node);
2418 else
2419 xmlElemDump(ctxt->output, ctxt->doc, node);
2420 #endif /* LIBXML_HTML_ENABLED */
2421 } else {
2422 if (node->type == XML_DOCUMENT_NODE)
2423 xmlDocDump(ctxt->output, (xmlDocPtr) node);
2424 else
2425 xmlElemDump(ctxt->output, ctxt->doc, node);
2427 fprintf(ctxt->output, "\n");
2428 return (0);
2430 #endif /* LIBXML_OUTPUT_ENABLED */
2433 * xmlShellLoad:
2434 * @ctxt: the shell context
2435 * @filename: the file name
2436 * @node: unused
2437 * @node2: unused
2439 * Implements the XML shell function "load"
2440 * loads a new document specified by the filename
2442 * Returns 0 or -1 if loading failed
2445 xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2446 xmlNodePtr node ATTRIBUTE_UNUSED,
2447 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2449 xmlDocPtr doc;
2450 int html = 0;
2452 if ((ctxt == NULL) || (filename == NULL)) return(-1);
2453 if (ctxt->doc != NULL)
2454 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
2456 if (html) {
2457 #ifdef LIBXML_HTML_ENABLED
2458 doc = htmlParseFile(filename, NULL);
2459 #else
2460 fprintf(ctxt->output, "HTML support not compiled in\n");
2461 doc = NULL;
2462 #endif /* LIBXML_HTML_ENABLED */
2463 } else {
2464 doc = xmlReadFile(filename,NULL,0);
2466 if (doc != NULL) {
2467 if (ctxt->loaded == 1) {
2468 xmlFreeDoc(ctxt->doc);
2470 ctxt->loaded = 1;
2471 #ifdef LIBXML_XPATH_ENABLED
2472 xmlXPathFreeContext(ctxt->pctxt);
2473 #endif /* LIBXML_XPATH_ENABLED */
2474 xmlFree(ctxt->filename);
2475 ctxt->doc = doc;
2476 ctxt->node = (xmlNodePtr) doc;
2477 #ifdef LIBXML_XPATH_ENABLED
2478 ctxt->pctxt = xmlXPathNewContext(doc);
2479 #endif /* LIBXML_XPATH_ENABLED */
2480 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
2481 } else
2482 return (-1);
2483 return (0);
2486 #ifdef LIBXML_OUTPUT_ENABLED
2488 * xmlShellWrite:
2489 * @ctxt: the shell context
2490 * @filename: the file name
2491 * @node: a node in the tree
2492 * @node2: unused
2494 * Implements the XML shell function "write"
2495 * Write the current node to the filename, it saves the serialization
2496 * of the subtree under the @node specified
2498 * Returns 0 or -1 in case of error
2501 xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
2502 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2504 if (node == NULL)
2505 return (-1);
2506 if ((filename == NULL) || (filename[0] == 0)) {
2507 return (-1);
2509 #ifdef W_OK
2510 if (access((char *) filename, W_OK)) {
2511 xmlGenericError(xmlGenericErrorContext,
2512 "Cannot write to %s\n", filename);
2513 return (-1);
2515 #endif
2516 switch (node->type) {
2517 case XML_DOCUMENT_NODE:
2518 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2519 xmlGenericError(xmlGenericErrorContext,
2520 "Failed to write to %s\n", filename);
2521 return (-1);
2523 break;
2524 case XML_HTML_DOCUMENT_NODE:
2525 #ifdef LIBXML_HTML_ENABLED
2526 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2527 xmlGenericError(xmlGenericErrorContext,
2528 "Failed to write to %s\n", filename);
2529 return (-1);
2531 #else
2532 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2533 xmlGenericError(xmlGenericErrorContext,
2534 "Failed to write to %s\n", filename);
2535 return (-1);
2537 #endif /* LIBXML_HTML_ENABLED */
2538 break;
2539 default:{
2540 FILE *f;
2542 f = fopen((char *) filename, "w");
2543 if (f == NULL) {
2544 xmlGenericError(xmlGenericErrorContext,
2545 "Failed to write to %s\n", filename);
2546 return (-1);
2548 xmlElemDump(f, ctxt->doc, node);
2549 fclose(f);
2552 return (0);
2556 * xmlShellSave:
2557 * @ctxt: the shell context
2558 * @filename: the file name (optional)
2559 * @node: unused
2560 * @node2: unused
2562 * Implements the XML shell function "save"
2563 * Write the current document to the filename, or it's original name
2565 * Returns 0 or -1 in case of error
2568 xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2569 xmlNodePtr node ATTRIBUTE_UNUSED,
2570 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2572 if ((ctxt == NULL) || (ctxt->doc == NULL))
2573 return (-1);
2574 if ((filename == NULL) || (filename[0] == 0))
2575 filename = ctxt->filename;
2576 if (filename == NULL)
2577 return (-1);
2578 #ifdef W_OK
2579 if (access((char *) filename, W_OK)) {
2580 xmlGenericError(xmlGenericErrorContext,
2581 "Cannot save to %s\n", filename);
2582 return (-1);
2584 #endif
2585 switch (ctxt->doc->type) {
2586 case XML_DOCUMENT_NODE:
2587 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2588 xmlGenericError(xmlGenericErrorContext,
2589 "Failed to save to %s\n", filename);
2591 break;
2592 case XML_HTML_DOCUMENT_NODE:
2593 #ifdef LIBXML_HTML_ENABLED
2594 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2595 xmlGenericError(xmlGenericErrorContext,
2596 "Failed to save to %s\n", filename);
2598 #else
2599 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2600 xmlGenericError(xmlGenericErrorContext,
2601 "Failed to save to %s\n", filename);
2603 #endif /* LIBXML_HTML_ENABLED */
2604 break;
2605 default:
2606 xmlGenericError(xmlGenericErrorContext,
2607 "To save to subparts of a document use the 'write' command\n");
2608 return (-1);
2611 return (0);
2613 #endif /* LIBXML_OUTPUT_ENABLED */
2615 #ifdef LIBXML_VALID_ENABLED
2617 * xmlShellValidate:
2618 * @ctxt: the shell context
2619 * @dtd: the DTD URI (optional)
2620 * @node: unused
2621 * @node2: unused
2623 * Implements the XML shell function "validate"
2624 * Validate the document, if a DTD path is provided, then the validation
2625 * is done against the given DTD.
2627 * Returns 0 or -1 in case of error
2630 xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2631 xmlNodePtr node ATTRIBUTE_UNUSED,
2632 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2634 xmlValidCtxt vctxt;
2635 int res = -1;
2637 if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
2638 memset(&vctxt, 0, sizeof(vctxt));
2639 vctxt.error = xmlGenericError;
2640 vctxt.warning = xmlGenericError;
2642 if ((dtd == NULL) || (dtd[0] == 0)) {
2643 res = xmlValidateDocument(&vctxt, ctxt->doc);
2644 } else {
2645 xmlDtdPtr subset;
2647 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2648 if (subset != NULL) {
2649 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2651 xmlFreeDtd(subset);
2654 return (res);
2656 #endif /* LIBXML_VALID_ENABLED */
2659 * xmlShellDu:
2660 * @ctxt: the shell context
2661 * @arg: unused
2662 * @tree: a node defining a subtree
2663 * @node2: unused
2665 * Implements the XML shell function "du"
2666 * show the structure of the subtree under node @tree
2667 * If @tree is null, the command works on the current node.
2669 * Returns 0 or -1 in case of error
2672 xmlShellDu(xmlShellCtxtPtr ctxt,
2673 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2674 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2676 xmlNodePtr node;
2677 int indent = 0, i;
2679 if (!ctxt)
2680 return (-1);
2682 if (tree == NULL)
2683 return (-1);
2684 node = tree;
2685 while (node != NULL) {
2686 if ((node->type == XML_DOCUMENT_NODE) ||
2687 (node->type == XML_HTML_DOCUMENT_NODE)) {
2688 fprintf(ctxt->output, "/\n");
2689 } else if (node->type == XML_ELEMENT_NODE) {
2690 for (i = 0; i < indent; i++)
2691 fprintf(ctxt->output, " ");
2692 if ((node->ns) && (node->ns->prefix))
2693 fprintf(ctxt->output, "%s:", node->ns->prefix);
2694 fprintf(ctxt->output, "%s\n", node->name);
2695 } else {
2699 * Browse the full subtree, deep first
2702 if ((node->type == XML_DOCUMENT_NODE) ||
2703 (node->type == XML_HTML_DOCUMENT_NODE)) {
2704 node = ((xmlDocPtr) node)->children;
2705 } else if ((node->children != NULL)
2706 && (node->type != XML_ENTITY_REF_NODE)) {
2707 /* deep first */
2708 node = node->children;
2709 indent++;
2710 } else if ((node != tree) && (node->next != NULL)) {
2711 /* then siblings */
2712 node = node->next;
2713 } else if (node != tree) {
2714 /* go up to parents->next if needed */
2715 while (node != tree) {
2716 if (node->parent != NULL) {
2717 node = node->parent;
2718 indent--;
2720 if ((node != tree) && (node->next != NULL)) {
2721 node = node->next;
2722 break;
2724 if (node->parent == NULL) {
2725 node = NULL;
2726 break;
2728 if (node == tree) {
2729 node = NULL;
2730 break;
2733 /* exit condition */
2734 if (node == tree)
2735 node = NULL;
2736 } else
2737 node = NULL;
2739 return (0);
2743 * xmlShellPwd:
2744 * @ctxt: the shell context
2745 * @buffer: the output buffer
2746 * @node: a node
2747 * @node2: unused
2749 * Implements the XML shell function "pwd"
2750 * Show the full path from the root to the node, if needed building
2751 * thumblers when similar elements exists at a given ancestor level.
2752 * The output is compatible with XPath commands.
2754 * Returns 0 or -1 in case of error
2757 xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2758 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2760 xmlChar *path;
2762 if ((node == NULL) || (buffer == NULL))
2763 return (-1);
2765 path = xmlGetNodePath(node);
2766 if (path == NULL)
2767 return (-1);
2770 * This test prevents buffer overflow, because this routine
2771 * is only called by xmlShell, in which the second argument is
2772 * 500 chars long.
2773 * It is a dirty hack before a cleaner solution is found.
2774 * Documentation should mention that the second argument must
2775 * be at least 500 chars long, and could be stripped if too long.
2777 snprintf(buffer, 499, "%s", path);
2778 buffer[499] = '0';
2779 xmlFree(path);
2781 return (0);
2785 * xmlShell:
2786 * @doc: the initial document
2787 * @filename: the output buffer
2788 * @input: the line reading function
2789 * @output: the output FILE*, defaults to stdout if NULL
2791 * Implements the XML shell
2792 * This allow to load, validate, view, modify and save a document
2793 * using a environment similar to a UNIX commandline.
2795 void
2796 xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
2797 FILE * output)
2799 char prompt[500] = "/ > ";
2800 char *cmdline = NULL, *cur;
2801 char command[100];
2802 char arg[400];
2803 int i;
2804 xmlShellCtxtPtr ctxt;
2805 xmlXPathObjectPtr list;
2807 if (doc == NULL)
2808 return;
2809 if (filename == NULL)
2810 return;
2811 if (input == NULL)
2812 return;
2813 if (output == NULL)
2814 output = stdout;
2815 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
2816 if (ctxt == NULL)
2817 return;
2818 ctxt->loaded = 0;
2819 ctxt->doc = doc;
2820 ctxt->input = input;
2821 ctxt->output = output;
2822 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
2823 ctxt->node = (xmlNodePtr) ctxt->doc;
2825 #ifdef LIBXML_XPATH_ENABLED
2826 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2827 if (ctxt->pctxt == NULL) {
2828 xmlFree(ctxt);
2829 return;
2831 #endif /* LIBXML_XPATH_ENABLED */
2832 while (1) {
2833 if (ctxt->node == (xmlNodePtr) ctxt->doc)
2834 snprintf(prompt, sizeof(prompt), "%s > ", "/");
2835 else if ((ctxt->node != NULL) && (ctxt->node->name) &&
2836 (ctxt->node->ns) && (ctxt->node->ns->prefix))
2837 snprintf(prompt, sizeof(prompt), "%s:%s > ",
2838 (ctxt->node->ns->prefix), ctxt->node->name);
2839 else if ((ctxt->node != NULL) && (ctxt->node->name))
2840 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
2841 else
2842 snprintf(prompt, sizeof(prompt), "? > ");
2843 prompt[sizeof(prompt) - 1] = 0;
2846 * Get a new command line
2848 cmdline = ctxt->input(prompt);
2849 if (cmdline == NULL)
2850 break;
2853 * Parse the command itself
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++;
2865 command[i] = 0;
2866 if (i == 0)
2867 continue;
2870 * Parse the argument
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++;
2880 arg[i] = 0;
2883 * start interpreting the command
2885 if (!strcmp(command, "exit"))
2886 break;
2887 if (!strcmp(command, "quit"))
2888 break;
2889 if (!strcmp(command, "bye"))
2890 break;
2891 if (!strcmp(command, "help")) {
2892 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2893 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2894 fprintf(ctxt->output, "\tbye leave shell\n");
2895 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2896 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2897 fprintf(ctxt->output, "\tdir [path] dumps information about the node (namespace, attributes, content)\n");
2898 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2899 fprintf(ctxt->output, "\texit leave shell\n");
2900 fprintf(ctxt->output, "\thelp display this help\n");
2901 fprintf(ctxt->output, "\tfree display memory usage\n");
2902 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2903 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
2904 fprintf(ctxt->output, "\tset xml_fragment replace the current node content with the fragment parsed in context\n");
2905 #ifdef LIBXML_XPATH_ENABLED
2906 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
2907 fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2908 fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
2909 fprintf(ctxt->output, "\tsetrootns register all namespace found on the root element\n");
2910 fprintf(ctxt->output, "\t the default namespace if any uses 'defaultns' prefix\n");
2911 #endif /* LIBXML_XPATH_ENABLED */
2912 fprintf(ctxt->output, "\tpwd display current working directory\n");
2913 fprintf(ctxt->output, "\twhereis display absolute path of [path] or current working directory\n");
2914 fprintf(ctxt->output, "\tquit leave shell\n");
2915 #ifdef LIBXML_OUTPUT_ENABLED
2916 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
2917 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
2918 #endif /* LIBXML_OUTPUT_ENABLED */
2919 #ifdef LIBXML_VALID_ENABLED
2920 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
2921 #endif /* LIBXML_VALID_ENABLED */
2922 #ifdef LIBXML_SCHEMAS_ENABLED
2923 fprintf(ctxt->output, "\trelaxng rng validate the document against the Relax-NG schemas\n");
2924 #endif
2925 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
2926 #ifdef LIBXML_VALID_ENABLED
2927 } else if (!strcmp(command, "validate")) {
2928 xmlShellValidate(ctxt, arg, NULL, NULL);
2929 #endif /* LIBXML_VALID_ENABLED */
2930 } else if (!strcmp(command, "load")) {
2931 xmlShellLoad(ctxt, arg, NULL, NULL);
2932 #ifdef LIBXML_SCHEMAS_ENABLED
2933 } else if (!strcmp(command, "relaxng")) {
2934 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2935 #endif
2936 #ifdef LIBXML_OUTPUT_ENABLED
2937 } else if (!strcmp(command, "save")) {
2938 xmlShellSave(ctxt, arg, NULL, NULL);
2939 } else if (!strcmp(command, "write")) {
2940 if (arg[0] == 0)
2941 xmlGenericError(xmlGenericErrorContext,
2942 "Write command requires a filename argument\n");
2943 else
2944 xmlShellWrite(ctxt, arg, ctxt->node, NULL);
2945 #endif /* LIBXML_OUTPUT_ENABLED */
2946 } else if (!strcmp(command, "grep")) {
2947 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
2948 } else if (!strcmp(command, "free")) {
2949 if (arg[0] == 0) {
2950 xmlMemShow(ctxt->output, 0);
2951 } else {
2952 int len = 0;
2954 sscanf(arg, "%d", &len);
2955 xmlMemShow(ctxt->output, len);
2957 } else if (!strcmp(command, "pwd")) {
2958 char dir[500];
2960 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
2961 fprintf(ctxt->output, "%s\n", dir);
2962 } else if (!strcmp(command, "du")) {
2963 if (arg[0] == 0) {
2964 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2965 } else {
2966 ctxt->pctxt->node = ctxt->node;
2967 #ifdef LIBXML_XPATH_ENABLED
2968 ctxt->pctxt->node = ctxt->node;
2969 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2970 #else
2971 list = NULL;
2972 #endif /* LIBXML_XPATH_ENABLED */
2973 if (list != NULL) {
2974 switch (list->type) {
2975 case XPATH_UNDEFINED:
2976 xmlGenericError(xmlGenericErrorContext,
2977 "%s: no such node\n", arg);
2978 break;
2979 case XPATH_NODESET:{
2980 int indx;
2982 if (list->nodesetval == NULL)
2983 break;
2985 for (indx = 0;
2986 indx < list->nodesetval->nodeNr;
2987 indx++)
2988 xmlShellDu(ctxt, NULL,
2989 list->nodesetval->
2990 nodeTab[indx], NULL);
2991 break;
2993 case XPATH_BOOLEAN:
2994 xmlGenericError(xmlGenericErrorContext,
2995 "%s is a Boolean\n", arg);
2996 break;
2997 case XPATH_NUMBER:
2998 xmlGenericError(xmlGenericErrorContext,
2999 "%s is a number\n", arg);
3000 break;
3001 case XPATH_STRING:
3002 xmlGenericError(xmlGenericErrorContext,
3003 "%s is a string\n", arg);
3004 break;
3005 #ifdef LIBXML_XPTR_LOCS_ENABLED
3006 case XPATH_POINT:
3007 xmlGenericError(xmlGenericErrorContext,
3008 "%s is a point\n", arg);
3009 break;
3010 case XPATH_RANGE:
3011 xmlGenericError(xmlGenericErrorContext,
3012 "%s is a range\n", arg);
3013 break;
3014 case XPATH_LOCATIONSET:
3015 xmlGenericError(xmlGenericErrorContext,
3016 "%s is a range\n", arg);
3017 break;
3018 #endif /* LIBXML_XPTR_LOCS_ENABLED */
3019 case XPATH_USERS:
3020 xmlGenericError(xmlGenericErrorContext,
3021 "%s is user-defined\n", arg);
3022 break;
3023 case XPATH_XSLT_TREE:
3024 xmlGenericError(xmlGenericErrorContext,
3025 "%s is an XSLT value tree\n",
3026 arg);
3027 break;
3029 #ifdef LIBXML_XPATH_ENABLED
3030 xmlXPathFreeObject(list);
3031 #endif
3032 } else {
3033 xmlGenericError(xmlGenericErrorContext,
3034 "%s: no such node\n", arg);
3036 ctxt->pctxt->node = NULL;
3038 } else if (!strcmp(command, "base")) {
3039 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
3040 } else if (!strcmp(command, "set")) {
3041 xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
3042 #ifdef LIBXML_XPATH_ENABLED
3043 } else if (!strcmp(command, "setns")) {
3044 if (arg[0] == 0) {
3045 xmlGenericError(xmlGenericErrorContext,
3046 "setns: prefix=[nsuri] required\n");
3047 } else {
3048 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
3050 } else if (!strcmp(command, "setrootns")) {
3051 xmlNodePtr root;
3053 root = xmlDocGetRootElement(ctxt->doc);
3054 xmlShellRegisterRootNamespaces(ctxt, NULL, root, NULL);
3055 } else if (!strcmp(command, "xpath")) {
3056 if (arg[0] == 0) {
3057 xmlGenericError(xmlGenericErrorContext,
3058 "xpath: expression required\n");
3059 } else {
3060 ctxt->pctxt->node = ctxt->node;
3061 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3062 xmlXPathDebugDumpObject(ctxt->output, list, 0);
3063 xmlXPathFreeObject(list);
3065 #endif /* LIBXML_XPATH_ENABLED */
3066 #ifdef LIBXML_TREE_ENABLED
3067 } else if (!strcmp(command, "setbase")) {
3068 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
3069 #endif
3070 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
3071 int dir = (!strcmp(command, "dir"));
3073 if (arg[0] == 0) {
3074 if (dir)
3075 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
3076 else
3077 xmlShellList(ctxt, NULL, ctxt->node, NULL);
3078 } else {
3079 ctxt->pctxt->node = ctxt->node;
3080 #ifdef LIBXML_XPATH_ENABLED
3081 ctxt->pctxt->node = ctxt->node;
3082 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3083 #else
3084 list = NULL;
3085 #endif /* LIBXML_XPATH_ENABLED */
3086 if (list != NULL) {
3087 switch (list->type) {
3088 case XPATH_UNDEFINED:
3089 xmlGenericError(xmlGenericErrorContext,
3090 "%s: no such node\n", arg);
3091 break;
3092 case XPATH_NODESET:{
3093 int indx;
3095 if (list->nodesetval == NULL)
3096 break;
3098 for (indx = 0;
3099 indx < list->nodesetval->nodeNr;
3100 indx++) {
3101 if (dir)
3102 xmlShellDir(ctxt, NULL,
3103 list->nodesetval->
3104 nodeTab[indx], NULL);
3105 else
3106 xmlShellList(ctxt, NULL,
3107 list->nodesetval->
3108 nodeTab[indx], NULL);
3110 break;
3112 case XPATH_BOOLEAN:
3113 xmlGenericError(xmlGenericErrorContext,
3114 "%s is a Boolean\n", arg);
3115 break;
3116 case XPATH_NUMBER:
3117 xmlGenericError(xmlGenericErrorContext,
3118 "%s is a number\n", arg);
3119 break;
3120 case XPATH_STRING:
3121 xmlGenericError(xmlGenericErrorContext,
3122 "%s is a string\n", arg);
3123 break;
3124 #ifdef LIBXML_XPTR_LOCS_ENABLED
3125 case XPATH_POINT:
3126 xmlGenericError(xmlGenericErrorContext,
3127 "%s is a point\n", arg);
3128 break;
3129 case XPATH_RANGE:
3130 xmlGenericError(xmlGenericErrorContext,
3131 "%s is a range\n", arg);
3132 break;
3133 case XPATH_LOCATIONSET:
3134 xmlGenericError(xmlGenericErrorContext,
3135 "%s is a range\n", arg);
3136 break;
3137 #endif /* LIBXML_XPTR_LOCS_ENABLED */
3138 case XPATH_USERS:
3139 xmlGenericError(xmlGenericErrorContext,
3140 "%s is user-defined\n", arg);
3141 break;
3142 case XPATH_XSLT_TREE:
3143 xmlGenericError(xmlGenericErrorContext,
3144 "%s is an XSLT value tree\n",
3145 arg);
3146 break;
3148 #ifdef LIBXML_XPATH_ENABLED
3149 xmlXPathFreeObject(list);
3150 #endif
3151 } else {
3152 xmlGenericError(xmlGenericErrorContext,
3153 "%s: no such node\n", arg);
3155 ctxt->pctxt->node = NULL;
3157 } else if (!strcmp(command, "whereis")) {
3158 char dir[500];
3160 if (arg[0] == 0) {
3161 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
3162 fprintf(ctxt->output, "%s\n", dir);
3163 } else {
3164 ctxt->pctxt->node = ctxt->node;
3165 #ifdef LIBXML_XPATH_ENABLED
3166 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3167 #else
3168 list = NULL;
3169 #endif /* LIBXML_XPATH_ENABLED */
3170 if (list != NULL) {
3171 switch (list->type) {
3172 case XPATH_UNDEFINED:
3173 xmlGenericError(xmlGenericErrorContext,
3174 "%s: no such node\n", arg);
3175 break;
3176 case XPATH_NODESET:{
3177 int indx;
3179 if (list->nodesetval == NULL)
3180 break;
3182 for (indx = 0;
3183 indx < list->nodesetval->nodeNr;
3184 indx++) {
3185 if (!xmlShellPwd(ctxt, dir, list->nodesetval->
3186 nodeTab[indx], NULL))
3187 fprintf(ctxt->output, "%s\n", dir);
3189 break;
3191 case XPATH_BOOLEAN:
3192 xmlGenericError(xmlGenericErrorContext,
3193 "%s is a Boolean\n", arg);
3194 break;
3195 case XPATH_NUMBER:
3196 xmlGenericError(xmlGenericErrorContext,
3197 "%s is a number\n", arg);
3198 break;
3199 case XPATH_STRING:
3200 xmlGenericError(xmlGenericErrorContext,
3201 "%s is a string\n", arg);
3202 break;
3203 #ifdef LIBXML_XPTR_LOCS_ENABLED
3204 case XPATH_POINT:
3205 xmlGenericError(xmlGenericErrorContext,
3206 "%s is a point\n", arg);
3207 break;
3208 case XPATH_RANGE:
3209 xmlGenericError(xmlGenericErrorContext,
3210 "%s is a range\n", arg);
3211 break;
3212 case XPATH_LOCATIONSET:
3213 xmlGenericError(xmlGenericErrorContext,
3214 "%s is a range\n", arg);
3215 break;
3216 #endif /* LIBXML_XPTR_LOCS_ENABLED */
3217 case XPATH_USERS:
3218 xmlGenericError(xmlGenericErrorContext,
3219 "%s is user-defined\n", arg);
3220 break;
3221 case XPATH_XSLT_TREE:
3222 xmlGenericError(xmlGenericErrorContext,
3223 "%s is an XSLT value tree\n",
3224 arg);
3225 break;
3227 #ifdef LIBXML_XPATH_ENABLED
3228 xmlXPathFreeObject(list);
3229 #endif
3230 } else {
3231 xmlGenericError(xmlGenericErrorContext,
3232 "%s: no such node\n", arg);
3234 ctxt->pctxt->node = NULL;
3236 } else if (!strcmp(command, "cd")) {
3237 if (arg[0] == 0) {
3238 ctxt->node = (xmlNodePtr) ctxt->doc;
3239 } else {
3240 #ifdef LIBXML_XPATH_ENABLED
3241 int l;
3243 ctxt->pctxt->node = ctxt->node;
3244 l = strlen(arg);
3245 if ((l >= 2) && (arg[l - 1] == '/'))
3246 arg[l - 1] = 0;
3247 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3248 #else
3249 list = NULL;
3250 #endif /* LIBXML_XPATH_ENABLED */
3251 if (list != NULL) {
3252 switch (list->type) {
3253 case XPATH_UNDEFINED:
3254 xmlGenericError(xmlGenericErrorContext,
3255 "%s: no such node\n", arg);
3256 break;
3257 case XPATH_NODESET:
3258 if (list->nodesetval != NULL) {
3259 if (list->nodesetval->nodeNr == 1) {
3260 ctxt->node = list->nodesetval->nodeTab[0];
3261 if ((ctxt->node != NULL) &&
3262 (ctxt->node->type ==
3263 XML_NAMESPACE_DECL)) {
3264 xmlGenericError(xmlGenericErrorContext,
3265 "cannot cd to namespace\n");
3266 ctxt->node = NULL;
3268 } else
3269 xmlGenericError(xmlGenericErrorContext,
3270 "%s is a %d Node Set\n",
3271 arg,
3272 list->nodesetval->nodeNr);
3273 } else
3274 xmlGenericError(xmlGenericErrorContext,
3275 "%s is an empty Node Set\n",
3276 arg);
3277 break;
3278 case XPATH_BOOLEAN:
3279 xmlGenericError(xmlGenericErrorContext,
3280 "%s is a Boolean\n", arg);
3281 break;
3282 case XPATH_NUMBER:
3283 xmlGenericError(xmlGenericErrorContext,
3284 "%s is a number\n", arg);
3285 break;
3286 case XPATH_STRING:
3287 xmlGenericError(xmlGenericErrorContext,
3288 "%s is a string\n", arg);
3289 break;
3290 #ifdef LIBXML_XPTR_LOCS_ENABLED
3291 case XPATH_POINT:
3292 xmlGenericError(xmlGenericErrorContext,
3293 "%s is a point\n", arg);
3294 break;
3295 case XPATH_RANGE:
3296 xmlGenericError(xmlGenericErrorContext,
3297 "%s is a range\n", arg);
3298 break;
3299 case XPATH_LOCATIONSET:
3300 xmlGenericError(xmlGenericErrorContext,
3301 "%s is a range\n", arg);
3302 break;
3303 #endif /* LIBXML_XPTR_LOCS_ENABLED */
3304 case XPATH_USERS:
3305 xmlGenericError(xmlGenericErrorContext,
3306 "%s is user-defined\n", arg);
3307 break;
3308 case XPATH_XSLT_TREE:
3309 xmlGenericError(xmlGenericErrorContext,
3310 "%s is an XSLT value tree\n",
3311 arg);
3312 break;
3314 #ifdef LIBXML_XPATH_ENABLED
3315 xmlXPathFreeObject(list);
3316 #endif
3317 } else {
3318 xmlGenericError(xmlGenericErrorContext,
3319 "%s: no such node\n", arg);
3321 ctxt->pctxt->node = NULL;
3323 #ifdef LIBXML_OUTPUT_ENABLED
3324 } else if (!strcmp(command, "cat")) {
3325 if (arg[0] == 0) {
3326 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3327 } else {
3328 ctxt->pctxt->node = ctxt->node;
3329 #ifdef LIBXML_XPATH_ENABLED
3330 ctxt->pctxt->node = ctxt->node;
3331 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3332 #else
3333 list = NULL;
3334 #endif /* LIBXML_XPATH_ENABLED */
3335 if (list != NULL) {
3336 switch (list->type) {
3337 case XPATH_UNDEFINED:
3338 xmlGenericError(xmlGenericErrorContext,
3339 "%s: no such node\n", arg);
3340 break;
3341 case XPATH_NODESET:{
3342 int indx;
3344 if (list->nodesetval == NULL)
3345 break;
3347 for (indx = 0;
3348 indx < list->nodesetval->nodeNr;
3349 indx++) {
3350 if (i > 0)
3351 fprintf(ctxt->output, " -------\n");
3352 xmlShellCat(ctxt, NULL,
3353 list->nodesetval->
3354 nodeTab[indx], NULL);
3356 break;
3358 case XPATH_BOOLEAN:
3359 xmlGenericError(xmlGenericErrorContext,
3360 "%s is a Boolean\n", arg);
3361 break;
3362 case XPATH_NUMBER:
3363 xmlGenericError(xmlGenericErrorContext,
3364 "%s is a number\n", arg);
3365 break;
3366 case XPATH_STRING:
3367 xmlGenericError(xmlGenericErrorContext,
3368 "%s is a string\n", arg);
3369 break;
3370 #ifdef LIBXML_XPTR_LOCS_ENABLED
3371 case XPATH_POINT:
3372 xmlGenericError(xmlGenericErrorContext,
3373 "%s is a point\n", arg);
3374 break;
3375 case XPATH_RANGE:
3376 xmlGenericError(xmlGenericErrorContext,
3377 "%s is a range\n", arg);
3378 break;
3379 case XPATH_LOCATIONSET:
3380 xmlGenericError(xmlGenericErrorContext,
3381 "%s is a range\n", arg);
3382 break;
3383 #endif /* LIBXML_XPTR_LOCS_ENABLED */
3384 case XPATH_USERS:
3385 xmlGenericError(xmlGenericErrorContext,
3386 "%s is user-defined\n", arg);
3387 break;
3388 case XPATH_XSLT_TREE:
3389 xmlGenericError(xmlGenericErrorContext,
3390 "%s is an XSLT value tree\n",
3391 arg);
3392 break;
3394 #ifdef LIBXML_XPATH_ENABLED
3395 xmlXPathFreeObject(list);
3396 #endif
3397 } else {
3398 xmlGenericError(xmlGenericErrorContext,
3399 "%s: no such node\n", arg);
3401 ctxt->pctxt->node = NULL;
3403 #endif /* LIBXML_OUTPUT_ENABLED */
3404 } else {
3405 xmlGenericError(xmlGenericErrorContext,
3406 "Unknown command %s\n", command);
3408 free(cmdline); /* not xmlFree here ! */
3409 cmdline = NULL;
3411 #ifdef LIBXML_XPATH_ENABLED
3412 xmlXPathFreeContext(ctxt->pctxt);
3413 #endif /* LIBXML_XPATH_ENABLED */
3414 if (ctxt->loaded) {
3415 xmlFreeDoc(ctxt->doc);
3417 if (ctxt->filename != NULL)
3418 xmlFree(ctxt->filename);
3419 xmlFree(ctxt);
3420 if (cmdline != NULL)
3421 free(cmdline); /* not xmlFree here ! */
3424 #endif /* LIBXML_XPATH_ENABLED */
3426 #endif /* LIBXML_DEBUG_ENABLED */