blob: 3985ad38b94e9e9b029019a3f18a32d1835e15ad [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)
144 fprintf(ctxt->output, &ctxt->shift[100 - 2 * ctxt->depth]);
145 else
146 fprintf(ctxt->output, ctxt->shift);
147 }
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,
165 msg);
166}
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) &&
323 (node->parent->last != node))
324 xmlDebugErr(ctxt, XML_CHECK_NO_NEXT,
325 "Node has no next and not last of parent list\n");
326 } else {
327 if (node->next->prev != node)
328 xmlDebugErr(ctxt, XML_CHECK_WRONG_NEXT,
329 "Node next->prev : forward link wrong\n");
Daniel Veillard0d24b112004-10-11 12:28:34 +0000330 if (node->next->parent != node->parent)
331 xmlDebugErr(ctxt, XML_CHECK_WRONG_PARENT,
332 "Node next->prev : forward link wrong\n");
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000333 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000334 if (node->type == XML_ELEMENT_NODE) {
335 xmlNsPtr ns;
336
337 ns = node->nsDef;
338 while (ns != NULL) {
339 xmlCtxtNsCheckScope(ctxt, node, ns);
340 ns = ns->next;
341 }
342 if (node->ns != NULL)
343 xmlCtxtNsCheckScope(ctxt, node, node->ns);
344 } else if (node->type == XML_ATTRIBUTE_NODE) {
345 if (node->ns != NULL)
346 xmlCtxtNsCheckScope(ctxt, node, node->ns);
347 }
348
Daniel Veillardc6095782004-10-15 14:50:10 +0000349 if ((node->type != XML_ELEMENT_NODE) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000350 (node->type != XML_ATTRIBUTE_NODE) &&
Daniel Veillardf2e066a2005-06-30 13:04:44 +0000351 (node->type != XML_ELEMENT_DECL) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000352 (node->type != XML_ATTRIBUTE_DECL) &&
353 (node->type != XML_DTD_NODE) &&
William M. Brack0357a302005-07-06 17:39:14 +0000354 (node->type != XML_ELEMENT_DECL) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000355 (node->type != XML_HTML_DOCUMENT_NODE) &&
356 (node->type != XML_DOCUMENT_NODE)) {
Daniel Veillardc6095782004-10-15 14:50:10 +0000357 if (node->content != NULL)
William M. Brack9638d4c2004-10-15 18:25:33 +0000358 xmlCtxtCheckString(ctxt, (const xmlChar *) node->content);
Daniel Veillardc6095782004-10-15 14:50:10 +0000359 }
Daniel Veillard03a53c32004-10-26 16:06:51 +0000360 switch (node->type) {
361 case XML_ELEMENT_NODE:
362 case XML_ATTRIBUTE_NODE:
363 xmlCtxtCheckName(ctxt, node->name);
364 break;
365 case XML_TEXT_NODE:
366 if ((node->name == xmlStringText) ||
367 (node->name == xmlStringTextNoenc))
368 break;
369 /* some case of entity substitution can lead to this */
370 if ((ctxt->dict != NULL) &&
Daniel Veillard6927b102004-10-27 17:29:04 +0000371 (node->name == xmlDictLookup(ctxt->dict, BAD_CAST "nbktext",
372 7)))
Daniel Veillard03a53c32004-10-26 16:06:51 +0000373 break;
374
375 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
376 "Text node has wrong name '%s'",
377 (const char *) node->name);
378 break;
379 case XML_COMMENT_NODE:
380 if (node->name == xmlStringComment)
381 break;
382 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
383 "Comment node has wrong name '%s'",
384 (const char *) node->name);
385 break;
386 case XML_PI_NODE:
387 xmlCtxtCheckName(ctxt, node->name);
388 break;
389 case XML_CDATA_SECTION_NODE:
390 if (node->name == NULL)
391 break;
392 xmlDebugErr3(ctxt, XML_CHECK_NAME_NOT_NULL,
393 "CData section has non NULL name '%s'",
394 (const char *) node->name);
395 break;
396 case XML_ENTITY_REF_NODE:
397 case XML_ENTITY_NODE:
398 case XML_DOCUMENT_TYPE_NODE:
399 case XML_DOCUMENT_FRAG_NODE:
400 case XML_NOTATION_NODE:
401 case XML_DTD_NODE:
402 case XML_ELEMENT_DECL:
403 case XML_ATTRIBUTE_DECL:
404 case XML_ENTITY_DECL:
405 case XML_NAMESPACE_DECL:
406 case XML_XINCLUDE_START:
407 case XML_XINCLUDE_END:
408#ifdef LIBXML_DOCB_ENABLED
409 case XML_DOCB_DOCUMENT_NODE:
410#endif
411 case XML_DOCUMENT_NODE:
412 case XML_HTML_DOCUMENT_NODE:
413 break;
414 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000415}
416
Daniel Veillard22cdb842004-10-04 14:09:17 +0000417static void
418xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
419{
420 int i;
421
Daniel Veillardc6095782004-10-15 14:50:10 +0000422 if (ctxt->check) {
Daniel Veillard22cdb842004-10-04 14:09:17 +0000423 return;
Daniel Veillardc6095782004-10-15 14:50:10 +0000424 }
Daniel Veillard22cdb842004-10-04 14:09:17 +0000425 /* TODO: check UTF8 content of the string */
426 if (str == NULL) {
427 fprintf(ctxt->output, "(NULL)");
428 return;
429 }
430 for (i = 0; i < 40; i++)
431 if (str[i] == 0)
432 return;
433 else if (IS_BLANK_CH(str[i]))
434 fputc(' ', ctxt->output);
435 else if (str[i] >= 0x80)
436 fprintf(ctxt->output, "#%X", str[i]);
437 else
438 fputc(str[i], ctxt->output);
439 fprintf(ctxt->output, "...");
440}
441
442static void
443xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
444{
445 xmlCtxtDumpSpaces(ctxt);
446
447 if (dtd == NULL) {
448 if (!ctxt->check)
449 fprintf(ctxt->output, "DTD node is NULL\n");
450 return;
451 }
452
453 if (dtd->type != XML_DTD_NODE) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000454 xmlDebugErr(ctxt, XML_CHECK_NOT_DTD,
455 "Node is not a DTD");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000456 return;
457 }
458 if (!ctxt->check) {
459 if (dtd->name != NULL)
460 fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name);
461 else
462 fprintf(ctxt->output, "DTD");
463 if (dtd->ExternalID != NULL)
464 fprintf(ctxt->output, ", PUBLIC %s", (char *) dtd->ExternalID);
465 if (dtd->SystemID != NULL)
466 fprintf(ctxt->output, ", SYSTEM %s", (char *) dtd->SystemID);
467 fprintf(ctxt->output, "\n");
468 }
469 /*
470 * Do a bit of checking
471 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000472 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000473}
474
475static void
476xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr)
477{
478 xmlCtxtDumpSpaces(ctxt);
479
480 if (attr == NULL) {
481 if (!ctxt->check)
482 fprintf(ctxt->output, "Attribute declaration is NULL\n");
483 return;
484 }
485 if (attr->type != XML_ATTRIBUTE_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000486 xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL,
487 "Node is not an attribute declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000488 return;
489 }
490 if (attr->name != NULL) {
491 if (!ctxt->check)
492 fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name);
493 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000494 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
495 "Node attribute declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000496 if (attr->elem != NULL) {
497 if (!ctxt->check)
498 fprintf(ctxt->output, " for %s", (char *) attr->elem);
499 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000500 xmlDebugErr(ctxt, XML_CHECK_NO_ELEM,
501 "Node attribute declaration has no element name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000502 if (!ctxt->check) {
503 switch (attr->atype) {
504 case XML_ATTRIBUTE_CDATA:
505 fprintf(ctxt->output, " CDATA");
506 break;
507 case XML_ATTRIBUTE_ID:
508 fprintf(ctxt->output, " ID");
509 break;
510 case XML_ATTRIBUTE_IDREF:
511 fprintf(ctxt->output, " IDREF");
512 break;
513 case XML_ATTRIBUTE_IDREFS:
514 fprintf(ctxt->output, " IDREFS");
515 break;
516 case XML_ATTRIBUTE_ENTITY:
517 fprintf(ctxt->output, " ENTITY");
518 break;
519 case XML_ATTRIBUTE_ENTITIES:
520 fprintf(ctxt->output, " ENTITIES");
521 break;
522 case XML_ATTRIBUTE_NMTOKEN:
523 fprintf(ctxt->output, " NMTOKEN");
524 break;
525 case XML_ATTRIBUTE_NMTOKENS:
526 fprintf(ctxt->output, " NMTOKENS");
527 break;
528 case XML_ATTRIBUTE_ENUMERATION:
529 fprintf(ctxt->output, " ENUMERATION");
530 break;
531 case XML_ATTRIBUTE_NOTATION:
532 fprintf(ctxt->output, " NOTATION ");
533 break;
534 }
535 if (attr->tree != NULL) {
536 int indx;
537 xmlEnumerationPtr cur = attr->tree;
538
539 for (indx = 0; indx < 5; indx++) {
540 if (indx != 0)
541 fprintf(ctxt->output, "|%s", (char *) cur->name);
542 else
543 fprintf(ctxt->output, " (%s", (char *) cur->name);
544 cur = cur->next;
545 if (cur == NULL)
546 break;
547 }
548 if (cur == NULL)
549 fprintf(ctxt->output, ")");
550 else
551 fprintf(ctxt->output, "...)");
552 }
553 switch (attr->def) {
554 case XML_ATTRIBUTE_NONE:
555 break;
556 case XML_ATTRIBUTE_REQUIRED:
557 fprintf(ctxt->output, " REQUIRED");
558 break;
559 case XML_ATTRIBUTE_IMPLIED:
560 fprintf(ctxt->output, " IMPLIED");
561 break;
562 case XML_ATTRIBUTE_FIXED:
563 fprintf(ctxt->output, " FIXED");
564 break;
565 }
566 if (attr->defaultValue != NULL) {
567 fprintf(ctxt->output, "\"");
568 xmlCtxtDumpString(ctxt, attr->defaultValue);
569 fprintf(ctxt->output, "\"");
570 }
571 fprintf(ctxt->output, "\n");
572 }
573
574 /*
575 * Do a bit of checking
576 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000577 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000578}
579
580static void
581xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem)
582{
583 xmlCtxtDumpSpaces(ctxt);
584
585 if (elem == NULL) {
586 if (!ctxt->check)
587 fprintf(ctxt->output, "Element declaration is NULL\n");
588 return;
589 }
590 if (elem->type != XML_ELEMENT_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000591 xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL,
592 "Node is not an element declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000593 return;
594 }
595 if (elem->name != NULL) {
596 if (!ctxt->check) {
597 fprintf(ctxt->output, "ELEMDECL(");
598 xmlCtxtDumpString(ctxt, elem->name);
599 fprintf(ctxt->output, ")");
600 }
601 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000602 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
603 "Element declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000604 if (!ctxt->check) {
605 switch (elem->etype) {
606 case XML_ELEMENT_TYPE_UNDEFINED:
607 fprintf(ctxt->output, ", UNDEFINED");
608 break;
609 case XML_ELEMENT_TYPE_EMPTY:
610 fprintf(ctxt->output, ", EMPTY");
611 break;
612 case XML_ELEMENT_TYPE_ANY:
613 fprintf(ctxt->output, ", ANY");
614 break;
615 case XML_ELEMENT_TYPE_MIXED:
616 fprintf(ctxt->output, ", MIXED ");
617 break;
618 case XML_ELEMENT_TYPE_ELEMENT:
619 fprintf(ctxt->output, ", MIXED ");
620 break;
621 }
622 if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) {
623 char buf[5001];
624
625 buf[0] = 0;
626 xmlSnprintfElementContent(buf, 5000, elem->content, 1);
627 buf[5000] = 0;
628 fprintf(ctxt->output, "%s", buf);
629 }
630 fprintf(ctxt->output, "\n");
631 }
632
633 /*
634 * Do a bit of checking
635 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000636 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000637}
638
639static void
640xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
641{
642 xmlCtxtDumpSpaces(ctxt);
643
644 if (ent == NULL) {
645 if (!ctxt->check)
646 fprintf(ctxt->output, "Entity declaration is NULL\n");
647 return;
648 }
649 if (ent->type != XML_ENTITY_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000650 xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL,
651 "Node is not an entity declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000652 return;
653 }
654 if (ent->name != NULL) {
655 if (!ctxt->check) {
656 fprintf(ctxt->output, "ENTITYDECL(");
657 xmlCtxtDumpString(ctxt, ent->name);
658 fprintf(ctxt->output, ")");
659 }
660 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000661 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
662 "Entity declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000663 if (!ctxt->check) {
664 switch (ent->etype) {
665 case XML_INTERNAL_GENERAL_ENTITY:
666 fprintf(ctxt->output, ", internal\n");
667 break;
668 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
669 fprintf(ctxt->output, ", external parsed\n");
670 break;
671 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
672 fprintf(ctxt->output, ", unparsed\n");
673 break;
674 case XML_INTERNAL_PARAMETER_ENTITY:
675 fprintf(ctxt->output, ", parameter\n");
676 break;
677 case XML_EXTERNAL_PARAMETER_ENTITY:
678 fprintf(ctxt->output, ", external parameter\n");
679 break;
680 case XML_INTERNAL_PREDEFINED_ENTITY:
681 fprintf(ctxt->output, ", predefined\n");
682 break;
683 }
684 if (ent->ExternalID) {
685 xmlCtxtDumpSpaces(ctxt);
686 fprintf(ctxt->output, " ExternalID=%s\n",
687 (char *) ent->ExternalID);
688 }
689 if (ent->SystemID) {
690 xmlCtxtDumpSpaces(ctxt);
691 fprintf(ctxt->output, " SystemID=%s\n",
692 (char *) ent->SystemID);
693 }
694 if (ent->URI != NULL) {
695 xmlCtxtDumpSpaces(ctxt);
696 fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI);
697 }
698 if (ent->content) {
699 xmlCtxtDumpSpaces(ctxt);
700 fprintf(ctxt->output, " content=");
701 xmlCtxtDumpString(ctxt, ent->content);
702 fprintf(ctxt->output, "\n");
703 }
704 }
705
706 /*
707 * Do a bit of checking
708 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000709 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000710}
711
712static void
713xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
714{
715 xmlCtxtDumpSpaces(ctxt);
716
717 if (ns == NULL) {
718 if (!ctxt->check)
719 fprintf(ctxt->output, "namespace node is NULL\n");
720 return;
721 }
722 if (ns->type != XML_NAMESPACE_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000723 xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL,
724 "Node is not a namespace declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000725 return;
726 }
727 if (ns->href == NULL) {
728 if (ns->prefix != NULL)
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000729 xmlDebugErr3(ctxt, XML_CHECK_NO_HREF,
730 "Incomplete namespace %s href=NULL\n",
Daniel Veillard22cdb842004-10-04 14:09:17 +0000731 (char *) ns->prefix);
732 else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000733 xmlDebugErr(ctxt, XML_CHECK_NO_HREF,
734 "Incomplete default namespace href=NULL\n");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000735 } else {
736 if (!ctxt->check) {
737 if (ns->prefix != NULL)
738 fprintf(ctxt->output, "namespace %s href=",
739 (char *) ns->prefix);
740 else
741 fprintf(ctxt->output, "default namespace href=");
742
743 xmlCtxtDumpString(ctxt, ns->href);
744 fprintf(ctxt->output, "\n");
745 }
746 }
747}
748
749static void
750xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
751{
752 while (ns != NULL) {
753 xmlCtxtDumpNamespace(ctxt, ns);
754 ns = ns->next;
755 }
756}
757
758static void
759xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
760{
761 xmlCtxtDumpSpaces(ctxt);
762
763 if (ent == NULL) {
764 if (!ctxt->check)
765 fprintf(ctxt->output, "Entity is NULL\n");
766 return;
767 }
768 if (!ctxt->check) {
769 switch (ent->etype) {
770 case XML_INTERNAL_GENERAL_ENTITY:
771 fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY ");
772 break;
773 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
774 fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
775 break;
776 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
777 fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
778 break;
779 case XML_INTERNAL_PARAMETER_ENTITY:
780 fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY ");
781 break;
782 case XML_EXTERNAL_PARAMETER_ENTITY:
783 fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY ");
784 break;
785 default:
786 fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype);
787 }
788 fprintf(ctxt->output, "%s\n", ent->name);
789 if (ent->ExternalID) {
790 xmlCtxtDumpSpaces(ctxt);
791 fprintf(ctxt->output, "ExternalID=%s\n",
792 (char *) ent->ExternalID);
793 }
794 if (ent->SystemID) {
795 xmlCtxtDumpSpaces(ctxt);
796 fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID);
797 }
798 if (ent->URI) {
799 xmlCtxtDumpSpaces(ctxt);
800 fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI);
801 }
802 if (ent->content) {
803 xmlCtxtDumpSpaces(ctxt);
804 fprintf(ctxt->output, "content=");
805 xmlCtxtDumpString(ctxt, ent->content);
806 fprintf(ctxt->output, "\n");
807 }
808 }
809}
810
811/**
812 * xmlCtxtDumpAttr:
813 * @output: the FILE * for the output
814 * @attr: the attribute
815 * @depth: the indentation level.
816 *
817 * Dumps debug information for the attribute
818 */
819static void
820xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
821{
822 xmlCtxtDumpSpaces(ctxt);
823
824 if (attr == NULL) {
825 if (!ctxt->check)
826 fprintf(ctxt->output, "Attr is NULL");
827 return;
828 }
829 if (!ctxt->check) {
830 fprintf(ctxt->output, "ATTRIBUTE ");
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000831 xmlCtxtDumpString(ctxt, attr->name);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000832 fprintf(ctxt->output, "\n");
833 if (attr->children != NULL) {
834 ctxt->depth++;
835 xmlCtxtDumpNodeList(ctxt, attr->children);
836 ctxt->depth--;
837 }
838 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000839 if (attr->name == NULL)
840 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
841 "Attribute has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000842
843 /*
844 * Do a bit of checking
845 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000846 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000847}
848
849/**
850 * xmlCtxtDumpAttrList:
851 * @output: the FILE * for the output
852 * @attr: the attribute list
853 * @depth: the indentation level.
854 *
855 * Dumps debug information for the attribute list
856 */
857static void
858xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
859{
860 while (attr != NULL) {
861 xmlCtxtDumpAttr(ctxt, attr);
862 attr = attr->next;
863 }
864}
865
866/**
867 * xmlCtxtDumpOneNode:
868 * @output: the FILE * for the output
869 * @node: the node
870 * @depth: the indentation level.
871 *
872 * Dumps debug information for the element node, it is not recursive
873 */
874static void
875xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
876{
877 if (node == NULL) {
878 if (!ctxt->check) {
879 xmlCtxtDumpSpaces(ctxt);
880 fprintf(ctxt->output, "node is NULL\n");
881 }
882 return;
883 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000884 ctxt->node = node;
885
Daniel Veillard22cdb842004-10-04 14:09:17 +0000886 switch (node->type) {
887 case XML_ELEMENT_NODE:
888 if (!ctxt->check) {
889 xmlCtxtDumpSpaces(ctxt);
890 fprintf(ctxt->output, "ELEMENT ");
891 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
892 xmlCtxtDumpString(ctxt, node->ns->prefix);
893 fprintf(ctxt->output, ":");
894 }
895 xmlCtxtDumpString(ctxt, node->name);
896 fprintf(ctxt->output, "\n");
897 }
898 break;
899 case XML_ATTRIBUTE_NODE:
900 if (!ctxt->check)
901 xmlCtxtDumpSpaces(ctxt);
902 fprintf(ctxt->output, "Error, ATTRIBUTE found here\n");
William M. Brack5a9c1fd2004-12-17 21:38:09 +0000903 xmlCtxtGenericNodeCheck(ctxt, node);
904 return;
Daniel Veillard22cdb842004-10-04 14:09:17 +0000905 case XML_TEXT_NODE:
906 if (!ctxt->check) {
907 xmlCtxtDumpSpaces(ctxt);
908 if (node->name == (const xmlChar *) xmlStringTextNoenc)
Daniel Veillard8874b942005-08-25 13:19:21 +0000909 fprintf(ctxt->output, "TEXT no enc");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000910 else
Daniel Veillard8874b942005-08-25 13:19:21 +0000911 fprintf(ctxt->output, "TEXT");
Daniel Veillardcfa303a2005-08-25 14:03:56 +0000912 if (ctxt->options & DUMP_TEXT_TYPE) {
913 if (node->content == (xmlChar *) &(node->properties))
914 fprintf(ctxt->output, " compact\n");
915 else if (xmlDictOwns(ctxt->dict, node->content) == 1)
916 fprintf(ctxt->output, " interned\n");
917 else
918 fprintf(ctxt->output, "\n");
919 } else
Daniel Veillard8874b942005-08-25 13:19:21 +0000920 fprintf(ctxt->output, "\n");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000921 }
922 break;
923 case XML_CDATA_SECTION_NODE:
924 if (!ctxt->check) {
925 xmlCtxtDumpSpaces(ctxt);
926 fprintf(ctxt->output, "CDATA_SECTION\n");
927 }
928 break;
929 case XML_ENTITY_REF_NODE:
930 if (!ctxt->check) {
931 xmlCtxtDumpSpaces(ctxt);
932 fprintf(ctxt->output, "ENTITY_REF(%s)\n",
933 (char *) node->name);
934 }
935 break;
936 case XML_ENTITY_NODE:
937 if (!ctxt->check) {
938 xmlCtxtDumpSpaces(ctxt);
939 fprintf(ctxt->output, "ENTITY\n");
940 }
941 break;
942 case XML_PI_NODE:
943 if (!ctxt->check) {
944 xmlCtxtDumpSpaces(ctxt);
945 fprintf(ctxt->output, "PI %s\n", (char *) node->name);
946 }
947 break;
948 case XML_COMMENT_NODE:
949 if (!ctxt->check) {
950 xmlCtxtDumpSpaces(ctxt);
951 fprintf(ctxt->output, "COMMENT\n");
952 }
953 break;
954 case XML_DOCUMENT_NODE:
955 case XML_HTML_DOCUMENT_NODE:
956 if (!ctxt->check) {
957 xmlCtxtDumpSpaces(ctxt);
958 }
William M. Brack5a9c1fd2004-12-17 21:38:09 +0000959 fprintf(ctxt->output, "Error, DOCUMENT found here\n");
960 xmlCtxtGenericNodeCheck(ctxt, node);
961 return;
Daniel Veillard22cdb842004-10-04 14:09:17 +0000962 case XML_DOCUMENT_TYPE_NODE:
963 if (!ctxt->check) {
964 xmlCtxtDumpSpaces(ctxt);
965 fprintf(ctxt->output, "DOCUMENT_TYPE\n");
966 }
967 break;
968 case XML_DOCUMENT_FRAG_NODE:
969 if (!ctxt->check) {
970 xmlCtxtDumpSpaces(ctxt);
971 fprintf(ctxt->output, "DOCUMENT_FRAG\n");
972 }
973 break;
974 case XML_NOTATION_NODE:
975 if (!ctxt->check) {
976 xmlCtxtDumpSpaces(ctxt);
977 fprintf(ctxt->output, "NOTATION\n");
978 }
979 break;
980 case XML_DTD_NODE:
981 xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
982 return;
983 case XML_ELEMENT_DECL:
984 xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
985 return;
986 case XML_ATTRIBUTE_DECL:
987 xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
988 return;
989 case XML_ENTITY_DECL:
990 xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
991 return;
992 case XML_NAMESPACE_DECL:
993 xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
994 return;
995 case XML_XINCLUDE_START:
996 if (!ctxt->check) {
997 xmlCtxtDumpSpaces(ctxt);
998 fprintf(ctxt->output, "INCLUDE START\n");
999 }
1000 return;
1001 case XML_XINCLUDE_END:
1002 if (!ctxt->check) {
1003 xmlCtxtDumpSpaces(ctxt);
1004 fprintf(ctxt->output, "INCLUDE END\n");
1005 }
1006 return;
1007 default:
1008 if (!ctxt->check)
1009 xmlCtxtDumpSpaces(ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001010 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1011 "Unknown node type %d\n", node->type);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001012 return;
1013 }
1014 if (node->doc == NULL) {
1015 if (!ctxt->check) {
1016 xmlCtxtDumpSpaces(ctxt);
1017 }
1018 fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
1019 }
1020 ctxt->depth++;
Daniel Veillard8874b942005-08-25 13:19:21 +00001021 if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL))
Daniel Veillard22cdb842004-10-04 14:09:17 +00001022 xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
Daniel Veillard8874b942005-08-25 13:19:21 +00001023 if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL))
Daniel Veillard22cdb842004-10-04 14:09:17 +00001024 xmlCtxtDumpAttrList(ctxt, node->properties);
1025 if (node->type != XML_ENTITY_REF_NODE) {
1026 if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
1027 if (!ctxt->check) {
1028 xmlCtxtDumpSpaces(ctxt);
1029 fprintf(ctxt->output, "content=");
1030 xmlCtxtDumpString(ctxt, node->content);
1031 fprintf(ctxt->output, "\n");
1032 }
1033 }
1034 } else {
1035 xmlEntityPtr ent;
1036
1037 ent = xmlGetDocEntity(node->doc, node->name);
1038 if (ent != NULL)
1039 xmlCtxtDumpEntity(ctxt, ent);
1040 }
1041 ctxt->depth--;
1042
1043 /*
1044 * Do a bit of checking
1045 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001046 xmlCtxtGenericNodeCheck(ctxt, node);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001047}
1048
1049/**
1050 * xmlCtxtDumpNode:
1051 * @output: the FILE * for the output
1052 * @node: the node
1053 * @depth: the indentation level.
1054 *
1055 * Dumps debug information for the element node, it is recursive
1056 */
1057static void
1058xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1059{
1060 if (node == NULL) {
1061 if (!ctxt->check) {
1062 xmlCtxtDumpSpaces(ctxt);
1063 fprintf(ctxt->output, "node is NULL\n");
1064 }
1065 return;
1066 }
1067 xmlCtxtDumpOneNode(ctxt, node);
Daniel Veillard7837dd82005-09-06 22:16:57 +00001068 if ((node->type != XML_NAMESPACE_DECL) &&
1069 (node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
Daniel Veillard22cdb842004-10-04 14:09:17 +00001070 ctxt->depth++;
1071 xmlCtxtDumpNodeList(ctxt, node->children);
1072 ctxt->depth--;
1073 }
1074}
1075
1076/**
1077 * xmlCtxtDumpNodeList:
1078 * @output: the FILE * for the output
1079 * @node: the node list
1080 * @depth: the indentation level.
1081 *
1082 * Dumps debug information for the list of element node, it is recursive
1083 */
1084static void
1085xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1086{
1087 while (node != NULL) {
1088 xmlCtxtDumpNode(ctxt, node);
1089 node = node->next;
1090 }
1091}
1092
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001093static void
1094xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1095{
1096 if (doc == NULL) {
1097 if (!ctxt->check)
1098 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1099 return;
1100 }
1101 ctxt->node = (xmlNodePtr) doc;
1102
1103 switch (doc->type) {
1104 case XML_ELEMENT_NODE:
1105 xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
1106 "Misplaced ELEMENT node\n");
1107 break;
1108 case XML_ATTRIBUTE_NODE:
1109 xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
1110 "Misplaced ATTRIBUTE node\n");
1111 break;
1112 case XML_TEXT_NODE:
1113 xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
1114 "Misplaced TEXT node\n");
1115 break;
1116 case XML_CDATA_SECTION_NODE:
1117 xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
1118 "Misplaced CDATA node\n");
1119 break;
1120 case XML_ENTITY_REF_NODE:
1121 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
1122 "Misplaced ENTITYREF node\n");
1123 break;
1124 case XML_ENTITY_NODE:
1125 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
1126 "Misplaced ENTITY node\n");
1127 break;
1128 case XML_PI_NODE:
1129 xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
1130 "Misplaced PI node\n");
1131 break;
1132 case XML_COMMENT_NODE:
1133 xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
1134 "Misplaced COMMENT node\n");
1135 break;
1136 case XML_DOCUMENT_NODE:
1137 if (!ctxt->check)
1138 fprintf(ctxt->output, "DOCUMENT\n");
1139 break;
1140 case XML_HTML_DOCUMENT_NODE:
1141 if (!ctxt->check)
1142 fprintf(ctxt->output, "HTML DOCUMENT\n");
1143 break;
1144 case XML_DOCUMENT_TYPE_NODE:
1145 xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
1146 "Misplaced DOCTYPE node\n");
1147 break;
1148 case XML_DOCUMENT_FRAG_NODE:
1149 xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
1150 "Misplaced FRAGMENT node\n");
1151 break;
1152 case XML_NOTATION_NODE:
1153 xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
1154 "Misplaced NOTATION node\n");
1155 break;
1156 default:
1157 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1158 "Unknown node type %d\n", doc->type);
1159 }
1160}
Daniel Veillard22cdb842004-10-04 14:09:17 +00001161
1162/**
1163 * xmlCtxtDumpDocumentHead:
1164 * @output: the FILE * for the output
1165 * @doc: the document
1166 *
1167 * Dumps debug information cncerning the document, not recursive
1168 */
1169static void
1170xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1171{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001172 if (doc == NULL) return;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001173 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001174 if (!ctxt->check) {
1175 if (doc->name != NULL) {
1176 fprintf(ctxt->output, "name=");
1177 xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
1178 fprintf(ctxt->output, "\n");
1179 }
1180 if (doc->version != NULL) {
1181 fprintf(ctxt->output, "version=");
1182 xmlCtxtDumpString(ctxt, doc->version);
1183 fprintf(ctxt->output, "\n");
1184 }
1185 if (doc->encoding != NULL) {
1186 fprintf(ctxt->output, "encoding=");
1187 xmlCtxtDumpString(ctxt, doc->encoding);
1188 fprintf(ctxt->output, "\n");
1189 }
1190 if (doc->URL != NULL) {
1191 fprintf(ctxt->output, "URL=");
1192 xmlCtxtDumpString(ctxt, doc->URL);
1193 fprintf(ctxt->output, "\n");
1194 }
1195 if (doc->standalone)
1196 fprintf(ctxt->output, "standalone=true\n");
1197 }
1198 if (doc->oldNs != NULL)
1199 xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
1200}
1201
1202/**
1203 * xmlCtxtDumpDocument:
1204 * @output: the FILE * for the output
1205 * @doc: the document
1206 *
1207 * Dumps debug information for the document, it's recursive
1208 */
1209static void
1210xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1211{
1212 if (doc == NULL) {
1213 if (!ctxt->check)
1214 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1215 return;
1216 }
1217 xmlCtxtDumpDocumentHead(ctxt, doc);
1218 if (((doc->type == XML_DOCUMENT_NODE) ||
1219 (doc->type == XML_HTML_DOCUMENT_NODE))
1220 && (doc->children != NULL)) {
1221 ctxt->depth++;
1222 xmlCtxtDumpNodeList(ctxt, doc->children);
1223 ctxt->depth--;
1224 }
1225}
1226
1227static void
1228xmlCtxtDumpEntityCallback(xmlEntityPtr cur, xmlDebugCtxtPtr ctxt)
1229{
1230 if (cur == NULL) {
1231 if (!ctxt->check)
1232 fprintf(ctxt->output, "Entity is NULL");
1233 return;
1234 }
1235 if (!ctxt->check) {
1236 fprintf(ctxt->output, "%s : ", (char *) cur->name);
1237 switch (cur->etype) {
1238 case XML_INTERNAL_GENERAL_ENTITY:
1239 fprintf(ctxt->output, "INTERNAL GENERAL, ");
1240 break;
1241 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1242 fprintf(ctxt->output, "EXTERNAL PARSED, ");
1243 break;
1244 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1245 fprintf(ctxt->output, "EXTERNAL UNPARSED, ");
1246 break;
1247 case XML_INTERNAL_PARAMETER_ENTITY:
1248 fprintf(ctxt->output, "INTERNAL PARAMETER, ");
1249 break;
1250 case XML_EXTERNAL_PARAMETER_ENTITY:
1251 fprintf(ctxt->output, "EXTERNAL PARAMETER, ");
1252 break;
1253 default:
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001254 xmlDebugErr2(ctxt, XML_CHECK_ENTITY_TYPE,
1255 "Unknown entity type %d\n", cur->etype);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001256 }
1257 if (cur->ExternalID != NULL)
1258 fprintf(ctxt->output, "ID \"%s\"", (char *) cur->ExternalID);
1259 if (cur->SystemID != NULL)
1260 fprintf(ctxt->output, "SYSTEM \"%s\"", (char *) cur->SystemID);
1261 if (cur->orig != NULL)
1262 fprintf(ctxt->output, "\n orig \"%s\"", (char *) cur->orig);
1263 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL))
1264 fprintf(ctxt->output, "\n content \"%s\"",
1265 (char *) cur->content);
1266 fprintf(ctxt->output, "\n");
1267 }
1268}
1269
1270/**
1271 * xmlCtxtDumpEntities:
1272 * @output: the FILE * for the output
1273 * @doc: the document
1274 *
1275 * Dumps debug information for all the entities in use by the document
1276 */
1277static void
1278xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1279{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001280 if (doc == NULL) return;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001281 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001282 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
1283 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1284 doc->intSubset->entities;
1285
1286 if (!ctxt->check)
1287 fprintf(ctxt->output, "Entities in internal subset\n");
1288 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1289 ctxt);
1290 } else
1291 fprintf(ctxt->output, "No entities in internal subset\n");
1292 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
1293 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1294 doc->extSubset->entities;
1295
1296 if (!ctxt->check)
1297 fprintf(ctxt->output, "Entities in external subset\n");
1298 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1299 ctxt);
1300 } else if (!ctxt->check)
1301 fprintf(ctxt->output, "No entities in external subset\n");
1302}
1303
1304/**
1305 * xmlCtxtDumpDTD:
1306 * @output: the FILE * for the output
1307 * @dtd: the DTD
1308 *
1309 * Dumps debug information for the DTD
1310 */
1311static void
1312xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
1313{
1314 if (dtd == NULL) {
1315 if (!ctxt->check)
1316 fprintf(ctxt->output, "DTD is NULL\n");
1317 return;
1318 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001319 xmlCtxtDumpDtdNode(ctxt, dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001320 if (dtd->children == NULL)
1321 fprintf(ctxt->output, " DTD is empty\n");
1322 else {
1323 ctxt->depth++;
1324 xmlCtxtDumpNodeList(ctxt, dtd->children);
1325 ctxt->depth--;
1326 }
1327}
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001328
Daniel Veillard22cdb842004-10-04 14:09:17 +00001329/************************************************************************
1330 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001331 * Public entry points for dump *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001332 * *
1333 ************************************************************************/
1334
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001335/**
1336 * xmlDebugDumpString:
1337 * @output: the FILE * for the output
1338 * @str: the string
1339 *
1340 * Dumps informations about the string, shorten it if necessary
1341 */
1342void
1343xmlDebugDumpString(FILE * output, const xmlChar * str)
1344{
Owen Taylor3473f882001-02-23 17:55:21 +00001345 int i;
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001346
Daniel Veillard7db38712002-02-07 16:39:11 +00001347 if (output == NULL)
1348 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00001349 if (str == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001350 fprintf(output, "(NULL)");
1351 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001352 }
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001353 for (i = 0; i < 40; i++)
1354 if (str[i] == 0)
1355 return;
William M. Brack76e95df2003-10-18 16:20:14 +00001356 else if (IS_BLANK_CH(str[i]))
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001357 fputc(' ', output);
1358 else if (str[i] >= 0x80)
1359 fprintf(output, "#%X", str[i]);
1360 else
1361 fputc(str[i], output);
Owen Taylor3473f882001-02-23 17:55:21 +00001362 fprintf(output, "...");
1363}
1364
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001365/**
1366 * xmlDebugDumpAttr:
1367 * @output: the FILE * for the output
1368 * @attr: the attribute
1369 * @depth: the indentation level.
1370 *
1371 * Dumps debug information for the attribute
1372 */
1373void
1374xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
Daniel Veillard22cdb842004-10-04 14:09:17 +00001375 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001376
Daniel Veillarda82b1822004-11-08 16:24:57 +00001377 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001378 xmlCtxtDumpInitCtxt(&ctxt);
1379 ctxt.output = output;
1380 ctxt.depth = depth;
1381 xmlCtxtDumpAttr(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001382 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001383}
Owen Taylor3473f882001-02-23 17:55:21 +00001384
Owen Taylor3473f882001-02-23 17:55:21 +00001385
Daniel Veillard22cdb842004-10-04 14:09:17 +00001386/**
1387 * xmlDebugDumpEntities:
1388 * @output: the FILE * for the output
1389 * @doc: the document
1390 *
1391 * Dumps debug information for all the entities in use by the document
1392 */
1393void
1394xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
1395{
1396 xmlDebugCtxt ctxt;
1397
Daniel Veillarda82b1822004-11-08 16:24:57 +00001398 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001399 xmlCtxtDumpInitCtxt(&ctxt);
1400 ctxt.output = output;
1401 xmlCtxtDumpEntities(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001402 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001403}
1404
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001405/**
1406 * xmlDebugDumpAttrList:
1407 * @output: the FILE * for the output
1408 * @attr: the attribute list
1409 * @depth: the indentation level.
1410 *
1411 * Dumps debug information for the attribute list
1412 */
1413void
1414xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
1415{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001416 xmlDebugCtxt ctxt;
1417
Daniel Veillarda82b1822004-11-08 16:24:57 +00001418 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001419 xmlCtxtDumpInitCtxt(&ctxt);
1420 ctxt.output = output;
1421 ctxt.depth = depth;
1422 xmlCtxtDumpAttrList(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001423 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001424}
1425
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001426/**
1427 * xmlDebugDumpOneNode:
1428 * @output: the FILE * for the output
1429 * @node: the node
1430 * @depth: the indentation level.
1431 *
1432 * Dumps debug information for the element node, it is not recursive
1433 */
1434void
1435xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
1436{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001437 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001438
Daniel Veillarda82b1822004-11-08 16:24:57 +00001439 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001440 xmlCtxtDumpInitCtxt(&ctxt);
1441 ctxt.output = output;
1442 ctxt.depth = depth;
1443 xmlCtxtDumpOneNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001444 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001445}
1446
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001447/**
1448 * xmlDebugDumpNode:
1449 * @output: the FILE * for the output
1450 * @node: the node
1451 * @depth: the indentation level.
1452 *
1453 * Dumps debug information for the element node, it is recursive
1454 */
1455void
1456xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
1457{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001458 xmlDebugCtxt ctxt;
1459
Daniel Veillard7db38712002-02-07 16:39:11 +00001460 if (output == NULL)
1461 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001462 xmlCtxtDumpInitCtxt(&ctxt);
1463 ctxt.output = output;
1464 ctxt.depth = depth;
1465 xmlCtxtDumpNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001466 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001467}
1468
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001469/**
1470 * xmlDebugDumpNodeList:
1471 * @output: the FILE * for the output
1472 * @node: the node list
1473 * @depth: the indentation level.
1474 *
1475 * Dumps debug information for the list of element node, it is recursive
1476 */
1477void
1478xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
1479{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001480 xmlDebugCtxt ctxt;
1481
Daniel Veillard7db38712002-02-07 16:39:11 +00001482 if (output == NULL)
1483 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001484 xmlCtxtDumpInitCtxt(&ctxt);
1485 ctxt.output = output;
1486 ctxt.depth = depth;
1487 xmlCtxtDumpNodeList(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001488 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001489}
1490
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001491/**
1492 * xmlDebugDumpDocumentHead:
1493 * @output: the FILE * for the output
1494 * @doc: the document
1495 *
1496 * Dumps debug information cncerning the document, not recursive
1497 */
1498void
1499xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
1500{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001501 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001502
Daniel Veillard22cdb842004-10-04 14:09:17 +00001503 if (output == NULL)
1504 output = stdout;
1505 xmlCtxtDumpInitCtxt(&ctxt);
Daniel Veillardcfa303a2005-08-25 14:03:56 +00001506 ctxt.options |= DUMP_TEXT_TYPE;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001507 ctxt.output = output;
1508 xmlCtxtDumpDocumentHead(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001509 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001510}
1511
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001512/**
1513 * xmlDebugDumpDocument:
1514 * @output: the FILE * for the output
1515 * @doc: the document
1516 *
1517 * Dumps debug information for the document, it's recursive
1518 */
1519void
1520xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
1521{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001522 xmlDebugCtxt ctxt;
1523
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001524 if (output == NULL)
Daniel Veillard22cdb842004-10-04 14:09:17 +00001525 output = stdout;
1526 xmlCtxtDumpInitCtxt(&ctxt);
Daniel Veillardcfa303a2005-08-25 14:03:56 +00001527 ctxt.options |= DUMP_TEXT_TYPE;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001528 ctxt.output = output;
1529 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001530 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001531}
Owen Taylor3473f882001-02-23 17:55:21 +00001532
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001533/**
1534 * xmlDebugDumpDTD:
1535 * @output: the FILE * for the output
1536 * @dtd: the DTD
1537 *
1538 * Dumps debug information for the DTD
1539 */
1540void
1541xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
1542{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001543 xmlDebugCtxt ctxt;
1544
Daniel Veillard7db38712002-02-07 16:39:11 +00001545 if (output == NULL)
1546 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001547 xmlCtxtDumpInitCtxt(&ctxt);
Daniel Veillardcfa303a2005-08-25 14:03:56 +00001548 ctxt.options |= DUMP_TEXT_TYPE;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001549 ctxt.output = output;
1550 xmlCtxtDumpDTD(&ctxt, dtd);
Daniel Veillard76821142004-10-09 20:39:04 +00001551 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001552}
1553
Daniel Veillard22cdb842004-10-04 14:09:17 +00001554/************************************************************************
1555 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001556 * Public entry points for checkings *
1557 * *
1558 ************************************************************************/
1559
1560/**
1561 * xmlDebugCheckDocument:
1562 * @output: the FILE * for the output
1563 * @doc: the document
1564 *
1565 * Check the document for potential content problems, and output
1566 * the errors to @output
1567 *
1568 * Returns the number of errors found
1569 */
1570int
1571xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
1572{
1573 xmlDebugCtxt ctxt;
1574
1575 if (output == NULL)
1576 output = stdout;
1577 xmlCtxtDumpInitCtxt(&ctxt);
1578 ctxt.output = output;
1579 ctxt.check = 1;
1580 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001581 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001582 return(ctxt.errors);
1583}
1584
1585/************************************************************************
1586 * *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001587 * Helpers for Shell *
1588 * *
1589 ************************************************************************/
Owen Taylor3473f882001-02-23 17:55:21 +00001590
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001591/**
1592 * xmlLsCountNode:
1593 * @node: the node to count
1594 *
1595 * Count the children of @node.
1596 *
1597 * Returns the number of children of @node.
1598 */
1599int
1600xmlLsCountNode(xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00001601 int ret = 0;
1602 xmlNodePtr list = NULL;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001603
1604 if (node == NULL)
1605 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001606
1607 switch (node->type) {
1608 case XML_ELEMENT_NODE:
1609 list = node->children;
1610 break;
1611 case XML_DOCUMENT_NODE:
1612 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00001613#ifdef LIBXML_DOCB_ENABLED
1614 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001615#endif
1616 list = ((xmlDocPtr) node)->children;
1617 break;
1618 case XML_ATTRIBUTE_NODE:
1619 list = ((xmlAttrPtr) node)->children;
1620 break;
1621 case XML_TEXT_NODE:
1622 case XML_CDATA_SECTION_NODE:
1623 case XML_PI_NODE:
1624 case XML_COMMENT_NODE:
1625 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001626 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001627 }
1628 break;
1629 case XML_ENTITY_REF_NODE:
1630 case XML_DOCUMENT_TYPE_NODE:
1631 case XML_ENTITY_NODE:
1632 case XML_DOCUMENT_FRAG_NODE:
1633 case XML_NOTATION_NODE:
1634 case XML_DTD_NODE:
1635 case XML_ELEMENT_DECL:
1636 case XML_ATTRIBUTE_DECL:
1637 case XML_ENTITY_DECL:
1638 case XML_NAMESPACE_DECL:
1639 case XML_XINCLUDE_START:
1640 case XML_XINCLUDE_END:
1641 ret = 1;
1642 break;
1643 }
1644 for (;list != NULL;ret++)
1645 list = list->next;
1646 return(ret);
1647}
1648
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001649/**
1650 * xmlLsOneNode:
1651 * @output: the FILE * for the output
1652 * @node: the node to dump
1653 *
1654 * Dump to @output the type and name of @node.
1655 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001656void
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001657xmlLsOneNode(FILE *output, xmlNodePtr node) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00001658 if (output == NULL) return;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001659 if (node == NULL) {
1660 fprintf(output, "NULL\n");
1661 return;
1662 }
Owen Taylor3473f882001-02-23 17:55:21 +00001663 switch (node->type) {
1664 case XML_ELEMENT_NODE:
1665 fprintf(output, "-");
1666 break;
1667 case XML_ATTRIBUTE_NODE:
1668 fprintf(output, "a");
1669 break;
1670 case XML_TEXT_NODE:
1671 fprintf(output, "t");
1672 break;
1673 case XML_CDATA_SECTION_NODE:
Daniel Veillard75be0132002-03-13 10:03:35 +00001674 fprintf(output, "C");
Owen Taylor3473f882001-02-23 17:55:21 +00001675 break;
1676 case XML_ENTITY_REF_NODE:
1677 fprintf(output, "e");
1678 break;
1679 case XML_ENTITY_NODE:
1680 fprintf(output, "E");
1681 break;
1682 case XML_PI_NODE:
1683 fprintf(output, "p");
1684 break;
1685 case XML_COMMENT_NODE:
1686 fprintf(output, "c");
1687 break;
1688 case XML_DOCUMENT_NODE:
1689 fprintf(output, "d");
1690 break;
1691 case XML_HTML_DOCUMENT_NODE:
1692 fprintf(output, "h");
1693 break;
1694 case XML_DOCUMENT_TYPE_NODE:
1695 fprintf(output, "T");
1696 break;
1697 case XML_DOCUMENT_FRAG_NODE:
1698 fprintf(output, "F");
1699 break;
1700 case XML_NOTATION_NODE:
1701 fprintf(output, "N");
1702 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001703 case XML_NAMESPACE_DECL:
1704 fprintf(output, "n");
1705 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001706 default:
1707 fprintf(output, "?");
1708 }
Daniel Veillarde6a55192002-01-14 17:11:53 +00001709 if (node->type != XML_NAMESPACE_DECL) {
1710 if (node->properties != NULL)
1711 fprintf(output, "a");
1712 else
1713 fprintf(output, "-");
1714 if (node->nsDef != NULL)
1715 fprintf(output, "n");
1716 else
1717 fprintf(output, "-");
1718 }
Owen Taylor3473f882001-02-23 17:55:21 +00001719
1720 fprintf(output, " %8d ", xmlLsCountNode(node));
1721
1722 switch (node->type) {
1723 case XML_ELEMENT_NODE:
1724 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001725 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001726 break;
1727 case XML_ATTRIBUTE_NODE:
1728 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001729 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001730 break;
1731 case XML_TEXT_NODE:
1732 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001733 xmlDebugDumpString(output, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001734 }
1735 break;
1736 case XML_CDATA_SECTION_NODE:
1737 break;
1738 case XML_ENTITY_REF_NODE:
1739 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001740 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001741 break;
1742 case XML_ENTITY_NODE:
1743 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001744 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001745 break;
1746 case XML_PI_NODE:
1747 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001748 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001749 break;
1750 case XML_COMMENT_NODE:
1751 break;
1752 case XML_DOCUMENT_NODE:
1753 break;
1754 case XML_HTML_DOCUMENT_NODE:
1755 break;
1756 case XML_DOCUMENT_TYPE_NODE:
1757 break;
1758 case XML_DOCUMENT_FRAG_NODE:
1759 break;
1760 case XML_NOTATION_NODE:
1761 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001762 case XML_NAMESPACE_DECL: {
1763 xmlNsPtr ns = (xmlNsPtr) node;
1764
1765 if (ns->prefix == NULL)
William M. Brack13dfa872004-09-18 04:52:08 +00001766 fprintf(output, "default -> %s", (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001767 else
William M. Brack13dfa872004-09-18 04:52:08 +00001768 fprintf(output, "%s -> %s", (char *)ns->prefix,
1769 (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001770 break;
1771 }
Owen Taylor3473f882001-02-23 17:55:21 +00001772 default:
1773 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001774 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001775 }
1776 fprintf(output, "\n");
1777}
1778
Daniel Veillard78d12092001-10-11 09:12:24 +00001779/**
1780 * xmlBoolToText:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001781 * @boolval: a bool to turn into text
Daniel Veillard78d12092001-10-11 09:12:24 +00001782 *
1783 * Convenient way to turn bool into text
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001784 *
1785 * Returns a pointer to either "True" or "False"
1786 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001787const char *
Daniel Veillardebd38c52001-11-01 08:38:12 +00001788xmlBoolToText(int boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001789{
Daniel Veillardebd38c52001-11-01 08:38:12 +00001790 if (boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001791 return("True");
1792 else
1793 return("False");
1794}
1795
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00001796#ifdef LIBXML_XPATH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001797/****************************************************************
1798 * *
1799 * The XML shell related functions *
1800 * *
1801 ****************************************************************/
1802
Daniel Veillard78d12092001-10-11 09:12:24 +00001803
1804
Owen Taylor3473f882001-02-23 17:55:21 +00001805/*
1806 * TODO: Improvement/cleanups for the XML shell
1807 * - allow to shell out an editor on a subpart
1808 * - cleanup function registrations (with help) and calling
1809 * - provide registration routines
1810 */
1811
1812/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001813 * xmlShellPrintXPathError:
Daniel Veillard78d12092001-10-11 09:12:24 +00001814 * @errorType: valid xpath error id
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001815 * @arg: the argument that cause xpath to fail
Daniel Veillard78d12092001-10-11 09:12:24 +00001816 *
1817 * Print the xpath error to libxml default error channel
1818 */
1819void
1820xmlShellPrintXPathError(int errorType, const char *arg)
1821{
1822 const char *default_arg = "Result";
1823
1824 if (!arg)
1825 arg = default_arg;
1826
1827 switch (errorType) {
1828 case XPATH_UNDEFINED:
1829 xmlGenericError(xmlGenericErrorContext,
1830 "%s: no such node\n", arg);
1831 break;
1832
1833 case XPATH_BOOLEAN:
1834 xmlGenericError(xmlGenericErrorContext,
1835 "%s is a Boolean\n", arg);
1836 break;
1837 case XPATH_NUMBER:
1838 xmlGenericError(xmlGenericErrorContext,
1839 "%s is a number\n", arg);
1840 break;
1841 case XPATH_STRING:
1842 xmlGenericError(xmlGenericErrorContext,
1843 "%s is a string\n", arg);
1844 break;
1845 case XPATH_POINT:
1846 xmlGenericError(xmlGenericErrorContext,
1847 "%s is a point\n", arg);
1848 break;
1849 case XPATH_RANGE:
1850 xmlGenericError(xmlGenericErrorContext,
1851 "%s is a range\n", arg);
1852 break;
1853 case XPATH_LOCATIONSET:
1854 xmlGenericError(xmlGenericErrorContext,
1855 "%s is a range\n", arg);
1856 break;
1857 case XPATH_USERS:
1858 xmlGenericError(xmlGenericErrorContext,
1859 "%s is user-defined\n", arg);
1860 break;
1861 case XPATH_XSLT_TREE:
1862 xmlGenericError(xmlGenericErrorContext,
1863 "%s is an XSLT value tree\n", arg);
1864 break;
1865 }
Daniel Veillarda82b1822004-11-08 16:24:57 +00001866#if 0
Daniel Veillard78d12092001-10-11 09:12:24 +00001867 xmlGenericError(xmlGenericErrorContext,
1868 "Try casting the result string function (xpath builtin)\n",
1869 arg);
Daniel Veillarda82b1822004-11-08 16:24:57 +00001870#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00001871}
1872
1873
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001874#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001875/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001876 * xmlShellPrintNodeCtxt:
1877 * @ctxt : a non-null shell context
1878 * @node : a non-null node to print to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001879 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001880 * Print node to the output FILE
1881 */
1882static void
1883xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1884{
Daniel Veillard01992e02002-10-09 10:20:30 +00001885 FILE *fp;
1886
1887 if (!node)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001888 return;
Daniel Veillard01992e02002-10-09 10:20:30 +00001889 if (ctxt == NULL)
1890 fp = stdout;
1891 else
1892 fp = ctxt->output;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001893
1894 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001895 xmlDocDump(fp, (xmlDocPtr) node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001896 else if (node->type == XML_ATTRIBUTE_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001897 xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001898 else
Daniel Veillard01992e02002-10-09 10:20:30 +00001899 xmlElemDump(fp, node->doc, node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001900
Daniel Veillard01992e02002-10-09 10:20:30 +00001901 fprintf(fp, "\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00001902}
1903
1904/**
1905 * xmlShellPrintNode:
1906 * @node : a non-null node to print to the output FILE
1907 *
1908 * Print node to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001909 */
1910void
1911xmlShellPrintNode(xmlNodePtr node)
1912{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001913 xmlShellPrintNodeCtxt(NULL, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001914}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001915#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001916
Daniel Veillard78d12092001-10-11 09:12:24 +00001917/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001918 * xmlShellPrintXPathResultCtxt:
1919 * @ctxt: a valid shell context
Daniel Veillard9d06d302002-01-22 18:15:52 +00001920 * @list: a valid result generated by an xpath evaluation
Daniel Veillard78d12092001-10-11 09:12:24 +00001921 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001922 * Prints result to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001923 */
Daniel Veillard321be0c2002-10-08 21:26:42 +00001924static void
1925xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
Daniel Veillard78d12092001-10-11 09:12:24 +00001926{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001927 if (!ctxt)
1928 return;
Daniel Veillard78d12092001-10-11 09:12:24 +00001929
1930 if (list != NULL) {
1931 switch (list->type) {
1932 case XPATH_NODESET:{
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001933#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001934 int indx;
1935
1936 if (list->nodesetval) {
1937 for (indx = 0; indx < list->nodesetval->nodeNr;
1938 indx++) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001939 xmlShellPrintNodeCtxt(ctxt,
1940 list->nodesetval->nodeTab[indx]);
Daniel Veillard78d12092001-10-11 09:12:24 +00001941 }
1942 } else {
1943 xmlGenericError(xmlGenericErrorContext,
1944 "Empty node set\n");
1945 }
1946 break;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001947#else
1948 xmlGenericError(xmlGenericErrorContext,
1949 "Node set\n");
1950#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001951 }
1952 case XPATH_BOOLEAN:
1953 xmlGenericError(xmlGenericErrorContext,
1954 "Is a Boolean:%s\n",
1955 xmlBoolToText(list->boolval));
1956 break;
1957 case XPATH_NUMBER:
1958 xmlGenericError(xmlGenericErrorContext,
1959 "Is a number:%0g\n", list->floatval);
1960 break;
1961 case XPATH_STRING:
1962 xmlGenericError(xmlGenericErrorContext,
1963 "Is a string:%s\n", list->stringval);
1964 break;
1965
1966 default:
1967 xmlShellPrintXPathError(list->type, NULL);
1968 }
1969 }
1970}
1971
1972/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001973 * xmlShellPrintXPathResult:
1974 * @list: a valid result generated by an xpath evaluation
1975 *
1976 * Prints result to the output FILE
1977 */
1978void
1979xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1980{
1981 xmlShellPrintXPathResultCtxt(NULL, list);
1982}
1983
1984/**
Owen Taylor3473f882001-02-23 17:55:21 +00001985 * xmlShellList:
1986 * @ctxt: the shell context
1987 * @arg: unused
1988 * @node: a node
1989 * @node2: unused
1990 *
1991 * Implements the XML shell function "ls"
1992 * Does an Unix like listing of the given node (like a directory)
1993 *
1994 * Returns 0
1995 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001996int
Daniel Veillard321be0c2002-10-08 21:26:42 +00001997xmlShellList(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00001998 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1999 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2000{
Owen Taylor3473f882001-02-23 17:55:21 +00002001 xmlNodePtr cur;
Daniel Veillard321be0c2002-10-08 21:26:42 +00002002 if (!ctxt)
2003 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002004 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002005 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002006 return (0);
2007 }
Owen Taylor3473f882001-02-23 17:55:21 +00002008 if ((node->type == XML_DOCUMENT_NODE) ||
2009 (node->type == XML_HTML_DOCUMENT_NODE)) {
2010 cur = ((xmlDocPtr) node)->children;
Daniel Veillarde6a55192002-01-14 17:11:53 +00002011 } else if (node->type == XML_NAMESPACE_DECL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002012 xmlLsOneNode(ctxt->output, node);
Daniel Veillarde6a55192002-01-14 17:11:53 +00002013 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002014 } else if (node->children != NULL) {
2015 cur = node->children;
2016 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002017 xmlLsOneNode(ctxt->output, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002018 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002019 }
2020 while (cur != NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002021 xmlLsOneNode(ctxt->output, cur);
Daniel Veillard78d12092001-10-11 09:12:24 +00002022 cur = cur->next;
Owen Taylor3473f882001-02-23 17:55:21 +00002023 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002024 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002025}
2026
2027/**
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002028 * xmlShellBase:
2029 * @ctxt: the shell context
2030 * @arg: unused
2031 * @node: a node
2032 * @node2: unused
2033 *
2034 * Implements the XML shell function "base"
2035 * dumps the current XML base of the node
2036 *
2037 * Returns 0
2038 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002039int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002040xmlShellBase(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002041 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2042 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2043{
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002044 xmlChar *base;
Daniel Veillard321be0c2002-10-08 21:26:42 +00002045 if (!ctxt)
2046 return 0;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002047 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002048 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002049 return (0);
2050 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002051
2052 base = xmlNodeGetBase(node->doc, node);
2053
2054 if (base == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002055 fprintf(ctxt->output, " No base found !!!\n");
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002056 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002057 fprintf(ctxt->output, "%s\n", base);
Daniel Veillard78d12092001-10-11 09:12:24 +00002058 xmlFree(base);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002059 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002060 return (0);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002061}
2062
Daniel Veillardb34321c2004-03-04 17:09:47 +00002063#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002064/**
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002065 * xmlShellSetBase:
2066 * @ctxt: the shell context
2067 * @arg: the new base
2068 * @node: a node
2069 * @node2: unused
2070 *
2071 * Implements the XML shell function "setbase"
2072 * change the current XML base of the node
2073 *
2074 * Returns 0
2075 */
2076static int
2077xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2078 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2079 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2080{
2081 xmlNodeSetBase(node, (xmlChar*) arg);
2082 return (0);
2083}
Daniel Veillard2156d432004-03-04 15:59:36 +00002084#endif
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002085
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002086#ifdef LIBXML_XPATH_ENABLED
2087/**
2088 * xmlShellRegisterNamespace:
2089 * @ctxt: the shell context
2090 * @arg: a string in prefix=nsuri format
2091 * @node: unused
2092 * @node2: unused
2093 *
2094 * Implements the XML shell function "setns"
2095 * register/unregister a prefix=namespace pair
2096 * on the XPath context
2097 *
2098 * Returns 0 on success and a negative value otherwise.
2099 */
2100static int
2101xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
2102 xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2103{
2104 xmlChar* nsListDup;
2105 xmlChar* prefix;
2106 xmlChar* href;
2107 xmlChar* next;
2108
2109 nsListDup = xmlStrdup((xmlChar *) arg);
2110 next = nsListDup;
2111 while(next != NULL) {
2112 /* skip spaces */
2113 /*while((*next) == ' ') next++;*/
2114 if((*next) == '\0') break;
2115
2116 /* find prefix */
2117 prefix = next;
2118 next = (xmlChar*)xmlStrchr(next, '=');
2119 if(next == NULL) {
2120 fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
2121 xmlFree(nsListDup);
2122 return(-1);
2123 }
2124 *(next++) = '\0';
2125
2126 /* find href */
2127 href = next;
2128 next = (xmlChar*)xmlStrchr(next, ' ');
2129 if(next != NULL) {
2130 *(next++) = '\0';
2131 }
2132
2133 /* do register namespace */
2134 if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
2135 fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
2136 xmlFree(nsListDup);
2137 return(-1);
2138 }
2139 }
2140
2141 xmlFree(nsListDup);
2142 return(0);
2143}
Daniel Veillard20887ee2005-07-04 09:27:40 +00002144/**
2145 * xmlShellRegisterRootNamespaces:
2146 * @ctxt: the shell context
2147 * @arg: unused
2148 * @node: the root element
2149 * @node2: unused
2150 *
2151 * Implements the XML shell function "setrootns"
2152 * which registers all namespaces declarations found on the root element.
2153 *
2154 * Returns 0 on success and a negative value otherwise.
2155 */
2156static int
2157xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2158 xmlNodePtr root, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2159{
2160 xmlNsPtr ns;
2161
2162 if ((root == NULL) || (root->type != XML_ELEMENT_NODE) ||
2163 (root->nsDef == NULL) || (ctxt == NULL) || (ctxt->pctxt == NULL))
2164 return(-1);
2165 ns = root->nsDef;
2166 while (ns != NULL) {
2167 if (ns->prefix == NULL)
2168 xmlXPathRegisterNs(ctxt->pctxt, BAD_CAST "defaultns", ns->href);
2169 else
2170 xmlXPathRegisterNs(ctxt->pctxt, ns->prefix, ns->href);
2171 ns = ns->next;
2172 }
2173 return(0);
2174}
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002175#endif
2176
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002177/**
Daniel Veillard1e208222002-10-22 14:25:25 +00002178 * xmlShellGrep:
2179 * @ctxt: the shell context
2180 * @arg: the string or regular expression to find
2181 * @node: a node
2182 * @node2: unused
2183 *
2184 * Implements the XML shell function "grep"
2185 * dumps informations about the node (namespace, attributes, content).
2186 *
2187 * Returns 0
2188 */
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002189static int
Daniel Veillard1e208222002-10-22 14:25:25 +00002190xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2191 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2192{
2193 if (!ctxt)
2194 return (0);
2195 if (node == NULL)
2196 return (0);
2197 if (arg == NULL)
2198 return (0);
2199#ifdef LIBXML_REGEXP_ENABLED
2200 if ((xmlStrchr((xmlChar *) arg, '?')) ||
2201 (xmlStrchr((xmlChar *) arg, '*')) ||
2202 (xmlStrchr((xmlChar *) arg, '.')) ||
2203 (xmlStrchr((xmlChar *) arg, '['))) {
2204 }
2205#endif
2206 while (node != NULL) {
2207 if (node->type == XML_COMMENT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002208 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002209
2210 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
2211 xmlShellList(ctxt, NULL, node, NULL);
2212 }
2213 } else if (node->type == XML_TEXT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002214 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002215
2216 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002217 xmlShellList(ctxt, NULL, node->parent, NULL);
Daniel Veillard1e208222002-10-22 14:25:25 +00002218 }
2219 }
2220
2221 /*
2222 * Browse the full subtree, deep first
2223 */
2224
2225 if ((node->type == XML_DOCUMENT_NODE) ||
2226 (node->type == XML_HTML_DOCUMENT_NODE)) {
2227 node = ((xmlDocPtr) node)->children;
2228 } else if ((node->children != NULL)
2229 && (node->type != XML_ENTITY_REF_NODE)) {
2230 /* deep first */
2231 node = node->children;
2232 } else if (node->next != NULL) {
2233 /* then siblings */
2234 node = node->next;
2235 } else {
2236 /* go up to parents->next if needed */
2237 while (node != NULL) {
2238 if (node->parent != NULL) {
2239 node = node->parent;
2240 }
2241 if (node->next != NULL) {
2242 node = node->next;
2243 break;
2244 }
2245 if (node->parent == NULL) {
2246 node = NULL;
2247 break;
2248 }
2249 }
2250 }
2251 }
2252 return (0);
2253}
2254
2255/**
Owen Taylor3473f882001-02-23 17:55:21 +00002256 * xmlShellDir:
2257 * @ctxt: the shell context
2258 * @arg: unused
2259 * @node: a node
2260 * @node2: unused
2261 *
2262 * Implements the XML shell function "dir"
2263 * dumps informations about the node (namespace, attributes, content).
2264 *
2265 * Returns 0
2266 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002267int
2268xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2269 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2270 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2271{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002272 if (!ctxt)
2273 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002274 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002275 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002276 return (0);
2277 }
Owen Taylor3473f882001-02-23 17:55:21 +00002278 if ((node->type == XML_DOCUMENT_NODE) ||
2279 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002280 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
Owen Taylor3473f882001-02-23 17:55:21 +00002281 } else if (node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002282 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002283 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002284 xmlDebugDumpOneNode(ctxt->output, node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002285 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002286 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002287}
2288
Daniel Veillard29b17482004-08-16 00:39:03 +00002289/**
2290 * xmlShellSetContent:
2291 * @ctxt: the shell context
2292 * @value: the content as a string
2293 * @node: a node
2294 * @node2: unused
2295 *
2296 * Implements the XML shell function "dir"
2297 * dumps informations about the node (namespace, attributes, content).
2298 *
2299 * Returns 0
2300 */
2301static int
2302xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2303 char *value, xmlNodePtr node,
2304 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2305{
2306 xmlNodePtr results;
2307 xmlParserErrors ret;
2308
2309 if (!ctxt)
2310 return (0);
2311 if (node == NULL) {
2312 fprintf(ctxt->output, "NULL\n");
2313 return (0);
2314 }
2315 if (value == NULL) {
2316 fprintf(ctxt->output, "NULL\n");
2317 return (0);
2318 }
2319
2320 ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
2321 if (ret == XML_ERR_OK) {
2322 if (node->children != NULL) {
2323 xmlFreeNodeList(node->children);
2324 node->children = NULL;
2325 node->last = NULL;
2326 }
2327 xmlAddChildList(node, results);
2328 } else {
2329 fprintf(ctxt->output, "failed to parse content\n");
2330 }
2331 return (0);
2332}
2333
Daniel Veillard522bc602004-02-21 11:53:09 +00002334#ifdef LIBXML_SCHEMAS_ENABLED
2335/**
2336 * xmlShellRNGValidate:
2337 * @ctxt: the shell context
2338 * @schemas: the path to the Relax-NG schemas
2339 * @node: a node
2340 * @node2: unused
2341 *
2342 * Implements the XML shell function "relaxng"
2343 * validating the instance against a Relax-NG schemas
2344 *
2345 * Returns 0
2346 */
2347static int
2348xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
2349 xmlNodePtr node ATTRIBUTE_UNUSED,
2350 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2351{
2352 xmlRelaxNGPtr relaxngschemas;
2353 xmlRelaxNGParserCtxtPtr ctxt;
2354 xmlRelaxNGValidCtxtPtr vctxt;
2355 int ret;
2356
2357 ctxt = xmlRelaxNGNewParserCtxt(schemas);
2358 xmlRelaxNGSetParserErrors(ctxt,
2359 (xmlRelaxNGValidityErrorFunc) fprintf,
2360 (xmlRelaxNGValidityWarningFunc) fprintf,
2361 stderr);
2362 relaxngschemas = xmlRelaxNGParse(ctxt);
2363 xmlRelaxNGFreeParserCtxt(ctxt);
2364 if (relaxngschemas == NULL) {
2365 xmlGenericError(xmlGenericErrorContext,
2366 "Relax-NG schema %s failed to compile\n", schemas);
2367 return(-1);
2368 }
2369 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2370 xmlRelaxNGSetValidErrors(vctxt,
2371 (xmlRelaxNGValidityErrorFunc) fprintf,
2372 (xmlRelaxNGValidityWarningFunc) fprintf,
2373 stderr);
2374 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2375 if (ret == 0) {
2376 fprintf(stderr, "%s validates\n", sctxt->filename);
2377 } else if (ret > 0) {
2378 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2379 } else {
2380 fprintf(stderr, "%s validation generated an internal error\n",
2381 sctxt->filename);
2382 }
2383 xmlRelaxNGFreeValidCtxt(vctxt);
2384 if (relaxngschemas != NULL)
2385 xmlRelaxNGFree(relaxngschemas);
2386 return(0);
2387}
2388#endif
2389
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002390#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002391/**
2392 * xmlShellCat:
2393 * @ctxt: the shell context
2394 * @arg: unused
2395 * @node: a node
2396 * @node2: unused
2397 *
2398 * Implements the XML shell function "cat"
2399 * dumps the serialization node content (XML or HTML).
2400 *
2401 * Returns 0
2402 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002403int
2404xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2405 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2406{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002407 if (!ctxt)
2408 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002409 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002410 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002411 return (0);
2412 }
Owen Taylor3473f882001-02-23 17:55:21 +00002413 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2414#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002415 if (node->type == XML_HTML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002416 htmlDocDump(ctxt->output, (htmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002417 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002418 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002419#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002420 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002421 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002422 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002423 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002424#endif /* LIBXML_HTML_ENABLED */
2425 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00002426 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002427 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002428 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002429 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002430 }
Daniel Veillard321be0c2002-10-08 21:26:42 +00002431 fprintf(ctxt->output, "\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002432 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002433}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002434#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002435
2436/**
2437 * xmlShellLoad:
2438 * @ctxt: the shell context
2439 * @filename: the file name
2440 * @node: unused
2441 * @node2: unused
2442 *
2443 * Implements the XML shell function "load"
2444 * loads a new document specified by the filename
2445 *
2446 * Returns 0 or -1 if loading failed
2447 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002448int
2449xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2450 xmlNodePtr node ATTRIBUTE_UNUSED,
2451 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2452{
Owen Taylor3473f882001-02-23 17:55:21 +00002453 xmlDocPtr doc;
2454 int html = 0;
2455
Daniel Veillarda82b1822004-11-08 16:24:57 +00002456 if ((ctxt == NULL) || (filename == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002457 if (ctxt->doc != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002458 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00002459
2460 if (html) {
2461#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002462 doc = htmlParseFile(filename, NULL);
2463#else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002464 fprintf(ctxt->output, "HTML support not compiled in\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002465 doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002466#endif /* LIBXML_HTML_ENABLED */
2467 } else {
Daniel Veillardebe25d42004-03-25 09:35:49 +00002468 doc = xmlReadFile(filename,NULL,0);
Owen Taylor3473f882001-02-23 17:55:21 +00002469 }
2470 if (doc != NULL) {
2471 if (ctxt->loaded == 1) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002472 xmlFreeDoc(ctxt->doc);
2473 }
2474 ctxt->loaded = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002475#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002476 xmlXPathFreeContext(ctxt->pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00002477#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002478 xmlFree(ctxt->filename);
2479 ctxt->doc = doc;
2480 ctxt->node = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002481#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002482 ctxt->pctxt = xmlXPathNewContext(doc);
Owen Taylor3473f882001-02-23 17:55:21 +00002483#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard85095e22003-04-23 13:56:44 +00002484 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002485 } else
Daniel Veillard78d12092001-10-11 09:12:24 +00002486 return (-1);
2487 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002488}
2489
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002490#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002491/**
2492 * xmlShellWrite:
2493 * @ctxt: the shell context
2494 * @filename: the file name
2495 * @node: a node in the tree
2496 * @node2: unused
2497 *
2498 * Implements the XML shell function "write"
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002499 * Write the current node to the filename, it saves the serialization
Owen Taylor3473f882001-02-23 17:55:21 +00002500 * of the subtree under the @node specified
2501 *
2502 * Returns 0 or -1 in case of error
2503 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002504int
Owen Taylor3473f882001-02-23 17:55:21 +00002505xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Daniel Veillard78d12092001-10-11 09:12:24 +00002506 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2507{
Owen Taylor3473f882001-02-23 17:55:21 +00002508 if (node == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002509 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002510 if ((filename == NULL) || (filename[0] == 0)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002511 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002512 }
2513#ifdef W_OK
2514 if (access((char *) filename, W_OK)) {
2515 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002516 "Cannot write to %s\n", filename);
2517 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002518 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002519#endif
2520 switch (node->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002521 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002522 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2523 xmlGenericError(xmlGenericErrorContext,
2524 "Failed to write to %s\n", filename);
2525 return (-1);
2526 }
2527 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002528 case XML_HTML_DOCUMENT_NODE:
2529#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002530 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2531 xmlGenericError(xmlGenericErrorContext,
2532 "Failed to write to %s\n", filename);
2533 return (-1);
2534 }
Owen Taylor3473f882001-02-23 17:55:21 +00002535#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002536 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2537 xmlGenericError(xmlGenericErrorContext,
2538 "Failed to write to %s\n", filename);
2539 return (-1);
2540 }
Owen Taylor3473f882001-02-23 17:55:21 +00002541#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002542 break;
2543 default:{
2544 FILE *f;
Owen Taylor3473f882001-02-23 17:55:21 +00002545
Daniel Veillard78d12092001-10-11 09:12:24 +00002546 f = fopen((char *) filename, "w");
2547 if (f == NULL) {
2548 xmlGenericError(xmlGenericErrorContext,
2549 "Failed to write to %s\n", filename);
2550 return (-1);
2551 }
2552 xmlElemDump(f, ctxt->doc, node);
2553 fclose(f);
2554 }
Owen Taylor3473f882001-02-23 17:55:21 +00002555 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002556 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002557}
2558
2559/**
2560 * xmlShellSave:
2561 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002562 * @filename: the file name (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002563 * @node: unused
2564 * @node2: unused
2565 *
2566 * Implements the XML shell function "save"
2567 * Write the current document to the filename, or it's original name
2568 *
2569 * Returns 0 or -1 in case of error
2570 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002571int
2572xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2573 xmlNodePtr node ATTRIBUTE_UNUSED,
2574 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2575{
Daniel Veillarda82b1822004-11-08 16:24:57 +00002576 if ((ctxt == NULL) || (ctxt->doc == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002577 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002578 if ((filename == NULL) || (filename[0] == 0))
2579 filename = ctxt->filename;
Daniel Veillarda82b1822004-11-08 16:24:57 +00002580 if (filename == NULL)
2581 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002582#ifdef W_OK
2583 if (access((char *) filename, W_OK)) {
2584 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002585 "Cannot save to %s\n", filename);
2586 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002587 }
2588#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002589 switch (ctxt->doc->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002590 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002591 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2592 xmlGenericError(xmlGenericErrorContext,
2593 "Failed to save to %s\n", filename);
2594 }
2595 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002596 case XML_HTML_DOCUMENT_NODE:
2597#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002598 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2599 xmlGenericError(xmlGenericErrorContext,
2600 "Failed to save to %s\n", filename);
2601 }
Owen Taylor3473f882001-02-23 17:55:21 +00002602#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002603 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2604 xmlGenericError(xmlGenericErrorContext,
2605 "Failed to save to %s\n", filename);
2606 }
Owen Taylor3473f882001-02-23 17:55:21 +00002607#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002608 break;
2609 default:
2610 xmlGenericError(xmlGenericErrorContext,
2611 "To save to subparts of a document use the 'write' command\n");
2612 return (-1);
2613
Owen Taylor3473f882001-02-23 17:55:21 +00002614 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002615 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002616}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002617#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002618
Daniel Veillardf54cd532004-02-25 11:52:31 +00002619#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002620/**
2621 * xmlShellValidate:
2622 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002623 * @dtd: the DTD URI (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002624 * @node: unused
2625 * @node2: unused
2626 *
2627 * Implements the XML shell function "validate"
2628 * Validate the document, if a DTD path is provided, then the validation
2629 * is done against the given DTD.
2630 *
2631 * Returns 0 or -1 in case of error
2632 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002633int
2634xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2635 xmlNodePtr node ATTRIBUTE_UNUSED,
2636 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2637{
Owen Taylor3473f882001-02-23 17:55:21 +00002638 xmlValidCtxt vctxt;
2639 int res = -1;
2640
Daniel Veillarda82b1822004-11-08 16:24:57 +00002641 if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002642 vctxt.userData = stderr;
2643 vctxt.error = (xmlValidityErrorFunc) fprintf;
2644 vctxt.warning = (xmlValidityWarningFunc) fprintf;
2645
2646 if ((dtd == NULL) || (dtd[0] == 0)) {
2647 res = xmlValidateDocument(&vctxt, ctxt->doc);
2648 } else {
2649 xmlDtdPtr subset;
2650
Daniel Veillard78d12092001-10-11 09:12:24 +00002651 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2652 if (subset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002653 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2654
Daniel Veillard78d12092001-10-11 09:12:24 +00002655 xmlFreeDtd(subset);
2656 }
Owen Taylor3473f882001-02-23 17:55:21 +00002657 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002658 return (res);
Owen Taylor3473f882001-02-23 17:55:21 +00002659}
Daniel Veillardf54cd532004-02-25 11:52:31 +00002660#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002661
2662/**
2663 * xmlShellDu:
2664 * @ctxt: the shell context
2665 * @arg: unused
2666 * @tree: a node defining a subtree
2667 * @node2: unused
2668 *
2669 * Implements the XML shell function "du"
2670 * show the structure of the subtree under node @tree
2671 * If @tree is null, the command works on the current node.
2672 *
2673 * Returns 0 or -1 in case of error
2674 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002675int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002676xmlShellDu(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002677 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2678 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2679{
Owen Taylor3473f882001-02-23 17:55:21 +00002680 xmlNodePtr node;
Daniel Veillard78d12092001-10-11 09:12:24 +00002681 int indent = 0, i;
Owen Taylor3473f882001-02-23 17:55:21 +00002682
Daniel Veillard321be0c2002-10-08 21:26:42 +00002683 if (!ctxt)
2684 return (-1);
2685
Daniel Veillard78d12092001-10-11 09:12:24 +00002686 if (tree == NULL)
2687 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002688 node = tree;
2689 while (node != NULL) {
2690 if ((node->type == XML_DOCUMENT_NODE) ||
2691 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002692 fprintf(ctxt->output, "/\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002693 } else if (node->type == XML_ELEMENT_NODE) {
2694 for (i = 0; i < indent; i++)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002695 fprintf(ctxt->output, " ");
2696 fprintf(ctxt->output, "%s\n", node->name);
Daniel Veillard78d12092001-10-11 09:12:24 +00002697 } else {
2698 }
Owen Taylor3473f882001-02-23 17:55:21 +00002699
Daniel Veillard78d12092001-10-11 09:12:24 +00002700 /*
2701 * Browse the full subtree, deep first
2702 */
Owen Taylor3473f882001-02-23 17:55:21 +00002703
2704 if ((node->type == XML_DOCUMENT_NODE) ||
2705 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002706 node = ((xmlDocPtr) node)->children;
2707 } else if ((node->children != NULL)
2708 && (node->type != XML_ENTITY_REF_NODE)) {
2709 /* deep first */
2710 node = node->children;
2711 indent++;
2712 } else if ((node != tree) && (node->next != NULL)) {
2713 /* then siblings */
2714 node = node->next;
2715 } else if (node != tree) {
2716 /* go up to parents->next if needed */
2717 while (node != tree) {
2718 if (node->parent != NULL) {
2719 node = node->parent;
2720 indent--;
2721 }
2722 if ((node != tree) && (node->next != NULL)) {
2723 node = node->next;
2724 break;
2725 }
2726 if (node->parent == NULL) {
2727 node = NULL;
2728 break;
2729 }
2730 if (node == tree) {
2731 node = NULL;
2732 break;
2733 }
2734 }
2735 /* exit condition */
2736 if (node == tree)
2737 node = NULL;
2738 } else
2739 node = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002740 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002741 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002742}
2743
2744/**
2745 * xmlShellPwd:
2746 * @ctxt: the shell context
2747 * @buffer: the output buffer
Daniel Veillard9d06d302002-01-22 18:15:52 +00002748 * @node: a node
Owen Taylor3473f882001-02-23 17:55:21 +00002749 * @node2: unused
2750 *
2751 * Implements the XML shell function "pwd"
2752 * Show the full path from the root to the node, if needed building
2753 * thumblers when similar elements exists at a given ancestor level.
2754 * The output is compatible with XPath commands.
2755 *
2756 * Returns 0 or -1 in case of error
2757 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002758int
2759xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2760 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2761{
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002762 xmlChar *path;
Owen Taylor3473f882001-02-23 17:55:21 +00002763
Daniel Veillarda82b1822004-11-08 16:24:57 +00002764 if ((node == NULL) || (buffer == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002765 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002766
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002767 path = xmlGetNodePath(node);
2768 if (path == NULL)
2769 return (-1);
2770
2771 /*
2772 * This test prevents buffer overflow, because this routine
2773 * is only called by xmlShell, in which the second argument is
2774 * 500 chars long.
2775 * It is a dirty hack before a cleaner solution is found.
2776 * Documentation should mention that the second argument must
2777 * be at least 500 chars long, and could be stripped if too long.
2778 */
2779 snprintf(buffer, 499, "%s", path);
2780 buffer[499] = '0';
2781 xmlFree(path);
2782
Daniel Veillard78d12092001-10-11 09:12:24 +00002783 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002784}
2785
2786/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002787 * xmlShell:
Owen Taylor3473f882001-02-23 17:55:21 +00002788 * @doc: the initial document
2789 * @filename: the output buffer
2790 * @input: the line reading function
Daniel Veillard321be0c2002-10-08 21:26:42 +00002791 * @output: the output FILE*, defaults to stdout if NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002792 *
2793 * Implements the XML shell
2794 * This allow to load, validate, view, modify and save a document
2795 * using a environment similar to a UNIX commandline.
2796 */
2797void
2798xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Daniel Veillard78d12092001-10-11 09:12:24 +00002799 FILE * output)
2800{
Owen Taylor3473f882001-02-23 17:55:21 +00002801 char prompt[500] = "/ > ";
2802 char *cmdline = NULL, *cur;
2803 int nbargs;
2804 char command[100];
2805 char arg[400];
2806 int i;
2807 xmlShellCtxtPtr ctxt;
2808 xmlXPathObjectPtr list;
2809
2810 if (doc == NULL)
2811 return;
2812 if (filename == NULL)
2813 return;
2814 if (input == NULL)
2815 return;
2816 if (output == NULL)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002817 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00002818 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Daniel Veillard78d12092001-10-11 09:12:24 +00002819 if (ctxt == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00002820 return;
2821 ctxt->loaded = 0;
2822 ctxt->doc = doc;
2823 ctxt->input = input;
2824 ctxt->output = output;
2825 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Daniel Veillard78d12092001-10-11 09:12:24 +00002826 ctxt->node = (xmlNodePtr) ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002827
2828#ifdef LIBXML_XPATH_ENABLED
2829 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2830 if (ctxt->pctxt == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002831 xmlFree(ctxt);
2832 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002833 }
2834#endif /* LIBXML_XPATH_ENABLED */
2835 while (1) {
2836 if (ctxt->node == (xmlNodePtr) ctxt->doc)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002837 snprintf(prompt, sizeof(prompt), "%s > ", "/");
Daniel Veillard7a985a12003-07-06 17:57:42 +00002838 else if ((ctxt->node != NULL) && (ctxt->node->name))
Daniel Veillard78d12092001-10-11 09:12:24 +00002839 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002840 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002841 snprintf(prompt, sizeof(prompt), "? > ");
Owen Taylor3473f882001-02-23 17:55:21 +00002842 prompt[sizeof(prompt) - 1] = 0;
2843
Daniel Veillard78d12092001-10-11 09:12:24 +00002844 /*
2845 * Get a new command line
2846 */
Owen Taylor3473f882001-02-23 17:55:21 +00002847 cmdline = ctxt->input(prompt);
Daniel Veillard78d12092001-10-11 09:12:24 +00002848 if (cmdline == NULL)
2849 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002850
Daniel Veillard78d12092001-10-11 09:12:24 +00002851 /*
2852 * Parse the command itself
2853 */
2854 cur = cmdline;
2855 nbargs = 0;
2856 while ((*cur == ' ') || (*cur == '\t'))
2857 cur++;
2858 i = 0;
2859 while ((*cur != ' ') && (*cur != '\t') &&
2860 (*cur != '\n') && (*cur != '\r')) {
2861 if (*cur == 0)
2862 break;
2863 command[i++] = *cur++;
2864 }
2865 command[i] = 0;
2866 if (i == 0)
2867 continue;
2868 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002869
Daniel Veillard78d12092001-10-11 09:12:24 +00002870 /*
2871 * Parse the argument
2872 */
2873 while ((*cur == ' ') || (*cur == '\t'))
2874 cur++;
2875 i = 0;
2876 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2877 if (*cur == 0)
2878 break;
2879 arg[i++] = *cur++;
2880 }
2881 arg[i] = 0;
2882 if (i != 0)
2883 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002884
Daniel Veillard78d12092001-10-11 09:12:24 +00002885 /*
2886 * start interpreting the command
2887 */
Owen Taylor3473f882001-02-23 17:55:21 +00002888 if (!strcmp(command, "exit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002889 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002890 if (!strcmp(command, "quit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002891 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002892 if (!strcmp(command, "bye"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002893 break;
Daniel Veillard5004f422001-11-08 13:53:05 +00002894 if (!strcmp(command, "help")) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002895 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2896 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2897 fprintf(ctxt->output, "\tbye leave shell\n");
2898 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2899 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2900 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2901 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2902 fprintf(ctxt->output, "\texit leave shell\n");
2903 fprintf(ctxt->output, "\thelp display this help\n");
2904 fprintf(ctxt->output, "\tfree display memory usage\n");
2905 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2906 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00002907 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 +00002908#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002909 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002910 fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2911 fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
Daniel Veillard20887ee2005-07-04 09:27:40 +00002912 fprintf(ctxt->output, "\tsetrootns register all namespace found on the root element\n");
2913 fprintf(ctxt->output, "\t the default namespace if any uses 'defaultns' prefix\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002914#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard321be0c2002-10-08 21:26:42 +00002915 fprintf(ctxt->output, "\tpwd display current working directory\n");
2916 fprintf(ctxt->output, "\tquit leave shell\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002917#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002918 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00002919 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002920#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf54cd532004-02-25 11:52:31 +00002921#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002922 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002923#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard522bc602004-02-21 11:53:09 +00002924#ifdef LIBXML_SCHEMAS_ENABLED
2925 fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n");
2926#endif
Daniel Veillard1e208222002-10-22 14:25:25 +00002927 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002928#ifdef LIBXML_VALID_ENABLED
Daniel Veillard5004f422001-11-08 13:53:05 +00002929 } else if (!strcmp(command, "validate")) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002930 xmlShellValidate(ctxt, arg, NULL, NULL);
Daniel Veillardf54cd532004-02-25 11:52:31 +00002931#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002932 } else if (!strcmp(command, "load")) {
2933 xmlShellLoad(ctxt, arg, NULL, NULL);
Daniel Veillard522bc602004-02-21 11:53:09 +00002934#ifdef LIBXML_SCHEMAS_ENABLED
2935 } else if (!strcmp(command, "relaxng")) {
2936 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2937#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002938#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002939 } else if (!strcmp(command, "save")) {
2940 xmlShellSave(ctxt, arg, NULL, NULL);
2941 } else if (!strcmp(command, "write")) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00002942 if ((arg == NULL) || (arg[0] == 0))
2943 xmlGenericError(xmlGenericErrorContext,
2944 "Write command requires a filename argument\n");
2945 else
2946 xmlShellWrite(ctxt, arg, NULL, NULL);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002947#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard1e208222002-10-22 14:25:25 +00002948 } else if (!strcmp(command, "grep")) {
2949 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
Daniel Veillard78d12092001-10-11 09:12:24 +00002950 } else if (!strcmp(command, "free")) {
2951 if (arg[0] == 0) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002952 xmlMemShow(ctxt->output, 0);
Daniel Veillard78d12092001-10-11 09:12:24 +00002953 } else {
2954 int len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002955
Daniel Veillard78d12092001-10-11 09:12:24 +00002956 sscanf(arg, "%d", &len);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002957 xmlMemShow(ctxt->output, len);
Daniel Veillard78d12092001-10-11 09:12:24 +00002958 }
2959 } else if (!strcmp(command, "pwd")) {
2960 char dir[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002961
Daniel Veillard78d12092001-10-11 09:12:24 +00002962 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
Daniel Veillard321be0c2002-10-08 21:26:42 +00002963 fprintf(ctxt->output, "%s\n", dir);
Daniel Veillard78d12092001-10-11 09:12:24 +00002964 } else if (!strcmp(command, "du")) {
2965 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2966 } else if (!strcmp(command, "base")) {
2967 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
Daniel Veillard29b17482004-08-16 00:39:03 +00002968 } else if (!strcmp(command, "set")) {
2969 xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002970#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002971 } else if (!strcmp(command, "setns")) {
2972 if (arg[0] == 0) {
2973 xmlGenericError(xmlGenericErrorContext,
2974 "setns: prefix=[nsuri] required\n");
2975 } else {
2976 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
2977 }
Daniel Veillard20887ee2005-07-04 09:27:40 +00002978 } else if (!strcmp(command, "setrootns")) {
2979 xmlNodePtr root;
2980
2981 root = xmlDocGetRootElement(ctxt->doc);
2982 xmlShellRegisterRootNamespaces(ctxt, NULL, root, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002983 } else if (!strcmp(command, "xpath")) {
2984 if (arg[0] == 0) {
2985 xmlGenericError(xmlGenericErrorContext,
2986 "xpath: expression required\n");
2987 } else {
2988 ctxt->pctxt->node = ctxt->node;
2989 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002990 xmlXPathDebugDumpObject(ctxt->output, list, 0);
Daniel Veillard2070c482002-01-22 22:12:19 +00002991 xmlXPathFreeObject(list);
2992 }
2993#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard2156d432004-03-04 15:59:36 +00002994#ifdef LIBXML_TREE_ENABLED
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002995 } else if (!strcmp(command, "setbase")) {
2996 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2156d432004-03-04 15:59:36 +00002997#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002998 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
2999 int dir = (!strcmp(command, "dir"));
3000
3001 if (arg[0] == 0) {
3002 if (dir)
3003 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
3004 else
3005 xmlShellList(ctxt, NULL, ctxt->node, NULL);
3006 } else {
3007 ctxt->pctxt->node = ctxt->node;
Daniel Veillard61d80a22001-04-27 17:13:01 +00003008#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00003009 ctxt->pctxt->node = ctxt->node;
3010 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3011#else
3012 list = NULL;
3013#endif /* LIBXML_XPATH_ENABLED */
3014 if (list != NULL) {
3015 switch (list->type) {
3016 case XPATH_UNDEFINED:
3017 xmlGenericError(xmlGenericErrorContext,
3018 "%s: no such node\n", arg);
3019 break;
3020 case XPATH_NODESET:{
3021 int indx;
3022
Daniel Veillarda6825e82001-11-07 13:33:59 +00003023 if (list->nodesetval == NULL)
3024 break;
3025
Daniel Veillard78d12092001-10-11 09:12:24 +00003026 for (indx = 0;
3027 indx < list->nodesetval->nodeNr;
3028 indx++) {
3029 if (dir)
3030 xmlShellDir(ctxt, NULL,
3031 list->nodesetval->
3032 nodeTab[indx], NULL);
3033 else
3034 xmlShellList(ctxt, NULL,
3035 list->nodesetval->
3036 nodeTab[indx], NULL);
3037 }
3038 break;
3039 }
3040 case XPATH_BOOLEAN:
3041 xmlGenericError(xmlGenericErrorContext,
3042 "%s is a Boolean\n", arg);
3043 break;
3044 case XPATH_NUMBER:
3045 xmlGenericError(xmlGenericErrorContext,
3046 "%s is a number\n", arg);
3047 break;
3048 case XPATH_STRING:
3049 xmlGenericError(xmlGenericErrorContext,
3050 "%s is a string\n", arg);
3051 break;
3052 case XPATH_POINT:
3053 xmlGenericError(xmlGenericErrorContext,
3054 "%s is a point\n", arg);
3055 break;
3056 case XPATH_RANGE:
3057 xmlGenericError(xmlGenericErrorContext,
3058 "%s is a range\n", arg);
3059 break;
3060 case XPATH_LOCATIONSET:
3061 xmlGenericError(xmlGenericErrorContext,
3062 "%s is a range\n", arg);
3063 break;
3064 case XPATH_USERS:
3065 xmlGenericError(xmlGenericErrorContext,
3066 "%s is user-defined\n", arg);
3067 break;
3068 case XPATH_XSLT_TREE:
3069 xmlGenericError(xmlGenericErrorContext,
3070 "%s is an XSLT value tree\n",
3071 arg);
3072 break;
3073 }
3074#ifdef LIBXML_XPATH_ENABLED
3075 xmlXPathFreeObject(list);
Daniel Veillard61d80a22001-04-27 17:13:01 +00003076#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00003077 } else {
3078 xmlGenericError(xmlGenericErrorContext,
3079 "%s: no such node\n", arg);
3080 }
3081 ctxt->pctxt->node = NULL;
3082 }
3083 } else if (!strcmp(command, "cd")) {
3084 if (arg[0] == 0) {
3085 ctxt->node = (xmlNodePtr) ctxt->doc;
3086 } else {
3087#ifdef LIBXML_XPATH_ENABLED
3088 ctxt->pctxt->node = ctxt->node;
3089 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3090#else
3091 list = NULL;
3092#endif /* LIBXML_XPATH_ENABLED */
3093 if (list != NULL) {
3094 switch (list->type) {
3095 case XPATH_UNDEFINED:
3096 xmlGenericError(xmlGenericErrorContext,
3097 "%s: no such node\n", arg);
3098 break;
3099 case XPATH_NODESET:
Daniel Veillarda6825e82001-11-07 13:33:59 +00003100 if (list->nodesetval != NULL) {
3101 if (list->nodesetval->nodeNr == 1) {
3102 ctxt->node = list->nodesetval->nodeTab[0];
Daniel Veillard7a985a12003-07-06 17:57:42 +00003103 if ((ctxt->node != NULL) &&
3104 (ctxt->node->type ==
3105 XML_NAMESPACE_DECL)) {
3106 xmlGenericError(xmlGenericErrorContext,
3107 "cannot cd to namespace\n");
3108 ctxt->node = NULL;
3109 }
Daniel Veillarda6825e82001-11-07 13:33:59 +00003110 } else
3111 xmlGenericError(xmlGenericErrorContext,
3112 "%s is a %d Node Set\n",
3113 arg,
3114 list->nodesetval->nodeNr);
Daniel Veillard78d12092001-10-11 09:12:24 +00003115 } else
3116 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda6825e82001-11-07 13:33:59 +00003117 "%s is an empty Node Set\n",
3118 arg);
Daniel Veillard78d12092001-10-11 09:12:24 +00003119 break;
3120 case XPATH_BOOLEAN:
3121 xmlGenericError(xmlGenericErrorContext,
3122 "%s is a Boolean\n", arg);
3123 break;
3124 case XPATH_NUMBER:
3125 xmlGenericError(xmlGenericErrorContext,
3126 "%s is a number\n", arg);
3127 break;
3128 case XPATH_STRING:
3129 xmlGenericError(xmlGenericErrorContext,
3130 "%s is a string\n", arg);
3131 break;
3132 case XPATH_POINT:
3133 xmlGenericError(xmlGenericErrorContext,
3134 "%s is a point\n", arg);
3135 break;
3136 case XPATH_RANGE:
3137 xmlGenericError(xmlGenericErrorContext,
3138 "%s is a range\n", arg);
3139 break;
3140 case XPATH_LOCATIONSET:
3141 xmlGenericError(xmlGenericErrorContext,
3142 "%s is a range\n", arg);
3143 break;
3144 case XPATH_USERS:
3145 xmlGenericError(xmlGenericErrorContext,
3146 "%s is user-defined\n", arg);
3147 break;
3148 case XPATH_XSLT_TREE:
3149 xmlGenericError(xmlGenericErrorContext,
3150 "%s is an XSLT value tree\n",
3151 arg);
3152 break;
3153 }
3154#ifdef LIBXML_XPATH_ENABLED
3155 xmlXPathFreeObject(list);
3156#endif
3157 } else {
3158 xmlGenericError(xmlGenericErrorContext,
3159 "%s: no such node\n", arg);
3160 }
3161 ctxt->pctxt->node = NULL;
3162 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003163#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00003164 } else if (!strcmp(command, "cat")) {
3165 if (arg[0] == 0) {
3166 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3167 } else {
3168 ctxt->pctxt->node = ctxt->node;
3169#ifdef LIBXML_XPATH_ENABLED
3170 ctxt->pctxt->node = ctxt->node;
3171 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3172#else
3173 list = NULL;
3174#endif /* LIBXML_XPATH_ENABLED */
3175 if (list != NULL) {
3176 switch (list->type) {
3177 case XPATH_UNDEFINED:
3178 xmlGenericError(xmlGenericErrorContext,
3179 "%s: no such node\n", arg);
3180 break;
3181 case XPATH_NODESET:{
3182 int indx;
3183
Daniel Veillarda6825e82001-11-07 13:33:59 +00003184 if (list->nodesetval == NULL)
3185 break;
3186
Daniel Veillard78d12092001-10-11 09:12:24 +00003187 for (indx = 0;
3188 indx < list->nodesetval->nodeNr;
3189 indx++) {
3190 if (i > 0)
Daniel Veillard321be0c2002-10-08 21:26:42 +00003191 fprintf(ctxt->output, " -------\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00003192 xmlShellCat(ctxt, NULL,
3193 list->nodesetval->
3194 nodeTab[indx], NULL);
3195 }
3196 break;
3197 }
3198 case XPATH_BOOLEAN:
3199 xmlGenericError(xmlGenericErrorContext,
3200 "%s is a Boolean\n", arg);
3201 break;
3202 case XPATH_NUMBER:
3203 xmlGenericError(xmlGenericErrorContext,
3204 "%s is a number\n", arg);
3205 break;
3206 case XPATH_STRING:
3207 xmlGenericError(xmlGenericErrorContext,
3208 "%s is a string\n", arg);
3209 break;
3210 case XPATH_POINT:
3211 xmlGenericError(xmlGenericErrorContext,
3212 "%s is a point\n", arg);
3213 break;
3214 case XPATH_RANGE:
3215 xmlGenericError(xmlGenericErrorContext,
3216 "%s is a range\n", arg);
3217 break;
3218 case XPATH_LOCATIONSET:
3219 xmlGenericError(xmlGenericErrorContext,
3220 "%s is a range\n", arg);
3221 break;
3222 case XPATH_USERS:
3223 xmlGenericError(xmlGenericErrorContext,
3224 "%s is user-defined\n", arg);
3225 break;
3226 case XPATH_XSLT_TREE:
3227 xmlGenericError(xmlGenericErrorContext,
3228 "%s is an XSLT value tree\n",
3229 arg);
3230 break;
3231 }
3232#ifdef LIBXML_XPATH_ENABLED
3233 xmlXPathFreeObject(list);
3234#endif
3235 } else {
3236 xmlGenericError(xmlGenericErrorContext,
3237 "%s: no such node\n", arg);
3238 }
3239 ctxt->pctxt->node = NULL;
3240 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003241#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00003242 } else {
3243 xmlGenericError(xmlGenericErrorContext,
3244 "Unknown command %s\n", command);
3245 }
3246 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003247 }
3248#ifdef LIBXML_XPATH_ENABLED
3249 xmlXPathFreeContext(ctxt->pctxt);
3250#endif /* LIBXML_XPATH_ENABLED */
3251 if (ctxt->loaded) {
3252 xmlFreeDoc(ctxt->doc);
3253 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003254 if (ctxt->filename != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003255 xmlFree(ctxt->filename);
Owen Taylor3473f882001-02-23 17:55:21 +00003256 xmlFree(ctxt);
3257 if (cmdline != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003258 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003259}
3260
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00003261#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00003262#define bottom_debugXML
3263#include "elfgcchack.h"
Owen Taylor3473f882001-02-23 17:55:21 +00003264#endif /* LIBXML_DEBUG_ENABLED */