blob: 24c7c4df3aa2d3fc7409d25f13a6675610b6c69f [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) &&
Daniel Veillardf2e066a2005-06-30 13:04:44 +0000347 (node->type != XML_ELEMENT_DECL) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000348 (node->type != XML_ATTRIBUTE_DECL) &&
349 (node->type != XML_DTD_NODE) &&
William M. Brack0357a302005-07-06 17:39:14 +0000350 (node->type != XML_ELEMENT_DECL) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000351 (node->type != XML_HTML_DOCUMENT_NODE) &&
352 (node->type != XML_DOCUMENT_NODE)) {
Daniel Veillardc6095782004-10-15 14:50:10 +0000353 if (node->content != NULL)
William M. Brack9638d4c2004-10-15 18:25:33 +0000354 xmlCtxtCheckString(ctxt, (const xmlChar *) node->content);
Daniel Veillardc6095782004-10-15 14:50:10 +0000355 }
Daniel Veillard03a53c32004-10-26 16:06:51 +0000356 switch (node->type) {
357 case XML_ELEMENT_NODE:
358 case XML_ATTRIBUTE_NODE:
359 xmlCtxtCheckName(ctxt, node->name);
360 break;
361 case XML_TEXT_NODE:
362 if ((node->name == xmlStringText) ||
363 (node->name == xmlStringTextNoenc))
364 break;
365 /* some case of entity substitution can lead to this */
366 if ((ctxt->dict != NULL) &&
Daniel Veillard6927b102004-10-27 17:29:04 +0000367 (node->name == xmlDictLookup(ctxt->dict, BAD_CAST "nbktext",
368 7)))
Daniel Veillard03a53c32004-10-26 16:06:51 +0000369 break;
370
371 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
372 "Text node has wrong name '%s'",
373 (const char *) node->name);
374 break;
375 case XML_COMMENT_NODE:
376 if (node->name == xmlStringComment)
377 break;
378 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
379 "Comment node has wrong name '%s'",
380 (const char *) node->name);
381 break;
382 case XML_PI_NODE:
383 xmlCtxtCheckName(ctxt, node->name);
384 break;
385 case XML_CDATA_SECTION_NODE:
386 if (node->name == NULL)
387 break;
388 xmlDebugErr3(ctxt, XML_CHECK_NAME_NOT_NULL,
389 "CData section has non NULL name '%s'",
390 (const char *) node->name);
391 break;
392 case XML_ENTITY_REF_NODE:
393 case XML_ENTITY_NODE:
394 case XML_DOCUMENT_TYPE_NODE:
395 case XML_DOCUMENT_FRAG_NODE:
396 case XML_NOTATION_NODE:
397 case XML_DTD_NODE:
398 case XML_ELEMENT_DECL:
399 case XML_ATTRIBUTE_DECL:
400 case XML_ENTITY_DECL:
401 case XML_NAMESPACE_DECL:
402 case XML_XINCLUDE_START:
403 case XML_XINCLUDE_END:
404#ifdef LIBXML_DOCB_ENABLED
405 case XML_DOCB_DOCUMENT_NODE:
406#endif
407 case XML_DOCUMENT_NODE:
408 case XML_HTML_DOCUMENT_NODE:
409 break;
410 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000411}
412
Daniel Veillard22cdb842004-10-04 14:09:17 +0000413static void
414xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
415{
416 int i;
417
Daniel Veillardc6095782004-10-15 14:50:10 +0000418 if (ctxt->check) {
Daniel Veillard22cdb842004-10-04 14:09:17 +0000419 return;
Daniel Veillardc6095782004-10-15 14:50:10 +0000420 }
Daniel Veillard22cdb842004-10-04 14:09:17 +0000421 /* TODO: check UTF8 content of the string */
422 if (str == NULL) {
423 fprintf(ctxt->output, "(NULL)");
424 return;
425 }
426 for (i = 0; i < 40; i++)
427 if (str[i] == 0)
428 return;
429 else if (IS_BLANK_CH(str[i]))
430 fputc(' ', ctxt->output);
431 else if (str[i] >= 0x80)
432 fprintf(ctxt->output, "#%X", str[i]);
433 else
434 fputc(str[i], ctxt->output);
435 fprintf(ctxt->output, "...");
436}
437
438static void
439xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
440{
441 xmlCtxtDumpSpaces(ctxt);
442
443 if (dtd == NULL) {
444 if (!ctxt->check)
445 fprintf(ctxt->output, "DTD node is NULL\n");
446 return;
447 }
448
449 if (dtd->type != XML_DTD_NODE) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000450 xmlDebugErr(ctxt, XML_CHECK_NOT_DTD,
451 "Node is not a DTD");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000452 return;
453 }
454 if (!ctxt->check) {
455 if (dtd->name != NULL)
456 fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name);
457 else
458 fprintf(ctxt->output, "DTD");
459 if (dtd->ExternalID != NULL)
460 fprintf(ctxt->output, ", PUBLIC %s", (char *) dtd->ExternalID);
461 if (dtd->SystemID != NULL)
462 fprintf(ctxt->output, ", SYSTEM %s", (char *) dtd->SystemID);
463 fprintf(ctxt->output, "\n");
464 }
465 /*
466 * Do a bit of checking
467 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000468 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000469}
470
471static void
472xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr)
473{
474 xmlCtxtDumpSpaces(ctxt);
475
476 if (attr == NULL) {
477 if (!ctxt->check)
478 fprintf(ctxt->output, "Attribute declaration is NULL\n");
479 return;
480 }
481 if (attr->type != XML_ATTRIBUTE_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000482 xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL,
483 "Node is not an attribute declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000484 return;
485 }
486 if (attr->name != NULL) {
487 if (!ctxt->check)
488 fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name);
489 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000490 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
491 "Node attribute declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000492 if (attr->elem != NULL) {
493 if (!ctxt->check)
494 fprintf(ctxt->output, " for %s", (char *) attr->elem);
495 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000496 xmlDebugErr(ctxt, XML_CHECK_NO_ELEM,
497 "Node attribute declaration has no element name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000498 if (!ctxt->check) {
499 switch (attr->atype) {
500 case XML_ATTRIBUTE_CDATA:
501 fprintf(ctxt->output, " CDATA");
502 break;
503 case XML_ATTRIBUTE_ID:
504 fprintf(ctxt->output, " ID");
505 break;
506 case XML_ATTRIBUTE_IDREF:
507 fprintf(ctxt->output, " IDREF");
508 break;
509 case XML_ATTRIBUTE_IDREFS:
510 fprintf(ctxt->output, " IDREFS");
511 break;
512 case XML_ATTRIBUTE_ENTITY:
513 fprintf(ctxt->output, " ENTITY");
514 break;
515 case XML_ATTRIBUTE_ENTITIES:
516 fprintf(ctxt->output, " ENTITIES");
517 break;
518 case XML_ATTRIBUTE_NMTOKEN:
519 fprintf(ctxt->output, " NMTOKEN");
520 break;
521 case XML_ATTRIBUTE_NMTOKENS:
522 fprintf(ctxt->output, " NMTOKENS");
523 break;
524 case XML_ATTRIBUTE_ENUMERATION:
525 fprintf(ctxt->output, " ENUMERATION");
526 break;
527 case XML_ATTRIBUTE_NOTATION:
528 fprintf(ctxt->output, " NOTATION ");
529 break;
530 }
531 if (attr->tree != NULL) {
532 int indx;
533 xmlEnumerationPtr cur = attr->tree;
534
535 for (indx = 0; indx < 5; indx++) {
536 if (indx != 0)
537 fprintf(ctxt->output, "|%s", (char *) cur->name);
538 else
539 fprintf(ctxt->output, " (%s", (char *) cur->name);
540 cur = cur->next;
541 if (cur == NULL)
542 break;
543 }
544 if (cur == NULL)
545 fprintf(ctxt->output, ")");
546 else
547 fprintf(ctxt->output, "...)");
548 }
549 switch (attr->def) {
550 case XML_ATTRIBUTE_NONE:
551 break;
552 case XML_ATTRIBUTE_REQUIRED:
553 fprintf(ctxt->output, " REQUIRED");
554 break;
555 case XML_ATTRIBUTE_IMPLIED:
556 fprintf(ctxt->output, " IMPLIED");
557 break;
558 case XML_ATTRIBUTE_FIXED:
559 fprintf(ctxt->output, " FIXED");
560 break;
561 }
562 if (attr->defaultValue != NULL) {
563 fprintf(ctxt->output, "\"");
564 xmlCtxtDumpString(ctxt, attr->defaultValue);
565 fprintf(ctxt->output, "\"");
566 }
567 fprintf(ctxt->output, "\n");
568 }
569
570 /*
571 * Do a bit of checking
572 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000573 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000574}
575
576static void
577xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem)
578{
579 xmlCtxtDumpSpaces(ctxt);
580
581 if (elem == NULL) {
582 if (!ctxt->check)
583 fprintf(ctxt->output, "Element declaration is NULL\n");
584 return;
585 }
586 if (elem->type != XML_ELEMENT_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000587 xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL,
588 "Node is not an element declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000589 return;
590 }
591 if (elem->name != NULL) {
592 if (!ctxt->check) {
593 fprintf(ctxt->output, "ELEMDECL(");
594 xmlCtxtDumpString(ctxt, elem->name);
595 fprintf(ctxt->output, ")");
596 }
597 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000598 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
599 "Element declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000600 if (!ctxt->check) {
601 switch (elem->etype) {
602 case XML_ELEMENT_TYPE_UNDEFINED:
603 fprintf(ctxt->output, ", UNDEFINED");
604 break;
605 case XML_ELEMENT_TYPE_EMPTY:
606 fprintf(ctxt->output, ", EMPTY");
607 break;
608 case XML_ELEMENT_TYPE_ANY:
609 fprintf(ctxt->output, ", ANY");
610 break;
611 case XML_ELEMENT_TYPE_MIXED:
612 fprintf(ctxt->output, ", MIXED ");
613 break;
614 case XML_ELEMENT_TYPE_ELEMENT:
615 fprintf(ctxt->output, ", MIXED ");
616 break;
617 }
618 if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) {
619 char buf[5001];
620
621 buf[0] = 0;
622 xmlSnprintfElementContent(buf, 5000, elem->content, 1);
623 buf[5000] = 0;
624 fprintf(ctxt->output, "%s", buf);
625 }
626 fprintf(ctxt->output, "\n");
627 }
628
629 /*
630 * Do a bit of checking
631 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000632 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000633}
634
635static void
636xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
637{
638 xmlCtxtDumpSpaces(ctxt);
639
640 if (ent == NULL) {
641 if (!ctxt->check)
642 fprintf(ctxt->output, "Entity declaration is NULL\n");
643 return;
644 }
645 if (ent->type != XML_ENTITY_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000646 xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL,
647 "Node is not an entity declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000648 return;
649 }
650 if (ent->name != NULL) {
651 if (!ctxt->check) {
652 fprintf(ctxt->output, "ENTITYDECL(");
653 xmlCtxtDumpString(ctxt, ent->name);
654 fprintf(ctxt->output, ")");
655 }
656 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000657 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
658 "Entity declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000659 if (!ctxt->check) {
660 switch (ent->etype) {
661 case XML_INTERNAL_GENERAL_ENTITY:
662 fprintf(ctxt->output, ", internal\n");
663 break;
664 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
665 fprintf(ctxt->output, ", external parsed\n");
666 break;
667 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
668 fprintf(ctxt->output, ", unparsed\n");
669 break;
670 case XML_INTERNAL_PARAMETER_ENTITY:
671 fprintf(ctxt->output, ", parameter\n");
672 break;
673 case XML_EXTERNAL_PARAMETER_ENTITY:
674 fprintf(ctxt->output, ", external parameter\n");
675 break;
676 case XML_INTERNAL_PREDEFINED_ENTITY:
677 fprintf(ctxt->output, ", predefined\n");
678 break;
679 }
680 if (ent->ExternalID) {
681 xmlCtxtDumpSpaces(ctxt);
682 fprintf(ctxt->output, " ExternalID=%s\n",
683 (char *) ent->ExternalID);
684 }
685 if (ent->SystemID) {
686 xmlCtxtDumpSpaces(ctxt);
687 fprintf(ctxt->output, " SystemID=%s\n",
688 (char *) ent->SystemID);
689 }
690 if (ent->URI != NULL) {
691 xmlCtxtDumpSpaces(ctxt);
692 fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI);
693 }
694 if (ent->content) {
695 xmlCtxtDumpSpaces(ctxt);
696 fprintf(ctxt->output, " content=");
697 xmlCtxtDumpString(ctxt, ent->content);
698 fprintf(ctxt->output, "\n");
699 }
700 }
701
702 /*
703 * Do a bit of checking
704 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000705 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000706}
707
708static void
709xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
710{
711 xmlCtxtDumpSpaces(ctxt);
712
713 if (ns == NULL) {
714 if (!ctxt->check)
715 fprintf(ctxt->output, "namespace node is NULL\n");
716 return;
717 }
718 if (ns->type != XML_NAMESPACE_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000719 xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL,
720 "Node is not a namespace declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000721 return;
722 }
723 if (ns->href == NULL) {
724 if (ns->prefix != NULL)
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000725 xmlDebugErr3(ctxt, XML_CHECK_NO_HREF,
726 "Incomplete namespace %s href=NULL\n",
Daniel Veillard22cdb842004-10-04 14:09:17 +0000727 (char *) ns->prefix);
728 else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000729 xmlDebugErr(ctxt, XML_CHECK_NO_HREF,
730 "Incomplete default namespace href=NULL\n");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000731 } else {
732 if (!ctxt->check) {
733 if (ns->prefix != NULL)
734 fprintf(ctxt->output, "namespace %s href=",
735 (char *) ns->prefix);
736 else
737 fprintf(ctxt->output, "default namespace href=");
738
739 xmlCtxtDumpString(ctxt, ns->href);
740 fprintf(ctxt->output, "\n");
741 }
742 }
743}
744
745static void
746xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
747{
748 while (ns != NULL) {
749 xmlCtxtDumpNamespace(ctxt, ns);
750 ns = ns->next;
751 }
752}
753
754static void
755xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
756{
757 xmlCtxtDumpSpaces(ctxt);
758
759 if (ent == NULL) {
760 if (!ctxt->check)
761 fprintf(ctxt->output, "Entity is NULL\n");
762 return;
763 }
764 if (!ctxt->check) {
765 switch (ent->etype) {
766 case XML_INTERNAL_GENERAL_ENTITY:
767 fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY ");
768 break;
769 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
770 fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
771 break;
772 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
773 fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
774 break;
775 case XML_INTERNAL_PARAMETER_ENTITY:
776 fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY ");
777 break;
778 case XML_EXTERNAL_PARAMETER_ENTITY:
779 fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY ");
780 break;
781 default:
782 fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype);
783 }
784 fprintf(ctxt->output, "%s\n", ent->name);
785 if (ent->ExternalID) {
786 xmlCtxtDumpSpaces(ctxt);
787 fprintf(ctxt->output, "ExternalID=%s\n",
788 (char *) ent->ExternalID);
789 }
790 if (ent->SystemID) {
791 xmlCtxtDumpSpaces(ctxt);
792 fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID);
793 }
794 if (ent->URI) {
795 xmlCtxtDumpSpaces(ctxt);
796 fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI);
797 }
798 if (ent->content) {
799 xmlCtxtDumpSpaces(ctxt);
800 fprintf(ctxt->output, "content=");
801 xmlCtxtDumpString(ctxt, ent->content);
802 fprintf(ctxt->output, "\n");
803 }
804 }
805}
806
807/**
808 * xmlCtxtDumpAttr:
809 * @output: the FILE * for the output
810 * @attr: the attribute
811 * @depth: the indentation level.
812 *
813 * Dumps debug information for the attribute
814 */
815static void
816xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
817{
818 xmlCtxtDumpSpaces(ctxt);
819
820 if (attr == NULL) {
821 if (!ctxt->check)
822 fprintf(ctxt->output, "Attr is NULL");
823 return;
824 }
825 if (!ctxt->check) {
826 fprintf(ctxt->output, "ATTRIBUTE ");
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000827 xmlCtxtDumpString(ctxt, attr->name);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000828 fprintf(ctxt->output, "\n");
829 if (attr->children != NULL) {
830 ctxt->depth++;
831 xmlCtxtDumpNodeList(ctxt, attr->children);
832 ctxt->depth--;
833 }
834 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000835 if (attr->name == NULL)
836 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
837 "Attribute has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000838
839 /*
840 * Do a bit of checking
841 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000842 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000843}
844
845/**
846 * xmlCtxtDumpAttrList:
847 * @output: the FILE * for the output
848 * @attr: the attribute list
849 * @depth: the indentation level.
850 *
851 * Dumps debug information for the attribute list
852 */
853static void
854xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
855{
856 while (attr != NULL) {
857 xmlCtxtDumpAttr(ctxt, attr);
858 attr = attr->next;
859 }
860}
861
862/**
863 * xmlCtxtDumpOneNode:
864 * @output: the FILE * for the output
865 * @node: the node
866 * @depth: the indentation level.
867 *
868 * Dumps debug information for the element node, it is not recursive
869 */
870static void
871xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
872{
873 if (node == NULL) {
874 if (!ctxt->check) {
875 xmlCtxtDumpSpaces(ctxt);
876 fprintf(ctxt->output, "node is NULL\n");
877 }
878 return;
879 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000880 ctxt->node = node;
881
Daniel Veillard22cdb842004-10-04 14:09:17 +0000882 switch (node->type) {
883 case XML_ELEMENT_NODE:
884 if (!ctxt->check) {
885 xmlCtxtDumpSpaces(ctxt);
886 fprintf(ctxt->output, "ELEMENT ");
887 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
888 xmlCtxtDumpString(ctxt, node->ns->prefix);
889 fprintf(ctxt->output, ":");
890 }
891 xmlCtxtDumpString(ctxt, node->name);
892 fprintf(ctxt->output, "\n");
893 }
894 break;
895 case XML_ATTRIBUTE_NODE:
896 if (!ctxt->check)
897 xmlCtxtDumpSpaces(ctxt);
898 fprintf(ctxt->output, "Error, ATTRIBUTE found here\n");
William M. Brack5a9c1fd2004-12-17 21:38:09 +0000899 xmlCtxtGenericNodeCheck(ctxt, node);
900 return;
Daniel Veillard22cdb842004-10-04 14:09:17 +0000901 case XML_TEXT_NODE:
902 if (!ctxt->check) {
903 xmlCtxtDumpSpaces(ctxt);
904 if (node->name == (const xmlChar *) xmlStringTextNoenc)
Daniel Veillard8874b942005-08-25 13:19:21 +0000905 fprintf(ctxt->output, "TEXT no enc");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000906 else
Daniel Veillard8874b942005-08-25 13:19:21 +0000907 fprintf(ctxt->output, "TEXT");
908 if (node->content == (xmlChar *) &(node->properties))
909 fprintf(ctxt->output, " compact\n");
910 else if (xmlDictOwns(ctxt->dict, node->content) == 1)
911 fprintf(ctxt->output, " interned\n");
912 else
913 fprintf(ctxt->output, "\n");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000914 }
915 break;
916 case XML_CDATA_SECTION_NODE:
917 if (!ctxt->check) {
918 xmlCtxtDumpSpaces(ctxt);
919 fprintf(ctxt->output, "CDATA_SECTION\n");
920 }
921 break;
922 case XML_ENTITY_REF_NODE:
923 if (!ctxt->check) {
924 xmlCtxtDumpSpaces(ctxt);
925 fprintf(ctxt->output, "ENTITY_REF(%s)\n",
926 (char *) node->name);
927 }
928 break;
929 case XML_ENTITY_NODE:
930 if (!ctxt->check) {
931 xmlCtxtDumpSpaces(ctxt);
932 fprintf(ctxt->output, "ENTITY\n");
933 }
934 break;
935 case XML_PI_NODE:
936 if (!ctxt->check) {
937 xmlCtxtDumpSpaces(ctxt);
938 fprintf(ctxt->output, "PI %s\n", (char *) node->name);
939 }
940 break;
941 case XML_COMMENT_NODE:
942 if (!ctxt->check) {
943 xmlCtxtDumpSpaces(ctxt);
944 fprintf(ctxt->output, "COMMENT\n");
945 }
946 break;
947 case XML_DOCUMENT_NODE:
948 case XML_HTML_DOCUMENT_NODE:
949 if (!ctxt->check) {
950 xmlCtxtDumpSpaces(ctxt);
951 }
William M. Brack5a9c1fd2004-12-17 21:38:09 +0000952 fprintf(ctxt->output, "Error, DOCUMENT found here\n");
953 xmlCtxtGenericNodeCheck(ctxt, node);
954 return;
Daniel Veillard22cdb842004-10-04 14:09:17 +0000955 case XML_DOCUMENT_TYPE_NODE:
956 if (!ctxt->check) {
957 xmlCtxtDumpSpaces(ctxt);
958 fprintf(ctxt->output, "DOCUMENT_TYPE\n");
959 }
960 break;
961 case XML_DOCUMENT_FRAG_NODE:
962 if (!ctxt->check) {
963 xmlCtxtDumpSpaces(ctxt);
964 fprintf(ctxt->output, "DOCUMENT_FRAG\n");
965 }
966 break;
967 case XML_NOTATION_NODE:
968 if (!ctxt->check) {
969 xmlCtxtDumpSpaces(ctxt);
970 fprintf(ctxt->output, "NOTATION\n");
971 }
972 break;
973 case XML_DTD_NODE:
974 xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
975 return;
976 case XML_ELEMENT_DECL:
977 xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
978 return;
979 case XML_ATTRIBUTE_DECL:
980 xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
981 return;
982 case XML_ENTITY_DECL:
983 xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
984 return;
985 case XML_NAMESPACE_DECL:
986 xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
987 return;
988 case XML_XINCLUDE_START:
989 if (!ctxt->check) {
990 xmlCtxtDumpSpaces(ctxt);
991 fprintf(ctxt->output, "INCLUDE START\n");
992 }
993 return;
994 case XML_XINCLUDE_END:
995 if (!ctxt->check) {
996 xmlCtxtDumpSpaces(ctxt);
997 fprintf(ctxt->output, "INCLUDE END\n");
998 }
999 return;
1000 default:
1001 if (!ctxt->check)
1002 xmlCtxtDumpSpaces(ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001003 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1004 "Unknown node type %d\n", node->type);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001005 return;
1006 }
1007 if (node->doc == NULL) {
1008 if (!ctxt->check) {
1009 xmlCtxtDumpSpaces(ctxt);
1010 }
1011 fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
1012 }
1013 ctxt->depth++;
Daniel Veillard8874b942005-08-25 13:19:21 +00001014 if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL))
Daniel Veillard22cdb842004-10-04 14:09:17 +00001015 xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
Daniel Veillard8874b942005-08-25 13:19:21 +00001016 if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL))
Daniel Veillard22cdb842004-10-04 14:09:17 +00001017 xmlCtxtDumpAttrList(ctxt, node->properties);
1018 if (node->type != XML_ENTITY_REF_NODE) {
1019 if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
1020 if (!ctxt->check) {
1021 xmlCtxtDumpSpaces(ctxt);
1022 fprintf(ctxt->output, "content=");
1023 xmlCtxtDumpString(ctxt, node->content);
1024 fprintf(ctxt->output, "\n");
1025 }
1026 }
1027 } else {
1028 xmlEntityPtr ent;
1029
1030 ent = xmlGetDocEntity(node->doc, node->name);
1031 if (ent != NULL)
1032 xmlCtxtDumpEntity(ctxt, ent);
1033 }
1034 ctxt->depth--;
1035
1036 /*
1037 * Do a bit of checking
1038 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001039 xmlCtxtGenericNodeCheck(ctxt, node);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001040}
1041
1042/**
1043 * xmlCtxtDumpNode:
1044 * @output: the FILE * for the output
1045 * @node: the node
1046 * @depth: the indentation level.
1047 *
1048 * Dumps debug information for the element node, it is recursive
1049 */
1050static void
1051xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1052{
1053 if (node == NULL) {
1054 if (!ctxt->check) {
1055 xmlCtxtDumpSpaces(ctxt);
1056 fprintf(ctxt->output, "node is NULL\n");
1057 }
1058 return;
1059 }
1060 xmlCtxtDumpOneNode(ctxt, node);
1061 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
1062 ctxt->depth++;
1063 xmlCtxtDumpNodeList(ctxt, node->children);
1064 ctxt->depth--;
1065 }
1066}
1067
1068/**
1069 * xmlCtxtDumpNodeList:
1070 * @output: the FILE * for the output
1071 * @node: the node list
1072 * @depth: the indentation level.
1073 *
1074 * Dumps debug information for the list of element node, it is recursive
1075 */
1076static void
1077xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1078{
1079 while (node != NULL) {
1080 xmlCtxtDumpNode(ctxt, node);
1081 node = node->next;
1082 }
1083}
1084
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001085static void
1086xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1087{
1088 if (doc == NULL) {
1089 if (!ctxt->check)
1090 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1091 return;
1092 }
1093 ctxt->node = (xmlNodePtr) doc;
1094
1095 switch (doc->type) {
1096 case XML_ELEMENT_NODE:
1097 xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
1098 "Misplaced ELEMENT node\n");
1099 break;
1100 case XML_ATTRIBUTE_NODE:
1101 xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
1102 "Misplaced ATTRIBUTE node\n");
1103 break;
1104 case XML_TEXT_NODE:
1105 xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
1106 "Misplaced TEXT node\n");
1107 break;
1108 case XML_CDATA_SECTION_NODE:
1109 xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
1110 "Misplaced CDATA node\n");
1111 break;
1112 case XML_ENTITY_REF_NODE:
1113 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
1114 "Misplaced ENTITYREF node\n");
1115 break;
1116 case XML_ENTITY_NODE:
1117 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
1118 "Misplaced ENTITY node\n");
1119 break;
1120 case XML_PI_NODE:
1121 xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
1122 "Misplaced PI node\n");
1123 break;
1124 case XML_COMMENT_NODE:
1125 xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
1126 "Misplaced COMMENT node\n");
1127 break;
1128 case XML_DOCUMENT_NODE:
1129 if (!ctxt->check)
1130 fprintf(ctxt->output, "DOCUMENT\n");
1131 break;
1132 case XML_HTML_DOCUMENT_NODE:
1133 if (!ctxt->check)
1134 fprintf(ctxt->output, "HTML DOCUMENT\n");
1135 break;
1136 case XML_DOCUMENT_TYPE_NODE:
1137 xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
1138 "Misplaced DOCTYPE node\n");
1139 break;
1140 case XML_DOCUMENT_FRAG_NODE:
1141 xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
1142 "Misplaced FRAGMENT node\n");
1143 break;
1144 case XML_NOTATION_NODE:
1145 xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
1146 "Misplaced NOTATION node\n");
1147 break;
1148 default:
1149 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1150 "Unknown node type %d\n", doc->type);
1151 }
1152}
Daniel Veillard22cdb842004-10-04 14:09:17 +00001153
1154/**
1155 * xmlCtxtDumpDocumentHead:
1156 * @output: the FILE * for the output
1157 * @doc: the document
1158 *
1159 * Dumps debug information cncerning the document, not recursive
1160 */
1161static void
1162xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1163{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001164 if (doc == NULL) return;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001165 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001166 if (!ctxt->check) {
1167 if (doc->name != NULL) {
1168 fprintf(ctxt->output, "name=");
1169 xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
1170 fprintf(ctxt->output, "\n");
1171 }
1172 if (doc->version != NULL) {
1173 fprintf(ctxt->output, "version=");
1174 xmlCtxtDumpString(ctxt, doc->version);
1175 fprintf(ctxt->output, "\n");
1176 }
1177 if (doc->encoding != NULL) {
1178 fprintf(ctxt->output, "encoding=");
1179 xmlCtxtDumpString(ctxt, doc->encoding);
1180 fprintf(ctxt->output, "\n");
1181 }
1182 if (doc->URL != NULL) {
1183 fprintf(ctxt->output, "URL=");
1184 xmlCtxtDumpString(ctxt, doc->URL);
1185 fprintf(ctxt->output, "\n");
1186 }
1187 if (doc->standalone)
1188 fprintf(ctxt->output, "standalone=true\n");
1189 }
1190 if (doc->oldNs != NULL)
1191 xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
1192}
1193
1194/**
1195 * xmlCtxtDumpDocument:
1196 * @output: the FILE * for the output
1197 * @doc: the document
1198 *
1199 * Dumps debug information for the document, it's recursive
1200 */
1201static void
1202xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1203{
1204 if (doc == NULL) {
1205 if (!ctxt->check)
1206 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1207 return;
1208 }
1209 xmlCtxtDumpDocumentHead(ctxt, doc);
1210 if (((doc->type == XML_DOCUMENT_NODE) ||
1211 (doc->type == XML_HTML_DOCUMENT_NODE))
1212 && (doc->children != NULL)) {
1213 ctxt->depth++;
1214 xmlCtxtDumpNodeList(ctxt, doc->children);
1215 ctxt->depth--;
1216 }
1217}
1218
1219static void
1220xmlCtxtDumpEntityCallback(xmlEntityPtr cur, xmlDebugCtxtPtr ctxt)
1221{
1222 if (cur == NULL) {
1223 if (!ctxt->check)
1224 fprintf(ctxt->output, "Entity is NULL");
1225 return;
1226 }
1227 if (!ctxt->check) {
1228 fprintf(ctxt->output, "%s : ", (char *) cur->name);
1229 switch (cur->etype) {
1230 case XML_INTERNAL_GENERAL_ENTITY:
1231 fprintf(ctxt->output, "INTERNAL GENERAL, ");
1232 break;
1233 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1234 fprintf(ctxt->output, "EXTERNAL PARSED, ");
1235 break;
1236 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1237 fprintf(ctxt->output, "EXTERNAL UNPARSED, ");
1238 break;
1239 case XML_INTERNAL_PARAMETER_ENTITY:
1240 fprintf(ctxt->output, "INTERNAL PARAMETER, ");
1241 break;
1242 case XML_EXTERNAL_PARAMETER_ENTITY:
1243 fprintf(ctxt->output, "EXTERNAL PARAMETER, ");
1244 break;
1245 default:
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001246 xmlDebugErr2(ctxt, XML_CHECK_ENTITY_TYPE,
1247 "Unknown entity type %d\n", cur->etype);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001248 }
1249 if (cur->ExternalID != NULL)
1250 fprintf(ctxt->output, "ID \"%s\"", (char *) cur->ExternalID);
1251 if (cur->SystemID != NULL)
1252 fprintf(ctxt->output, "SYSTEM \"%s\"", (char *) cur->SystemID);
1253 if (cur->orig != NULL)
1254 fprintf(ctxt->output, "\n orig \"%s\"", (char *) cur->orig);
1255 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL))
1256 fprintf(ctxt->output, "\n content \"%s\"",
1257 (char *) cur->content);
1258 fprintf(ctxt->output, "\n");
1259 }
1260}
1261
1262/**
1263 * xmlCtxtDumpEntities:
1264 * @output: the FILE * for the output
1265 * @doc: the document
1266 *
1267 * Dumps debug information for all the entities in use by the document
1268 */
1269static void
1270xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1271{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001272 if (doc == NULL) return;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001273 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001274 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
1275 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1276 doc->intSubset->entities;
1277
1278 if (!ctxt->check)
1279 fprintf(ctxt->output, "Entities in internal subset\n");
1280 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1281 ctxt);
1282 } else
1283 fprintf(ctxt->output, "No entities in internal subset\n");
1284 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
1285 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1286 doc->extSubset->entities;
1287
1288 if (!ctxt->check)
1289 fprintf(ctxt->output, "Entities in external subset\n");
1290 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1291 ctxt);
1292 } else if (!ctxt->check)
1293 fprintf(ctxt->output, "No entities in external subset\n");
1294}
1295
1296/**
1297 * xmlCtxtDumpDTD:
1298 * @output: the FILE * for the output
1299 * @dtd: the DTD
1300 *
1301 * Dumps debug information for the DTD
1302 */
1303static void
1304xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
1305{
1306 if (dtd == NULL) {
1307 if (!ctxt->check)
1308 fprintf(ctxt->output, "DTD is NULL\n");
1309 return;
1310 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001311 xmlCtxtDumpDtdNode(ctxt, dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001312 if (dtd->children == NULL)
1313 fprintf(ctxt->output, " DTD is empty\n");
1314 else {
1315 ctxt->depth++;
1316 xmlCtxtDumpNodeList(ctxt, dtd->children);
1317 ctxt->depth--;
1318 }
1319}
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001320
Daniel Veillard22cdb842004-10-04 14:09:17 +00001321/************************************************************************
1322 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001323 * Public entry points for dump *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001324 * *
1325 ************************************************************************/
1326
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001327/**
1328 * xmlDebugDumpString:
1329 * @output: the FILE * for the output
1330 * @str: the string
1331 *
1332 * Dumps informations about the string, shorten it if necessary
1333 */
1334void
1335xmlDebugDumpString(FILE * output, const xmlChar * str)
1336{
Owen Taylor3473f882001-02-23 17:55:21 +00001337 int i;
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001338
Daniel Veillard7db38712002-02-07 16:39:11 +00001339 if (output == NULL)
1340 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00001341 if (str == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001342 fprintf(output, "(NULL)");
1343 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001344 }
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001345 for (i = 0; i < 40; i++)
1346 if (str[i] == 0)
1347 return;
William M. Brack76e95df2003-10-18 16:20:14 +00001348 else if (IS_BLANK_CH(str[i]))
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001349 fputc(' ', output);
1350 else if (str[i] >= 0x80)
1351 fprintf(output, "#%X", str[i]);
1352 else
1353 fputc(str[i], output);
Owen Taylor3473f882001-02-23 17:55:21 +00001354 fprintf(output, "...");
1355}
1356
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001357/**
1358 * xmlDebugDumpAttr:
1359 * @output: the FILE * for the output
1360 * @attr: the attribute
1361 * @depth: the indentation level.
1362 *
1363 * Dumps debug information for the attribute
1364 */
1365void
1366xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
Daniel Veillard22cdb842004-10-04 14:09:17 +00001367 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001368
Daniel Veillarda82b1822004-11-08 16:24:57 +00001369 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001370 xmlCtxtDumpInitCtxt(&ctxt);
1371 ctxt.output = output;
1372 ctxt.depth = depth;
1373 xmlCtxtDumpAttr(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001374 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001375}
Owen Taylor3473f882001-02-23 17:55:21 +00001376
Owen Taylor3473f882001-02-23 17:55:21 +00001377
Daniel Veillard22cdb842004-10-04 14:09:17 +00001378/**
1379 * xmlDebugDumpEntities:
1380 * @output: the FILE * for the output
1381 * @doc: the document
1382 *
1383 * Dumps debug information for all the entities in use by the document
1384 */
1385void
1386xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
1387{
1388 xmlDebugCtxt ctxt;
1389
Daniel Veillarda82b1822004-11-08 16:24:57 +00001390 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001391 xmlCtxtDumpInitCtxt(&ctxt);
1392 ctxt.output = output;
1393 xmlCtxtDumpEntities(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001394 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001395}
1396
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001397/**
1398 * xmlDebugDumpAttrList:
1399 * @output: the FILE * for the output
1400 * @attr: the attribute list
1401 * @depth: the indentation level.
1402 *
1403 * Dumps debug information for the attribute list
1404 */
1405void
1406xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
1407{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001408 xmlDebugCtxt ctxt;
1409
Daniel Veillarda82b1822004-11-08 16:24:57 +00001410 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001411 xmlCtxtDumpInitCtxt(&ctxt);
1412 ctxt.output = output;
1413 ctxt.depth = depth;
1414 xmlCtxtDumpAttrList(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001415 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001416}
1417
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001418/**
1419 * xmlDebugDumpOneNode:
1420 * @output: the FILE * for the output
1421 * @node: the node
1422 * @depth: the indentation level.
1423 *
1424 * Dumps debug information for the element node, it is not recursive
1425 */
1426void
1427xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
1428{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001429 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001430
Daniel Veillarda82b1822004-11-08 16:24:57 +00001431 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001432 xmlCtxtDumpInitCtxt(&ctxt);
1433 ctxt.output = output;
1434 ctxt.depth = depth;
1435 xmlCtxtDumpOneNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001436 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001437}
1438
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001439/**
1440 * xmlDebugDumpNode:
1441 * @output: the FILE * for the output
1442 * @node: the node
1443 * @depth: the indentation level.
1444 *
1445 * Dumps debug information for the element node, it is recursive
1446 */
1447void
1448xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
1449{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001450 xmlDebugCtxt ctxt;
1451
Daniel Veillard7db38712002-02-07 16:39:11 +00001452 if (output == NULL)
1453 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001454 xmlCtxtDumpInitCtxt(&ctxt);
1455 ctxt.output = output;
1456 ctxt.depth = depth;
1457 xmlCtxtDumpNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001458 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001459}
1460
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001461/**
1462 * xmlDebugDumpNodeList:
1463 * @output: the FILE * for the output
1464 * @node: the node list
1465 * @depth: the indentation level.
1466 *
1467 * Dumps debug information for the list of element node, it is recursive
1468 */
1469void
1470xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
1471{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001472 xmlDebugCtxt ctxt;
1473
Daniel Veillard7db38712002-02-07 16:39:11 +00001474 if (output == NULL)
1475 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001476 xmlCtxtDumpInitCtxt(&ctxt);
1477 ctxt.output = output;
1478 ctxt.depth = depth;
1479 xmlCtxtDumpNodeList(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001480 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001481}
1482
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001483/**
1484 * xmlDebugDumpDocumentHead:
1485 * @output: the FILE * for the output
1486 * @doc: the document
1487 *
1488 * Dumps debug information cncerning the document, not recursive
1489 */
1490void
1491xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
1492{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001493 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001494
Daniel Veillard22cdb842004-10-04 14:09:17 +00001495 if (output == NULL)
1496 output = stdout;
1497 xmlCtxtDumpInitCtxt(&ctxt);
1498 ctxt.output = output;
1499 xmlCtxtDumpDocumentHead(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001500 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001501}
1502
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001503/**
1504 * xmlDebugDumpDocument:
1505 * @output: the FILE * for the output
1506 * @doc: the document
1507 *
1508 * Dumps debug information for the document, it's recursive
1509 */
1510void
1511xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
1512{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001513 xmlDebugCtxt ctxt;
1514
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001515 if (output == NULL)
Daniel Veillard22cdb842004-10-04 14:09:17 +00001516 output = stdout;
1517 xmlCtxtDumpInitCtxt(&ctxt);
1518 ctxt.output = output;
1519 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001520 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001521}
Owen Taylor3473f882001-02-23 17:55:21 +00001522
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001523/**
1524 * xmlDebugDumpDTD:
1525 * @output: the FILE * for the output
1526 * @dtd: the DTD
1527 *
1528 * Dumps debug information for the DTD
1529 */
1530void
1531xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
1532{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001533 xmlDebugCtxt ctxt;
1534
Daniel Veillard7db38712002-02-07 16:39:11 +00001535 if (output == NULL)
1536 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001537 xmlCtxtDumpInitCtxt(&ctxt);
1538 ctxt.output = output;
1539 xmlCtxtDumpDTD(&ctxt, dtd);
Daniel Veillard76821142004-10-09 20:39:04 +00001540 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001541}
1542
Daniel Veillard22cdb842004-10-04 14:09:17 +00001543/************************************************************************
1544 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001545 * Public entry points for checkings *
1546 * *
1547 ************************************************************************/
1548
1549/**
1550 * xmlDebugCheckDocument:
1551 * @output: the FILE * for the output
1552 * @doc: the document
1553 *
1554 * Check the document for potential content problems, and output
1555 * the errors to @output
1556 *
1557 * Returns the number of errors found
1558 */
1559int
1560xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
1561{
1562 xmlDebugCtxt ctxt;
1563
1564 if (output == NULL)
1565 output = stdout;
1566 xmlCtxtDumpInitCtxt(&ctxt);
1567 ctxt.output = output;
1568 ctxt.check = 1;
1569 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001570 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001571 return(ctxt.errors);
1572}
1573
1574/************************************************************************
1575 * *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001576 * Helpers for Shell *
1577 * *
1578 ************************************************************************/
Owen Taylor3473f882001-02-23 17:55:21 +00001579
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001580/**
1581 * xmlLsCountNode:
1582 * @node: the node to count
1583 *
1584 * Count the children of @node.
1585 *
1586 * Returns the number of children of @node.
1587 */
1588int
1589xmlLsCountNode(xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00001590 int ret = 0;
1591 xmlNodePtr list = NULL;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001592
1593 if (node == NULL)
1594 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001595
1596 switch (node->type) {
1597 case XML_ELEMENT_NODE:
1598 list = node->children;
1599 break;
1600 case XML_DOCUMENT_NODE:
1601 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00001602#ifdef LIBXML_DOCB_ENABLED
1603 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001604#endif
1605 list = ((xmlDocPtr) node)->children;
1606 break;
1607 case XML_ATTRIBUTE_NODE:
1608 list = ((xmlAttrPtr) node)->children;
1609 break;
1610 case XML_TEXT_NODE:
1611 case XML_CDATA_SECTION_NODE:
1612 case XML_PI_NODE:
1613 case XML_COMMENT_NODE:
1614 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001615 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001616 }
1617 break;
1618 case XML_ENTITY_REF_NODE:
1619 case XML_DOCUMENT_TYPE_NODE:
1620 case XML_ENTITY_NODE:
1621 case XML_DOCUMENT_FRAG_NODE:
1622 case XML_NOTATION_NODE:
1623 case XML_DTD_NODE:
1624 case XML_ELEMENT_DECL:
1625 case XML_ATTRIBUTE_DECL:
1626 case XML_ENTITY_DECL:
1627 case XML_NAMESPACE_DECL:
1628 case XML_XINCLUDE_START:
1629 case XML_XINCLUDE_END:
1630 ret = 1;
1631 break;
1632 }
1633 for (;list != NULL;ret++)
1634 list = list->next;
1635 return(ret);
1636}
1637
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001638/**
1639 * xmlLsOneNode:
1640 * @output: the FILE * for the output
1641 * @node: the node to dump
1642 *
1643 * Dump to @output the type and name of @node.
1644 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001645void
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001646xmlLsOneNode(FILE *output, xmlNodePtr node) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00001647 if (output == NULL) return;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001648 if (node == NULL) {
1649 fprintf(output, "NULL\n");
1650 return;
1651 }
Owen Taylor3473f882001-02-23 17:55:21 +00001652 switch (node->type) {
1653 case XML_ELEMENT_NODE:
1654 fprintf(output, "-");
1655 break;
1656 case XML_ATTRIBUTE_NODE:
1657 fprintf(output, "a");
1658 break;
1659 case XML_TEXT_NODE:
1660 fprintf(output, "t");
1661 break;
1662 case XML_CDATA_SECTION_NODE:
Daniel Veillard75be0132002-03-13 10:03:35 +00001663 fprintf(output, "C");
Owen Taylor3473f882001-02-23 17:55:21 +00001664 break;
1665 case XML_ENTITY_REF_NODE:
1666 fprintf(output, "e");
1667 break;
1668 case XML_ENTITY_NODE:
1669 fprintf(output, "E");
1670 break;
1671 case XML_PI_NODE:
1672 fprintf(output, "p");
1673 break;
1674 case XML_COMMENT_NODE:
1675 fprintf(output, "c");
1676 break;
1677 case XML_DOCUMENT_NODE:
1678 fprintf(output, "d");
1679 break;
1680 case XML_HTML_DOCUMENT_NODE:
1681 fprintf(output, "h");
1682 break;
1683 case XML_DOCUMENT_TYPE_NODE:
1684 fprintf(output, "T");
1685 break;
1686 case XML_DOCUMENT_FRAG_NODE:
1687 fprintf(output, "F");
1688 break;
1689 case XML_NOTATION_NODE:
1690 fprintf(output, "N");
1691 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001692 case XML_NAMESPACE_DECL:
1693 fprintf(output, "n");
1694 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001695 default:
1696 fprintf(output, "?");
1697 }
Daniel Veillarde6a55192002-01-14 17:11:53 +00001698 if (node->type != XML_NAMESPACE_DECL) {
1699 if (node->properties != NULL)
1700 fprintf(output, "a");
1701 else
1702 fprintf(output, "-");
1703 if (node->nsDef != NULL)
1704 fprintf(output, "n");
1705 else
1706 fprintf(output, "-");
1707 }
Owen Taylor3473f882001-02-23 17:55:21 +00001708
1709 fprintf(output, " %8d ", xmlLsCountNode(node));
1710
1711 switch (node->type) {
1712 case XML_ELEMENT_NODE:
1713 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001714 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001715 break;
1716 case XML_ATTRIBUTE_NODE:
1717 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001718 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001719 break;
1720 case XML_TEXT_NODE:
1721 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001722 xmlDebugDumpString(output, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001723 }
1724 break;
1725 case XML_CDATA_SECTION_NODE:
1726 break;
1727 case XML_ENTITY_REF_NODE:
1728 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001729 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001730 break;
1731 case XML_ENTITY_NODE:
1732 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001733 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001734 break;
1735 case XML_PI_NODE:
1736 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001737 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001738 break;
1739 case XML_COMMENT_NODE:
1740 break;
1741 case XML_DOCUMENT_NODE:
1742 break;
1743 case XML_HTML_DOCUMENT_NODE:
1744 break;
1745 case XML_DOCUMENT_TYPE_NODE:
1746 break;
1747 case XML_DOCUMENT_FRAG_NODE:
1748 break;
1749 case XML_NOTATION_NODE:
1750 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001751 case XML_NAMESPACE_DECL: {
1752 xmlNsPtr ns = (xmlNsPtr) node;
1753
1754 if (ns->prefix == NULL)
William M. Brack13dfa872004-09-18 04:52:08 +00001755 fprintf(output, "default -> %s", (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001756 else
William M. Brack13dfa872004-09-18 04:52:08 +00001757 fprintf(output, "%s -> %s", (char *)ns->prefix,
1758 (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001759 break;
1760 }
Owen Taylor3473f882001-02-23 17:55:21 +00001761 default:
1762 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001763 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001764 }
1765 fprintf(output, "\n");
1766}
1767
Daniel Veillard78d12092001-10-11 09:12:24 +00001768/**
1769 * xmlBoolToText:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001770 * @boolval: a bool to turn into text
Daniel Veillard78d12092001-10-11 09:12:24 +00001771 *
1772 * Convenient way to turn bool into text
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001773 *
1774 * Returns a pointer to either "True" or "False"
1775 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001776const char *
Daniel Veillardebd38c52001-11-01 08:38:12 +00001777xmlBoolToText(int boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001778{
Daniel Veillardebd38c52001-11-01 08:38:12 +00001779 if (boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001780 return("True");
1781 else
1782 return("False");
1783}
1784
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00001785#ifdef LIBXML_XPATH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001786/****************************************************************
1787 * *
1788 * The XML shell related functions *
1789 * *
1790 ****************************************************************/
1791
Daniel Veillard78d12092001-10-11 09:12:24 +00001792
1793
Owen Taylor3473f882001-02-23 17:55:21 +00001794/*
1795 * TODO: Improvement/cleanups for the XML shell
1796 * - allow to shell out an editor on a subpart
1797 * - cleanup function registrations (with help) and calling
1798 * - provide registration routines
1799 */
1800
1801/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001802 * xmlShellPrintXPathError:
Daniel Veillard78d12092001-10-11 09:12:24 +00001803 * @errorType: valid xpath error id
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001804 * @arg: the argument that cause xpath to fail
Daniel Veillard78d12092001-10-11 09:12:24 +00001805 *
1806 * Print the xpath error to libxml default error channel
1807 */
1808void
1809xmlShellPrintXPathError(int errorType, const char *arg)
1810{
1811 const char *default_arg = "Result";
1812
1813 if (!arg)
1814 arg = default_arg;
1815
1816 switch (errorType) {
1817 case XPATH_UNDEFINED:
1818 xmlGenericError(xmlGenericErrorContext,
1819 "%s: no such node\n", arg);
1820 break;
1821
1822 case XPATH_BOOLEAN:
1823 xmlGenericError(xmlGenericErrorContext,
1824 "%s is a Boolean\n", arg);
1825 break;
1826 case XPATH_NUMBER:
1827 xmlGenericError(xmlGenericErrorContext,
1828 "%s is a number\n", arg);
1829 break;
1830 case XPATH_STRING:
1831 xmlGenericError(xmlGenericErrorContext,
1832 "%s is a string\n", arg);
1833 break;
1834 case XPATH_POINT:
1835 xmlGenericError(xmlGenericErrorContext,
1836 "%s is a point\n", arg);
1837 break;
1838 case XPATH_RANGE:
1839 xmlGenericError(xmlGenericErrorContext,
1840 "%s is a range\n", arg);
1841 break;
1842 case XPATH_LOCATIONSET:
1843 xmlGenericError(xmlGenericErrorContext,
1844 "%s is a range\n", arg);
1845 break;
1846 case XPATH_USERS:
1847 xmlGenericError(xmlGenericErrorContext,
1848 "%s is user-defined\n", arg);
1849 break;
1850 case XPATH_XSLT_TREE:
1851 xmlGenericError(xmlGenericErrorContext,
1852 "%s is an XSLT value tree\n", arg);
1853 break;
1854 }
Daniel Veillarda82b1822004-11-08 16:24:57 +00001855#if 0
Daniel Veillard78d12092001-10-11 09:12:24 +00001856 xmlGenericError(xmlGenericErrorContext,
1857 "Try casting the result string function (xpath builtin)\n",
1858 arg);
Daniel Veillarda82b1822004-11-08 16:24:57 +00001859#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00001860}
1861
1862
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001863#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001864/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001865 * xmlShellPrintNodeCtxt:
1866 * @ctxt : a non-null shell context
1867 * @node : a non-null node to print to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001868 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001869 * Print node to the output FILE
1870 */
1871static void
1872xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1873{
Daniel Veillard01992e02002-10-09 10:20:30 +00001874 FILE *fp;
1875
1876 if (!node)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001877 return;
Daniel Veillard01992e02002-10-09 10:20:30 +00001878 if (ctxt == NULL)
1879 fp = stdout;
1880 else
1881 fp = ctxt->output;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001882
1883 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001884 xmlDocDump(fp, (xmlDocPtr) node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001885 else if (node->type == XML_ATTRIBUTE_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001886 xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001887 else
Daniel Veillard01992e02002-10-09 10:20:30 +00001888 xmlElemDump(fp, node->doc, node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001889
Daniel Veillard01992e02002-10-09 10:20:30 +00001890 fprintf(fp, "\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00001891}
1892
1893/**
1894 * xmlShellPrintNode:
1895 * @node : a non-null node to print to the output FILE
1896 *
1897 * Print node to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001898 */
1899void
1900xmlShellPrintNode(xmlNodePtr node)
1901{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001902 xmlShellPrintNodeCtxt(NULL, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001903}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001904#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001905
Daniel Veillard78d12092001-10-11 09:12:24 +00001906/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001907 * xmlShellPrintXPathResultCtxt:
1908 * @ctxt: a valid shell context
Daniel Veillard9d06d302002-01-22 18:15:52 +00001909 * @list: a valid result generated by an xpath evaluation
Daniel Veillard78d12092001-10-11 09:12:24 +00001910 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001911 * Prints result to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001912 */
Daniel Veillard321be0c2002-10-08 21:26:42 +00001913static void
1914xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
Daniel Veillard78d12092001-10-11 09:12:24 +00001915{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001916 if (!ctxt)
1917 return;
Daniel Veillard78d12092001-10-11 09:12:24 +00001918
1919 if (list != NULL) {
1920 switch (list->type) {
1921 case XPATH_NODESET:{
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001922#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001923 int indx;
1924
1925 if (list->nodesetval) {
1926 for (indx = 0; indx < list->nodesetval->nodeNr;
1927 indx++) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001928 xmlShellPrintNodeCtxt(ctxt,
1929 list->nodesetval->nodeTab[indx]);
Daniel Veillard78d12092001-10-11 09:12:24 +00001930 }
1931 } else {
1932 xmlGenericError(xmlGenericErrorContext,
1933 "Empty node set\n");
1934 }
1935 break;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001936#else
1937 xmlGenericError(xmlGenericErrorContext,
1938 "Node set\n");
1939#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001940 }
1941 case XPATH_BOOLEAN:
1942 xmlGenericError(xmlGenericErrorContext,
1943 "Is a Boolean:%s\n",
1944 xmlBoolToText(list->boolval));
1945 break;
1946 case XPATH_NUMBER:
1947 xmlGenericError(xmlGenericErrorContext,
1948 "Is a number:%0g\n", list->floatval);
1949 break;
1950 case XPATH_STRING:
1951 xmlGenericError(xmlGenericErrorContext,
1952 "Is a string:%s\n", list->stringval);
1953 break;
1954
1955 default:
1956 xmlShellPrintXPathError(list->type, NULL);
1957 }
1958 }
1959}
1960
1961/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001962 * xmlShellPrintXPathResult:
1963 * @list: a valid result generated by an xpath evaluation
1964 *
1965 * Prints result to the output FILE
1966 */
1967void
1968xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1969{
1970 xmlShellPrintXPathResultCtxt(NULL, list);
1971}
1972
1973/**
Owen Taylor3473f882001-02-23 17:55:21 +00001974 * xmlShellList:
1975 * @ctxt: the shell context
1976 * @arg: unused
1977 * @node: a node
1978 * @node2: unused
1979 *
1980 * Implements the XML shell function "ls"
1981 * Does an Unix like listing of the given node (like a directory)
1982 *
1983 * Returns 0
1984 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001985int
Daniel Veillard321be0c2002-10-08 21:26:42 +00001986xmlShellList(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00001987 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1988 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1989{
Owen Taylor3473f882001-02-23 17:55:21 +00001990 xmlNodePtr cur;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001991 if (!ctxt)
1992 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001993 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001994 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001995 return (0);
1996 }
Owen Taylor3473f882001-02-23 17:55:21 +00001997 if ((node->type == XML_DOCUMENT_NODE) ||
1998 (node->type == XML_HTML_DOCUMENT_NODE)) {
1999 cur = ((xmlDocPtr) node)->children;
Daniel Veillarde6a55192002-01-14 17:11:53 +00002000 } else if (node->type == XML_NAMESPACE_DECL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002001 xmlLsOneNode(ctxt->output, node);
Daniel Veillarde6a55192002-01-14 17:11:53 +00002002 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002003 } else if (node->children != NULL) {
2004 cur = node->children;
2005 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002006 xmlLsOneNode(ctxt->output, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002007 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002008 }
2009 while (cur != NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002010 xmlLsOneNode(ctxt->output, cur);
Daniel Veillard78d12092001-10-11 09:12:24 +00002011 cur = cur->next;
Owen Taylor3473f882001-02-23 17:55:21 +00002012 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002013 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002014}
2015
2016/**
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002017 * xmlShellBase:
2018 * @ctxt: the shell context
2019 * @arg: unused
2020 * @node: a node
2021 * @node2: unused
2022 *
2023 * Implements the XML shell function "base"
2024 * dumps the current XML base of the node
2025 *
2026 * Returns 0
2027 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002028int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002029xmlShellBase(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002030 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2031 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2032{
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002033 xmlChar *base;
Daniel Veillard321be0c2002-10-08 21:26:42 +00002034 if (!ctxt)
2035 return 0;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002036 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002037 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002038 return (0);
2039 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002040
2041 base = xmlNodeGetBase(node->doc, node);
2042
2043 if (base == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002044 fprintf(ctxt->output, " No base found !!!\n");
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002045 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002046 fprintf(ctxt->output, "%s\n", base);
Daniel Veillard78d12092001-10-11 09:12:24 +00002047 xmlFree(base);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002048 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002049 return (0);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002050}
2051
Daniel Veillardb34321c2004-03-04 17:09:47 +00002052#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002053/**
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002054 * xmlShellSetBase:
2055 * @ctxt: the shell context
2056 * @arg: the new base
2057 * @node: a node
2058 * @node2: unused
2059 *
2060 * Implements the XML shell function "setbase"
2061 * change the current XML base of the node
2062 *
2063 * Returns 0
2064 */
2065static int
2066xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2067 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2068 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2069{
2070 xmlNodeSetBase(node, (xmlChar*) arg);
2071 return (0);
2072}
Daniel Veillard2156d432004-03-04 15:59:36 +00002073#endif
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002074
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002075#ifdef LIBXML_XPATH_ENABLED
2076/**
2077 * xmlShellRegisterNamespace:
2078 * @ctxt: the shell context
2079 * @arg: a string in prefix=nsuri format
2080 * @node: unused
2081 * @node2: unused
2082 *
2083 * Implements the XML shell function "setns"
2084 * register/unregister a prefix=namespace pair
2085 * on the XPath context
2086 *
2087 * Returns 0 on success and a negative value otherwise.
2088 */
2089static int
2090xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
2091 xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2092{
2093 xmlChar* nsListDup;
2094 xmlChar* prefix;
2095 xmlChar* href;
2096 xmlChar* next;
2097
2098 nsListDup = xmlStrdup((xmlChar *) arg);
2099 next = nsListDup;
2100 while(next != NULL) {
2101 /* skip spaces */
2102 /*while((*next) == ' ') next++;*/
2103 if((*next) == '\0') break;
2104
2105 /* find prefix */
2106 prefix = next;
2107 next = (xmlChar*)xmlStrchr(next, '=');
2108 if(next == NULL) {
2109 fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
2110 xmlFree(nsListDup);
2111 return(-1);
2112 }
2113 *(next++) = '\0';
2114
2115 /* find href */
2116 href = next;
2117 next = (xmlChar*)xmlStrchr(next, ' ');
2118 if(next != NULL) {
2119 *(next++) = '\0';
2120 }
2121
2122 /* do register namespace */
2123 if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
2124 fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
2125 xmlFree(nsListDup);
2126 return(-1);
2127 }
2128 }
2129
2130 xmlFree(nsListDup);
2131 return(0);
2132}
Daniel Veillard20887ee2005-07-04 09:27:40 +00002133/**
2134 * xmlShellRegisterRootNamespaces:
2135 * @ctxt: the shell context
2136 * @arg: unused
2137 * @node: the root element
2138 * @node2: unused
2139 *
2140 * Implements the XML shell function "setrootns"
2141 * which registers all namespaces declarations found on the root element.
2142 *
2143 * Returns 0 on success and a negative value otherwise.
2144 */
2145static int
2146xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2147 xmlNodePtr root, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2148{
2149 xmlNsPtr ns;
2150
2151 if ((root == NULL) || (root->type != XML_ELEMENT_NODE) ||
2152 (root->nsDef == NULL) || (ctxt == NULL) || (ctxt->pctxt == NULL))
2153 return(-1);
2154 ns = root->nsDef;
2155 while (ns != NULL) {
2156 if (ns->prefix == NULL)
2157 xmlXPathRegisterNs(ctxt->pctxt, BAD_CAST "defaultns", ns->href);
2158 else
2159 xmlXPathRegisterNs(ctxt->pctxt, ns->prefix, ns->href);
2160 ns = ns->next;
2161 }
2162 return(0);
2163}
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002164#endif
2165
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002166/**
Daniel Veillard1e208222002-10-22 14:25:25 +00002167 * xmlShellGrep:
2168 * @ctxt: the shell context
2169 * @arg: the string or regular expression to find
2170 * @node: a node
2171 * @node2: unused
2172 *
2173 * Implements the XML shell function "grep"
2174 * dumps informations about the node (namespace, attributes, content).
2175 *
2176 * Returns 0
2177 */
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002178static int
Daniel Veillard1e208222002-10-22 14:25:25 +00002179xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2180 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2181{
2182 if (!ctxt)
2183 return (0);
2184 if (node == NULL)
2185 return (0);
2186 if (arg == NULL)
2187 return (0);
2188#ifdef LIBXML_REGEXP_ENABLED
2189 if ((xmlStrchr((xmlChar *) arg, '?')) ||
2190 (xmlStrchr((xmlChar *) arg, '*')) ||
2191 (xmlStrchr((xmlChar *) arg, '.')) ||
2192 (xmlStrchr((xmlChar *) arg, '['))) {
2193 }
2194#endif
2195 while (node != NULL) {
2196 if (node->type == XML_COMMENT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002197 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002198
2199 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
2200 xmlShellList(ctxt, NULL, node, NULL);
2201 }
2202 } else if (node->type == XML_TEXT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002203 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002204
2205 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002206 xmlShellList(ctxt, NULL, node->parent, NULL);
Daniel Veillard1e208222002-10-22 14:25:25 +00002207 }
2208 }
2209
2210 /*
2211 * Browse the full subtree, deep first
2212 */
2213
2214 if ((node->type == XML_DOCUMENT_NODE) ||
2215 (node->type == XML_HTML_DOCUMENT_NODE)) {
2216 node = ((xmlDocPtr) node)->children;
2217 } else if ((node->children != NULL)
2218 && (node->type != XML_ENTITY_REF_NODE)) {
2219 /* deep first */
2220 node = node->children;
2221 } else if (node->next != NULL) {
2222 /* then siblings */
2223 node = node->next;
2224 } else {
2225 /* go up to parents->next if needed */
2226 while (node != NULL) {
2227 if (node->parent != NULL) {
2228 node = node->parent;
2229 }
2230 if (node->next != NULL) {
2231 node = node->next;
2232 break;
2233 }
2234 if (node->parent == NULL) {
2235 node = NULL;
2236 break;
2237 }
2238 }
2239 }
2240 }
2241 return (0);
2242}
2243
2244/**
Owen Taylor3473f882001-02-23 17:55:21 +00002245 * xmlShellDir:
2246 * @ctxt: the shell context
2247 * @arg: unused
2248 * @node: a node
2249 * @node2: unused
2250 *
2251 * Implements the XML shell function "dir"
2252 * dumps informations about the node (namespace, attributes, content).
2253 *
2254 * Returns 0
2255 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002256int
2257xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2258 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2259 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2260{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002261 if (!ctxt)
2262 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002263 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002264 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002265 return (0);
2266 }
Owen Taylor3473f882001-02-23 17:55:21 +00002267 if ((node->type == XML_DOCUMENT_NODE) ||
2268 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002269 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
Owen Taylor3473f882001-02-23 17:55:21 +00002270 } else if (node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002271 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002272 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002273 xmlDebugDumpOneNode(ctxt->output, node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002274 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002275 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002276}
2277
Daniel Veillard29b17482004-08-16 00:39:03 +00002278/**
2279 * xmlShellSetContent:
2280 * @ctxt: the shell context
2281 * @value: the content as a string
2282 * @node: a node
2283 * @node2: unused
2284 *
2285 * Implements the XML shell function "dir"
2286 * dumps informations about the node (namespace, attributes, content).
2287 *
2288 * Returns 0
2289 */
2290static int
2291xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2292 char *value, xmlNodePtr node,
2293 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2294{
2295 xmlNodePtr results;
2296 xmlParserErrors ret;
2297
2298 if (!ctxt)
2299 return (0);
2300 if (node == NULL) {
2301 fprintf(ctxt->output, "NULL\n");
2302 return (0);
2303 }
2304 if (value == NULL) {
2305 fprintf(ctxt->output, "NULL\n");
2306 return (0);
2307 }
2308
2309 ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
2310 if (ret == XML_ERR_OK) {
2311 if (node->children != NULL) {
2312 xmlFreeNodeList(node->children);
2313 node->children = NULL;
2314 node->last = NULL;
2315 }
2316 xmlAddChildList(node, results);
2317 } else {
2318 fprintf(ctxt->output, "failed to parse content\n");
2319 }
2320 return (0);
2321}
2322
Daniel Veillard522bc602004-02-21 11:53:09 +00002323#ifdef LIBXML_SCHEMAS_ENABLED
2324/**
2325 * xmlShellRNGValidate:
2326 * @ctxt: the shell context
2327 * @schemas: the path to the Relax-NG schemas
2328 * @node: a node
2329 * @node2: unused
2330 *
2331 * Implements the XML shell function "relaxng"
2332 * validating the instance against a Relax-NG schemas
2333 *
2334 * Returns 0
2335 */
2336static int
2337xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
2338 xmlNodePtr node ATTRIBUTE_UNUSED,
2339 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2340{
2341 xmlRelaxNGPtr relaxngschemas;
2342 xmlRelaxNGParserCtxtPtr ctxt;
2343 xmlRelaxNGValidCtxtPtr vctxt;
2344 int ret;
2345
2346 ctxt = xmlRelaxNGNewParserCtxt(schemas);
2347 xmlRelaxNGSetParserErrors(ctxt,
2348 (xmlRelaxNGValidityErrorFunc) fprintf,
2349 (xmlRelaxNGValidityWarningFunc) fprintf,
2350 stderr);
2351 relaxngschemas = xmlRelaxNGParse(ctxt);
2352 xmlRelaxNGFreeParserCtxt(ctxt);
2353 if (relaxngschemas == NULL) {
2354 xmlGenericError(xmlGenericErrorContext,
2355 "Relax-NG schema %s failed to compile\n", schemas);
2356 return(-1);
2357 }
2358 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2359 xmlRelaxNGSetValidErrors(vctxt,
2360 (xmlRelaxNGValidityErrorFunc) fprintf,
2361 (xmlRelaxNGValidityWarningFunc) fprintf,
2362 stderr);
2363 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2364 if (ret == 0) {
2365 fprintf(stderr, "%s validates\n", sctxt->filename);
2366 } else if (ret > 0) {
2367 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2368 } else {
2369 fprintf(stderr, "%s validation generated an internal error\n",
2370 sctxt->filename);
2371 }
2372 xmlRelaxNGFreeValidCtxt(vctxt);
2373 if (relaxngschemas != NULL)
2374 xmlRelaxNGFree(relaxngschemas);
2375 return(0);
2376}
2377#endif
2378
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002379#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002380/**
2381 * xmlShellCat:
2382 * @ctxt: the shell context
2383 * @arg: unused
2384 * @node: a node
2385 * @node2: unused
2386 *
2387 * Implements the XML shell function "cat"
2388 * dumps the serialization node content (XML or HTML).
2389 *
2390 * Returns 0
2391 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002392int
2393xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2394 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2395{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002396 if (!ctxt)
2397 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002398 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002399 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002400 return (0);
2401 }
Owen Taylor3473f882001-02-23 17:55:21 +00002402 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2403#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002404 if (node->type == XML_HTML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002405 htmlDocDump(ctxt->output, (htmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002406 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002407 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002408#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002409 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002410 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002411 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002412 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002413#endif /* LIBXML_HTML_ENABLED */
2414 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00002415 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002416 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002417 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002418 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002419 }
Daniel Veillard321be0c2002-10-08 21:26:42 +00002420 fprintf(ctxt->output, "\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002421 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002422}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002423#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002424
2425/**
2426 * xmlShellLoad:
2427 * @ctxt: the shell context
2428 * @filename: the file name
2429 * @node: unused
2430 * @node2: unused
2431 *
2432 * Implements the XML shell function "load"
2433 * loads a new document specified by the filename
2434 *
2435 * Returns 0 or -1 if loading failed
2436 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002437int
2438xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2439 xmlNodePtr node ATTRIBUTE_UNUSED,
2440 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2441{
Owen Taylor3473f882001-02-23 17:55:21 +00002442 xmlDocPtr doc;
2443 int html = 0;
2444
Daniel Veillarda82b1822004-11-08 16:24:57 +00002445 if ((ctxt == NULL) || (filename == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002446 if (ctxt->doc != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002447 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00002448
2449 if (html) {
2450#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002451 doc = htmlParseFile(filename, NULL);
2452#else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002453 fprintf(ctxt->output, "HTML support not compiled in\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002454 doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002455#endif /* LIBXML_HTML_ENABLED */
2456 } else {
Daniel Veillardebe25d42004-03-25 09:35:49 +00002457 doc = xmlReadFile(filename,NULL,0);
Owen Taylor3473f882001-02-23 17:55:21 +00002458 }
2459 if (doc != NULL) {
2460 if (ctxt->loaded == 1) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002461 xmlFreeDoc(ctxt->doc);
2462 }
2463 ctxt->loaded = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002464#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002465 xmlXPathFreeContext(ctxt->pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00002466#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002467 xmlFree(ctxt->filename);
2468 ctxt->doc = doc;
2469 ctxt->node = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002470#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002471 ctxt->pctxt = xmlXPathNewContext(doc);
Owen Taylor3473f882001-02-23 17:55:21 +00002472#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard85095e22003-04-23 13:56:44 +00002473 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002474 } else
Daniel Veillard78d12092001-10-11 09:12:24 +00002475 return (-1);
2476 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002477}
2478
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002479#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002480/**
2481 * xmlShellWrite:
2482 * @ctxt: the shell context
2483 * @filename: the file name
2484 * @node: a node in the tree
2485 * @node2: unused
2486 *
2487 * Implements the XML shell function "write"
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002488 * Write the current node to the filename, it saves the serialization
Owen Taylor3473f882001-02-23 17:55:21 +00002489 * of the subtree under the @node specified
2490 *
2491 * Returns 0 or -1 in case of error
2492 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002493int
Owen Taylor3473f882001-02-23 17:55:21 +00002494xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Daniel Veillard78d12092001-10-11 09:12:24 +00002495 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2496{
Owen Taylor3473f882001-02-23 17:55:21 +00002497 if (node == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002498 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002499 if ((filename == NULL) || (filename[0] == 0)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002500 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002501 }
2502#ifdef W_OK
2503 if (access((char *) filename, W_OK)) {
2504 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002505 "Cannot write to %s\n", filename);
2506 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002507 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002508#endif
2509 switch (node->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002510 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002511 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2512 xmlGenericError(xmlGenericErrorContext,
2513 "Failed to write to %s\n", filename);
2514 return (-1);
2515 }
2516 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002517 case XML_HTML_DOCUMENT_NODE:
2518#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002519 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2520 xmlGenericError(xmlGenericErrorContext,
2521 "Failed to write to %s\n", filename);
2522 return (-1);
2523 }
Owen Taylor3473f882001-02-23 17:55:21 +00002524#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002525 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2526 xmlGenericError(xmlGenericErrorContext,
2527 "Failed to write to %s\n", filename);
2528 return (-1);
2529 }
Owen Taylor3473f882001-02-23 17:55:21 +00002530#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002531 break;
2532 default:{
2533 FILE *f;
Owen Taylor3473f882001-02-23 17:55:21 +00002534
Daniel Veillard78d12092001-10-11 09:12:24 +00002535 f = fopen((char *) filename, "w");
2536 if (f == NULL) {
2537 xmlGenericError(xmlGenericErrorContext,
2538 "Failed to write to %s\n", filename);
2539 return (-1);
2540 }
2541 xmlElemDump(f, ctxt->doc, node);
2542 fclose(f);
2543 }
Owen Taylor3473f882001-02-23 17:55:21 +00002544 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002545 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002546}
2547
2548/**
2549 * xmlShellSave:
2550 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002551 * @filename: the file name (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002552 * @node: unused
2553 * @node2: unused
2554 *
2555 * Implements the XML shell function "save"
2556 * Write the current document to the filename, or it's original name
2557 *
2558 * Returns 0 or -1 in case of error
2559 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002560int
2561xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2562 xmlNodePtr node ATTRIBUTE_UNUSED,
2563 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2564{
Daniel Veillarda82b1822004-11-08 16:24:57 +00002565 if ((ctxt == NULL) || (ctxt->doc == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002566 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002567 if ((filename == NULL) || (filename[0] == 0))
2568 filename = ctxt->filename;
Daniel Veillarda82b1822004-11-08 16:24:57 +00002569 if (filename == NULL)
2570 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002571#ifdef W_OK
2572 if (access((char *) filename, W_OK)) {
2573 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002574 "Cannot save to %s\n", filename);
2575 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002576 }
2577#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002578 switch (ctxt->doc->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002579 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002580 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2581 xmlGenericError(xmlGenericErrorContext,
2582 "Failed to save to %s\n", filename);
2583 }
2584 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002585 case XML_HTML_DOCUMENT_NODE:
2586#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002587 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2588 xmlGenericError(xmlGenericErrorContext,
2589 "Failed to save to %s\n", filename);
2590 }
Owen Taylor3473f882001-02-23 17:55:21 +00002591#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002592 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2593 xmlGenericError(xmlGenericErrorContext,
2594 "Failed to save to %s\n", filename);
2595 }
Owen Taylor3473f882001-02-23 17:55:21 +00002596#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002597 break;
2598 default:
2599 xmlGenericError(xmlGenericErrorContext,
2600 "To save to subparts of a document use the 'write' command\n");
2601 return (-1);
2602
Owen Taylor3473f882001-02-23 17:55:21 +00002603 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002604 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002605}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002606#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002607
Daniel Veillardf54cd532004-02-25 11:52:31 +00002608#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002609/**
2610 * xmlShellValidate:
2611 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002612 * @dtd: the DTD URI (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002613 * @node: unused
2614 * @node2: unused
2615 *
2616 * Implements the XML shell function "validate"
2617 * Validate the document, if a DTD path is provided, then the validation
2618 * is done against the given DTD.
2619 *
2620 * Returns 0 or -1 in case of error
2621 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002622int
2623xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2624 xmlNodePtr node ATTRIBUTE_UNUSED,
2625 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2626{
Owen Taylor3473f882001-02-23 17:55:21 +00002627 xmlValidCtxt vctxt;
2628 int res = -1;
2629
Daniel Veillarda82b1822004-11-08 16:24:57 +00002630 if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002631 vctxt.userData = stderr;
2632 vctxt.error = (xmlValidityErrorFunc) fprintf;
2633 vctxt.warning = (xmlValidityWarningFunc) fprintf;
2634
2635 if ((dtd == NULL) || (dtd[0] == 0)) {
2636 res = xmlValidateDocument(&vctxt, ctxt->doc);
2637 } else {
2638 xmlDtdPtr subset;
2639
Daniel Veillard78d12092001-10-11 09:12:24 +00002640 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2641 if (subset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002642 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2643
Daniel Veillard78d12092001-10-11 09:12:24 +00002644 xmlFreeDtd(subset);
2645 }
Owen Taylor3473f882001-02-23 17:55:21 +00002646 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002647 return (res);
Owen Taylor3473f882001-02-23 17:55:21 +00002648}
Daniel Veillardf54cd532004-02-25 11:52:31 +00002649#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002650
2651/**
2652 * xmlShellDu:
2653 * @ctxt: the shell context
2654 * @arg: unused
2655 * @tree: a node defining a subtree
2656 * @node2: unused
2657 *
2658 * Implements the XML shell function "du"
2659 * show the structure of the subtree under node @tree
2660 * If @tree is null, the command works on the current node.
2661 *
2662 * Returns 0 or -1 in case of error
2663 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002664int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002665xmlShellDu(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002666 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2667 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2668{
Owen Taylor3473f882001-02-23 17:55:21 +00002669 xmlNodePtr node;
Daniel Veillard78d12092001-10-11 09:12:24 +00002670 int indent = 0, i;
Owen Taylor3473f882001-02-23 17:55:21 +00002671
Daniel Veillard321be0c2002-10-08 21:26:42 +00002672 if (!ctxt)
2673 return (-1);
2674
Daniel Veillard78d12092001-10-11 09:12:24 +00002675 if (tree == NULL)
2676 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002677 node = tree;
2678 while (node != NULL) {
2679 if ((node->type == XML_DOCUMENT_NODE) ||
2680 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002681 fprintf(ctxt->output, "/\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002682 } else if (node->type == XML_ELEMENT_NODE) {
2683 for (i = 0; i < indent; i++)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002684 fprintf(ctxt->output, " ");
2685 fprintf(ctxt->output, "%s\n", node->name);
Daniel Veillard78d12092001-10-11 09:12:24 +00002686 } else {
2687 }
Owen Taylor3473f882001-02-23 17:55:21 +00002688
Daniel Veillard78d12092001-10-11 09:12:24 +00002689 /*
2690 * Browse the full subtree, deep first
2691 */
Owen Taylor3473f882001-02-23 17:55:21 +00002692
2693 if ((node->type == XML_DOCUMENT_NODE) ||
2694 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002695 node = ((xmlDocPtr) node)->children;
2696 } else if ((node->children != NULL)
2697 && (node->type != XML_ENTITY_REF_NODE)) {
2698 /* deep first */
2699 node = node->children;
2700 indent++;
2701 } else if ((node != tree) && (node->next != NULL)) {
2702 /* then siblings */
2703 node = node->next;
2704 } else if (node != tree) {
2705 /* go up to parents->next if needed */
2706 while (node != tree) {
2707 if (node->parent != NULL) {
2708 node = node->parent;
2709 indent--;
2710 }
2711 if ((node != tree) && (node->next != NULL)) {
2712 node = node->next;
2713 break;
2714 }
2715 if (node->parent == NULL) {
2716 node = NULL;
2717 break;
2718 }
2719 if (node == tree) {
2720 node = NULL;
2721 break;
2722 }
2723 }
2724 /* exit condition */
2725 if (node == tree)
2726 node = NULL;
2727 } else
2728 node = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002729 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002730 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002731}
2732
2733/**
2734 * xmlShellPwd:
2735 * @ctxt: the shell context
2736 * @buffer: the output buffer
Daniel Veillard9d06d302002-01-22 18:15:52 +00002737 * @node: a node
Owen Taylor3473f882001-02-23 17:55:21 +00002738 * @node2: unused
2739 *
2740 * Implements the XML shell function "pwd"
2741 * Show the full path from the root to the node, if needed building
2742 * thumblers when similar elements exists at a given ancestor level.
2743 * The output is compatible with XPath commands.
2744 *
2745 * Returns 0 or -1 in case of error
2746 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002747int
2748xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2749 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2750{
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002751 xmlChar *path;
Owen Taylor3473f882001-02-23 17:55:21 +00002752
Daniel Veillarda82b1822004-11-08 16:24:57 +00002753 if ((node == NULL) || (buffer == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002754 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002755
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002756 path = xmlGetNodePath(node);
2757 if (path == NULL)
2758 return (-1);
2759
2760 /*
2761 * This test prevents buffer overflow, because this routine
2762 * is only called by xmlShell, in which the second argument is
2763 * 500 chars long.
2764 * It is a dirty hack before a cleaner solution is found.
2765 * Documentation should mention that the second argument must
2766 * be at least 500 chars long, and could be stripped if too long.
2767 */
2768 snprintf(buffer, 499, "%s", path);
2769 buffer[499] = '0';
2770 xmlFree(path);
2771
Daniel Veillard78d12092001-10-11 09:12:24 +00002772 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002773}
2774
2775/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002776 * xmlShell:
Owen Taylor3473f882001-02-23 17:55:21 +00002777 * @doc: the initial document
2778 * @filename: the output buffer
2779 * @input: the line reading function
Daniel Veillard321be0c2002-10-08 21:26:42 +00002780 * @output: the output FILE*, defaults to stdout if NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002781 *
2782 * Implements the XML shell
2783 * This allow to load, validate, view, modify and save a document
2784 * using a environment similar to a UNIX commandline.
2785 */
2786void
2787xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Daniel Veillard78d12092001-10-11 09:12:24 +00002788 FILE * output)
2789{
Owen Taylor3473f882001-02-23 17:55:21 +00002790 char prompt[500] = "/ > ";
2791 char *cmdline = NULL, *cur;
2792 int nbargs;
2793 char command[100];
2794 char arg[400];
2795 int i;
2796 xmlShellCtxtPtr ctxt;
2797 xmlXPathObjectPtr list;
2798
2799 if (doc == NULL)
2800 return;
2801 if (filename == NULL)
2802 return;
2803 if (input == NULL)
2804 return;
2805 if (output == NULL)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002806 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00002807 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Daniel Veillard78d12092001-10-11 09:12:24 +00002808 if (ctxt == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00002809 return;
2810 ctxt->loaded = 0;
2811 ctxt->doc = doc;
2812 ctxt->input = input;
2813 ctxt->output = output;
2814 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Daniel Veillard78d12092001-10-11 09:12:24 +00002815 ctxt->node = (xmlNodePtr) ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002816
2817#ifdef LIBXML_XPATH_ENABLED
2818 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2819 if (ctxt->pctxt == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002820 xmlFree(ctxt);
2821 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002822 }
2823#endif /* LIBXML_XPATH_ENABLED */
2824 while (1) {
2825 if (ctxt->node == (xmlNodePtr) ctxt->doc)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002826 snprintf(prompt, sizeof(prompt), "%s > ", "/");
Daniel Veillard7a985a12003-07-06 17:57:42 +00002827 else if ((ctxt->node != NULL) && (ctxt->node->name))
Daniel Veillard78d12092001-10-11 09:12:24 +00002828 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002829 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002830 snprintf(prompt, sizeof(prompt), "? > ");
Owen Taylor3473f882001-02-23 17:55:21 +00002831 prompt[sizeof(prompt) - 1] = 0;
2832
Daniel Veillard78d12092001-10-11 09:12:24 +00002833 /*
2834 * Get a new command line
2835 */
Owen Taylor3473f882001-02-23 17:55:21 +00002836 cmdline = ctxt->input(prompt);
Daniel Veillard78d12092001-10-11 09:12:24 +00002837 if (cmdline == NULL)
2838 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002839
Daniel Veillard78d12092001-10-11 09:12:24 +00002840 /*
2841 * Parse the command itself
2842 */
2843 cur = cmdline;
2844 nbargs = 0;
2845 while ((*cur == ' ') || (*cur == '\t'))
2846 cur++;
2847 i = 0;
2848 while ((*cur != ' ') && (*cur != '\t') &&
2849 (*cur != '\n') && (*cur != '\r')) {
2850 if (*cur == 0)
2851 break;
2852 command[i++] = *cur++;
2853 }
2854 command[i] = 0;
2855 if (i == 0)
2856 continue;
2857 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002858
Daniel Veillard78d12092001-10-11 09:12:24 +00002859 /*
2860 * Parse the argument
2861 */
2862 while ((*cur == ' ') || (*cur == '\t'))
2863 cur++;
2864 i = 0;
2865 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2866 if (*cur == 0)
2867 break;
2868 arg[i++] = *cur++;
2869 }
2870 arg[i] = 0;
2871 if (i != 0)
2872 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002873
Daniel Veillard78d12092001-10-11 09:12:24 +00002874 /*
2875 * start interpreting the command
2876 */
Owen Taylor3473f882001-02-23 17:55:21 +00002877 if (!strcmp(command, "exit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002878 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002879 if (!strcmp(command, "quit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002880 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002881 if (!strcmp(command, "bye"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002882 break;
Daniel Veillard5004f422001-11-08 13:53:05 +00002883 if (!strcmp(command, "help")) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002884 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2885 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2886 fprintf(ctxt->output, "\tbye leave shell\n");
2887 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2888 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2889 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2890 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2891 fprintf(ctxt->output, "\texit leave shell\n");
2892 fprintf(ctxt->output, "\thelp display this help\n");
2893 fprintf(ctxt->output, "\tfree display memory usage\n");
2894 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2895 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00002896 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 +00002897#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002898 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002899 fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2900 fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
Daniel Veillard20887ee2005-07-04 09:27:40 +00002901 fprintf(ctxt->output, "\tsetrootns register all namespace found on the root element\n");
2902 fprintf(ctxt->output, "\t the default namespace if any uses 'defaultns' prefix\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002903#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard321be0c2002-10-08 21:26:42 +00002904 fprintf(ctxt->output, "\tpwd display current working directory\n");
2905 fprintf(ctxt->output, "\tquit leave shell\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002906#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002907 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00002908 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002909#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf54cd532004-02-25 11:52:31 +00002910#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002911 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002912#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard522bc602004-02-21 11:53:09 +00002913#ifdef LIBXML_SCHEMAS_ENABLED
2914 fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n");
2915#endif
Daniel Veillard1e208222002-10-22 14:25:25 +00002916 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002917#ifdef LIBXML_VALID_ENABLED
Daniel Veillard5004f422001-11-08 13:53:05 +00002918 } else if (!strcmp(command, "validate")) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002919 xmlShellValidate(ctxt, arg, NULL, NULL);
Daniel Veillardf54cd532004-02-25 11:52:31 +00002920#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002921 } else if (!strcmp(command, "load")) {
2922 xmlShellLoad(ctxt, arg, NULL, NULL);
Daniel Veillard522bc602004-02-21 11:53:09 +00002923#ifdef LIBXML_SCHEMAS_ENABLED
2924 } else if (!strcmp(command, "relaxng")) {
2925 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2926#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002927#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002928 } else if (!strcmp(command, "save")) {
2929 xmlShellSave(ctxt, arg, NULL, NULL);
2930 } else if (!strcmp(command, "write")) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00002931 if ((arg == NULL) || (arg[0] == 0))
2932 xmlGenericError(xmlGenericErrorContext,
2933 "Write command requires a filename argument\n");
2934 else
2935 xmlShellWrite(ctxt, arg, NULL, NULL);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002936#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard1e208222002-10-22 14:25:25 +00002937 } else if (!strcmp(command, "grep")) {
2938 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
Daniel Veillard78d12092001-10-11 09:12:24 +00002939 } else if (!strcmp(command, "free")) {
2940 if (arg[0] == 0) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002941 xmlMemShow(ctxt->output, 0);
Daniel Veillard78d12092001-10-11 09:12:24 +00002942 } else {
2943 int len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002944
Daniel Veillard78d12092001-10-11 09:12:24 +00002945 sscanf(arg, "%d", &len);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002946 xmlMemShow(ctxt->output, len);
Daniel Veillard78d12092001-10-11 09:12:24 +00002947 }
2948 } else if (!strcmp(command, "pwd")) {
2949 char dir[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002950
Daniel Veillard78d12092001-10-11 09:12:24 +00002951 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
Daniel Veillard321be0c2002-10-08 21:26:42 +00002952 fprintf(ctxt->output, "%s\n", dir);
Daniel Veillard78d12092001-10-11 09:12:24 +00002953 } else if (!strcmp(command, "du")) {
2954 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2955 } else if (!strcmp(command, "base")) {
2956 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
Daniel Veillard29b17482004-08-16 00:39:03 +00002957 } else if (!strcmp(command, "set")) {
2958 xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002959#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002960 } else if (!strcmp(command, "setns")) {
2961 if (arg[0] == 0) {
2962 xmlGenericError(xmlGenericErrorContext,
2963 "setns: prefix=[nsuri] required\n");
2964 } else {
2965 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
2966 }
Daniel Veillard20887ee2005-07-04 09:27:40 +00002967 } else if (!strcmp(command, "setrootns")) {
2968 xmlNodePtr root;
2969
2970 root = xmlDocGetRootElement(ctxt->doc);
2971 xmlShellRegisterRootNamespaces(ctxt, NULL, root, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002972 } else if (!strcmp(command, "xpath")) {
2973 if (arg[0] == 0) {
2974 xmlGenericError(xmlGenericErrorContext,
2975 "xpath: expression required\n");
2976 } else {
2977 ctxt->pctxt->node = ctxt->node;
2978 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002979 xmlXPathDebugDumpObject(ctxt->output, list, 0);
Daniel Veillard2070c482002-01-22 22:12:19 +00002980 xmlXPathFreeObject(list);
2981 }
2982#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard2156d432004-03-04 15:59:36 +00002983#ifdef LIBXML_TREE_ENABLED
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002984 } else if (!strcmp(command, "setbase")) {
2985 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2156d432004-03-04 15:59:36 +00002986#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002987 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
2988 int dir = (!strcmp(command, "dir"));
2989
2990 if (arg[0] == 0) {
2991 if (dir)
2992 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
2993 else
2994 xmlShellList(ctxt, NULL, ctxt->node, NULL);
2995 } else {
2996 ctxt->pctxt->node = ctxt->node;
Daniel Veillard61d80a22001-04-27 17:13:01 +00002997#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002998 ctxt->pctxt->node = ctxt->node;
2999 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3000#else
3001 list = NULL;
3002#endif /* LIBXML_XPATH_ENABLED */
3003 if (list != NULL) {
3004 switch (list->type) {
3005 case XPATH_UNDEFINED:
3006 xmlGenericError(xmlGenericErrorContext,
3007 "%s: no such node\n", arg);
3008 break;
3009 case XPATH_NODESET:{
3010 int indx;
3011
Daniel Veillarda6825e82001-11-07 13:33:59 +00003012 if (list->nodesetval == NULL)
3013 break;
3014
Daniel Veillard78d12092001-10-11 09:12:24 +00003015 for (indx = 0;
3016 indx < list->nodesetval->nodeNr;
3017 indx++) {
3018 if (dir)
3019 xmlShellDir(ctxt, NULL,
3020 list->nodesetval->
3021 nodeTab[indx], NULL);
3022 else
3023 xmlShellList(ctxt, NULL,
3024 list->nodesetval->
3025 nodeTab[indx], NULL);
3026 }
3027 break;
3028 }
3029 case XPATH_BOOLEAN:
3030 xmlGenericError(xmlGenericErrorContext,
3031 "%s is a Boolean\n", arg);
3032 break;
3033 case XPATH_NUMBER:
3034 xmlGenericError(xmlGenericErrorContext,
3035 "%s is a number\n", arg);
3036 break;
3037 case XPATH_STRING:
3038 xmlGenericError(xmlGenericErrorContext,
3039 "%s is a string\n", arg);
3040 break;
3041 case XPATH_POINT:
3042 xmlGenericError(xmlGenericErrorContext,
3043 "%s is a point\n", arg);
3044 break;
3045 case XPATH_RANGE:
3046 xmlGenericError(xmlGenericErrorContext,
3047 "%s is a range\n", arg);
3048 break;
3049 case XPATH_LOCATIONSET:
3050 xmlGenericError(xmlGenericErrorContext,
3051 "%s is a range\n", arg);
3052 break;
3053 case XPATH_USERS:
3054 xmlGenericError(xmlGenericErrorContext,
3055 "%s is user-defined\n", arg);
3056 break;
3057 case XPATH_XSLT_TREE:
3058 xmlGenericError(xmlGenericErrorContext,
3059 "%s is an XSLT value tree\n",
3060 arg);
3061 break;
3062 }
3063#ifdef LIBXML_XPATH_ENABLED
3064 xmlXPathFreeObject(list);
Daniel Veillard61d80a22001-04-27 17:13:01 +00003065#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00003066 } else {
3067 xmlGenericError(xmlGenericErrorContext,
3068 "%s: no such node\n", arg);
3069 }
3070 ctxt->pctxt->node = NULL;
3071 }
3072 } else if (!strcmp(command, "cd")) {
3073 if (arg[0] == 0) {
3074 ctxt->node = (xmlNodePtr) ctxt->doc;
3075 } else {
3076#ifdef LIBXML_XPATH_ENABLED
3077 ctxt->pctxt->node = ctxt->node;
3078 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3079#else
3080 list = NULL;
3081#endif /* LIBXML_XPATH_ENABLED */
3082 if (list != NULL) {
3083 switch (list->type) {
3084 case XPATH_UNDEFINED:
3085 xmlGenericError(xmlGenericErrorContext,
3086 "%s: no such node\n", arg);
3087 break;
3088 case XPATH_NODESET:
Daniel Veillarda6825e82001-11-07 13:33:59 +00003089 if (list->nodesetval != NULL) {
3090 if (list->nodesetval->nodeNr == 1) {
3091 ctxt->node = list->nodesetval->nodeTab[0];
Daniel Veillard7a985a12003-07-06 17:57:42 +00003092 if ((ctxt->node != NULL) &&
3093 (ctxt->node->type ==
3094 XML_NAMESPACE_DECL)) {
3095 xmlGenericError(xmlGenericErrorContext,
3096 "cannot cd to namespace\n");
3097 ctxt->node = NULL;
3098 }
Daniel Veillarda6825e82001-11-07 13:33:59 +00003099 } else
3100 xmlGenericError(xmlGenericErrorContext,
3101 "%s is a %d Node Set\n",
3102 arg,
3103 list->nodesetval->nodeNr);
Daniel Veillard78d12092001-10-11 09:12:24 +00003104 } else
3105 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda6825e82001-11-07 13:33:59 +00003106 "%s is an empty Node Set\n",
3107 arg);
Daniel Veillard78d12092001-10-11 09:12:24 +00003108 break;
3109 case XPATH_BOOLEAN:
3110 xmlGenericError(xmlGenericErrorContext,
3111 "%s is a Boolean\n", arg);
3112 break;
3113 case XPATH_NUMBER:
3114 xmlGenericError(xmlGenericErrorContext,
3115 "%s is a number\n", arg);
3116 break;
3117 case XPATH_STRING:
3118 xmlGenericError(xmlGenericErrorContext,
3119 "%s is a string\n", arg);
3120 break;
3121 case XPATH_POINT:
3122 xmlGenericError(xmlGenericErrorContext,
3123 "%s is a point\n", arg);
3124 break;
3125 case XPATH_RANGE:
3126 xmlGenericError(xmlGenericErrorContext,
3127 "%s is a range\n", arg);
3128 break;
3129 case XPATH_LOCATIONSET:
3130 xmlGenericError(xmlGenericErrorContext,
3131 "%s is a range\n", arg);
3132 break;
3133 case XPATH_USERS:
3134 xmlGenericError(xmlGenericErrorContext,
3135 "%s is user-defined\n", arg);
3136 break;
3137 case XPATH_XSLT_TREE:
3138 xmlGenericError(xmlGenericErrorContext,
3139 "%s is an XSLT value tree\n",
3140 arg);
3141 break;
3142 }
3143#ifdef LIBXML_XPATH_ENABLED
3144 xmlXPathFreeObject(list);
3145#endif
3146 } else {
3147 xmlGenericError(xmlGenericErrorContext,
3148 "%s: no such node\n", arg);
3149 }
3150 ctxt->pctxt->node = NULL;
3151 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003152#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00003153 } else if (!strcmp(command, "cat")) {
3154 if (arg[0] == 0) {
3155 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3156 } else {
3157 ctxt->pctxt->node = ctxt->node;
3158#ifdef LIBXML_XPATH_ENABLED
3159 ctxt->pctxt->node = ctxt->node;
3160 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3161#else
3162 list = NULL;
3163#endif /* LIBXML_XPATH_ENABLED */
3164 if (list != NULL) {
3165 switch (list->type) {
3166 case XPATH_UNDEFINED:
3167 xmlGenericError(xmlGenericErrorContext,
3168 "%s: no such node\n", arg);
3169 break;
3170 case XPATH_NODESET:{
3171 int indx;
3172
Daniel Veillarda6825e82001-11-07 13:33:59 +00003173 if (list->nodesetval == NULL)
3174 break;
3175
Daniel Veillard78d12092001-10-11 09:12:24 +00003176 for (indx = 0;
3177 indx < list->nodesetval->nodeNr;
3178 indx++) {
3179 if (i > 0)
Daniel Veillard321be0c2002-10-08 21:26:42 +00003180 fprintf(ctxt->output, " -------\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00003181 xmlShellCat(ctxt, NULL,
3182 list->nodesetval->
3183 nodeTab[indx], NULL);
3184 }
3185 break;
3186 }
3187 case XPATH_BOOLEAN:
3188 xmlGenericError(xmlGenericErrorContext,
3189 "%s is a Boolean\n", arg);
3190 break;
3191 case XPATH_NUMBER:
3192 xmlGenericError(xmlGenericErrorContext,
3193 "%s is a number\n", arg);
3194 break;
3195 case XPATH_STRING:
3196 xmlGenericError(xmlGenericErrorContext,
3197 "%s is a string\n", arg);
3198 break;
3199 case XPATH_POINT:
3200 xmlGenericError(xmlGenericErrorContext,
3201 "%s is a point\n", arg);
3202 break;
3203 case XPATH_RANGE:
3204 xmlGenericError(xmlGenericErrorContext,
3205 "%s is a range\n", arg);
3206 break;
3207 case XPATH_LOCATIONSET:
3208 xmlGenericError(xmlGenericErrorContext,
3209 "%s is a range\n", arg);
3210 break;
3211 case XPATH_USERS:
3212 xmlGenericError(xmlGenericErrorContext,
3213 "%s is user-defined\n", arg);
3214 break;
3215 case XPATH_XSLT_TREE:
3216 xmlGenericError(xmlGenericErrorContext,
3217 "%s is an XSLT value tree\n",
3218 arg);
3219 break;
3220 }
3221#ifdef LIBXML_XPATH_ENABLED
3222 xmlXPathFreeObject(list);
3223#endif
3224 } else {
3225 xmlGenericError(xmlGenericErrorContext,
3226 "%s: no such node\n", arg);
3227 }
3228 ctxt->pctxt->node = NULL;
3229 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003230#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00003231 } else {
3232 xmlGenericError(xmlGenericErrorContext,
3233 "Unknown command %s\n", command);
3234 }
3235 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003236 }
3237#ifdef LIBXML_XPATH_ENABLED
3238 xmlXPathFreeContext(ctxt->pctxt);
3239#endif /* LIBXML_XPATH_ENABLED */
3240 if (ctxt->loaded) {
3241 xmlFreeDoc(ctxt->doc);
3242 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003243 if (ctxt->filename != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003244 xmlFree(ctxt->filename);
Owen Taylor3473f882001-02-23 17:55:21 +00003245 xmlFree(ctxt);
3246 if (cmdline != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003247 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003248}
3249
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00003250#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00003251#define bottom_debugXML
3252#include "elfgcchack.h"
Owen Taylor3473f882001-02-23 17:55:21 +00003253#endif /* LIBXML_DEBUG_ENABLED */