blob: 5ab0836d54785199b8525dae96f7695b69185a48 [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 Veillard6927b102004-10-27 17:29:04 +000065 ctxt->nodict = 0;
Daniel Veillard22cdb842004-10-04 14:09:17 +000066 for (i = 0; i < 100; i++)
67 ctxt->shift[i] = ' ';
68 ctxt->shift[100] = 0;
69}
70
71static void
William M. Brack9638d4c2004-10-15 18:25:33 +000072xmlCtxtDumpCleanCtxt(xmlDebugCtxtPtr ctxt ATTRIBUTE_UNUSED)
Daniel Veillard76821142004-10-09 20:39:04 +000073{
William M. Brack9638d4c2004-10-15 18:25:33 +000074 /* remove the ATTRIBUTE_UNUSED when this is added */
Daniel Veillard76821142004-10-09 20:39:04 +000075}
76
77/**
Daniel Veillard0d24b112004-10-11 12:28:34 +000078 * xmlNsCheckScope:
79 * @node: the node
80 * @ns: the namespace node
Daniel Veillard76821142004-10-09 20:39:04 +000081 *
Daniel Veillard0d24b112004-10-11 12:28:34 +000082 * Check that a given namespace is in scope on a node.
Daniel Veillard76821142004-10-09 20:39:04 +000083 *
Daniel Veillard0d24b112004-10-11 12:28:34 +000084 * Returns 1 if in scope, -1 in case of argument error,
85 * -2 if the namespace is not in scope, and -3 if not on
86 * an ancestor node.
Daniel Veillard76821142004-10-09 20:39:04 +000087 */
88static int
Daniel Veillard0d24b112004-10-11 12:28:34 +000089xmlNsCheckScope(xmlNodePtr node, xmlNsPtr ns)
Daniel Veillard76821142004-10-09 20:39:04 +000090{
Daniel Veillard0d24b112004-10-11 12:28:34 +000091 xmlNsPtr cur;
Daniel Veillard76821142004-10-09 20:39:04 +000092
Daniel Veillard0d24b112004-10-11 12:28:34 +000093 if ((node == NULL) || (ns == NULL))
94 return(-1);
95
96 if ((node->type != XML_ELEMENT_NODE) &&
97 (node->type != XML_ATTRIBUTE_NODE) &&
98 (node->type != XML_DOCUMENT_NODE) &&
99 (node->type != XML_TEXT_NODE) &&
100 (node->type != XML_HTML_DOCUMENT_NODE) &&
101 (node->type != XML_XINCLUDE_START))
102 return(-2);
103
104 while ((node != NULL) &&
105 ((node->type == XML_ELEMENT_NODE) ||
106 (node->type == XML_ATTRIBUTE_NODE) ||
107 (node->type == XML_TEXT_NODE) ||
108 (node->type == XML_XINCLUDE_START))) {
109 if ((node->type == XML_ELEMENT_NODE) ||
110 (node->type == XML_XINCLUDE_START)) {
111 cur = node->nsDef;
112 while (cur != NULL) {
113 if (cur == ns)
114 return(1);
115 if (xmlStrEqual(cur->prefix, ns->prefix))
116 return(-2);
117 cur = cur->next;
118 }
Daniel Veillard76821142004-10-09 20:39:04 +0000119 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000120 node = node->parent;
Daniel Veillard76821142004-10-09 20:39:04 +0000121 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000122 /* the xml namespace may be declared on the document node */
123 if ((node != NULL) &&
124 ((node->type == XML_DOCUMENT_NODE) ||
125 (node->type == XML_HTML_DOCUMENT_NODE))) {
126 xmlNsPtr oldNs = ((xmlDocPtr) node)->oldNs;
127 if (oldNs == ns)
128 return(1);
129 }
130 return(-3);
Daniel Veillard76821142004-10-09 20:39:04 +0000131}
Daniel Veillard76821142004-10-09 20:39:04 +0000132
Daniel Veillard76821142004-10-09 20:39:04 +0000133static void
Daniel Veillard22cdb842004-10-04 14:09:17 +0000134xmlCtxtDumpSpaces(xmlDebugCtxtPtr ctxt)
135{
136 if (ctxt->check)
137 return;
138 if ((ctxt->output != NULL) && (ctxt->depth > 0)) {
139 if (ctxt->depth < 50)
140 fprintf(ctxt->output, &ctxt->shift[100 - 2 * ctxt->depth]);
141 else
142 fprintf(ctxt->output, ctxt->shift);
143 }
144}
145
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000146/**
147 * xmlDebugErr:
148 * @ctxt: a debug context
149 * @error: the error code
150 *
151 * Handle a debug error.
152 */
153static void
154xmlDebugErr(xmlDebugCtxtPtr ctxt, int error, const char *msg)
155{
156 ctxt->errors++;
157 __xmlRaiseError(NULL, NULL, NULL,
158 NULL, ctxt->node, XML_FROM_CHECK,
159 error, XML_ERR_ERROR, NULL, 0,
160 NULL, NULL, NULL, 0, 0,
161 msg);
162}
163static void
164xmlDebugErr2(xmlDebugCtxtPtr ctxt, int error, const char *msg, int extra)
165{
166 ctxt->errors++;
167 __xmlRaiseError(NULL, NULL, NULL,
168 NULL, ctxt->node, XML_FROM_CHECK,
169 error, XML_ERR_ERROR, NULL, 0,
170 NULL, NULL, NULL, 0, 0,
171 msg, extra);
172}
173static void
Daniel Veillardc6095782004-10-15 14:50:10 +0000174xmlDebugErr3(xmlDebugCtxtPtr ctxt, int error, const char *msg, const char *extra)
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000175{
176 ctxt->errors++;
177 __xmlRaiseError(NULL, NULL, NULL,
178 NULL, ctxt->node, XML_FROM_CHECK,
179 error, XML_ERR_ERROR, NULL, 0,
180 NULL, NULL, NULL, 0, 0,
181 msg, extra);
182}
183
Daniel Veillard0d24b112004-10-11 12:28:34 +0000184/**
185 * xmlCtxtNsCheckScope:
186 * @ctxt: the debugging context
187 * @node: the node
188 * @ns: the namespace node
189 *
190 * Report if a given namespace is is not in scope.
191 */
192static void
193xmlCtxtNsCheckScope(xmlDebugCtxtPtr ctxt, xmlNodePtr node, xmlNsPtr ns)
194{
195 int ret;
196
197 ret = xmlNsCheckScope(node, ns);
198 if (ret == -2) {
199 if (ns->prefix == NULL)
200 xmlDebugErr(ctxt, XML_CHECK_NS_SCOPE,
201 "Reference to default namespace not in scope\n");
202 else
203 xmlDebugErr3(ctxt, XML_CHECK_NS_SCOPE,
204 "Reference to namespace '%s' not in scope\n",
205 (char *) ns->prefix);
206 }
207 if (ret == -3) {
208 if (ns->prefix == NULL)
209 xmlDebugErr(ctxt, XML_CHECK_NS_ANCESTOR,
210 "Reference to default namespace not on ancestor\n");
211 else
212 xmlDebugErr3(ctxt, XML_CHECK_NS_ANCESTOR,
213 "Reference to namespace '%s' not on ancestor\n",
214 (char *) ns->prefix);
215 }
216}
217
Daniel Veillardc6095782004-10-15 14:50:10 +0000218/**
219 * xmlCtxtCheckString:
220 * @ctxt: the debug context
221 * @str: the string
222 *
223 * Do debugging on the string, currently it just checks the UTF-8 content
224 */
225static void
226xmlCtxtCheckString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
227{
228 if (str == NULL) return;
229 if (ctxt->check) {
230 if (!xmlCheckUTF8(str)) {
Daniel Veillard03a53c32004-10-26 16:06:51 +0000231 xmlDebugErr3(ctxt, XML_CHECK_NOT_UTF8,
Daniel Veillardc6095782004-10-15 14:50:10 +0000232 "String is not UTF-8 %s", (const char *) str);
233 }
234 }
235}
236
Daniel Veillard03a53c32004-10-26 16:06:51 +0000237/**
238 * xmlCtxtCheckName:
239 * @ctxt: the debug context
240 * @name: the name
241 *
242 * Do debugging on the name, for example the dictionnary status and
243 * conformance to the Name production.
244 */
245static void
246xmlCtxtCheckName(xmlDebugCtxtPtr ctxt, const xmlChar * name)
247{
248 if (ctxt->check) {
249 if (name == NULL) {
250 xmlDebugErr(ctxt, XML_CHECK_NO_NAME, "Name is NULL");
251 return;
252 }
253 if (xmlValidateName(name, 0)) {
254 xmlDebugErr3(ctxt, XML_CHECK_NOT_NCNAME,
255 "Name is not an NCName '%s'", (const char *) name);
256 }
257 if ((ctxt->dict != NULL) &&
258 (!xmlDictOwns(ctxt->dict, name))) {
259 xmlDebugErr3(ctxt, XML_CHECK_OUTSIDE_DICT,
260 "Name is not from the document dictionnary '%s'",
261 (const char *) name);
262 }
263 }
264}
265
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000266static void
267xmlCtxtGenericNodeCheck(xmlDebugCtxtPtr ctxt, xmlNodePtr node) {
Daniel Veillard03a53c32004-10-26 16:06:51 +0000268 xmlDocPtr doc;
269 xmlDictPtr dict;
270
271 doc = node->doc;
272
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000273 if (node->parent == NULL)
274 xmlDebugErr(ctxt, XML_CHECK_NO_PARENT,
275 "Node has no parent\n");
Daniel Veillard03a53c32004-10-26 16:06:51 +0000276 if (node->doc == NULL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000277 xmlDebugErr(ctxt, XML_CHECK_NO_DOC,
278 "Node has no doc\n");
Daniel Veillard03a53c32004-10-26 16:06:51 +0000279 dict = NULL;
280 } else {
281 dict = doc->dict;
282 if ((dict == NULL) && (ctxt->nodict == 0)) {
Daniel Veillard6927b102004-10-27 17:29:04 +0000283#if 0
284 /* desactivated right now as it raises too many errors */
285 if (doc->type == XML_DOCUMENT_NODE)
286 xmlDebugErr(ctxt, XML_CHECK_NO_DICT,
287 "Document has no dictionnary\n");
288#endif
Daniel Veillard03a53c32004-10-26 16:06:51 +0000289 ctxt->nodict = 1;
290 }
291 if (ctxt->doc == NULL)
292 ctxt->doc = doc;
293
294 if (ctxt->dict == NULL) {
295 ctxt->dict = dict;
296 }
297 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000298 if ((node->parent != NULL) && (node->doc != node->parent->doc) &&
299 (!xmlStrEqual(node->name, BAD_CAST "pseudoroot")))
300 xmlDebugErr(ctxt, XML_CHECK_WRONG_DOC,
301 "Node doc differs from parent's one\n");
302 if (node->prev == NULL) {
303 if (node->type == XML_ATTRIBUTE_NODE) {
304 if ((node->parent != NULL) &&
305 (node != (xmlNodePtr) node->parent->properties))
306 xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
307 "Attr has no prev and not first of attr list\n");
308
309 } else if ((node->parent != NULL) && (node->parent->children != node))
310 xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
311 "Node has no prev and not first of parent list\n");
312 } else {
313 if (node->prev->next != node)
314 xmlDebugErr(ctxt, XML_CHECK_WRONG_PREV,
315 "Node prev->next : back link wrong\n");
316 }
317 if (node->next == NULL) {
318 if ((node->parent != NULL) && (node->type != XML_ATTRIBUTE_NODE) &&
319 (node->parent->last != node))
320 xmlDebugErr(ctxt, XML_CHECK_NO_NEXT,
321 "Node has no next and not last of parent list\n");
322 } else {
323 if (node->next->prev != node)
324 xmlDebugErr(ctxt, XML_CHECK_WRONG_NEXT,
325 "Node next->prev : forward link wrong\n");
Daniel Veillard0d24b112004-10-11 12:28:34 +0000326 if (node->next->parent != node->parent)
327 xmlDebugErr(ctxt, XML_CHECK_WRONG_PARENT,
328 "Node next->prev : forward link wrong\n");
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000329 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000330 if (node->type == XML_ELEMENT_NODE) {
331 xmlNsPtr ns;
332
333 ns = node->nsDef;
334 while (ns != NULL) {
335 xmlCtxtNsCheckScope(ctxt, node, ns);
336 ns = ns->next;
337 }
338 if (node->ns != NULL)
339 xmlCtxtNsCheckScope(ctxt, node, node->ns);
340 } else if (node->type == XML_ATTRIBUTE_NODE) {
341 if (node->ns != NULL)
342 xmlCtxtNsCheckScope(ctxt, node, node->ns);
343 }
344
Daniel Veillardc6095782004-10-15 14:50:10 +0000345 if ((node->type != XML_ELEMENT_NODE) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000346 (node->type != XML_ATTRIBUTE_NODE) &&
347 (node->type != XML_ATTRIBUTE_DECL) &&
348 (node->type != XML_DTD_NODE) &&
349 (node->type != XML_HTML_DOCUMENT_NODE) &&
350 (node->type != XML_DOCUMENT_NODE)) {
Daniel Veillardc6095782004-10-15 14:50:10 +0000351 if (node->content != NULL)
William M. Brack9638d4c2004-10-15 18:25:33 +0000352 xmlCtxtCheckString(ctxt, (const xmlChar *) node->content);
Daniel Veillardc6095782004-10-15 14:50:10 +0000353 }
Daniel Veillard03a53c32004-10-26 16:06:51 +0000354 switch (node->type) {
355 case XML_ELEMENT_NODE:
356 case XML_ATTRIBUTE_NODE:
357 xmlCtxtCheckName(ctxt, node->name);
358 break;
359 case XML_TEXT_NODE:
360 if ((node->name == xmlStringText) ||
361 (node->name == xmlStringTextNoenc))
362 break;
363 /* some case of entity substitution can lead to this */
364 if ((ctxt->dict != NULL) &&
Daniel Veillard6927b102004-10-27 17:29:04 +0000365 (node->name == xmlDictLookup(ctxt->dict, BAD_CAST "nbktext",
366 7)))
Daniel Veillard03a53c32004-10-26 16:06:51 +0000367 break;
368
369 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
370 "Text node has wrong name '%s'",
371 (const char *) node->name);
372 break;
373 case XML_COMMENT_NODE:
374 if (node->name == xmlStringComment)
375 break;
376 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
377 "Comment node has wrong name '%s'",
378 (const char *) node->name);
379 break;
380 case XML_PI_NODE:
381 xmlCtxtCheckName(ctxt, node->name);
382 break;
383 case XML_CDATA_SECTION_NODE:
384 if (node->name == NULL)
385 break;
386 xmlDebugErr3(ctxt, XML_CHECK_NAME_NOT_NULL,
387 "CData section has non NULL name '%s'",
388 (const char *) node->name);
389 break;
390 case XML_ENTITY_REF_NODE:
391 case XML_ENTITY_NODE:
392 case XML_DOCUMENT_TYPE_NODE:
393 case XML_DOCUMENT_FRAG_NODE:
394 case XML_NOTATION_NODE:
395 case XML_DTD_NODE:
396 case XML_ELEMENT_DECL:
397 case XML_ATTRIBUTE_DECL:
398 case XML_ENTITY_DECL:
399 case XML_NAMESPACE_DECL:
400 case XML_XINCLUDE_START:
401 case XML_XINCLUDE_END:
402#ifdef LIBXML_DOCB_ENABLED
403 case XML_DOCB_DOCUMENT_NODE:
404#endif
405 case XML_DOCUMENT_NODE:
406 case XML_HTML_DOCUMENT_NODE:
407 break;
408 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000409}
410
Daniel Veillard22cdb842004-10-04 14:09:17 +0000411static void
412xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
413{
414 int i;
415
Daniel Veillardc6095782004-10-15 14:50:10 +0000416 if (ctxt->check) {
Daniel Veillard22cdb842004-10-04 14:09:17 +0000417 return;
Daniel Veillardc6095782004-10-15 14:50:10 +0000418 }
Daniel Veillard22cdb842004-10-04 14:09:17 +0000419 /* TODO: check UTF8 content of the string */
420 if (str == NULL) {
421 fprintf(ctxt->output, "(NULL)");
422 return;
423 }
424 for (i = 0; i < 40; i++)
425 if (str[i] == 0)
426 return;
427 else if (IS_BLANK_CH(str[i]))
428 fputc(' ', ctxt->output);
429 else if (str[i] >= 0x80)
430 fprintf(ctxt->output, "#%X", str[i]);
431 else
432 fputc(str[i], ctxt->output);
433 fprintf(ctxt->output, "...");
434}
435
436static void
437xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
438{
439 xmlCtxtDumpSpaces(ctxt);
440
441 if (dtd == NULL) {
442 if (!ctxt->check)
443 fprintf(ctxt->output, "DTD node is NULL\n");
444 return;
445 }
446
447 if (dtd->type != XML_DTD_NODE) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000448 xmlDebugErr(ctxt, XML_CHECK_NOT_DTD,
449 "Node is not a DTD");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000450 return;
451 }
452 if (!ctxt->check) {
453 if (dtd->name != NULL)
454 fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name);
455 else
456 fprintf(ctxt->output, "DTD");
457 if (dtd->ExternalID != NULL)
458 fprintf(ctxt->output, ", PUBLIC %s", (char *) dtd->ExternalID);
459 if (dtd->SystemID != NULL)
460 fprintf(ctxt->output, ", SYSTEM %s", (char *) dtd->SystemID);
461 fprintf(ctxt->output, "\n");
462 }
463 /*
464 * Do a bit of checking
465 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000466 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000467}
468
469static void
470xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr)
471{
472 xmlCtxtDumpSpaces(ctxt);
473
474 if (attr == NULL) {
475 if (!ctxt->check)
476 fprintf(ctxt->output, "Attribute declaration is NULL\n");
477 return;
478 }
479 if (attr->type != XML_ATTRIBUTE_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000480 xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL,
481 "Node is not an attribute declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000482 return;
483 }
484 if (attr->name != NULL) {
485 if (!ctxt->check)
486 fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name);
487 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000488 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
489 "Node attribute declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000490 if (attr->elem != NULL) {
491 if (!ctxt->check)
492 fprintf(ctxt->output, " for %s", (char *) attr->elem);
493 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000494 xmlDebugErr(ctxt, XML_CHECK_NO_ELEM,
495 "Node attribute declaration has no element name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000496 if (!ctxt->check) {
497 switch (attr->atype) {
498 case XML_ATTRIBUTE_CDATA:
499 fprintf(ctxt->output, " CDATA");
500 break;
501 case XML_ATTRIBUTE_ID:
502 fprintf(ctxt->output, " ID");
503 break;
504 case XML_ATTRIBUTE_IDREF:
505 fprintf(ctxt->output, " IDREF");
506 break;
507 case XML_ATTRIBUTE_IDREFS:
508 fprintf(ctxt->output, " IDREFS");
509 break;
510 case XML_ATTRIBUTE_ENTITY:
511 fprintf(ctxt->output, " ENTITY");
512 break;
513 case XML_ATTRIBUTE_ENTITIES:
514 fprintf(ctxt->output, " ENTITIES");
515 break;
516 case XML_ATTRIBUTE_NMTOKEN:
517 fprintf(ctxt->output, " NMTOKEN");
518 break;
519 case XML_ATTRIBUTE_NMTOKENS:
520 fprintf(ctxt->output, " NMTOKENS");
521 break;
522 case XML_ATTRIBUTE_ENUMERATION:
523 fprintf(ctxt->output, " ENUMERATION");
524 break;
525 case XML_ATTRIBUTE_NOTATION:
526 fprintf(ctxt->output, " NOTATION ");
527 break;
528 }
529 if (attr->tree != NULL) {
530 int indx;
531 xmlEnumerationPtr cur = attr->tree;
532
533 for (indx = 0; indx < 5; indx++) {
534 if (indx != 0)
535 fprintf(ctxt->output, "|%s", (char *) cur->name);
536 else
537 fprintf(ctxt->output, " (%s", (char *) cur->name);
538 cur = cur->next;
539 if (cur == NULL)
540 break;
541 }
542 if (cur == NULL)
543 fprintf(ctxt->output, ")");
544 else
545 fprintf(ctxt->output, "...)");
546 }
547 switch (attr->def) {
548 case XML_ATTRIBUTE_NONE:
549 break;
550 case XML_ATTRIBUTE_REQUIRED:
551 fprintf(ctxt->output, " REQUIRED");
552 break;
553 case XML_ATTRIBUTE_IMPLIED:
554 fprintf(ctxt->output, " IMPLIED");
555 break;
556 case XML_ATTRIBUTE_FIXED:
557 fprintf(ctxt->output, " FIXED");
558 break;
559 }
560 if (attr->defaultValue != NULL) {
561 fprintf(ctxt->output, "\"");
562 xmlCtxtDumpString(ctxt, attr->defaultValue);
563 fprintf(ctxt->output, "\"");
564 }
565 fprintf(ctxt->output, "\n");
566 }
567
568 /*
569 * Do a bit of checking
570 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000571 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000572}
573
574static void
575xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem)
576{
577 xmlCtxtDumpSpaces(ctxt);
578
579 if (elem == NULL) {
580 if (!ctxt->check)
581 fprintf(ctxt->output, "Element declaration is NULL\n");
582 return;
583 }
584 if (elem->type != XML_ELEMENT_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000585 xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL,
586 "Node is not an element declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000587 return;
588 }
589 if (elem->name != NULL) {
590 if (!ctxt->check) {
591 fprintf(ctxt->output, "ELEMDECL(");
592 xmlCtxtDumpString(ctxt, elem->name);
593 fprintf(ctxt->output, ")");
594 }
595 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000596 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
597 "Element declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000598 if (!ctxt->check) {
599 switch (elem->etype) {
600 case XML_ELEMENT_TYPE_UNDEFINED:
601 fprintf(ctxt->output, ", UNDEFINED");
602 break;
603 case XML_ELEMENT_TYPE_EMPTY:
604 fprintf(ctxt->output, ", EMPTY");
605 break;
606 case XML_ELEMENT_TYPE_ANY:
607 fprintf(ctxt->output, ", ANY");
608 break;
609 case XML_ELEMENT_TYPE_MIXED:
610 fprintf(ctxt->output, ", MIXED ");
611 break;
612 case XML_ELEMENT_TYPE_ELEMENT:
613 fprintf(ctxt->output, ", MIXED ");
614 break;
615 }
616 if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) {
617 char buf[5001];
618
619 buf[0] = 0;
620 xmlSnprintfElementContent(buf, 5000, elem->content, 1);
621 buf[5000] = 0;
622 fprintf(ctxt->output, "%s", buf);
623 }
624 fprintf(ctxt->output, "\n");
625 }
626
627 /*
628 * Do a bit of checking
629 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000630 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000631}
632
633static void
634xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
635{
636 xmlCtxtDumpSpaces(ctxt);
637
638 if (ent == NULL) {
639 if (!ctxt->check)
640 fprintf(ctxt->output, "Entity declaration is NULL\n");
641 return;
642 }
643 if (ent->type != XML_ENTITY_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000644 xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL,
645 "Node is not an entity declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000646 return;
647 }
648 if (ent->name != NULL) {
649 if (!ctxt->check) {
650 fprintf(ctxt->output, "ENTITYDECL(");
651 xmlCtxtDumpString(ctxt, ent->name);
652 fprintf(ctxt->output, ")");
653 }
654 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000655 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
656 "Entity declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000657 if (!ctxt->check) {
658 switch (ent->etype) {
659 case XML_INTERNAL_GENERAL_ENTITY:
660 fprintf(ctxt->output, ", internal\n");
661 break;
662 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
663 fprintf(ctxt->output, ", external parsed\n");
664 break;
665 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
666 fprintf(ctxt->output, ", unparsed\n");
667 break;
668 case XML_INTERNAL_PARAMETER_ENTITY:
669 fprintf(ctxt->output, ", parameter\n");
670 break;
671 case XML_EXTERNAL_PARAMETER_ENTITY:
672 fprintf(ctxt->output, ", external parameter\n");
673 break;
674 case XML_INTERNAL_PREDEFINED_ENTITY:
675 fprintf(ctxt->output, ", predefined\n");
676 break;
677 }
678 if (ent->ExternalID) {
679 xmlCtxtDumpSpaces(ctxt);
680 fprintf(ctxt->output, " ExternalID=%s\n",
681 (char *) ent->ExternalID);
682 }
683 if (ent->SystemID) {
684 xmlCtxtDumpSpaces(ctxt);
685 fprintf(ctxt->output, " SystemID=%s\n",
686 (char *) ent->SystemID);
687 }
688 if (ent->URI != NULL) {
689 xmlCtxtDumpSpaces(ctxt);
690 fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI);
691 }
692 if (ent->content) {
693 xmlCtxtDumpSpaces(ctxt);
694 fprintf(ctxt->output, " content=");
695 xmlCtxtDumpString(ctxt, ent->content);
696 fprintf(ctxt->output, "\n");
697 }
698 }
699
700 /*
701 * Do a bit of checking
702 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000703 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000704}
705
706static void
707xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
708{
709 xmlCtxtDumpSpaces(ctxt);
710
711 if (ns == NULL) {
712 if (!ctxt->check)
713 fprintf(ctxt->output, "namespace node is NULL\n");
714 return;
715 }
716 if (ns->type != XML_NAMESPACE_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000717 xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL,
718 "Node is not a namespace declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000719 return;
720 }
721 if (ns->href == NULL) {
722 if (ns->prefix != NULL)
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000723 xmlDebugErr3(ctxt, XML_CHECK_NO_HREF,
724 "Incomplete namespace %s href=NULL\n",
Daniel Veillard22cdb842004-10-04 14:09:17 +0000725 (char *) ns->prefix);
726 else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000727 xmlDebugErr(ctxt, XML_CHECK_NO_HREF,
728 "Incomplete default namespace href=NULL\n");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000729 } else {
730 if (!ctxt->check) {
731 if (ns->prefix != NULL)
732 fprintf(ctxt->output, "namespace %s href=",
733 (char *) ns->prefix);
734 else
735 fprintf(ctxt->output, "default namespace href=");
736
737 xmlCtxtDumpString(ctxt, ns->href);
738 fprintf(ctxt->output, "\n");
739 }
740 }
741}
742
743static void
744xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
745{
746 while (ns != NULL) {
747 xmlCtxtDumpNamespace(ctxt, ns);
748 ns = ns->next;
749 }
750}
751
752static void
753xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
754{
755 xmlCtxtDumpSpaces(ctxt);
756
757 if (ent == NULL) {
758 if (!ctxt->check)
759 fprintf(ctxt->output, "Entity is NULL\n");
760 return;
761 }
762 if (!ctxt->check) {
763 switch (ent->etype) {
764 case XML_INTERNAL_GENERAL_ENTITY:
765 fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY ");
766 break;
767 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
768 fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
769 break;
770 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
771 fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
772 break;
773 case XML_INTERNAL_PARAMETER_ENTITY:
774 fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY ");
775 break;
776 case XML_EXTERNAL_PARAMETER_ENTITY:
777 fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY ");
778 break;
779 default:
780 fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype);
781 }
782 fprintf(ctxt->output, "%s\n", ent->name);
783 if (ent->ExternalID) {
784 xmlCtxtDumpSpaces(ctxt);
785 fprintf(ctxt->output, "ExternalID=%s\n",
786 (char *) ent->ExternalID);
787 }
788 if (ent->SystemID) {
789 xmlCtxtDumpSpaces(ctxt);
790 fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID);
791 }
792 if (ent->URI) {
793 xmlCtxtDumpSpaces(ctxt);
794 fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI);
795 }
796 if (ent->content) {
797 xmlCtxtDumpSpaces(ctxt);
798 fprintf(ctxt->output, "content=");
799 xmlCtxtDumpString(ctxt, ent->content);
800 fprintf(ctxt->output, "\n");
801 }
802 }
803}
804
805/**
806 * xmlCtxtDumpAttr:
807 * @output: the FILE * for the output
808 * @attr: the attribute
809 * @depth: the indentation level.
810 *
811 * Dumps debug information for the attribute
812 */
813static void
814xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
815{
816 xmlCtxtDumpSpaces(ctxt);
817
818 if (attr == NULL) {
819 if (!ctxt->check)
820 fprintf(ctxt->output, "Attr is NULL");
821 return;
822 }
823 if (!ctxt->check) {
824 fprintf(ctxt->output, "ATTRIBUTE ");
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000825 xmlCtxtDumpString(ctxt, attr->name);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000826 fprintf(ctxt->output, "\n");
827 if (attr->children != NULL) {
828 ctxt->depth++;
829 xmlCtxtDumpNodeList(ctxt, attr->children);
830 ctxt->depth--;
831 }
832 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000833 if (attr->name == NULL)
834 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
835 "Attribute has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000836
837 /*
838 * Do a bit of checking
839 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000840 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000841}
842
843/**
844 * xmlCtxtDumpAttrList:
845 * @output: the FILE * for the output
846 * @attr: the attribute list
847 * @depth: the indentation level.
848 *
849 * Dumps debug information for the attribute list
850 */
851static void
852xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
853{
854 while (attr != NULL) {
855 xmlCtxtDumpAttr(ctxt, attr);
856 attr = attr->next;
857 }
858}
859
860/**
861 * xmlCtxtDumpOneNode:
862 * @output: the FILE * for the output
863 * @node: the node
864 * @depth: the indentation level.
865 *
866 * Dumps debug information for the element node, it is not recursive
867 */
868static void
869xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
870{
871 if (node == NULL) {
872 if (!ctxt->check) {
873 xmlCtxtDumpSpaces(ctxt);
874 fprintf(ctxt->output, "node is NULL\n");
875 }
876 return;
877 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000878 ctxt->node = node;
879
Daniel Veillard22cdb842004-10-04 14:09:17 +0000880 switch (node->type) {
881 case XML_ELEMENT_NODE:
882 if (!ctxt->check) {
883 xmlCtxtDumpSpaces(ctxt);
884 fprintf(ctxt->output, "ELEMENT ");
885 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
886 xmlCtxtDumpString(ctxt, node->ns->prefix);
887 fprintf(ctxt->output, ":");
888 }
889 xmlCtxtDumpString(ctxt, node->name);
890 fprintf(ctxt->output, "\n");
891 }
892 break;
893 case XML_ATTRIBUTE_NODE:
894 if (!ctxt->check)
895 xmlCtxtDumpSpaces(ctxt);
896 fprintf(ctxt->output, "Error, ATTRIBUTE found here\n");
897 break;
898 case XML_TEXT_NODE:
899 if (!ctxt->check) {
900 xmlCtxtDumpSpaces(ctxt);
901 if (node->name == (const xmlChar *) xmlStringTextNoenc)
902 fprintf(ctxt->output, "TEXT no enc\n");
903 else
904 fprintf(ctxt->output, "TEXT\n");
905 }
906 break;
907 case XML_CDATA_SECTION_NODE:
908 if (!ctxt->check) {
909 xmlCtxtDumpSpaces(ctxt);
910 fprintf(ctxt->output, "CDATA_SECTION\n");
911 }
912 break;
913 case XML_ENTITY_REF_NODE:
914 if (!ctxt->check) {
915 xmlCtxtDumpSpaces(ctxt);
916 fprintf(ctxt->output, "ENTITY_REF(%s)\n",
917 (char *) node->name);
918 }
919 break;
920 case XML_ENTITY_NODE:
921 if (!ctxt->check) {
922 xmlCtxtDumpSpaces(ctxt);
923 fprintf(ctxt->output, "ENTITY\n");
924 }
925 break;
926 case XML_PI_NODE:
927 if (!ctxt->check) {
928 xmlCtxtDumpSpaces(ctxt);
929 fprintf(ctxt->output, "PI %s\n", (char *) node->name);
930 }
931 break;
932 case XML_COMMENT_NODE:
933 if (!ctxt->check) {
934 xmlCtxtDumpSpaces(ctxt);
935 fprintf(ctxt->output, "COMMENT\n");
936 }
937 break;
938 case XML_DOCUMENT_NODE:
939 case XML_HTML_DOCUMENT_NODE:
940 if (!ctxt->check) {
941 xmlCtxtDumpSpaces(ctxt);
942 }
943 fprintf(ctxt->output, "PBM: DOCUMENT found here\n");
944 break;
945 case XML_DOCUMENT_TYPE_NODE:
946 if (!ctxt->check) {
947 xmlCtxtDumpSpaces(ctxt);
948 fprintf(ctxt->output, "DOCUMENT_TYPE\n");
949 }
950 break;
951 case XML_DOCUMENT_FRAG_NODE:
952 if (!ctxt->check) {
953 xmlCtxtDumpSpaces(ctxt);
954 fprintf(ctxt->output, "DOCUMENT_FRAG\n");
955 }
956 break;
957 case XML_NOTATION_NODE:
958 if (!ctxt->check) {
959 xmlCtxtDumpSpaces(ctxt);
960 fprintf(ctxt->output, "NOTATION\n");
961 }
962 break;
963 case XML_DTD_NODE:
964 xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
965 return;
966 case XML_ELEMENT_DECL:
967 xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
968 return;
969 case XML_ATTRIBUTE_DECL:
970 xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
971 return;
972 case XML_ENTITY_DECL:
973 xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
974 return;
975 case XML_NAMESPACE_DECL:
976 xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
977 return;
978 case XML_XINCLUDE_START:
979 if (!ctxt->check) {
980 xmlCtxtDumpSpaces(ctxt);
981 fprintf(ctxt->output, "INCLUDE START\n");
982 }
983 return;
984 case XML_XINCLUDE_END:
985 if (!ctxt->check) {
986 xmlCtxtDumpSpaces(ctxt);
987 fprintf(ctxt->output, "INCLUDE END\n");
988 }
989 return;
990 default:
991 if (!ctxt->check)
992 xmlCtxtDumpSpaces(ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000993 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
994 "Unknown node type %d\n", node->type);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000995 return;
996 }
997 if (node->doc == NULL) {
998 if (!ctxt->check) {
999 xmlCtxtDumpSpaces(ctxt);
1000 }
1001 fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
1002 }
1003 ctxt->depth++;
1004 if (node->nsDef != NULL)
1005 xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
1006 if (node->properties != NULL)
1007 xmlCtxtDumpAttrList(ctxt, node->properties);
1008 if (node->type != XML_ENTITY_REF_NODE) {
1009 if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
1010 if (!ctxt->check) {
1011 xmlCtxtDumpSpaces(ctxt);
1012 fprintf(ctxt->output, "content=");
1013 xmlCtxtDumpString(ctxt, node->content);
1014 fprintf(ctxt->output, "\n");
1015 }
1016 }
1017 } else {
1018 xmlEntityPtr ent;
1019
1020 ent = xmlGetDocEntity(node->doc, node->name);
1021 if (ent != NULL)
1022 xmlCtxtDumpEntity(ctxt, ent);
1023 }
1024 ctxt->depth--;
1025
1026 /*
1027 * Do a bit of checking
1028 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001029 xmlCtxtGenericNodeCheck(ctxt, node);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001030}
1031
1032/**
1033 * xmlCtxtDumpNode:
1034 * @output: the FILE * for the output
1035 * @node: the node
1036 * @depth: the indentation level.
1037 *
1038 * Dumps debug information for the element node, it is recursive
1039 */
1040static void
1041xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1042{
1043 if (node == NULL) {
1044 if (!ctxt->check) {
1045 xmlCtxtDumpSpaces(ctxt);
1046 fprintf(ctxt->output, "node is NULL\n");
1047 }
1048 return;
1049 }
1050 xmlCtxtDumpOneNode(ctxt, node);
1051 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
1052 ctxt->depth++;
1053 xmlCtxtDumpNodeList(ctxt, node->children);
1054 ctxt->depth--;
1055 }
1056}
1057
1058/**
1059 * xmlCtxtDumpNodeList:
1060 * @output: the FILE * for the output
1061 * @node: the node list
1062 * @depth: the indentation level.
1063 *
1064 * Dumps debug information for the list of element node, it is recursive
1065 */
1066static void
1067xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1068{
1069 while (node != NULL) {
1070 xmlCtxtDumpNode(ctxt, node);
1071 node = node->next;
1072 }
1073}
1074
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001075static void
1076xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1077{
1078 if (doc == NULL) {
1079 if (!ctxt->check)
1080 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1081 return;
1082 }
1083 ctxt->node = (xmlNodePtr) doc;
1084
1085 switch (doc->type) {
1086 case XML_ELEMENT_NODE:
1087 xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
1088 "Misplaced ELEMENT node\n");
1089 break;
1090 case XML_ATTRIBUTE_NODE:
1091 xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
1092 "Misplaced ATTRIBUTE node\n");
1093 break;
1094 case XML_TEXT_NODE:
1095 xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
1096 "Misplaced TEXT node\n");
1097 break;
1098 case XML_CDATA_SECTION_NODE:
1099 xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
1100 "Misplaced CDATA node\n");
1101 break;
1102 case XML_ENTITY_REF_NODE:
1103 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
1104 "Misplaced ENTITYREF node\n");
1105 break;
1106 case XML_ENTITY_NODE:
1107 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
1108 "Misplaced ENTITY node\n");
1109 break;
1110 case XML_PI_NODE:
1111 xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
1112 "Misplaced PI node\n");
1113 break;
1114 case XML_COMMENT_NODE:
1115 xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
1116 "Misplaced COMMENT node\n");
1117 break;
1118 case XML_DOCUMENT_NODE:
1119 if (!ctxt->check)
1120 fprintf(ctxt->output, "DOCUMENT\n");
1121 break;
1122 case XML_HTML_DOCUMENT_NODE:
1123 if (!ctxt->check)
1124 fprintf(ctxt->output, "HTML DOCUMENT\n");
1125 break;
1126 case XML_DOCUMENT_TYPE_NODE:
1127 xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
1128 "Misplaced DOCTYPE node\n");
1129 break;
1130 case XML_DOCUMENT_FRAG_NODE:
1131 xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
1132 "Misplaced FRAGMENT node\n");
1133 break;
1134 case XML_NOTATION_NODE:
1135 xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
1136 "Misplaced NOTATION node\n");
1137 break;
1138 default:
1139 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1140 "Unknown node type %d\n", doc->type);
1141 }
1142}
Daniel Veillard22cdb842004-10-04 14:09:17 +00001143
1144/**
1145 * xmlCtxtDumpDocumentHead:
1146 * @output: the FILE * for the output
1147 * @doc: the document
1148 *
1149 * Dumps debug information cncerning the document, not recursive
1150 */
1151static void
1152xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1153{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001154 if (doc == NULL) return;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001155 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001156 if (!ctxt->check) {
1157 if (doc->name != NULL) {
1158 fprintf(ctxt->output, "name=");
1159 xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
1160 fprintf(ctxt->output, "\n");
1161 }
1162 if (doc->version != NULL) {
1163 fprintf(ctxt->output, "version=");
1164 xmlCtxtDumpString(ctxt, doc->version);
1165 fprintf(ctxt->output, "\n");
1166 }
1167 if (doc->encoding != NULL) {
1168 fprintf(ctxt->output, "encoding=");
1169 xmlCtxtDumpString(ctxt, doc->encoding);
1170 fprintf(ctxt->output, "\n");
1171 }
1172 if (doc->URL != NULL) {
1173 fprintf(ctxt->output, "URL=");
1174 xmlCtxtDumpString(ctxt, doc->URL);
1175 fprintf(ctxt->output, "\n");
1176 }
1177 if (doc->standalone)
1178 fprintf(ctxt->output, "standalone=true\n");
1179 }
1180 if (doc->oldNs != NULL)
1181 xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
1182}
1183
1184/**
1185 * xmlCtxtDumpDocument:
1186 * @output: the FILE * for the output
1187 * @doc: the document
1188 *
1189 * Dumps debug information for the document, it's recursive
1190 */
1191static void
1192xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1193{
1194 if (doc == NULL) {
1195 if (!ctxt->check)
1196 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1197 return;
1198 }
1199 xmlCtxtDumpDocumentHead(ctxt, doc);
1200 if (((doc->type == XML_DOCUMENT_NODE) ||
1201 (doc->type == XML_HTML_DOCUMENT_NODE))
1202 && (doc->children != NULL)) {
1203 ctxt->depth++;
1204 xmlCtxtDumpNodeList(ctxt, doc->children);
1205 ctxt->depth--;
1206 }
1207}
1208
1209static void
1210xmlCtxtDumpEntityCallback(xmlEntityPtr cur, xmlDebugCtxtPtr ctxt)
1211{
1212 if (cur == NULL) {
1213 if (!ctxt->check)
1214 fprintf(ctxt->output, "Entity is NULL");
1215 return;
1216 }
1217 if (!ctxt->check) {
1218 fprintf(ctxt->output, "%s : ", (char *) cur->name);
1219 switch (cur->etype) {
1220 case XML_INTERNAL_GENERAL_ENTITY:
1221 fprintf(ctxt->output, "INTERNAL GENERAL, ");
1222 break;
1223 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1224 fprintf(ctxt->output, "EXTERNAL PARSED, ");
1225 break;
1226 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1227 fprintf(ctxt->output, "EXTERNAL UNPARSED, ");
1228 break;
1229 case XML_INTERNAL_PARAMETER_ENTITY:
1230 fprintf(ctxt->output, "INTERNAL PARAMETER, ");
1231 break;
1232 case XML_EXTERNAL_PARAMETER_ENTITY:
1233 fprintf(ctxt->output, "EXTERNAL PARAMETER, ");
1234 break;
1235 default:
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001236 xmlDebugErr2(ctxt, XML_CHECK_ENTITY_TYPE,
1237 "Unknown entity type %d\n", cur->etype);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001238 }
1239 if (cur->ExternalID != NULL)
1240 fprintf(ctxt->output, "ID \"%s\"", (char *) cur->ExternalID);
1241 if (cur->SystemID != NULL)
1242 fprintf(ctxt->output, "SYSTEM \"%s\"", (char *) cur->SystemID);
1243 if (cur->orig != NULL)
1244 fprintf(ctxt->output, "\n orig \"%s\"", (char *) cur->orig);
1245 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL))
1246 fprintf(ctxt->output, "\n content \"%s\"",
1247 (char *) cur->content);
1248 fprintf(ctxt->output, "\n");
1249 }
1250}
1251
1252/**
1253 * xmlCtxtDumpEntities:
1254 * @output: the FILE * for the output
1255 * @doc: the document
1256 *
1257 * Dumps debug information for all the entities in use by the document
1258 */
1259static void
1260xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1261{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001262 if (doc == NULL) return;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001263 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001264 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
1265 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1266 doc->intSubset->entities;
1267
1268 if (!ctxt->check)
1269 fprintf(ctxt->output, "Entities in internal subset\n");
1270 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1271 ctxt);
1272 } else
1273 fprintf(ctxt->output, "No entities in internal subset\n");
1274 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
1275 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1276 doc->extSubset->entities;
1277
1278 if (!ctxt->check)
1279 fprintf(ctxt->output, "Entities in external subset\n");
1280 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1281 ctxt);
1282 } else if (!ctxt->check)
1283 fprintf(ctxt->output, "No entities in external subset\n");
1284}
1285
1286/**
1287 * xmlCtxtDumpDTD:
1288 * @output: the FILE * for the output
1289 * @dtd: the DTD
1290 *
1291 * Dumps debug information for the DTD
1292 */
1293static void
1294xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
1295{
1296 if (dtd == NULL) {
1297 if (!ctxt->check)
1298 fprintf(ctxt->output, "DTD is NULL\n");
1299 return;
1300 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001301 xmlCtxtDumpDtdNode(ctxt, dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001302 if (dtd->children == NULL)
1303 fprintf(ctxt->output, " DTD is empty\n");
1304 else {
1305 ctxt->depth++;
1306 xmlCtxtDumpNodeList(ctxt, dtd->children);
1307 ctxt->depth--;
1308 }
1309}
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001310
Daniel Veillard22cdb842004-10-04 14:09:17 +00001311/************************************************************************
1312 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001313 * Public entry points for dump *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001314 * *
1315 ************************************************************************/
1316
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001317/**
1318 * xmlDebugDumpString:
1319 * @output: the FILE * for the output
1320 * @str: the string
1321 *
1322 * Dumps informations about the string, shorten it if necessary
1323 */
1324void
1325xmlDebugDumpString(FILE * output, const xmlChar * str)
1326{
Owen Taylor3473f882001-02-23 17:55:21 +00001327 int i;
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001328
Daniel Veillard7db38712002-02-07 16:39:11 +00001329 if (output == NULL)
1330 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00001331 if (str == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001332 fprintf(output, "(NULL)");
1333 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001334 }
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001335 for (i = 0; i < 40; i++)
1336 if (str[i] == 0)
1337 return;
William M. Brack76e95df2003-10-18 16:20:14 +00001338 else if (IS_BLANK_CH(str[i]))
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001339 fputc(' ', output);
1340 else if (str[i] >= 0x80)
1341 fprintf(output, "#%X", str[i]);
1342 else
1343 fputc(str[i], output);
Owen Taylor3473f882001-02-23 17:55:21 +00001344 fprintf(output, "...");
1345}
1346
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001347/**
1348 * xmlDebugDumpAttr:
1349 * @output: the FILE * for the output
1350 * @attr: the attribute
1351 * @depth: the indentation level.
1352 *
1353 * Dumps debug information for the attribute
1354 */
1355void
1356xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
Daniel Veillard22cdb842004-10-04 14:09:17 +00001357 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001358
Daniel Veillarda82b1822004-11-08 16:24:57 +00001359 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001360 xmlCtxtDumpInitCtxt(&ctxt);
1361 ctxt.output = output;
1362 ctxt.depth = depth;
1363 xmlCtxtDumpAttr(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001364 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001365}
Owen Taylor3473f882001-02-23 17:55:21 +00001366
Owen Taylor3473f882001-02-23 17:55:21 +00001367
Daniel Veillard22cdb842004-10-04 14:09:17 +00001368/**
1369 * xmlDebugDumpEntities:
1370 * @output: the FILE * for the output
1371 * @doc: the document
1372 *
1373 * Dumps debug information for all the entities in use by the document
1374 */
1375void
1376xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
1377{
1378 xmlDebugCtxt ctxt;
1379
Daniel Veillarda82b1822004-11-08 16:24:57 +00001380 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001381 xmlCtxtDumpInitCtxt(&ctxt);
1382 ctxt.output = output;
1383 xmlCtxtDumpEntities(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001384 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001385}
1386
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001387/**
1388 * xmlDebugDumpAttrList:
1389 * @output: the FILE * for the output
1390 * @attr: the attribute list
1391 * @depth: the indentation level.
1392 *
1393 * Dumps debug information for the attribute list
1394 */
1395void
1396xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
1397{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001398 xmlDebugCtxt ctxt;
1399
Daniel Veillarda82b1822004-11-08 16:24:57 +00001400 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001401 xmlCtxtDumpInitCtxt(&ctxt);
1402 ctxt.output = output;
1403 ctxt.depth = depth;
1404 xmlCtxtDumpAttrList(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001405 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001406}
1407
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001408/**
1409 * xmlDebugDumpOneNode:
1410 * @output: the FILE * for the output
1411 * @node: the node
1412 * @depth: the indentation level.
1413 *
1414 * Dumps debug information for the element node, it is not recursive
1415 */
1416void
1417xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
1418{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001419 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001420
Daniel Veillarda82b1822004-11-08 16:24:57 +00001421 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001422 xmlCtxtDumpInitCtxt(&ctxt);
1423 ctxt.output = output;
1424 ctxt.depth = depth;
1425 xmlCtxtDumpOneNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001426 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001427}
1428
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001429/**
1430 * xmlDebugDumpNode:
1431 * @output: the FILE * for the output
1432 * @node: the node
1433 * @depth: the indentation level.
1434 *
1435 * Dumps debug information for the element node, it is recursive
1436 */
1437void
1438xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
1439{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001440 xmlDebugCtxt ctxt;
1441
Daniel Veillard7db38712002-02-07 16:39:11 +00001442 if (output == NULL)
1443 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001444 xmlCtxtDumpInitCtxt(&ctxt);
1445 ctxt.output = output;
1446 ctxt.depth = depth;
1447 xmlCtxtDumpNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001448 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001449}
1450
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001451/**
1452 * xmlDebugDumpNodeList:
1453 * @output: the FILE * for the output
1454 * @node: the node list
1455 * @depth: the indentation level.
1456 *
1457 * Dumps debug information for the list of element node, it is recursive
1458 */
1459void
1460xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
1461{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001462 xmlDebugCtxt ctxt;
1463
Daniel Veillard7db38712002-02-07 16:39:11 +00001464 if (output == NULL)
1465 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001466 xmlCtxtDumpInitCtxt(&ctxt);
1467 ctxt.output = output;
1468 ctxt.depth = depth;
1469 xmlCtxtDumpNodeList(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001470 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001471}
1472
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001473/**
1474 * xmlDebugDumpDocumentHead:
1475 * @output: the FILE * for the output
1476 * @doc: the document
1477 *
1478 * Dumps debug information cncerning the document, not recursive
1479 */
1480void
1481xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
1482{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001483 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001484
Daniel Veillard22cdb842004-10-04 14:09:17 +00001485 if (output == NULL)
1486 output = stdout;
1487 xmlCtxtDumpInitCtxt(&ctxt);
1488 ctxt.output = output;
1489 xmlCtxtDumpDocumentHead(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001490 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001491}
1492
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001493/**
1494 * xmlDebugDumpDocument:
1495 * @output: the FILE * for the output
1496 * @doc: the document
1497 *
1498 * Dumps debug information for the document, it's recursive
1499 */
1500void
1501xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
1502{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001503 xmlDebugCtxt ctxt;
1504
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001505 if (output == NULL)
Daniel Veillard22cdb842004-10-04 14:09:17 +00001506 output = stdout;
1507 xmlCtxtDumpInitCtxt(&ctxt);
1508 ctxt.output = output;
1509 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001510 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001511}
Owen Taylor3473f882001-02-23 17:55:21 +00001512
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001513/**
1514 * xmlDebugDumpDTD:
1515 * @output: the FILE * for the output
1516 * @dtd: the DTD
1517 *
1518 * Dumps debug information for the DTD
1519 */
1520void
1521xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
1522{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001523 xmlDebugCtxt ctxt;
1524
Daniel Veillard7db38712002-02-07 16:39:11 +00001525 if (output == NULL)
1526 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001527 xmlCtxtDumpInitCtxt(&ctxt);
1528 ctxt.output = output;
1529 xmlCtxtDumpDTD(&ctxt, dtd);
Daniel Veillard76821142004-10-09 20:39:04 +00001530 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001531}
1532
Daniel Veillard22cdb842004-10-04 14:09:17 +00001533/************************************************************************
1534 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001535 * Public entry points for checkings *
1536 * *
1537 ************************************************************************/
1538
1539/**
1540 * xmlDebugCheckDocument:
1541 * @output: the FILE * for the output
1542 * @doc: the document
1543 *
1544 * Check the document for potential content problems, and output
1545 * the errors to @output
1546 *
1547 * Returns the number of errors found
1548 */
1549int
1550xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
1551{
1552 xmlDebugCtxt ctxt;
1553
1554 if (output == NULL)
1555 output = stdout;
1556 xmlCtxtDumpInitCtxt(&ctxt);
1557 ctxt.output = output;
1558 ctxt.check = 1;
1559 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001560 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001561 return(ctxt.errors);
1562}
1563
1564/************************************************************************
1565 * *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001566 * Helpers for Shell *
1567 * *
1568 ************************************************************************/
Owen Taylor3473f882001-02-23 17:55:21 +00001569
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001570/**
1571 * xmlLsCountNode:
1572 * @node: the node to count
1573 *
1574 * Count the children of @node.
1575 *
1576 * Returns the number of children of @node.
1577 */
1578int
1579xmlLsCountNode(xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00001580 int ret = 0;
1581 xmlNodePtr list = NULL;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001582
1583 if (node == NULL)
1584 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001585
1586 switch (node->type) {
1587 case XML_ELEMENT_NODE:
1588 list = node->children;
1589 break;
1590 case XML_DOCUMENT_NODE:
1591 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00001592#ifdef LIBXML_DOCB_ENABLED
1593 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001594#endif
1595 list = ((xmlDocPtr) node)->children;
1596 break;
1597 case XML_ATTRIBUTE_NODE:
1598 list = ((xmlAttrPtr) node)->children;
1599 break;
1600 case XML_TEXT_NODE:
1601 case XML_CDATA_SECTION_NODE:
1602 case XML_PI_NODE:
1603 case XML_COMMENT_NODE:
1604 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001605 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001606 }
1607 break;
1608 case XML_ENTITY_REF_NODE:
1609 case XML_DOCUMENT_TYPE_NODE:
1610 case XML_ENTITY_NODE:
1611 case XML_DOCUMENT_FRAG_NODE:
1612 case XML_NOTATION_NODE:
1613 case XML_DTD_NODE:
1614 case XML_ELEMENT_DECL:
1615 case XML_ATTRIBUTE_DECL:
1616 case XML_ENTITY_DECL:
1617 case XML_NAMESPACE_DECL:
1618 case XML_XINCLUDE_START:
1619 case XML_XINCLUDE_END:
1620 ret = 1;
1621 break;
1622 }
1623 for (;list != NULL;ret++)
1624 list = list->next;
1625 return(ret);
1626}
1627
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001628/**
1629 * xmlLsOneNode:
1630 * @output: the FILE * for the output
1631 * @node: the node to dump
1632 *
1633 * Dump to @output the type and name of @node.
1634 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001635void
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001636xmlLsOneNode(FILE *output, xmlNodePtr node) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00001637 if (output == NULL) return;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001638 if (node == NULL) {
1639 fprintf(output, "NULL\n");
1640 return;
1641 }
Owen Taylor3473f882001-02-23 17:55:21 +00001642 switch (node->type) {
1643 case XML_ELEMENT_NODE:
1644 fprintf(output, "-");
1645 break;
1646 case XML_ATTRIBUTE_NODE:
1647 fprintf(output, "a");
1648 break;
1649 case XML_TEXT_NODE:
1650 fprintf(output, "t");
1651 break;
1652 case XML_CDATA_SECTION_NODE:
Daniel Veillard75be0132002-03-13 10:03:35 +00001653 fprintf(output, "C");
Owen Taylor3473f882001-02-23 17:55:21 +00001654 break;
1655 case XML_ENTITY_REF_NODE:
1656 fprintf(output, "e");
1657 break;
1658 case XML_ENTITY_NODE:
1659 fprintf(output, "E");
1660 break;
1661 case XML_PI_NODE:
1662 fprintf(output, "p");
1663 break;
1664 case XML_COMMENT_NODE:
1665 fprintf(output, "c");
1666 break;
1667 case XML_DOCUMENT_NODE:
1668 fprintf(output, "d");
1669 break;
1670 case XML_HTML_DOCUMENT_NODE:
1671 fprintf(output, "h");
1672 break;
1673 case XML_DOCUMENT_TYPE_NODE:
1674 fprintf(output, "T");
1675 break;
1676 case XML_DOCUMENT_FRAG_NODE:
1677 fprintf(output, "F");
1678 break;
1679 case XML_NOTATION_NODE:
1680 fprintf(output, "N");
1681 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001682 case XML_NAMESPACE_DECL:
1683 fprintf(output, "n");
1684 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001685 default:
1686 fprintf(output, "?");
1687 }
Daniel Veillarde6a55192002-01-14 17:11:53 +00001688 if (node->type != XML_NAMESPACE_DECL) {
1689 if (node->properties != NULL)
1690 fprintf(output, "a");
1691 else
1692 fprintf(output, "-");
1693 if (node->nsDef != NULL)
1694 fprintf(output, "n");
1695 else
1696 fprintf(output, "-");
1697 }
Owen Taylor3473f882001-02-23 17:55:21 +00001698
1699 fprintf(output, " %8d ", xmlLsCountNode(node));
1700
1701 switch (node->type) {
1702 case XML_ELEMENT_NODE:
1703 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001704 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001705 break;
1706 case XML_ATTRIBUTE_NODE:
1707 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001708 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001709 break;
1710 case XML_TEXT_NODE:
1711 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001712 xmlDebugDumpString(output, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001713 }
1714 break;
1715 case XML_CDATA_SECTION_NODE:
1716 break;
1717 case XML_ENTITY_REF_NODE:
1718 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001719 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001720 break;
1721 case XML_ENTITY_NODE:
1722 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001723 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001724 break;
1725 case XML_PI_NODE:
1726 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001727 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001728 break;
1729 case XML_COMMENT_NODE:
1730 break;
1731 case XML_DOCUMENT_NODE:
1732 break;
1733 case XML_HTML_DOCUMENT_NODE:
1734 break;
1735 case XML_DOCUMENT_TYPE_NODE:
1736 break;
1737 case XML_DOCUMENT_FRAG_NODE:
1738 break;
1739 case XML_NOTATION_NODE:
1740 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001741 case XML_NAMESPACE_DECL: {
1742 xmlNsPtr ns = (xmlNsPtr) node;
1743
1744 if (ns->prefix == NULL)
William M. Brack13dfa872004-09-18 04:52:08 +00001745 fprintf(output, "default -> %s", (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001746 else
William M. Brack13dfa872004-09-18 04:52:08 +00001747 fprintf(output, "%s -> %s", (char *)ns->prefix,
1748 (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001749 break;
1750 }
Owen Taylor3473f882001-02-23 17:55:21 +00001751 default:
1752 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001753 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001754 }
1755 fprintf(output, "\n");
1756}
1757
Daniel Veillard78d12092001-10-11 09:12:24 +00001758/**
1759 * xmlBoolToText:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001760 * @boolval: a bool to turn into text
Daniel Veillard78d12092001-10-11 09:12:24 +00001761 *
1762 * Convenient way to turn bool into text
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001763 *
1764 * Returns a pointer to either "True" or "False"
1765 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001766const char *
Daniel Veillardebd38c52001-11-01 08:38:12 +00001767xmlBoolToText(int boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001768{
Daniel Veillardebd38c52001-11-01 08:38:12 +00001769 if (boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001770 return("True");
1771 else
1772 return("False");
1773}
1774
Owen Taylor3473f882001-02-23 17:55:21 +00001775/****************************************************************
1776 * *
1777 * The XML shell related functions *
1778 * *
1779 ****************************************************************/
1780
Daniel Veillard78d12092001-10-11 09:12:24 +00001781
1782
Owen Taylor3473f882001-02-23 17:55:21 +00001783/*
1784 * TODO: Improvement/cleanups for the XML shell
1785 * - allow to shell out an editor on a subpart
1786 * - cleanup function registrations (with help) and calling
1787 * - provide registration routines
1788 */
1789
1790/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001791 * xmlShellPrintXPathError:
Daniel Veillard78d12092001-10-11 09:12:24 +00001792 * @errorType: valid xpath error id
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001793 * @arg: the argument that cause xpath to fail
Daniel Veillard78d12092001-10-11 09:12:24 +00001794 *
1795 * Print the xpath error to libxml default error channel
1796 */
1797void
1798xmlShellPrintXPathError(int errorType, const char *arg)
1799{
1800 const char *default_arg = "Result";
1801
1802 if (!arg)
1803 arg = default_arg;
1804
1805 switch (errorType) {
1806 case XPATH_UNDEFINED:
1807 xmlGenericError(xmlGenericErrorContext,
1808 "%s: no such node\n", arg);
1809 break;
1810
1811 case XPATH_BOOLEAN:
1812 xmlGenericError(xmlGenericErrorContext,
1813 "%s is a Boolean\n", arg);
1814 break;
1815 case XPATH_NUMBER:
1816 xmlGenericError(xmlGenericErrorContext,
1817 "%s is a number\n", arg);
1818 break;
1819 case XPATH_STRING:
1820 xmlGenericError(xmlGenericErrorContext,
1821 "%s is a string\n", arg);
1822 break;
1823 case XPATH_POINT:
1824 xmlGenericError(xmlGenericErrorContext,
1825 "%s is a point\n", arg);
1826 break;
1827 case XPATH_RANGE:
1828 xmlGenericError(xmlGenericErrorContext,
1829 "%s is a range\n", arg);
1830 break;
1831 case XPATH_LOCATIONSET:
1832 xmlGenericError(xmlGenericErrorContext,
1833 "%s is a range\n", arg);
1834 break;
1835 case XPATH_USERS:
1836 xmlGenericError(xmlGenericErrorContext,
1837 "%s is user-defined\n", arg);
1838 break;
1839 case XPATH_XSLT_TREE:
1840 xmlGenericError(xmlGenericErrorContext,
1841 "%s is an XSLT value tree\n", arg);
1842 break;
1843 }
Daniel Veillarda82b1822004-11-08 16:24:57 +00001844#if 0
Daniel Veillard78d12092001-10-11 09:12:24 +00001845 xmlGenericError(xmlGenericErrorContext,
1846 "Try casting the result string function (xpath builtin)\n",
1847 arg);
Daniel Veillarda82b1822004-11-08 16:24:57 +00001848#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00001849}
1850
1851
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001852#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001853/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001854 * xmlShellPrintNodeCtxt:
1855 * @ctxt : a non-null shell context
1856 * @node : a non-null node to print to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001857 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001858 * Print node to the output FILE
1859 */
1860static void
1861xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1862{
Daniel Veillard01992e02002-10-09 10:20:30 +00001863 FILE *fp;
1864
1865 if (!node)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001866 return;
Daniel Veillard01992e02002-10-09 10:20:30 +00001867 if (ctxt == NULL)
1868 fp = stdout;
1869 else
1870 fp = ctxt->output;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001871
1872 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001873 xmlDocDump(fp, (xmlDocPtr) node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001874 else if (node->type == XML_ATTRIBUTE_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001875 xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001876 else
Daniel Veillard01992e02002-10-09 10:20:30 +00001877 xmlElemDump(fp, node->doc, node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001878
Daniel Veillard01992e02002-10-09 10:20:30 +00001879 fprintf(fp, "\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00001880}
1881
1882/**
1883 * xmlShellPrintNode:
1884 * @node : a non-null node to print to the output FILE
1885 *
1886 * Print node to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001887 */
1888void
1889xmlShellPrintNode(xmlNodePtr node)
1890{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001891 xmlShellPrintNodeCtxt(NULL, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001892}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001893#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001894
Daniel Veillard78d12092001-10-11 09:12:24 +00001895/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001896 * xmlShellPrintXPathResultCtxt:
1897 * @ctxt: a valid shell context
Daniel Veillard9d06d302002-01-22 18:15:52 +00001898 * @list: a valid result generated by an xpath evaluation
Daniel Veillard78d12092001-10-11 09:12:24 +00001899 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001900 * Prints result to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001901 */
Daniel Veillard321be0c2002-10-08 21:26:42 +00001902static void
1903xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
Daniel Veillard78d12092001-10-11 09:12:24 +00001904{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001905 if (!ctxt)
1906 return;
Daniel Veillard78d12092001-10-11 09:12:24 +00001907
1908 if (list != NULL) {
1909 switch (list->type) {
1910 case XPATH_NODESET:{
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001911#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001912 int indx;
1913
1914 if (list->nodesetval) {
1915 for (indx = 0; indx < list->nodesetval->nodeNr;
1916 indx++) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001917 xmlShellPrintNodeCtxt(ctxt,
1918 list->nodesetval->nodeTab[indx]);
Daniel Veillard78d12092001-10-11 09:12:24 +00001919 }
1920 } else {
1921 xmlGenericError(xmlGenericErrorContext,
1922 "Empty node set\n");
1923 }
1924 break;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001925#else
1926 xmlGenericError(xmlGenericErrorContext,
1927 "Node set\n");
1928#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001929 }
1930 case XPATH_BOOLEAN:
1931 xmlGenericError(xmlGenericErrorContext,
1932 "Is a Boolean:%s\n",
1933 xmlBoolToText(list->boolval));
1934 break;
1935 case XPATH_NUMBER:
1936 xmlGenericError(xmlGenericErrorContext,
1937 "Is a number:%0g\n", list->floatval);
1938 break;
1939 case XPATH_STRING:
1940 xmlGenericError(xmlGenericErrorContext,
1941 "Is a string:%s\n", list->stringval);
1942 break;
1943
1944 default:
1945 xmlShellPrintXPathError(list->type, NULL);
1946 }
1947 }
1948}
1949
1950/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001951 * xmlShellPrintXPathResult:
1952 * @list: a valid result generated by an xpath evaluation
1953 *
1954 * Prints result to the output FILE
1955 */
1956void
1957xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1958{
1959 xmlShellPrintXPathResultCtxt(NULL, list);
1960}
1961
1962/**
Owen Taylor3473f882001-02-23 17:55:21 +00001963 * xmlShellList:
1964 * @ctxt: the shell context
1965 * @arg: unused
1966 * @node: a node
1967 * @node2: unused
1968 *
1969 * Implements the XML shell function "ls"
1970 * Does an Unix like listing of the given node (like a directory)
1971 *
1972 * Returns 0
1973 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001974int
Daniel Veillard321be0c2002-10-08 21:26:42 +00001975xmlShellList(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00001976 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1977 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1978{
Owen Taylor3473f882001-02-23 17:55:21 +00001979 xmlNodePtr cur;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001980 if (!ctxt)
1981 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001982 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001983 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001984 return (0);
1985 }
Owen Taylor3473f882001-02-23 17:55:21 +00001986 if ((node->type == XML_DOCUMENT_NODE) ||
1987 (node->type == XML_HTML_DOCUMENT_NODE)) {
1988 cur = ((xmlDocPtr) node)->children;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001989 } else if (node->type == XML_NAMESPACE_DECL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001990 xmlLsOneNode(ctxt->output, node);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001991 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001992 } else if (node->children != NULL) {
1993 cur = node->children;
1994 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001995 xmlLsOneNode(ctxt->output, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001996 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001997 }
1998 while (cur != NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001999 xmlLsOneNode(ctxt->output, cur);
Daniel Veillard78d12092001-10-11 09:12:24 +00002000 cur = cur->next;
Owen Taylor3473f882001-02-23 17:55:21 +00002001 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002002 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002003}
2004
2005/**
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002006 * xmlShellBase:
2007 * @ctxt: the shell context
2008 * @arg: unused
2009 * @node: a node
2010 * @node2: unused
2011 *
2012 * Implements the XML shell function "base"
2013 * dumps the current XML base of the node
2014 *
2015 * Returns 0
2016 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002017int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002018xmlShellBase(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002019 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2020 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2021{
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002022 xmlChar *base;
Daniel Veillard321be0c2002-10-08 21:26:42 +00002023 if (!ctxt)
2024 return 0;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002025 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002026 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002027 return (0);
2028 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002029
2030 base = xmlNodeGetBase(node->doc, node);
2031
2032 if (base == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002033 fprintf(ctxt->output, " No base found !!!\n");
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002034 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002035 fprintf(ctxt->output, "%s\n", base);
Daniel Veillard78d12092001-10-11 09:12:24 +00002036 xmlFree(base);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002037 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002038 return (0);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002039}
2040
Daniel Veillardb34321c2004-03-04 17:09:47 +00002041#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002042/**
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002043 * xmlShellSetBase:
2044 * @ctxt: the shell context
2045 * @arg: the new base
2046 * @node: a node
2047 * @node2: unused
2048 *
2049 * Implements the XML shell function "setbase"
2050 * change the current XML base of the node
2051 *
2052 * Returns 0
2053 */
2054static int
2055xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2056 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2057 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2058{
2059 xmlNodeSetBase(node, (xmlChar*) arg);
2060 return (0);
2061}
Daniel Veillard2156d432004-03-04 15:59:36 +00002062#endif
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002063
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002064#ifdef LIBXML_XPATH_ENABLED
2065/**
2066 * xmlShellRegisterNamespace:
2067 * @ctxt: the shell context
2068 * @arg: a string in prefix=nsuri format
2069 * @node: unused
2070 * @node2: unused
2071 *
2072 * Implements the XML shell function "setns"
2073 * register/unregister a prefix=namespace pair
2074 * on the XPath context
2075 *
2076 * Returns 0 on success and a negative value otherwise.
2077 */
2078static int
2079xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
2080 xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2081{
2082 xmlChar* nsListDup;
2083 xmlChar* prefix;
2084 xmlChar* href;
2085 xmlChar* next;
2086
2087 nsListDup = xmlStrdup((xmlChar *) arg);
2088 next = nsListDup;
2089 while(next != NULL) {
2090 /* skip spaces */
2091 /*while((*next) == ' ') next++;*/
2092 if((*next) == '\0') break;
2093
2094 /* find prefix */
2095 prefix = next;
2096 next = (xmlChar*)xmlStrchr(next, '=');
2097 if(next == NULL) {
2098 fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
2099 xmlFree(nsListDup);
2100 return(-1);
2101 }
2102 *(next++) = '\0';
2103
2104 /* find href */
2105 href = next;
2106 next = (xmlChar*)xmlStrchr(next, ' ');
2107 if(next != NULL) {
2108 *(next++) = '\0';
2109 }
2110
2111 /* do register namespace */
2112 if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
2113 fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
2114 xmlFree(nsListDup);
2115 return(-1);
2116 }
2117 }
2118
2119 xmlFree(nsListDup);
2120 return(0);
2121}
2122#endif
2123
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002124/**
Daniel Veillard1e208222002-10-22 14:25:25 +00002125 * xmlShellGrep:
2126 * @ctxt: the shell context
2127 * @arg: the string or regular expression to find
2128 * @node: a node
2129 * @node2: unused
2130 *
2131 * Implements the XML shell function "grep"
2132 * dumps informations about the node (namespace, attributes, content).
2133 *
2134 * Returns 0
2135 */
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002136static int
Daniel Veillard1e208222002-10-22 14:25:25 +00002137xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2138 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2139{
2140 if (!ctxt)
2141 return (0);
2142 if (node == NULL)
2143 return (0);
2144 if (arg == NULL)
2145 return (0);
2146#ifdef LIBXML_REGEXP_ENABLED
2147 if ((xmlStrchr((xmlChar *) arg, '?')) ||
2148 (xmlStrchr((xmlChar *) arg, '*')) ||
2149 (xmlStrchr((xmlChar *) arg, '.')) ||
2150 (xmlStrchr((xmlChar *) arg, '['))) {
2151 }
2152#endif
2153 while (node != NULL) {
2154 if (node->type == XML_COMMENT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002155 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002156
2157 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
2158 xmlShellList(ctxt, NULL, node, NULL);
2159 }
2160 } else if (node->type == XML_TEXT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002161 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002162
2163 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002164 xmlShellList(ctxt, NULL, node->parent, NULL);
Daniel Veillard1e208222002-10-22 14:25:25 +00002165 }
2166 }
2167
2168 /*
2169 * Browse the full subtree, deep first
2170 */
2171
2172 if ((node->type == XML_DOCUMENT_NODE) ||
2173 (node->type == XML_HTML_DOCUMENT_NODE)) {
2174 node = ((xmlDocPtr) node)->children;
2175 } else if ((node->children != NULL)
2176 && (node->type != XML_ENTITY_REF_NODE)) {
2177 /* deep first */
2178 node = node->children;
2179 } else if (node->next != NULL) {
2180 /* then siblings */
2181 node = node->next;
2182 } else {
2183 /* go up to parents->next if needed */
2184 while (node != NULL) {
2185 if (node->parent != NULL) {
2186 node = node->parent;
2187 }
2188 if (node->next != NULL) {
2189 node = node->next;
2190 break;
2191 }
2192 if (node->parent == NULL) {
2193 node = NULL;
2194 break;
2195 }
2196 }
2197 }
2198 }
2199 return (0);
2200}
2201
2202/**
Owen Taylor3473f882001-02-23 17:55:21 +00002203 * xmlShellDir:
2204 * @ctxt: the shell context
2205 * @arg: unused
2206 * @node: a node
2207 * @node2: unused
2208 *
2209 * Implements the XML shell function "dir"
2210 * dumps informations about the node (namespace, attributes, content).
2211 *
2212 * Returns 0
2213 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002214int
2215xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2216 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2217 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2218{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002219 if (!ctxt)
2220 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002221 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002222 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002223 return (0);
2224 }
Owen Taylor3473f882001-02-23 17:55:21 +00002225 if ((node->type == XML_DOCUMENT_NODE) ||
2226 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002227 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
Owen Taylor3473f882001-02-23 17:55:21 +00002228 } else if (node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002229 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002230 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002231 xmlDebugDumpOneNode(ctxt->output, node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002232 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002233 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002234}
2235
Daniel Veillard29b17482004-08-16 00:39:03 +00002236/**
2237 * xmlShellSetContent:
2238 * @ctxt: the shell context
2239 * @value: the content as a string
2240 * @node: a node
2241 * @node2: unused
2242 *
2243 * Implements the XML shell function "dir"
2244 * dumps informations about the node (namespace, attributes, content).
2245 *
2246 * Returns 0
2247 */
2248static int
2249xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2250 char *value, xmlNodePtr node,
2251 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2252{
2253 xmlNodePtr results;
2254 xmlParserErrors ret;
2255
2256 if (!ctxt)
2257 return (0);
2258 if (node == NULL) {
2259 fprintf(ctxt->output, "NULL\n");
2260 return (0);
2261 }
2262 if (value == NULL) {
2263 fprintf(ctxt->output, "NULL\n");
2264 return (0);
2265 }
2266
2267 ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
2268 if (ret == XML_ERR_OK) {
2269 if (node->children != NULL) {
2270 xmlFreeNodeList(node->children);
2271 node->children = NULL;
2272 node->last = NULL;
2273 }
2274 xmlAddChildList(node, results);
2275 } else {
2276 fprintf(ctxt->output, "failed to parse content\n");
2277 }
2278 return (0);
2279}
2280
Daniel Veillard522bc602004-02-21 11:53:09 +00002281#ifdef LIBXML_SCHEMAS_ENABLED
2282/**
2283 * xmlShellRNGValidate:
2284 * @ctxt: the shell context
2285 * @schemas: the path to the Relax-NG schemas
2286 * @node: a node
2287 * @node2: unused
2288 *
2289 * Implements the XML shell function "relaxng"
2290 * validating the instance against a Relax-NG schemas
2291 *
2292 * Returns 0
2293 */
2294static int
2295xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
2296 xmlNodePtr node ATTRIBUTE_UNUSED,
2297 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2298{
2299 xmlRelaxNGPtr relaxngschemas;
2300 xmlRelaxNGParserCtxtPtr ctxt;
2301 xmlRelaxNGValidCtxtPtr vctxt;
2302 int ret;
2303
2304 ctxt = xmlRelaxNGNewParserCtxt(schemas);
2305 xmlRelaxNGSetParserErrors(ctxt,
2306 (xmlRelaxNGValidityErrorFunc) fprintf,
2307 (xmlRelaxNGValidityWarningFunc) fprintf,
2308 stderr);
2309 relaxngschemas = xmlRelaxNGParse(ctxt);
2310 xmlRelaxNGFreeParserCtxt(ctxt);
2311 if (relaxngschemas == NULL) {
2312 xmlGenericError(xmlGenericErrorContext,
2313 "Relax-NG schema %s failed to compile\n", schemas);
2314 return(-1);
2315 }
2316 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2317 xmlRelaxNGSetValidErrors(vctxt,
2318 (xmlRelaxNGValidityErrorFunc) fprintf,
2319 (xmlRelaxNGValidityWarningFunc) fprintf,
2320 stderr);
2321 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2322 if (ret == 0) {
2323 fprintf(stderr, "%s validates\n", sctxt->filename);
2324 } else if (ret > 0) {
2325 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2326 } else {
2327 fprintf(stderr, "%s validation generated an internal error\n",
2328 sctxt->filename);
2329 }
2330 xmlRelaxNGFreeValidCtxt(vctxt);
2331 if (relaxngschemas != NULL)
2332 xmlRelaxNGFree(relaxngschemas);
2333 return(0);
2334}
2335#endif
2336
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002337#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002338/**
2339 * xmlShellCat:
2340 * @ctxt: the shell context
2341 * @arg: unused
2342 * @node: a node
2343 * @node2: unused
2344 *
2345 * Implements the XML shell function "cat"
2346 * dumps the serialization node content (XML or HTML).
2347 *
2348 * Returns 0
2349 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002350int
2351xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2352 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2353{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002354 if (!ctxt)
2355 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002356 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002357 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002358 return (0);
2359 }
Owen Taylor3473f882001-02-23 17:55:21 +00002360 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2361#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002362 if (node->type == XML_HTML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002363 htmlDocDump(ctxt->output, (htmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002364 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002365 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002366#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002367 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002368 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002369 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002370 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002371#endif /* LIBXML_HTML_ENABLED */
2372 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00002373 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002374 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002375 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002376 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002377 }
Daniel Veillard321be0c2002-10-08 21:26:42 +00002378 fprintf(ctxt->output, "\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002379 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002380}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002381#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002382
2383/**
2384 * xmlShellLoad:
2385 * @ctxt: the shell context
2386 * @filename: the file name
2387 * @node: unused
2388 * @node2: unused
2389 *
2390 * Implements the XML shell function "load"
2391 * loads a new document specified by the filename
2392 *
2393 * Returns 0 or -1 if loading failed
2394 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002395int
2396xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2397 xmlNodePtr node ATTRIBUTE_UNUSED,
2398 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2399{
Owen Taylor3473f882001-02-23 17:55:21 +00002400 xmlDocPtr doc;
2401 int html = 0;
2402
Daniel Veillarda82b1822004-11-08 16:24:57 +00002403 if ((ctxt == NULL) || (filename == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002404 if (ctxt->doc != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002405 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00002406
2407 if (html) {
2408#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002409 doc = htmlParseFile(filename, NULL);
2410#else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002411 fprintf(ctxt->output, "HTML support not compiled in\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002412 doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002413#endif /* LIBXML_HTML_ENABLED */
2414 } else {
Daniel Veillardebe25d42004-03-25 09:35:49 +00002415 doc = xmlReadFile(filename,NULL,0);
Owen Taylor3473f882001-02-23 17:55:21 +00002416 }
2417 if (doc != NULL) {
2418 if (ctxt->loaded == 1) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002419 xmlFreeDoc(ctxt->doc);
2420 }
2421 ctxt->loaded = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002422#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002423 xmlXPathFreeContext(ctxt->pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00002424#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002425 xmlFree(ctxt->filename);
2426 ctxt->doc = doc;
2427 ctxt->node = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002428#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002429 ctxt->pctxt = xmlXPathNewContext(doc);
Owen Taylor3473f882001-02-23 17:55:21 +00002430#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard85095e22003-04-23 13:56:44 +00002431 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002432 } else
Daniel Veillard78d12092001-10-11 09:12:24 +00002433 return (-1);
2434 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002435}
2436
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002437#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002438/**
2439 * xmlShellWrite:
2440 * @ctxt: the shell context
2441 * @filename: the file name
2442 * @node: a node in the tree
2443 * @node2: unused
2444 *
2445 * Implements the XML shell function "write"
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002446 * Write the current node to the filename, it saves the serialization
Owen Taylor3473f882001-02-23 17:55:21 +00002447 * of the subtree under the @node specified
2448 *
2449 * Returns 0 or -1 in case of error
2450 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002451int
Owen Taylor3473f882001-02-23 17:55:21 +00002452xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Daniel Veillard78d12092001-10-11 09:12:24 +00002453 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2454{
Owen Taylor3473f882001-02-23 17:55:21 +00002455 if (node == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002456 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002457 if ((filename == NULL) || (filename[0] == 0)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002458 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002459 }
2460#ifdef W_OK
2461 if (access((char *) filename, W_OK)) {
2462 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002463 "Cannot write to %s\n", filename);
2464 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002465 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002466#endif
2467 switch (node->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002468 case XML_DOCUMENT_NODE:
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 }
2474 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002475 case XML_HTML_DOCUMENT_NODE:
2476#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002477 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2478 xmlGenericError(xmlGenericErrorContext,
2479 "Failed to write to %s\n", filename);
2480 return (-1);
2481 }
Owen Taylor3473f882001-02-23 17:55:21 +00002482#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002483 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2484 xmlGenericError(xmlGenericErrorContext,
2485 "Failed to write to %s\n", filename);
2486 return (-1);
2487 }
Owen Taylor3473f882001-02-23 17:55:21 +00002488#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002489 break;
2490 default:{
2491 FILE *f;
Owen Taylor3473f882001-02-23 17:55:21 +00002492
Daniel Veillard78d12092001-10-11 09:12:24 +00002493 f = fopen((char *) filename, "w");
2494 if (f == NULL) {
2495 xmlGenericError(xmlGenericErrorContext,
2496 "Failed to write to %s\n", filename);
2497 return (-1);
2498 }
2499 xmlElemDump(f, ctxt->doc, node);
2500 fclose(f);
2501 }
Owen Taylor3473f882001-02-23 17:55:21 +00002502 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002503 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002504}
2505
2506/**
2507 * xmlShellSave:
2508 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002509 * @filename: the file name (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002510 * @node: unused
2511 * @node2: unused
2512 *
2513 * Implements the XML shell function "save"
2514 * Write the current document to the filename, or it's original name
2515 *
2516 * Returns 0 or -1 in case of error
2517 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002518int
2519xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2520 xmlNodePtr node ATTRIBUTE_UNUSED,
2521 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2522{
Daniel Veillarda82b1822004-11-08 16:24:57 +00002523 if ((ctxt == NULL) || (ctxt->doc == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002524 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002525 if ((filename == NULL) || (filename[0] == 0))
2526 filename = ctxt->filename;
Daniel Veillarda82b1822004-11-08 16:24:57 +00002527 if (filename == NULL)
2528 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002529#ifdef W_OK
2530 if (access((char *) filename, W_OK)) {
2531 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002532 "Cannot save to %s\n", filename);
2533 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002534 }
2535#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002536 switch (ctxt->doc->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002537 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002538 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2539 xmlGenericError(xmlGenericErrorContext,
2540 "Failed to save to %s\n", filename);
2541 }
2542 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002543 case XML_HTML_DOCUMENT_NODE:
2544#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002545 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2546 xmlGenericError(xmlGenericErrorContext,
2547 "Failed to save to %s\n", filename);
2548 }
Owen Taylor3473f882001-02-23 17:55:21 +00002549#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002550 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2551 xmlGenericError(xmlGenericErrorContext,
2552 "Failed to save to %s\n", filename);
2553 }
Owen Taylor3473f882001-02-23 17:55:21 +00002554#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002555 break;
2556 default:
2557 xmlGenericError(xmlGenericErrorContext,
2558 "To save to subparts of a document use the 'write' command\n");
2559 return (-1);
2560
Owen Taylor3473f882001-02-23 17:55:21 +00002561 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002562 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002563}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002564#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002565
Daniel Veillardf54cd532004-02-25 11:52:31 +00002566#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002567/**
2568 * xmlShellValidate:
2569 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002570 * @dtd: the DTD URI (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002571 * @node: unused
2572 * @node2: unused
2573 *
2574 * Implements the XML shell function "validate"
2575 * Validate the document, if a DTD path is provided, then the validation
2576 * is done against the given DTD.
2577 *
2578 * Returns 0 or -1 in case of error
2579 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002580int
2581xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2582 xmlNodePtr node ATTRIBUTE_UNUSED,
2583 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2584{
Owen Taylor3473f882001-02-23 17:55:21 +00002585 xmlValidCtxt vctxt;
2586 int res = -1;
2587
Daniel Veillarda82b1822004-11-08 16:24:57 +00002588 if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002589 vctxt.userData = stderr;
2590 vctxt.error = (xmlValidityErrorFunc) fprintf;
2591 vctxt.warning = (xmlValidityWarningFunc) fprintf;
2592
2593 if ((dtd == NULL) || (dtd[0] == 0)) {
2594 res = xmlValidateDocument(&vctxt, ctxt->doc);
2595 } else {
2596 xmlDtdPtr subset;
2597
Daniel Veillard78d12092001-10-11 09:12:24 +00002598 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2599 if (subset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002600 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2601
Daniel Veillard78d12092001-10-11 09:12:24 +00002602 xmlFreeDtd(subset);
2603 }
Owen Taylor3473f882001-02-23 17:55:21 +00002604 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002605 return (res);
Owen Taylor3473f882001-02-23 17:55:21 +00002606}
Daniel Veillardf54cd532004-02-25 11:52:31 +00002607#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002608
2609/**
2610 * xmlShellDu:
2611 * @ctxt: the shell context
2612 * @arg: unused
2613 * @tree: a node defining a subtree
2614 * @node2: unused
2615 *
2616 * Implements the XML shell function "du"
2617 * show the structure of the subtree under node @tree
2618 * If @tree is null, the command works on the current node.
2619 *
2620 * Returns 0 or -1 in case of error
2621 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002622int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002623xmlShellDu(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002624 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2625 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2626{
Owen Taylor3473f882001-02-23 17:55:21 +00002627 xmlNodePtr node;
Daniel Veillard78d12092001-10-11 09:12:24 +00002628 int indent = 0, i;
Owen Taylor3473f882001-02-23 17:55:21 +00002629
Daniel Veillard321be0c2002-10-08 21:26:42 +00002630 if (!ctxt)
2631 return (-1);
2632
Daniel Veillard78d12092001-10-11 09:12:24 +00002633 if (tree == NULL)
2634 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002635 node = tree;
2636 while (node != NULL) {
2637 if ((node->type == XML_DOCUMENT_NODE) ||
2638 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002639 fprintf(ctxt->output, "/\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002640 } else if (node->type == XML_ELEMENT_NODE) {
2641 for (i = 0; i < indent; i++)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002642 fprintf(ctxt->output, " ");
2643 fprintf(ctxt->output, "%s\n", node->name);
Daniel Veillard78d12092001-10-11 09:12:24 +00002644 } else {
2645 }
Owen Taylor3473f882001-02-23 17:55:21 +00002646
Daniel Veillard78d12092001-10-11 09:12:24 +00002647 /*
2648 * Browse the full subtree, deep first
2649 */
Owen Taylor3473f882001-02-23 17:55:21 +00002650
2651 if ((node->type == XML_DOCUMENT_NODE) ||
2652 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002653 node = ((xmlDocPtr) node)->children;
2654 } else if ((node->children != NULL)
2655 && (node->type != XML_ENTITY_REF_NODE)) {
2656 /* deep first */
2657 node = node->children;
2658 indent++;
2659 } else if ((node != tree) && (node->next != NULL)) {
2660 /* then siblings */
2661 node = node->next;
2662 } else if (node != tree) {
2663 /* go up to parents->next if needed */
2664 while (node != tree) {
2665 if (node->parent != NULL) {
2666 node = node->parent;
2667 indent--;
2668 }
2669 if ((node != tree) && (node->next != NULL)) {
2670 node = node->next;
2671 break;
2672 }
2673 if (node->parent == NULL) {
2674 node = NULL;
2675 break;
2676 }
2677 if (node == tree) {
2678 node = NULL;
2679 break;
2680 }
2681 }
2682 /* exit condition */
2683 if (node == tree)
2684 node = NULL;
2685 } else
2686 node = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002687 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002688 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002689}
2690
2691/**
2692 * xmlShellPwd:
2693 * @ctxt: the shell context
2694 * @buffer: the output buffer
Daniel Veillard9d06d302002-01-22 18:15:52 +00002695 * @node: a node
Owen Taylor3473f882001-02-23 17:55:21 +00002696 * @node2: unused
2697 *
2698 * Implements the XML shell function "pwd"
2699 * Show the full path from the root to the node, if needed building
2700 * thumblers when similar elements exists at a given ancestor level.
2701 * The output is compatible with XPath commands.
2702 *
2703 * Returns 0 or -1 in case of error
2704 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002705int
2706xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2707 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2708{
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002709 xmlChar *path;
Owen Taylor3473f882001-02-23 17:55:21 +00002710
Daniel Veillarda82b1822004-11-08 16:24:57 +00002711 if ((node == NULL) || (buffer == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002712 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002713
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002714 path = xmlGetNodePath(node);
2715 if (path == NULL)
2716 return (-1);
2717
2718 /*
2719 * This test prevents buffer overflow, because this routine
2720 * is only called by xmlShell, in which the second argument is
2721 * 500 chars long.
2722 * It is a dirty hack before a cleaner solution is found.
2723 * Documentation should mention that the second argument must
2724 * be at least 500 chars long, and could be stripped if too long.
2725 */
2726 snprintf(buffer, 499, "%s", path);
2727 buffer[499] = '0';
2728 xmlFree(path);
2729
Daniel Veillard78d12092001-10-11 09:12:24 +00002730 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002731}
2732
2733/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002734 * xmlShell:
Owen Taylor3473f882001-02-23 17:55:21 +00002735 * @doc: the initial document
2736 * @filename: the output buffer
2737 * @input: the line reading function
Daniel Veillard321be0c2002-10-08 21:26:42 +00002738 * @output: the output FILE*, defaults to stdout if NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002739 *
2740 * Implements the XML shell
2741 * This allow to load, validate, view, modify and save a document
2742 * using a environment similar to a UNIX commandline.
2743 */
2744void
2745xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Daniel Veillard78d12092001-10-11 09:12:24 +00002746 FILE * output)
2747{
Owen Taylor3473f882001-02-23 17:55:21 +00002748 char prompt[500] = "/ > ";
2749 char *cmdline = NULL, *cur;
2750 int nbargs;
2751 char command[100];
2752 char arg[400];
2753 int i;
2754 xmlShellCtxtPtr ctxt;
2755 xmlXPathObjectPtr list;
2756
2757 if (doc == NULL)
2758 return;
2759 if (filename == NULL)
2760 return;
2761 if (input == NULL)
2762 return;
2763 if (output == NULL)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002764 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00002765 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Daniel Veillard78d12092001-10-11 09:12:24 +00002766 if (ctxt == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00002767 return;
2768 ctxt->loaded = 0;
2769 ctxt->doc = doc;
2770 ctxt->input = input;
2771 ctxt->output = output;
2772 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Daniel Veillard78d12092001-10-11 09:12:24 +00002773 ctxt->node = (xmlNodePtr) ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002774
2775#ifdef LIBXML_XPATH_ENABLED
2776 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2777 if (ctxt->pctxt == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002778 xmlFree(ctxt);
2779 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002780 }
2781#endif /* LIBXML_XPATH_ENABLED */
2782 while (1) {
2783 if (ctxt->node == (xmlNodePtr) ctxt->doc)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002784 snprintf(prompt, sizeof(prompt), "%s > ", "/");
Daniel Veillard7a985a12003-07-06 17:57:42 +00002785 else if ((ctxt->node != NULL) && (ctxt->node->name))
Daniel Veillard78d12092001-10-11 09:12:24 +00002786 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002787 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002788 snprintf(prompt, sizeof(prompt), "? > ");
Owen Taylor3473f882001-02-23 17:55:21 +00002789 prompt[sizeof(prompt) - 1] = 0;
2790
Daniel Veillard78d12092001-10-11 09:12:24 +00002791 /*
2792 * Get a new command line
2793 */
Owen Taylor3473f882001-02-23 17:55:21 +00002794 cmdline = ctxt->input(prompt);
Daniel Veillard78d12092001-10-11 09:12:24 +00002795 if (cmdline == NULL)
2796 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002797
Daniel Veillard78d12092001-10-11 09:12:24 +00002798 /*
2799 * Parse the command itself
2800 */
2801 cur = cmdline;
2802 nbargs = 0;
2803 while ((*cur == ' ') || (*cur == '\t'))
2804 cur++;
2805 i = 0;
2806 while ((*cur != ' ') && (*cur != '\t') &&
2807 (*cur != '\n') && (*cur != '\r')) {
2808 if (*cur == 0)
2809 break;
2810 command[i++] = *cur++;
2811 }
2812 command[i] = 0;
2813 if (i == 0)
2814 continue;
2815 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002816
Daniel Veillard78d12092001-10-11 09:12:24 +00002817 /*
2818 * Parse the argument
2819 */
2820 while ((*cur == ' ') || (*cur == '\t'))
2821 cur++;
2822 i = 0;
2823 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2824 if (*cur == 0)
2825 break;
2826 arg[i++] = *cur++;
2827 }
2828 arg[i] = 0;
2829 if (i != 0)
2830 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002831
Daniel Veillard78d12092001-10-11 09:12:24 +00002832 /*
2833 * start interpreting the command
2834 */
Owen Taylor3473f882001-02-23 17:55:21 +00002835 if (!strcmp(command, "exit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002836 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002837 if (!strcmp(command, "quit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002838 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002839 if (!strcmp(command, "bye"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002840 break;
Daniel Veillard5004f422001-11-08 13:53:05 +00002841 if (!strcmp(command, "help")) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002842 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2843 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2844 fprintf(ctxt->output, "\tbye leave shell\n");
2845 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2846 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2847 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2848 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2849 fprintf(ctxt->output, "\texit leave shell\n");
2850 fprintf(ctxt->output, "\thelp display this help\n");
2851 fprintf(ctxt->output, "\tfree display memory usage\n");
2852 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2853 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00002854 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 +00002855#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002856 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002857 fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2858 fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002859#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard321be0c2002-10-08 21:26:42 +00002860 fprintf(ctxt->output, "\tpwd display current working directory\n");
2861 fprintf(ctxt->output, "\tquit leave shell\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002862#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002863 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00002864 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002865#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf54cd532004-02-25 11:52:31 +00002866#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002867 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002868#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard522bc602004-02-21 11:53:09 +00002869#ifdef LIBXML_SCHEMAS_ENABLED
2870 fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n");
2871#endif
Daniel Veillard1e208222002-10-22 14:25:25 +00002872 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002873#ifdef LIBXML_VALID_ENABLED
Daniel Veillard5004f422001-11-08 13:53:05 +00002874 } else if (!strcmp(command, "validate")) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002875 xmlShellValidate(ctxt, arg, NULL, NULL);
Daniel Veillardf54cd532004-02-25 11:52:31 +00002876#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002877 } else if (!strcmp(command, "load")) {
2878 xmlShellLoad(ctxt, arg, NULL, NULL);
Daniel Veillard522bc602004-02-21 11:53:09 +00002879#ifdef LIBXML_SCHEMAS_ENABLED
2880 } else if (!strcmp(command, "relaxng")) {
2881 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2882#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002883#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002884 } else if (!strcmp(command, "save")) {
2885 xmlShellSave(ctxt, arg, NULL, NULL);
2886 } else if (!strcmp(command, "write")) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00002887 if ((arg == NULL) || (arg[0] == 0))
2888 xmlGenericError(xmlGenericErrorContext,
2889 "Write command requires a filename argument\n");
2890 else
2891 xmlShellWrite(ctxt, arg, NULL, NULL);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002892#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard1e208222002-10-22 14:25:25 +00002893 } else if (!strcmp(command, "grep")) {
2894 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
Daniel Veillard78d12092001-10-11 09:12:24 +00002895 } else if (!strcmp(command, "free")) {
2896 if (arg[0] == 0) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002897 xmlMemShow(ctxt->output, 0);
Daniel Veillard78d12092001-10-11 09:12:24 +00002898 } else {
2899 int len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002900
Daniel Veillard78d12092001-10-11 09:12:24 +00002901 sscanf(arg, "%d", &len);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002902 xmlMemShow(ctxt->output, len);
Daniel Veillard78d12092001-10-11 09:12:24 +00002903 }
2904 } else if (!strcmp(command, "pwd")) {
2905 char dir[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002906
Daniel Veillard78d12092001-10-11 09:12:24 +00002907 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
Daniel Veillard321be0c2002-10-08 21:26:42 +00002908 fprintf(ctxt->output, "%s\n", dir);
Daniel Veillard78d12092001-10-11 09:12:24 +00002909 } else if (!strcmp(command, "du")) {
2910 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2911 } else if (!strcmp(command, "base")) {
2912 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
Daniel Veillard29b17482004-08-16 00:39:03 +00002913 } else if (!strcmp(command, "set")) {
2914 xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002915#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002916 } else if (!strcmp(command, "setns")) {
2917 if (arg[0] == 0) {
2918 xmlGenericError(xmlGenericErrorContext,
2919 "setns: prefix=[nsuri] required\n");
2920 } else {
2921 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
2922 }
Daniel Veillard2070c482002-01-22 22:12:19 +00002923 } else if (!strcmp(command, "xpath")) {
2924 if (arg[0] == 0) {
2925 xmlGenericError(xmlGenericErrorContext,
2926 "xpath: expression required\n");
2927 } else {
2928 ctxt->pctxt->node = ctxt->node;
2929 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002930 xmlXPathDebugDumpObject(ctxt->output, list, 0);
Daniel Veillard2070c482002-01-22 22:12:19 +00002931 xmlXPathFreeObject(list);
2932 }
2933#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard2156d432004-03-04 15:59:36 +00002934#ifdef LIBXML_TREE_ENABLED
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002935 } else if (!strcmp(command, "setbase")) {
2936 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2156d432004-03-04 15:59:36 +00002937#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002938 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
2939 int dir = (!strcmp(command, "dir"));
2940
2941 if (arg[0] == 0) {
2942 if (dir)
2943 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
2944 else
2945 xmlShellList(ctxt, NULL, ctxt->node, NULL);
2946 } else {
2947 ctxt->pctxt->node = ctxt->node;
Daniel Veillard61d80a22001-04-27 17:13:01 +00002948#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002949 ctxt->pctxt->node = ctxt->node;
2950 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2951#else
2952 list = NULL;
2953#endif /* LIBXML_XPATH_ENABLED */
2954 if (list != NULL) {
2955 switch (list->type) {
2956 case XPATH_UNDEFINED:
2957 xmlGenericError(xmlGenericErrorContext,
2958 "%s: no such node\n", arg);
2959 break;
2960 case XPATH_NODESET:{
2961 int indx;
2962
Daniel Veillarda6825e82001-11-07 13:33:59 +00002963 if (list->nodesetval == NULL)
2964 break;
2965
Daniel Veillard78d12092001-10-11 09:12:24 +00002966 for (indx = 0;
2967 indx < list->nodesetval->nodeNr;
2968 indx++) {
2969 if (dir)
2970 xmlShellDir(ctxt, NULL,
2971 list->nodesetval->
2972 nodeTab[indx], NULL);
2973 else
2974 xmlShellList(ctxt, NULL,
2975 list->nodesetval->
2976 nodeTab[indx], NULL);
2977 }
2978 break;
2979 }
2980 case XPATH_BOOLEAN:
2981 xmlGenericError(xmlGenericErrorContext,
2982 "%s is a Boolean\n", arg);
2983 break;
2984 case XPATH_NUMBER:
2985 xmlGenericError(xmlGenericErrorContext,
2986 "%s is a number\n", arg);
2987 break;
2988 case XPATH_STRING:
2989 xmlGenericError(xmlGenericErrorContext,
2990 "%s is a string\n", arg);
2991 break;
2992 case XPATH_POINT:
2993 xmlGenericError(xmlGenericErrorContext,
2994 "%s is a point\n", arg);
2995 break;
2996 case XPATH_RANGE:
2997 xmlGenericError(xmlGenericErrorContext,
2998 "%s is a range\n", arg);
2999 break;
3000 case XPATH_LOCATIONSET:
3001 xmlGenericError(xmlGenericErrorContext,
3002 "%s is a range\n", arg);
3003 break;
3004 case XPATH_USERS:
3005 xmlGenericError(xmlGenericErrorContext,
3006 "%s is user-defined\n", arg);
3007 break;
3008 case XPATH_XSLT_TREE:
3009 xmlGenericError(xmlGenericErrorContext,
3010 "%s is an XSLT value tree\n",
3011 arg);
3012 break;
3013 }
3014#ifdef LIBXML_XPATH_ENABLED
3015 xmlXPathFreeObject(list);
Daniel Veillard61d80a22001-04-27 17:13:01 +00003016#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00003017 } else {
3018 xmlGenericError(xmlGenericErrorContext,
3019 "%s: no such node\n", arg);
3020 }
3021 ctxt->pctxt->node = NULL;
3022 }
3023 } else if (!strcmp(command, "cd")) {
3024 if (arg[0] == 0) {
3025 ctxt->node = (xmlNodePtr) ctxt->doc;
3026 } else {
3027#ifdef LIBXML_XPATH_ENABLED
3028 ctxt->pctxt->node = ctxt->node;
3029 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3030#else
3031 list = NULL;
3032#endif /* LIBXML_XPATH_ENABLED */
3033 if (list != NULL) {
3034 switch (list->type) {
3035 case XPATH_UNDEFINED:
3036 xmlGenericError(xmlGenericErrorContext,
3037 "%s: no such node\n", arg);
3038 break;
3039 case XPATH_NODESET:
Daniel Veillarda6825e82001-11-07 13:33:59 +00003040 if (list->nodesetval != NULL) {
3041 if (list->nodesetval->nodeNr == 1) {
3042 ctxt->node = list->nodesetval->nodeTab[0];
Daniel Veillard7a985a12003-07-06 17:57:42 +00003043 if ((ctxt->node != NULL) &&
3044 (ctxt->node->type ==
3045 XML_NAMESPACE_DECL)) {
3046 xmlGenericError(xmlGenericErrorContext,
3047 "cannot cd to namespace\n");
3048 ctxt->node = NULL;
3049 }
Daniel Veillarda6825e82001-11-07 13:33:59 +00003050 } else
3051 xmlGenericError(xmlGenericErrorContext,
3052 "%s is a %d Node Set\n",
3053 arg,
3054 list->nodesetval->nodeNr);
Daniel Veillard78d12092001-10-11 09:12:24 +00003055 } else
3056 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda6825e82001-11-07 13:33:59 +00003057 "%s is an empty Node Set\n",
3058 arg);
Daniel Veillard78d12092001-10-11 09:12:24 +00003059 break;
3060 case XPATH_BOOLEAN:
3061 xmlGenericError(xmlGenericErrorContext,
3062 "%s is a Boolean\n", arg);
3063 break;
3064 case XPATH_NUMBER:
3065 xmlGenericError(xmlGenericErrorContext,
3066 "%s is a number\n", arg);
3067 break;
3068 case XPATH_STRING:
3069 xmlGenericError(xmlGenericErrorContext,
3070 "%s is a string\n", arg);
3071 break;
3072 case XPATH_POINT:
3073 xmlGenericError(xmlGenericErrorContext,
3074 "%s is a point\n", arg);
3075 break;
3076 case XPATH_RANGE:
3077 xmlGenericError(xmlGenericErrorContext,
3078 "%s is a range\n", arg);
3079 break;
3080 case XPATH_LOCATIONSET:
3081 xmlGenericError(xmlGenericErrorContext,
3082 "%s is a range\n", arg);
3083 break;
3084 case XPATH_USERS:
3085 xmlGenericError(xmlGenericErrorContext,
3086 "%s is user-defined\n", arg);
3087 break;
3088 case XPATH_XSLT_TREE:
3089 xmlGenericError(xmlGenericErrorContext,
3090 "%s is an XSLT value tree\n",
3091 arg);
3092 break;
3093 }
3094#ifdef LIBXML_XPATH_ENABLED
3095 xmlXPathFreeObject(list);
3096#endif
3097 } else {
3098 xmlGenericError(xmlGenericErrorContext,
3099 "%s: no such node\n", arg);
3100 }
3101 ctxt->pctxt->node = NULL;
3102 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003103#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00003104 } else if (!strcmp(command, "cat")) {
3105 if (arg[0] == 0) {
3106 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3107 } else {
3108 ctxt->pctxt->node = ctxt->node;
3109#ifdef LIBXML_XPATH_ENABLED
3110 ctxt->pctxt->node = ctxt->node;
3111 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3112#else
3113 list = NULL;
3114#endif /* LIBXML_XPATH_ENABLED */
3115 if (list != NULL) {
3116 switch (list->type) {
3117 case XPATH_UNDEFINED:
3118 xmlGenericError(xmlGenericErrorContext,
3119 "%s: no such node\n", arg);
3120 break;
3121 case XPATH_NODESET:{
3122 int indx;
3123
Daniel Veillarda6825e82001-11-07 13:33:59 +00003124 if (list->nodesetval == NULL)
3125 break;
3126
Daniel Veillard78d12092001-10-11 09:12:24 +00003127 for (indx = 0;
3128 indx < list->nodesetval->nodeNr;
3129 indx++) {
3130 if (i > 0)
Daniel Veillard321be0c2002-10-08 21:26:42 +00003131 fprintf(ctxt->output, " -------\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00003132 xmlShellCat(ctxt, NULL,
3133 list->nodesetval->
3134 nodeTab[indx], NULL);
3135 }
3136 break;
3137 }
3138 case XPATH_BOOLEAN:
3139 xmlGenericError(xmlGenericErrorContext,
3140 "%s is a Boolean\n", arg);
3141 break;
3142 case XPATH_NUMBER:
3143 xmlGenericError(xmlGenericErrorContext,
3144 "%s is a number\n", arg);
3145 break;
3146 case XPATH_STRING:
3147 xmlGenericError(xmlGenericErrorContext,
3148 "%s is a string\n", arg);
3149 break;
3150 case XPATH_POINT:
3151 xmlGenericError(xmlGenericErrorContext,
3152 "%s is a point\n", arg);
3153 break;
3154 case XPATH_RANGE:
3155 xmlGenericError(xmlGenericErrorContext,
3156 "%s is a range\n", arg);
3157 break;
3158 case XPATH_LOCATIONSET:
3159 xmlGenericError(xmlGenericErrorContext,
3160 "%s is a range\n", arg);
3161 break;
3162 case XPATH_USERS:
3163 xmlGenericError(xmlGenericErrorContext,
3164 "%s is user-defined\n", arg);
3165 break;
3166 case XPATH_XSLT_TREE:
3167 xmlGenericError(xmlGenericErrorContext,
3168 "%s is an XSLT value tree\n",
3169 arg);
3170 break;
3171 }
3172#ifdef LIBXML_XPATH_ENABLED
3173 xmlXPathFreeObject(list);
3174#endif
3175 } else {
3176 xmlGenericError(xmlGenericErrorContext,
3177 "%s: no such node\n", arg);
3178 }
3179 ctxt->pctxt->node = NULL;
3180 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003181#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00003182 } else {
3183 xmlGenericError(xmlGenericErrorContext,
3184 "Unknown command %s\n", command);
3185 }
3186 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003187 }
3188#ifdef LIBXML_XPATH_ENABLED
3189 xmlXPathFreeContext(ctxt->pctxt);
3190#endif /* LIBXML_XPATH_ENABLED */
3191 if (ctxt->loaded) {
3192 xmlFreeDoc(ctxt->doc);
3193 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003194 if (ctxt->filename != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003195 xmlFree(ctxt->filename);
Owen Taylor3473f882001-02-23 17:55:21 +00003196 xmlFree(ctxt);
3197 if (cmdline != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003198 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003199}
3200
3201#endif /* LIBXML_DEBUG_ENABLED */