blob: d0e17c28e76312223a7c613c2bf90a1c6a1556e8 [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 Veillard22cdb842004-10-04 14:09:17 +000037typedef struct _xmlDebugCtxt xmlDebugCtxt;
38typedef xmlDebugCtxt *xmlDebugCtxtPtr;
39struct _xmlDebugCtxt {
40 FILE *output; /* the output file */
41 char shift[101]; /* used for indenting */
42 int depth; /* current depth */
43 xmlDocPtr doc; /* current document */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000044 xmlNodePtr node; /* current node */
Daniel Veillard03a53c32004-10-26 16:06:51 +000045 xmlDictPtr dict; /* the doc dictionnary */
Daniel Veillard22cdb842004-10-04 14:09:17 +000046 int check; /* do just checkings */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000047 int errors; /* number of errors found */
Daniel Veillard03a53c32004-10-26 16:06:51 +000048 int nodict; /* if the document has no dictionnary */
Daniel Veillard22cdb842004-10-04 14:09:17 +000049};
50
51static void xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node);
52
53static void
54xmlCtxtDumpInitCtxt(xmlDebugCtxtPtr ctxt)
55{
56 int i;
57
58 ctxt->depth = 0;
59 ctxt->check = 0;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000060 ctxt->errors = 0;
Daniel Veillard22cdb842004-10-04 14:09:17 +000061 ctxt->output = stdout;
Daniel Veillard03a53c32004-10-26 16:06:51 +000062 ctxt->doc = NULL;
63 ctxt->node = NULL;
64 ctxt->dict = NULL;
Daniel Veillard22cdb842004-10-04 14:09:17 +000065 for (i = 0; i < 100; i++)
66 ctxt->shift[i] = ' ';
67 ctxt->shift[100] = 0;
68}
69
70static void
William M. Brack9638d4c2004-10-15 18:25:33 +000071xmlCtxtDumpCleanCtxt(xmlDebugCtxtPtr ctxt ATTRIBUTE_UNUSED)
Daniel Veillard76821142004-10-09 20:39:04 +000072{
William M. Brack9638d4c2004-10-15 18:25:33 +000073 /* remove the ATTRIBUTE_UNUSED when this is added */
Daniel Veillard76821142004-10-09 20:39:04 +000074}
75
76/**
Daniel Veillard0d24b112004-10-11 12:28:34 +000077 * xmlNsCheckScope:
78 * @node: the node
79 * @ns: the namespace node
Daniel Veillard76821142004-10-09 20:39:04 +000080 *
Daniel Veillard0d24b112004-10-11 12:28:34 +000081 * Check that a given namespace is in scope on a node.
Daniel Veillard76821142004-10-09 20:39:04 +000082 *
Daniel Veillard0d24b112004-10-11 12:28:34 +000083 * Returns 1 if in scope, -1 in case of argument error,
84 * -2 if the namespace is not in scope, and -3 if not on
85 * an ancestor node.
Daniel Veillard76821142004-10-09 20:39:04 +000086 */
87static int
Daniel Veillard0d24b112004-10-11 12:28:34 +000088xmlNsCheckScope(xmlNodePtr node, xmlNsPtr ns)
Daniel Veillard76821142004-10-09 20:39:04 +000089{
Daniel Veillard0d24b112004-10-11 12:28:34 +000090 xmlNsPtr cur;
Daniel Veillard76821142004-10-09 20:39:04 +000091
Daniel Veillard0d24b112004-10-11 12:28:34 +000092 if ((node == NULL) || (ns == NULL))
93 return(-1);
94
95 if ((node->type != XML_ELEMENT_NODE) &&
96 (node->type != XML_ATTRIBUTE_NODE) &&
97 (node->type != XML_DOCUMENT_NODE) &&
98 (node->type != XML_TEXT_NODE) &&
99 (node->type != XML_HTML_DOCUMENT_NODE) &&
100 (node->type != XML_XINCLUDE_START))
101 return(-2);
102
103 while ((node != NULL) &&
104 ((node->type == XML_ELEMENT_NODE) ||
105 (node->type == XML_ATTRIBUTE_NODE) ||
106 (node->type == XML_TEXT_NODE) ||
107 (node->type == XML_XINCLUDE_START))) {
108 if ((node->type == XML_ELEMENT_NODE) ||
109 (node->type == XML_XINCLUDE_START)) {
110 cur = node->nsDef;
111 while (cur != NULL) {
112 if (cur == ns)
113 return(1);
114 if (xmlStrEqual(cur->prefix, ns->prefix))
115 return(-2);
116 cur = cur->next;
117 }
Daniel Veillard76821142004-10-09 20:39:04 +0000118 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000119 node = node->parent;
Daniel Veillard76821142004-10-09 20:39:04 +0000120 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000121 /* the xml namespace may be declared on the document node */
122 if ((node != NULL) &&
123 ((node->type == XML_DOCUMENT_NODE) ||
124 (node->type == XML_HTML_DOCUMENT_NODE))) {
125 xmlNsPtr oldNs = ((xmlDocPtr) node)->oldNs;
126 if (oldNs == ns)
127 return(1);
128 }
129 return(-3);
Daniel Veillard76821142004-10-09 20:39:04 +0000130}
Daniel Veillard76821142004-10-09 20:39:04 +0000131
Daniel Veillard76821142004-10-09 20:39:04 +0000132static void
Daniel Veillard22cdb842004-10-04 14:09:17 +0000133xmlCtxtDumpSpaces(xmlDebugCtxtPtr ctxt)
134{
135 if (ctxt->check)
136 return;
137 if ((ctxt->output != NULL) && (ctxt->depth > 0)) {
138 if (ctxt->depth < 50)
139 fprintf(ctxt->output, &ctxt->shift[100 - 2 * ctxt->depth]);
140 else
141 fprintf(ctxt->output, ctxt->shift);
142 }
143}
144
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000145/**
146 * xmlDebugErr:
147 * @ctxt: a debug context
148 * @error: the error code
149 *
150 * Handle a debug error.
151 */
152static void
153xmlDebugErr(xmlDebugCtxtPtr ctxt, int error, const char *msg)
154{
155 ctxt->errors++;
156 __xmlRaiseError(NULL, NULL, NULL,
157 NULL, ctxt->node, XML_FROM_CHECK,
158 error, XML_ERR_ERROR, NULL, 0,
159 NULL, NULL, NULL, 0, 0,
160 msg);
161}
162static void
163xmlDebugErr2(xmlDebugCtxtPtr ctxt, int error, const char *msg, int extra)
164{
165 ctxt->errors++;
166 __xmlRaiseError(NULL, NULL, NULL,
167 NULL, ctxt->node, XML_FROM_CHECK,
168 error, XML_ERR_ERROR, NULL, 0,
169 NULL, NULL, NULL, 0, 0,
170 msg, extra);
171}
172static void
Daniel Veillardc6095782004-10-15 14:50:10 +0000173xmlDebugErr3(xmlDebugCtxtPtr ctxt, int error, const char *msg, const char *extra)
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000174{
175 ctxt->errors++;
176 __xmlRaiseError(NULL, NULL, NULL,
177 NULL, ctxt->node, XML_FROM_CHECK,
178 error, XML_ERR_ERROR, NULL, 0,
179 NULL, NULL, NULL, 0, 0,
180 msg, extra);
181}
182
Daniel Veillard0d24b112004-10-11 12:28:34 +0000183/**
184 * xmlCtxtNsCheckScope:
185 * @ctxt: the debugging context
186 * @node: the node
187 * @ns: the namespace node
188 *
189 * Report if a given namespace is is not in scope.
190 */
191static void
192xmlCtxtNsCheckScope(xmlDebugCtxtPtr ctxt, xmlNodePtr node, xmlNsPtr ns)
193{
194 int ret;
195
196 ret = xmlNsCheckScope(node, ns);
197 if (ret == -2) {
198 if (ns->prefix == NULL)
199 xmlDebugErr(ctxt, XML_CHECK_NS_SCOPE,
200 "Reference to default namespace not in scope\n");
201 else
202 xmlDebugErr3(ctxt, XML_CHECK_NS_SCOPE,
203 "Reference to namespace '%s' not in scope\n",
204 (char *) ns->prefix);
205 }
206 if (ret == -3) {
207 if (ns->prefix == NULL)
208 xmlDebugErr(ctxt, XML_CHECK_NS_ANCESTOR,
209 "Reference to default namespace not on ancestor\n");
210 else
211 xmlDebugErr3(ctxt, XML_CHECK_NS_ANCESTOR,
212 "Reference to namespace '%s' not on ancestor\n",
213 (char *) ns->prefix);
214 }
215}
216
Daniel Veillardc6095782004-10-15 14:50:10 +0000217/**
218 * xmlCtxtCheckString:
219 * @ctxt: the debug context
220 * @str: the string
221 *
222 * Do debugging on the string, currently it just checks the UTF-8 content
223 */
224static void
225xmlCtxtCheckString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
226{
227 if (str == NULL) return;
228 if (ctxt->check) {
229 if (!xmlCheckUTF8(str)) {
Daniel Veillard03a53c32004-10-26 16:06:51 +0000230 xmlDebugErr3(ctxt, XML_CHECK_NOT_UTF8,
Daniel Veillardc6095782004-10-15 14:50:10 +0000231 "String is not UTF-8 %s", (const char *) str);
232 }
233 }
234}
235
Daniel Veillard03a53c32004-10-26 16:06:51 +0000236/**
237 * xmlCtxtCheckName:
238 * @ctxt: the debug context
239 * @name: the name
240 *
241 * Do debugging on the name, for example the dictionnary status and
242 * conformance to the Name production.
243 */
244static void
245xmlCtxtCheckName(xmlDebugCtxtPtr ctxt, const xmlChar * name)
246{
247 if (ctxt->check) {
248 if (name == NULL) {
249 xmlDebugErr(ctxt, XML_CHECK_NO_NAME, "Name is NULL");
250 return;
251 }
252 if (xmlValidateName(name, 0)) {
253 xmlDebugErr3(ctxt, XML_CHECK_NOT_NCNAME,
254 "Name is not an NCName '%s'", (const char *) name);
255 }
256 if ((ctxt->dict != NULL) &&
257 (!xmlDictOwns(ctxt->dict, name))) {
258 xmlDebugErr3(ctxt, XML_CHECK_OUTSIDE_DICT,
259 "Name is not from the document dictionnary '%s'",
260 (const char *) name);
261 }
262 }
263}
264
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000265static void
266xmlCtxtGenericNodeCheck(xmlDebugCtxtPtr ctxt, xmlNodePtr node) {
Daniel Veillard03a53c32004-10-26 16:06:51 +0000267 xmlDocPtr doc;
268 xmlDictPtr dict;
269
270 doc = node->doc;
271
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000272 if (node->parent == NULL)
273 xmlDebugErr(ctxt, XML_CHECK_NO_PARENT,
274 "Node has no parent\n");
Daniel Veillard03a53c32004-10-26 16:06:51 +0000275 if (node->doc == NULL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000276 xmlDebugErr(ctxt, XML_CHECK_NO_DOC,
277 "Node has no doc\n");
Daniel Veillard03a53c32004-10-26 16:06:51 +0000278 dict = NULL;
279 } else {
280 dict = doc->dict;
281 if ((dict == NULL) && (ctxt->nodict == 0)) {
282 xmlDebugErr(ctxt, XML_CHECK_NO_DICT,
283 "Document has no dictionnary\n");
284 ctxt->nodict = 1;
285 }
286 if (ctxt->doc == NULL)
287 ctxt->doc = doc;
288
289 if (ctxt->dict == NULL) {
290 ctxt->dict = dict;
291 }
292 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000293 if ((node->parent != NULL) && (node->doc != node->parent->doc) &&
294 (!xmlStrEqual(node->name, BAD_CAST "pseudoroot")))
295 xmlDebugErr(ctxt, XML_CHECK_WRONG_DOC,
296 "Node doc differs from parent's one\n");
297 if (node->prev == NULL) {
298 if (node->type == XML_ATTRIBUTE_NODE) {
299 if ((node->parent != NULL) &&
300 (node != (xmlNodePtr) node->parent->properties))
301 xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
302 "Attr has no prev and not first of attr list\n");
303
304 } else if ((node->parent != NULL) && (node->parent->children != node))
305 xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
306 "Node has no prev and not first of parent list\n");
307 } else {
308 if (node->prev->next != node)
309 xmlDebugErr(ctxt, XML_CHECK_WRONG_PREV,
310 "Node prev->next : back link wrong\n");
311 }
312 if (node->next == NULL) {
313 if ((node->parent != NULL) && (node->type != XML_ATTRIBUTE_NODE) &&
314 (node->parent->last != node))
315 xmlDebugErr(ctxt, XML_CHECK_NO_NEXT,
316 "Node has no next and not last of parent list\n");
317 } else {
318 if (node->next->prev != node)
319 xmlDebugErr(ctxt, XML_CHECK_WRONG_NEXT,
320 "Node next->prev : forward link wrong\n");
Daniel Veillard0d24b112004-10-11 12:28:34 +0000321 if (node->next->parent != node->parent)
322 xmlDebugErr(ctxt, XML_CHECK_WRONG_PARENT,
323 "Node next->prev : forward link wrong\n");
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000324 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000325 if (node->type == XML_ELEMENT_NODE) {
326 xmlNsPtr ns;
327
328 ns = node->nsDef;
329 while (ns != NULL) {
330 xmlCtxtNsCheckScope(ctxt, node, ns);
331 ns = ns->next;
332 }
333 if (node->ns != NULL)
334 xmlCtxtNsCheckScope(ctxt, node, node->ns);
335 } else if (node->type == XML_ATTRIBUTE_NODE) {
336 if (node->ns != NULL)
337 xmlCtxtNsCheckScope(ctxt, node, node->ns);
338 }
339
Daniel Veillardc6095782004-10-15 14:50:10 +0000340 if ((node->type != XML_ELEMENT_NODE) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000341 (node->type != XML_ATTRIBUTE_NODE) &&
342 (node->type != XML_ATTRIBUTE_DECL) &&
343 (node->type != XML_DTD_NODE) &&
344 (node->type != XML_HTML_DOCUMENT_NODE) &&
345 (node->type != XML_DOCUMENT_NODE)) {
Daniel Veillardc6095782004-10-15 14:50:10 +0000346 if (node->content != NULL)
William M. Brack9638d4c2004-10-15 18:25:33 +0000347 xmlCtxtCheckString(ctxt, (const xmlChar *) node->content);
Daniel Veillardc6095782004-10-15 14:50:10 +0000348 }
Daniel Veillard03a53c32004-10-26 16:06:51 +0000349 switch (node->type) {
350 case XML_ELEMENT_NODE:
351 case XML_ATTRIBUTE_NODE:
352 xmlCtxtCheckName(ctxt, node->name);
353 break;
354 case XML_TEXT_NODE:
355 if ((node->name == xmlStringText) ||
356 (node->name == xmlStringTextNoenc))
357 break;
358 /* some case of entity substitution can lead to this */
359 if ((ctxt->dict != NULL) &&
360 (node->name == xmlDictLookup(ctxt->dict, "nbktext", 7)))
361 break;
362
363 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
364 "Text node has wrong name '%s'",
365 (const char *) node->name);
366 break;
367 case XML_COMMENT_NODE:
368 if (node->name == xmlStringComment)
369 break;
370 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
371 "Comment node has wrong name '%s'",
372 (const char *) node->name);
373 break;
374 case XML_PI_NODE:
375 xmlCtxtCheckName(ctxt, node->name);
376 break;
377 case XML_CDATA_SECTION_NODE:
378 if (node->name == NULL)
379 break;
380 xmlDebugErr3(ctxt, XML_CHECK_NAME_NOT_NULL,
381 "CData section has non NULL name '%s'",
382 (const char *) node->name);
383 break;
384 case XML_ENTITY_REF_NODE:
385 case XML_ENTITY_NODE:
386 case XML_DOCUMENT_TYPE_NODE:
387 case XML_DOCUMENT_FRAG_NODE:
388 case XML_NOTATION_NODE:
389 case XML_DTD_NODE:
390 case XML_ELEMENT_DECL:
391 case XML_ATTRIBUTE_DECL:
392 case XML_ENTITY_DECL:
393 case XML_NAMESPACE_DECL:
394 case XML_XINCLUDE_START:
395 case XML_XINCLUDE_END:
396#ifdef LIBXML_DOCB_ENABLED
397 case XML_DOCB_DOCUMENT_NODE:
398#endif
399 case XML_DOCUMENT_NODE:
400 case XML_HTML_DOCUMENT_NODE:
401 break;
402 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000403}
404
Daniel Veillard22cdb842004-10-04 14:09:17 +0000405static void
406xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
407{
408 int i;
409
Daniel Veillardc6095782004-10-15 14:50:10 +0000410 if (ctxt->check) {
Daniel Veillard22cdb842004-10-04 14:09:17 +0000411 return;
Daniel Veillardc6095782004-10-15 14:50:10 +0000412 }
Daniel Veillard22cdb842004-10-04 14:09:17 +0000413 /* TODO: check UTF8 content of the string */
414 if (str == NULL) {
415 fprintf(ctxt->output, "(NULL)");
416 return;
417 }
418 for (i = 0; i < 40; i++)
419 if (str[i] == 0)
420 return;
421 else if (IS_BLANK_CH(str[i]))
422 fputc(' ', ctxt->output);
423 else if (str[i] >= 0x80)
424 fprintf(ctxt->output, "#%X", str[i]);
425 else
426 fputc(str[i], ctxt->output);
427 fprintf(ctxt->output, "...");
428}
429
430static void
431xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
432{
433 xmlCtxtDumpSpaces(ctxt);
434
435 if (dtd == NULL) {
436 if (!ctxt->check)
437 fprintf(ctxt->output, "DTD node is NULL\n");
438 return;
439 }
440
441 if (dtd->type != XML_DTD_NODE) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000442 xmlDebugErr(ctxt, XML_CHECK_NOT_DTD,
443 "Node is not a DTD");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000444 return;
445 }
446 if (!ctxt->check) {
447 if (dtd->name != NULL)
448 fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name);
449 else
450 fprintf(ctxt->output, "DTD");
451 if (dtd->ExternalID != NULL)
452 fprintf(ctxt->output, ", PUBLIC %s", (char *) dtd->ExternalID);
453 if (dtd->SystemID != NULL)
454 fprintf(ctxt->output, ", SYSTEM %s", (char *) dtd->SystemID);
455 fprintf(ctxt->output, "\n");
456 }
457 /*
458 * Do a bit of checking
459 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000460 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000461}
462
463static void
464xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr)
465{
466 xmlCtxtDumpSpaces(ctxt);
467
468 if (attr == NULL) {
469 if (!ctxt->check)
470 fprintf(ctxt->output, "Attribute declaration is NULL\n");
471 return;
472 }
473 if (attr->type != XML_ATTRIBUTE_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000474 xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL,
475 "Node is not an attribute declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000476 return;
477 }
478 if (attr->name != NULL) {
479 if (!ctxt->check)
480 fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name);
481 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000482 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
483 "Node attribute declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000484 if (attr->elem != NULL) {
485 if (!ctxt->check)
486 fprintf(ctxt->output, " for %s", (char *) attr->elem);
487 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000488 xmlDebugErr(ctxt, XML_CHECK_NO_ELEM,
489 "Node attribute declaration has no element name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000490 if (!ctxt->check) {
491 switch (attr->atype) {
492 case XML_ATTRIBUTE_CDATA:
493 fprintf(ctxt->output, " CDATA");
494 break;
495 case XML_ATTRIBUTE_ID:
496 fprintf(ctxt->output, " ID");
497 break;
498 case XML_ATTRIBUTE_IDREF:
499 fprintf(ctxt->output, " IDREF");
500 break;
501 case XML_ATTRIBUTE_IDREFS:
502 fprintf(ctxt->output, " IDREFS");
503 break;
504 case XML_ATTRIBUTE_ENTITY:
505 fprintf(ctxt->output, " ENTITY");
506 break;
507 case XML_ATTRIBUTE_ENTITIES:
508 fprintf(ctxt->output, " ENTITIES");
509 break;
510 case XML_ATTRIBUTE_NMTOKEN:
511 fprintf(ctxt->output, " NMTOKEN");
512 break;
513 case XML_ATTRIBUTE_NMTOKENS:
514 fprintf(ctxt->output, " NMTOKENS");
515 break;
516 case XML_ATTRIBUTE_ENUMERATION:
517 fprintf(ctxt->output, " ENUMERATION");
518 break;
519 case XML_ATTRIBUTE_NOTATION:
520 fprintf(ctxt->output, " NOTATION ");
521 break;
522 }
523 if (attr->tree != NULL) {
524 int indx;
525 xmlEnumerationPtr cur = attr->tree;
526
527 for (indx = 0; indx < 5; indx++) {
528 if (indx != 0)
529 fprintf(ctxt->output, "|%s", (char *) cur->name);
530 else
531 fprintf(ctxt->output, " (%s", (char *) cur->name);
532 cur = cur->next;
533 if (cur == NULL)
534 break;
535 }
536 if (cur == NULL)
537 fprintf(ctxt->output, ")");
538 else
539 fprintf(ctxt->output, "...)");
540 }
541 switch (attr->def) {
542 case XML_ATTRIBUTE_NONE:
543 break;
544 case XML_ATTRIBUTE_REQUIRED:
545 fprintf(ctxt->output, " REQUIRED");
546 break;
547 case XML_ATTRIBUTE_IMPLIED:
548 fprintf(ctxt->output, " IMPLIED");
549 break;
550 case XML_ATTRIBUTE_FIXED:
551 fprintf(ctxt->output, " FIXED");
552 break;
553 }
554 if (attr->defaultValue != NULL) {
555 fprintf(ctxt->output, "\"");
556 xmlCtxtDumpString(ctxt, attr->defaultValue);
557 fprintf(ctxt->output, "\"");
558 }
559 fprintf(ctxt->output, "\n");
560 }
561
562 /*
563 * Do a bit of checking
564 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000565 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000566}
567
568static void
569xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem)
570{
571 xmlCtxtDumpSpaces(ctxt);
572
573 if (elem == NULL) {
574 if (!ctxt->check)
575 fprintf(ctxt->output, "Element declaration is NULL\n");
576 return;
577 }
578 if (elem->type != XML_ELEMENT_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000579 xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL,
580 "Node is not an element declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000581 return;
582 }
583 if (elem->name != NULL) {
584 if (!ctxt->check) {
585 fprintf(ctxt->output, "ELEMDECL(");
586 xmlCtxtDumpString(ctxt, elem->name);
587 fprintf(ctxt->output, ")");
588 }
589 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000590 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
591 "Element declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000592 if (!ctxt->check) {
593 switch (elem->etype) {
594 case XML_ELEMENT_TYPE_UNDEFINED:
595 fprintf(ctxt->output, ", UNDEFINED");
596 break;
597 case XML_ELEMENT_TYPE_EMPTY:
598 fprintf(ctxt->output, ", EMPTY");
599 break;
600 case XML_ELEMENT_TYPE_ANY:
601 fprintf(ctxt->output, ", ANY");
602 break;
603 case XML_ELEMENT_TYPE_MIXED:
604 fprintf(ctxt->output, ", MIXED ");
605 break;
606 case XML_ELEMENT_TYPE_ELEMENT:
607 fprintf(ctxt->output, ", MIXED ");
608 break;
609 }
610 if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) {
611 char buf[5001];
612
613 buf[0] = 0;
614 xmlSnprintfElementContent(buf, 5000, elem->content, 1);
615 buf[5000] = 0;
616 fprintf(ctxt->output, "%s", buf);
617 }
618 fprintf(ctxt->output, "\n");
619 }
620
621 /*
622 * Do a bit of checking
623 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000624 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000625}
626
627static void
628xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
629{
630 xmlCtxtDumpSpaces(ctxt);
631
632 if (ent == NULL) {
633 if (!ctxt->check)
634 fprintf(ctxt->output, "Entity declaration is NULL\n");
635 return;
636 }
637 if (ent->type != XML_ENTITY_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000638 xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL,
639 "Node is not an entity declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000640 return;
641 }
642 if (ent->name != NULL) {
643 if (!ctxt->check) {
644 fprintf(ctxt->output, "ENTITYDECL(");
645 xmlCtxtDumpString(ctxt, ent->name);
646 fprintf(ctxt->output, ")");
647 }
648 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000649 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
650 "Entity declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000651 if (!ctxt->check) {
652 switch (ent->etype) {
653 case XML_INTERNAL_GENERAL_ENTITY:
654 fprintf(ctxt->output, ", internal\n");
655 break;
656 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
657 fprintf(ctxt->output, ", external parsed\n");
658 break;
659 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
660 fprintf(ctxt->output, ", unparsed\n");
661 break;
662 case XML_INTERNAL_PARAMETER_ENTITY:
663 fprintf(ctxt->output, ", parameter\n");
664 break;
665 case XML_EXTERNAL_PARAMETER_ENTITY:
666 fprintf(ctxt->output, ", external parameter\n");
667 break;
668 case XML_INTERNAL_PREDEFINED_ENTITY:
669 fprintf(ctxt->output, ", predefined\n");
670 break;
671 }
672 if (ent->ExternalID) {
673 xmlCtxtDumpSpaces(ctxt);
674 fprintf(ctxt->output, " ExternalID=%s\n",
675 (char *) ent->ExternalID);
676 }
677 if (ent->SystemID) {
678 xmlCtxtDumpSpaces(ctxt);
679 fprintf(ctxt->output, " SystemID=%s\n",
680 (char *) ent->SystemID);
681 }
682 if (ent->URI != NULL) {
683 xmlCtxtDumpSpaces(ctxt);
684 fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI);
685 }
686 if (ent->content) {
687 xmlCtxtDumpSpaces(ctxt);
688 fprintf(ctxt->output, " content=");
689 xmlCtxtDumpString(ctxt, ent->content);
690 fprintf(ctxt->output, "\n");
691 }
692 }
693
694 /*
695 * Do a bit of checking
696 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000697 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000698}
699
700static void
701xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
702{
703 xmlCtxtDumpSpaces(ctxt);
704
705 if (ns == NULL) {
706 if (!ctxt->check)
707 fprintf(ctxt->output, "namespace node is NULL\n");
708 return;
709 }
710 if (ns->type != XML_NAMESPACE_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000711 xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL,
712 "Node is not a namespace declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000713 return;
714 }
715 if (ns->href == NULL) {
716 if (ns->prefix != NULL)
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000717 xmlDebugErr3(ctxt, XML_CHECK_NO_HREF,
718 "Incomplete namespace %s href=NULL\n",
Daniel Veillard22cdb842004-10-04 14:09:17 +0000719 (char *) ns->prefix);
720 else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000721 xmlDebugErr(ctxt, XML_CHECK_NO_HREF,
722 "Incomplete default namespace href=NULL\n");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000723 } else {
724 if (!ctxt->check) {
725 if (ns->prefix != NULL)
726 fprintf(ctxt->output, "namespace %s href=",
727 (char *) ns->prefix);
728 else
729 fprintf(ctxt->output, "default namespace href=");
730
731 xmlCtxtDumpString(ctxt, ns->href);
732 fprintf(ctxt->output, "\n");
733 }
734 }
735}
736
737static void
738xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
739{
740 while (ns != NULL) {
741 xmlCtxtDumpNamespace(ctxt, ns);
742 ns = ns->next;
743 }
744}
745
746static void
747xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
748{
749 xmlCtxtDumpSpaces(ctxt);
750
751 if (ent == NULL) {
752 if (!ctxt->check)
753 fprintf(ctxt->output, "Entity is NULL\n");
754 return;
755 }
756 if (!ctxt->check) {
757 switch (ent->etype) {
758 case XML_INTERNAL_GENERAL_ENTITY:
759 fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY ");
760 break;
761 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
762 fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
763 break;
764 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
765 fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
766 break;
767 case XML_INTERNAL_PARAMETER_ENTITY:
768 fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY ");
769 break;
770 case XML_EXTERNAL_PARAMETER_ENTITY:
771 fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY ");
772 break;
773 default:
774 fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype);
775 }
776 fprintf(ctxt->output, "%s\n", ent->name);
777 if (ent->ExternalID) {
778 xmlCtxtDumpSpaces(ctxt);
779 fprintf(ctxt->output, "ExternalID=%s\n",
780 (char *) ent->ExternalID);
781 }
782 if (ent->SystemID) {
783 xmlCtxtDumpSpaces(ctxt);
784 fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID);
785 }
786 if (ent->URI) {
787 xmlCtxtDumpSpaces(ctxt);
788 fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI);
789 }
790 if (ent->content) {
791 xmlCtxtDumpSpaces(ctxt);
792 fprintf(ctxt->output, "content=");
793 xmlCtxtDumpString(ctxt, ent->content);
794 fprintf(ctxt->output, "\n");
795 }
796 }
797}
798
799/**
800 * xmlCtxtDumpAttr:
801 * @output: the FILE * for the output
802 * @attr: the attribute
803 * @depth: the indentation level.
804 *
805 * Dumps debug information for the attribute
806 */
807static void
808xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
809{
810 xmlCtxtDumpSpaces(ctxt);
811
812 if (attr == NULL) {
813 if (!ctxt->check)
814 fprintf(ctxt->output, "Attr is NULL");
815 return;
816 }
817 if (!ctxt->check) {
818 fprintf(ctxt->output, "ATTRIBUTE ");
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000819 xmlCtxtDumpString(ctxt, attr->name);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000820 fprintf(ctxt->output, "\n");
821 if (attr->children != NULL) {
822 ctxt->depth++;
823 xmlCtxtDumpNodeList(ctxt, attr->children);
824 ctxt->depth--;
825 }
826 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000827 if (attr->name == NULL)
828 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
829 "Attribute has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000830
831 /*
832 * Do a bit of checking
833 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000834 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000835}
836
837/**
838 * xmlCtxtDumpAttrList:
839 * @output: the FILE * for the output
840 * @attr: the attribute list
841 * @depth: the indentation level.
842 *
843 * Dumps debug information for the attribute list
844 */
845static void
846xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
847{
848 while (attr != NULL) {
849 xmlCtxtDumpAttr(ctxt, attr);
850 attr = attr->next;
851 }
852}
853
854/**
855 * xmlCtxtDumpOneNode:
856 * @output: the FILE * for the output
857 * @node: the node
858 * @depth: the indentation level.
859 *
860 * Dumps debug information for the element node, it is not recursive
861 */
862static void
863xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
864{
865 if (node == NULL) {
866 if (!ctxt->check) {
867 xmlCtxtDumpSpaces(ctxt);
868 fprintf(ctxt->output, "node is NULL\n");
869 }
870 return;
871 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000872 ctxt->node = node;
873
Daniel Veillard22cdb842004-10-04 14:09:17 +0000874 switch (node->type) {
875 case XML_ELEMENT_NODE:
876 if (!ctxt->check) {
877 xmlCtxtDumpSpaces(ctxt);
878 fprintf(ctxt->output, "ELEMENT ");
879 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
880 xmlCtxtDumpString(ctxt, node->ns->prefix);
881 fprintf(ctxt->output, ":");
882 }
883 xmlCtxtDumpString(ctxt, node->name);
884 fprintf(ctxt->output, "\n");
885 }
886 break;
887 case XML_ATTRIBUTE_NODE:
888 if (!ctxt->check)
889 xmlCtxtDumpSpaces(ctxt);
890 fprintf(ctxt->output, "Error, ATTRIBUTE found here\n");
891 break;
892 case XML_TEXT_NODE:
893 if (!ctxt->check) {
894 xmlCtxtDumpSpaces(ctxt);
895 if (node->name == (const xmlChar *) xmlStringTextNoenc)
896 fprintf(ctxt->output, "TEXT no enc\n");
897 else
898 fprintf(ctxt->output, "TEXT\n");
899 }
900 break;
901 case XML_CDATA_SECTION_NODE:
902 if (!ctxt->check) {
903 xmlCtxtDumpSpaces(ctxt);
904 fprintf(ctxt->output, "CDATA_SECTION\n");
905 }
906 break;
907 case XML_ENTITY_REF_NODE:
908 if (!ctxt->check) {
909 xmlCtxtDumpSpaces(ctxt);
910 fprintf(ctxt->output, "ENTITY_REF(%s)\n",
911 (char *) node->name);
912 }
913 break;
914 case XML_ENTITY_NODE:
915 if (!ctxt->check) {
916 xmlCtxtDumpSpaces(ctxt);
917 fprintf(ctxt->output, "ENTITY\n");
918 }
919 break;
920 case XML_PI_NODE:
921 if (!ctxt->check) {
922 xmlCtxtDumpSpaces(ctxt);
923 fprintf(ctxt->output, "PI %s\n", (char *) node->name);
924 }
925 break;
926 case XML_COMMENT_NODE:
927 if (!ctxt->check) {
928 xmlCtxtDumpSpaces(ctxt);
929 fprintf(ctxt->output, "COMMENT\n");
930 }
931 break;
932 case XML_DOCUMENT_NODE:
933 case XML_HTML_DOCUMENT_NODE:
934 if (!ctxt->check) {
935 xmlCtxtDumpSpaces(ctxt);
936 }
937 fprintf(ctxt->output, "PBM: DOCUMENT found here\n");
938 break;
939 case XML_DOCUMENT_TYPE_NODE:
940 if (!ctxt->check) {
941 xmlCtxtDumpSpaces(ctxt);
942 fprintf(ctxt->output, "DOCUMENT_TYPE\n");
943 }
944 break;
945 case XML_DOCUMENT_FRAG_NODE:
946 if (!ctxt->check) {
947 xmlCtxtDumpSpaces(ctxt);
948 fprintf(ctxt->output, "DOCUMENT_FRAG\n");
949 }
950 break;
951 case XML_NOTATION_NODE:
952 if (!ctxt->check) {
953 xmlCtxtDumpSpaces(ctxt);
954 fprintf(ctxt->output, "NOTATION\n");
955 }
956 break;
957 case XML_DTD_NODE:
958 xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
959 return;
960 case XML_ELEMENT_DECL:
961 xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
962 return;
963 case XML_ATTRIBUTE_DECL:
964 xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
965 return;
966 case XML_ENTITY_DECL:
967 xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
968 return;
969 case XML_NAMESPACE_DECL:
970 xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
971 return;
972 case XML_XINCLUDE_START:
973 if (!ctxt->check) {
974 xmlCtxtDumpSpaces(ctxt);
975 fprintf(ctxt->output, "INCLUDE START\n");
976 }
977 return;
978 case XML_XINCLUDE_END:
979 if (!ctxt->check) {
980 xmlCtxtDumpSpaces(ctxt);
981 fprintf(ctxt->output, "INCLUDE END\n");
982 }
983 return;
984 default:
985 if (!ctxt->check)
986 xmlCtxtDumpSpaces(ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000987 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
988 "Unknown node type %d\n", node->type);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000989 return;
990 }
991 if (node->doc == NULL) {
992 if (!ctxt->check) {
993 xmlCtxtDumpSpaces(ctxt);
994 }
995 fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
996 }
997 ctxt->depth++;
998 if (node->nsDef != NULL)
999 xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
1000 if (node->properties != NULL)
1001 xmlCtxtDumpAttrList(ctxt, node->properties);
1002 if (node->type != XML_ENTITY_REF_NODE) {
1003 if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
1004 if (!ctxt->check) {
1005 xmlCtxtDumpSpaces(ctxt);
1006 fprintf(ctxt->output, "content=");
1007 xmlCtxtDumpString(ctxt, node->content);
1008 fprintf(ctxt->output, "\n");
1009 }
1010 }
1011 } else {
1012 xmlEntityPtr ent;
1013
1014 ent = xmlGetDocEntity(node->doc, node->name);
1015 if (ent != NULL)
1016 xmlCtxtDumpEntity(ctxt, ent);
1017 }
1018 ctxt->depth--;
1019
1020 /*
1021 * Do a bit of checking
1022 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001023 xmlCtxtGenericNodeCheck(ctxt, node);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001024}
1025
1026/**
1027 * xmlCtxtDumpNode:
1028 * @output: the FILE * for the output
1029 * @node: the node
1030 * @depth: the indentation level.
1031 *
1032 * Dumps debug information for the element node, it is recursive
1033 */
1034static void
1035xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1036{
1037 if (node == NULL) {
1038 if (!ctxt->check) {
1039 xmlCtxtDumpSpaces(ctxt);
1040 fprintf(ctxt->output, "node is NULL\n");
1041 }
1042 return;
1043 }
1044 xmlCtxtDumpOneNode(ctxt, node);
1045 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
1046 ctxt->depth++;
1047 xmlCtxtDumpNodeList(ctxt, node->children);
1048 ctxt->depth--;
1049 }
1050}
1051
1052/**
1053 * xmlCtxtDumpNodeList:
1054 * @output: the FILE * for the output
1055 * @node: the node list
1056 * @depth: the indentation level.
1057 *
1058 * Dumps debug information for the list of element node, it is recursive
1059 */
1060static void
1061xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1062{
1063 while (node != NULL) {
1064 xmlCtxtDumpNode(ctxt, node);
1065 node = node->next;
1066 }
1067}
1068
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001069static void
1070xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1071{
1072 if (doc == NULL) {
1073 if (!ctxt->check)
1074 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1075 return;
1076 }
1077 ctxt->node = (xmlNodePtr) doc;
1078
1079 switch (doc->type) {
1080 case XML_ELEMENT_NODE:
1081 xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
1082 "Misplaced ELEMENT node\n");
1083 break;
1084 case XML_ATTRIBUTE_NODE:
1085 xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
1086 "Misplaced ATTRIBUTE node\n");
1087 break;
1088 case XML_TEXT_NODE:
1089 xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
1090 "Misplaced TEXT node\n");
1091 break;
1092 case XML_CDATA_SECTION_NODE:
1093 xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
1094 "Misplaced CDATA node\n");
1095 break;
1096 case XML_ENTITY_REF_NODE:
1097 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
1098 "Misplaced ENTITYREF node\n");
1099 break;
1100 case XML_ENTITY_NODE:
1101 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
1102 "Misplaced ENTITY node\n");
1103 break;
1104 case XML_PI_NODE:
1105 xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
1106 "Misplaced PI node\n");
1107 break;
1108 case XML_COMMENT_NODE:
1109 xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
1110 "Misplaced COMMENT node\n");
1111 break;
1112 case XML_DOCUMENT_NODE:
1113 if (!ctxt->check)
1114 fprintf(ctxt->output, "DOCUMENT\n");
1115 break;
1116 case XML_HTML_DOCUMENT_NODE:
1117 if (!ctxt->check)
1118 fprintf(ctxt->output, "HTML DOCUMENT\n");
1119 break;
1120 case XML_DOCUMENT_TYPE_NODE:
1121 xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
1122 "Misplaced DOCTYPE node\n");
1123 break;
1124 case XML_DOCUMENT_FRAG_NODE:
1125 xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
1126 "Misplaced FRAGMENT node\n");
1127 break;
1128 case XML_NOTATION_NODE:
1129 xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
1130 "Misplaced NOTATION node\n");
1131 break;
1132 default:
1133 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1134 "Unknown node type %d\n", doc->type);
1135 }
1136}
Daniel Veillard22cdb842004-10-04 14:09:17 +00001137
1138/**
1139 * xmlCtxtDumpDocumentHead:
1140 * @output: the FILE * for the output
1141 * @doc: the document
1142 *
1143 * Dumps debug information cncerning the document, not recursive
1144 */
1145static void
1146xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1147{
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001148 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001149 if (!ctxt->check) {
1150 if (doc->name != NULL) {
1151 fprintf(ctxt->output, "name=");
1152 xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
1153 fprintf(ctxt->output, "\n");
1154 }
1155 if (doc->version != NULL) {
1156 fprintf(ctxt->output, "version=");
1157 xmlCtxtDumpString(ctxt, doc->version);
1158 fprintf(ctxt->output, "\n");
1159 }
1160 if (doc->encoding != NULL) {
1161 fprintf(ctxt->output, "encoding=");
1162 xmlCtxtDumpString(ctxt, doc->encoding);
1163 fprintf(ctxt->output, "\n");
1164 }
1165 if (doc->URL != NULL) {
1166 fprintf(ctxt->output, "URL=");
1167 xmlCtxtDumpString(ctxt, doc->URL);
1168 fprintf(ctxt->output, "\n");
1169 }
1170 if (doc->standalone)
1171 fprintf(ctxt->output, "standalone=true\n");
1172 }
1173 if (doc->oldNs != NULL)
1174 xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
1175}
1176
1177/**
1178 * xmlCtxtDumpDocument:
1179 * @output: the FILE * for the output
1180 * @doc: the document
1181 *
1182 * Dumps debug information for the document, it's recursive
1183 */
1184static void
1185xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1186{
1187 if (doc == NULL) {
1188 if (!ctxt->check)
1189 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1190 return;
1191 }
1192 xmlCtxtDumpDocumentHead(ctxt, doc);
1193 if (((doc->type == XML_DOCUMENT_NODE) ||
1194 (doc->type == XML_HTML_DOCUMENT_NODE))
1195 && (doc->children != NULL)) {
1196 ctxt->depth++;
1197 xmlCtxtDumpNodeList(ctxt, doc->children);
1198 ctxt->depth--;
1199 }
1200}
1201
1202static void
1203xmlCtxtDumpEntityCallback(xmlEntityPtr cur, xmlDebugCtxtPtr ctxt)
1204{
1205 if (cur == NULL) {
1206 if (!ctxt->check)
1207 fprintf(ctxt->output, "Entity is NULL");
1208 return;
1209 }
1210 if (!ctxt->check) {
1211 fprintf(ctxt->output, "%s : ", (char *) cur->name);
1212 switch (cur->etype) {
1213 case XML_INTERNAL_GENERAL_ENTITY:
1214 fprintf(ctxt->output, "INTERNAL GENERAL, ");
1215 break;
1216 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1217 fprintf(ctxt->output, "EXTERNAL PARSED, ");
1218 break;
1219 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1220 fprintf(ctxt->output, "EXTERNAL UNPARSED, ");
1221 break;
1222 case XML_INTERNAL_PARAMETER_ENTITY:
1223 fprintf(ctxt->output, "INTERNAL PARAMETER, ");
1224 break;
1225 case XML_EXTERNAL_PARAMETER_ENTITY:
1226 fprintf(ctxt->output, "EXTERNAL PARAMETER, ");
1227 break;
1228 default:
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001229 xmlDebugErr2(ctxt, XML_CHECK_ENTITY_TYPE,
1230 "Unknown entity type %d\n", cur->etype);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001231 }
1232 if (cur->ExternalID != NULL)
1233 fprintf(ctxt->output, "ID \"%s\"", (char *) cur->ExternalID);
1234 if (cur->SystemID != NULL)
1235 fprintf(ctxt->output, "SYSTEM \"%s\"", (char *) cur->SystemID);
1236 if (cur->orig != NULL)
1237 fprintf(ctxt->output, "\n orig \"%s\"", (char *) cur->orig);
1238 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL))
1239 fprintf(ctxt->output, "\n content \"%s\"",
1240 (char *) cur->content);
1241 fprintf(ctxt->output, "\n");
1242 }
1243}
1244
1245/**
1246 * xmlCtxtDumpEntities:
1247 * @output: the FILE * for the output
1248 * @doc: the document
1249 *
1250 * Dumps debug information for all the entities in use by the document
1251 */
1252static void
1253xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1254{
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001255 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001256 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
1257 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1258 doc->intSubset->entities;
1259
1260 if (!ctxt->check)
1261 fprintf(ctxt->output, "Entities in internal subset\n");
1262 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1263 ctxt);
1264 } else
1265 fprintf(ctxt->output, "No entities in internal subset\n");
1266 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
1267 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1268 doc->extSubset->entities;
1269
1270 if (!ctxt->check)
1271 fprintf(ctxt->output, "Entities in external subset\n");
1272 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1273 ctxt);
1274 } else if (!ctxt->check)
1275 fprintf(ctxt->output, "No entities in external subset\n");
1276}
1277
1278/**
1279 * xmlCtxtDumpDTD:
1280 * @output: the FILE * for the output
1281 * @dtd: the DTD
1282 *
1283 * Dumps debug information for the DTD
1284 */
1285static void
1286xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
1287{
1288 if (dtd == NULL) {
1289 if (!ctxt->check)
1290 fprintf(ctxt->output, "DTD is NULL\n");
1291 return;
1292 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001293 xmlCtxtDumpDtdNode(ctxt, dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001294 if (dtd->children == NULL)
1295 fprintf(ctxt->output, " DTD is empty\n");
1296 else {
1297 ctxt->depth++;
1298 xmlCtxtDumpNodeList(ctxt, dtd->children);
1299 ctxt->depth--;
1300 }
1301}
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001302
Daniel Veillard22cdb842004-10-04 14:09:17 +00001303/************************************************************************
1304 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001305 * Public entry points for dump *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001306 * *
1307 ************************************************************************/
1308
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001309/**
1310 * xmlDebugDumpString:
1311 * @output: the FILE * for the output
1312 * @str: the string
1313 *
1314 * Dumps informations about the string, shorten it if necessary
1315 */
1316void
1317xmlDebugDumpString(FILE * output, const xmlChar * str)
1318{
Owen Taylor3473f882001-02-23 17:55:21 +00001319 int i;
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001320
Daniel Veillard7db38712002-02-07 16:39:11 +00001321 if (output == NULL)
1322 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00001323 if (str == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001324 fprintf(output, "(NULL)");
1325 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001326 }
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001327 for (i = 0; i < 40; i++)
1328 if (str[i] == 0)
1329 return;
William M. Brack76e95df2003-10-18 16:20:14 +00001330 else if (IS_BLANK_CH(str[i]))
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001331 fputc(' ', output);
1332 else if (str[i] >= 0x80)
1333 fprintf(output, "#%X", str[i]);
1334 else
1335 fputc(str[i], output);
Owen Taylor3473f882001-02-23 17:55:21 +00001336 fprintf(output, "...");
1337}
1338
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001339/**
1340 * xmlDebugDumpAttr:
1341 * @output: the FILE * for the output
1342 * @attr: the attribute
1343 * @depth: the indentation level.
1344 *
1345 * Dumps debug information for the attribute
1346 */
1347void
1348xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
Daniel Veillard22cdb842004-10-04 14:09:17 +00001349 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001350
Daniel Veillard22cdb842004-10-04 14:09:17 +00001351 xmlCtxtDumpInitCtxt(&ctxt);
1352 ctxt.output = output;
1353 ctxt.depth = depth;
1354 xmlCtxtDumpAttr(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001355 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001356}
Owen Taylor3473f882001-02-23 17:55:21 +00001357
Owen Taylor3473f882001-02-23 17:55:21 +00001358
Daniel Veillard22cdb842004-10-04 14:09:17 +00001359/**
1360 * xmlDebugDumpEntities:
1361 * @output: the FILE * for the output
1362 * @doc: the document
1363 *
1364 * Dumps debug information for all the entities in use by the document
1365 */
1366void
1367xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
1368{
1369 xmlDebugCtxt ctxt;
1370
1371 xmlCtxtDumpInitCtxt(&ctxt);
1372 ctxt.output = output;
1373 xmlCtxtDumpEntities(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001374 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001375}
1376
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001377/**
1378 * xmlDebugDumpAttrList:
1379 * @output: the FILE * for the output
1380 * @attr: the attribute list
1381 * @depth: the indentation level.
1382 *
1383 * Dumps debug information for the attribute list
1384 */
1385void
1386xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
1387{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001388 xmlDebugCtxt ctxt;
1389
1390 xmlCtxtDumpInitCtxt(&ctxt);
1391 ctxt.output = output;
1392 ctxt.depth = depth;
1393 xmlCtxtDumpAttrList(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001394 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001395}
1396
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001397/**
1398 * xmlDebugDumpOneNode:
1399 * @output: the FILE * for the output
1400 * @node: the node
1401 * @depth: the indentation level.
1402 *
1403 * Dumps debug information for the element node, it is not recursive
1404 */
1405void
1406xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
1407{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001408 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001409
Daniel Veillard22cdb842004-10-04 14:09:17 +00001410 xmlCtxtDumpInitCtxt(&ctxt);
1411 ctxt.output = output;
1412 ctxt.depth = depth;
1413 xmlCtxtDumpOneNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001414 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001415}
1416
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001417/**
1418 * xmlDebugDumpNode:
1419 * @output: the FILE * for the output
1420 * @node: the node
1421 * @depth: the indentation level.
1422 *
1423 * Dumps debug information for the element node, it is recursive
1424 */
1425void
1426xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
1427{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001428 xmlDebugCtxt ctxt;
1429
Daniel Veillard7db38712002-02-07 16:39:11 +00001430 if (output == NULL)
1431 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001432 xmlCtxtDumpInitCtxt(&ctxt);
1433 ctxt.output = output;
1434 ctxt.depth = depth;
1435 xmlCtxtDumpNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001436 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001437}
1438
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001439/**
1440 * xmlDebugDumpNodeList:
1441 * @output: the FILE * for the output
1442 * @node: the node list
1443 * @depth: the indentation level.
1444 *
1445 * Dumps debug information for the list of element node, it is recursive
1446 */
1447void
1448xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
1449{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001450 xmlDebugCtxt ctxt;
1451
Daniel Veillard7db38712002-02-07 16:39:11 +00001452 if (output == NULL)
1453 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001454 xmlCtxtDumpInitCtxt(&ctxt);
1455 ctxt.output = output;
1456 ctxt.depth = depth;
1457 xmlCtxtDumpNodeList(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001458 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001459}
1460
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001461/**
1462 * xmlDebugDumpDocumentHead:
1463 * @output: the FILE * for the output
1464 * @doc: the document
1465 *
1466 * Dumps debug information cncerning the document, not recursive
1467 */
1468void
1469xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
1470{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001471 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001472
Daniel Veillard22cdb842004-10-04 14:09:17 +00001473 if (output == NULL)
1474 output = stdout;
1475 xmlCtxtDumpInitCtxt(&ctxt);
1476 ctxt.output = output;
1477 xmlCtxtDumpDocumentHead(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001478 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001479}
1480
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001481/**
1482 * xmlDebugDumpDocument:
1483 * @output: the FILE * for the output
1484 * @doc: the document
1485 *
1486 * Dumps debug information for the document, it's recursive
1487 */
1488void
1489xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
1490{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001491 xmlDebugCtxt ctxt;
1492
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001493 if (output == NULL)
Daniel Veillard22cdb842004-10-04 14:09:17 +00001494 output = stdout;
1495 xmlCtxtDumpInitCtxt(&ctxt);
1496 ctxt.output = output;
1497 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001498 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001499}
Owen Taylor3473f882001-02-23 17:55:21 +00001500
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001501/**
1502 * xmlDebugDumpDTD:
1503 * @output: the FILE * for the output
1504 * @dtd: the DTD
1505 *
1506 * Dumps debug information for the DTD
1507 */
1508void
1509xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
1510{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001511 xmlDebugCtxt ctxt;
1512
Daniel Veillard7db38712002-02-07 16:39:11 +00001513 if (output == NULL)
1514 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001515 xmlCtxtDumpInitCtxt(&ctxt);
1516 ctxt.output = output;
1517 xmlCtxtDumpDTD(&ctxt, dtd);
Daniel Veillard76821142004-10-09 20:39:04 +00001518 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001519}
1520
Daniel Veillard22cdb842004-10-04 14:09:17 +00001521/************************************************************************
1522 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001523 * Public entry points for checkings *
1524 * *
1525 ************************************************************************/
1526
1527/**
1528 * xmlDebugCheckDocument:
1529 * @output: the FILE * for the output
1530 * @doc: the document
1531 *
1532 * Check the document for potential content problems, and output
1533 * the errors to @output
1534 *
1535 * Returns the number of errors found
1536 */
1537int
1538xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
1539{
1540 xmlDebugCtxt ctxt;
1541
1542 if (output == NULL)
1543 output = stdout;
1544 xmlCtxtDumpInitCtxt(&ctxt);
1545 ctxt.output = output;
1546 ctxt.check = 1;
1547 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001548 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001549 return(ctxt.errors);
1550}
1551
1552/************************************************************************
1553 * *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001554 * Helpers for Shell *
1555 * *
1556 ************************************************************************/
Owen Taylor3473f882001-02-23 17:55:21 +00001557
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001558/**
1559 * xmlLsCountNode:
1560 * @node: the node to count
1561 *
1562 * Count the children of @node.
1563 *
1564 * Returns the number of children of @node.
1565 */
1566int
1567xmlLsCountNode(xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00001568 int ret = 0;
1569 xmlNodePtr list = NULL;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001570
1571 if (node == NULL)
1572 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001573
1574 switch (node->type) {
1575 case XML_ELEMENT_NODE:
1576 list = node->children;
1577 break;
1578 case XML_DOCUMENT_NODE:
1579 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00001580#ifdef LIBXML_DOCB_ENABLED
1581 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001582#endif
1583 list = ((xmlDocPtr) node)->children;
1584 break;
1585 case XML_ATTRIBUTE_NODE:
1586 list = ((xmlAttrPtr) node)->children;
1587 break;
1588 case XML_TEXT_NODE:
1589 case XML_CDATA_SECTION_NODE:
1590 case XML_PI_NODE:
1591 case XML_COMMENT_NODE:
1592 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001593 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001594 }
1595 break;
1596 case XML_ENTITY_REF_NODE:
1597 case XML_DOCUMENT_TYPE_NODE:
1598 case XML_ENTITY_NODE:
1599 case XML_DOCUMENT_FRAG_NODE:
1600 case XML_NOTATION_NODE:
1601 case XML_DTD_NODE:
1602 case XML_ELEMENT_DECL:
1603 case XML_ATTRIBUTE_DECL:
1604 case XML_ENTITY_DECL:
1605 case XML_NAMESPACE_DECL:
1606 case XML_XINCLUDE_START:
1607 case XML_XINCLUDE_END:
1608 ret = 1;
1609 break;
1610 }
1611 for (;list != NULL;ret++)
1612 list = list->next;
1613 return(ret);
1614}
1615
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001616/**
1617 * xmlLsOneNode:
1618 * @output: the FILE * for the output
1619 * @node: the node to dump
1620 *
1621 * Dump to @output the type and name of @node.
1622 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001623void
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001624xmlLsOneNode(FILE *output, xmlNodePtr node) {
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001625 if (node == NULL) {
1626 fprintf(output, "NULL\n");
1627 return;
1628 }
Owen Taylor3473f882001-02-23 17:55:21 +00001629 switch (node->type) {
1630 case XML_ELEMENT_NODE:
1631 fprintf(output, "-");
1632 break;
1633 case XML_ATTRIBUTE_NODE:
1634 fprintf(output, "a");
1635 break;
1636 case XML_TEXT_NODE:
1637 fprintf(output, "t");
1638 break;
1639 case XML_CDATA_SECTION_NODE:
Daniel Veillard75be0132002-03-13 10:03:35 +00001640 fprintf(output, "C");
Owen Taylor3473f882001-02-23 17:55:21 +00001641 break;
1642 case XML_ENTITY_REF_NODE:
1643 fprintf(output, "e");
1644 break;
1645 case XML_ENTITY_NODE:
1646 fprintf(output, "E");
1647 break;
1648 case XML_PI_NODE:
1649 fprintf(output, "p");
1650 break;
1651 case XML_COMMENT_NODE:
1652 fprintf(output, "c");
1653 break;
1654 case XML_DOCUMENT_NODE:
1655 fprintf(output, "d");
1656 break;
1657 case XML_HTML_DOCUMENT_NODE:
1658 fprintf(output, "h");
1659 break;
1660 case XML_DOCUMENT_TYPE_NODE:
1661 fprintf(output, "T");
1662 break;
1663 case XML_DOCUMENT_FRAG_NODE:
1664 fprintf(output, "F");
1665 break;
1666 case XML_NOTATION_NODE:
1667 fprintf(output, "N");
1668 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001669 case XML_NAMESPACE_DECL:
1670 fprintf(output, "n");
1671 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001672 default:
1673 fprintf(output, "?");
1674 }
Daniel Veillarde6a55192002-01-14 17:11:53 +00001675 if (node->type != XML_NAMESPACE_DECL) {
1676 if (node->properties != NULL)
1677 fprintf(output, "a");
1678 else
1679 fprintf(output, "-");
1680 if (node->nsDef != NULL)
1681 fprintf(output, "n");
1682 else
1683 fprintf(output, "-");
1684 }
Owen Taylor3473f882001-02-23 17:55:21 +00001685
1686 fprintf(output, " %8d ", xmlLsCountNode(node));
1687
1688 switch (node->type) {
1689 case XML_ELEMENT_NODE:
1690 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001691 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001692 break;
1693 case XML_ATTRIBUTE_NODE:
1694 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001695 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001696 break;
1697 case XML_TEXT_NODE:
1698 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001699 xmlDebugDumpString(output, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001700 }
1701 break;
1702 case XML_CDATA_SECTION_NODE:
1703 break;
1704 case XML_ENTITY_REF_NODE:
1705 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001706 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001707 break;
1708 case XML_ENTITY_NODE:
1709 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001710 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001711 break;
1712 case XML_PI_NODE:
1713 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001714 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001715 break;
1716 case XML_COMMENT_NODE:
1717 break;
1718 case XML_DOCUMENT_NODE:
1719 break;
1720 case XML_HTML_DOCUMENT_NODE:
1721 break;
1722 case XML_DOCUMENT_TYPE_NODE:
1723 break;
1724 case XML_DOCUMENT_FRAG_NODE:
1725 break;
1726 case XML_NOTATION_NODE:
1727 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001728 case XML_NAMESPACE_DECL: {
1729 xmlNsPtr ns = (xmlNsPtr) node;
1730
1731 if (ns->prefix == NULL)
William M. Brack13dfa872004-09-18 04:52:08 +00001732 fprintf(output, "default -> %s", (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001733 else
William M. Brack13dfa872004-09-18 04:52:08 +00001734 fprintf(output, "%s -> %s", (char *)ns->prefix,
1735 (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001736 break;
1737 }
Owen Taylor3473f882001-02-23 17:55:21 +00001738 default:
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 }
1742 fprintf(output, "\n");
1743}
1744
Daniel Veillard78d12092001-10-11 09:12:24 +00001745/**
1746 * xmlBoolToText:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001747 * @boolval: a bool to turn into text
Daniel Veillard78d12092001-10-11 09:12:24 +00001748 *
1749 * Convenient way to turn bool into text
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001750 *
1751 * Returns a pointer to either "True" or "False"
1752 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001753const char *
Daniel Veillardebd38c52001-11-01 08:38:12 +00001754xmlBoolToText(int boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001755{
Daniel Veillardebd38c52001-11-01 08:38:12 +00001756 if (boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001757 return("True");
1758 else
1759 return("False");
1760}
1761
Owen Taylor3473f882001-02-23 17:55:21 +00001762/****************************************************************
1763 * *
1764 * The XML shell related functions *
1765 * *
1766 ****************************************************************/
1767
Daniel Veillard78d12092001-10-11 09:12:24 +00001768
1769
Owen Taylor3473f882001-02-23 17:55:21 +00001770/*
1771 * TODO: Improvement/cleanups for the XML shell
1772 * - allow to shell out an editor on a subpart
1773 * - cleanup function registrations (with help) and calling
1774 * - provide registration routines
1775 */
1776
1777/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001778 * xmlShellPrintXPathError:
Daniel Veillard78d12092001-10-11 09:12:24 +00001779 * @errorType: valid xpath error id
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001780 * @arg: the argument that cause xpath to fail
Daniel Veillard78d12092001-10-11 09:12:24 +00001781 *
1782 * Print the xpath error to libxml default error channel
1783 */
1784void
1785xmlShellPrintXPathError(int errorType, const char *arg)
1786{
1787 const char *default_arg = "Result";
1788
1789 if (!arg)
1790 arg = default_arg;
1791
1792 switch (errorType) {
1793 case XPATH_UNDEFINED:
1794 xmlGenericError(xmlGenericErrorContext,
1795 "%s: no such node\n", arg);
1796 break;
1797
1798 case XPATH_BOOLEAN:
1799 xmlGenericError(xmlGenericErrorContext,
1800 "%s is a Boolean\n", arg);
1801 break;
1802 case XPATH_NUMBER:
1803 xmlGenericError(xmlGenericErrorContext,
1804 "%s is a number\n", arg);
1805 break;
1806 case XPATH_STRING:
1807 xmlGenericError(xmlGenericErrorContext,
1808 "%s is a string\n", arg);
1809 break;
1810 case XPATH_POINT:
1811 xmlGenericError(xmlGenericErrorContext,
1812 "%s is a point\n", arg);
1813 break;
1814 case XPATH_RANGE:
1815 xmlGenericError(xmlGenericErrorContext,
1816 "%s is a range\n", arg);
1817 break;
1818 case XPATH_LOCATIONSET:
1819 xmlGenericError(xmlGenericErrorContext,
1820 "%s is a range\n", arg);
1821 break;
1822 case XPATH_USERS:
1823 xmlGenericError(xmlGenericErrorContext,
1824 "%s is user-defined\n", arg);
1825 break;
1826 case XPATH_XSLT_TREE:
1827 xmlGenericError(xmlGenericErrorContext,
1828 "%s is an XSLT value tree\n", arg);
1829 break;
1830 }
1831 xmlGenericError(xmlGenericErrorContext,
1832 "Try casting the result string function (xpath builtin)\n",
1833 arg);
1834}
1835
1836
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001837#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001838/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001839 * xmlShellPrintNodeCtxt:
1840 * @ctxt : a non-null shell context
1841 * @node : a non-null node to print to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001842 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001843 * Print node to the output FILE
1844 */
1845static void
1846xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1847{
Daniel Veillard01992e02002-10-09 10:20:30 +00001848 FILE *fp;
1849
1850 if (!node)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001851 return;
Daniel Veillard01992e02002-10-09 10:20:30 +00001852 if (ctxt == NULL)
1853 fp = stdout;
1854 else
1855 fp = ctxt->output;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001856
1857 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001858 xmlDocDump(fp, (xmlDocPtr) node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001859 else if (node->type == XML_ATTRIBUTE_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001860 xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001861 else
Daniel Veillard01992e02002-10-09 10:20:30 +00001862 xmlElemDump(fp, node->doc, node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001863
Daniel Veillard01992e02002-10-09 10:20:30 +00001864 fprintf(fp, "\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00001865}
1866
1867/**
1868 * xmlShellPrintNode:
1869 * @node : a non-null node to print to the output FILE
1870 *
1871 * Print node to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001872 */
1873void
1874xmlShellPrintNode(xmlNodePtr node)
1875{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001876 xmlShellPrintNodeCtxt(NULL, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001877}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001878#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001879
Daniel Veillard78d12092001-10-11 09:12:24 +00001880/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001881 * xmlShellPrintXPathResultCtxt:
1882 * @ctxt: a valid shell context
Daniel Veillard9d06d302002-01-22 18:15:52 +00001883 * @list: a valid result generated by an xpath evaluation
Daniel Veillard78d12092001-10-11 09:12:24 +00001884 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001885 * Prints result to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001886 */
Daniel Veillard321be0c2002-10-08 21:26:42 +00001887static void
1888xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
Daniel Veillard78d12092001-10-11 09:12:24 +00001889{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001890 if (!ctxt)
1891 return;
Daniel Veillard78d12092001-10-11 09:12:24 +00001892
1893 if (list != NULL) {
1894 switch (list->type) {
1895 case XPATH_NODESET:{
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001896#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001897 int indx;
1898
1899 if (list->nodesetval) {
1900 for (indx = 0; indx < list->nodesetval->nodeNr;
1901 indx++) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001902 xmlShellPrintNodeCtxt(ctxt,
1903 list->nodesetval->nodeTab[indx]);
Daniel Veillard78d12092001-10-11 09:12:24 +00001904 }
1905 } else {
1906 xmlGenericError(xmlGenericErrorContext,
1907 "Empty node set\n");
1908 }
1909 break;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001910#else
1911 xmlGenericError(xmlGenericErrorContext,
1912 "Node set\n");
1913#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001914 }
1915 case XPATH_BOOLEAN:
1916 xmlGenericError(xmlGenericErrorContext,
1917 "Is a Boolean:%s\n",
1918 xmlBoolToText(list->boolval));
1919 break;
1920 case XPATH_NUMBER:
1921 xmlGenericError(xmlGenericErrorContext,
1922 "Is a number:%0g\n", list->floatval);
1923 break;
1924 case XPATH_STRING:
1925 xmlGenericError(xmlGenericErrorContext,
1926 "Is a string:%s\n", list->stringval);
1927 break;
1928
1929 default:
1930 xmlShellPrintXPathError(list->type, NULL);
1931 }
1932 }
1933}
1934
1935/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001936 * xmlShellPrintXPathResult:
1937 * @list: a valid result generated by an xpath evaluation
1938 *
1939 * Prints result to the output FILE
1940 */
1941void
1942xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1943{
1944 xmlShellPrintXPathResultCtxt(NULL, list);
1945}
1946
1947/**
Owen Taylor3473f882001-02-23 17:55:21 +00001948 * xmlShellList:
1949 * @ctxt: the shell context
1950 * @arg: unused
1951 * @node: a node
1952 * @node2: unused
1953 *
1954 * Implements the XML shell function "ls"
1955 * Does an Unix like listing of the given node (like a directory)
1956 *
1957 * Returns 0
1958 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001959int
Daniel Veillard321be0c2002-10-08 21:26:42 +00001960xmlShellList(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00001961 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1962 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1963{
Owen Taylor3473f882001-02-23 17:55:21 +00001964 xmlNodePtr cur;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001965 if (!ctxt)
1966 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001967 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001968 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001969 return (0);
1970 }
Owen Taylor3473f882001-02-23 17:55:21 +00001971 if ((node->type == XML_DOCUMENT_NODE) ||
1972 (node->type == XML_HTML_DOCUMENT_NODE)) {
1973 cur = ((xmlDocPtr) node)->children;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001974 } else if (node->type == XML_NAMESPACE_DECL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001975 xmlLsOneNode(ctxt->output, node);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001976 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001977 } else if (node->children != NULL) {
1978 cur = node->children;
1979 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001980 xmlLsOneNode(ctxt->output, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001981 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001982 }
1983 while (cur != NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001984 xmlLsOneNode(ctxt->output, cur);
Daniel Veillard78d12092001-10-11 09:12:24 +00001985 cur = cur->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001986 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001987 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001988}
1989
1990/**
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001991 * xmlShellBase:
1992 * @ctxt: the shell context
1993 * @arg: unused
1994 * @node: a node
1995 * @node2: unused
1996 *
1997 * Implements the XML shell function "base"
1998 * dumps the current XML base of the node
1999 *
2000 * Returns 0
2001 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002002int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002003xmlShellBase(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002004 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2005 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2006{
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002007 xmlChar *base;
Daniel Veillard321be0c2002-10-08 21:26:42 +00002008 if (!ctxt)
2009 return 0;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002010 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002011 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002012 return (0);
2013 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002014
2015 base = xmlNodeGetBase(node->doc, node);
2016
2017 if (base == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002018 fprintf(ctxt->output, " No base found !!!\n");
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002019 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002020 fprintf(ctxt->output, "%s\n", base);
Daniel Veillard78d12092001-10-11 09:12:24 +00002021 xmlFree(base);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002022 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002023 return (0);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002024}
2025
Daniel Veillardb34321c2004-03-04 17:09:47 +00002026#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002027/**
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002028 * xmlShellSetBase:
2029 * @ctxt: the shell context
2030 * @arg: the new base
2031 * @node: a node
2032 * @node2: unused
2033 *
2034 * Implements the XML shell function "setbase"
2035 * change the current XML base of the node
2036 *
2037 * Returns 0
2038 */
2039static int
2040xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2041 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2042 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2043{
2044 xmlNodeSetBase(node, (xmlChar*) arg);
2045 return (0);
2046}
Daniel Veillard2156d432004-03-04 15:59:36 +00002047#endif
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002048
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002049#ifdef LIBXML_XPATH_ENABLED
2050/**
2051 * xmlShellRegisterNamespace:
2052 * @ctxt: the shell context
2053 * @arg: a string in prefix=nsuri format
2054 * @node: unused
2055 * @node2: unused
2056 *
2057 * Implements the XML shell function "setns"
2058 * register/unregister a prefix=namespace pair
2059 * on the XPath context
2060 *
2061 * Returns 0 on success and a negative value otherwise.
2062 */
2063static int
2064xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
2065 xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2066{
2067 xmlChar* nsListDup;
2068 xmlChar* prefix;
2069 xmlChar* href;
2070 xmlChar* next;
2071
2072 nsListDup = xmlStrdup((xmlChar *) arg);
2073 next = nsListDup;
2074 while(next != NULL) {
2075 /* skip spaces */
2076 /*while((*next) == ' ') next++;*/
2077 if((*next) == '\0') break;
2078
2079 /* find prefix */
2080 prefix = next;
2081 next = (xmlChar*)xmlStrchr(next, '=');
2082 if(next == NULL) {
2083 fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
2084 xmlFree(nsListDup);
2085 return(-1);
2086 }
2087 *(next++) = '\0';
2088
2089 /* find href */
2090 href = next;
2091 next = (xmlChar*)xmlStrchr(next, ' ');
2092 if(next != NULL) {
2093 *(next++) = '\0';
2094 }
2095
2096 /* do register namespace */
2097 if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
2098 fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
2099 xmlFree(nsListDup);
2100 return(-1);
2101 }
2102 }
2103
2104 xmlFree(nsListDup);
2105 return(0);
2106}
2107#endif
2108
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002109/**
Daniel Veillard1e208222002-10-22 14:25:25 +00002110 * xmlShellGrep:
2111 * @ctxt: the shell context
2112 * @arg: the string or regular expression to find
2113 * @node: a node
2114 * @node2: unused
2115 *
2116 * Implements the XML shell function "grep"
2117 * dumps informations about the node (namespace, attributes, content).
2118 *
2119 * Returns 0
2120 */
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002121static int
Daniel Veillard1e208222002-10-22 14:25:25 +00002122xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2123 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2124{
2125 if (!ctxt)
2126 return (0);
2127 if (node == NULL)
2128 return (0);
2129 if (arg == NULL)
2130 return (0);
2131#ifdef LIBXML_REGEXP_ENABLED
2132 if ((xmlStrchr((xmlChar *) arg, '?')) ||
2133 (xmlStrchr((xmlChar *) arg, '*')) ||
2134 (xmlStrchr((xmlChar *) arg, '.')) ||
2135 (xmlStrchr((xmlChar *) arg, '['))) {
2136 }
2137#endif
2138 while (node != NULL) {
2139 if (node->type == XML_COMMENT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002140 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002141
2142 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
2143 xmlShellList(ctxt, NULL, node, NULL);
2144 }
2145 } else if (node->type == XML_TEXT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002146 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002147
2148 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002149 xmlShellList(ctxt, NULL, node->parent, NULL);
Daniel Veillard1e208222002-10-22 14:25:25 +00002150 }
2151 }
2152
2153 /*
2154 * Browse the full subtree, deep first
2155 */
2156
2157 if ((node->type == XML_DOCUMENT_NODE) ||
2158 (node->type == XML_HTML_DOCUMENT_NODE)) {
2159 node = ((xmlDocPtr) node)->children;
2160 } else if ((node->children != NULL)
2161 && (node->type != XML_ENTITY_REF_NODE)) {
2162 /* deep first */
2163 node = node->children;
2164 } else if (node->next != NULL) {
2165 /* then siblings */
2166 node = node->next;
2167 } else {
2168 /* go up to parents->next if needed */
2169 while (node != NULL) {
2170 if (node->parent != NULL) {
2171 node = node->parent;
2172 }
2173 if (node->next != NULL) {
2174 node = node->next;
2175 break;
2176 }
2177 if (node->parent == NULL) {
2178 node = NULL;
2179 break;
2180 }
2181 }
2182 }
2183 }
2184 return (0);
2185}
2186
2187/**
Owen Taylor3473f882001-02-23 17:55:21 +00002188 * xmlShellDir:
2189 * @ctxt: the shell context
2190 * @arg: unused
2191 * @node: a node
2192 * @node2: unused
2193 *
2194 * Implements the XML shell function "dir"
2195 * dumps informations about the node (namespace, attributes, content).
2196 *
2197 * Returns 0
2198 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002199int
2200xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2201 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2202 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2203{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002204 if (!ctxt)
2205 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002206 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002207 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002208 return (0);
2209 }
Owen Taylor3473f882001-02-23 17:55:21 +00002210 if ((node->type == XML_DOCUMENT_NODE) ||
2211 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002212 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
Owen Taylor3473f882001-02-23 17:55:21 +00002213 } else if (node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002214 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002215 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002216 xmlDebugDumpOneNode(ctxt->output, node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002217 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002218 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002219}
2220
Daniel Veillard29b17482004-08-16 00:39:03 +00002221/**
2222 * xmlShellSetContent:
2223 * @ctxt: the shell context
2224 * @value: the content as a string
2225 * @node: a node
2226 * @node2: unused
2227 *
2228 * Implements the XML shell function "dir"
2229 * dumps informations about the node (namespace, attributes, content).
2230 *
2231 * Returns 0
2232 */
2233static int
2234xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2235 char *value, xmlNodePtr node,
2236 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2237{
2238 xmlNodePtr results;
2239 xmlParserErrors ret;
2240
2241 if (!ctxt)
2242 return (0);
2243 if (node == NULL) {
2244 fprintf(ctxt->output, "NULL\n");
2245 return (0);
2246 }
2247 if (value == NULL) {
2248 fprintf(ctxt->output, "NULL\n");
2249 return (0);
2250 }
2251
2252 ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
2253 if (ret == XML_ERR_OK) {
2254 if (node->children != NULL) {
2255 xmlFreeNodeList(node->children);
2256 node->children = NULL;
2257 node->last = NULL;
2258 }
2259 xmlAddChildList(node, results);
2260 } else {
2261 fprintf(ctxt->output, "failed to parse content\n");
2262 }
2263 return (0);
2264}
2265
Daniel Veillard522bc602004-02-21 11:53:09 +00002266#ifdef LIBXML_SCHEMAS_ENABLED
2267/**
2268 * xmlShellRNGValidate:
2269 * @ctxt: the shell context
2270 * @schemas: the path to the Relax-NG schemas
2271 * @node: a node
2272 * @node2: unused
2273 *
2274 * Implements the XML shell function "relaxng"
2275 * validating the instance against a Relax-NG schemas
2276 *
2277 * Returns 0
2278 */
2279static int
2280xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
2281 xmlNodePtr node ATTRIBUTE_UNUSED,
2282 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2283{
2284 xmlRelaxNGPtr relaxngschemas;
2285 xmlRelaxNGParserCtxtPtr ctxt;
2286 xmlRelaxNGValidCtxtPtr vctxt;
2287 int ret;
2288
2289 ctxt = xmlRelaxNGNewParserCtxt(schemas);
2290 xmlRelaxNGSetParserErrors(ctxt,
2291 (xmlRelaxNGValidityErrorFunc) fprintf,
2292 (xmlRelaxNGValidityWarningFunc) fprintf,
2293 stderr);
2294 relaxngschemas = xmlRelaxNGParse(ctxt);
2295 xmlRelaxNGFreeParserCtxt(ctxt);
2296 if (relaxngschemas == NULL) {
2297 xmlGenericError(xmlGenericErrorContext,
2298 "Relax-NG schema %s failed to compile\n", schemas);
2299 return(-1);
2300 }
2301 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2302 xmlRelaxNGSetValidErrors(vctxt,
2303 (xmlRelaxNGValidityErrorFunc) fprintf,
2304 (xmlRelaxNGValidityWarningFunc) fprintf,
2305 stderr);
2306 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2307 if (ret == 0) {
2308 fprintf(stderr, "%s validates\n", sctxt->filename);
2309 } else if (ret > 0) {
2310 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2311 } else {
2312 fprintf(stderr, "%s validation generated an internal error\n",
2313 sctxt->filename);
2314 }
2315 xmlRelaxNGFreeValidCtxt(vctxt);
2316 if (relaxngschemas != NULL)
2317 xmlRelaxNGFree(relaxngschemas);
2318 return(0);
2319}
2320#endif
2321
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002322#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002323/**
2324 * xmlShellCat:
2325 * @ctxt: the shell context
2326 * @arg: unused
2327 * @node: a node
2328 * @node2: unused
2329 *
2330 * Implements the XML shell function "cat"
2331 * dumps the serialization node content (XML or HTML).
2332 *
2333 * Returns 0
2334 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002335int
2336xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2337 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2338{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002339 if (!ctxt)
2340 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002341 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002342 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002343 return (0);
2344 }
Owen Taylor3473f882001-02-23 17:55:21 +00002345 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2346#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002347 if (node->type == XML_HTML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002348 htmlDocDump(ctxt->output, (htmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002349 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002350 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002351#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002352 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002353 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002354 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002355 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002356#endif /* LIBXML_HTML_ENABLED */
2357 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00002358 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002359 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002360 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002361 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002362 }
Daniel Veillard321be0c2002-10-08 21:26:42 +00002363 fprintf(ctxt->output, "\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002364 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002365}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002366#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002367
2368/**
2369 * xmlShellLoad:
2370 * @ctxt: the shell context
2371 * @filename: the file name
2372 * @node: unused
2373 * @node2: unused
2374 *
2375 * Implements the XML shell function "load"
2376 * loads a new document specified by the filename
2377 *
2378 * Returns 0 or -1 if loading failed
2379 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002380int
2381xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2382 xmlNodePtr node ATTRIBUTE_UNUSED,
2383 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2384{
Owen Taylor3473f882001-02-23 17:55:21 +00002385 xmlDocPtr doc;
2386 int html = 0;
2387
2388 if (ctxt->doc != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002389 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00002390
2391 if (html) {
2392#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002393 doc = htmlParseFile(filename, NULL);
2394#else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002395 fprintf(ctxt->output, "HTML support not compiled in\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002396 doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002397#endif /* LIBXML_HTML_ENABLED */
2398 } else {
Daniel Veillardebe25d42004-03-25 09:35:49 +00002399 doc = xmlReadFile(filename,NULL,0);
Owen Taylor3473f882001-02-23 17:55:21 +00002400 }
2401 if (doc != NULL) {
2402 if (ctxt->loaded == 1) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002403 xmlFreeDoc(ctxt->doc);
2404 }
2405 ctxt->loaded = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002406#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002407 xmlXPathFreeContext(ctxt->pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00002408#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002409 xmlFree(ctxt->filename);
2410 ctxt->doc = doc;
2411 ctxt->node = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002412#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002413 ctxt->pctxt = xmlXPathNewContext(doc);
Owen Taylor3473f882001-02-23 17:55:21 +00002414#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard85095e22003-04-23 13:56:44 +00002415 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002416 } else
Daniel Veillard78d12092001-10-11 09:12:24 +00002417 return (-1);
2418 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002419}
2420
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002421#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002422/**
2423 * xmlShellWrite:
2424 * @ctxt: the shell context
2425 * @filename: the file name
2426 * @node: a node in the tree
2427 * @node2: unused
2428 *
2429 * Implements the XML shell function "write"
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002430 * Write the current node to the filename, it saves the serialization
Owen Taylor3473f882001-02-23 17:55:21 +00002431 * of the subtree under the @node specified
2432 *
2433 * Returns 0 or -1 in case of error
2434 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002435int
Owen Taylor3473f882001-02-23 17:55:21 +00002436xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Daniel Veillard78d12092001-10-11 09:12:24 +00002437 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2438{
Owen Taylor3473f882001-02-23 17:55:21 +00002439 if (node == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002440 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002441 if ((filename == NULL) || (filename[0] == 0)) {
2442 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002443 "Write command requires a filename argument\n");
2444 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002445 }
2446#ifdef W_OK
2447 if (access((char *) filename, W_OK)) {
2448 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002449 "Cannot write to %s\n", filename);
2450 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002451 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002452#endif
2453 switch (node->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002454 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002455 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2456 xmlGenericError(xmlGenericErrorContext,
2457 "Failed to write to %s\n", filename);
2458 return (-1);
2459 }
2460 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002461 case XML_HTML_DOCUMENT_NODE:
2462#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002463 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2464 xmlGenericError(xmlGenericErrorContext,
2465 "Failed to write to %s\n", filename);
2466 return (-1);
2467 }
Owen Taylor3473f882001-02-23 17:55:21 +00002468#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002469 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2470 xmlGenericError(xmlGenericErrorContext,
2471 "Failed to write to %s\n", filename);
2472 return (-1);
2473 }
Owen Taylor3473f882001-02-23 17:55:21 +00002474#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002475 break;
2476 default:{
2477 FILE *f;
Owen Taylor3473f882001-02-23 17:55:21 +00002478
Daniel Veillard78d12092001-10-11 09:12:24 +00002479 f = fopen((char *) filename, "w");
2480 if (f == NULL) {
2481 xmlGenericError(xmlGenericErrorContext,
2482 "Failed to write to %s\n", filename);
2483 return (-1);
2484 }
2485 xmlElemDump(f, ctxt->doc, node);
2486 fclose(f);
2487 }
Owen Taylor3473f882001-02-23 17:55:21 +00002488 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002489 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002490}
2491
2492/**
2493 * xmlShellSave:
2494 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002495 * @filename: the file name (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002496 * @node: unused
2497 * @node2: unused
2498 *
2499 * Implements the XML shell function "save"
2500 * Write the current document to the filename, or it's original name
2501 *
2502 * Returns 0 or -1 in case of error
2503 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002504int
2505xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2506 xmlNodePtr node ATTRIBUTE_UNUSED,
2507 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2508{
Owen Taylor3473f882001-02-23 17:55:21 +00002509 if (ctxt->doc == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002510 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002511 if ((filename == NULL) || (filename[0] == 0))
2512 filename = ctxt->filename;
2513#ifdef W_OK
2514 if (access((char *) filename, W_OK)) {
2515 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002516 "Cannot save to %s\n", filename);
2517 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002518 }
2519#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002520 switch (ctxt->doc->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) < 0) {
2523 xmlGenericError(xmlGenericErrorContext,
2524 "Failed to save to %s\n", filename);
2525 }
2526 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002527 case XML_HTML_DOCUMENT_NODE:
2528#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002529 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2530 xmlGenericError(xmlGenericErrorContext,
2531 "Failed to save to %s\n", filename);
2532 }
Owen Taylor3473f882001-02-23 17:55:21 +00002533#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002534 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2535 xmlGenericError(xmlGenericErrorContext,
2536 "Failed to save to %s\n", filename);
2537 }
Owen Taylor3473f882001-02-23 17:55:21 +00002538#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002539 break;
2540 default:
2541 xmlGenericError(xmlGenericErrorContext,
2542 "To save to subparts of a document use the 'write' command\n");
2543 return (-1);
2544
Owen Taylor3473f882001-02-23 17:55:21 +00002545 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002546 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002547}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002548#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002549
Daniel Veillardf54cd532004-02-25 11:52:31 +00002550#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002551/**
2552 * xmlShellValidate:
2553 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002554 * @dtd: the DTD URI (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002555 * @node: unused
2556 * @node2: unused
2557 *
2558 * Implements the XML shell function "validate"
2559 * Validate the document, if a DTD path is provided, then the validation
2560 * is done against the given DTD.
2561 *
2562 * Returns 0 or -1 in case of error
2563 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002564int
2565xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2566 xmlNodePtr node ATTRIBUTE_UNUSED,
2567 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2568{
Owen Taylor3473f882001-02-23 17:55:21 +00002569 xmlValidCtxt vctxt;
2570 int res = -1;
2571
2572 vctxt.userData = stderr;
2573 vctxt.error = (xmlValidityErrorFunc) fprintf;
2574 vctxt.warning = (xmlValidityWarningFunc) fprintf;
2575
2576 if ((dtd == NULL) || (dtd[0] == 0)) {
2577 res = xmlValidateDocument(&vctxt, ctxt->doc);
2578 } else {
2579 xmlDtdPtr subset;
2580
Daniel Veillard78d12092001-10-11 09:12:24 +00002581 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2582 if (subset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002583 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2584
Daniel Veillard78d12092001-10-11 09:12:24 +00002585 xmlFreeDtd(subset);
2586 }
Owen Taylor3473f882001-02-23 17:55:21 +00002587 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002588 return (res);
Owen Taylor3473f882001-02-23 17:55:21 +00002589}
Daniel Veillardf54cd532004-02-25 11:52:31 +00002590#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002591
2592/**
2593 * xmlShellDu:
2594 * @ctxt: the shell context
2595 * @arg: unused
2596 * @tree: a node defining a subtree
2597 * @node2: unused
2598 *
2599 * Implements the XML shell function "du"
2600 * show the structure of the subtree under node @tree
2601 * If @tree is null, the command works on the current node.
2602 *
2603 * Returns 0 or -1 in case of error
2604 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002605int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002606xmlShellDu(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002607 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2608 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2609{
Owen Taylor3473f882001-02-23 17:55:21 +00002610 xmlNodePtr node;
Daniel Veillard78d12092001-10-11 09:12:24 +00002611 int indent = 0, i;
Owen Taylor3473f882001-02-23 17:55:21 +00002612
Daniel Veillard321be0c2002-10-08 21:26:42 +00002613 if (!ctxt)
2614 return (-1);
2615
Daniel Veillard78d12092001-10-11 09:12:24 +00002616 if (tree == NULL)
2617 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002618 node = tree;
2619 while (node != NULL) {
2620 if ((node->type == XML_DOCUMENT_NODE) ||
2621 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002622 fprintf(ctxt->output, "/\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002623 } else if (node->type == XML_ELEMENT_NODE) {
2624 for (i = 0; i < indent; i++)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002625 fprintf(ctxt->output, " ");
2626 fprintf(ctxt->output, "%s\n", node->name);
Daniel Veillard78d12092001-10-11 09:12:24 +00002627 } else {
2628 }
Owen Taylor3473f882001-02-23 17:55:21 +00002629
Daniel Veillard78d12092001-10-11 09:12:24 +00002630 /*
2631 * Browse the full subtree, deep first
2632 */
Owen Taylor3473f882001-02-23 17:55:21 +00002633
2634 if ((node->type == XML_DOCUMENT_NODE) ||
2635 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002636 node = ((xmlDocPtr) node)->children;
2637 } else if ((node->children != NULL)
2638 && (node->type != XML_ENTITY_REF_NODE)) {
2639 /* deep first */
2640 node = node->children;
2641 indent++;
2642 } else if ((node != tree) && (node->next != NULL)) {
2643 /* then siblings */
2644 node = node->next;
2645 } else if (node != tree) {
2646 /* go up to parents->next if needed */
2647 while (node != tree) {
2648 if (node->parent != NULL) {
2649 node = node->parent;
2650 indent--;
2651 }
2652 if ((node != tree) && (node->next != NULL)) {
2653 node = node->next;
2654 break;
2655 }
2656 if (node->parent == NULL) {
2657 node = NULL;
2658 break;
2659 }
2660 if (node == tree) {
2661 node = NULL;
2662 break;
2663 }
2664 }
2665 /* exit condition */
2666 if (node == tree)
2667 node = NULL;
2668 } else
2669 node = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002670 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002671 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002672}
2673
2674/**
2675 * xmlShellPwd:
2676 * @ctxt: the shell context
2677 * @buffer: the output buffer
Daniel Veillard9d06d302002-01-22 18:15:52 +00002678 * @node: a node
Owen Taylor3473f882001-02-23 17:55:21 +00002679 * @node2: unused
2680 *
2681 * Implements the XML shell function "pwd"
2682 * Show the full path from the root to the node, if needed building
2683 * thumblers when similar elements exists at a given ancestor level.
2684 * The output is compatible with XPath commands.
2685 *
2686 * Returns 0 or -1 in case of error
2687 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002688int
2689xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2690 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2691{
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002692 xmlChar *path;
Owen Taylor3473f882001-02-23 17:55:21 +00002693
Daniel Veillard78d12092001-10-11 09:12:24 +00002694 if (node == NULL)
2695 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002696
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002697 path = xmlGetNodePath(node);
2698 if (path == NULL)
2699 return (-1);
2700
2701 /*
2702 * This test prevents buffer overflow, because this routine
2703 * is only called by xmlShell, in which the second argument is
2704 * 500 chars long.
2705 * It is a dirty hack before a cleaner solution is found.
2706 * Documentation should mention that the second argument must
2707 * be at least 500 chars long, and could be stripped if too long.
2708 */
2709 snprintf(buffer, 499, "%s", path);
2710 buffer[499] = '0';
2711 xmlFree(path);
2712
Daniel Veillard78d12092001-10-11 09:12:24 +00002713 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002714}
2715
2716/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002717 * xmlShell:
Owen Taylor3473f882001-02-23 17:55:21 +00002718 * @doc: the initial document
2719 * @filename: the output buffer
2720 * @input: the line reading function
Daniel Veillard321be0c2002-10-08 21:26:42 +00002721 * @output: the output FILE*, defaults to stdout if NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002722 *
2723 * Implements the XML shell
2724 * This allow to load, validate, view, modify and save a document
2725 * using a environment similar to a UNIX commandline.
2726 */
2727void
2728xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Daniel Veillard78d12092001-10-11 09:12:24 +00002729 FILE * output)
2730{
Owen Taylor3473f882001-02-23 17:55:21 +00002731 char prompt[500] = "/ > ";
2732 char *cmdline = NULL, *cur;
2733 int nbargs;
2734 char command[100];
2735 char arg[400];
2736 int i;
2737 xmlShellCtxtPtr ctxt;
2738 xmlXPathObjectPtr list;
2739
2740 if (doc == NULL)
2741 return;
2742 if (filename == NULL)
2743 return;
2744 if (input == NULL)
2745 return;
2746 if (output == NULL)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002747 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00002748 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Daniel Veillard78d12092001-10-11 09:12:24 +00002749 if (ctxt == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00002750 return;
2751 ctxt->loaded = 0;
2752 ctxt->doc = doc;
2753 ctxt->input = input;
2754 ctxt->output = output;
2755 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Daniel Veillard78d12092001-10-11 09:12:24 +00002756 ctxt->node = (xmlNodePtr) ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002757
2758#ifdef LIBXML_XPATH_ENABLED
2759 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2760 if (ctxt->pctxt == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002761 xmlFree(ctxt);
2762 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002763 }
2764#endif /* LIBXML_XPATH_ENABLED */
2765 while (1) {
2766 if (ctxt->node == (xmlNodePtr) ctxt->doc)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002767 snprintf(prompt, sizeof(prompt), "%s > ", "/");
Daniel Veillard7a985a12003-07-06 17:57:42 +00002768 else if ((ctxt->node != NULL) && (ctxt->node->name))
Daniel Veillard78d12092001-10-11 09:12:24 +00002769 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002770 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002771 snprintf(prompt, sizeof(prompt), "? > ");
Owen Taylor3473f882001-02-23 17:55:21 +00002772 prompt[sizeof(prompt) - 1] = 0;
2773
Daniel Veillard78d12092001-10-11 09:12:24 +00002774 /*
2775 * Get a new command line
2776 */
Owen Taylor3473f882001-02-23 17:55:21 +00002777 cmdline = ctxt->input(prompt);
Daniel Veillard78d12092001-10-11 09:12:24 +00002778 if (cmdline == NULL)
2779 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002780
Daniel Veillard78d12092001-10-11 09:12:24 +00002781 /*
2782 * Parse the command itself
2783 */
2784 cur = cmdline;
2785 nbargs = 0;
2786 while ((*cur == ' ') || (*cur == '\t'))
2787 cur++;
2788 i = 0;
2789 while ((*cur != ' ') && (*cur != '\t') &&
2790 (*cur != '\n') && (*cur != '\r')) {
2791 if (*cur == 0)
2792 break;
2793 command[i++] = *cur++;
2794 }
2795 command[i] = 0;
2796 if (i == 0)
2797 continue;
2798 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002799
Daniel Veillard78d12092001-10-11 09:12:24 +00002800 /*
2801 * Parse the argument
2802 */
2803 while ((*cur == ' ') || (*cur == '\t'))
2804 cur++;
2805 i = 0;
2806 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2807 if (*cur == 0)
2808 break;
2809 arg[i++] = *cur++;
2810 }
2811 arg[i] = 0;
2812 if (i != 0)
2813 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002814
Daniel Veillard78d12092001-10-11 09:12:24 +00002815 /*
2816 * start interpreting the command
2817 */
Owen Taylor3473f882001-02-23 17:55:21 +00002818 if (!strcmp(command, "exit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002819 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002820 if (!strcmp(command, "quit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002821 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002822 if (!strcmp(command, "bye"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002823 break;
Daniel Veillard5004f422001-11-08 13:53:05 +00002824 if (!strcmp(command, "help")) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002825 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2826 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2827 fprintf(ctxt->output, "\tbye leave shell\n");
2828 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2829 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2830 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2831 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2832 fprintf(ctxt->output, "\texit leave shell\n");
2833 fprintf(ctxt->output, "\thelp display this help\n");
2834 fprintf(ctxt->output, "\tfree display memory usage\n");
2835 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2836 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00002837 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 +00002838#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002839 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002840 fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2841 fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002842#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard321be0c2002-10-08 21:26:42 +00002843 fprintf(ctxt->output, "\tpwd display current working directory\n");
2844 fprintf(ctxt->output, "\tquit leave shell\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002845#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002846 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00002847 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002848#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf54cd532004-02-25 11:52:31 +00002849#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002850 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002851#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard522bc602004-02-21 11:53:09 +00002852#ifdef LIBXML_SCHEMAS_ENABLED
2853 fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n");
2854#endif
Daniel Veillard1e208222002-10-22 14:25:25 +00002855 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002856#ifdef LIBXML_VALID_ENABLED
Daniel Veillard5004f422001-11-08 13:53:05 +00002857 } else if (!strcmp(command, "validate")) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002858 xmlShellValidate(ctxt, arg, NULL, NULL);
Daniel Veillardf54cd532004-02-25 11:52:31 +00002859#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002860 } else if (!strcmp(command, "load")) {
2861 xmlShellLoad(ctxt, arg, NULL, NULL);
Daniel Veillard522bc602004-02-21 11:53:09 +00002862#ifdef LIBXML_SCHEMAS_ENABLED
2863 } else if (!strcmp(command, "relaxng")) {
2864 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2865#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002866#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002867 } else if (!strcmp(command, "save")) {
2868 xmlShellSave(ctxt, arg, NULL, NULL);
2869 } else if (!strcmp(command, "write")) {
2870 xmlShellWrite(ctxt, arg, NULL, NULL);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002871#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard1e208222002-10-22 14:25:25 +00002872 } else if (!strcmp(command, "grep")) {
2873 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
Daniel Veillard78d12092001-10-11 09:12:24 +00002874 } else if (!strcmp(command, "free")) {
2875 if (arg[0] == 0) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002876 xmlMemShow(ctxt->output, 0);
Daniel Veillard78d12092001-10-11 09:12:24 +00002877 } else {
2878 int len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002879
Daniel Veillard78d12092001-10-11 09:12:24 +00002880 sscanf(arg, "%d", &len);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002881 xmlMemShow(ctxt->output, len);
Daniel Veillard78d12092001-10-11 09:12:24 +00002882 }
2883 } else if (!strcmp(command, "pwd")) {
2884 char dir[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002885
Daniel Veillard78d12092001-10-11 09:12:24 +00002886 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
Daniel Veillard321be0c2002-10-08 21:26:42 +00002887 fprintf(ctxt->output, "%s\n", dir);
Daniel Veillard78d12092001-10-11 09:12:24 +00002888 } else if (!strcmp(command, "du")) {
2889 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2890 } else if (!strcmp(command, "base")) {
2891 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
Daniel Veillard29b17482004-08-16 00:39:03 +00002892 } else if (!strcmp(command, "set")) {
2893 xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002894#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002895 } else if (!strcmp(command, "setns")) {
2896 if (arg[0] == 0) {
2897 xmlGenericError(xmlGenericErrorContext,
2898 "setns: prefix=[nsuri] required\n");
2899 } else {
2900 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
2901 }
Daniel Veillard2070c482002-01-22 22:12:19 +00002902 } else if (!strcmp(command, "xpath")) {
2903 if (arg[0] == 0) {
2904 xmlGenericError(xmlGenericErrorContext,
2905 "xpath: expression required\n");
2906 } else {
2907 ctxt->pctxt->node = ctxt->node;
2908 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002909 xmlXPathDebugDumpObject(ctxt->output, list, 0);
Daniel Veillard2070c482002-01-22 22:12:19 +00002910 xmlXPathFreeObject(list);
2911 }
2912#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard2156d432004-03-04 15:59:36 +00002913#ifdef LIBXML_TREE_ENABLED
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002914 } else if (!strcmp(command, "setbase")) {
2915 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2156d432004-03-04 15:59:36 +00002916#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002917 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
2918 int dir = (!strcmp(command, "dir"));
2919
2920 if (arg[0] == 0) {
2921 if (dir)
2922 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
2923 else
2924 xmlShellList(ctxt, NULL, ctxt->node, NULL);
2925 } else {
2926 ctxt->pctxt->node = ctxt->node;
Daniel Veillard61d80a22001-04-27 17:13:01 +00002927#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002928 ctxt->pctxt->node = ctxt->node;
2929 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2930#else
2931 list = NULL;
2932#endif /* LIBXML_XPATH_ENABLED */
2933 if (list != NULL) {
2934 switch (list->type) {
2935 case XPATH_UNDEFINED:
2936 xmlGenericError(xmlGenericErrorContext,
2937 "%s: no such node\n", arg);
2938 break;
2939 case XPATH_NODESET:{
2940 int indx;
2941
Daniel Veillarda6825e82001-11-07 13:33:59 +00002942 if (list->nodesetval == NULL)
2943 break;
2944
Daniel Veillard78d12092001-10-11 09:12:24 +00002945 for (indx = 0;
2946 indx < list->nodesetval->nodeNr;
2947 indx++) {
2948 if (dir)
2949 xmlShellDir(ctxt, NULL,
2950 list->nodesetval->
2951 nodeTab[indx], NULL);
2952 else
2953 xmlShellList(ctxt, NULL,
2954 list->nodesetval->
2955 nodeTab[indx], NULL);
2956 }
2957 break;
2958 }
2959 case XPATH_BOOLEAN:
2960 xmlGenericError(xmlGenericErrorContext,
2961 "%s is a Boolean\n", arg);
2962 break;
2963 case XPATH_NUMBER:
2964 xmlGenericError(xmlGenericErrorContext,
2965 "%s is a number\n", arg);
2966 break;
2967 case XPATH_STRING:
2968 xmlGenericError(xmlGenericErrorContext,
2969 "%s is a string\n", arg);
2970 break;
2971 case XPATH_POINT:
2972 xmlGenericError(xmlGenericErrorContext,
2973 "%s is a point\n", arg);
2974 break;
2975 case XPATH_RANGE:
2976 xmlGenericError(xmlGenericErrorContext,
2977 "%s is a range\n", arg);
2978 break;
2979 case XPATH_LOCATIONSET:
2980 xmlGenericError(xmlGenericErrorContext,
2981 "%s is a range\n", arg);
2982 break;
2983 case XPATH_USERS:
2984 xmlGenericError(xmlGenericErrorContext,
2985 "%s is user-defined\n", arg);
2986 break;
2987 case XPATH_XSLT_TREE:
2988 xmlGenericError(xmlGenericErrorContext,
2989 "%s is an XSLT value tree\n",
2990 arg);
2991 break;
2992 }
2993#ifdef LIBXML_XPATH_ENABLED
2994 xmlXPathFreeObject(list);
Daniel Veillard61d80a22001-04-27 17:13:01 +00002995#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002996 } else {
2997 xmlGenericError(xmlGenericErrorContext,
2998 "%s: no such node\n", arg);
2999 }
3000 ctxt->pctxt->node = NULL;
3001 }
3002 } else if (!strcmp(command, "cd")) {
3003 if (arg[0] == 0) {
3004 ctxt->node = (xmlNodePtr) ctxt->doc;
3005 } else {
3006#ifdef LIBXML_XPATH_ENABLED
3007 ctxt->pctxt->node = ctxt->node;
3008 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3009#else
3010 list = NULL;
3011#endif /* LIBXML_XPATH_ENABLED */
3012 if (list != NULL) {
3013 switch (list->type) {
3014 case XPATH_UNDEFINED:
3015 xmlGenericError(xmlGenericErrorContext,
3016 "%s: no such node\n", arg);
3017 break;
3018 case XPATH_NODESET:
Daniel Veillarda6825e82001-11-07 13:33:59 +00003019 if (list->nodesetval != NULL) {
3020 if (list->nodesetval->nodeNr == 1) {
3021 ctxt->node = list->nodesetval->nodeTab[0];
Daniel Veillard7a985a12003-07-06 17:57:42 +00003022 if ((ctxt->node != NULL) &&
3023 (ctxt->node->type ==
3024 XML_NAMESPACE_DECL)) {
3025 xmlGenericError(xmlGenericErrorContext,
3026 "cannot cd to namespace\n");
3027 ctxt->node = NULL;
3028 }
Daniel Veillarda6825e82001-11-07 13:33:59 +00003029 } else
3030 xmlGenericError(xmlGenericErrorContext,
3031 "%s is a %d Node Set\n",
3032 arg,
3033 list->nodesetval->nodeNr);
Daniel Veillard78d12092001-10-11 09:12:24 +00003034 } else
3035 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda6825e82001-11-07 13:33:59 +00003036 "%s is an empty Node Set\n",
3037 arg);
Daniel Veillard78d12092001-10-11 09:12:24 +00003038 break;
3039 case XPATH_BOOLEAN:
3040 xmlGenericError(xmlGenericErrorContext,
3041 "%s is a Boolean\n", arg);
3042 break;
3043 case XPATH_NUMBER:
3044 xmlGenericError(xmlGenericErrorContext,
3045 "%s is a number\n", arg);
3046 break;
3047 case XPATH_STRING:
3048 xmlGenericError(xmlGenericErrorContext,
3049 "%s is a string\n", arg);
3050 break;
3051 case XPATH_POINT:
3052 xmlGenericError(xmlGenericErrorContext,
3053 "%s is a point\n", arg);
3054 break;
3055 case XPATH_RANGE:
3056 xmlGenericError(xmlGenericErrorContext,
3057 "%s is a range\n", arg);
3058 break;
3059 case XPATH_LOCATIONSET:
3060 xmlGenericError(xmlGenericErrorContext,
3061 "%s is a range\n", arg);
3062 break;
3063 case XPATH_USERS:
3064 xmlGenericError(xmlGenericErrorContext,
3065 "%s is user-defined\n", arg);
3066 break;
3067 case XPATH_XSLT_TREE:
3068 xmlGenericError(xmlGenericErrorContext,
3069 "%s is an XSLT value tree\n",
3070 arg);
3071 break;
3072 }
3073#ifdef LIBXML_XPATH_ENABLED
3074 xmlXPathFreeObject(list);
3075#endif
3076 } else {
3077 xmlGenericError(xmlGenericErrorContext,
3078 "%s: no such node\n", arg);
3079 }
3080 ctxt->pctxt->node = NULL;
3081 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003082#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00003083 } else if (!strcmp(command, "cat")) {
3084 if (arg[0] == 0) {
3085 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3086 } else {
3087 ctxt->pctxt->node = ctxt->node;
3088#ifdef LIBXML_XPATH_ENABLED
3089 ctxt->pctxt->node = ctxt->node;
3090 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3091#else
3092 list = NULL;
3093#endif /* LIBXML_XPATH_ENABLED */
3094 if (list != NULL) {
3095 switch (list->type) {
3096 case XPATH_UNDEFINED:
3097 xmlGenericError(xmlGenericErrorContext,
3098 "%s: no such node\n", arg);
3099 break;
3100 case XPATH_NODESET:{
3101 int indx;
3102
Daniel Veillarda6825e82001-11-07 13:33:59 +00003103 if (list->nodesetval == NULL)
3104 break;
3105
Daniel Veillard78d12092001-10-11 09:12:24 +00003106 for (indx = 0;
3107 indx < list->nodesetval->nodeNr;
3108 indx++) {
3109 if (i > 0)
Daniel Veillard321be0c2002-10-08 21:26:42 +00003110 fprintf(ctxt->output, " -------\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00003111 xmlShellCat(ctxt, NULL,
3112 list->nodesetval->
3113 nodeTab[indx], NULL);
3114 }
3115 break;
3116 }
3117 case XPATH_BOOLEAN:
3118 xmlGenericError(xmlGenericErrorContext,
3119 "%s is a Boolean\n", arg);
3120 break;
3121 case XPATH_NUMBER:
3122 xmlGenericError(xmlGenericErrorContext,
3123 "%s is a number\n", arg);
3124 break;
3125 case XPATH_STRING:
3126 xmlGenericError(xmlGenericErrorContext,
3127 "%s is a string\n", arg);
3128 break;
3129 case XPATH_POINT:
3130 xmlGenericError(xmlGenericErrorContext,
3131 "%s is a point\n", arg);
3132 break;
3133 case XPATH_RANGE:
3134 xmlGenericError(xmlGenericErrorContext,
3135 "%s is a range\n", arg);
3136 break;
3137 case XPATH_LOCATIONSET:
3138 xmlGenericError(xmlGenericErrorContext,
3139 "%s is a range\n", arg);
3140 break;
3141 case XPATH_USERS:
3142 xmlGenericError(xmlGenericErrorContext,
3143 "%s is user-defined\n", arg);
3144 break;
3145 case XPATH_XSLT_TREE:
3146 xmlGenericError(xmlGenericErrorContext,
3147 "%s is an XSLT value tree\n",
3148 arg);
3149 break;
3150 }
3151#ifdef LIBXML_XPATH_ENABLED
3152 xmlXPathFreeObject(list);
3153#endif
3154 } else {
3155 xmlGenericError(xmlGenericErrorContext,
3156 "%s: no such node\n", arg);
3157 }
3158 ctxt->pctxt->node = NULL;
3159 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003160#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00003161 } else {
3162 xmlGenericError(xmlGenericErrorContext,
3163 "Unknown command %s\n", command);
3164 }
3165 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003166 }
3167#ifdef LIBXML_XPATH_ENABLED
3168 xmlXPathFreeContext(ctxt->pctxt);
3169#endif /* LIBXML_XPATH_ENABLED */
3170 if (ctxt->loaded) {
3171 xmlFreeDoc(ctxt->doc);
3172 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003173 if (ctxt->filename != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003174 xmlFree(ctxt->filename);
Owen Taylor3473f882001-02-23 17:55:21 +00003175 xmlFree(ctxt);
3176 if (cmdline != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003177 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003178}
3179
3180#endif /* LIBXML_DEBUG_ENABLED */