blob: 3cb2848be8327c16c2e52fcceda83412002776a4 [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)
905 fprintf(ctxt->output, "TEXT no enc\n");
906 else
907 fprintf(ctxt->output, "TEXT\n");
908 }
909 break;
910 case XML_CDATA_SECTION_NODE:
911 if (!ctxt->check) {
912 xmlCtxtDumpSpaces(ctxt);
913 fprintf(ctxt->output, "CDATA_SECTION\n");
914 }
915 break;
916 case XML_ENTITY_REF_NODE:
917 if (!ctxt->check) {
918 xmlCtxtDumpSpaces(ctxt);
919 fprintf(ctxt->output, "ENTITY_REF(%s)\n",
920 (char *) node->name);
921 }
922 break;
923 case XML_ENTITY_NODE:
924 if (!ctxt->check) {
925 xmlCtxtDumpSpaces(ctxt);
926 fprintf(ctxt->output, "ENTITY\n");
927 }
928 break;
929 case XML_PI_NODE:
930 if (!ctxt->check) {
931 xmlCtxtDumpSpaces(ctxt);
932 fprintf(ctxt->output, "PI %s\n", (char *) node->name);
933 }
934 break;
935 case XML_COMMENT_NODE:
936 if (!ctxt->check) {
937 xmlCtxtDumpSpaces(ctxt);
938 fprintf(ctxt->output, "COMMENT\n");
939 }
940 break;
941 case XML_DOCUMENT_NODE:
942 case XML_HTML_DOCUMENT_NODE:
943 if (!ctxt->check) {
944 xmlCtxtDumpSpaces(ctxt);
945 }
William M. Brack5a9c1fd2004-12-17 21:38:09 +0000946 fprintf(ctxt->output, "Error, DOCUMENT found here\n");
947 xmlCtxtGenericNodeCheck(ctxt, node);
948 return;
Daniel Veillard22cdb842004-10-04 14:09:17 +0000949 case XML_DOCUMENT_TYPE_NODE:
950 if (!ctxt->check) {
951 xmlCtxtDumpSpaces(ctxt);
952 fprintf(ctxt->output, "DOCUMENT_TYPE\n");
953 }
954 break;
955 case XML_DOCUMENT_FRAG_NODE:
956 if (!ctxt->check) {
957 xmlCtxtDumpSpaces(ctxt);
958 fprintf(ctxt->output, "DOCUMENT_FRAG\n");
959 }
960 break;
961 case XML_NOTATION_NODE:
962 if (!ctxt->check) {
963 xmlCtxtDumpSpaces(ctxt);
964 fprintf(ctxt->output, "NOTATION\n");
965 }
966 break;
967 case XML_DTD_NODE:
968 xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
969 return;
970 case XML_ELEMENT_DECL:
971 xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
972 return;
973 case XML_ATTRIBUTE_DECL:
974 xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
975 return;
976 case XML_ENTITY_DECL:
977 xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
978 return;
979 case XML_NAMESPACE_DECL:
980 xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
981 return;
982 case XML_XINCLUDE_START:
983 if (!ctxt->check) {
984 xmlCtxtDumpSpaces(ctxt);
985 fprintf(ctxt->output, "INCLUDE START\n");
986 }
987 return;
988 case XML_XINCLUDE_END:
989 if (!ctxt->check) {
990 xmlCtxtDumpSpaces(ctxt);
991 fprintf(ctxt->output, "INCLUDE END\n");
992 }
993 return;
994 default:
995 if (!ctxt->check)
996 xmlCtxtDumpSpaces(ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000997 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
998 "Unknown node type %d\n", node->type);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000999 return;
1000 }
1001 if (node->doc == NULL) {
1002 if (!ctxt->check) {
1003 xmlCtxtDumpSpaces(ctxt);
1004 }
1005 fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
1006 }
1007 ctxt->depth++;
1008 if (node->nsDef != NULL)
1009 xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
1010 if (node->properties != NULL)
1011 xmlCtxtDumpAttrList(ctxt, node->properties);
1012 if (node->type != XML_ENTITY_REF_NODE) {
1013 if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
1014 if (!ctxt->check) {
1015 xmlCtxtDumpSpaces(ctxt);
1016 fprintf(ctxt->output, "content=");
1017 xmlCtxtDumpString(ctxt, node->content);
1018 fprintf(ctxt->output, "\n");
1019 }
1020 }
1021 } else {
1022 xmlEntityPtr ent;
1023
1024 ent = xmlGetDocEntity(node->doc, node->name);
1025 if (ent != NULL)
1026 xmlCtxtDumpEntity(ctxt, ent);
1027 }
1028 ctxt->depth--;
1029
1030 /*
1031 * Do a bit of checking
1032 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001033 xmlCtxtGenericNodeCheck(ctxt, node);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001034}
1035
1036/**
1037 * xmlCtxtDumpNode:
1038 * @output: the FILE * for the output
1039 * @node: the node
1040 * @depth: the indentation level.
1041 *
1042 * Dumps debug information for the element node, it is recursive
1043 */
1044static void
1045xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1046{
1047 if (node == NULL) {
1048 if (!ctxt->check) {
1049 xmlCtxtDumpSpaces(ctxt);
1050 fprintf(ctxt->output, "node is NULL\n");
1051 }
1052 return;
1053 }
1054 xmlCtxtDumpOneNode(ctxt, node);
1055 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
1056 ctxt->depth++;
1057 xmlCtxtDumpNodeList(ctxt, node->children);
1058 ctxt->depth--;
1059 }
1060}
1061
1062/**
1063 * xmlCtxtDumpNodeList:
1064 * @output: the FILE * for the output
1065 * @node: the node list
1066 * @depth: the indentation level.
1067 *
1068 * Dumps debug information for the list of element node, it is recursive
1069 */
1070static void
1071xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1072{
1073 while (node != NULL) {
1074 xmlCtxtDumpNode(ctxt, node);
1075 node = node->next;
1076 }
1077}
1078
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001079static void
1080xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1081{
1082 if (doc == NULL) {
1083 if (!ctxt->check)
1084 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1085 return;
1086 }
1087 ctxt->node = (xmlNodePtr) doc;
1088
1089 switch (doc->type) {
1090 case XML_ELEMENT_NODE:
1091 xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
1092 "Misplaced ELEMENT node\n");
1093 break;
1094 case XML_ATTRIBUTE_NODE:
1095 xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
1096 "Misplaced ATTRIBUTE node\n");
1097 break;
1098 case XML_TEXT_NODE:
1099 xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
1100 "Misplaced TEXT node\n");
1101 break;
1102 case XML_CDATA_SECTION_NODE:
1103 xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
1104 "Misplaced CDATA node\n");
1105 break;
1106 case XML_ENTITY_REF_NODE:
1107 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
1108 "Misplaced ENTITYREF node\n");
1109 break;
1110 case XML_ENTITY_NODE:
1111 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
1112 "Misplaced ENTITY node\n");
1113 break;
1114 case XML_PI_NODE:
1115 xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
1116 "Misplaced PI node\n");
1117 break;
1118 case XML_COMMENT_NODE:
1119 xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
1120 "Misplaced COMMENT node\n");
1121 break;
1122 case XML_DOCUMENT_NODE:
1123 if (!ctxt->check)
1124 fprintf(ctxt->output, "DOCUMENT\n");
1125 break;
1126 case XML_HTML_DOCUMENT_NODE:
1127 if (!ctxt->check)
1128 fprintf(ctxt->output, "HTML DOCUMENT\n");
1129 break;
1130 case XML_DOCUMENT_TYPE_NODE:
1131 xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
1132 "Misplaced DOCTYPE node\n");
1133 break;
1134 case XML_DOCUMENT_FRAG_NODE:
1135 xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
1136 "Misplaced FRAGMENT node\n");
1137 break;
1138 case XML_NOTATION_NODE:
1139 xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
1140 "Misplaced NOTATION node\n");
1141 break;
1142 default:
1143 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1144 "Unknown node type %d\n", doc->type);
1145 }
1146}
Daniel Veillard22cdb842004-10-04 14:09:17 +00001147
1148/**
1149 * xmlCtxtDumpDocumentHead:
1150 * @output: the FILE * for the output
1151 * @doc: the document
1152 *
1153 * Dumps debug information cncerning the document, not recursive
1154 */
1155static void
1156xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1157{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001158 if (doc == NULL) return;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001159 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001160 if (!ctxt->check) {
1161 if (doc->name != NULL) {
1162 fprintf(ctxt->output, "name=");
1163 xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
1164 fprintf(ctxt->output, "\n");
1165 }
1166 if (doc->version != NULL) {
1167 fprintf(ctxt->output, "version=");
1168 xmlCtxtDumpString(ctxt, doc->version);
1169 fprintf(ctxt->output, "\n");
1170 }
1171 if (doc->encoding != NULL) {
1172 fprintf(ctxt->output, "encoding=");
1173 xmlCtxtDumpString(ctxt, doc->encoding);
1174 fprintf(ctxt->output, "\n");
1175 }
1176 if (doc->URL != NULL) {
1177 fprintf(ctxt->output, "URL=");
1178 xmlCtxtDumpString(ctxt, doc->URL);
1179 fprintf(ctxt->output, "\n");
1180 }
1181 if (doc->standalone)
1182 fprintf(ctxt->output, "standalone=true\n");
1183 }
1184 if (doc->oldNs != NULL)
1185 xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
1186}
1187
1188/**
1189 * xmlCtxtDumpDocument:
1190 * @output: the FILE * for the output
1191 * @doc: the document
1192 *
1193 * Dumps debug information for the document, it's recursive
1194 */
1195static void
1196xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1197{
1198 if (doc == NULL) {
1199 if (!ctxt->check)
1200 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1201 return;
1202 }
1203 xmlCtxtDumpDocumentHead(ctxt, doc);
1204 if (((doc->type == XML_DOCUMENT_NODE) ||
1205 (doc->type == XML_HTML_DOCUMENT_NODE))
1206 && (doc->children != NULL)) {
1207 ctxt->depth++;
1208 xmlCtxtDumpNodeList(ctxt, doc->children);
1209 ctxt->depth--;
1210 }
1211}
1212
1213static void
1214xmlCtxtDumpEntityCallback(xmlEntityPtr cur, xmlDebugCtxtPtr ctxt)
1215{
1216 if (cur == NULL) {
1217 if (!ctxt->check)
1218 fprintf(ctxt->output, "Entity is NULL");
1219 return;
1220 }
1221 if (!ctxt->check) {
1222 fprintf(ctxt->output, "%s : ", (char *) cur->name);
1223 switch (cur->etype) {
1224 case XML_INTERNAL_GENERAL_ENTITY:
1225 fprintf(ctxt->output, "INTERNAL GENERAL, ");
1226 break;
1227 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1228 fprintf(ctxt->output, "EXTERNAL PARSED, ");
1229 break;
1230 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1231 fprintf(ctxt->output, "EXTERNAL UNPARSED, ");
1232 break;
1233 case XML_INTERNAL_PARAMETER_ENTITY:
1234 fprintf(ctxt->output, "INTERNAL PARAMETER, ");
1235 break;
1236 case XML_EXTERNAL_PARAMETER_ENTITY:
1237 fprintf(ctxt->output, "EXTERNAL PARAMETER, ");
1238 break;
1239 default:
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001240 xmlDebugErr2(ctxt, XML_CHECK_ENTITY_TYPE,
1241 "Unknown entity type %d\n", cur->etype);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001242 }
1243 if (cur->ExternalID != NULL)
1244 fprintf(ctxt->output, "ID \"%s\"", (char *) cur->ExternalID);
1245 if (cur->SystemID != NULL)
1246 fprintf(ctxt->output, "SYSTEM \"%s\"", (char *) cur->SystemID);
1247 if (cur->orig != NULL)
1248 fprintf(ctxt->output, "\n orig \"%s\"", (char *) cur->orig);
1249 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL))
1250 fprintf(ctxt->output, "\n content \"%s\"",
1251 (char *) cur->content);
1252 fprintf(ctxt->output, "\n");
1253 }
1254}
1255
1256/**
1257 * xmlCtxtDumpEntities:
1258 * @output: the FILE * for the output
1259 * @doc: the document
1260 *
1261 * Dumps debug information for all the entities in use by the document
1262 */
1263static void
1264xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1265{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001266 if (doc == NULL) return;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001267 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001268 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
1269 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1270 doc->intSubset->entities;
1271
1272 if (!ctxt->check)
1273 fprintf(ctxt->output, "Entities in internal subset\n");
1274 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1275 ctxt);
1276 } else
1277 fprintf(ctxt->output, "No entities in internal subset\n");
1278 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
1279 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1280 doc->extSubset->entities;
1281
1282 if (!ctxt->check)
1283 fprintf(ctxt->output, "Entities in external subset\n");
1284 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1285 ctxt);
1286 } else if (!ctxt->check)
1287 fprintf(ctxt->output, "No entities in external subset\n");
1288}
1289
1290/**
1291 * xmlCtxtDumpDTD:
1292 * @output: the FILE * for the output
1293 * @dtd: the DTD
1294 *
1295 * Dumps debug information for the DTD
1296 */
1297static void
1298xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
1299{
1300 if (dtd == NULL) {
1301 if (!ctxt->check)
1302 fprintf(ctxt->output, "DTD is NULL\n");
1303 return;
1304 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001305 xmlCtxtDumpDtdNode(ctxt, dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001306 if (dtd->children == NULL)
1307 fprintf(ctxt->output, " DTD is empty\n");
1308 else {
1309 ctxt->depth++;
1310 xmlCtxtDumpNodeList(ctxt, dtd->children);
1311 ctxt->depth--;
1312 }
1313}
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001314
Daniel Veillard22cdb842004-10-04 14:09:17 +00001315/************************************************************************
1316 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001317 * Public entry points for dump *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001318 * *
1319 ************************************************************************/
1320
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001321/**
1322 * xmlDebugDumpString:
1323 * @output: the FILE * for the output
1324 * @str: the string
1325 *
1326 * Dumps informations about the string, shorten it if necessary
1327 */
1328void
1329xmlDebugDumpString(FILE * output, const xmlChar * str)
1330{
Owen Taylor3473f882001-02-23 17:55:21 +00001331 int i;
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001332
Daniel Veillard7db38712002-02-07 16:39:11 +00001333 if (output == NULL)
1334 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00001335 if (str == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001336 fprintf(output, "(NULL)");
1337 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001338 }
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001339 for (i = 0; i < 40; i++)
1340 if (str[i] == 0)
1341 return;
William M. Brack76e95df2003-10-18 16:20:14 +00001342 else if (IS_BLANK_CH(str[i]))
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001343 fputc(' ', output);
1344 else if (str[i] >= 0x80)
1345 fprintf(output, "#%X", str[i]);
1346 else
1347 fputc(str[i], output);
Owen Taylor3473f882001-02-23 17:55:21 +00001348 fprintf(output, "...");
1349}
1350
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001351/**
1352 * xmlDebugDumpAttr:
1353 * @output: the FILE * for the output
1354 * @attr: the attribute
1355 * @depth: the indentation level.
1356 *
1357 * Dumps debug information for the attribute
1358 */
1359void
1360xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
Daniel Veillard22cdb842004-10-04 14:09:17 +00001361 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001362
Daniel Veillarda82b1822004-11-08 16:24:57 +00001363 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001364 xmlCtxtDumpInitCtxt(&ctxt);
1365 ctxt.output = output;
1366 ctxt.depth = depth;
1367 xmlCtxtDumpAttr(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001368 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001369}
Owen Taylor3473f882001-02-23 17:55:21 +00001370
Owen Taylor3473f882001-02-23 17:55:21 +00001371
Daniel Veillard22cdb842004-10-04 14:09:17 +00001372/**
1373 * xmlDebugDumpEntities:
1374 * @output: the FILE * for the output
1375 * @doc: the document
1376 *
1377 * Dumps debug information for all the entities in use by the document
1378 */
1379void
1380xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
1381{
1382 xmlDebugCtxt ctxt;
1383
Daniel Veillarda82b1822004-11-08 16:24:57 +00001384 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001385 xmlCtxtDumpInitCtxt(&ctxt);
1386 ctxt.output = output;
1387 xmlCtxtDumpEntities(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001388 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001389}
1390
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001391/**
1392 * xmlDebugDumpAttrList:
1393 * @output: the FILE * for the output
1394 * @attr: the attribute list
1395 * @depth: the indentation level.
1396 *
1397 * Dumps debug information for the attribute list
1398 */
1399void
1400xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
1401{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001402 xmlDebugCtxt ctxt;
1403
Daniel Veillarda82b1822004-11-08 16:24:57 +00001404 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001405 xmlCtxtDumpInitCtxt(&ctxt);
1406 ctxt.output = output;
1407 ctxt.depth = depth;
1408 xmlCtxtDumpAttrList(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001409 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001410}
1411
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001412/**
1413 * xmlDebugDumpOneNode:
1414 * @output: the FILE * for the output
1415 * @node: the node
1416 * @depth: the indentation level.
1417 *
1418 * Dumps debug information for the element node, it is not recursive
1419 */
1420void
1421xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
1422{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001423 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001424
Daniel Veillarda82b1822004-11-08 16:24:57 +00001425 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001426 xmlCtxtDumpInitCtxt(&ctxt);
1427 ctxt.output = output;
1428 ctxt.depth = depth;
1429 xmlCtxtDumpOneNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001430 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001431}
1432
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001433/**
1434 * xmlDebugDumpNode:
1435 * @output: the FILE * for the output
1436 * @node: the node
1437 * @depth: the indentation level.
1438 *
1439 * Dumps debug information for the element node, it is recursive
1440 */
1441void
1442xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
1443{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001444 xmlDebugCtxt ctxt;
1445
Daniel Veillard7db38712002-02-07 16:39:11 +00001446 if (output == NULL)
1447 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001448 xmlCtxtDumpInitCtxt(&ctxt);
1449 ctxt.output = output;
1450 ctxt.depth = depth;
1451 xmlCtxtDumpNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001452 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001453}
1454
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001455/**
1456 * xmlDebugDumpNodeList:
1457 * @output: the FILE * for the output
1458 * @node: the node list
1459 * @depth: the indentation level.
1460 *
1461 * Dumps debug information for the list of element node, it is recursive
1462 */
1463void
1464xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
1465{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001466 xmlDebugCtxt ctxt;
1467
Daniel Veillard7db38712002-02-07 16:39:11 +00001468 if (output == NULL)
1469 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001470 xmlCtxtDumpInitCtxt(&ctxt);
1471 ctxt.output = output;
1472 ctxt.depth = depth;
1473 xmlCtxtDumpNodeList(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001474 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001475}
1476
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001477/**
1478 * xmlDebugDumpDocumentHead:
1479 * @output: the FILE * for the output
1480 * @doc: the document
1481 *
1482 * Dumps debug information cncerning the document, not recursive
1483 */
1484void
1485xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
1486{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001487 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001488
Daniel Veillard22cdb842004-10-04 14:09:17 +00001489 if (output == NULL)
1490 output = stdout;
1491 xmlCtxtDumpInitCtxt(&ctxt);
1492 ctxt.output = output;
1493 xmlCtxtDumpDocumentHead(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001494 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001495}
1496
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001497/**
1498 * xmlDebugDumpDocument:
1499 * @output: the FILE * for the output
1500 * @doc: the document
1501 *
1502 * Dumps debug information for the document, it's recursive
1503 */
1504void
1505xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
1506{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001507 xmlDebugCtxt ctxt;
1508
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001509 if (output == NULL)
Daniel Veillard22cdb842004-10-04 14:09:17 +00001510 output = stdout;
1511 xmlCtxtDumpInitCtxt(&ctxt);
1512 ctxt.output = output;
1513 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001514 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001515}
Owen Taylor3473f882001-02-23 17:55:21 +00001516
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001517/**
1518 * xmlDebugDumpDTD:
1519 * @output: the FILE * for the output
1520 * @dtd: the DTD
1521 *
1522 * Dumps debug information for the DTD
1523 */
1524void
1525xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
1526{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001527 xmlDebugCtxt ctxt;
1528
Daniel Veillard7db38712002-02-07 16:39:11 +00001529 if (output == NULL)
1530 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001531 xmlCtxtDumpInitCtxt(&ctxt);
1532 ctxt.output = output;
1533 xmlCtxtDumpDTD(&ctxt, dtd);
Daniel Veillard76821142004-10-09 20:39:04 +00001534 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001535}
1536
Daniel Veillard22cdb842004-10-04 14:09:17 +00001537/************************************************************************
1538 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001539 * Public entry points for checkings *
1540 * *
1541 ************************************************************************/
1542
1543/**
1544 * xmlDebugCheckDocument:
1545 * @output: the FILE * for the output
1546 * @doc: the document
1547 *
1548 * Check the document for potential content problems, and output
1549 * the errors to @output
1550 *
1551 * Returns the number of errors found
1552 */
1553int
1554xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
1555{
1556 xmlDebugCtxt ctxt;
1557
1558 if (output == NULL)
1559 output = stdout;
1560 xmlCtxtDumpInitCtxt(&ctxt);
1561 ctxt.output = output;
1562 ctxt.check = 1;
1563 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001564 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001565 return(ctxt.errors);
1566}
1567
1568/************************************************************************
1569 * *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001570 * Helpers for Shell *
1571 * *
1572 ************************************************************************/
Owen Taylor3473f882001-02-23 17:55:21 +00001573
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001574/**
1575 * xmlLsCountNode:
1576 * @node: the node to count
1577 *
1578 * Count the children of @node.
1579 *
1580 * Returns the number of children of @node.
1581 */
1582int
1583xmlLsCountNode(xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00001584 int ret = 0;
1585 xmlNodePtr list = NULL;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001586
1587 if (node == NULL)
1588 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001589
1590 switch (node->type) {
1591 case XML_ELEMENT_NODE:
1592 list = node->children;
1593 break;
1594 case XML_DOCUMENT_NODE:
1595 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00001596#ifdef LIBXML_DOCB_ENABLED
1597 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001598#endif
1599 list = ((xmlDocPtr) node)->children;
1600 break;
1601 case XML_ATTRIBUTE_NODE:
1602 list = ((xmlAttrPtr) node)->children;
1603 break;
1604 case XML_TEXT_NODE:
1605 case XML_CDATA_SECTION_NODE:
1606 case XML_PI_NODE:
1607 case XML_COMMENT_NODE:
1608 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001609 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001610 }
1611 break;
1612 case XML_ENTITY_REF_NODE:
1613 case XML_DOCUMENT_TYPE_NODE:
1614 case XML_ENTITY_NODE:
1615 case XML_DOCUMENT_FRAG_NODE:
1616 case XML_NOTATION_NODE:
1617 case XML_DTD_NODE:
1618 case XML_ELEMENT_DECL:
1619 case XML_ATTRIBUTE_DECL:
1620 case XML_ENTITY_DECL:
1621 case XML_NAMESPACE_DECL:
1622 case XML_XINCLUDE_START:
1623 case XML_XINCLUDE_END:
1624 ret = 1;
1625 break;
1626 }
1627 for (;list != NULL;ret++)
1628 list = list->next;
1629 return(ret);
1630}
1631
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001632/**
1633 * xmlLsOneNode:
1634 * @output: the FILE * for the output
1635 * @node: the node to dump
1636 *
1637 * Dump to @output the type and name of @node.
1638 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001639void
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001640xmlLsOneNode(FILE *output, xmlNodePtr node) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00001641 if (output == NULL) return;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001642 if (node == NULL) {
1643 fprintf(output, "NULL\n");
1644 return;
1645 }
Owen Taylor3473f882001-02-23 17:55:21 +00001646 switch (node->type) {
1647 case XML_ELEMENT_NODE:
1648 fprintf(output, "-");
1649 break;
1650 case XML_ATTRIBUTE_NODE:
1651 fprintf(output, "a");
1652 break;
1653 case XML_TEXT_NODE:
1654 fprintf(output, "t");
1655 break;
1656 case XML_CDATA_SECTION_NODE:
Daniel Veillard75be0132002-03-13 10:03:35 +00001657 fprintf(output, "C");
Owen Taylor3473f882001-02-23 17:55:21 +00001658 break;
1659 case XML_ENTITY_REF_NODE:
1660 fprintf(output, "e");
1661 break;
1662 case XML_ENTITY_NODE:
1663 fprintf(output, "E");
1664 break;
1665 case XML_PI_NODE:
1666 fprintf(output, "p");
1667 break;
1668 case XML_COMMENT_NODE:
1669 fprintf(output, "c");
1670 break;
1671 case XML_DOCUMENT_NODE:
1672 fprintf(output, "d");
1673 break;
1674 case XML_HTML_DOCUMENT_NODE:
1675 fprintf(output, "h");
1676 break;
1677 case XML_DOCUMENT_TYPE_NODE:
1678 fprintf(output, "T");
1679 break;
1680 case XML_DOCUMENT_FRAG_NODE:
1681 fprintf(output, "F");
1682 break;
1683 case XML_NOTATION_NODE:
1684 fprintf(output, "N");
1685 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001686 case XML_NAMESPACE_DECL:
1687 fprintf(output, "n");
1688 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001689 default:
1690 fprintf(output, "?");
1691 }
Daniel Veillarde6a55192002-01-14 17:11:53 +00001692 if (node->type != XML_NAMESPACE_DECL) {
1693 if (node->properties != NULL)
1694 fprintf(output, "a");
1695 else
1696 fprintf(output, "-");
1697 if (node->nsDef != NULL)
1698 fprintf(output, "n");
1699 else
1700 fprintf(output, "-");
1701 }
Owen Taylor3473f882001-02-23 17:55:21 +00001702
1703 fprintf(output, " %8d ", xmlLsCountNode(node));
1704
1705 switch (node->type) {
1706 case XML_ELEMENT_NODE:
1707 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001708 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001709 break;
1710 case XML_ATTRIBUTE_NODE:
1711 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001712 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001713 break;
1714 case XML_TEXT_NODE:
1715 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001716 xmlDebugDumpString(output, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001717 }
1718 break;
1719 case XML_CDATA_SECTION_NODE:
1720 break;
1721 case XML_ENTITY_REF_NODE:
1722 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001723 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001724 break;
1725 case XML_ENTITY_NODE:
1726 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001727 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001728 break;
1729 case XML_PI_NODE:
1730 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001731 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001732 break;
1733 case XML_COMMENT_NODE:
1734 break;
1735 case XML_DOCUMENT_NODE:
1736 break;
1737 case XML_HTML_DOCUMENT_NODE:
1738 break;
1739 case XML_DOCUMENT_TYPE_NODE:
1740 break;
1741 case XML_DOCUMENT_FRAG_NODE:
1742 break;
1743 case XML_NOTATION_NODE:
1744 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001745 case XML_NAMESPACE_DECL: {
1746 xmlNsPtr ns = (xmlNsPtr) node;
1747
1748 if (ns->prefix == NULL)
William M. Brack13dfa872004-09-18 04:52:08 +00001749 fprintf(output, "default -> %s", (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001750 else
William M. Brack13dfa872004-09-18 04:52:08 +00001751 fprintf(output, "%s -> %s", (char *)ns->prefix,
1752 (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001753 break;
1754 }
Owen Taylor3473f882001-02-23 17:55:21 +00001755 default:
1756 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001757 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001758 }
1759 fprintf(output, "\n");
1760}
1761
Daniel Veillard78d12092001-10-11 09:12:24 +00001762/**
1763 * xmlBoolToText:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001764 * @boolval: a bool to turn into text
Daniel Veillard78d12092001-10-11 09:12:24 +00001765 *
1766 * Convenient way to turn bool into text
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001767 *
1768 * Returns a pointer to either "True" or "False"
1769 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001770const char *
Daniel Veillardebd38c52001-11-01 08:38:12 +00001771xmlBoolToText(int boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001772{
Daniel Veillardebd38c52001-11-01 08:38:12 +00001773 if (boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001774 return("True");
1775 else
1776 return("False");
1777}
1778
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00001779#ifdef LIBXML_XPATH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001780/****************************************************************
1781 * *
1782 * The XML shell related functions *
1783 * *
1784 ****************************************************************/
1785
Daniel Veillard78d12092001-10-11 09:12:24 +00001786
1787
Owen Taylor3473f882001-02-23 17:55:21 +00001788/*
1789 * TODO: Improvement/cleanups for the XML shell
1790 * - allow to shell out an editor on a subpart
1791 * - cleanup function registrations (with help) and calling
1792 * - provide registration routines
1793 */
1794
1795/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001796 * xmlShellPrintXPathError:
Daniel Veillard78d12092001-10-11 09:12:24 +00001797 * @errorType: valid xpath error id
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001798 * @arg: the argument that cause xpath to fail
Daniel Veillard78d12092001-10-11 09:12:24 +00001799 *
1800 * Print the xpath error to libxml default error channel
1801 */
1802void
1803xmlShellPrintXPathError(int errorType, const char *arg)
1804{
1805 const char *default_arg = "Result";
1806
1807 if (!arg)
1808 arg = default_arg;
1809
1810 switch (errorType) {
1811 case XPATH_UNDEFINED:
1812 xmlGenericError(xmlGenericErrorContext,
1813 "%s: no such node\n", arg);
1814 break;
1815
1816 case XPATH_BOOLEAN:
1817 xmlGenericError(xmlGenericErrorContext,
1818 "%s is a Boolean\n", arg);
1819 break;
1820 case XPATH_NUMBER:
1821 xmlGenericError(xmlGenericErrorContext,
1822 "%s is a number\n", arg);
1823 break;
1824 case XPATH_STRING:
1825 xmlGenericError(xmlGenericErrorContext,
1826 "%s is a string\n", arg);
1827 break;
1828 case XPATH_POINT:
1829 xmlGenericError(xmlGenericErrorContext,
1830 "%s is a point\n", arg);
1831 break;
1832 case XPATH_RANGE:
1833 xmlGenericError(xmlGenericErrorContext,
1834 "%s is a range\n", arg);
1835 break;
1836 case XPATH_LOCATIONSET:
1837 xmlGenericError(xmlGenericErrorContext,
1838 "%s is a range\n", arg);
1839 break;
1840 case XPATH_USERS:
1841 xmlGenericError(xmlGenericErrorContext,
1842 "%s is user-defined\n", arg);
1843 break;
1844 case XPATH_XSLT_TREE:
1845 xmlGenericError(xmlGenericErrorContext,
1846 "%s is an XSLT value tree\n", arg);
1847 break;
1848 }
Daniel Veillarda82b1822004-11-08 16:24:57 +00001849#if 0
Daniel Veillard78d12092001-10-11 09:12:24 +00001850 xmlGenericError(xmlGenericErrorContext,
1851 "Try casting the result string function (xpath builtin)\n",
1852 arg);
Daniel Veillarda82b1822004-11-08 16:24:57 +00001853#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00001854}
1855
1856
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001857#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001858/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001859 * xmlShellPrintNodeCtxt:
1860 * @ctxt : a non-null shell context
1861 * @node : a non-null node to print to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001862 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001863 * Print node to the output FILE
1864 */
1865static void
1866xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1867{
Daniel Veillard01992e02002-10-09 10:20:30 +00001868 FILE *fp;
1869
1870 if (!node)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001871 return;
Daniel Veillard01992e02002-10-09 10:20:30 +00001872 if (ctxt == NULL)
1873 fp = stdout;
1874 else
1875 fp = ctxt->output;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001876
1877 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001878 xmlDocDump(fp, (xmlDocPtr) node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001879 else if (node->type == XML_ATTRIBUTE_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001880 xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001881 else
Daniel Veillard01992e02002-10-09 10:20:30 +00001882 xmlElemDump(fp, node->doc, node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001883
Daniel Veillard01992e02002-10-09 10:20:30 +00001884 fprintf(fp, "\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00001885}
1886
1887/**
1888 * xmlShellPrintNode:
1889 * @node : a non-null node to print to the output FILE
1890 *
1891 * Print node to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001892 */
1893void
1894xmlShellPrintNode(xmlNodePtr node)
1895{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001896 xmlShellPrintNodeCtxt(NULL, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001897}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001898#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001899
Daniel Veillard78d12092001-10-11 09:12:24 +00001900/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001901 * xmlShellPrintXPathResultCtxt:
1902 * @ctxt: a valid shell context
Daniel Veillard9d06d302002-01-22 18:15:52 +00001903 * @list: a valid result generated by an xpath evaluation
Daniel Veillard78d12092001-10-11 09:12:24 +00001904 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001905 * Prints result to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001906 */
Daniel Veillard321be0c2002-10-08 21:26:42 +00001907static void
1908xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
Daniel Veillard78d12092001-10-11 09:12:24 +00001909{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001910 if (!ctxt)
1911 return;
Daniel Veillard78d12092001-10-11 09:12:24 +00001912
1913 if (list != NULL) {
1914 switch (list->type) {
1915 case XPATH_NODESET:{
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001916#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001917 int indx;
1918
1919 if (list->nodesetval) {
1920 for (indx = 0; indx < list->nodesetval->nodeNr;
1921 indx++) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001922 xmlShellPrintNodeCtxt(ctxt,
1923 list->nodesetval->nodeTab[indx]);
Daniel Veillard78d12092001-10-11 09:12:24 +00001924 }
1925 } else {
1926 xmlGenericError(xmlGenericErrorContext,
1927 "Empty node set\n");
1928 }
1929 break;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001930#else
1931 xmlGenericError(xmlGenericErrorContext,
1932 "Node set\n");
1933#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001934 }
1935 case XPATH_BOOLEAN:
1936 xmlGenericError(xmlGenericErrorContext,
1937 "Is a Boolean:%s\n",
1938 xmlBoolToText(list->boolval));
1939 break;
1940 case XPATH_NUMBER:
1941 xmlGenericError(xmlGenericErrorContext,
1942 "Is a number:%0g\n", list->floatval);
1943 break;
1944 case XPATH_STRING:
1945 xmlGenericError(xmlGenericErrorContext,
1946 "Is a string:%s\n", list->stringval);
1947 break;
1948
1949 default:
1950 xmlShellPrintXPathError(list->type, NULL);
1951 }
1952 }
1953}
1954
1955/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001956 * xmlShellPrintXPathResult:
1957 * @list: a valid result generated by an xpath evaluation
1958 *
1959 * Prints result to the output FILE
1960 */
1961void
1962xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1963{
1964 xmlShellPrintXPathResultCtxt(NULL, list);
1965}
1966
1967/**
Owen Taylor3473f882001-02-23 17:55:21 +00001968 * xmlShellList:
1969 * @ctxt: the shell context
1970 * @arg: unused
1971 * @node: a node
1972 * @node2: unused
1973 *
1974 * Implements the XML shell function "ls"
1975 * Does an Unix like listing of the given node (like a directory)
1976 *
1977 * Returns 0
1978 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001979int
Daniel Veillard321be0c2002-10-08 21:26:42 +00001980xmlShellList(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00001981 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1982 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1983{
Owen Taylor3473f882001-02-23 17:55:21 +00001984 xmlNodePtr cur;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001985 if (!ctxt)
1986 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001987 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001988 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001989 return (0);
1990 }
Owen Taylor3473f882001-02-23 17:55:21 +00001991 if ((node->type == XML_DOCUMENT_NODE) ||
1992 (node->type == XML_HTML_DOCUMENT_NODE)) {
1993 cur = ((xmlDocPtr) node)->children;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001994 } else if (node->type == XML_NAMESPACE_DECL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001995 xmlLsOneNode(ctxt->output, node);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001996 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001997 } else if (node->children != NULL) {
1998 cur = node->children;
1999 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002000 xmlLsOneNode(ctxt->output, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002001 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002002 }
2003 while (cur != NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002004 xmlLsOneNode(ctxt->output, cur);
Daniel Veillard78d12092001-10-11 09:12:24 +00002005 cur = cur->next;
Owen Taylor3473f882001-02-23 17:55:21 +00002006 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002007 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002008}
2009
2010/**
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002011 * xmlShellBase:
2012 * @ctxt: the shell context
2013 * @arg: unused
2014 * @node: a node
2015 * @node2: unused
2016 *
2017 * Implements the XML shell function "base"
2018 * dumps the current XML base of the node
2019 *
2020 * Returns 0
2021 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002022int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002023xmlShellBase(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002024 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2025 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2026{
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002027 xmlChar *base;
Daniel Veillard321be0c2002-10-08 21:26:42 +00002028 if (!ctxt)
2029 return 0;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002030 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002031 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002032 return (0);
2033 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002034
2035 base = xmlNodeGetBase(node->doc, node);
2036
2037 if (base == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002038 fprintf(ctxt->output, " No base found !!!\n");
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002039 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002040 fprintf(ctxt->output, "%s\n", base);
Daniel Veillard78d12092001-10-11 09:12:24 +00002041 xmlFree(base);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002042 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002043 return (0);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002044}
2045
Daniel Veillardb34321c2004-03-04 17:09:47 +00002046#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002047/**
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002048 * xmlShellSetBase:
2049 * @ctxt: the shell context
2050 * @arg: the new base
2051 * @node: a node
2052 * @node2: unused
2053 *
2054 * Implements the XML shell function "setbase"
2055 * change the current XML base of the node
2056 *
2057 * Returns 0
2058 */
2059static int
2060xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2061 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2062 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2063{
2064 xmlNodeSetBase(node, (xmlChar*) arg);
2065 return (0);
2066}
Daniel Veillard2156d432004-03-04 15:59:36 +00002067#endif
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002068
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002069#ifdef LIBXML_XPATH_ENABLED
2070/**
2071 * xmlShellRegisterNamespace:
2072 * @ctxt: the shell context
2073 * @arg: a string in prefix=nsuri format
2074 * @node: unused
2075 * @node2: unused
2076 *
2077 * Implements the XML shell function "setns"
2078 * register/unregister a prefix=namespace pair
2079 * on the XPath context
2080 *
2081 * Returns 0 on success and a negative value otherwise.
2082 */
2083static int
2084xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
2085 xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2086{
2087 xmlChar* nsListDup;
2088 xmlChar* prefix;
2089 xmlChar* href;
2090 xmlChar* next;
2091
2092 nsListDup = xmlStrdup((xmlChar *) arg);
2093 next = nsListDup;
2094 while(next != NULL) {
2095 /* skip spaces */
2096 /*while((*next) == ' ') next++;*/
2097 if((*next) == '\0') break;
2098
2099 /* find prefix */
2100 prefix = next;
2101 next = (xmlChar*)xmlStrchr(next, '=');
2102 if(next == NULL) {
2103 fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
2104 xmlFree(nsListDup);
2105 return(-1);
2106 }
2107 *(next++) = '\0';
2108
2109 /* find href */
2110 href = next;
2111 next = (xmlChar*)xmlStrchr(next, ' ');
2112 if(next != NULL) {
2113 *(next++) = '\0';
2114 }
2115
2116 /* do register namespace */
2117 if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
2118 fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
2119 xmlFree(nsListDup);
2120 return(-1);
2121 }
2122 }
2123
2124 xmlFree(nsListDup);
2125 return(0);
2126}
Daniel Veillard20887ee2005-07-04 09:27:40 +00002127/**
2128 * xmlShellRegisterRootNamespaces:
2129 * @ctxt: the shell context
2130 * @arg: unused
2131 * @node: the root element
2132 * @node2: unused
2133 *
2134 * Implements the XML shell function "setrootns"
2135 * which registers all namespaces declarations found on the root element.
2136 *
2137 * Returns 0 on success and a negative value otherwise.
2138 */
2139static int
2140xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2141 xmlNodePtr root, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2142{
2143 xmlNsPtr ns;
2144
2145 if ((root == NULL) || (root->type != XML_ELEMENT_NODE) ||
2146 (root->nsDef == NULL) || (ctxt == NULL) || (ctxt->pctxt == NULL))
2147 return(-1);
2148 ns = root->nsDef;
2149 while (ns != NULL) {
2150 if (ns->prefix == NULL)
2151 xmlXPathRegisterNs(ctxt->pctxt, BAD_CAST "defaultns", ns->href);
2152 else
2153 xmlXPathRegisterNs(ctxt->pctxt, ns->prefix, ns->href);
2154 ns = ns->next;
2155 }
2156 return(0);
2157}
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002158#endif
2159
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002160/**
Daniel Veillard1e208222002-10-22 14:25:25 +00002161 * xmlShellGrep:
2162 * @ctxt: the shell context
2163 * @arg: the string or regular expression to find
2164 * @node: a node
2165 * @node2: unused
2166 *
2167 * Implements the XML shell function "grep"
2168 * dumps informations about the node (namespace, attributes, content).
2169 *
2170 * Returns 0
2171 */
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002172static int
Daniel Veillard1e208222002-10-22 14:25:25 +00002173xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2174 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2175{
2176 if (!ctxt)
2177 return (0);
2178 if (node == NULL)
2179 return (0);
2180 if (arg == NULL)
2181 return (0);
2182#ifdef LIBXML_REGEXP_ENABLED
2183 if ((xmlStrchr((xmlChar *) arg, '?')) ||
2184 (xmlStrchr((xmlChar *) arg, '*')) ||
2185 (xmlStrchr((xmlChar *) arg, '.')) ||
2186 (xmlStrchr((xmlChar *) arg, '['))) {
2187 }
2188#endif
2189 while (node != NULL) {
2190 if (node->type == XML_COMMENT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002191 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002192
2193 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
2194 xmlShellList(ctxt, NULL, node, NULL);
2195 }
2196 } else if (node->type == XML_TEXT_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->parent));
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002200 xmlShellList(ctxt, NULL, node->parent, NULL);
Daniel Veillard1e208222002-10-22 14:25:25 +00002201 }
2202 }
2203
2204 /*
2205 * Browse the full subtree, deep first
2206 */
2207
2208 if ((node->type == XML_DOCUMENT_NODE) ||
2209 (node->type == XML_HTML_DOCUMENT_NODE)) {
2210 node = ((xmlDocPtr) node)->children;
2211 } else if ((node->children != NULL)
2212 && (node->type != XML_ENTITY_REF_NODE)) {
2213 /* deep first */
2214 node = node->children;
2215 } else if (node->next != NULL) {
2216 /* then siblings */
2217 node = node->next;
2218 } else {
2219 /* go up to parents->next if needed */
2220 while (node != NULL) {
2221 if (node->parent != NULL) {
2222 node = node->parent;
2223 }
2224 if (node->next != NULL) {
2225 node = node->next;
2226 break;
2227 }
2228 if (node->parent == NULL) {
2229 node = NULL;
2230 break;
2231 }
2232 }
2233 }
2234 }
2235 return (0);
2236}
2237
2238/**
Owen Taylor3473f882001-02-23 17:55:21 +00002239 * xmlShellDir:
2240 * @ctxt: the shell context
2241 * @arg: unused
2242 * @node: a node
2243 * @node2: unused
2244 *
2245 * Implements the XML shell function "dir"
2246 * dumps informations about the node (namespace, attributes, content).
2247 *
2248 * Returns 0
2249 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002250int
2251xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2252 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2253 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2254{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002255 if (!ctxt)
2256 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002257 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002258 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002259 return (0);
2260 }
Owen Taylor3473f882001-02-23 17:55:21 +00002261 if ((node->type == XML_DOCUMENT_NODE) ||
2262 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002263 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
Owen Taylor3473f882001-02-23 17:55:21 +00002264 } else if (node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002265 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002266 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002267 xmlDebugDumpOneNode(ctxt->output, node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002268 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002269 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002270}
2271
Daniel Veillard29b17482004-08-16 00:39:03 +00002272/**
2273 * xmlShellSetContent:
2274 * @ctxt: the shell context
2275 * @value: the content as a string
2276 * @node: a node
2277 * @node2: unused
2278 *
2279 * Implements the XML shell function "dir"
2280 * dumps informations about the node (namespace, attributes, content).
2281 *
2282 * Returns 0
2283 */
2284static int
2285xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2286 char *value, xmlNodePtr node,
2287 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2288{
2289 xmlNodePtr results;
2290 xmlParserErrors ret;
2291
2292 if (!ctxt)
2293 return (0);
2294 if (node == NULL) {
2295 fprintf(ctxt->output, "NULL\n");
2296 return (0);
2297 }
2298 if (value == NULL) {
2299 fprintf(ctxt->output, "NULL\n");
2300 return (0);
2301 }
2302
2303 ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
2304 if (ret == XML_ERR_OK) {
2305 if (node->children != NULL) {
2306 xmlFreeNodeList(node->children);
2307 node->children = NULL;
2308 node->last = NULL;
2309 }
2310 xmlAddChildList(node, results);
2311 } else {
2312 fprintf(ctxt->output, "failed to parse content\n");
2313 }
2314 return (0);
2315}
2316
Daniel Veillard522bc602004-02-21 11:53:09 +00002317#ifdef LIBXML_SCHEMAS_ENABLED
2318/**
2319 * xmlShellRNGValidate:
2320 * @ctxt: the shell context
2321 * @schemas: the path to the Relax-NG schemas
2322 * @node: a node
2323 * @node2: unused
2324 *
2325 * Implements the XML shell function "relaxng"
2326 * validating the instance against a Relax-NG schemas
2327 *
2328 * Returns 0
2329 */
2330static int
2331xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
2332 xmlNodePtr node ATTRIBUTE_UNUSED,
2333 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2334{
2335 xmlRelaxNGPtr relaxngschemas;
2336 xmlRelaxNGParserCtxtPtr ctxt;
2337 xmlRelaxNGValidCtxtPtr vctxt;
2338 int ret;
2339
2340 ctxt = xmlRelaxNGNewParserCtxt(schemas);
2341 xmlRelaxNGSetParserErrors(ctxt,
2342 (xmlRelaxNGValidityErrorFunc) fprintf,
2343 (xmlRelaxNGValidityWarningFunc) fprintf,
2344 stderr);
2345 relaxngschemas = xmlRelaxNGParse(ctxt);
2346 xmlRelaxNGFreeParserCtxt(ctxt);
2347 if (relaxngschemas == NULL) {
2348 xmlGenericError(xmlGenericErrorContext,
2349 "Relax-NG schema %s failed to compile\n", schemas);
2350 return(-1);
2351 }
2352 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2353 xmlRelaxNGSetValidErrors(vctxt,
2354 (xmlRelaxNGValidityErrorFunc) fprintf,
2355 (xmlRelaxNGValidityWarningFunc) fprintf,
2356 stderr);
2357 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2358 if (ret == 0) {
2359 fprintf(stderr, "%s validates\n", sctxt->filename);
2360 } else if (ret > 0) {
2361 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2362 } else {
2363 fprintf(stderr, "%s validation generated an internal error\n",
2364 sctxt->filename);
2365 }
2366 xmlRelaxNGFreeValidCtxt(vctxt);
2367 if (relaxngschemas != NULL)
2368 xmlRelaxNGFree(relaxngschemas);
2369 return(0);
2370}
2371#endif
2372
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002373#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002374/**
2375 * xmlShellCat:
2376 * @ctxt: the shell context
2377 * @arg: unused
2378 * @node: a node
2379 * @node2: unused
2380 *
2381 * Implements the XML shell function "cat"
2382 * dumps the serialization node content (XML or HTML).
2383 *
2384 * Returns 0
2385 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002386int
2387xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2388 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2389{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002390 if (!ctxt)
2391 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002392 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002393 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002394 return (0);
2395 }
Owen Taylor3473f882001-02-23 17:55:21 +00002396 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2397#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002398 if (node->type == XML_HTML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002399 htmlDocDump(ctxt->output, (htmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002400 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002401 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002402#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002403 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002404 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002405 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002406 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002407#endif /* LIBXML_HTML_ENABLED */
2408 } 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 }
Daniel Veillard321be0c2002-10-08 21:26:42 +00002414 fprintf(ctxt->output, "\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002415 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002416}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002417#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002418
2419/**
2420 * xmlShellLoad:
2421 * @ctxt: the shell context
2422 * @filename: the file name
2423 * @node: unused
2424 * @node2: unused
2425 *
2426 * Implements the XML shell function "load"
2427 * loads a new document specified by the filename
2428 *
2429 * Returns 0 or -1 if loading failed
2430 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002431int
2432xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2433 xmlNodePtr node ATTRIBUTE_UNUSED,
2434 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2435{
Owen Taylor3473f882001-02-23 17:55:21 +00002436 xmlDocPtr doc;
2437 int html = 0;
2438
Daniel Veillarda82b1822004-11-08 16:24:57 +00002439 if ((ctxt == NULL) || (filename == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002440 if (ctxt->doc != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002441 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00002442
2443 if (html) {
2444#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002445 doc = htmlParseFile(filename, NULL);
2446#else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002447 fprintf(ctxt->output, "HTML support not compiled in\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002448 doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002449#endif /* LIBXML_HTML_ENABLED */
2450 } else {
Daniel Veillardebe25d42004-03-25 09:35:49 +00002451 doc = xmlReadFile(filename,NULL,0);
Owen Taylor3473f882001-02-23 17:55:21 +00002452 }
2453 if (doc != NULL) {
2454 if (ctxt->loaded == 1) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002455 xmlFreeDoc(ctxt->doc);
2456 }
2457 ctxt->loaded = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002458#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002459 xmlXPathFreeContext(ctxt->pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00002460#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002461 xmlFree(ctxt->filename);
2462 ctxt->doc = doc;
2463 ctxt->node = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002464#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002465 ctxt->pctxt = xmlXPathNewContext(doc);
Owen Taylor3473f882001-02-23 17:55:21 +00002466#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard85095e22003-04-23 13:56:44 +00002467 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002468 } else
Daniel Veillard78d12092001-10-11 09:12:24 +00002469 return (-1);
2470 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002471}
2472
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002473#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002474/**
2475 * xmlShellWrite:
2476 * @ctxt: the shell context
2477 * @filename: the file name
2478 * @node: a node in the tree
2479 * @node2: unused
2480 *
2481 * Implements the XML shell function "write"
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002482 * Write the current node to the filename, it saves the serialization
Owen Taylor3473f882001-02-23 17:55:21 +00002483 * of the subtree under the @node specified
2484 *
2485 * Returns 0 or -1 in case of error
2486 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002487int
Owen Taylor3473f882001-02-23 17:55:21 +00002488xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Daniel Veillard78d12092001-10-11 09:12:24 +00002489 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2490{
Owen Taylor3473f882001-02-23 17:55:21 +00002491 if (node == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002492 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002493 if ((filename == NULL) || (filename[0] == 0)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002494 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002495 }
2496#ifdef W_OK
2497 if (access((char *) filename, W_OK)) {
2498 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002499 "Cannot write to %s\n", filename);
2500 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002501 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002502#endif
2503 switch (node->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002504 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002505 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2506 xmlGenericError(xmlGenericErrorContext,
2507 "Failed to write to %s\n", filename);
2508 return (-1);
2509 }
2510 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002511 case XML_HTML_DOCUMENT_NODE:
2512#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002513 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2514 xmlGenericError(xmlGenericErrorContext,
2515 "Failed to write to %s\n", filename);
2516 return (-1);
2517 }
Owen Taylor3473f882001-02-23 17:55:21 +00002518#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002519 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2520 xmlGenericError(xmlGenericErrorContext,
2521 "Failed to write to %s\n", filename);
2522 return (-1);
2523 }
Owen Taylor3473f882001-02-23 17:55:21 +00002524#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002525 break;
2526 default:{
2527 FILE *f;
Owen Taylor3473f882001-02-23 17:55:21 +00002528
Daniel Veillard78d12092001-10-11 09:12:24 +00002529 f = fopen((char *) filename, "w");
2530 if (f == NULL) {
2531 xmlGenericError(xmlGenericErrorContext,
2532 "Failed to write to %s\n", filename);
2533 return (-1);
2534 }
2535 xmlElemDump(f, ctxt->doc, node);
2536 fclose(f);
2537 }
Owen Taylor3473f882001-02-23 17:55:21 +00002538 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002539 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002540}
2541
2542/**
2543 * xmlShellSave:
2544 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002545 * @filename: the file name (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002546 * @node: unused
2547 * @node2: unused
2548 *
2549 * Implements the XML shell function "save"
2550 * Write the current document to the filename, or it's original name
2551 *
2552 * Returns 0 or -1 in case of error
2553 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002554int
2555xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2556 xmlNodePtr node ATTRIBUTE_UNUSED,
2557 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2558{
Daniel Veillarda82b1822004-11-08 16:24:57 +00002559 if ((ctxt == NULL) || (ctxt->doc == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002560 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002561 if ((filename == NULL) || (filename[0] == 0))
2562 filename = ctxt->filename;
Daniel Veillarda82b1822004-11-08 16:24:57 +00002563 if (filename == NULL)
2564 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002565#ifdef W_OK
2566 if (access((char *) filename, W_OK)) {
2567 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002568 "Cannot save to %s\n", filename);
2569 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002570 }
2571#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002572 switch (ctxt->doc->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002573 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002574 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2575 xmlGenericError(xmlGenericErrorContext,
2576 "Failed to save to %s\n", filename);
2577 }
2578 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002579 case XML_HTML_DOCUMENT_NODE:
2580#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002581 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2582 xmlGenericError(xmlGenericErrorContext,
2583 "Failed to save to %s\n", filename);
2584 }
Owen Taylor3473f882001-02-23 17:55:21 +00002585#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002586 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2587 xmlGenericError(xmlGenericErrorContext,
2588 "Failed to save to %s\n", filename);
2589 }
Owen Taylor3473f882001-02-23 17:55:21 +00002590#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002591 break;
2592 default:
2593 xmlGenericError(xmlGenericErrorContext,
2594 "To save to subparts of a document use the 'write' command\n");
2595 return (-1);
2596
Owen Taylor3473f882001-02-23 17:55:21 +00002597 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002598 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002599}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002600#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002601
Daniel Veillardf54cd532004-02-25 11:52:31 +00002602#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002603/**
2604 * xmlShellValidate:
2605 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002606 * @dtd: the DTD URI (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002607 * @node: unused
2608 * @node2: unused
2609 *
2610 * Implements the XML shell function "validate"
2611 * Validate the document, if a DTD path is provided, then the validation
2612 * is done against the given DTD.
2613 *
2614 * Returns 0 or -1 in case of error
2615 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002616int
2617xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2618 xmlNodePtr node ATTRIBUTE_UNUSED,
2619 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2620{
Owen Taylor3473f882001-02-23 17:55:21 +00002621 xmlValidCtxt vctxt;
2622 int res = -1;
2623
Daniel Veillarda82b1822004-11-08 16:24:57 +00002624 if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002625 vctxt.userData = stderr;
2626 vctxt.error = (xmlValidityErrorFunc) fprintf;
2627 vctxt.warning = (xmlValidityWarningFunc) fprintf;
2628
2629 if ((dtd == NULL) || (dtd[0] == 0)) {
2630 res = xmlValidateDocument(&vctxt, ctxt->doc);
2631 } else {
2632 xmlDtdPtr subset;
2633
Daniel Veillard78d12092001-10-11 09:12:24 +00002634 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2635 if (subset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002636 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2637
Daniel Veillard78d12092001-10-11 09:12:24 +00002638 xmlFreeDtd(subset);
2639 }
Owen Taylor3473f882001-02-23 17:55:21 +00002640 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002641 return (res);
Owen Taylor3473f882001-02-23 17:55:21 +00002642}
Daniel Veillardf54cd532004-02-25 11:52:31 +00002643#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002644
2645/**
2646 * xmlShellDu:
2647 * @ctxt: the shell context
2648 * @arg: unused
2649 * @tree: a node defining a subtree
2650 * @node2: unused
2651 *
2652 * Implements the XML shell function "du"
2653 * show the structure of the subtree under node @tree
2654 * If @tree is null, the command works on the current node.
2655 *
2656 * Returns 0 or -1 in case of error
2657 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002658int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002659xmlShellDu(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002660 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2661 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2662{
Owen Taylor3473f882001-02-23 17:55:21 +00002663 xmlNodePtr node;
Daniel Veillard78d12092001-10-11 09:12:24 +00002664 int indent = 0, i;
Owen Taylor3473f882001-02-23 17:55:21 +00002665
Daniel Veillard321be0c2002-10-08 21:26:42 +00002666 if (!ctxt)
2667 return (-1);
2668
Daniel Veillard78d12092001-10-11 09:12:24 +00002669 if (tree == NULL)
2670 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002671 node = tree;
2672 while (node != NULL) {
2673 if ((node->type == XML_DOCUMENT_NODE) ||
2674 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002675 fprintf(ctxt->output, "/\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002676 } else if (node->type == XML_ELEMENT_NODE) {
2677 for (i = 0; i < indent; i++)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002678 fprintf(ctxt->output, " ");
2679 fprintf(ctxt->output, "%s\n", node->name);
Daniel Veillard78d12092001-10-11 09:12:24 +00002680 } else {
2681 }
Owen Taylor3473f882001-02-23 17:55:21 +00002682
Daniel Veillard78d12092001-10-11 09:12:24 +00002683 /*
2684 * Browse the full subtree, deep first
2685 */
Owen Taylor3473f882001-02-23 17:55:21 +00002686
2687 if ((node->type == XML_DOCUMENT_NODE) ||
2688 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002689 node = ((xmlDocPtr) node)->children;
2690 } else if ((node->children != NULL)
2691 && (node->type != XML_ENTITY_REF_NODE)) {
2692 /* deep first */
2693 node = node->children;
2694 indent++;
2695 } else if ((node != tree) && (node->next != NULL)) {
2696 /* then siblings */
2697 node = node->next;
2698 } else if (node != tree) {
2699 /* go up to parents->next if needed */
2700 while (node != tree) {
2701 if (node->parent != NULL) {
2702 node = node->parent;
2703 indent--;
2704 }
2705 if ((node != tree) && (node->next != NULL)) {
2706 node = node->next;
2707 break;
2708 }
2709 if (node->parent == NULL) {
2710 node = NULL;
2711 break;
2712 }
2713 if (node == tree) {
2714 node = NULL;
2715 break;
2716 }
2717 }
2718 /* exit condition */
2719 if (node == tree)
2720 node = NULL;
2721 } else
2722 node = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002723 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002724 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002725}
2726
2727/**
2728 * xmlShellPwd:
2729 * @ctxt: the shell context
2730 * @buffer: the output buffer
Daniel Veillard9d06d302002-01-22 18:15:52 +00002731 * @node: a node
Owen Taylor3473f882001-02-23 17:55:21 +00002732 * @node2: unused
2733 *
2734 * Implements the XML shell function "pwd"
2735 * Show the full path from the root to the node, if needed building
2736 * thumblers when similar elements exists at a given ancestor level.
2737 * The output is compatible with XPath commands.
2738 *
2739 * Returns 0 or -1 in case of error
2740 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002741int
2742xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2743 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2744{
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002745 xmlChar *path;
Owen Taylor3473f882001-02-23 17:55:21 +00002746
Daniel Veillarda82b1822004-11-08 16:24:57 +00002747 if ((node == NULL) || (buffer == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002748 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002749
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002750 path = xmlGetNodePath(node);
2751 if (path == NULL)
2752 return (-1);
2753
2754 /*
2755 * This test prevents buffer overflow, because this routine
2756 * is only called by xmlShell, in which the second argument is
2757 * 500 chars long.
2758 * It is a dirty hack before a cleaner solution is found.
2759 * Documentation should mention that the second argument must
2760 * be at least 500 chars long, and could be stripped if too long.
2761 */
2762 snprintf(buffer, 499, "%s", path);
2763 buffer[499] = '0';
2764 xmlFree(path);
2765
Daniel Veillard78d12092001-10-11 09:12:24 +00002766 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002767}
2768
2769/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002770 * xmlShell:
Owen Taylor3473f882001-02-23 17:55:21 +00002771 * @doc: the initial document
2772 * @filename: the output buffer
2773 * @input: the line reading function
Daniel Veillard321be0c2002-10-08 21:26:42 +00002774 * @output: the output FILE*, defaults to stdout if NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002775 *
2776 * Implements the XML shell
2777 * This allow to load, validate, view, modify and save a document
2778 * using a environment similar to a UNIX commandline.
2779 */
2780void
2781xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Daniel Veillard78d12092001-10-11 09:12:24 +00002782 FILE * output)
2783{
Owen Taylor3473f882001-02-23 17:55:21 +00002784 char prompt[500] = "/ > ";
2785 char *cmdline = NULL, *cur;
2786 int nbargs;
2787 char command[100];
2788 char arg[400];
2789 int i;
2790 xmlShellCtxtPtr ctxt;
2791 xmlXPathObjectPtr list;
2792
2793 if (doc == NULL)
2794 return;
2795 if (filename == NULL)
2796 return;
2797 if (input == NULL)
2798 return;
2799 if (output == NULL)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002800 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00002801 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Daniel Veillard78d12092001-10-11 09:12:24 +00002802 if (ctxt == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00002803 return;
2804 ctxt->loaded = 0;
2805 ctxt->doc = doc;
2806 ctxt->input = input;
2807 ctxt->output = output;
2808 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Daniel Veillard78d12092001-10-11 09:12:24 +00002809 ctxt->node = (xmlNodePtr) ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002810
2811#ifdef LIBXML_XPATH_ENABLED
2812 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2813 if (ctxt->pctxt == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002814 xmlFree(ctxt);
2815 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002816 }
2817#endif /* LIBXML_XPATH_ENABLED */
2818 while (1) {
2819 if (ctxt->node == (xmlNodePtr) ctxt->doc)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002820 snprintf(prompt, sizeof(prompt), "%s > ", "/");
Daniel Veillard7a985a12003-07-06 17:57:42 +00002821 else if ((ctxt->node != NULL) && (ctxt->node->name))
Daniel Veillard78d12092001-10-11 09:12:24 +00002822 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002823 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002824 snprintf(prompt, sizeof(prompt), "? > ");
Owen Taylor3473f882001-02-23 17:55:21 +00002825 prompt[sizeof(prompt) - 1] = 0;
2826
Daniel Veillard78d12092001-10-11 09:12:24 +00002827 /*
2828 * Get a new command line
2829 */
Owen Taylor3473f882001-02-23 17:55:21 +00002830 cmdline = ctxt->input(prompt);
Daniel Veillard78d12092001-10-11 09:12:24 +00002831 if (cmdline == NULL)
2832 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002833
Daniel Veillard78d12092001-10-11 09:12:24 +00002834 /*
2835 * Parse the command itself
2836 */
2837 cur = cmdline;
2838 nbargs = 0;
2839 while ((*cur == ' ') || (*cur == '\t'))
2840 cur++;
2841 i = 0;
2842 while ((*cur != ' ') && (*cur != '\t') &&
2843 (*cur != '\n') && (*cur != '\r')) {
2844 if (*cur == 0)
2845 break;
2846 command[i++] = *cur++;
2847 }
2848 command[i] = 0;
2849 if (i == 0)
2850 continue;
2851 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002852
Daniel Veillard78d12092001-10-11 09:12:24 +00002853 /*
2854 * Parse the argument
2855 */
2856 while ((*cur == ' ') || (*cur == '\t'))
2857 cur++;
2858 i = 0;
2859 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2860 if (*cur == 0)
2861 break;
2862 arg[i++] = *cur++;
2863 }
2864 arg[i] = 0;
2865 if (i != 0)
2866 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002867
Daniel Veillard78d12092001-10-11 09:12:24 +00002868 /*
2869 * start interpreting the command
2870 */
Owen Taylor3473f882001-02-23 17:55:21 +00002871 if (!strcmp(command, "exit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002872 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002873 if (!strcmp(command, "quit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002874 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002875 if (!strcmp(command, "bye"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002876 break;
Daniel Veillard5004f422001-11-08 13:53:05 +00002877 if (!strcmp(command, "help")) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002878 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2879 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2880 fprintf(ctxt->output, "\tbye leave shell\n");
2881 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2882 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2883 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2884 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2885 fprintf(ctxt->output, "\texit leave shell\n");
2886 fprintf(ctxt->output, "\thelp display this help\n");
2887 fprintf(ctxt->output, "\tfree display memory usage\n");
2888 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2889 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00002890 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 +00002891#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002892 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002893 fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2894 fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
Daniel Veillard20887ee2005-07-04 09:27:40 +00002895 fprintf(ctxt->output, "\tsetrootns register all namespace found on the root element\n");
2896 fprintf(ctxt->output, "\t the default namespace if any uses 'defaultns' prefix\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002897#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard321be0c2002-10-08 21:26:42 +00002898 fprintf(ctxt->output, "\tpwd display current working directory\n");
2899 fprintf(ctxt->output, "\tquit leave shell\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002900#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002901 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00002902 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002903#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf54cd532004-02-25 11:52:31 +00002904#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002905 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002906#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard522bc602004-02-21 11:53:09 +00002907#ifdef LIBXML_SCHEMAS_ENABLED
2908 fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n");
2909#endif
Daniel Veillard1e208222002-10-22 14:25:25 +00002910 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002911#ifdef LIBXML_VALID_ENABLED
Daniel Veillard5004f422001-11-08 13:53:05 +00002912 } else if (!strcmp(command, "validate")) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002913 xmlShellValidate(ctxt, arg, NULL, NULL);
Daniel Veillardf54cd532004-02-25 11:52:31 +00002914#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002915 } else if (!strcmp(command, "load")) {
2916 xmlShellLoad(ctxt, arg, NULL, NULL);
Daniel Veillard522bc602004-02-21 11:53:09 +00002917#ifdef LIBXML_SCHEMAS_ENABLED
2918 } else if (!strcmp(command, "relaxng")) {
2919 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2920#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002921#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002922 } else if (!strcmp(command, "save")) {
2923 xmlShellSave(ctxt, arg, NULL, NULL);
2924 } else if (!strcmp(command, "write")) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00002925 if ((arg == NULL) || (arg[0] == 0))
2926 xmlGenericError(xmlGenericErrorContext,
2927 "Write command requires a filename argument\n");
2928 else
2929 xmlShellWrite(ctxt, arg, NULL, NULL);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002930#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard1e208222002-10-22 14:25:25 +00002931 } else if (!strcmp(command, "grep")) {
2932 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
Daniel Veillard78d12092001-10-11 09:12:24 +00002933 } else if (!strcmp(command, "free")) {
2934 if (arg[0] == 0) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002935 xmlMemShow(ctxt->output, 0);
Daniel Veillard78d12092001-10-11 09:12:24 +00002936 } else {
2937 int len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002938
Daniel Veillard78d12092001-10-11 09:12:24 +00002939 sscanf(arg, "%d", &len);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002940 xmlMemShow(ctxt->output, len);
Daniel Veillard78d12092001-10-11 09:12:24 +00002941 }
2942 } else if (!strcmp(command, "pwd")) {
2943 char dir[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002944
Daniel Veillard78d12092001-10-11 09:12:24 +00002945 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
Daniel Veillard321be0c2002-10-08 21:26:42 +00002946 fprintf(ctxt->output, "%s\n", dir);
Daniel Veillard78d12092001-10-11 09:12:24 +00002947 } else if (!strcmp(command, "du")) {
2948 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2949 } else if (!strcmp(command, "base")) {
2950 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
Daniel Veillard29b17482004-08-16 00:39:03 +00002951 } else if (!strcmp(command, "set")) {
2952 xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002953#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002954 } else if (!strcmp(command, "setns")) {
2955 if (arg[0] == 0) {
2956 xmlGenericError(xmlGenericErrorContext,
2957 "setns: prefix=[nsuri] required\n");
2958 } else {
2959 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
2960 }
Daniel Veillard20887ee2005-07-04 09:27:40 +00002961 } else if (!strcmp(command, "setrootns")) {
2962 xmlNodePtr root;
2963
2964 root = xmlDocGetRootElement(ctxt->doc);
2965 xmlShellRegisterRootNamespaces(ctxt, NULL, root, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002966 } else if (!strcmp(command, "xpath")) {
2967 if (arg[0] == 0) {
2968 xmlGenericError(xmlGenericErrorContext,
2969 "xpath: expression required\n");
2970 } else {
2971 ctxt->pctxt->node = ctxt->node;
2972 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002973 xmlXPathDebugDumpObject(ctxt->output, list, 0);
Daniel Veillard2070c482002-01-22 22:12:19 +00002974 xmlXPathFreeObject(list);
2975 }
2976#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard2156d432004-03-04 15:59:36 +00002977#ifdef LIBXML_TREE_ENABLED
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002978 } else if (!strcmp(command, "setbase")) {
2979 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2156d432004-03-04 15:59:36 +00002980#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002981 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
2982 int dir = (!strcmp(command, "dir"));
2983
2984 if (arg[0] == 0) {
2985 if (dir)
2986 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
2987 else
2988 xmlShellList(ctxt, NULL, ctxt->node, NULL);
2989 } else {
2990 ctxt->pctxt->node = ctxt->node;
Daniel Veillard61d80a22001-04-27 17:13:01 +00002991#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002992 ctxt->pctxt->node = ctxt->node;
2993 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2994#else
2995 list = NULL;
2996#endif /* LIBXML_XPATH_ENABLED */
2997 if (list != NULL) {
2998 switch (list->type) {
2999 case XPATH_UNDEFINED:
3000 xmlGenericError(xmlGenericErrorContext,
3001 "%s: no such node\n", arg);
3002 break;
3003 case XPATH_NODESET:{
3004 int indx;
3005
Daniel Veillarda6825e82001-11-07 13:33:59 +00003006 if (list->nodesetval == NULL)
3007 break;
3008
Daniel Veillard78d12092001-10-11 09:12:24 +00003009 for (indx = 0;
3010 indx < list->nodesetval->nodeNr;
3011 indx++) {
3012 if (dir)
3013 xmlShellDir(ctxt, NULL,
3014 list->nodesetval->
3015 nodeTab[indx], NULL);
3016 else
3017 xmlShellList(ctxt, NULL,
3018 list->nodesetval->
3019 nodeTab[indx], NULL);
3020 }
3021 break;
3022 }
3023 case XPATH_BOOLEAN:
3024 xmlGenericError(xmlGenericErrorContext,
3025 "%s is a Boolean\n", arg);
3026 break;
3027 case XPATH_NUMBER:
3028 xmlGenericError(xmlGenericErrorContext,
3029 "%s is a number\n", arg);
3030 break;
3031 case XPATH_STRING:
3032 xmlGenericError(xmlGenericErrorContext,
3033 "%s is a string\n", arg);
3034 break;
3035 case XPATH_POINT:
3036 xmlGenericError(xmlGenericErrorContext,
3037 "%s is a point\n", arg);
3038 break;
3039 case XPATH_RANGE:
3040 xmlGenericError(xmlGenericErrorContext,
3041 "%s is a range\n", arg);
3042 break;
3043 case XPATH_LOCATIONSET:
3044 xmlGenericError(xmlGenericErrorContext,
3045 "%s is a range\n", arg);
3046 break;
3047 case XPATH_USERS:
3048 xmlGenericError(xmlGenericErrorContext,
3049 "%s is user-defined\n", arg);
3050 break;
3051 case XPATH_XSLT_TREE:
3052 xmlGenericError(xmlGenericErrorContext,
3053 "%s is an XSLT value tree\n",
3054 arg);
3055 break;
3056 }
3057#ifdef LIBXML_XPATH_ENABLED
3058 xmlXPathFreeObject(list);
Daniel Veillard61d80a22001-04-27 17:13:01 +00003059#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00003060 } else {
3061 xmlGenericError(xmlGenericErrorContext,
3062 "%s: no such node\n", arg);
3063 }
3064 ctxt->pctxt->node = NULL;
3065 }
3066 } else if (!strcmp(command, "cd")) {
3067 if (arg[0] == 0) {
3068 ctxt->node = (xmlNodePtr) ctxt->doc;
3069 } else {
3070#ifdef LIBXML_XPATH_ENABLED
3071 ctxt->pctxt->node = ctxt->node;
3072 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3073#else
3074 list = NULL;
3075#endif /* LIBXML_XPATH_ENABLED */
3076 if (list != NULL) {
3077 switch (list->type) {
3078 case XPATH_UNDEFINED:
3079 xmlGenericError(xmlGenericErrorContext,
3080 "%s: no such node\n", arg);
3081 break;
3082 case XPATH_NODESET:
Daniel Veillarda6825e82001-11-07 13:33:59 +00003083 if (list->nodesetval != NULL) {
3084 if (list->nodesetval->nodeNr == 1) {
3085 ctxt->node = list->nodesetval->nodeTab[0];
Daniel Veillard7a985a12003-07-06 17:57:42 +00003086 if ((ctxt->node != NULL) &&
3087 (ctxt->node->type ==
3088 XML_NAMESPACE_DECL)) {
3089 xmlGenericError(xmlGenericErrorContext,
3090 "cannot cd to namespace\n");
3091 ctxt->node = NULL;
3092 }
Daniel Veillarda6825e82001-11-07 13:33:59 +00003093 } else
3094 xmlGenericError(xmlGenericErrorContext,
3095 "%s is a %d Node Set\n",
3096 arg,
3097 list->nodesetval->nodeNr);
Daniel Veillard78d12092001-10-11 09:12:24 +00003098 } else
3099 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda6825e82001-11-07 13:33:59 +00003100 "%s is an empty Node Set\n",
3101 arg);
Daniel Veillard78d12092001-10-11 09:12:24 +00003102 break;
3103 case XPATH_BOOLEAN:
3104 xmlGenericError(xmlGenericErrorContext,
3105 "%s is a Boolean\n", arg);
3106 break;
3107 case XPATH_NUMBER:
3108 xmlGenericError(xmlGenericErrorContext,
3109 "%s is a number\n", arg);
3110 break;
3111 case XPATH_STRING:
3112 xmlGenericError(xmlGenericErrorContext,
3113 "%s is a string\n", arg);
3114 break;
3115 case XPATH_POINT:
3116 xmlGenericError(xmlGenericErrorContext,
3117 "%s is a point\n", arg);
3118 break;
3119 case XPATH_RANGE:
3120 xmlGenericError(xmlGenericErrorContext,
3121 "%s is a range\n", arg);
3122 break;
3123 case XPATH_LOCATIONSET:
3124 xmlGenericError(xmlGenericErrorContext,
3125 "%s is a range\n", arg);
3126 break;
3127 case XPATH_USERS:
3128 xmlGenericError(xmlGenericErrorContext,
3129 "%s is user-defined\n", arg);
3130 break;
3131 case XPATH_XSLT_TREE:
3132 xmlGenericError(xmlGenericErrorContext,
3133 "%s is an XSLT value tree\n",
3134 arg);
3135 break;
3136 }
3137#ifdef LIBXML_XPATH_ENABLED
3138 xmlXPathFreeObject(list);
3139#endif
3140 } else {
3141 xmlGenericError(xmlGenericErrorContext,
3142 "%s: no such node\n", arg);
3143 }
3144 ctxt->pctxt->node = NULL;
3145 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003146#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00003147 } else if (!strcmp(command, "cat")) {
3148 if (arg[0] == 0) {
3149 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3150 } else {
3151 ctxt->pctxt->node = ctxt->node;
3152#ifdef LIBXML_XPATH_ENABLED
3153 ctxt->pctxt->node = ctxt->node;
3154 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3155#else
3156 list = NULL;
3157#endif /* LIBXML_XPATH_ENABLED */
3158 if (list != NULL) {
3159 switch (list->type) {
3160 case XPATH_UNDEFINED:
3161 xmlGenericError(xmlGenericErrorContext,
3162 "%s: no such node\n", arg);
3163 break;
3164 case XPATH_NODESET:{
3165 int indx;
3166
Daniel Veillarda6825e82001-11-07 13:33:59 +00003167 if (list->nodesetval == NULL)
3168 break;
3169
Daniel Veillard78d12092001-10-11 09:12:24 +00003170 for (indx = 0;
3171 indx < list->nodesetval->nodeNr;
3172 indx++) {
3173 if (i > 0)
Daniel Veillard321be0c2002-10-08 21:26:42 +00003174 fprintf(ctxt->output, " -------\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00003175 xmlShellCat(ctxt, NULL,
3176 list->nodesetval->
3177 nodeTab[indx], NULL);
3178 }
3179 break;
3180 }
3181 case XPATH_BOOLEAN:
3182 xmlGenericError(xmlGenericErrorContext,
3183 "%s is a Boolean\n", arg);
3184 break;
3185 case XPATH_NUMBER:
3186 xmlGenericError(xmlGenericErrorContext,
3187 "%s is a number\n", arg);
3188 break;
3189 case XPATH_STRING:
3190 xmlGenericError(xmlGenericErrorContext,
3191 "%s is a string\n", arg);
3192 break;
3193 case XPATH_POINT:
3194 xmlGenericError(xmlGenericErrorContext,
3195 "%s is a point\n", arg);
3196 break;
3197 case XPATH_RANGE:
3198 xmlGenericError(xmlGenericErrorContext,
3199 "%s is a range\n", arg);
3200 break;
3201 case XPATH_LOCATIONSET:
3202 xmlGenericError(xmlGenericErrorContext,
3203 "%s is a range\n", arg);
3204 break;
3205 case XPATH_USERS:
3206 xmlGenericError(xmlGenericErrorContext,
3207 "%s is user-defined\n", arg);
3208 break;
3209 case XPATH_XSLT_TREE:
3210 xmlGenericError(xmlGenericErrorContext,
3211 "%s is an XSLT value tree\n",
3212 arg);
3213 break;
3214 }
3215#ifdef LIBXML_XPATH_ENABLED
3216 xmlXPathFreeObject(list);
3217#endif
3218 } else {
3219 xmlGenericError(xmlGenericErrorContext,
3220 "%s: no such node\n", arg);
3221 }
3222 ctxt->pctxt->node = NULL;
3223 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003224#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00003225 } else {
3226 xmlGenericError(xmlGenericErrorContext,
3227 "Unknown command %s\n", command);
3228 }
3229 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003230 }
3231#ifdef LIBXML_XPATH_ENABLED
3232 xmlXPathFreeContext(ctxt->pctxt);
3233#endif /* LIBXML_XPATH_ENABLED */
3234 if (ctxt->loaded) {
3235 xmlFreeDoc(ctxt->doc);
3236 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003237 if (ctxt->filename != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003238 xmlFree(ctxt->filename);
Owen Taylor3473f882001-02-23 17:55:21 +00003239 xmlFree(ctxt);
3240 if (cmdline != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003241 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003242}
3243
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00003244#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00003245#define bottom_debugXML
3246#include "elfgcchack.h"
Owen Taylor3473f882001-02-23 17:55:21 +00003247#endif /* LIBXML_DEBUG_ENABLED */