blob: 1b8fd708658322e279a6b83c1738f6a8df62e94c [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
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00001775#ifdef LIBXML_XPATH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001776/****************************************************************
1777 * *
1778 * The XML shell related functions *
1779 * *
1780 ****************************************************************/
1781
Daniel Veillard78d12092001-10-11 09:12:24 +00001782
1783
Owen Taylor3473f882001-02-23 17:55:21 +00001784/*
1785 * TODO: Improvement/cleanups for the XML shell
1786 * - allow to shell out an editor on a subpart
1787 * - cleanup function registrations (with help) and calling
1788 * - provide registration routines
1789 */
1790
1791/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001792 * xmlShellPrintXPathError:
Daniel Veillard78d12092001-10-11 09:12:24 +00001793 * @errorType: valid xpath error id
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001794 * @arg: the argument that cause xpath to fail
Daniel Veillard78d12092001-10-11 09:12:24 +00001795 *
1796 * Print the xpath error to libxml default error channel
1797 */
1798void
1799xmlShellPrintXPathError(int errorType, const char *arg)
1800{
1801 const char *default_arg = "Result";
1802
1803 if (!arg)
1804 arg = default_arg;
1805
1806 switch (errorType) {
1807 case XPATH_UNDEFINED:
1808 xmlGenericError(xmlGenericErrorContext,
1809 "%s: no such node\n", arg);
1810 break;
1811
1812 case XPATH_BOOLEAN:
1813 xmlGenericError(xmlGenericErrorContext,
1814 "%s is a Boolean\n", arg);
1815 break;
1816 case XPATH_NUMBER:
1817 xmlGenericError(xmlGenericErrorContext,
1818 "%s is a number\n", arg);
1819 break;
1820 case XPATH_STRING:
1821 xmlGenericError(xmlGenericErrorContext,
1822 "%s is a string\n", arg);
1823 break;
1824 case XPATH_POINT:
1825 xmlGenericError(xmlGenericErrorContext,
1826 "%s is a point\n", arg);
1827 break;
1828 case XPATH_RANGE:
1829 xmlGenericError(xmlGenericErrorContext,
1830 "%s is a range\n", arg);
1831 break;
1832 case XPATH_LOCATIONSET:
1833 xmlGenericError(xmlGenericErrorContext,
1834 "%s is a range\n", arg);
1835 break;
1836 case XPATH_USERS:
1837 xmlGenericError(xmlGenericErrorContext,
1838 "%s is user-defined\n", arg);
1839 break;
1840 case XPATH_XSLT_TREE:
1841 xmlGenericError(xmlGenericErrorContext,
1842 "%s is an XSLT value tree\n", arg);
1843 break;
1844 }
Daniel Veillarda82b1822004-11-08 16:24:57 +00001845#if 0
Daniel Veillard78d12092001-10-11 09:12:24 +00001846 xmlGenericError(xmlGenericErrorContext,
1847 "Try casting the result string function (xpath builtin)\n",
1848 arg);
Daniel Veillarda82b1822004-11-08 16:24:57 +00001849#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00001850}
1851
1852
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001853#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001854/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001855 * xmlShellPrintNodeCtxt:
1856 * @ctxt : a non-null shell context
1857 * @node : a non-null node to print to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001858 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001859 * Print node to the output FILE
1860 */
1861static void
1862xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1863{
Daniel Veillard01992e02002-10-09 10:20:30 +00001864 FILE *fp;
1865
1866 if (!node)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001867 return;
Daniel Veillard01992e02002-10-09 10:20:30 +00001868 if (ctxt == NULL)
1869 fp = stdout;
1870 else
1871 fp = ctxt->output;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001872
1873 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001874 xmlDocDump(fp, (xmlDocPtr) node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001875 else if (node->type == XML_ATTRIBUTE_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001876 xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001877 else
Daniel Veillard01992e02002-10-09 10:20:30 +00001878 xmlElemDump(fp, node->doc, node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001879
Daniel Veillard01992e02002-10-09 10:20:30 +00001880 fprintf(fp, "\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00001881}
1882
1883/**
1884 * xmlShellPrintNode:
1885 * @node : a non-null node to print to the output FILE
1886 *
1887 * Print node to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001888 */
1889void
1890xmlShellPrintNode(xmlNodePtr node)
1891{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001892 xmlShellPrintNodeCtxt(NULL, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001893}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001894#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001895
Daniel Veillard78d12092001-10-11 09:12:24 +00001896/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001897 * xmlShellPrintXPathResultCtxt:
1898 * @ctxt: a valid shell context
Daniel Veillard9d06d302002-01-22 18:15:52 +00001899 * @list: a valid result generated by an xpath evaluation
Daniel Veillard78d12092001-10-11 09:12:24 +00001900 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001901 * Prints result to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001902 */
Daniel Veillard321be0c2002-10-08 21:26:42 +00001903static void
1904xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
Daniel Veillard78d12092001-10-11 09:12:24 +00001905{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001906 if (!ctxt)
1907 return;
Daniel Veillard78d12092001-10-11 09:12:24 +00001908
1909 if (list != NULL) {
1910 switch (list->type) {
1911 case XPATH_NODESET:{
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001912#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001913 int indx;
1914
1915 if (list->nodesetval) {
1916 for (indx = 0; indx < list->nodesetval->nodeNr;
1917 indx++) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001918 xmlShellPrintNodeCtxt(ctxt,
1919 list->nodesetval->nodeTab[indx]);
Daniel Veillard78d12092001-10-11 09:12:24 +00001920 }
1921 } else {
1922 xmlGenericError(xmlGenericErrorContext,
1923 "Empty node set\n");
1924 }
1925 break;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001926#else
1927 xmlGenericError(xmlGenericErrorContext,
1928 "Node set\n");
1929#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001930 }
1931 case XPATH_BOOLEAN:
1932 xmlGenericError(xmlGenericErrorContext,
1933 "Is a Boolean:%s\n",
1934 xmlBoolToText(list->boolval));
1935 break;
1936 case XPATH_NUMBER:
1937 xmlGenericError(xmlGenericErrorContext,
1938 "Is a number:%0g\n", list->floatval);
1939 break;
1940 case XPATH_STRING:
1941 xmlGenericError(xmlGenericErrorContext,
1942 "Is a string:%s\n", list->stringval);
1943 break;
1944
1945 default:
1946 xmlShellPrintXPathError(list->type, NULL);
1947 }
1948 }
1949}
1950
1951/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001952 * xmlShellPrintXPathResult:
1953 * @list: a valid result generated by an xpath evaluation
1954 *
1955 * Prints result to the output FILE
1956 */
1957void
1958xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1959{
1960 xmlShellPrintXPathResultCtxt(NULL, list);
1961}
1962
1963/**
Owen Taylor3473f882001-02-23 17:55:21 +00001964 * xmlShellList:
1965 * @ctxt: the shell context
1966 * @arg: unused
1967 * @node: a node
1968 * @node2: unused
1969 *
1970 * Implements the XML shell function "ls"
1971 * Does an Unix like listing of the given node (like a directory)
1972 *
1973 * Returns 0
1974 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001975int
Daniel Veillard321be0c2002-10-08 21:26:42 +00001976xmlShellList(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00001977 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1978 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1979{
Owen Taylor3473f882001-02-23 17:55:21 +00001980 xmlNodePtr cur;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001981 if (!ctxt)
1982 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001983 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001984 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001985 return (0);
1986 }
Owen Taylor3473f882001-02-23 17:55:21 +00001987 if ((node->type == XML_DOCUMENT_NODE) ||
1988 (node->type == XML_HTML_DOCUMENT_NODE)) {
1989 cur = ((xmlDocPtr) node)->children;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001990 } else if (node->type == XML_NAMESPACE_DECL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001991 xmlLsOneNode(ctxt->output, node);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001992 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001993 } else if (node->children != NULL) {
1994 cur = node->children;
1995 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001996 xmlLsOneNode(ctxt->output, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001997 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001998 }
1999 while (cur != NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002000 xmlLsOneNode(ctxt->output, cur);
Daniel Veillard78d12092001-10-11 09:12:24 +00002001 cur = cur->next;
Owen Taylor3473f882001-02-23 17:55:21 +00002002 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002003 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002004}
2005
2006/**
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002007 * xmlShellBase:
2008 * @ctxt: the shell context
2009 * @arg: unused
2010 * @node: a node
2011 * @node2: unused
2012 *
2013 * Implements the XML shell function "base"
2014 * dumps the current XML base of the node
2015 *
2016 * Returns 0
2017 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002018int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002019xmlShellBase(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002020 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2021 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2022{
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002023 xmlChar *base;
Daniel Veillard321be0c2002-10-08 21:26:42 +00002024 if (!ctxt)
2025 return 0;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002026 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002027 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002028 return (0);
2029 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002030
2031 base = xmlNodeGetBase(node->doc, node);
2032
2033 if (base == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002034 fprintf(ctxt->output, " No base found !!!\n");
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002035 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002036 fprintf(ctxt->output, "%s\n", base);
Daniel Veillard78d12092001-10-11 09:12:24 +00002037 xmlFree(base);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002038 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002039 return (0);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002040}
2041
Daniel Veillardb34321c2004-03-04 17:09:47 +00002042#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002043/**
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002044 * xmlShellSetBase:
2045 * @ctxt: the shell context
2046 * @arg: the new base
2047 * @node: a node
2048 * @node2: unused
2049 *
2050 * Implements the XML shell function "setbase"
2051 * change the current XML base of the node
2052 *
2053 * Returns 0
2054 */
2055static int
2056xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2057 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2058 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2059{
2060 xmlNodeSetBase(node, (xmlChar*) arg);
2061 return (0);
2062}
Daniel Veillard2156d432004-03-04 15:59:36 +00002063#endif
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002064
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002065#ifdef LIBXML_XPATH_ENABLED
2066/**
2067 * xmlShellRegisterNamespace:
2068 * @ctxt: the shell context
2069 * @arg: a string in prefix=nsuri format
2070 * @node: unused
2071 * @node2: unused
2072 *
2073 * Implements the XML shell function "setns"
2074 * register/unregister a prefix=namespace pair
2075 * on the XPath context
2076 *
2077 * Returns 0 on success and a negative value otherwise.
2078 */
2079static int
2080xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
2081 xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2082{
2083 xmlChar* nsListDup;
2084 xmlChar* prefix;
2085 xmlChar* href;
2086 xmlChar* next;
2087
2088 nsListDup = xmlStrdup((xmlChar *) arg);
2089 next = nsListDup;
2090 while(next != NULL) {
2091 /* skip spaces */
2092 /*while((*next) == ' ') next++;*/
2093 if((*next) == '\0') break;
2094
2095 /* find prefix */
2096 prefix = next;
2097 next = (xmlChar*)xmlStrchr(next, '=');
2098 if(next == NULL) {
2099 fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
2100 xmlFree(nsListDup);
2101 return(-1);
2102 }
2103 *(next++) = '\0';
2104
2105 /* find href */
2106 href = next;
2107 next = (xmlChar*)xmlStrchr(next, ' ');
2108 if(next != NULL) {
2109 *(next++) = '\0';
2110 }
2111
2112 /* do register namespace */
2113 if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
2114 fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
2115 xmlFree(nsListDup);
2116 return(-1);
2117 }
2118 }
2119
2120 xmlFree(nsListDup);
2121 return(0);
2122}
2123#endif
2124
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002125/**
Daniel Veillard1e208222002-10-22 14:25:25 +00002126 * xmlShellGrep:
2127 * @ctxt: the shell context
2128 * @arg: the string or regular expression to find
2129 * @node: a node
2130 * @node2: unused
2131 *
2132 * Implements the XML shell function "grep"
2133 * dumps informations about the node (namespace, attributes, content).
2134 *
2135 * Returns 0
2136 */
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002137static int
Daniel Veillard1e208222002-10-22 14:25:25 +00002138xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2139 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2140{
2141 if (!ctxt)
2142 return (0);
2143 if (node == NULL)
2144 return (0);
2145 if (arg == NULL)
2146 return (0);
2147#ifdef LIBXML_REGEXP_ENABLED
2148 if ((xmlStrchr((xmlChar *) arg, '?')) ||
2149 (xmlStrchr((xmlChar *) arg, '*')) ||
2150 (xmlStrchr((xmlChar *) arg, '.')) ||
2151 (xmlStrchr((xmlChar *) arg, '['))) {
2152 }
2153#endif
2154 while (node != NULL) {
2155 if (node->type == XML_COMMENT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002156 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002157
2158 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
2159 xmlShellList(ctxt, NULL, node, NULL);
2160 }
2161 } else if (node->type == XML_TEXT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002162 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002163
2164 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002165 xmlShellList(ctxt, NULL, node->parent, NULL);
Daniel Veillard1e208222002-10-22 14:25:25 +00002166 }
2167 }
2168
2169 /*
2170 * Browse the full subtree, deep first
2171 */
2172
2173 if ((node->type == XML_DOCUMENT_NODE) ||
2174 (node->type == XML_HTML_DOCUMENT_NODE)) {
2175 node = ((xmlDocPtr) node)->children;
2176 } else if ((node->children != NULL)
2177 && (node->type != XML_ENTITY_REF_NODE)) {
2178 /* deep first */
2179 node = node->children;
2180 } else if (node->next != NULL) {
2181 /* then siblings */
2182 node = node->next;
2183 } else {
2184 /* go up to parents->next if needed */
2185 while (node != NULL) {
2186 if (node->parent != NULL) {
2187 node = node->parent;
2188 }
2189 if (node->next != NULL) {
2190 node = node->next;
2191 break;
2192 }
2193 if (node->parent == NULL) {
2194 node = NULL;
2195 break;
2196 }
2197 }
2198 }
2199 }
2200 return (0);
2201}
2202
2203/**
Owen Taylor3473f882001-02-23 17:55:21 +00002204 * xmlShellDir:
2205 * @ctxt: the shell context
2206 * @arg: unused
2207 * @node: a node
2208 * @node2: unused
2209 *
2210 * Implements the XML shell function "dir"
2211 * dumps informations about the node (namespace, attributes, content).
2212 *
2213 * Returns 0
2214 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002215int
2216xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2217 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2218 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2219{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002220 if (!ctxt)
2221 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002222 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002223 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002224 return (0);
2225 }
Owen Taylor3473f882001-02-23 17:55:21 +00002226 if ((node->type == XML_DOCUMENT_NODE) ||
2227 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002228 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
Owen Taylor3473f882001-02-23 17:55:21 +00002229 } else if (node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002230 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002231 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002232 xmlDebugDumpOneNode(ctxt->output, node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002233 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002234 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002235}
2236
Daniel Veillard29b17482004-08-16 00:39:03 +00002237/**
2238 * xmlShellSetContent:
2239 * @ctxt: the shell context
2240 * @value: the content as a string
2241 * @node: a node
2242 * @node2: unused
2243 *
2244 * Implements the XML shell function "dir"
2245 * dumps informations about the node (namespace, attributes, content).
2246 *
2247 * Returns 0
2248 */
2249static int
2250xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2251 char *value, xmlNodePtr node,
2252 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2253{
2254 xmlNodePtr results;
2255 xmlParserErrors ret;
2256
2257 if (!ctxt)
2258 return (0);
2259 if (node == NULL) {
2260 fprintf(ctxt->output, "NULL\n");
2261 return (0);
2262 }
2263 if (value == NULL) {
2264 fprintf(ctxt->output, "NULL\n");
2265 return (0);
2266 }
2267
2268 ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
2269 if (ret == XML_ERR_OK) {
2270 if (node->children != NULL) {
2271 xmlFreeNodeList(node->children);
2272 node->children = NULL;
2273 node->last = NULL;
2274 }
2275 xmlAddChildList(node, results);
2276 } else {
2277 fprintf(ctxt->output, "failed to parse content\n");
2278 }
2279 return (0);
2280}
2281
Daniel Veillard522bc602004-02-21 11:53:09 +00002282#ifdef LIBXML_SCHEMAS_ENABLED
2283/**
2284 * xmlShellRNGValidate:
2285 * @ctxt: the shell context
2286 * @schemas: the path to the Relax-NG schemas
2287 * @node: a node
2288 * @node2: unused
2289 *
2290 * Implements the XML shell function "relaxng"
2291 * validating the instance against a Relax-NG schemas
2292 *
2293 * Returns 0
2294 */
2295static int
2296xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
2297 xmlNodePtr node ATTRIBUTE_UNUSED,
2298 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2299{
2300 xmlRelaxNGPtr relaxngschemas;
2301 xmlRelaxNGParserCtxtPtr ctxt;
2302 xmlRelaxNGValidCtxtPtr vctxt;
2303 int ret;
2304
2305 ctxt = xmlRelaxNGNewParserCtxt(schemas);
2306 xmlRelaxNGSetParserErrors(ctxt,
2307 (xmlRelaxNGValidityErrorFunc) fprintf,
2308 (xmlRelaxNGValidityWarningFunc) fprintf,
2309 stderr);
2310 relaxngschemas = xmlRelaxNGParse(ctxt);
2311 xmlRelaxNGFreeParserCtxt(ctxt);
2312 if (relaxngschemas == NULL) {
2313 xmlGenericError(xmlGenericErrorContext,
2314 "Relax-NG schema %s failed to compile\n", schemas);
2315 return(-1);
2316 }
2317 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2318 xmlRelaxNGSetValidErrors(vctxt,
2319 (xmlRelaxNGValidityErrorFunc) fprintf,
2320 (xmlRelaxNGValidityWarningFunc) fprintf,
2321 stderr);
2322 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2323 if (ret == 0) {
2324 fprintf(stderr, "%s validates\n", sctxt->filename);
2325 } else if (ret > 0) {
2326 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2327 } else {
2328 fprintf(stderr, "%s validation generated an internal error\n",
2329 sctxt->filename);
2330 }
2331 xmlRelaxNGFreeValidCtxt(vctxt);
2332 if (relaxngschemas != NULL)
2333 xmlRelaxNGFree(relaxngschemas);
2334 return(0);
2335}
2336#endif
2337
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002338#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002339/**
2340 * xmlShellCat:
2341 * @ctxt: the shell context
2342 * @arg: unused
2343 * @node: a node
2344 * @node2: unused
2345 *
2346 * Implements the XML shell function "cat"
2347 * dumps the serialization node content (XML or HTML).
2348 *
2349 * Returns 0
2350 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002351int
2352xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2353 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2354{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002355 if (!ctxt)
2356 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002357 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002358 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002359 return (0);
2360 }
Owen Taylor3473f882001-02-23 17:55:21 +00002361 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2362#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002363 if (node->type == XML_HTML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002364 htmlDocDump(ctxt->output, (htmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002365 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002366 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002367#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002368 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002369 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002370 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002371 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002372#endif /* LIBXML_HTML_ENABLED */
2373 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00002374 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002375 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002376 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002377 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002378 }
Daniel Veillard321be0c2002-10-08 21:26:42 +00002379 fprintf(ctxt->output, "\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002380 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002381}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002382#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002383
2384/**
2385 * xmlShellLoad:
2386 * @ctxt: the shell context
2387 * @filename: the file name
2388 * @node: unused
2389 * @node2: unused
2390 *
2391 * Implements the XML shell function "load"
2392 * loads a new document specified by the filename
2393 *
2394 * Returns 0 or -1 if loading failed
2395 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002396int
2397xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2398 xmlNodePtr node ATTRIBUTE_UNUSED,
2399 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2400{
Owen Taylor3473f882001-02-23 17:55:21 +00002401 xmlDocPtr doc;
2402 int html = 0;
2403
Daniel Veillarda82b1822004-11-08 16:24:57 +00002404 if ((ctxt == NULL) || (filename == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002405 if (ctxt->doc != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002406 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00002407
2408 if (html) {
2409#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002410 doc = htmlParseFile(filename, NULL);
2411#else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002412 fprintf(ctxt->output, "HTML support not compiled in\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002413 doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002414#endif /* LIBXML_HTML_ENABLED */
2415 } else {
Daniel Veillardebe25d42004-03-25 09:35:49 +00002416 doc = xmlReadFile(filename,NULL,0);
Owen Taylor3473f882001-02-23 17:55:21 +00002417 }
2418 if (doc != NULL) {
2419 if (ctxt->loaded == 1) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002420 xmlFreeDoc(ctxt->doc);
2421 }
2422 ctxt->loaded = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002423#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002424 xmlXPathFreeContext(ctxt->pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00002425#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002426 xmlFree(ctxt->filename);
2427 ctxt->doc = doc;
2428 ctxt->node = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002429#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002430 ctxt->pctxt = xmlXPathNewContext(doc);
Owen Taylor3473f882001-02-23 17:55:21 +00002431#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard85095e22003-04-23 13:56:44 +00002432 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002433 } else
Daniel Veillard78d12092001-10-11 09:12:24 +00002434 return (-1);
2435 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002436}
2437
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002438#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002439/**
2440 * xmlShellWrite:
2441 * @ctxt: the shell context
2442 * @filename: the file name
2443 * @node: a node in the tree
2444 * @node2: unused
2445 *
2446 * Implements the XML shell function "write"
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002447 * Write the current node to the filename, it saves the serialization
Owen Taylor3473f882001-02-23 17:55:21 +00002448 * of the subtree under the @node specified
2449 *
2450 * Returns 0 or -1 in case of error
2451 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002452int
Owen Taylor3473f882001-02-23 17:55:21 +00002453xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Daniel Veillard78d12092001-10-11 09:12:24 +00002454 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2455{
Owen Taylor3473f882001-02-23 17:55:21 +00002456 if (node == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002457 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002458 if ((filename == NULL) || (filename[0] == 0)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002459 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002460 }
2461#ifdef W_OK
2462 if (access((char *) filename, W_OK)) {
2463 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002464 "Cannot write to %s\n", filename);
2465 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002466 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002467#endif
2468 switch (node->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002469 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002470 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2471 xmlGenericError(xmlGenericErrorContext,
2472 "Failed to write to %s\n", filename);
2473 return (-1);
2474 }
2475 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002476 case XML_HTML_DOCUMENT_NODE:
2477#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002478 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2479 xmlGenericError(xmlGenericErrorContext,
2480 "Failed to write to %s\n", filename);
2481 return (-1);
2482 }
Owen Taylor3473f882001-02-23 17:55:21 +00002483#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002484 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2485 xmlGenericError(xmlGenericErrorContext,
2486 "Failed to write to %s\n", filename);
2487 return (-1);
2488 }
Owen Taylor3473f882001-02-23 17:55:21 +00002489#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002490 break;
2491 default:{
2492 FILE *f;
Owen Taylor3473f882001-02-23 17:55:21 +00002493
Daniel Veillard78d12092001-10-11 09:12:24 +00002494 f = fopen((char *) filename, "w");
2495 if (f == NULL) {
2496 xmlGenericError(xmlGenericErrorContext,
2497 "Failed to write to %s\n", filename);
2498 return (-1);
2499 }
2500 xmlElemDump(f, ctxt->doc, node);
2501 fclose(f);
2502 }
Owen Taylor3473f882001-02-23 17:55:21 +00002503 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002504 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002505}
2506
2507/**
2508 * xmlShellSave:
2509 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002510 * @filename: the file name (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002511 * @node: unused
2512 * @node2: unused
2513 *
2514 * Implements the XML shell function "save"
2515 * Write the current document to the filename, or it's original name
2516 *
2517 * Returns 0 or -1 in case of error
2518 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002519int
2520xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2521 xmlNodePtr node ATTRIBUTE_UNUSED,
2522 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2523{
Daniel Veillarda82b1822004-11-08 16:24:57 +00002524 if ((ctxt == NULL) || (ctxt->doc == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002525 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002526 if ((filename == NULL) || (filename[0] == 0))
2527 filename = ctxt->filename;
Daniel Veillarda82b1822004-11-08 16:24:57 +00002528 if (filename == NULL)
2529 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002530#ifdef W_OK
2531 if (access((char *) filename, W_OK)) {
2532 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002533 "Cannot save to %s\n", filename);
2534 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002535 }
2536#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002537 switch (ctxt->doc->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002538 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002539 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2540 xmlGenericError(xmlGenericErrorContext,
2541 "Failed to save to %s\n", filename);
2542 }
2543 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002544 case XML_HTML_DOCUMENT_NODE:
2545#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002546 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2547 xmlGenericError(xmlGenericErrorContext,
2548 "Failed to save to %s\n", filename);
2549 }
Owen Taylor3473f882001-02-23 17:55:21 +00002550#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002551 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2552 xmlGenericError(xmlGenericErrorContext,
2553 "Failed to save to %s\n", filename);
2554 }
Owen Taylor3473f882001-02-23 17:55:21 +00002555#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002556 break;
2557 default:
2558 xmlGenericError(xmlGenericErrorContext,
2559 "To save to subparts of a document use the 'write' command\n");
2560 return (-1);
2561
Owen Taylor3473f882001-02-23 17:55:21 +00002562 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002563 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002564}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002565#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002566
Daniel Veillardf54cd532004-02-25 11:52:31 +00002567#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002568/**
2569 * xmlShellValidate:
2570 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002571 * @dtd: the DTD URI (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002572 * @node: unused
2573 * @node2: unused
2574 *
2575 * Implements the XML shell function "validate"
2576 * Validate the document, if a DTD path is provided, then the validation
2577 * is done against the given DTD.
2578 *
2579 * Returns 0 or -1 in case of error
2580 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002581int
2582xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2583 xmlNodePtr node ATTRIBUTE_UNUSED,
2584 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2585{
Owen Taylor3473f882001-02-23 17:55:21 +00002586 xmlValidCtxt vctxt;
2587 int res = -1;
2588
Daniel Veillarda82b1822004-11-08 16:24:57 +00002589 if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002590 vctxt.userData = stderr;
2591 vctxt.error = (xmlValidityErrorFunc) fprintf;
2592 vctxt.warning = (xmlValidityWarningFunc) fprintf;
2593
2594 if ((dtd == NULL) || (dtd[0] == 0)) {
2595 res = xmlValidateDocument(&vctxt, ctxt->doc);
2596 } else {
2597 xmlDtdPtr subset;
2598
Daniel Veillard78d12092001-10-11 09:12:24 +00002599 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2600 if (subset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002601 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2602
Daniel Veillard78d12092001-10-11 09:12:24 +00002603 xmlFreeDtd(subset);
2604 }
Owen Taylor3473f882001-02-23 17:55:21 +00002605 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002606 return (res);
Owen Taylor3473f882001-02-23 17:55:21 +00002607}
Daniel Veillardf54cd532004-02-25 11:52:31 +00002608#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002609
2610/**
2611 * xmlShellDu:
2612 * @ctxt: the shell context
2613 * @arg: unused
2614 * @tree: a node defining a subtree
2615 * @node2: unused
2616 *
2617 * Implements the XML shell function "du"
2618 * show the structure of the subtree under node @tree
2619 * If @tree is null, the command works on the current node.
2620 *
2621 * Returns 0 or -1 in case of error
2622 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002623int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002624xmlShellDu(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002625 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2626 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2627{
Owen Taylor3473f882001-02-23 17:55:21 +00002628 xmlNodePtr node;
Daniel Veillard78d12092001-10-11 09:12:24 +00002629 int indent = 0, i;
Owen Taylor3473f882001-02-23 17:55:21 +00002630
Daniel Veillard321be0c2002-10-08 21:26:42 +00002631 if (!ctxt)
2632 return (-1);
2633
Daniel Veillard78d12092001-10-11 09:12:24 +00002634 if (tree == NULL)
2635 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002636 node = tree;
2637 while (node != NULL) {
2638 if ((node->type == XML_DOCUMENT_NODE) ||
2639 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002640 fprintf(ctxt->output, "/\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002641 } else if (node->type == XML_ELEMENT_NODE) {
2642 for (i = 0; i < indent; i++)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002643 fprintf(ctxt->output, " ");
2644 fprintf(ctxt->output, "%s\n", node->name);
Daniel Veillard78d12092001-10-11 09:12:24 +00002645 } else {
2646 }
Owen Taylor3473f882001-02-23 17:55:21 +00002647
Daniel Veillard78d12092001-10-11 09:12:24 +00002648 /*
2649 * Browse the full subtree, deep first
2650 */
Owen Taylor3473f882001-02-23 17:55:21 +00002651
2652 if ((node->type == XML_DOCUMENT_NODE) ||
2653 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002654 node = ((xmlDocPtr) node)->children;
2655 } else if ((node->children != NULL)
2656 && (node->type != XML_ENTITY_REF_NODE)) {
2657 /* deep first */
2658 node = node->children;
2659 indent++;
2660 } else if ((node != tree) && (node->next != NULL)) {
2661 /* then siblings */
2662 node = node->next;
2663 } else if (node != tree) {
2664 /* go up to parents->next if needed */
2665 while (node != tree) {
2666 if (node->parent != NULL) {
2667 node = node->parent;
2668 indent--;
2669 }
2670 if ((node != tree) && (node->next != NULL)) {
2671 node = node->next;
2672 break;
2673 }
2674 if (node->parent == NULL) {
2675 node = NULL;
2676 break;
2677 }
2678 if (node == tree) {
2679 node = NULL;
2680 break;
2681 }
2682 }
2683 /* exit condition */
2684 if (node == tree)
2685 node = NULL;
2686 } else
2687 node = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002688 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002689 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002690}
2691
2692/**
2693 * xmlShellPwd:
2694 * @ctxt: the shell context
2695 * @buffer: the output buffer
Daniel Veillard9d06d302002-01-22 18:15:52 +00002696 * @node: a node
Owen Taylor3473f882001-02-23 17:55:21 +00002697 * @node2: unused
2698 *
2699 * Implements the XML shell function "pwd"
2700 * Show the full path from the root to the node, if needed building
2701 * thumblers when similar elements exists at a given ancestor level.
2702 * The output is compatible with XPath commands.
2703 *
2704 * Returns 0 or -1 in case of error
2705 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002706int
2707xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2708 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2709{
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002710 xmlChar *path;
Owen Taylor3473f882001-02-23 17:55:21 +00002711
Daniel Veillarda82b1822004-11-08 16:24:57 +00002712 if ((node == NULL) || (buffer == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002713 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002714
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002715 path = xmlGetNodePath(node);
2716 if (path == NULL)
2717 return (-1);
2718
2719 /*
2720 * This test prevents buffer overflow, because this routine
2721 * is only called by xmlShell, in which the second argument is
2722 * 500 chars long.
2723 * It is a dirty hack before a cleaner solution is found.
2724 * Documentation should mention that the second argument must
2725 * be at least 500 chars long, and could be stripped if too long.
2726 */
2727 snprintf(buffer, 499, "%s", path);
2728 buffer[499] = '0';
2729 xmlFree(path);
2730
Daniel Veillard78d12092001-10-11 09:12:24 +00002731 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002732}
2733
2734/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002735 * xmlShell:
Owen Taylor3473f882001-02-23 17:55:21 +00002736 * @doc: the initial document
2737 * @filename: the output buffer
2738 * @input: the line reading function
Daniel Veillard321be0c2002-10-08 21:26:42 +00002739 * @output: the output FILE*, defaults to stdout if NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002740 *
2741 * Implements the XML shell
2742 * This allow to load, validate, view, modify and save a document
2743 * using a environment similar to a UNIX commandline.
2744 */
2745void
2746xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Daniel Veillard78d12092001-10-11 09:12:24 +00002747 FILE * output)
2748{
Owen Taylor3473f882001-02-23 17:55:21 +00002749 char prompt[500] = "/ > ";
2750 char *cmdline = NULL, *cur;
2751 int nbargs;
2752 char command[100];
2753 char arg[400];
2754 int i;
2755 xmlShellCtxtPtr ctxt;
2756 xmlXPathObjectPtr list;
2757
2758 if (doc == NULL)
2759 return;
2760 if (filename == NULL)
2761 return;
2762 if (input == NULL)
2763 return;
2764 if (output == NULL)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002765 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00002766 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Daniel Veillard78d12092001-10-11 09:12:24 +00002767 if (ctxt == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00002768 return;
2769 ctxt->loaded = 0;
2770 ctxt->doc = doc;
2771 ctxt->input = input;
2772 ctxt->output = output;
2773 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Daniel Veillard78d12092001-10-11 09:12:24 +00002774 ctxt->node = (xmlNodePtr) ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002775
2776#ifdef LIBXML_XPATH_ENABLED
2777 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2778 if (ctxt->pctxt == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002779 xmlFree(ctxt);
2780 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002781 }
2782#endif /* LIBXML_XPATH_ENABLED */
2783 while (1) {
2784 if (ctxt->node == (xmlNodePtr) ctxt->doc)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002785 snprintf(prompt, sizeof(prompt), "%s > ", "/");
Daniel Veillard7a985a12003-07-06 17:57:42 +00002786 else if ((ctxt->node != NULL) && (ctxt->node->name))
Daniel Veillard78d12092001-10-11 09:12:24 +00002787 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002788 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002789 snprintf(prompt, sizeof(prompt), "? > ");
Owen Taylor3473f882001-02-23 17:55:21 +00002790 prompt[sizeof(prompt) - 1] = 0;
2791
Daniel Veillard78d12092001-10-11 09:12:24 +00002792 /*
2793 * Get a new command line
2794 */
Owen Taylor3473f882001-02-23 17:55:21 +00002795 cmdline = ctxt->input(prompt);
Daniel Veillard78d12092001-10-11 09:12:24 +00002796 if (cmdline == NULL)
2797 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002798
Daniel Veillard78d12092001-10-11 09:12:24 +00002799 /*
2800 * Parse the command itself
2801 */
2802 cur = cmdline;
2803 nbargs = 0;
2804 while ((*cur == ' ') || (*cur == '\t'))
2805 cur++;
2806 i = 0;
2807 while ((*cur != ' ') && (*cur != '\t') &&
2808 (*cur != '\n') && (*cur != '\r')) {
2809 if (*cur == 0)
2810 break;
2811 command[i++] = *cur++;
2812 }
2813 command[i] = 0;
2814 if (i == 0)
2815 continue;
2816 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002817
Daniel Veillard78d12092001-10-11 09:12:24 +00002818 /*
2819 * Parse the argument
2820 */
2821 while ((*cur == ' ') || (*cur == '\t'))
2822 cur++;
2823 i = 0;
2824 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2825 if (*cur == 0)
2826 break;
2827 arg[i++] = *cur++;
2828 }
2829 arg[i] = 0;
2830 if (i != 0)
2831 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002832
Daniel Veillard78d12092001-10-11 09:12:24 +00002833 /*
2834 * start interpreting the command
2835 */
Owen Taylor3473f882001-02-23 17:55:21 +00002836 if (!strcmp(command, "exit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002837 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002838 if (!strcmp(command, "quit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002839 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002840 if (!strcmp(command, "bye"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002841 break;
Daniel Veillard5004f422001-11-08 13:53:05 +00002842 if (!strcmp(command, "help")) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002843 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2844 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2845 fprintf(ctxt->output, "\tbye leave shell\n");
2846 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2847 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2848 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2849 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2850 fprintf(ctxt->output, "\texit leave shell\n");
2851 fprintf(ctxt->output, "\thelp display this help\n");
2852 fprintf(ctxt->output, "\tfree display memory usage\n");
2853 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2854 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00002855 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 +00002856#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002857 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002858 fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2859 fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002860#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard321be0c2002-10-08 21:26:42 +00002861 fprintf(ctxt->output, "\tpwd display current working directory\n");
2862 fprintf(ctxt->output, "\tquit leave shell\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002863#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002864 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00002865 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002866#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf54cd532004-02-25 11:52:31 +00002867#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002868 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002869#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard522bc602004-02-21 11:53:09 +00002870#ifdef LIBXML_SCHEMAS_ENABLED
2871 fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n");
2872#endif
Daniel Veillard1e208222002-10-22 14:25:25 +00002873 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002874#ifdef LIBXML_VALID_ENABLED
Daniel Veillard5004f422001-11-08 13:53:05 +00002875 } else if (!strcmp(command, "validate")) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002876 xmlShellValidate(ctxt, arg, NULL, NULL);
Daniel Veillardf54cd532004-02-25 11:52:31 +00002877#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002878 } else if (!strcmp(command, "load")) {
2879 xmlShellLoad(ctxt, arg, NULL, NULL);
Daniel Veillard522bc602004-02-21 11:53:09 +00002880#ifdef LIBXML_SCHEMAS_ENABLED
2881 } else if (!strcmp(command, "relaxng")) {
2882 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2883#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002884#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002885 } else if (!strcmp(command, "save")) {
2886 xmlShellSave(ctxt, arg, NULL, NULL);
2887 } else if (!strcmp(command, "write")) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00002888 if ((arg == NULL) || (arg[0] == 0))
2889 xmlGenericError(xmlGenericErrorContext,
2890 "Write command requires a filename argument\n");
2891 else
2892 xmlShellWrite(ctxt, arg, NULL, NULL);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002893#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard1e208222002-10-22 14:25:25 +00002894 } else if (!strcmp(command, "grep")) {
2895 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
Daniel Veillard78d12092001-10-11 09:12:24 +00002896 } else if (!strcmp(command, "free")) {
2897 if (arg[0] == 0) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002898 xmlMemShow(ctxt->output, 0);
Daniel Veillard78d12092001-10-11 09:12:24 +00002899 } else {
2900 int len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002901
Daniel Veillard78d12092001-10-11 09:12:24 +00002902 sscanf(arg, "%d", &len);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002903 xmlMemShow(ctxt->output, len);
Daniel Veillard78d12092001-10-11 09:12:24 +00002904 }
2905 } else if (!strcmp(command, "pwd")) {
2906 char dir[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002907
Daniel Veillard78d12092001-10-11 09:12:24 +00002908 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
Daniel Veillard321be0c2002-10-08 21:26:42 +00002909 fprintf(ctxt->output, "%s\n", dir);
Daniel Veillard78d12092001-10-11 09:12:24 +00002910 } else if (!strcmp(command, "du")) {
2911 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2912 } else if (!strcmp(command, "base")) {
2913 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
Daniel Veillard29b17482004-08-16 00:39:03 +00002914 } else if (!strcmp(command, "set")) {
2915 xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002916#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002917 } else if (!strcmp(command, "setns")) {
2918 if (arg[0] == 0) {
2919 xmlGenericError(xmlGenericErrorContext,
2920 "setns: prefix=[nsuri] required\n");
2921 } else {
2922 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
2923 }
Daniel Veillard2070c482002-01-22 22:12:19 +00002924 } else if (!strcmp(command, "xpath")) {
2925 if (arg[0] == 0) {
2926 xmlGenericError(xmlGenericErrorContext,
2927 "xpath: expression required\n");
2928 } else {
2929 ctxt->pctxt->node = ctxt->node;
2930 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002931 xmlXPathDebugDumpObject(ctxt->output, list, 0);
Daniel Veillard2070c482002-01-22 22:12:19 +00002932 xmlXPathFreeObject(list);
2933 }
2934#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard2156d432004-03-04 15:59:36 +00002935#ifdef LIBXML_TREE_ENABLED
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002936 } else if (!strcmp(command, "setbase")) {
2937 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2156d432004-03-04 15:59:36 +00002938#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002939 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
2940 int dir = (!strcmp(command, "dir"));
2941
2942 if (arg[0] == 0) {
2943 if (dir)
2944 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
2945 else
2946 xmlShellList(ctxt, NULL, ctxt->node, NULL);
2947 } else {
2948 ctxt->pctxt->node = ctxt->node;
Daniel Veillard61d80a22001-04-27 17:13:01 +00002949#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002950 ctxt->pctxt->node = ctxt->node;
2951 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2952#else
2953 list = NULL;
2954#endif /* LIBXML_XPATH_ENABLED */
2955 if (list != NULL) {
2956 switch (list->type) {
2957 case XPATH_UNDEFINED:
2958 xmlGenericError(xmlGenericErrorContext,
2959 "%s: no such node\n", arg);
2960 break;
2961 case XPATH_NODESET:{
2962 int indx;
2963
Daniel Veillarda6825e82001-11-07 13:33:59 +00002964 if (list->nodesetval == NULL)
2965 break;
2966
Daniel Veillard78d12092001-10-11 09:12:24 +00002967 for (indx = 0;
2968 indx < list->nodesetval->nodeNr;
2969 indx++) {
2970 if (dir)
2971 xmlShellDir(ctxt, NULL,
2972 list->nodesetval->
2973 nodeTab[indx], NULL);
2974 else
2975 xmlShellList(ctxt, NULL,
2976 list->nodesetval->
2977 nodeTab[indx], NULL);
2978 }
2979 break;
2980 }
2981 case XPATH_BOOLEAN:
2982 xmlGenericError(xmlGenericErrorContext,
2983 "%s is a Boolean\n", arg);
2984 break;
2985 case XPATH_NUMBER:
2986 xmlGenericError(xmlGenericErrorContext,
2987 "%s is a number\n", arg);
2988 break;
2989 case XPATH_STRING:
2990 xmlGenericError(xmlGenericErrorContext,
2991 "%s is a string\n", arg);
2992 break;
2993 case XPATH_POINT:
2994 xmlGenericError(xmlGenericErrorContext,
2995 "%s is a point\n", arg);
2996 break;
2997 case XPATH_RANGE:
2998 xmlGenericError(xmlGenericErrorContext,
2999 "%s is a range\n", arg);
3000 break;
3001 case XPATH_LOCATIONSET:
3002 xmlGenericError(xmlGenericErrorContext,
3003 "%s is a range\n", arg);
3004 break;
3005 case XPATH_USERS:
3006 xmlGenericError(xmlGenericErrorContext,
3007 "%s is user-defined\n", arg);
3008 break;
3009 case XPATH_XSLT_TREE:
3010 xmlGenericError(xmlGenericErrorContext,
3011 "%s is an XSLT value tree\n",
3012 arg);
3013 break;
3014 }
3015#ifdef LIBXML_XPATH_ENABLED
3016 xmlXPathFreeObject(list);
Daniel Veillard61d80a22001-04-27 17:13:01 +00003017#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00003018 } else {
3019 xmlGenericError(xmlGenericErrorContext,
3020 "%s: no such node\n", arg);
3021 }
3022 ctxt->pctxt->node = NULL;
3023 }
3024 } else if (!strcmp(command, "cd")) {
3025 if (arg[0] == 0) {
3026 ctxt->node = (xmlNodePtr) ctxt->doc;
3027 } else {
3028#ifdef LIBXML_XPATH_ENABLED
3029 ctxt->pctxt->node = ctxt->node;
3030 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3031#else
3032 list = NULL;
3033#endif /* LIBXML_XPATH_ENABLED */
3034 if (list != NULL) {
3035 switch (list->type) {
3036 case XPATH_UNDEFINED:
3037 xmlGenericError(xmlGenericErrorContext,
3038 "%s: no such node\n", arg);
3039 break;
3040 case XPATH_NODESET:
Daniel Veillarda6825e82001-11-07 13:33:59 +00003041 if (list->nodesetval != NULL) {
3042 if (list->nodesetval->nodeNr == 1) {
3043 ctxt->node = list->nodesetval->nodeTab[0];
Daniel Veillard7a985a12003-07-06 17:57:42 +00003044 if ((ctxt->node != NULL) &&
3045 (ctxt->node->type ==
3046 XML_NAMESPACE_DECL)) {
3047 xmlGenericError(xmlGenericErrorContext,
3048 "cannot cd to namespace\n");
3049 ctxt->node = NULL;
3050 }
Daniel Veillarda6825e82001-11-07 13:33:59 +00003051 } else
3052 xmlGenericError(xmlGenericErrorContext,
3053 "%s is a %d Node Set\n",
3054 arg,
3055 list->nodesetval->nodeNr);
Daniel Veillard78d12092001-10-11 09:12:24 +00003056 } else
3057 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda6825e82001-11-07 13:33:59 +00003058 "%s is an empty Node Set\n",
3059 arg);
Daniel Veillard78d12092001-10-11 09:12:24 +00003060 break;
3061 case XPATH_BOOLEAN:
3062 xmlGenericError(xmlGenericErrorContext,
3063 "%s is a Boolean\n", arg);
3064 break;
3065 case XPATH_NUMBER:
3066 xmlGenericError(xmlGenericErrorContext,
3067 "%s is a number\n", arg);
3068 break;
3069 case XPATH_STRING:
3070 xmlGenericError(xmlGenericErrorContext,
3071 "%s is a string\n", arg);
3072 break;
3073 case XPATH_POINT:
3074 xmlGenericError(xmlGenericErrorContext,
3075 "%s is a point\n", arg);
3076 break;
3077 case XPATH_RANGE:
3078 xmlGenericError(xmlGenericErrorContext,
3079 "%s is a range\n", arg);
3080 break;
3081 case XPATH_LOCATIONSET:
3082 xmlGenericError(xmlGenericErrorContext,
3083 "%s is a range\n", arg);
3084 break;
3085 case XPATH_USERS:
3086 xmlGenericError(xmlGenericErrorContext,
3087 "%s is user-defined\n", arg);
3088 break;
3089 case XPATH_XSLT_TREE:
3090 xmlGenericError(xmlGenericErrorContext,
3091 "%s is an XSLT value tree\n",
3092 arg);
3093 break;
3094 }
3095#ifdef LIBXML_XPATH_ENABLED
3096 xmlXPathFreeObject(list);
3097#endif
3098 } else {
3099 xmlGenericError(xmlGenericErrorContext,
3100 "%s: no such node\n", arg);
3101 }
3102 ctxt->pctxt->node = NULL;
3103 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003104#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00003105 } else if (!strcmp(command, "cat")) {
3106 if (arg[0] == 0) {
3107 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3108 } else {
3109 ctxt->pctxt->node = ctxt->node;
3110#ifdef LIBXML_XPATH_ENABLED
3111 ctxt->pctxt->node = ctxt->node;
3112 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3113#else
3114 list = NULL;
3115#endif /* LIBXML_XPATH_ENABLED */
3116 if (list != NULL) {
3117 switch (list->type) {
3118 case XPATH_UNDEFINED:
3119 xmlGenericError(xmlGenericErrorContext,
3120 "%s: no such node\n", arg);
3121 break;
3122 case XPATH_NODESET:{
3123 int indx;
3124
Daniel Veillarda6825e82001-11-07 13:33:59 +00003125 if (list->nodesetval == NULL)
3126 break;
3127
Daniel Veillard78d12092001-10-11 09:12:24 +00003128 for (indx = 0;
3129 indx < list->nodesetval->nodeNr;
3130 indx++) {
3131 if (i > 0)
Daniel Veillard321be0c2002-10-08 21:26:42 +00003132 fprintf(ctxt->output, " -------\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00003133 xmlShellCat(ctxt, NULL,
3134 list->nodesetval->
3135 nodeTab[indx], NULL);
3136 }
3137 break;
3138 }
3139 case XPATH_BOOLEAN:
3140 xmlGenericError(xmlGenericErrorContext,
3141 "%s is a Boolean\n", arg);
3142 break;
3143 case XPATH_NUMBER:
3144 xmlGenericError(xmlGenericErrorContext,
3145 "%s is a number\n", arg);
3146 break;
3147 case XPATH_STRING:
3148 xmlGenericError(xmlGenericErrorContext,
3149 "%s is a string\n", arg);
3150 break;
3151 case XPATH_POINT:
3152 xmlGenericError(xmlGenericErrorContext,
3153 "%s is a point\n", arg);
3154 break;
3155 case XPATH_RANGE:
3156 xmlGenericError(xmlGenericErrorContext,
3157 "%s is a range\n", arg);
3158 break;
3159 case XPATH_LOCATIONSET:
3160 xmlGenericError(xmlGenericErrorContext,
3161 "%s is a range\n", arg);
3162 break;
3163 case XPATH_USERS:
3164 xmlGenericError(xmlGenericErrorContext,
3165 "%s is user-defined\n", arg);
3166 break;
3167 case XPATH_XSLT_TREE:
3168 xmlGenericError(xmlGenericErrorContext,
3169 "%s is an XSLT value tree\n",
3170 arg);
3171 break;
3172 }
3173#ifdef LIBXML_XPATH_ENABLED
3174 xmlXPathFreeObject(list);
3175#endif
3176 } else {
3177 xmlGenericError(xmlGenericErrorContext,
3178 "%s: no such node\n", arg);
3179 }
3180 ctxt->pctxt->node = NULL;
3181 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003182#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00003183 } else {
3184 xmlGenericError(xmlGenericErrorContext,
3185 "Unknown command %s\n", command);
3186 }
3187 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003188 }
3189#ifdef LIBXML_XPATH_ENABLED
3190 xmlXPathFreeContext(ctxt->pctxt);
3191#endif /* LIBXML_XPATH_ENABLED */
3192 if (ctxt->loaded) {
3193 xmlFreeDoc(ctxt->doc);
3194 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003195 if (ctxt->filename != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003196 xmlFree(ctxt->filename);
Owen Taylor3473f882001-02-23 17:55:21 +00003197 xmlFree(ctxt);
3198 if (cmdline != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003199 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003200}
3201
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00003202#endif /* LIBXML_XPATH_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003203#endif /* LIBXML_DEBUG_ENABLED */