blob: b56651b0ed3199cdd16860d65f8c0d21096a01d9 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
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 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00007 * Daniel Veillard <daniel@veillard.com>
Owen Taylor3473f882001-02-23 17:55:21 +00008 */
9
Daniel Veillard34ce8be2002-03-18 19:37:11 +000010#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000011#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000012#ifdef LIBXML_DEBUG_ENABLED
13
Owen Taylor3473f882001-02-23 17:55:21 +000014#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>
Daniel Veillard567e1b42001-08-01 15:53:47 +000024#include <libxml/parserInternals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000025#include <libxml/valid.h>
26#include <libxml/debugXML.h>
27#include <libxml/HTMLtree.h>
28#include <libxml/HTMLparser.h>
29#include <libxml/xmlerror.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000030#include <libxml/globals.h>
Daniel Veillard0ba59232002-02-10 13:20:39 +000031#include <libxml/xpathInternals.h>
Igor Zlatkovicc06bb622003-04-27 15:59:00 +000032#include <libxml/uri.h>
Daniel Veillard522bc602004-02-21 11:53:09 +000033#ifdef LIBXML_SCHEMAS_ENABLED
34#include <libxml/relaxng.h>
35#endif
Owen Taylor3473f882001-02-23 17:55:21 +000036
Daniel Veillardcfa303a2005-08-25 14:03:56 +000037#define DUMP_TEXT_TYPE 1
38
Daniel Veillard22cdb842004-10-04 14:09:17 +000039typedef struct _xmlDebugCtxt xmlDebugCtxt;
40typedef xmlDebugCtxt *xmlDebugCtxtPtr;
41struct _xmlDebugCtxt {
42 FILE *output; /* the output file */
43 char shift[101]; /* used for indenting */
44 int depth; /* current depth */
45 xmlDocPtr doc; /* current document */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000046 xmlNodePtr node; /* current node */
Daniel Veillard03a53c32004-10-26 16:06:51 +000047 xmlDictPtr dict; /* the doc dictionnary */
Daniel Veillard22cdb842004-10-04 14:09:17 +000048 int check; /* do just checkings */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000049 int errors; /* number of errors found */
Daniel Veillard03a53c32004-10-26 16:06:51 +000050 int nodict; /* if the document has no dictionnary */
Daniel Veillardcfa303a2005-08-25 14:03:56 +000051 int options; /* options */
Daniel Veillard22cdb842004-10-04 14:09:17 +000052};
53
54static void xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node);
55
56static void
57xmlCtxtDumpInitCtxt(xmlDebugCtxtPtr ctxt)
58{
59 int i;
60
61 ctxt->depth = 0;
62 ctxt->check = 0;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000063 ctxt->errors = 0;
Daniel Veillard22cdb842004-10-04 14:09:17 +000064 ctxt->output = stdout;
Daniel Veillard03a53c32004-10-26 16:06:51 +000065 ctxt->doc = NULL;
66 ctxt->node = NULL;
67 ctxt->dict = NULL;
Daniel Veillard6927b102004-10-27 17:29:04 +000068 ctxt->nodict = 0;
Daniel Veillardb6580ae2005-08-25 14:18:56 +000069 ctxt->options = 0;
Daniel Veillard22cdb842004-10-04 14:09:17 +000070 for (i = 0; i < 100; i++)
71 ctxt->shift[i] = ' ';
72 ctxt->shift[100] = 0;
73}
74
75static void
William M. Brack9638d4c2004-10-15 18:25:33 +000076xmlCtxtDumpCleanCtxt(xmlDebugCtxtPtr ctxt ATTRIBUTE_UNUSED)
Daniel Veillard76821142004-10-09 20:39:04 +000077{
William M. Brack9638d4c2004-10-15 18:25:33 +000078 /* remove the ATTRIBUTE_UNUSED when this is added */
Daniel Veillard76821142004-10-09 20:39:04 +000079}
80
81/**
Daniel Veillard0d24b112004-10-11 12:28:34 +000082 * xmlNsCheckScope:
83 * @node: the node
84 * @ns: the namespace node
Daniel Veillard76821142004-10-09 20:39:04 +000085 *
Daniel Veillard0d24b112004-10-11 12:28:34 +000086 * Check that a given namespace is in scope on a node.
Daniel Veillard76821142004-10-09 20:39:04 +000087 *
Daniel Veillard0d24b112004-10-11 12:28:34 +000088 * 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.
Daniel Veillard76821142004-10-09 20:39:04 +000091 */
92static int
Daniel Veillard0d24b112004-10-11 12:28:34 +000093xmlNsCheckScope(xmlNodePtr node, xmlNsPtr ns)
Daniel Veillard76821142004-10-09 20:39:04 +000094{
Daniel Veillard0d24b112004-10-11 12:28:34 +000095 xmlNsPtr cur;
Daniel Veillard76821142004-10-09 20:39:04 +000096
Daniel Veillard0d24b112004-10-11 12:28:34 +000097 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 }
Daniel Veillard76821142004-10-09 20:39:04 +0000123 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000124 node = node->parent;
Daniel Veillard76821142004-10-09 20:39:04 +0000125 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000126 /* 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);
Daniel Veillard76821142004-10-09 20:39:04 +0000135}
Daniel Veillard76821142004-10-09 20:39:04 +0000136
Daniel Veillard76821142004-10-09 20:39:04 +0000137static void
Daniel Veillard22cdb842004-10-04 14:09:17 +0000138xmlCtxtDumpSpaces(xmlDebugCtxtPtr ctxt)
139{
140 if (ctxt->check)
141 return;
142 if ((ctxt->output != NULL) && (ctxt->depth > 0)) {
143 if (ctxt->depth < 50)
Daniel Veillardbccae2d2009-06-04 11:22:45 +0200144 fprintf(ctxt->output, "%s", &ctxt->shift[100 - 2 * ctxt->depth]);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000145 else
Daniel Veillardbccae2d2009-06-04 11:22:45 +0200146 fprintf(ctxt->output, "%s", ctxt->shift);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000147 }
148}
149
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000150/**
151 * xmlDebugErr:
152 * @ctxt: a debug context
153 * @error: the error code
154 *
155 * Handle a debug error.
156 */
157static void
158xmlDebugErr(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,
Daniel Veillardbccae2d2009-06-04 11:22:45 +0200165 "%s", msg);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000166}
167static void
168xmlDebugErr2(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}
177static void
Daniel Veillardc6095782004-10-15 14:50:10 +0000178xmlDebugErr3(xmlDebugCtxtPtr ctxt, int error, const char *msg, const char *extra)
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000179{
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
Daniel Veillard0d24b112004-10-11 12:28:34 +0000188/**
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 */
196static void
197xmlCtxtNsCheckScope(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
Daniel Veillardc6095782004-10-15 14:50:10 +0000222/**
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 */
229static void
230xmlCtxtCheckString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
231{
232 if (str == NULL) return;
233 if (ctxt->check) {
234 if (!xmlCheckUTF8(str)) {
Daniel Veillard03a53c32004-10-26 16:06:51 +0000235 xmlDebugErr3(ctxt, XML_CHECK_NOT_UTF8,
Daniel Veillardc6095782004-10-15 14:50:10 +0000236 "String is not UTF-8 %s", (const char *) str);
237 }
238 }
239}
240
Daniel Veillard03a53c32004-10-26 16:06:51 +0000241/**
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 */
249static void
250xmlCtxtCheckName(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 xmlDebugErr3(ctxt, XML_CHECK_OUTSIDE_DICT,
264 "Name is not from the document dictionnary '%s'",
265 (const char *) name);
266 }
267 }
268}
269
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000270static void
271xmlCtxtGenericNodeCheck(xmlDebugCtxtPtr ctxt, xmlNodePtr node) {
Daniel Veillard03a53c32004-10-26 16:06:51 +0000272 xmlDocPtr doc;
273 xmlDictPtr dict;
274
275 doc = node->doc;
276
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000277 if (node->parent == NULL)
278 xmlDebugErr(ctxt, XML_CHECK_NO_PARENT,
279 "Node has no parent\n");
Daniel Veillard03a53c32004-10-26 16:06:51 +0000280 if (node->doc == NULL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000281 xmlDebugErr(ctxt, XML_CHECK_NO_DOC,
282 "Node has no doc\n");
Daniel Veillard03a53c32004-10-26 16:06:51 +0000283 dict = NULL;
284 } else {
285 dict = doc->dict;
286 if ((dict == NULL) && (ctxt->nodict == 0)) {
Daniel Veillard6927b102004-10-27 17:29:04 +0000287#if 0
288 /* desactivated right now as it raises too many errors */
289 if (doc->type == XML_DOCUMENT_NODE)
290 xmlDebugErr(ctxt, XML_CHECK_NO_DICT,
291 "Document has no dictionnary\n");
292#endif
Daniel Veillard03a53c32004-10-26 16:06:51 +0000293 ctxt->nodict = 1;
294 }
295 if (ctxt->doc == NULL)
296 ctxt->doc = doc;
297
298 if (ctxt->dict == NULL) {
299 ctxt->dict = dict;
300 }
301 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000302 if ((node->parent != NULL) && (node->doc != node->parent->doc) &&
303 (!xmlStrEqual(node->name, BAD_CAST "pseudoroot")))
304 xmlDebugErr(ctxt, XML_CHECK_WRONG_DOC,
305 "Node doc differs from parent's one\n");
306 if (node->prev == NULL) {
307 if (node->type == XML_ATTRIBUTE_NODE) {
308 if ((node->parent != NULL) &&
309 (node != (xmlNodePtr) node->parent->properties))
310 xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
311 "Attr has no prev and not first of attr list\n");
312
313 } else if ((node->parent != NULL) && (node->parent->children != node))
314 xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
315 "Node has no prev and not first of parent list\n");
316 } else {
317 if (node->prev->next != node)
318 xmlDebugErr(ctxt, XML_CHECK_WRONG_PREV,
319 "Node prev->next : back link wrong\n");
320 }
321 if (node->next == NULL) {
322 if ((node->parent != NULL) && (node->type != XML_ATTRIBUTE_NODE) &&
Daniel Veillard97c9ce22008-03-25 16:52:41 +0000323 (node->parent->last != node) &&
324 (node->parent->type == XML_ELEMENT_NODE))
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000325 xmlDebugErr(ctxt, XML_CHECK_NO_NEXT,
326 "Node has no next and not last of parent list\n");
327 } else {
328 if (node->next->prev != node)
329 xmlDebugErr(ctxt, XML_CHECK_WRONG_NEXT,
330 "Node next->prev : forward link wrong\n");
Daniel Veillard0d24b112004-10-11 12:28:34 +0000331 if (node->next->parent != node->parent)
332 xmlDebugErr(ctxt, XML_CHECK_WRONG_PARENT,
333 "Node next->prev : forward link wrong\n");
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000334 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000335 if (node->type == XML_ELEMENT_NODE) {
336 xmlNsPtr ns;
337
338 ns = node->nsDef;
339 while (ns != NULL) {
340 xmlCtxtNsCheckScope(ctxt, node, ns);
341 ns = ns->next;
342 }
343 if (node->ns != NULL)
344 xmlCtxtNsCheckScope(ctxt, node, node->ns);
345 } else if (node->type == XML_ATTRIBUTE_NODE) {
346 if (node->ns != NULL)
347 xmlCtxtNsCheckScope(ctxt, node, node->ns);
348 }
349
Daniel Veillardc6095782004-10-15 14:50:10 +0000350 if ((node->type != XML_ELEMENT_NODE) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000351 (node->type != XML_ATTRIBUTE_NODE) &&
Daniel Veillardf2e066a2005-06-30 13:04:44 +0000352 (node->type != XML_ELEMENT_DECL) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000353 (node->type != XML_ATTRIBUTE_DECL) &&
354 (node->type != XML_DTD_NODE) &&
William M. Brack0357a302005-07-06 17:39:14 +0000355 (node->type != XML_ELEMENT_DECL) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000356 (node->type != XML_HTML_DOCUMENT_NODE) &&
357 (node->type != XML_DOCUMENT_NODE)) {
Daniel Veillardc6095782004-10-15 14:50:10 +0000358 if (node->content != NULL)
William M. Brack9638d4c2004-10-15 18:25:33 +0000359 xmlCtxtCheckString(ctxt, (const xmlChar *) node->content);
Daniel Veillardc6095782004-10-15 14:50:10 +0000360 }
Daniel Veillard03a53c32004-10-26 16:06:51 +0000361 switch (node->type) {
362 case XML_ELEMENT_NODE:
363 case XML_ATTRIBUTE_NODE:
364 xmlCtxtCheckName(ctxt, node->name);
365 break;
366 case XML_TEXT_NODE:
367 if ((node->name == xmlStringText) ||
368 (node->name == xmlStringTextNoenc))
369 break;
370 /* some case of entity substitution can lead to this */
371 if ((ctxt->dict != NULL) &&
Daniel Veillard6927b102004-10-27 17:29:04 +0000372 (node->name == xmlDictLookup(ctxt->dict, BAD_CAST "nbktext",
373 7)))
Daniel Veillard03a53c32004-10-26 16:06:51 +0000374 break;
375
376 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
377 "Text node has wrong name '%s'",
378 (const char *) node->name);
379 break;
380 case XML_COMMENT_NODE:
381 if (node->name == xmlStringComment)
382 break;
383 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
384 "Comment node has wrong name '%s'",
385 (const char *) node->name);
386 break;
387 case XML_PI_NODE:
388 xmlCtxtCheckName(ctxt, node->name);
389 break;
390 case XML_CDATA_SECTION_NODE:
391 if (node->name == NULL)
392 break;
393 xmlDebugErr3(ctxt, XML_CHECK_NAME_NOT_NULL,
394 "CData section has non NULL name '%s'",
395 (const char *) node->name);
396 break;
397 case XML_ENTITY_REF_NODE:
398 case XML_ENTITY_NODE:
399 case XML_DOCUMENT_TYPE_NODE:
400 case XML_DOCUMENT_FRAG_NODE:
401 case XML_NOTATION_NODE:
402 case XML_DTD_NODE:
403 case XML_ELEMENT_DECL:
404 case XML_ATTRIBUTE_DECL:
405 case XML_ENTITY_DECL:
406 case XML_NAMESPACE_DECL:
407 case XML_XINCLUDE_START:
408 case XML_XINCLUDE_END:
409#ifdef LIBXML_DOCB_ENABLED
410 case XML_DOCB_DOCUMENT_NODE:
411#endif
412 case XML_DOCUMENT_NODE:
413 case XML_HTML_DOCUMENT_NODE:
414 break;
415 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000416}
417
Daniel Veillard22cdb842004-10-04 14:09:17 +0000418static void
419xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
420{
421 int i;
422
Daniel Veillardc6095782004-10-15 14:50:10 +0000423 if (ctxt->check) {
Daniel Veillard22cdb842004-10-04 14:09:17 +0000424 return;
Daniel Veillardc6095782004-10-15 14:50:10 +0000425 }
Daniel Veillard22cdb842004-10-04 14:09:17 +0000426 /* TODO: check UTF8 content of the string */
427 if (str == NULL) {
428 fprintf(ctxt->output, "(NULL)");
429 return;
430 }
431 for (i = 0; i < 40; i++)
432 if (str[i] == 0)
433 return;
434 else if (IS_BLANK_CH(str[i]))
435 fputc(' ', ctxt->output);
436 else if (str[i] >= 0x80)
437 fprintf(ctxt->output, "#%X", str[i]);
438 else
439 fputc(str[i], ctxt->output);
440 fprintf(ctxt->output, "...");
441}
442
443static void
444xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
445{
446 xmlCtxtDumpSpaces(ctxt);
447
448 if (dtd == NULL) {
449 if (!ctxt->check)
450 fprintf(ctxt->output, "DTD node is NULL\n");
451 return;
452 }
453
454 if (dtd->type != XML_DTD_NODE) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000455 xmlDebugErr(ctxt, XML_CHECK_NOT_DTD,
456 "Node is not a DTD");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000457 return;
458 }
459 if (!ctxt->check) {
460 if (dtd->name != NULL)
461 fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name);
462 else
463 fprintf(ctxt->output, "DTD");
464 if (dtd->ExternalID != NULL)
465 fprintf(ctxt->output, ", PUBLIC %s", (char *) dtd->ExternalID);
466 if (dtd->SystemID != NULL)
467 fprintf(ctxt->output, ", SYSTEM %s", (char *) dtd->SystemID);
468 fprintf(ctxt->output, "\n");
469 }
470 /*
471 * Do a bit of checking
472 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000473 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000474}
475
476static void
477xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr)
478{
479 xmlCtxtDumpSpaces(ctxt);
480
481 if (attr == NULL) {
482 if (!ctxt->check)
483 fprintf(ctxt->output, "Attribute declaration is NULL\n");
484 return;
485 }
486 if (attr->type != XML_ATTRIBUTE_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000487 xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL,
488 "Node is not an attribute declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000489 return;
490 }
491 if (attr->name != NULL) {
492 if (!ctxt->check)
493 fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name);
494 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000495 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
496 "Node attribute declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000497 if (attr->elem != NULL) {
498 if (!ctxt->check)
499 fprintf(ctxt->output, " for %s", (char *) attr->elem);
500 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000501 xmlDebugErr(ctxt, XML_CHECK_NO_ELEM,
502 "Node attribute declaration has no element name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000503 if (!ctxt->check) {
504 switch (attr->atype) {
505 case XML_ATTRIBUTE_CDATA:
506 fprintf(ctxt->output, " CDATA");
507 break;
508 case XML_ATTRIBUTE_ID:
509 fprintf(ctxt->output, " ID");
510 break;
511 case XML_ATTRIBUTE_IDREF:
512 fprintf(ctxt->output, " IDREF");
513 break;
514 case XML_ATTRIBUTE_IDREFS:
515 fprintf(ctxt->output, " IDREFS");
516 break;
517 case XML_ATTRIBUTE_ENTITY:
518 fprintf(ctxt->output, " ENTITY");
519 break;
520 case XML_ATTRIBUTE_ENTITIES:
521 fprintf(ctxt->output, " ENTITIES");
522 break;
523 case XML_ATTRIBUTE_NMTOKEN:
524 fprintf(ctxt->output, " NMTOKEN");
525 break;
526 case XML_ATTRIBUTE_NMTOKENS:
527 fprintf(ctxt->output, " NMTOKENS");
528 break;
529 case XML_ATTRIBUTE_ENUMERATION:
530 fprintf(ctxt->output, " ENUMERATION");
531 break;
532 case XML_ATTRIBUTE_NOTATION:
533 fprintf(ctxt->output, " NOTATION ");
534 break;
535 }
536 if (attr->tree != NULL) {
537 int indx;
538 xmlEnumerationPtr cur = attr->tree;
539
540 for (indx = 0; indx < 5; indx++) {
541 if (indx != 0)
542 fprintf(ctxt->output, "|%s", (char *) cur->name);
543 else
544 fprintf(ctxt->output, " (%s", (char *) cur->name);
545 cur = cur->next;
546 if (cur == NULL)
547 break;
548 }
549 if (cur == NULL)
550 fprintf(ctxt->output, ")");
551 else
552 fprintf(ctxt->output, "...)");
553 }
554 switch (attr->def) {
555 case XML_ATTRIBUTE_NONE:
556 break;
557 case XML_ATTRIBUTE_REQUIRED:
558 fprintf(ctxt->output, " REQUIRED");
559 break;
560 case XML_ATTRIBUTE_IMPLIED:
561 fprintf(ctxt->output, " IMPLIED");
562 break;
563 case XML_ATTRIBUTE_FIXED:
564 fprintf(ctxt->output, " FIXED");
565 break;
566 }
567 if (attr->defaultValue != NULL) {
568 fprintf(ctxt->output, "\"");
569 xmlCtxtDumpString(ctxt, attr->defaultValue);
570 fprintf(ctxt->output, "\"");
571 }
572 fprintf(ctxt->output, "\n");
573 }
574
575 /*
576 * Do a bit of checking
577 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000578 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000579}
580
581static void
582xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem)
583{
584 xmlCtxtDumpSpaces(ctxt);
585
586 if (elem == NULL) {
587 if (!ctxt->check)
588 fprintf(ctxt->output, "Element declaration is NULL\n");
589 return;
590 }
591 if (elem->type != XML_ELEMENT_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000592 xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL,
593 "Node is not an element declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000594 return;
595 }
596 if (elem->name != NULL) {
597 if (!ctxt->check) {
598 fprintf(ctxt->output, "ELEMDECL(");
599 xmlCtxtDumpString(ctxt, elem->name);
600 fprintf(ctxt->output, ")");
601 }
602 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000603 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
604 "Element declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000605 if (!ctxt->check) {
606 switch (elem->etype) {
607 case XML_ELEMENT_TYPE_UNDEFINED:
608 fprintf(ctxt->output, ", UNDEFINED");
609 break;
610 case XML_ELEMENT_TYPE_EMPTY:
611 fprintf(ctxt->output, ", EMPTY");
612 break;
613 case XML_ELEMENT_TYPE_ANY:
614 fprintf(ctxt->output, ", ANY");
615 break;
616 case XML_ELEMENT_TYPE_MIXED:
617 fprintf(ctxt->output, ", MIXED ");
618 break;
619 case XML_ELEMENT_TYPE_ELEMENT:
620 fprintf(ctxt->output, ", MIXED ");
621 break;
622 }
623 if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) {
624 char buf[5001];
625
626 buf[0] = 0;
627 xmlSnprintfElementContent(buf, 5000, elem->content, 1);
628 buf[5000] = 0;
629 fprintf(ctxt->output, "%s", buf);
630 }
631 fprintf(ctxt->output, "\n");
632 }
633
634 /*
635 * Do a bit of checking
636 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000637 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000638}
639
640static void
641xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
642{
643 xmlCtxtDumpSpaces(ctxt);
644
645 if (ent == NULL) {
646 if (!ctxt->check)
647 fprintf(ctxt->output, "Entity declaration is NULL\n");
648 return;
649 }
650 if (ent->type != XML_ENTITY_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000651 xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL,
652 "Node is not an entity declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000653 return;
654 }
655 if (ent->name != NULL) {
656 if (!ctxt->check) {
657 fprintf(ctxt->output, "ENTITYDECL(");
658 xmlCtxtDumpString(ctxt, ent->name);
659 fprintf(ctxt->output, ")");
660 }
661 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000662 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
663 "Entity declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000664 if (!ctxt->check) {
665 switch (ent->etype) {
666 case XML_INTERNAL_GENERAL_ENTITY:
667 fprintf(ctxt->output, ", internal\n");
668 break;
669 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
670 fprintf(ctxt->output, ", external parsed\n");
671 break;
672 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
673 fprintf(ctxt->output, ", unparsed\n");
674 break;
675 case XML_INTERNAL_PARAMETER_ENTITY:
676 fprintf(ctxt->output, ", parameter\n");
677 break;
678 case XML_EXTERNAL_PARAMETER_ENTITY:
679 fprintf(ctxt->output, ", external parameter\n");
680 break;
681 case XML_INTERNAL_PREDEFINED_ENTITY:
682 fprintf(ctxt->output, ", predefined\n");
683 break;
684 }
685 if (ent->ExternalID) {
686 xmlCtxtDumpSpaces(ctxt);
687 fprintf(ctxt->output, " ExternalID=%s\n",
688 (char *) ent->ExternalID);
689 }
690 if (ent->SystemID) {
691 xmlCtxtDumpSpaces(ctxt);
692 fprintf(ctxt->output, " SystemID=%s\n",
693 (char *) ent->SystemID);
694 }
695 if (ent->URI != NULL) {
696 xmlCtxtDumpSpaces(ctxt);
697 fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI);
698 }
699 if (ent->content) {
700 xmlCtxtDumpSpaces(ctxt);
701 fprintf(ctxt->output, " content=");
702 xmlCtxtDumpString(ctxt, ent->content);
703 fprintf(ctxt->output, "\n");
704 }
705 }
706
707 /*
708 * Do a bit of checking
709 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000710 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000711}
712
713static void
714xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
715{
716 xmlCtxtDumpSpaces(ctxt);
717
718 if (ns == NULL) {
719 if (!ctxt->check)
720 fprintf(ctxt->output, "namespace node is NULL\n");
721 return;
722 }
723 if (ns->type != XML_NAMESPACE_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000724 xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL,
725 "Node is not a namespace declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000726 return;
727 }
728 if (ns->href == NULL) {
729 if (ns->prefix != NULL)
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000730 xmlDebugErr3(ctxt, XML_CHECK_NO_HREF,
731 "Incomplete namespace %s href=NULL\n",
Daniel Veillard22cdb842004-10-04 14:09:17 +0000732 (char *) ns->prefix);
733 else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000734 xmlDebugErr(ctxt, XML_CHECK_NO_HREF,
735 "Incomplete default namespace href=NULL\n");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000736 } else {
737 if (!ctxt->check) {
738 if (ns->prefix != NULL)
739 fprintf(ctxt->output, "namespace %s href=",
740 (char *) ns->prefix);
741 else
742 fprintf(ctxt->output, "default namespace href=");
743
744 xmlCtxtDumpString(ctxt, ns->href);
745 fprintf(ctxt->output, "\n");
746 }
747 }
748}
749
750static void
751xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
752{
753 while (ns != NULL) {
754 xmlCtxtDumpNamespace(ctxt, ns);
755 ns = ns->next;
756 }
757}
758
759static void
760xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
761{
762 xmlCtxtDumpSpaces(ctxt);
763
764 if (ent == NULL) {
765 if (!ctxt->check)
766 fprintf(ctxt->output, "Entity is NULL\n");
767 return;
768 }
769 if (!ctxt->check) {
770 switch (ent->etype) {
771 case XML_INTERNAL_GENERAL_ENTITY:
772 fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY ");
773 break;
774 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
775 fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
776 break;
777 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
778 fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
779 break;
780 case XML_INTERNAL_PARAMETER_ENTITY:
781 fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY ");
782 break;
783 case XML_EXTERNAL_PARAMETER_ENTITY:
784 fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY ");
785 break;
786 default:
787 fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype);
788 }
789 fprintf(ctxt->output, "%s\n", ent->name);
790 if (ent->ExternalID) {
791 xmlCtxtDumpSpaces(ctxt);
792 fprintf(ctxt->output, "ExternalID=%s\n",
793 (char *) ent->ExternalID);
794 }
795 if (ent->SystemID) {
796 xmlCtxtDumpSpaces(ctxt);
797 fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID);
798 }
799 if (ent->URI) {
800 xmlCtxtDumpSpaces(ctxt);
801 fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI);
802 }
803 if (ent->content) {
804 xmlCtxtDumpSpaces(ctxt);
805 fprintf(ctxt->output, "content=");
806 xmlCtxtDumpString(ctxt, ent->content);
807 fprintf(ctxt->output, "\n");
808 }
809 }
810}
811
812/**
813 * xmlCtxtDumpAttr:
814 * @output: the FILE * for the output
815 * @attr: the attribute
816 * @depth: the indentation level.
817 *
818 * Dumps debug information for the attribute
819 */
820static void
821xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
822{
823 xmlCtxtDumpSpaces(ctxt);
824
825 if (attr == NULL) {
826 if (!ctxt->check)
827 fprintf(ctxt->output, "Attr is NULL");
828 return;
829 }
830 if (!ctxt->check) {
831 fprintf(ctxt->output, "ATTRIBUTE ");
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000832 xmlCtxtDumpString(ctxt, attr->name);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000833 fprintf(ctxt->output, "\n");
834 if (attr->children != NULL) {
835 ctxt->depth++;
836 xmlCtxtDumpNodeList(ctxt, attr->children);
837 ctxt->depth--;
838 }
839 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000840 if (attr->name == NULL)
841 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
842 "Attribute has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000843
844 /*
845 * Do a bit of checking
846 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000847 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000848}
849
850/**
851 * xmlCtxtDumpAttrList:
852 * @output: the FILE * for the output
853 * @attr: the attribute list
854 * @depth: the indentation level.
855 *
856 * Dumps debug information for the attribute list
857 */
858static void
859xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
860{
861 while (attr != NULL) {
862 xmlCtxtDumpAttr(ctxt, attr);
863 attr = attr->next;
864 }
865}
866
867/**
868 * xmlCtxtDumpOneNode:
869 * @output: the FILE * for the output
870 * @node: the node
871 * @depth: the indentation level.
872 *
873 * Dumps debug information for the element node, it is not recursive
874 */
875static void
876xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
877{
878 if (node == NULL) {
879 if (!ctxt->check) {
880 xmlCtxtDumpSpaces(ctxt);
881 fprintf(ctxt->output, "node is NULL\n");
882 }
883 return;
884 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000885 ctxt->node = node;
886
Daniel Veillard22cdb842004-10-04 14:09:17 +0000887 switch (node->type) {
888 case XML_ELEMENT_NODE:
889 if (!ctxt->check) {
890 xmlCtxtDumpSpaces(ctxt);
891 fprintf(ctxt->output, "ELEMENT ");
892 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
893 xmlCtxtDumpString(ctxt, node->ns->prefix);
894 fprintf(ctxt->output, ":");
895 }
896 xmlCtxtDumpString(ctxt, node->name);
897 fprintf(ctxt->output, "\n");
898 }
899 break;
900 case XML_ATTRIBUTE_NODE:
901 if (!ctxt->check)
902 xmlCtxtDumpSpaces(ctxt);
903 fprintf(ctxt->output, "Error, ATTRIBUTE found here\n");
William M. Brack5a9c1fd2004-12-17 21:38:09 +0000904 xmlCtxtGenericNodeCheck(ctxt, node);
905 return;
Daniel Veillard22cdb842004-10-04 14:09:17 +0000906 case XML_TEXT_NODE:
907 if (!ctxt->check) {
908 xmlCtxtDumpSpaces(ctxt);
909 if (node->name == (const xmlChar *) xmlStringTextNoenc)
Daniel Veillard8874b942005-08-25 13:19:21 +0000910 fprintf(ctxt->output, "TEXT no enc");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000911 else
Daniel Veillard8874b942005-08-25 13:19:21 +0000912 fprintf(ctxt->output, "TEXT");
Daniel Veillardcfa303a2005-08-25 14:03:56 +0000913 if (ctxt->options & DUMP_TEXT_TYPE) {
914 if (node->content == (xmlChar *) &(node->properties))
915 fprintf(ctxt->output, " compact\n");
916 else if (xmlDictOwns(ctxt->dict, node->content) == 1)
917 fprintf(ctxt->output, " interned\n");
918 else
919 fprintf(ctxt->output, "\n");
920 } else
Daniel Veillard8874b942005-08-25 13:19:21 +0000921 fprintf(ctxt->output, "\n");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000922 }
923 break;
924 case XML_CDATA_SECTION_NODE:
925 if (!ctxt->check) {
926 xmlCtxtDumpSpaces(ctxt);
927 fprintf(ctxt->output, "CDATA_SECTION\n");
928 }
929 break;
930 case XML_ENTITY_REF_NODE:
931 if (!ctxt->check) {
932 xmlCtxtDumpSpaces(ctxt);
933 fprintf(ctxt->output, "ENTITY_REF(%s)\n",
934 (char *) node->name);
935 }
936 break;
937 case XML_ENTITY_NODE:
938 if (!ctxt->check) {
939 xmlCtxtDumpSpaces(ctxt);
940 fprintf(ctxt->output, "ENTITY\n");
941 }
942 break;
943 case XML_PI_NODE:
944 if (!ctxt->check) {
945 xmlCtxtDumpSpaces(ctxt);
946 fprintf(ctxt->output, "PI %s\n", (char *) node->name);
947 }
948 break;
949 case XML_COMMENT_NODE:
950 if (!ctxt->check) {
951 xmlCtxtDumpSpaces(ctxt);
952 fprintf(ctxt->output, "COMMENT\n");
953 }
954 break;
955 case XML_DOCUMENT_NODE:
956 case XML_HTML_DOCUMENT_NODE:
957 if (!ctxt->check) {
958 xmlCtxtDumpSpaces(ctxt);
959 }
William M. Brack5a9c1fd2004-12-17 21:38:09 +0000960 fprintf(ctxt->output, "Error, DOCUMENT found here\n");
961 xmlCtxtGenericNodeCheck(ctxt, node);
962 return;
Daniel Veillard22cdb842004-10-04 14:09:17 +0000963 case XML_DOCUMENT_TYPE_NODE:
964 if (!ctxt->check) {
965 xmlCtxtDumpSpaces(ctxt);
966 fprintf(ctxt->output, "DOCUMENT_TYPE\n");
967 }
968 break;
969 case XML_DOCUMENT_FRAG_NODE:
970 if (!ctxt->check) {
971 xmlCtxtDumpSpaces(ctxt);
972 fprintf(ctxt->output, "DOCUMENT_FRAG\n");
973 }
974 break;
975 case XML_NOTATION_NODE:
976 if (!ctxt->check) {
977 xmlCtxtDumpSpaces(ctxt);
978 fprintf(ctxt->output, "NOTATION\n");
979 }
980 break;
981 case XML_DTD_NODE:
982 xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
983 return;
984 case XML_ELEMENT_DECL:
985 xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
986 return;
987 case XML_ATTRIBUTE_DECL:
988 xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
989 return;
990 case XML_ENTITY_DECL:
991 xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
992 return;
993 case XML_NAMESPACE_DECL:
994 xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
995 return;
996 case XML_XINCLUDE_START:
997 if (!ctxt->check) {
998 xmlCtxtDumpSpaces(ctxt);
999 fprintf(ctxt->output, "INCLUDE START\n");
1000 }
1001 return;
1002 case XML_XINCLUDE_END:
1003 if (!ctxt->check) {
1004 xmlCtxtDumpSpaces(ctxt);
1005 fprintf(ctxt->output, "INCLUDE END\n");
1006 }
1007 return;
1008 default:
1009 if (!ctxt->check)
1010 xmlCtxtDumpSpaces(ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001011 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1012 "Unknown node type %d\n", node->type);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001013 return;
1014 }
1015 if (node->doc == NULL) {
1016 if (!ctxt->check) {
1017 xmlCtxtDumpSpaces(ctxt);
1018 }
1019 fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
1020 }
1021 ctxt->depth++;
Daniel Veillard8874b942005-08-25 13:19:21 +00001022 if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL))
Daniel Veillard22cdb842004-10-04 14:09:17 +00001023 xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
Daniel Veillard8874b942005-08-25 13:19:21 +00001024 if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL))
Daniel Veillard22cdb842004-10-04 14:09:17 +00001025 xmlCtxtDumpAttrList(ctxt, node->properties);
1026 if (node->type != XML_ENTITY_REF_NODE) {
1027 if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
1028 if (!ctxt->check) {
1029 xmlCtxtDumpSpaces(ctxt);
1030 fprintf(ctxt->output, "content=");
1031 xmlCtxtDumpString(ctxt, node->content);
1032 fprintf(ctxt->output, "\n");
1033 }
1034 }
1035 } else {
1036 xmlEntityPtr ent;
1037
1038 ent = xmlGetDocEntity(node->doc, node->name);
1039 if (ent != NULL)
1040 xmlCtxtDumpEntity(ctxt, ent);
1041 }
1042 ctxt->depth--;
1043
1044 /*
1045 * Do a bit of checking
1046 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001047 xmlCtxtGenericNodeCheck(ctxt, node);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001048}
1049
1050/**
1051 * xmlCtxtDumpNode:
1052 * @output: the FILE * for the output
1053 * @node: the node
1054 * @depth: the indentation level.
1055 *
1056 * Dumps debug information for the element node, it is recursive
1057 */
1058static void
1059xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1060{
1061 if (node == NULL) {
1062 if (!ctxt->check) {
1063 xmlCtxtDumpSpaces(ctxt);
1064 fprintf(ctxt->output, "node is NULL\n");
1065 }
1066 return;
1067 }
1068 xmlCtxtDumpOneNode(ctxt, node);
Daniel Veillard7837dd82005-09-06 22:16:57 +00001069 if ((node->type != XML_NAMESPACE_DECL) &&
1070 (node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
Daniel Veillard22cdb842004-10-04 14:09:17 +00001071 ctxt->depth++;
1072 xmlCtxtDumpNodeList(ctxt, node->children);
1073 ctxt->depth--;
1074 }
1075}
1076
1077/**
1078 * xmlCtxtDumpNodeList:
1079 * @output: the FILE * for the output
1080 * @node: the node list
1081 * @depth: the indentation level.
1082 *
1083 * Dumps debug information for the list of element node, it is recursive
1084 */
1085static void
1086xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1087{
1088 while (node != NULL) {
1089 xmlCtxtDumpNode(ctxt, node);
1090 node = node->next;
1091 }
1092}
1093
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001094static void
1095xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1096{
1097 if (doc == NULL) {
1098 if (!ctxt->check)
1099 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1100 return;
1101 }
1102 ctxt->node = (xmlNodePtr) doc;
1103
1104 switch (doc->type) {
1105 case XML_ELEMENT_NODE:
1106 xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
1107 "Misplaced ELEMENT node\n");
1108 break;
1109 case XML_ATTRIBUTE_NODE:
1110 xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
1111 "Misplaced ATTRIBUTE node\n");
1112 break;
1113 case XML_TEXT_NODE:
1114 xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
1115 "Misplaced TEXT node\n");
1116 break;
1117 case XML_CDATA_SECTION_NODE:
1118 xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
1119 "Misplaced CDATA node\n");
1120 break;
1121 case XML_ENTITY_REF_NODE:
1122 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
1123 "Misplaced ENTITYREF node\n");
1124 break;
1125 case XML_ENTITY_NODE:
1126 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
1127 "Misplaced ENTITY node\n");
1128 break;
1129 case XML_PI_NODE:
1130 xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
1131 "Misplaced PI node\n");
1132 break;
1133 case XML_COMMENT_NODE:
1134 xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
1135 "Misplaced COMMENT node\n");
1136 break;
1137 case XML_DOCUMENT_NODE:
1138 if (!ctxt->check)
1139 fprintf(ctxt->output, "DOCUMENT\n");
1140 break;
1141 case XML_HTML_DOCUMENT_NODE:
1142 if (!ctxt->check)
1143 fprintf(ctxt->output, "HTML DOCUMENT\n");
1144 break;
1145 case XML_DOCUMENT_TYPE_NODE:
1146 xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
1147 "Misplaced DOCTYPE node\n");
1148 break;
1149 case XML_DOCUMENT_FRAG_NODE:
1150 xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
1151 "Misplaced FRAGMENT node\n");
1152 break;
1153 case XML_NOTATION_NODE:
1154 xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
1155 "Misplaced NOTATION node\n");
1156 break;
1157 default:
1158 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1159 "Unknown node type %d\n", doc->type);
1160 }
1161}
Daniel Veillard22cdb842004-10-04 14:09:17 +00001162
1163/**
1164 * xmlCtxtDumpDocumentHead:
1165 * @output: the FILE * for the output
1166 * @doc: the document
1167 *
1168 * Dumps debug information cncerning the document, not recursive
1169 */
1170static void
1171xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1172{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001173 if (doc == NULL) return;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001174 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001175 if (!ctxt->check) {
1176 if (doc->name != NULL) {
1177 fprintf(ctxt->output, "name=");
1178 xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
1179 fprintf(ctxt->output, "\n");
1180 }
1181 if (doc->version != NULL) {
1182 fprintf(ctxt->output, "version=");
1183 xmlCtxtDumpString(ctxt, doc->version);
1184 fprintf(ctxt->output, "\n");
1185 }
1186 if (doc->encoding != NULL) {
1187 fprintf(ctxt->output, "encoding=");
1188 xmlCtxtDumpString(ctxt, doc->encoding);
1189 fprintf(ctxt->output, "\n");
1190 }
1191 if (doc->URL != NULL) {
1192 fprintf(ctxt->output, "URL=");
1193 xmlCtxtDumpString(ctxt, doc->URL);
1194 fprintf(ctxt->output, "\n");
1195 }
1196 if (doc->standalone)
1197 fprintf(ctxt->output, "standalone=true\n");
1198 }
1199 if (doc->oldNs != NULL)
1200 xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
1201}
1202
1203/**
1204 * xmlCtxtDumpDocument:
1205 * @output: the FILE * for the output
1206 * @doc: the document
1207 *
1208 * Dumps debug information for the document, it's recursive
1209 */
1210static void
1211xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1212{
1213 if (doc == NULL) {
1214 if (!ctxt->check)
1215 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1216 return;
1217 }
1218 xmlCtxtDumpDocumentHead(ctxt, doc);
1219 if (((doc->type == XML_DOCUMENT_NODE) ||
1220 (doc->type == XML_HTML_DOCUMENT_NODE))
1221 && (doc->children != NULL)) {
1222 ctxt->depth++;
1223 xmlCtxtDumpNodeList(ctxt, doc->children);
1224 ctxt->depth--;
1225 }
1226}
1227
1228static void
1229xmlCtxtDumpEntityCallback(xmlEntityPtr cur, xmlDebugCtxtPtr ctxt)
1230{
1231 if (cur == NULL) {
1232 if (!ctxt->check)
1233 fprintf(ctxt->output, "Entity is NULL");
1234 return;
1235 }
1236 if (!ctxt->check) {
1237 fprintf(ctxt->output, "%s : ", (char *) cur->name);
1238 switch (cur->etype) {
1239 case XML_INTERNAL_GENERAL_ENTITY:
1240 fprintf(ctxt->output, "INTERNAL GENERAL, ");
1241 break;
1242 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1243 fprintf(ctxt->output, "EXTERNAL PARSED, ");
1244 break;
1245 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1246 fprintf(ctxt->output, "EXTERNAL UNPARSED, ");
1247 break;
1248 case XML_INTERNAL_PARAMETER_ENTITY:
1249 fprintf(ctxt->output, "INTERNAL PARAMETER, ");
1250 break;
1251 case XML_EXTERNAL_PARAMETER_ENTITY:
1252 fprintf(ctxt->output, "EXTERNAL PARAMETER, ");
1253 break;
1254 default:
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001255 xmlDebugErr2(ctxt, XML_CHECK_ENTITY_TYPE,
1256 "Unknown entity type %d\n", cur->etype);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001257 }
1258 if (cur->ExternalID != NULL)
1259 fprintf(ctxt->output, "ID \"%s\"", (char *) cur->ExternalID);
1260 if (cur->SystemID != NULL)
1261 fprintf(ctxt->output, "SYSTEM \"%s\"", (char *) cur->SystemID);
1262 if (cur->orig != NULL)
1263 fprintf(ctxt->output, "\n orig \"%s\"", (char *) cur->orig);
1264 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL))
1265 fprintf(ctxt->output, "\n content \"%s\"",
1266 (char *) cur->content);
1267 fprintf(ctxt->output, "\n");
1268 }
1269}
1270
1271/**
1272 * xmlCtxtDumpEntities:
1273 * @output: the FILE * for the output
1274 * @doc: the document
1275 *
1276 * Dumps debug information for all the entities in use by the document
1277 */
1278static void
1279xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1280{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001281 if (doc == NULL) return;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001282 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001283 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
1284 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1285 doc->intSubset->entities;
1286
1287 if (!ctxt->check)
1288 fprintf(ctxt->output, "Entities in internal subset\n");
1289 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1290 ctxt);
1291 } else
1292 fprintf(ctxt->output, "No entities in internal subset\n");
1293 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
1294 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1295 doc->extSubset->entities;
1296
1297 if (!ctxt->check)
1298 fprintf(ctxt->output, "Entities in external subset\n");
1299 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1300 ctxt);
1301 } else if (!ctxt->check)
1302 fprintf(ctxt->output, "No entities in external subset\n");
1303}
1304
1305/**
1306 * xmlCtxtDumpDTD:
1307 * @output: the FILE * for the output
1308 * @dtd: the DTD
1309 *
1310 * Dumps debug information for the DTD
1311 */
1312static void
1313xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
1314{
1315 if (dtd == NULL) {
1316 if (!ctxt->check)
1317 fprintf(ctxt->output, "DTD is NULL\n");
1318 return;
1319 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001320 xmlCtxtDumpDtdNode(ctxt, dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001321 if (dtd->children == NULL)
1322 fprintf(ctxt->output, " DTD is empty\n");
1323 else {
1324 ctxt->depth++;
1325 xmlCtxtDumpNodeList(ctxt, dtd->children);
1326 ctxt->depth--;
1327 }
1328}
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001329
Daniel Veillard22cdb842004-10-04 14:09:17 +00001330/************************************************************************
1331 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001332 * Public entry points for dump *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001333 * *
1334 ************************************************************************/
1335
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001336/**
1337 * xmlDebugDumpString:
1338 * @output: the FILE * for the output
1339 * @str: the string
1340 *
1341 * Dumps informations about the string, shorten it if necessary
1342 */
1343void
1344xmlDebugDumpString(FILE * output, const xmlChar * str)
1345{
Owen Taylor3473f882001-02-23 17:55:21 +00001346 int i;
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001347
Daniel Veillard7db38712002-02-07 16:39:11 +00001348 if (output == NULL)
1349 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00001350 if (str == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001351 fprintf(output, "(NULL)");
1352 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001353 }
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001354 for (i = 0; i < 40; i++)
1355 if (str[i] == 0)
1356 return;
William M. Brack76e95df2003-10-18 16:20:14 +00001357 else if (IS_BLANK_CH(str[i]))
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001358 fputc(' ', output);
1359 else if (str[i] >= 0x80)
1360 fprintf(output, "#%X", str[i]);
1361 else
1362 fputc(str[i], output);
Owen Taylor3473f882001-02-23 17:55:21 +00001363 fprintf(output, "...");
1364}
1365
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001366/**
1367 * xmlDebugDumpAttr:
1368 * @output: the FILE * for the output
1369 * @attr: the attribute
1370 * @depth: the indentation level.
1371 *
1372 * Dumps debug information for the attribute
1373 */
1374void
1375xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
Daniel Veillard22cdb842004-10-04 14:09:17 +00001376 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001377
Daniel Veillarda82b1822004-11-08 16:24:57 +00001378 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001379 xmlCtxtDumpInitCtxt(&ctxt);
1380 ctxt.output = output;
1381 ctxt.depth = depth;
1382 xmlCtxtDumpAttr(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001383 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001384}
Owen Taylor3473f882001-02-23 17:55:21 +00001385
Owen Taylor3473f882001-02-23 17:55:21 +00001386
Daniel Veillard22cdb842004-10-04 14:09:17 +00001387/**
1388 * xmlDebugDumpEntities:
1389 * @output: the FILE * for the output
1390 * @doc: the document
1391 *
1392 * Dumps debug information for all the entities in use by the document
1393 */
1394void
1395xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
1396{
1397 xmlDebugCtxt ctxt;
1398
Daniel Veillarda82b1822004-11-08 16:24:57 +00001399 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001400 xmlCtxtDumpInitCtxt(&ctxt);
1401 ctxt.output = output;
1402 xmlCtxtDumpEntities(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001403 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001404}
1405
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001406/**
1407 * xmlDebugDumpAttrList:
1408 * @output: the FILE * for the output
1409 * @attr: the attribute list
1410 * @depth: the indentation level.
1411 *
1412 * Dumps debug information for the attribute list
1413 */
1414void
1415xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
1416{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001417 xmlDebugCtxt ctxt;
1418
Daniel Veillarda82b1822004-11-08 16:24:57 +00001419 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001420 xmlCtxtDumpInitCtxt(&ctxt);
1421 ctxt.output = output;
1422 ctxt.depth = depth;
1423 xmlCtxtDumpAttrList(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001424 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001425}
1426
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001427/**
1428 * xmlDebugDumpOneNode:
1429 * @output: the FILE * for the output
1430 * @node: the node
1431 * @depth: the indentation level.
1432 *
1433 * Dumps debug information for the element node, it is not recursive
1434 */
1435void
1436xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
1437{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001438 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001439
Daniel Veillarda82b1822004-11-08 16:24:57 +00001440 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001441 xmlCtxtDumpInitCtxt(&ctxt);
1442 ctxt.output = output;
1443 ctxt.depth = depth;
1444 xmlCtxtDumpOneNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001445 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001446}
1447
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001448/**
1449 * xmlDebugDumpNode:
1450 * @output: the FILE * for the output
1451 * @node: the node
1452 * @depth: the indentation level.
1453 *
1454 * Dumps debug information for the element node, it is recursive
1455 */
1456void
1457xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
1458{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001459 xmlDebugCtxt ctxt;
1460
Daniel Veillard7db38712002-02-07 16:39:11 +00001461 if (output == NULL)
1462 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001463 xmlCtxtDumpInitCtxt(&ctxt);
1464 ctxt.output = output;
1465 ctxt.depth = depth;
1466 xmlCtxtDumpNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001467 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001468}
1469
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001470/**
1471 * xmlDebugDumpNodeList:
1472 * @output: the FILE * for the output
1473 * @node: the node list
1474 * @depth: the indentation level.
1475 *
1476 * Dumps debug information for the list of element node, it is recursive
1477 */
1478void
1479xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
1480{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001481 xmlDebugCtxt ctxt;
1482
Daniel Veillard7db38712002-02-07 16:39:11 +00001483 if (output == NULL)
1484 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001485 xmlCtxtDumpInitCtxt(&ctxt);
1486 ctxt.output = output;
1487 ctxt.depth = depth;
1488 xmlCtxtDumpNodeList(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001489 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001490}
1491
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001492/**
1493 * xmlDebugDumpDocumentHead:
1494 * @output: the FILE * for the output
1495 * @doc: the document
1496 *
1497 * Dumps debug information cncerning the document, not recursive
1498 */
1499void
1500xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
1501{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001502 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001503
Daniel Veillard22cdb842004-10-04 14:09:17 +00001504 if (output == NULL)
1505 output = stdout;
1506 xmlCtxtDumpInitCtxt(&ctxt);
Daniel Veillardcfa303a2005-08-25 14:03:56 +00001507 ctxt.options |= DUMP_TEXT_TYPE;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001508 ctxt.output = output;
1509 xmlCtxtDumpDocumentHead(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001510 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001511}
1512
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001513/**
1514 * xmlDebugDumpDocument:
1515 * @output: the FILE * for the output
1516 * @doc: the document
1517 *
1518 * Dumps debug information for the document, it's recursive
1519 */
1520void
1521xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
1522{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001523 xmlDebugCtxt ctxt;
1524
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001525 if (output == NULL)
Daniel Veillard22cdb842004-10-04 14:09:17 +00001526 output = stdout;
1527 xmlCtxtDumpInitCtxt(&ctxt);
Daniel Veillardcfa303a2005-08-25 14:03:56 +00001528 ctxt.options |= DUMP_TEXT_TYPE;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001529 ctxt.output = output;
1530 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001531 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001532}
Owen Taylor3473f882001-02-23 17:55:21 +00001533
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001534/**
1535 * xmlDebugDumpDTD:
1536 * @output: the FILE * for the output
1537 * @dtd: the DTD
1538 *
1539 * Dumps debug information for the DTD
1540 */
1541void
1542xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
1543{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001544 xmlDebugCtxt ctxt;
1545
Daniel Veillard7db38712002-02-07 16:39:11 +00001546 if (output == NULL)
1547 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001548 xmlCtxtDumpInitCtxt(&ctxt);
Daniel Veillardcfa303a2005-08-25 14:03:56 +00001549 ctxt.options |= DUMP_TEXT_TYPE;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001550 ctxt.output = output;
1551 xmlCtxtDumpDTD(&ctxt, dtd);
Daniel Veillard76821142004-10-09 20:39:04 +00001552 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001553}
1554
Daniel Veillard22cdb842004-10-04 14:09:17 +00001555/************************************************************************
1556 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001557 * Public entry points for checkings *
1558 * *
1559 ************************************************************************/
1560
1561/**
1562 * xmlDebugCheckDocument:
1563 * @output: the FILE * for the output
1564 * @doc: the document
1565 *
1566 * Check the document for potential content problems, and output
1567 * the errors to @output
1568 *
1569 * Returns the number of errors found
1570 */
1571int
1572xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
1573{
1574 xmlDebugCtxt ctxt;
1575
1576 if (output == NULL)
1577 output = stdout;
1578 xmlCtxtDumpInitCtxt(&ctxt);
1579 ctxt.output = output;
1580 ctxt.check = 1;
1581 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001582 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001583 return(ctxt.errors);
1584}
1585
1586/************************************************************************
1587 * *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001588 * Helpers for Shell *
1589 * *
1590 ************************************************************************/
Owen Taylor3473f882001-02-23 17:55:21 +00001591
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001592/**
1593 * xmlLsCountNode:
1594 * @node: the node to count
1595 *
1596 * Count the children of @node.
1597 *
1598 * Returns the number of children of @node.
1599 */
1600int
1601xmlLsCountNode(xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00001602 int ret = 0;
1603 xmlNodePtr list = NULL;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001604
1605 if (node == NULL)
1606 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001607
1608 switch (node->type) {
1609 case XML_ELEMENT_NODE:
1610 list = node->children;
1611 break;
1612 case XML_DOCUMENT_NODE:
1613 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00001614#ifdef LIBXML_DOCB_ENABLED
1615 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001616#endif
1617 list = ((xmlDocPtr) node)->children;
1618 break;
1619 case XML_ATTRIBUTE_NODE:
1620 list = ((xmlAttrPtr) node)->children;
1621 break;
1622 case XML_TEXT_NODE:
1623 case XML_CDATA_SECTION_NODE:
1624 case XML_PI_NODE:
1625 case XML_COMMENT_NODE:
1626 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001627 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001628 }
1629 break;
1630 case XML_ENTITY_REF_NODE:
1631 case XML_DOCUMENT_TYPE_NODE:
1632 case XML_ENTITY_NODE:
1633 case XML_DOCUMENT_FRAG_NODE:
1634 case XML_NOTATION_NODE:
1635 case XML_DTD_NODE:
1636 case XML_ELEMENT_DECL:
1637 case XML_ATTRIBUTE_DECL:
1638 case XML_ENTITY_DECL:
1639 case XML_NAMESPACE_DECL:
1640 case XML_XINCLUDE_START:
1641 case XML_XINCLUDE_END:
1642 ret = 1;
1643 break;
1644 }
1645 for (;list != NULL;ret++)
1646 list = list->next;
1647 return(ret);
1648}
1649
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001650/**
1651 * xmlLsOneNode:
1652 * @output: the FILE * for the output
1653 * @node: the node to dump
1654 *
1655 * Dump to @output the type and name of @node.
1656 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001657void
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001658xmlLsOneNode(FILE *output, xmlNodePtr node) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00001659 if (output == NULL) return;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001660 if (node == NULL) {
1661 fprintf(output, "NULL\n");
1662 return;
1663 }
Owen Taylor3473f882001-02-23 17:55:21 +00001664 switch (node->type) {
1665 case XML_ELEMENT_NODE:
1666 fprintf(output, "-");
1667 break;
1668 case XML_ATTRIBUTE_NODE:
1669 fprintf(output, "a");
1670 break;
1671 case XML_TEXT_NODE:
1672 fprintf(output, "t");
1673 break;
1674 case XML_CDATA_SECTION_NODE:
Daniel Veillard75be0132002-03-13 10:03:35 +00001675 fprintf(output, "C");
Owen Taylor3473f882001-02-23 17:55:21 +00001676 break;
1677 case XML_ENTITY_REF_NODE:
1678 fprintf(output, "e");
1679 break;
1680 case XML_ENTITY_NODE:
1681 fprintf(output, "E");
1682 break;
1683 case XML_PI_NODE:
1684 fprintf(output, "p");
1685 break;
1686 case XML_COMMENT_NODE:
1687 fprintf(output, "c");
1688 break;
1689 case XML_DOCUMENT_NODE:
1690 fprintf(output, "d");
1691 break;
1692 case XML_HTML_DOCUMENT_NODE:
1693 fprintf(output, "h");
1694 break;
1695 case XML_DOCUMENT_TYPE_NODE:
1696 fprintf(output, "T");
1697 break;
1698 case XML_DOCUMENT_FRAG_NODE:
1699 fprintf(output, "F");
1700 break;
1701 case XML_NOTATION_NODE:
1702 fprintf(output, "N");
1703 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001704 case XML_NAMESPACE_DECL:
1705 fprintf(output, "n");
1706 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001707 default:
1708 fprintf(output, "?");
1709 }
Daniel Veillarde6a55192002-01-14 17:11:53 +00001710 if (node->type != XML_NAMESPACE_DECL) {
1711 if (node->properties != NULL)
1712 fprintf(output, "a");
1713 else
1714 fprintf(output, "-");
1715 if (node->nsDef != NULL)
1716 fprintf(output, "n");
1717 else
1718 fprintf(output, "-");
1719 }
Owen Taylor3473f882001-02-23 17:55:21 +00001720
1721 fprintf(output, " %8d ", xmlLsCountNode(node));
1722
1723 switch (node->type) {
1724 case XML_ELEMENT_NODE:
1725 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001726 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001727 break;
1728 case XML_ATTRIBUTE_NODE:
1729 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001730 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001731 break;
1732 case XML_TEXT_NODE:
1733 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001734 xmlDebugDumpString(output, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001735 }
1736 break;
1737 case XML_CDATA_SECTION_NODE:
1738 break;
1739 case XML_ENTITY_REF_NODE:
1740 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001741 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001742 break;
1743 case XML_ENTITY_NODE:
1744 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001745 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001746 break;
1747 case XML_PI_NODE:
1748 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001749 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001750 break;
1751 case XML_COMMENT_NODE:
1752 break;
1753 case XML_DOCUMENT_NODE:
1754 break;
1755 case XML_HTML_DOCUMENT_NODE:
1756 break;
1757 case XML_DOCUMENT_TYPE_NODE:
1758 break;
1759 case XML_DOCUMENT_FRAG_NODE:
1760 break;
1761 case XML_NOTATION_NODE:
1762 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001763 case XML_NAMESPACE_DECL: {
1764 xmlNsPtr ns = (xmlNsPtr) node;
1765
1766 if (ns->prefix == NULL)
William M. Brack13dfa872004-09-18 04:52:08 +00001767 fprintf(output, "default -> %s", (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001768 else
William M. Brack13dfa872004-09-18 04:52:08 +00001769 fprintf(output, "%s -> %s", (char *)ns->prefix,
1770 (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001771 break;
1772 }
Owen Taylor3473f882001-02-23 17:55:21 +00001773 default:
1774 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001775 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001776 }
1777 fprintf(output, "\n");
1778}
1779
Daniel Veillard78d12092001-10-11 09:12:24 +00001780/**
1781 * xmlBoolToText:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001782 * @boolval: a bool to turn into text
Daniel Veillard78d12092001-10-11 09:12:24 +00001783 *
1784 * Convenient way to turn bool into text
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001785 *
1786 * Returns a pointer to either "True" or "False"
1787 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001788const char *
Daniel Veillardebd38c52001-11-01 08:38:12 +00001789xmlBoolToText(int boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001790{
Daniel Veillardebd38c52001-11-01 08:38:12 +00001791 if (boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001792 return("True");
1793 else
1794 return("False");
1795}
1796
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00001797#ifdef LIBXML_XPATH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001798/****************************************************************
1799 * *
1800 * The XML shell related functions *
1801 * *
1802 ****************************************************************/
1803
Daniel Veillard78d12092001-10-11 09:12:24 +00001804
1805
Owen Taylor3473f882001-02-23 17:55:21 +00001806/*
1807 * TODO: Improvement/cleanups for the XML shell
1808 * - allow to shell out an editor on a subpart
1809 * - cleanup function registrations (with help) and calling
1810 * - provide registration routines
1811 */
1812
1813/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001814 * xmlShellPrintXPathError:
Daniel Veillard78d12092001-10-11 09:12:24 +00001815 * @errorType: valid xpath error id
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001816 * @arg: the argument that cause xpath to fail
Daniel Veillard78d12092001-10-11 09:12:24 +00001817 *
1818 * Print the xpath error to libxml default error channel
1819 */
1820void
1821xmlShellPrintXPathError(int errorType, const char *arg)
1822{
1823 const char *default_arg = "Result";
1824
1825 if (!arg)
1826 arg = default_arg;
1827
1828 switch (errorType) {
1829 case XPATH_UNDEFINED:
1830 xmlGenericError(xmlGenericErrorContext,
1831 "%s: no such node\n", arg);
1832 break;
1833
1834 case XPATH_BOOLEAN:
1835 xmlGenericError(xmlGenericErrorContext,
1836 "%s is a Boolean\n", arg);
1837 break;
1838 case XPATH_NUMBER:
1839 xmlGenericError(xmlGenericErrorContext,
1840 "%s is a number\n", arg);
1841 break;
1842 case XPATH_STRING:
1843 xmlGenericError(xmlGenericErrorContext,
1844 "%s is a string\n", arg);
1845 break;
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 case XPATH_USERS:
1859 xmlGenericError(xmlGenericErrorContext,
1860 "%s is user-defined\n", arg);
1861 break;
1862 case XPATH_XSLT_TREE:
1863 xmlGenericError(xmlGenericErrorContext,
1864 "%s is an XSLT value tree\n", arg);
1865 break;
1866 }
Daniel Veillarda82b1822004-11-08 16:24:57 +00001867#if 0
Daniel Veillard78d12092001-10-11 09:12:24 +00001868 xmlGenericError(xmlGenericErrorContext,
1869 "Try casting the result string function (xpath builtin)\n",
1870 arg);
Daniel Veillarda82b1822004-11-08 16:24:57 +00001871#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00001872}
1873
1874
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001875#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001876/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001877 * xmlShellPrintNodeCtxt:
1878 * @ctxt : a non-null shell context
1879 * @node : a non-null node to print to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001880 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001881 * Print node to the output FILE
1882 */
1883static void
1884xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1885{
Daniel Veillard01992e02002-10-09 10:20:30 +00001886 FILE *fp;
1887
1888 if (!node)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001889 return;
Daniel Veillard01992e02002-10-09 10:20:30 +00001890 if (ctxt == NULL)
1891 fp = stdout;
1892 else
1893 fp = ctxt->output;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001894
1895 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001896 xmlDocDump(fp, (xmlDocPtr) node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001897 else if (node->type == XML_ATTRIBUTE_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001898 xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001899 else
Daniel Veillard01992e02002-10-09 10:20:30 +00001900 xmlElemDump(fp, node->doc, node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001901
Daniel Veillard01992e02002-10-09 10:20:30 +00001902 fprintf(fp, "\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00001903}
1904
1905/**
1906 * xmlShellPrintNode:
1907 * @node : a non-null node to print to the output FILE
1908 *
1909 * Print node to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001910 */
1911void
1912xmlShellPrintNode(xmlNodePtr node)
1913{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001914 xmlShellPrintNodeCtxt(NULL, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001915}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001916#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001917
Daniel Veillard78d12092001-10-11 09:12:24 +00001918/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001919 * xmlShellPrintXPathResultCtxt:
1920 * @ctxt: a valid shell context
Daniel Veillard9d06d302002-01-22 18:15:52 +00001921 * @list: a valid result generated by an xpath evaluation
Daniel Veillard78d12092001-10-11 09:12:24 +00001922 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001923 * Prints result to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001924 */
Daniel Veillard321be0c2002-10-08 21:26:42 +00001925static void
1926xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
Daniel Veillard78d12092001-10-11 09:12:24 +00001927{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001928 if (!ctxt)
1929 return;
Daniel Veillard78d12092001-10-11 09:12:24 +00001930
1931 if (list != NULL) {
1932 switch (list->type) {
1933 case XPATH_NODESET:{
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001934#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001935 int indx;
1936
1937 if (list->nodesetval) {
1938 for (indx = 0; indx < list->nodesetval->nodeNr;
1939 indx++) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001940 xmlShellPrintNodeCtxt(ctxt,
1941 list->nodesetval->nodeTab[indx]);
Daniel Veillard78d12092001-10-11 09:12:24 +00001942 }
1943 } else {
1944 xmlGenericError(xmlGenericErrorContext,
1945 "Empty node set\n");
1946 }
1947 break;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001948#else
1949 xmlGenericError(xmlGenericErrorContext,
1950 "Node set\n");
1951#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001952 }
1953 case XPATH_BOOLEAN:
1954 xmlGenericError(xmlGenericErrorContext,
1955 "Is a Boolean:%s\n",
1956 xmlBoolToText(list->boolval));
1957 break;
1958 case XPATH_NUMBER:
1959 xmlGenericError(xmlGenericErrorContext,
1960 "Is a number:%0g\n", list->floatval);
1961 break;
1962 case XPATH_STRING:
1963 xmlGenericError(xmlGenericErrorContext,
1964 "Is a string:%s\n", list->stringval);
1965 break;
1966
1967 default:
1968 xmlShellPrintXPathError(list->type, NULL);
1969 }
1970 }
1971}
1972
1973/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001974 * xmlShellPrintXPathResult:
1975 * @list: a valid result generated by an xpath evaluation
1976 *
1977 * Prints result to the output FILE
1978 */
1979void
1980xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1981{
1982 xmlShellPrintXPathResultCtxt(NULL, list);
1983}
1984
1985/**
Owen Taylor3473f882001-02-23 17:55:21 +00001986 * xmlShellList:
1987 * @ctxt: the shell context
1988 * @arg: unused
1989 * @node: a node
1990 * @node2: unused
1991 *
1992 * Implements the XML shell function "ls"
1993 * Does an Unix like listing of the given node (like a directory)
1994 *
1995 * Returns 0
1996 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001997int
Daniel Veillard321be0c2002-10-08 21:26:42 +00001998xmlShellList(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00001999 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2000 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2001{
Owen Taylor3473f882001-02-23 17:55:21 +00002002 xmlNodePtr cur;
Daniel Veillard321be0c2002-10-08 21:26:42 +00002003 if (!ctxt)
2004 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002005 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002006 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002007 return (0);
2008 }
Owen Taylor3473f882001-02-23 17:55:21 +00002009 if ((node->type == XML_DOCUMENT_NODE) ||
2010 (node->type == XML_HTML_DOCUMENT_NODE)) {
2011 cur = ((xmlDocPtr) node)->children;
Daniel Veillarde6a55192002-01-14 17:11:53 +00002012 } else if (node->type == XML_NAMESPACE_DECL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002013 xmlLsOneNode(ctxt->output, node);
Daniel Veillarde6a55192002-01-14 17:11:53 +00002014 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002015 } else if (node->children != NULL) {
2016 cur = node->children;
2017 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002018 xmlLsOneNode(ctxt->output, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002019 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002020 }
2021 while (cur != NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002022 xmlLsOneNode(ctxt->output, cur);
Daniel Veillard78d12092001-10-11 09:12:24 +00002023 cur = cur->next;
Owen Taylor3473f882001-02-23 17:55:21 +00002024 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002025 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002026}
2027
2028/**
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002029 * xmlShellBase:
2030 * @ctxt: the shell context
2031 * @arg: unused
2032 * @node: a node
2033 * @node2: unused
2034 *
2035 * Implements the XML shell function "base"
2036 * dumps the current XML base of the node
2037 *
2038 * Returns 0
2039 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002040int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002041xmlShellBase(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002042 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2043 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2044{
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002045 xmlChar *base;
Daniel Veillard321be0c2002-10-08 21:26:42 +00002046 if (!ctxt)
2047 return 0;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002048 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002049 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002050 return (0);
2051 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002052
2053 base = xmlNodeGetBase(node->doc, node);
2054
2055 if (base == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002056 fprintf(ctxt->output, " No base found !!!\n");
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002057 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002058 fprintf(ctxt->output, "%s\n", base);
Daniel Veillard78d12092001-10-11 09:12:24 +00002059 xmlFree(base);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002060 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002061 return (0);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002062}
2063
Daniel Veillardb34321c2004-03-04 17:09:47 +00002064#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002065/**
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002066 * xmlShellSetBase:
2067 * @ctxt: the shell context
2068 * @arg: the new base
2069 * @node: a node
2070 * @node2: unused
2071 *
2072 * Implements the XML shell function "setbase"
2073 * change the current XML base of the node
2074 *
2075 * Returns 0
2076 */
2077static int
2078xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2079 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2080 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2081{
2082 xmlNodeSetBase(node, (xmlChar*) arg);
2083 return (0);
2084}
Daniel Veillard2156d432004-03-04 15:59:36 +00002085#endif
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002086
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002087#ifdef LIBXML_XPATH_ENABLED
2088/**
2089 * xmlShellRegisterNamespace:
2090 * @ctxt: the shell context
2091 * @arg: a string in prefix=nsuri format
2092 * @node: unused
2093 * @node2: unused
2094 *
2095 * Implements the XML shell function "setns"
2096 * register/unregister a prefix=namespace pair
2097 * on the XPath context
2098 *
2099 * Returns 0 on success and a negative value otherwise.
2100 */
2101static int
2102xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
2103 xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2104{
2105 xmlChar* nsListDup;
2106 xmlChar* prefix;
2107 xmlChar* href;
2108 xmlChar* next;
2109
2110 nsListDup = xmlStrdup((xmlChar *) arg);
2111 next = nsListDup;
2112 while(next != NULL) {
2113 /* skip spaces */
2114 /*while((*next) == ' ') next++;*/
2115 if((*next) == '\0') break;
2116
2117 /* find prefix */
2118 prefix = next;
2119 next = (xmlChar*)xmlStrchr(next, '=');
2120 if(next == NULL) {
2121 fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
2122 xmlFree(nsListDup);
2123 return(-1);
2124 }
2125 *(next++) = '\0';
2126
2127 /* find href */
2128 href = next;
2129 next = (xmlChar*)xmlStrchr(next, ' ');
2130 if(next != NULL) {
2131 *(next++) = '\0';
2132 }
2133
2134 /* do register namespace */
2135 if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
2136 fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
2137 xmlFree(nsListDup);
2138 return(-1);
2139 }
2140 }
2141
2142 xmlFree(nsListDup);
2143 return(0);
2144}
Daniel Veillard20887ee2005-07-04 09:27:40 +00002145/**
2146 * xmlShellRegisterRootNamespaces:
2147 * @ctxt: the shell context
2148 * @arg: unused
2149 * @node: the root element
2150 * @node2: unused
2151 *
2152 * Implements the XML shell function "setrootns"
2153 * which registers all namespaces declarations found on the root element.
2154 *
2155 * Returns 0 on success and a negative value otherwise.
2156 */
2157static int
2158xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2159 xmlNodePtr root, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2160{
2161 xmlNsPtr ns;
2162
2163 if ((root == NULL) || (root->type != XML_ELEMENT_NODE) ||
2164 (root->nsDef == NULL) || (ctxt == NULL) || (ctxt->pctxt == NULL))
2165 return(-1);
2166 ns = root->nsDef;
2167 while (ns != NULL) {
2168 if (ns->prefix == NULL)
2169 xmlXPathRegisterNs(ctxt->pctxt, BAD_CAST "defaultns", ns->href);
2170 else
2171 xmlXPathRegisterNs(ctxt->pctxt, ns->prefix, ns->href);
2172 ns = ns->next;
2173 }
2174 return(0);
2175}
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002176#endif
2177
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002178/**
Daniel Veillard1e208222002-10-22 14:25:25 +00002179 * xmlShellGrep:
2180 * @ctxt: the shell context
2181 * @arg: the string or regular expression to find
2182 * @node: a node
2183 * @node2: unused
2184 *
2185 * Implements the XML shell function "grep"
2186 * dumps informations about the node (namespace, attributes, content).
2187 *
2188 * Returns 0
2189 */
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002190static int
Daniel Veillard1e208222002-10-22 14:25:25 +00002191xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2192 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2193{
2194 if (!ctxt)
2195 return (0);
2196 if (node == NULL)
2197 return (0);
2198 if (arg == NULL)
2199 return (0);
2200#ifdef LIBXML_REGEXP_ENABLED
2201 if ((xmlStrchr((xmlChar *) arg, '?')) ||
2202 (xmlStrchr((xmlChar *) arg, '*')) ||
2203 (xmlStrchr((xmlChar *) arg, '.')) ||
2204 (xmlStrchr((xmlChar *) arg, '['))) {
2205 }
2206#endif
2207 while (node != NULL) {
2208 if (node->type == XML_COMMENT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002209 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002210
2211 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
2212 xmlShellList(ctxt, NULL, node, NULL);
2213 }
2214 } else if (node->type == XML_TEXT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002215 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002216
2217 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002218 xmlShellList(ctxt, NULL, node->parent, NULL);
Daniel Veillard1e208222002-10-22 14:25:25 +00002219 }
2220 }
2221
2222 /*
2223 * Browse the full subtree, deep first
2224 */
2225
2226 if ((node->type == XML_DOCUMENT_NODE) ||
2227 (node->type == XML_HTML_DOCUMENT_NODE)) {
2228 node = ((xmlDocPtr) node)->children;
2229 } else if ((node->children != NULL)
2230 && (node->type != XML_ENTITY_REF_NODE)) {
2231 /* deep first */
2232 node = node->children;
2233 } else if (node->next != NULL) {
2234 /* then siblings */
2235 node = node->next;
2236 } else {
2237 /* go up to parents->next if needed */
2238 while (node != NULL) {
2239 if (node->parent != NULL) {
2240 node = node->parent;
2241 }
2242 if (node->next != NULL) {
2243 node = node->next;
2244 break;
2245 }
2246 if (node->parent == NULL) {
2247 node = NULL;
2248 break;
2249 }
2250 }
2251 }
2252 }
2253 return (0);
2254}
2255
2256/**
Owen Taylor3473f882001-02-23 17:55:21 +00002257 * xmlShellDir:
2258 * @ctxt: the shell context
2259 * @arg: unused
2260 * @node: a node
2261 * @node2: unused
2262 *
2263 * Implements the XML shell function "dir"
2264 * dumps informations about the node (namespace, attributes, content).
2265 *
2266 * Returns 0
2267 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002268int
2269xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2270 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2271 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2272{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002273 if (!ctxt)
2274 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002275 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002276 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002277 return (0);
2278 }
Owen Taylor3473f882001-02-23 17:55:21 +00002279 if ((node->type == XML_DOCUMENT_NODE) ||
2280 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002281 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
Owen Taylor3473f882001-02-23 17:55:21 +00002282 } else if (node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002283 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002284 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002285 xmlDebugDumpOneNode(ctxt->output, node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002286 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002287 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002288}
2289
Daniel Veillard29b17482004-08-16 00:39:03 +00002290/**
2291 * xmlShellSetContent:
2292 * @ctxt: the shell context
2293 * @value: the content as a string
2294 * @node: a node
2295 * @node2: unused
2296 *
2297 * Implements the XML shell function "dir"
2298 * dumps informations about the node (namespace, attributes, content).
2299 *
2300 * Returns 0
2301 */
2302static int
2303xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2304 char *value, xmlNodePtr node,
2305 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2306{
2307 xmlNodePtr results;
2308 xmlParserErrors ret;
2309
2310 if (!ctxt)
2311 return (0);
2312 if (node == NULL) {
2313 fprintf(ctxt->output, "NULL\n");
2314 return (0);
2315 }
2316 if (value == NULL) {
2317 fprintf(ctxt->output, "NULL\n");
2318 return (0);
2319 }
2320
2321 ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
2322 if (ret == XML_ERR_OK) {
2323 if (node->children != NULL) {
2324 xmlFreeNodeList(node->children);
2325 node->children = NULL;
2326 node->last = NULL;
2327 }
2328 xmlAddChildList(node, results);
2329 } else {
2330 fprintf(ctxt->output, "failed to parse content\n");
2331 }
2332 return (0);
2333}
2334
Daniel Veillard522bc602004-02-21 11:53:09 +00002335#ifdef LIBXML_SCHEMAS_ENABLED
2336/**
2337 * xmlShellRNGValidate:
2338 * @ctxt: the shell context
2339 * @schemas: the path to the Relax-NG schemas
2340 * @node: a node
2341 * @node2: unused
2342 *
2343 * Implements the XML shell function "relaxng"
2344 * validating the instance against a Relax-NG schemas
2345 *
2346 * Returns 0
2347 */
2348static int
2349xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
2350 xmlNodePtr node ATTRIBUTE_UNUSED,
2351 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2352{
2353 xmlRelaxNGPtr relaxngschemas;
2354 xmlRelaxNGParserCtxtPtr ctxt;
2355 xmlRelaxNGValidCtxtPtr vctxt;
2356 int ret;
2357
2358 ctxt = xmlRelaxNGNewParserCtxt(schemas);
2359 xmlRelaxNGSetParserErrors(ctxt,
2360 (xmlRelaxNGValidityErrorFunc) fprintf,
2361 (xmlRelaxNGValidityWarningFunc) fprintf,
2362 stderr);
2363 relaxngschemas = xmlRelaxNGParse(ctxt);
2364 xmlRelaxNGFreeParserCtxt(ctxt);
2365 if (relaxngschemas == NULL) {
2366 xmlGenericError(xmlGenericErrorContext,
2367 "Relax-NG schema %s failed to compile\n", schemas);
2368 return(-1);
2369 }
2370 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2371 xmlRelaxNGSetValidErrors(vctxt,
2372 (xmlRelaxNGValidityErrorFunc) fprintf,
2373 (xmlRelaxNGValidityWarningFunc) fprintf,
2374 stderr);
2375 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2376 if (ret == 0) {
2377 fprintf(stderr, "%s validates\n", sctxt->filename);
2378 } else if (ret > 0) {
2379 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2380 } else {
2381 fprintf(stderr, "%s validation generated an internal error\n",
2382 sctxt->filename);
2383 }
2384 xmlRelaxNGFreeValidCtxt(vctxt);
2385 if (relaxngschemas != NULL)
2386 xmlRelaxNGFree(relaxngschemas);
2387 return(0);
2388}
2389#endif
2390
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002391#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002392/**
2393 * xmlShellCat:
2394 * @ctxt: the shell context
2395 * @arg: unused
2396 * @node: a node
2397 * @node2: unused
2398 *
2399 * Implements the XML shell function "cat"
2400 * dumps the serialization node content (XML or HTML).
2401 *
2402 * Returns 0
2403 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002404int
2405xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2406 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2407{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002408 if (!ctxt)
2409 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002410 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002411 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002412 return (0);
2413 }
Owen Taylor3473f882001-02-23 17:55:21 +00002414 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2415#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002416 if (node->type == XML_HTML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002417 htmlDocDump(ctxt->output, (htmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002418 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002419 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002420#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002421 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002422 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002423 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002424 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002425#endif /* LIBXML_HTML_ENABLED */
2426 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00002427 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002428 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002429 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002430 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002431 }
Daniel Veillard321be0c2002-10-08 21:26:42 +00002432 fprintf(ctxt->output, "\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002433 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002434}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002435#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002436
2437/**
2438 * xmlShellLoad:
2439 * @ctxt: the shell context
2440 * @filename: the file name
2441 * @node: unused
2442 * @node2: unused
2443 *
2444 * Implements the XML shell function "load"
2445 * loads a new document specified by the filename
2446 *
2447 * Returns 0 or -1 if loading failed
2448 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002449int
2450xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2451 xmlNodePtr node ATTRIBUTE_UNUSED,
2452 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2453{
Owen Taylor3473f882001-02-23 17:55:21 +00002454 xmlDocPtr doc;
2455 int html = 0;
2456
Daniel Veillarda82b1822004-11-08 16:24:57 +00002457 if ((ctxt == NULL) || (filename == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002458 if (ctxt->doc != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002459 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00002460
2461 if (html) {
2462#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002463 doc = htmlParseFile(filename, NULL);
2464#else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002465 fprintf(ctxt->output, "HTML support not compiled in\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002466 doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002467#endif /* LIBXML_HTML_ENABLED */
2468 } else {
Daniel Veillardebe25d42004-03-25 09:35:49 +00002469 doc = xmlReadFile(filename,NULL,0);
Owen Taylor3473f882001-02-23 17:55:21 +00002470 }
2471 if (doc != NULL) {
2472 if (ctxt->loaded == 1) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002473 xmlFreeDoc(ctxt->doc);
2474 }
2475 ctxt->loaded = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002476#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002477 xmlXPathFreeContext(ctxt->pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00002478#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002479 xmlFree(ctxt->filename);
2480 ctxt->doc = doc;
2481 ctxt->node = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002482#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002483 ctxt->pctxt = xmlXPathNewContext(doc);
Owen Taylor3473f882001-02-23 17:55:21 +00002484#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard85095e22003-04-23 13:56:44 +00002485 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002486 } else
Daniel Veillard78d12092001-10-11 09:12:24 +00002487 return (-1);
2488 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002489}
2490
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002491#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002492/**
2493 * xmlShellWrite:
2494 * @ctxt: the shell context
2495 * @filename: the file name
2496 * @node: a node in the tree
2497 * @node2: unused
2498 *
2499 * Implements the XML shell function "write"
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002500 * Write the current node to the filename, it saves the serialization
Owen Taylor3473f882001-02-23 17:55:21 +00002501 * of the subtree under the @node specified
2502 *
2503 * Returns 0 or -1 in case of error
2504 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002505int
Owen Taylor3473f882001-02-23 17:55:21 +00002506xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Daniel Veillard78d12092001-10-11 09:12:24 +00002507 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2508{
Owen Taylor3473f882001-02-23 17:55:21 +00002509 if (node == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002510 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002511 if ((filename == NULL) || (filename[0] == 0)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002512 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002513 }
2514#ifdef W_OK
2515 if (access((char *) filename, W_OK)) {
2516 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002517 "Cannot write to %s\n", filename);
2518 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002519 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002520#endif
2521 switch (node->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002522 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002523 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2524 xmlGenericError(xmlGenericErrorContext,
2525 "Failed to write to %s\n", filename);
2526 return (-1);
2527 }
2528 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002529 case XML_HTML_DOCUMENT_NODE:
2530#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002531 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2532 xmlGenericError(xmlGenericErrorContext,
2533 "Failed to write to %s\n", filename);
2534 return (-1);
2535 }
Owen Taylor3473f882001-02-23 17:55:21 +00002536#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002537 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2538 xmlGenericError(xmlGenericErrorContext,
2539 "Failed to write to %s\n", filename);
2540 return (-1);
2541 }
Owen Taylor3473f882001-02-23 17:55:21 +00002542#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002543 break;
2544 default:{
2545 FILE *f;
Owen Taylor3473f882001-02-23 17:55:21 +00002546
Daniel Veillard78d12092001-10-11 09:12:24 +00002547 f = fopen((char *) filename, "w");
2548 if (f == NULL) {
2549 xmlGenericError(xmlGenericErrorContext,
2550 "Failed to write to %s\n", filename);
2551 return (-1);
2552 }
2553 xmlElemDump(f, ctxt->doc, node);
2554 fclose(f);
2555 }
Owen Taylor3473f882001-02-23 17:55:21 +00002556 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002557 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002558}
2559
2560/**
2561 * xmlShellSave:
2562 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002563 * @filename: the file name (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002564 * @node: unused
2565 * @node2: unused
2566 *
2567 * Implements the XML shell function "save"
2568 * Write the current document to the filename, or it's original name
2569 *
2570 * Returns 0 or -1 in case of error
2571 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002572int
2573xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2574 xmlNodePtr node ATTRIBUTE_UNUSED,
2575 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2576{
Daniel Veillarda82b1822004-11-08 16:24:57 +00002577 if ((ctxt == NULL) || (ctxt->doc == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002578 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002579 if ((filename == NULL) || (filename[0] == 0))
2580 filename = ctxt->filename;
Daniel Veillarda82b1822004-11-08 16:24:57 +00002581 if (filename == NULL)
2582 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002583#ifdef W_OK
2584 if (access((char *) filename, W_OK)) {
2585 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002586 "Cannot save to %s\n", filename);
2587 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002588 }
2589#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002590 switch (ctxt->doc->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002591 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002592 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2593 xmlGenericError(xmlGenericErrorContext,
2594 "Failed to save to %s\n", filename);
2595 }
2596 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002597 case XML_HTML_DOCUMENT_NODE:
2598#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002599 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2600 xmlGenericError(xmlGenericErrorContext,
2601 "Failed to save to %s\n", filename);
2602 }
Owen Taylor3473f882001-02-23 17:55:21 +00002603#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002604 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2605 xmlGenericError(xmlGenericErrorContext,
2606 "Failed to save to %s\n", filename);
2607 }
Owen Taylor3473f882001-02-23 17:55:21 +00002608#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002609 break;
2610 default:
2611 xmlGenericError(xmlGenericErrorContext,
2612 "To save to subparts of a document use the 'write' command\n");
2613 return (-1);
2614
Owen Taylor3473f882001-02-23 17:55:21 +00002615 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002616 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002617}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002618#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002619
Daniel Veillardf54cd532004-02-25 11:52:31 +00002620#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002621/**
2622 * xmlShellValidate:
2623 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002624 * @dtd: the DTD URI (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002625 * @node: unused
2626 * @node2: unused
2627 *
2628 * Implements the XML shell function "validate"
2629 * Validate the document, if a DTD path is provided, then the validation
2630 * is done against the given DTD.
2631 *
2632 * Returns 0 or -1 in case of error
2633 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002634int
2635xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2636 xmlNodePtr node ATTRIBUTE_UNUSED,
2637 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2638{
Owen Taylor3473f882001-02-23 17:55:21 +00002639 xmlValidCtxt vctxt;
2640 int res = -1;
2641
Daniel Veillarda82b1822004-11-08 16:24:57 +00002642 if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002643 vctxt.userData = stderr;
2644 vctxt.error = (xmlValidityErrorFunc) fprintf;
2645 vctxt.warning = (xmlValidityWarningFunc) fprintf;
2646
2647 if ((dtd == NULL) || (dtd[0] == 0)) {
2648 res = xmlValidateDocument(&vctxt, ctxt->doc);
2649 } else {
2650 xmlDtdPtr subset;
2651
Daniel Veillard78d12092001-10-11 09:12:24 +00002652 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2653 if (subset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002654 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2655
Daniel Veillard78d12092001-10-11 09:12:24 +00002656 xmlFreeDtd(subset);
2657 }
Owen Taylor3473f882001-02-23 17:55:21 +00002658 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002659 return (res);
Owen Taylor3473f882001-02-23 17:55:21 +00002660}
Daniel Veillardf54cd532004-02-25 11:52:31 +00002661#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002662
2663/**
2664 * xmlShellDu:
2665 * @ctxt: the shell context
2666 * @arg: unused
2667 * @tree: a node defining a subtree
2668 * @node2: unused
2669 *
2670 * Implements the XML shell function "du"
2671 * show the structure of the subtree under node @tree
2672 * If @tree is null, the command works on the current node.
2673 *
2674 * Returns 0 or -1 in case of error
2675 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002676int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002677xmlShellDu(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002678 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2679 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2680{
Owen Taylor3473f882001-02-23 17:55:21 +00002681 xmlNodePtr node;
Daniel Veillard78d12092001-10-11 09:12:24 +00002682 int indent = 0, i;
Owen Taylor3473f882001-02-23 17:55:21 +00002683
Daniel Veillard321be0c2002-10-08 21:26:42 +00002684 if (!ctxt)
2685 return (-1);
2686
Daniel Veillard78d12092001-10-11 09:12:24 +00002687 if (tree == NULL)
2688 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002689 node = tree;
2690 while (node != NULL) {
2691 if ((node->type == XML_DOCUMENT_NODE) ||
2692 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002693 fprintf(ctxt->output, "/\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002694 } else if (node->type == XML_ELEMENT_NODE) {
2695 for (i = 0; i < indent; i++)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002696 fprintf(ctxt->output, " ");
2697 fprintf(ctxt->output, "%s\n", node->name);
Daniel Veillard78d12092001-10-11 09:12:24 +00002698 } else {
2699 }
Owen Taylor3473f882001-02-23 17:55:21 +00002700
Daniel Veillard78d12092001-10-11 09:12:24 +00002701 /*
2702 * Browse the full subtree, deep first
2703 */
Owen Taylor3473f882001-02-23 17:55:21 +00002704
2705 if ((node->type == XML_DOCUMENT_NODE) ||
2706 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002707 node = ((xmlDocPtr) node)->children;
2708 } else if ((node->children != NULL)
2709 && (node->type != XML_ENTITY_REF_NODE)) {
2710 /* deep first */
2711 node = node->children;
2712 indent++;
2713 } else if ((node != tree) && (node->next != NULL)) {
2714 /* then siblings */
2715 node = node->next;
2716 } else if (node != tree) {
2717 /* go up to parents->next if needed */
2718 while (node != tree) {
2719 if (node->parent != NULL) {
2720 node = node->parent;
2721 indent--;
2722 }
2723 if ((node != tree) && (node->next != NULL)) {
2724 node = node->next;
2725 break;
2726 }
2727 if (node->parent == NULL) {
2728 node = NULL;
2729 break;
2730 }
2731 if (node == tree) {
2732 node = NULL;
2733 break;
2734 }
2735 }
2736 /* exit condition */
2737 if (node == tree)
2738 node = NULL;
2739 } else
2740 node = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002741 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002742 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002743}
2744
2745/**
2746 * xmlShellPwd:
2747 * @ctxt: the shell context
2748 * @buffer: the output buffer
Daniel Veillard9d06d302002-01-22 18:15:52 +00002749 * @node: a node
Owen Taylor3473f882001-02-23 17:55:21 +00002750 * @node2: unused
2751 *
2752 * Implements the XML shell function "pwd"
2753 * Show the full path from the root to the node, if needed building
2754 * thumblers when similar elements exists at a given ancestor level.
2755 * The output is compatible with XPath commands.
2756 *
2757 * Returns 0 or -1 in case of error
2758 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002759int
2760xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2761 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2762{
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002763 xmlChar *path;
Owen Taylor3473f882001-02-23 17:55:21 +00002764
Daniel Veillarda82b1822004-11-08 16:24:57 +00002765 if ((node == NULL) || (buffer == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002766 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002767
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002768 path = xmlGetNodePath(node);
2769 if (path == NULL)
2770 return (-1);
2771
2772 /*
2773 * This test prevents buffer overflow, because this routine
2774 * is only called by xmlShell, in which the second argument is
2775 * 500 chars long.
2776 * It is a dirty hack before a cleaner solution is found.
2777 * Documentation should mention that the second argument must
2778 * be at least 500 chars long, and could be stripped if too long.
2779 */
2780 snprintf(buffer, 499, "%s", path);
2781 buffer[499] = '0';
2782 xmlFree(path);
2783
Daniel Veillard78d12092001-10-11 09:12:24 +00002784 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002785}
2786
2787/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002788 * xmlShell:
Owen Taylor3473f882001-02-23 17:55:21 +00002789 * @doc: the initial document
2790 * @filename: the output buffer
2791 * @input: the line reading function
Daniel Veillard321be0c2002-10-08 21:26:42 +00002792 * @output: the output FILE*, defaults to stdout if NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002793 *
2794 * Implements the XML shell
2795 * This allow to load, validate, view, modify and save a document
2796 * using a environment similar to a UNIX commandline.
2797 */
2798void
2799xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Daniel Veillard78d12092001-10-11 09:12:24 +00002800 FILE * output)
2801{
Owen Taylor3473f882001-02-23 17:55:21 +00002802 char prompt[500] = "/ > ";
2803 char *cmdline = NULL, *cur;
2804 int nbargs;
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)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002818 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00002819 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Daniel Veillard78d12092001-10-11 09:12:24 +00002820 if (ctxt == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00002821 return;
2822 ctxt->loaded = 0;
2823 ctxt->doc = doc;
2824 ctxt->input = input;
2825 ctxt->output = output;
2826 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Daniel Veillard78d12092001-10-11 09:12:24 +00002827 ctxt->node = (xmlNodePtr) ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002828
2829#ifdef LIBXML_XPATH_ENABLED
2830 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2831 if (ctxt->pctxt == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002832 xmlFree(ctxt);
2833 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002834 }
2835#endif /* LIBXML_XPATH_ENABLED */
2836 while (1) {
2837 if (ctxt->node == (xmlNodePtr) ctxt->doc)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002838 snprintf(prompt, sizeof(prompt), "%s > ", "/");
Daniel Veillard7a985a12003-07-06 17:57:42 +00002839 else if ((ctxt->node != NULL) && (ctxt->node->name))
Daniel Veillard78d12092001-10-11 09:12:24 +00002840 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002841 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002842 snprintf(prompt, sizeof(prompt), "? > ");
Owen Taylor3473f882001-02-23 17:55:21 +00002843 prompt[sizeof(prompt) - 1] = 0;
2844
Daniel Veillard78d12092001-10-11 09:12:24 +00002845 /*
2846 * Get a new command line
2847 */
Owen Taylor3473f882001-02-23 17:55:21 +00002848 cmdline = ctxt->input(prompt);
Daniel Veillard78d12092001-10-11 09:12:24 +00002849 if (cmdline == NULL)
2850 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002851
Daniel Veillard78d12092001-10-11 09:12:24 +00002852 /*
2853 * Parse the command itself
2854 */
2855 cur = cmdline;
2856 nbargs = 0;
2857 while ((*cur == ' ') || (*cur == '\t'))
2858 cur++;
2859 i = 0;
2860 while ((*cur != ' ') && (*cur != '\t') &&
2861 (*cur != '\n') && (*cur != '\r')) {
2862 if (*cur == 0)
2863 break;
2864 command[i++] = *cur++;
2865 }
2866 command[i] = 0;
2867 if (i == 0)
2868 continue;
2869 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002870
Daniel Veillard78d12092001-10-11 09:12:24 +00002871 /*
2872 * Parse the argument
2873 */
2874 while ((*cur == ' ') || (*cur == '\t'))
2875 cur++;
2876 i = 0;
2877 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2878 if (*cur == 0)
2879 break;
2880 arg[i++] = *cur++;
2881 }
2882 arg[i] = 0;
2883 if (i != 0)
2884 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002885
Daniel Veillard78d12092001-10-11 09:12:24 +00002886 /*
2887 * start interpreting the command
2888 */
Owen Taylor3473f882001-02-23 17:55:21 +00002889 if (!strcmp(command, "exit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002890 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002891 if (!strcmp(command, "quit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002892 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002893 if (!strcmp(command, "bye"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002894 break;
Daniel Veillard5004f422001-11-08 13:53:05 +00002895 if (!strcmp(command, "help")) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002896 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2897 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2898 fprintf(ctxt->output, "\tbye leave shell\n");
2899 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2900 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2901 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2902 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2903 fprintf(ctxt->output, "\texit leave shell\n");
2904 fprintf(ctxt->output, "\thelp display this help\n");
2905 fprintf(ctxt->output, "\tfree display memory usage\n");
2906 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2907 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00002908 fprintf(ctxt->output, "\tset xml_fragment replace the current node content with the fragment parsed in context\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002909#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002910 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002911 fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2912 fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
Daniel Veillard20887ee2005-07-04 09:27:40 +00002913 fprintf(ctxt->output, "\tsetrootns register all namespace found on the root element\n");
2914 fprintf(ctxt->output, "\t the default namespace if any uses 'defaultns' prefix\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002915#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard321be0c2002-10-08 21:26:42 +00002916 fprintf(ctxt->output, "\tpwd display current working directory\n");
2917 fprintf(ctxt->output, "\tquit leave shell\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002918#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002919 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00002920 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002921#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf54cd532004-02-25 11:52:31 +00002922#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002923 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002924#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard522bc602004-02-21 11:53:09 +00002925#ifdef LIBXML_SCHEMAS_ENABLED
2926 fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n");
2927#endif
Daniel Veillard1e208222002-10-22 14:25:25 +00002928 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002929#ifdef LIBXML_VALID_ENABLED
Daniel Veillard5004f422001-11-08 13:53:05 +00002930 } else if (!strcmp(command, "validate")) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002931 xmlShellValidate(ctxt, arg, NULL, NULL);
Daniel Veillardf54cd532004-02-25 11:52:31 +00002932#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002933 } else if (!strcmp(command, "load")) {
2934 xmlShellLoad(ctxt, arg, NULL, NULL);
Daniel Veillard522bc602004-02-21 11:53:09 +00002935#ifdef LIBXML_SCHEMAS_ENABLED
2936 } else if (!strcmp(command, "relaxng")) {
2937 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2938#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002939#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002940 } else if (!strcmp(command, "save")) {
2941 xmlShellSave(ctxt, arg, NULL, NULL);
2942 } else if (!strcmp(command, "write")) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00002943 if ((arg == NULL) || (arg[0] == 0))
2944 xmlGenericError(xmlGenericErrorContext,
2945 "Write command requires a filename argument\n");
2946 else
2947 xmlShellWrite(ctxt, arg, NULL, NULL);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002948#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard1e208222002-10-22 14:25:25 +00002949 } else if (!strcmp(command, "grep")) {
2950 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
Daniel Veillard78d12092001-10-11 09:12:24 +00002951 } else if (!strcmp(command, "free")) {
2952 if (arg[0] == 0) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002953 xmlMemShow(ctxt->output, 0);
Daniel Veillard78d12092001-10-11 09:12:24 +00002954 } else {
2955 int len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002956
Daniel Veillard78d12092001-10-11 09:12:24 +00002957 sscanf(arg, "%d", &len);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002958 xmlMemShow(ctxt->output, len);
Daniel Veillard78d12092001-10-11 09:12:24 +00002959 }
2960 } else if (!strcmp(command, "pwd")) {
2961 char dir[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002962
Daniel Veillard78d12092001-10-11 09:12:24 +00002963 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
Daniel Veillard321be0c2002-10-08 21:26:42 +00002964 fprintf(ctxt->output, "%s\n", dir);
Daniel Veillard78d12092001-10-11 09:12:24 +00002965 } else if (!strcmp(command, "du")) {
2966 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2967 } else if (!strcmp(command, "base")) {
2968 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
Daniel Veillard29b17482004-08-16 00:39:03 +00002969 } else if (!strcmp(command, "set")) {
2970 xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002971#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002972 } else if (!strcmp(command, "setns")) {
2973 if (arg[0] == 0) {
2974 xmlGenericError(xmlGenericErrorContext,
2975 "setns: prefix=[nsuri] required\n");
2976 } else {
2977 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
2978 }
Daniel Veillard20887ee2005-07-04 09:27:40 +00002979 } else if (!strcmp(command, "setrootns")) {
2980 xmlNodePtr root;
2981
2982 root = xmlDocGetRootElement(ctxt->doc);
2983 xmlShellRegisterRootNamespaces(ctxt, NULL, root, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002984 } else if (!strcmp(command, "xpath")) {
2985 if (arg[0] == 0) {
2986 xmlGenericError(xmlGenericErrorContext,
2987 "xpath: expression required\n");
2988 } else {
2989 ctxt->pctxt->node = ctxt->node;
2990 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002991 xmlXPathDebugDumpObject(ctxt->output, list, 0);
Daniel Veillard2070c482002-01-22 22:12:19 +00002992 xmlXPathFreeObject(list);
2993 }
2994#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard2156d432004-03-04 15:59:36 +00002995#ifdef LIBXML_TREE_ENABLED
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002996 } else if (!strcmp(command, "setbase")) {
2997 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2156d432004-03-04 15:59:36 +00002998#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002999 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
3000 int dir = (!strcmp(command, "dir"));
3001
3002 if (arg[0] == 0) {
3003 if (dir)
3004 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
3005 else
3006 xmlShellList(ctxt, NULL, ctxt->node, NULL);
3007 } else {
3008 ctxt->pctxt->node = ctxt->node;
Daniel Veillard61d80a22001-04-27 17:13:01 +00003009#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00003010 ctxt->pctxt->node = ctxt->node;
3011 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3012#else
3013 list = NULL;
3014#endif /* LIBXML_XPATH_ENABLED */
3015 if (list != NULL) {
3016 switch (list->type) {
3017 case XPATH_UNDEFINED:
3018 xmlGenericError(xmlGenericErrorContext,
3019 "%s: no such node\n", arg);
3020 break;
3021 case XPATH_NODESET:{
3022 int indx;
3023
Daniel Veillarda6825e82001-11-07 13:33:59 +00003024 if (list->nodesetval == NULL)
3025 break;
3026
Daniel Veillard78d12092001-10-11 09:12:24 +00003027 for (indx = 0;
3028 indx < list->nodesetval->nodeNr;
3029 indx++) {
3030 if (dir)
3031 xmlShellDir(ctxt, NULL,
3032 list->nodesetval->
3033 nodeTab[indx], NULL);
3034 else
3035 xmlShellList(ctxt, NULL,
3036 list->nodesetval->
3037 nodeTab[indx], NULL);
3038 }
3039 break;
3040 }
3041 case XPATH_BOOLEAN:
3042 xmlGenericError(xmlGenericErrorContext,
3043 "%s is a Boolean\n", arg);
3044 break;
3045 case XPATH_NUMBER:
3046 xmlGenericError(xmlGenericErrorContext,
3047 "%s is a number\n", arg);
3048 break;
3049 case XPATH_STRING:
3050 xmlGenericError(xmlGenericErrorContext,
3051 "%s is a string\n", arg);
3052 break;
3053 case XPATH_POINT:
3054 xmlGenericError(xmlGenericErrorContext,
3055 "%s is a point\n", arg);
3056 break;
3057 case XPATH_RANGE:
3058 xmlGenericError(xmlGenericErrorContext,
3059 "%s is a range\n", arg);
3060 break;
3061 case XPATH_LOCATIONSET:
3062 xmlGenericError(xmlGenericErrorContext,
3063 "%s is a range\n", arg);
3064 break;
3065 case XPATH_USERS:
3066 xmlGenericError(xmlGenericErrorContext,
3067 "%s is user-defined\n", arg);
3068 break;
3069 case XPATH_XSLT_TREE:
3070 xmlGenericError(xmlGenericErrorContext,
3071 "%s is an XSLT value tree\n",
3072 arg);
3073 break;
3074 }
3075#ifdef LIBXML_XPATH_ENABLED
3076 xmlXPathFreeObject(list);
Daniel Veillard61d80a22001-04-27 17:13:01 +00003077#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00003078 } else {
3079 xmlGenericError(xmlGenericErrorContext,
3080 "%s: no such node\n", arg);
3081 }
3082 ctxt->pctxt->node = NULL;
3083 }
3084 } else if (!strcmp(command, "cd")) {
3085 if (arg[0] == 0) {
3086 ctxt->node = (xmlNodePtr) ctxt->doc;
3087 } else {
3088#ifdef LIBXML_XPATH_ENABLED
3089 ctxt->pctxt->node = ctxt->node;
3090 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3091#else
3092 list = NULL;
3093#endif /* LIBXML_XPATH_ENABLED */
3094 if (list != NULL) {
3095 switch (list->type) {
3096 case XPATH_UNDEFINED:
3097 xmlGenericError(xmlGenericErrorContext,
3098 "%s: no such node\n", arg);
3099 break;
3100 case XPATH_NODESET:
Daniel Veillarda6825e82001-11-07 13:33:59 +00003101 if (list->nodesetval != NULL) {
3102 if (list->nodesetval->nodeNr == 1) {
3103 ctxt->node = list->nodesetval->nodeTab[0];
Daniel Veillard7a985a12003-07-06 17:57:42 +00003104 if ((ctxt->node != NULL) &&
3105 (ctxt->node->type ==
3106 XML_NAMESPACE_DECL)) {
3107 xmlGenericError(xmlGenericErrorContext,
3108 "cannot cd to namespace\n");
3109 ctxt->node = NULL;
3110 }
Daniel Veillarda6825e82001-11-07 13:33:59 +00003111 } else
3112 xmlGenericError(xmlGenericErrorContext,
3113 "%s is a %d Node Set\n",
3114 arg,
3115 list->nodesetval->nodeNr);
Daniel Veillard78d12092001-10-11 09:12:24 +00003116 } else
3117 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda6825e82001-11-07 13:33:59 +00003118 "%s is an empty Node Set\n",
3119 arg);
Daniel Veillard78d12092001-10-11 09:12:24 +00003120 break;
3121 case XPATH_BOOLEAN:
3122 xmlGenericError(xmlGenericErrorContext,
3123 "%s is a Boolean\n", arg);
3124 break;
3125 case XPATH_NUMBER:
3126 xmlGenericError(xmlGenericErrorContext,
3127 "%s is a number\n", arg);
3128 break;
3129 case XPATH_STRING:
3130 xmlGenericError(xmlGenericErrorContext,
3131 "%s is a string\n", arg);
3132 break;
3133 case XPATH_POINT:
3134 xmlGenericError(xmlGenericErrorContext,
3135 "%s is a point\n", arg);
3136 break;
3137 case XPATH_RANGE:
3138 xmlGenericError(xmlGenericErrorContext,
3139 "%s is a range\n", arg);
3140 break;
3141 case XPATH_LOCATIONSET:
3142 xmlGenericError(xmlGenericErrorContext,
3143 "%s is a range\n", arg);
3144 break;
3145 case XPATH_USERS:
3146 xmlGenericError(xmlGenericErrorContext,
3147 "%s is user-defined\n", arg);
3148 break;
3149 case XPATH_XSLT_TREE:
3150 xmlGenericError(xmlGenericErrorContext,
3151 "%s is an XSLT value tree\n",
3152 arg);
3153 break;
3154 }
3155#ifdef LIBXML_XPATH_ENABLED
3156 xmlXPathFreeObject(list);
3157#endif
3158 } else {
3159 xmlGenericError(xmlGenericErrorContext,
3160 "%s: no such node\n", arg);
3161 }
3162 ctxt->pctxt->node = NULL;
3163 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003164#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00003165 } else if (!strcmp(command, "cat")) {
3166 if (arg[0] == 0) {
3167 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3168 } else {
3169 ctxt->pctxt->node = ctxt->node;
3170#ifdef LIBXML_XPATH_ENABLED
3171 ctxt->pctxt->node = ctxt->node;
3172 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3173#else
3174 list = NULL;
3175#endif /* LIBXML_XPATH_ENABLED */
3176 if (list != NULL) {
3177 switch (list->type) {
3178 case XPATH_UNDEFINED:
3179 xmlGenericError(xmlGenericErrorContext,
3180 "%s: no such node\n", arg);
3181 break;
3182 case XPATH_NODESET:{
3183 int indx;
3184
Daniel Veillarda6825e82001-11-07 13:33:59 +00003185 if (list->nodesetval == NULL)
3186 break;
3187
Daniel Veillard78d12092001-10-11 09:12:24 +00003188 for (indx = 0;
3189 indx < list->nodesetval->nodeNr;
3190 indx++) {
3191 if (i > 0)
Daniel Veillard321be0c2002-10-08 21:26:42 +00003192 fprintf(ctxt->output, " -------\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00003193 xmlShellCat(ctxt, NULL,
3194 list->nodesetval->
3195 nodeTab[indx], NULL);
3196 }
3197 break;
3198 }
3199 case XPATH_BOOLEAN:
3200 xmlGenericError(xmlGenericErrorContext,
3201 "%s is a Boolean\n", arg);
3202 break;
3203 case XPATH_NUMBER:
3204 xmlGenericError(xmlGenericErrorContext,
3205 "%s is a number\n", arg);
3206 break;
3207 case XPATH_STRING:
3208 xmlGenericError(xmlGenericErrorContext,
3209 "%s is a string\n", arg);
3210 break;
3211 case XPATH_POINT:
3212 xmlGenericError(xmlGenericErrorContext,
3213 "%s is a point\n", arg);
3214 break;
3215 case XPATH_RANGE:
3216 xmlGenericError(xmlGenericErrorContext,
3217 "%s is a range\n", arg);
3218 break;
3219 case XPATH_LOCATIONSET:
3220 xmlGenericError(xmlGenericErrorContext,
3221 "%s is a range\n", arg);
3222 break;
3223 case XPATH_USERS:
3224 xmlGenericError(xmlGenericErrorContext,
3225 "%s is user-defined\n", arg);
3226 break;
3227 case XPATH_XSLT_TREE:
3228 xmlGenericError(xmlGenericErrorContext,
3229 "%s is an XSLT value tree\n",
3230 arg);
3231 break;
3232 }
3233#ifdef LIBXML_XPATH_ENABLED
3234 xmlXPathFreeObject(list);
3235#endif
3236 } else {
3237 xmlGenericError(xmlGenericErrorContext,
3238 "%s: no such node\n", arg);
3239 }
3240 ctxt->pctxt->node = NULL;
3241 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003242#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00003243 } else {
3244 xmlGenericError(xmlGenericErrorContext,
3245 "Unknown command %s\n", command);
3246 }
3247 free(cmdline); /* not xmlFree here ! */
Daniel Veillard30663512008-02-21 22:31:55 +00003248 cmdline = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003249 }
3250#ifdef LIBXML_XPATH_ENABLED
3251 xmlXPathFreeContext(ctxt->pctxt);
3252#endif /* LIBXML_XPATH_ENABLED */
3253 if (ctxt->loaded) {
3254 xmlFreeDoc(ctxt->doc);
3255 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003256 if (ctxt->filename != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003257 xmlFree(ctxt->filename);
Owen Taylor3473f882001-02-23 17:55:21 +00003258 xmlFree(ctxt);
3259 if (cmdline != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003260 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003261}
3262
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00003263#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00003264#define bottom_debugXML
3265#include "elfgcchack.h"
Owen Taylor3473f882001-02-23 17:55:21 +00003266#endif /* LIBXML_DEBUG_ENABLED */