blob: c98db0bcc77e7b893c11fb2bac95ed3ce2fb1875 [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 Veillardcfa303a2005-08-25 14:03:56 +000037#define DUMP_TEXT_TYPE 1
38
Daniel Veillard22cdb842004-10-04 14:09:17 +000039typedef struct _xmlDebugCtxt xmlDebugCtxt;
40typedef xmlDebugCtxt *xmlDebugCtxtPtr;
41struct _xmlDebugCtxt {
42 FILE *output; /* the output file */
43 char shift[101]; /* used for indenting */
44 int depth; /* current depth */
45 xmlDocPtr doc; /* current document */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000046 xmlNodePtr node; /* current node */
Jan Pokornýbb654fe2016-04-13 16:56:07 +020047 xmlDictPtr dict; /* the doc dictionary */
Daniel Veillard22cdb842004-10-04 14:09:17 +000048 int check; /* do just checkings */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000049 int errors; /* number of errors found */
Jan Pokornýbb654fe2016-04-13 16:56:07 +020050 int nodict; /* if the document has no dictionary */
Daniel Veillardcfa303a2005-08-25 14:03:56 +000051 int options; /* options */
Daniel Veillard22cdb842004-10-04 14:09:17 +000052};
53
54static void xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node);
55
56static void
57xmlCtxtDumpInitCtxt(xmlDebugCtxtPtr ctxt)
58{
59 int i;
60
61 ctxt->depth = 0;
62 ctxt->check = 0;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000063 ctxt->errors = 0;
Daniel Veillard22cdb842004-10-04 14:09:17 +000064 ctxt->output = stdout;
Daniel Veillard03a53c32004-10-26 16:06:51 +000065 ctxt->doc = NULL;
66 ctxt->node = NULL;
67 ctxt->dict = NULL;
Daniel Veillard6927b102004-10-27 17:29:04 +000068 ctxt->nodict = 0;
Daniel Veillardb6580ae2005-08-25 14:18:56 +000069 ctxt->options = 0;
Daniel Veillard22cdb842004-10-04 14:09:17 +000070 for (i = 0; i < 100; i++)
71 ctxt->shift[i] = ' ';
72 ctxt->shift[100] = 0;
73}
74
75static void
William M. Brack9638d4c2004-10-15 18:25:33 +000076xmlCtxtDumpCleanCtxt(xmlDebugCtxtPtr ctxt ATTRIBUTE_UNUSED)
Daniel Veillard76821142004-10-09 20:39:04 +000077{
William M. Brack9638d4c2004-10-15 18:25:33 +000078 /* remove the ATTRIBUTE_UNUSED when this is added */
Daniel Veillard76821142004-10-09 20:39:04 +000079}
80
81/**
Daniel Veillard0d24b112004-10-11 12:28:34 +000082 * xmlNsCheckScope:
83 * @node: the node
84 * @ns: the namespace node
Daniel Veillard76821142004-10-09 20:39:04 +000085 *
Daniel Veillard0d24b112004-10-11 12:28:34 +000086 * Check that a given namespace is in scope on a node.
Daniel Veillard76821142004-10-09 20:39:04 +000087 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +080088 * Returns 1 if in scope, -1 in case of argument error,
Daniel Veillard0d24b112004-10-11 12:28:34 +000089 * -2 if the namespace is not in scope, and -3 if not on
90 * an ancestor node.
Daniel Veillard76821142004-10-09 20:39:04 +000091 */
92static int
Daniel Veillard0d24b112004-10-11 12:28:34 +000093xmlNsCheckScope(xmlNodePtr node, xmlNsPtr ns)
Daniel Veillard76821142004-10-09 20:39:04 +000094{
Daniel Veillard0d24b112004-10-11 12:28:34 +000095 xmlNsPtr cur;
Daniel Veillard76821142004-10-09 20:39:04 +000096
Daniel Veillard0d24b112004-10-11 12:28:34 +000097 if ((node == NULL) || (ns == NULL))
98 return(-1);
99
100 if ((node->type != XML_ELEMENT_NODE) &&
101 (node->type != XML_ATTRIBUTE_NODE) &&
102 (node->type != XML_DOCUMENT_NODE) &&
103 (node->type != XML_TEXT_NODE) &&
104 (node->type != XML_HTML_DOCUMENT_NODE) &&
105 (node->type != XML_XINCLUDE_START))
106 return(-2);
107
108 while ((node != NULL) &&
109 ((node->type == XML_ELEMENT_NODE) ||
110 (node->type == XML_ATTRIBUTE_NODE) ||
111 (node->type == XML_TEXT_NODE) ||
112 (node->type == XML_XINCLUDE_START))) {
113 if ((node->type == XML_ELEMENT_NODE) ||
114 (node->type == XML_XINCLUDE_START)) {
115 cur = node->nsDef;
116 while (cur != NULL) {
117 if (cur == ns)
118 return(1);
119 if (xmlStrEqual(cur->prefix, ns->prefix))
120 return(-2);
121 cur = cur->next;
122 }
Daniel Veillard76821142004-10-09 20:39:04 +0000123 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000124 node = node->parent;
Daniel Veillard76821142004-10-09 20:39:04 +0000125 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000126 /* the xml namespace may be declared on the document node */
127 if ((node != NULL) &&
128 ((node->type == XML_DOCUMENT_NODE) ||
129 (node->type == XML_HTML_DOCUMENT_NODE))) {
130 xmlNsPtr oldNs = ((xmlDocPtr) node)->oldNs;
131 if (oldNs == ns)
132 return(1);
133 }
134 return(-3);
Daniel Veillard76821142004-10-09 20:39:04 +0000135}
Daniel Veillard76821142004-10-09 20:39:04 +0000136
Daniel Veillard76821142004-10-09 20:39:04 +0000137static void
Daniel Veillard22cdb842004-10-04 14:09:17 +0000138xmlCtxtDumpSpaces(xmlDebugCtxtPtr ctxt)
139{
140 if (ctxt->check)
141 return;
142 if ((ctxt->output != NULL) && (ctxt->depth > 0)) {
143 if (ctxt->depth < 50)
Daniel Veillardbccae2d2009-06-04 11:22:45 +0200144 fprintf(ctxt->output, "%s", &ctxt->shift[100 - 2 * ctxt->depth]);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000145 else
Daniel Veillardbccae2d2009-06-04 11:22:45 +0200146 fprintf(ctxt->output, "%s", ctxt->shift);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000147 }
148}
149
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000150/**
151 * xmlDebugErr:
152 * @ctxt: a debug context
153 * @error: the error code
154 *
155 * Handle a debug error.
156 */
157static void
158xmlDebugErr(xmlDebugCtxtPtr ctxt, int error, const char *msg)
159{
160 ctxt->errors++;
161 __xmlRaiseError(NULL, NULL, NULL,
162 NULL, ctxt->node, XML_FROM_CHECK,
163 error, XML_ERR_ERROR, NULL, 0,
164 NULL, NULL, NULL, 0, 0,
Daniel Veillardbccae2d2009-06-04 11:22:45 +0200165 "%s", msg);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000166}
David Kilzer4472c3a2016-05-13 15:13:17 +0800167static void LIBXML_ATTR_FORMAT(3,0)
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000168xmlDebugErr2(xmlDebugCtxtPtr ctxt, int error, const char *msg, int extra)
169{
170 ctxt->errors++;
171 __xmlRaiseError(NULL, NULL, NULL,
172 NULL, ctxt->node, XML_FROM_CHECK,
173 error, XML_ERR_ERROR, NULL, 0,
174 NULL, NULL, NULL, 0, 0,
175 msg, extra);
176}
David Kilzer4472c3a2016-05-13 15:13:17 +0800177static void LIBXML_ATTR_FORMAT(3,0)
Daniel Veillardc6095782004-10-15 14:50:10 +0000178xmlDebugErr3(xmlDebugCtxtPtr ctxt, int error, const char *msg, const char *extra)
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000179{
180 ctxt->errors++;
181 __xmlRaiseError(NULL, NULL, NULL,
182 NULL, ctxt->node, XML_FROM_CHECK,
183 error, XML_ERR_ERROR, NULL, 0,
184 NULL, NULL, NULL, 0, 0,
185 msg, extra);
186}
187
Daniel Veillard0d24b112004-10-11 12:28:34 +0000188/**
189 * xmlCtxtNsCheckScope:
190 * @ctxt: the debugging context
191 * @node: the node
192 * @ns: the namespace node
193 *
194 * Report if a given namespace is is not in scope.
195 */
196static void
197xmlCtxtNsCheckScope(xmlDebugCtxtPtr ctxt, xmlNodePtr node, xmlNsPtr ns)
198{
199 int ret;
200
201 ret = xmlNsCheckScope(node, ns);
202 if (ret == -2) {
203 if (ns->prefix == NULL)
204 xmlDebugErr(ctxt, XML_CHECK_NS_SCOPE,
205 "Reference to default namespace not in scope\n");
206 else
207 xmlDebugErr3(ctxt, XML_CHECK_NS_SCOPE,
208 "Reference to namespace '%s' not in scope\n",
209 (char *) ns->prefix);
210 }
211 if (ret == -3) {
212 if (ns->prefix == NULL)
213 xmlDebugErr(ctxt, XML_CHECK_NS_ANCESTOR,
214 "Reference to default namespace not on ancestor\n");
215 else
216 xmlDebugErr3(ctxt, XML_CHECK_NS_ANCESTOR,
217 "Reference to namespace '%s' not on ancestor\n",
218 (char *) ns->prefix);
219 }
220}
221
Daniel Veillardc6095782004-10-15 14:50:10 +0000222/**
223 * xmlCtxtCheckString:
224 * @ctxt: the debug context
225 * @str: the string
226 *
227 * Do debugging on the string, currently it just checks the UTF-8 content
228 */
229static void
230xmlCtxtCheckString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
231{
232 if (str == NULL) return;
233 if (ctxt->check) {
234 if (!xmlCheckUTF8(str)) {
Daniel Veillard03a53c32004-10-26 16:06:51 +0000235 xmlDebugErr3(ctxt, XML_CHECK_NOT_UTF8,
Daniel Veillardc6095782004-10-15 14:50:10 +0000236 "String is not UTF-8 %s", (const char *) str);
237 }
238 }
239}
240
Daniel Veillard03a53c32004-10-26 16:06:51 +0000241/**
242 * xmlCtxtCheckName:
243 * @ctxt: the debug context
244 * @name: the name
245 *
Jan Pokornýbb654fe2016-04-13 16:56:07 +0200246 * Do debugging on the name, for example the dictionary status and
Daniel Veillard03a53c32004-10-26 16:06:51 +0000247 * conformance to the Name production.
248 */
249static void
250xmlCtxtCheckName(xmlDebugCtxtPtr ctxt, const xmlChar * name)
251{
252 if (ctxt->check) {
253 if (name == NULL) {
254 xmlDebugErr(ctxt, XML_CHECK_NO_NAME, "Name is NULL");
255 return;
256 }
Nicolas Le Cam3313d142013-06-14 21:11:27 +0200257#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Daniel Veillard03a53c32004-10-26 16:06:51 +0000258 if (xmlValidateName(name, 0)) {
259 xmlDebugErr3(ctxt, XML_CHECK_NOT_NCNAME,
260 "Name is not an NCName '%s'", (const char *) name);
261 }
Nicolas Le Cam3313d142013-06-14 21:11:27 +0200262#endif
Daniel Veillard03a53c32004-10-26 16:06:51 +0000263 if ((ctxt->dict != NULL) &&
Daniel Veillard023d0ba2009-07-29 11:34:50 +0200264 (!xmlDictOwns(ctxt->dict, name)) &&
265 ((ctxt->doc == NULL) ||
266 ((ctxt->doc->parseFlags & (XML_PARSE_SAX1 | XML_PARSE_NODICT)) == 0))) {
Daniel Veillard03a53c32004-10-26 16:06:51 +0000267 xmlDebugErr3(ctxt, XML_CHECK_OUTSIDE_DICT,
Jan Pokornýbb654fe2016-04-13 16:56:07 +0200268 "Name is not from the document dictionary '%s'",
Daniel Veillard03a53c32004-10-26 16:06:51 +0000269 (const char *) name);
270 }
271 }
272}
273
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000274static void
275xmlCtxtGenericNodeCheck(xmlDebugCtxtPtr ctxt, xmlNodePtr node) {
Daniel Veillard03a53c32004-10-26 16:06:51 +0000276 xmlDocPtr doc;
277 xmlDictPtr dict;
278
279 doc = node->doc;
280
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000281 if (node->parent == NULL)
282 xmlDebugErr(ctxt, XML_CHECK_NO_PARENT,
283 "Node has no parent\n");
Daniel Veillard03a53c32004-10-26 16:06:51 +0000284 if (node->doc == NULL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000285 xmlDebugErr(ctxt, XML_CHECK_NO_DOC,
286 "Node has no doc\n");
Daniel Veillard03a53c32004-10-26 16:06:51 +0000287 dict = NULL;
288 } else {
289 dict = doc->dict;
290 if ((dict == NULL) && (ctxt->nodict == 0)) {
Daniel Veillard6927b102004-10-27 17:29:04 +0000291#if 0
292 /* desactivated right now as it raises too many errors */
293 if (doc->type == XML_DOCUMENT_NODE)
294 xmlDebugErr(ctxt, XML_CHECK_NO_DICT,
Jan Pokornýbb654fe2016-04-13 16:56:07 +0200295 "Document has no dictionary\n");
Daniel Veillard6927b102004-10-27 17:29:04 +0000296#endif
Daniel Veillard03a53c32004-10-26 16:06:51 +0000297 ctxt->nodict = 1;
298 }
299 if (ctxt->doc == NULL)
300 ctxt->doc = doc;
301
302 if (ctxt->dict == NULL) {
303 ctxt->dict = dict;
304 }
305 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000306 if ((node->parent != NULL) && (node->doc != node->parent->doc) &&
307 (!xmlStrEqual(node->name, BAD_CAST "pseudoroot")))
308 xmlDebugErr(ctxt, XML_CHECK_WRONG_DOC,
309 "Node doc differs from parent's one\n");
310 if (node->prev == NULL) {
311 if (node->type == XML_ATTRIBUTE_NODE) {
312 if ((node->parent != NULL) &&
313 (node != (xmlNodePtr) node->parent->properties))
314 xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
315 "Attr has no prev and not first of attr list\n");
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800316
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000317 } else if ((node->parent != NULL) && (node->parent->children != node))
318 xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
319 "Node has no prev and not first of parent list\n");
320 } else {
321 if (node->prev->next != node)
322 xmlDebugErr(ctxt, XML_CHECK_WRONG_PREV,
323 "Node prev->next : back link wrong\n");
324 }
325 if (node->next == NULL) {
326 if ((node->parent != NULL) && (node->type != XML_ATTRIBUTE_NODE) &&
Daniel Veillard97c9ce22008-03-25 16:52:41 +0000327 (node->parent->last != node) &&
328 (node->parent->type == XML_ELEMENT_NODE))
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000329 xmlDebugErr(ctxt, XML_CHECK_NO_NEXT,
330 "Node has no next and not last of parent list\n");
331 } else {
332 if (node->next->prev != node)
333 xmlDebugErr(ctxt, XML_CHECK_WRONG_NEXT,
334 "Node next->prev : forward link wrong\n");
Daniel Veillard0d24b112004-10-11 12:28:34 +0000335 if (node->next->parent != node->parent)
336 xmlDebugErr(ctxt, XML_CHECK_WRONG_PARENT,
337 "Node next->prev : forward link wrong\n");
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000338 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000339 if (node->type == XML_ELEMENT_NODE) {
340 xmlNsPtr ns;
341
342 ns = node->nsDef;
343 while (ns != NULL) {
344 xmlCtxtNsCheckScope(ctxt, node, ns);
345 ns = ns->next;
346 }
347 if (node->ns != NULL)
348 xmlCtxtNsCheckScope(ctxt, node, node->ns);
349 } else if (node->type == XML_ATTRIBUTE_NODE) {
350 if (node->ns != NULL)
351 xmlCtxtNsCheckScope(ctxt, node, node->ns);
352 }
353
Daniel Veillardc6095782004-10-15 14:50:10 +0000354 if ((node->type != XML_ELEMENT_NODE) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000355 (node->type != XML_ATTRIBUTE_NODE) &&
Daniel Veillardf2e066a2005-06-30 13:04:44 +0000356 (node->type != XML_ELEMENT_DECL) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000357 (node->type != XML_ATTRIBUTE_DECL) &&
358 (node->type != XML_DTD_NODE) &&
359 (node->type != XML_HTML_DOCUMENT_NODE) &&
360 (node->type != XML_DOCUMENT_NODE)) {
Daniel Veillardc6095782004-10-15 14:50:10 +0000361 if (node->content != NULL)
William M. Brack9638d4c2004-10-15 18:25:33 +0000362 xmlCtxtCheckString(ctxt, (const xmlChar *) node->content);
Daniel Veillardc6095782004-10-15 14:50:10 +0000363 }
Daniel Veillard03a53c32004-10-26 16:06:51 +0000364 switch (node->type) {
365 case XML_ELEMENT_NODE:
366 case XML_ATTRIBUTE_NODE:
367 xmlCtxtCheckName(ctxt, node->name);
368 break;
369 case XML_TEXT_NODE:
370 if ((node->name == xmlStringText) ||
371 (node->name == xmlStringTextNoenc))
372 break;
373 /* some case of entity substitution can lead to this */
374 if ((ctxt->dict != NULL) &&
Daniel Veillard6927b102004-10-27 17:29:04 +0000375 (node->name == xmlDictLookup(ctxt->dict, BAD_CAST "nbktext",
376 7)))
Daniel Veillard03a53c32004-10-26 16:06:51 +0000377 break;
378
379 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
380 "Text node has wrong name '%s'",
381 (const char *) node->name);
382 break;
383 case XML_COMMENT_NODE:
384 if (node->name == xmlStringComment)
385 break;
386 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
387 "Comment node has wrong name '%s'",
388 (const char *) node->name);
389 break;
390 case XML_PI_NODE:
391 xmlCtxtCheckName(ctxt, node->name);
392 break;
393 case XML_CDATA_SECTION_NODE:
394 if (node->name == NULL)
395 break;
396 xmlDebugErr3(ctxt, XML_CHECK_NAME_NOT_NULL,
397 "CData section has non NULL name '%s'",
398 (const char *) node->name);
399 break;
400 case XML_ENTITY_REF_NODE:
401 case XML_ENTITY_NODE:
402 case XML_DOCUMENT_TYPE_NODE:
403 case XML_DOCUMENT_FRAG_NODE:
404 case XML_NOTATION_NODE:
405 case XML_DTD_NODE:
406 case XML_ELEMENT_DECL:
407 case XML_ATTRIBUTE_DECL:
408 case XML_ENTITY_DECL:
409 case XML_NAMESPACE_DECL:
410 case XML_XINCLUDE_START:
411 case XML_XINCLUDE_END:
412#ifdef LIBXML_DOCB_ENABLED
413 case XML_DOCB_DOCUMENT_NODE:
414#endif
415 case XML_DOCUMENT_NODE:
416 case XML_HTML_DOCUMENT_NODE:
417 break;
418 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000419}
420
Daniel Veillard22cdb842004-10-04 14:09:17 +0000421static void
422xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
423{
424 int i;
425
Daniel Veillardc6095782004-10-15 14:50:10 +0000426 if (ctxt->check) {
Daniel Veillard22cdb842004-10-04 14:09:17 +0000427 return;
Daniel Veillardc6095782004-10-15 14:50:10 +0000428 }
Daniel Veillard22cdb842004-10-04 14:09:17 +0000429 /* TODO: check UTF8 content of the string */
430 if (str == NULL) {
431 fprintf(ctxt->output, "(NULL)");
432 return;
433 }
434 for (i = 0; i < 40; i++)
435 if (str[i] == 0)
436 return;
437 else if (IS_BLANK_CH(str[i]))
438 fputc(' ', ctxt->output);
439 else if (str[i] >= 0x80)
440 fprintf(ctxt->output, "#%X", str[i]);
441 else
442 fputc(str[i], ctxt->output);
443 fprintf(ctxt->output, "...");
444}
445
446static void
447xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
448{
449 xmlCtxtDumpSpaces(ctxt);
450
451 if (dtd == NULL) {
452 if (!ctxt->check)
453 fprintf(ctxt->output, "DTD node is NULL\n");
454 return;
455 }
456
457 if (dtd->type != XML_DTD_NODE) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000458 xmlDebugErr(ctxt, XML_CHECK_NOT_DTD,
459 "Node is not a DTD");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000460 return;
461 }
462 if (!ctxt->check) {
463 if (dtd->name != NULL)
464 fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name);
465 else
466 fprintf(ctxt->output, "DTD");
467 if (dtd->ExternalID != NULL)
468 fprintf(ctxt->output, ", PUBLIC %s", (char *) dtd->ExternalID);
469 if (dtd->SystemID != NULL)
470 fprintf(ctxt->output, ", SYSTEM %s", (char *) dtd->SystemID);
471 fprintf(ctxt->output, "\n");
472 }
473 /*
474 * Do a bit of checking
475 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000476 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000477}
478
479static void
480xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr)
481{
482 xmlCtxtDumpSpaces(ctxt);
483
484 if (attr == NULL) {
485 if (!ctxt->check)
486 fprintf(ctxt->output, "Attribute declaration is NULL\n");
487 return;
488 }
489 if (attr->type != XML_ATTRIBUTE_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000490 xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL,
491 "Node is not an attribute declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000492 return;
493 }
494 if (attr->name != NULL) {
495 if (!ctxt->check)
496 fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name);
497 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000498 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
499 "Node attribute declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000500 if (attr->elem != NULL) {
501 if (!ctxt->check)
502 fprintf(ctxt->output, " for %s", (char *) attr->elem);
503 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000504 xmlDebugErr(ctxt, XML_CHECK_NO_ELEM,
505 "Node attribute declaration has no element name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000506 if (!ctxt->check) {
507 switch (attr->atype) {
508 case XML_ATTRIBUTE_CDATA:
509 fprintf(ctxt->output, " CDATA");
510 break;
511 case XML_ATTRIBUTE_ID:
512 fprintf(ctxt->output, " ID");
513 break;
514 case XML_ATTRIBUTE_IDREF:
515 fprintf(ctxt->output, " IDREF");
516 break;
517 case XML_ATTRIBUTE_IDREFS:
518 fprintf(ctxt->output, " IDREFS");
519 break;
520 case XML_ATTRIBUTE_ENTITY:
521 fprintf(ctxt->output, " ENTITY");
522 break;
523 case XML_ATTRIBUTE_ENTITIES:
524 fprintf(ctxt->output, " ENTITIES");
525 break;
526 case XML_ATTRIBUTE_NMTOKEN:
527 fprintf(ctxt->output, " NMTOKEN");
528 break;
529 case XML_ATTRIBUTE_NMTOKENS:
530 fprintf(ctxt->output, " NMTOKENS");
531 break;
532 case XML_ATTRIBUTE_ENUMERATION:
533 fprintf(ctxt->output, " ENUMERATION");
534 break;
535 case XML_ATTRIBUTE_NOTATION:
536 fprintf(ctxt->output, " NOTATION ");
537 break;
538 }
539 if (attr->tree != NULL) {
540 int indx;
541 xmlEnumerationPtr cur = attr->tree;
542
543 for (indx = 0; indx < 5; indx++) {
544 if (indx != 0)
545 fprintf(ctxt->output, "|%s", (char *) cur->name);
546 else
547 fprintf(ctxt->output, " (%s", (char *) cur->name);
548 cur = cur->next;
549 if (cur == NULL)
550 break;
551 }
552 if (cur == NULL)
553 fprintf(ctxt->output, ")");
554 else
555 fprintf(ctxt->output, "...)");
556 }
557 switch (attr->def) {
558 case XML_ATTRIBUTE_NONE:
559 break;
560 case XML_ATTRIBUTE_REQUIRED:
561 fprintf(ctxt->output, " REQUIRED");
562 break;
563 case XML_ATTRIBUTE_IMPLIED:
564 fprintf(ctxt->output, " IMPLIED");
565 break;
566 case XML_ATTRIBUTE_FIXED:
567 fprintf(ctxt->output, " FIXED");
568 break;
569 }
570 if (attr->defaultValue != NULL) {
571 fprintf(ctxt->output, "\"");
572 xmlCtxtDumpString(ctxt, attr->defaultValue);
573 fprintf(ctxt->output, "\"");
574 }
575 fprintf(ctxt->output, "\n");
576 }
577
578 /*
579 * Do a bit of checking
580 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000581 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000582}
583
584static void
585xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem)
586{
587 xmlCtxtDumpSpaces(ctxt);
588
589 if (elem == NULL) {
590 if (!ctxt->check)
591 fprintf(ctxt->output, "Element declaration is NULL\n");
592 return;
593 }
594 if (elem->type != XML_ELEMENT_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000595 xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL,
596 "Node is not an element declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000597 return;
598 }
599 if (elem->name != NULL) {
600 if (!ctxt->check) {
601 fprintf(ctxt->output, "ELEMDECL(");
602 xmlCtxtDumpString(ctxt, elem->name);
603 fprintf(ctxt->output, ")");
604 }
605 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000606 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
607 "Element declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000608 if (!ctxt->check) {
609 switch (elem->etype) {
610 case XML_ELEMENT_TYPE_UNDEFINED:
611 fprintf(ctxt->output, ", UNDEFINED");
612 break;
613 case XML_ELEMENT_TYPE_EMPTY:
614 fprintf(ctxt->output, ", EMPTY");
615 break;
616 case XML_ELEMENT_TYPE_ANY:
617 fprintf(ctxt->output, ", ANY");
618 break;
619 case XML_ELEMENT_TYPE_MIXED:
620 fprintf(ctxt->output, ", MIXED ");
621 break;
622 case XML_ELEMENT_TYPE_ELEMENT:
623 fprintf(ctxt->output, ", MIXED ");
624 break;
625 }
626 if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) {
627 char buf[5001];
628
629 buf[0] = 0;
630 xmlSnprintfElementContent(buf, 5000, elem->content, 1);
631 buf[5000] = 0;
632 fprintf(ctxt->output, "%s", buf);
633 }
634 fprintf(ctxt->output, "\n");
635 }
636
637 /*
638 * Do a bit of checking
639 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000640 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000641}
642
643static void
644xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
645{
646 xmlCtxtDumpSpaces(ctxt);
647
648 if (ent == NULL) {
649 if (!ctxt->check)
650 fprintf(ctxt->output, "Entity declaration is NULL\n");
651 return;
652 }
653 if (ent->type != XML_ENTITY_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000654 xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL,
655 "Node is not an entity declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000656 return;
657 }
658 if (ent->name != NULL) {
659 if (!ctxt->check) {
660 fprintf(ctxt->output, "ENTITYDECL(");
661 xmlCtxtDumpString(ctxt, ent->name);
662 fprintf(ctxt->output, ")");
663 }
664 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000665 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
666 "Entity declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000667 if (!ctxt->check) {
668 switch (ent->etype) {
669 case XML_INTERNAL_GENERAL_ENTITY:
670 fprintf(ctxt->output, ", internal\n");
671 break;
672 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
673 fprintf(ctxt->output, ", external parsed\n");
674 break;
675 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
676 fprintf(ctxt->output, ", unparsed\n");
677 break;
678 case XML_INTERNAL_PARAMETER_ENTITY:
679 fprintf(ctxt->output, ", parameter\n");
680 break;
681 case XML_EXTERNAL_PARAMETER_ENTITY:
682 fprintf(ctxt->output, ", external parameter\n");
683 break;
684 case XML_INTERNAL_PREDEFINED_ENTITY:
685 fprintf(ctxt->output, ", predefined\n");
686 break;
687 }
688 if (ent->ExternalID) {
689 xmlCtxtDumpSpaces(ctxt);
690 fprintf(ctxt->output, " ExternalID=%s\n",
691 (char *) ent->ExternalID);
692 }
693 if (ent->SystemID) {
694 xmlCtxtDumpSpaces(ctxt);
695 fprintf(ctxt->output, " SystemID=%s\n",
696 (char *) ent->SystemID);
697 }
698 if (ent->URI != NULL) {
699 xmlCtxtDumpSpaces(ctxt);
700 fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI);
701 }
702 if (ent->content) {
703 xmlCtxtDumpSpaces(ctxt);
704 fprintf(ctxt->output, " content=");
705 xmlCtxtDumpString(ctxt, ent->content);
706 fprintf(ctxt->output, "\n");
707 }
708 }
709
710 /*
711 * Do a bit of checking
712 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000713 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000714}
715
716static void
717xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
718{
719 xmlCtxtDumpSpaces(ctxt);
720
721 if (ns == NULL) {
722 if (!ctxt->check)
723 fprintf(ctxt->output, "namespace node is NULL\n");
724 return;
725 }
726 if (ns->type != XML_NAMESPACE_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000727 xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL,
728 "Node is not a namespace declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000729 return;
730 }
731 if (ns->href == NULL) {
732 if (ns->prefix != NULL)
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000733 xmlDebugErr3(ctxt, XML_CHECK_NO_HREF,
734 "Incomplete namespace %s href=NULL\n",
Daniel Veillard22cdb842004-10-04 14:09:17 +0000735 (char *) ns->prefix);
736 else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000737 xmlDebugErr(ctxt, XML_CHECK_NO_HREF,
738 "Incomplete default namespace href=NULL\n");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000739 } else {
740 if (!ctxt->check) {
741 if (ns->prefix != NULL)
742 fprintf(ctxt->output, "namespace %s href=",
743 (char *) ns->prefix);
744 else
745 fprintf(ctxt->output, "default namespace href=");
746
747 xmlCtxtDumpString(ctxt, ns->href);
748 fprintf(ctxt->output, "\n");
749 }
750 }
751}
752
753static void
754xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
755{
756 while (ns != NULL) {
757 xmlCtxtDumpNamespace(ctxt, ns);
758 ns = ns->next;
759 }
760}
761
762static void
763xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
764{
765 xmlCtxtDumpSpaces(ctxt);
766
767 if (ent == NULL) {
768 if (!ctxt->check)
769 fprintf(ctxt->output, "Entity is NULL\n");
770 return;
771 }
772 if (!ctxt->check) {
773 switch (ent->etype) {
774 case XML_INTERNAL_GENERAL_ENTITY:
775 fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY ");
776 break;
777 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
778 fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
779 break;
780 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
781 fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
782 break;
783 case XML_INTERNAL_PARAMETER_ENTITY:
784 fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY ");
785 break;
786 case XML_EXTERNAL_PARAMETER_ENTITY:
787 fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY ");
788 break;
789 default:
790 fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype);
791 }
792 fprintf(ctxt->output, "%s\n", ent->name);
793 if (ent->ExternalID) {
794 xmlCtxtDumpSpaces(ctxt);
795 fprintf(ctxt->output, "ExternalID=%s\n",
796 (char *) ent->ExternalID);
797 }
798 if (ent->SystemID) {
799 xmlCtxtDumpSpaces(ctxt);
800 fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID);
801 }
802 if (ent->URI) {
803 xmlCtxtDumpSpaces(ctxt);
804 fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI);
805 }
806 if (ent->content) {
807 xmlCtxtDumpSpaces(ctxt);
808 fprintf(ctxt->output, "content=");
809 xmlCtxtDumpString(ctxt, ent->content);
810 fprintf(ctxt->output, "\n");
811 }
812 }
813}
814
815/**
816 * xmlCtxtDumpAttr:
817 * @output: the FILE * for the output
818 * @attr: the attribute
819 * @depth: the indentation level.
820 *
821 * Dumps debug information for the attribute
822 */
823static void
824xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
825{
826 xmlCtxtDumpSpaces(ctxt);
827
828 if (attr == NULL) {
829 if (!ctxt->check)
830 fprintf(ctxt->output, "Attr is NULL");
831 return;
832 }
833 if (!ctxt->check) {
834 fprintf(ctxt->output, "ATTRIBUTE ");
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000835 xmlCtxtDumpString(ctxt, attr->name);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000836 fprintf(ctxt->output, "\n");
837 if (attr->children != NULL) {
838 ctxt->depth++;
839 xmlCtxtDumpNodeList(ctxt, attr->children);
840 ctxt->depth--;
841 }
842 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000843 if (attr->name == NULL)
844 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
845 "Attribute has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000846
847 /*
848 * Do a bit of checking
849 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000850 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000851}
852
853/**
854 * xmlCtxtDumpAttrList:
855 * @output: the FILE * for the output
856 * @attr: the attribute list
857 * @depth: the indentation level.
858 *
859 * Dumps debug information for the attribute list
860 */
861static void
862xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
863{
864 while (attr != NULL) {
865 xmlCtxtDumpAttr(ctxt, attr);
866 attr = attr->next;
867 }
868}
869
870/**
871 * xmlCtxtDumpOneNode:
872 * @output: the FILE * for the output
873 * @node: the node
874 * @depth: the indentation level.
875 *
876 * Dumps debug information for the element node, it is not recursive
877 */
878static void
879xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
880{
881 if (node == NULL) {
882 if (!ctxt->check) {
883 xmlCtxtDumpSpaces(ctxt);
884 fprintf(ctxt->output, "node is NULL\n");
885 }
886 return;
887 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000888 ctxt->node = node;
889
Daniel Veillard22cdb842004-10-04 14:09:17 +0000890 switch (node->type) {
891 case XML_ELEMENT_NODE:
892 if (!ctxt->check) {
893 xmlCtxtDumpSpaces(ctxt);
894 fprintf(ctxt->output, "ELEMENT ");
895 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
896 xmlCtxtDumpString(ctxt, node->ns->prefix);
897 fprintf(ctxt->output, ":");
898 }
899 xmlCtxtDumpString(ctxt, node->name);
900 fprintf(ctxt->output, "\n");
901 }
902 break;
903 case XML_ATTRIBUTE_NODE:
904 if (!ctxt->check)
905 xmlCtxtDumpSpaces(ctxt);
906 fprintf(ctxt->output, "Error, ATTRIBUTE found here\n");
William M. Brack5a9c1fd2004-12-17 21:38:09 +0000907 xmlCtxtGenericNodeCheck(ctxt, node);
908 return;
Daniel Veillard22cdb842004-10-04 14:09:17 +0000909 case XML_TEXT_NODE:
910 if (!ctxt->check) {
911 xmlCtxtDumpSpaces(ctxt);
912 if (node->name == (const xmlChar *) xmlStringTextNoenc)
Daniel Veillard8874b942005-08-25 13:19:21 +0000913 fprintf(ctxt->output, "TEXT no enc");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000914 else
Daniel Veillard8874b942005-08-25 13:19:21 +0000915 fprintf(ctxt->output, "TEXT");
Daniel Veillardcfa303a2005-08-25 14:03:56 +0000916 if (ctxt->options & DUMP_TEXT_TYPE) {
917 if (node->content == (xmlChar *) &(node->properties))
918 fprintf(ctxt->output, " compact\n");
919 else if (xmlDictOwns(ctxt->dict, node->content) == 1)
920 fprintf(ctxt->output, " interned\n");
921 else
922 fprintf(ctxt->output, "\n");
923 } else
Daniel Veillard8874b942005-08-25 13:19:21 +0000924 fprintf(ctxt->output, "\n");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000925 }
926 break;
927 case XML_CDATA_SECTION_NODE:
928 if (!ctxt->check) {
929 xmlCtxtDumpSpaces(ctxt);
930 fprintf(ctxt->output, "CDATA_SECTION\n");
931 }
932 break;
933 case XML_ENTITY_REF_NODE:
934 if (!ctxt->check) {
935 xmlCtxtDumpSpaces(ctxt);
936 fprintf(ctxt->output, "ENTITY_REF(%s)\n",
937 (char *) node->name);
938 }
939 break;
940 case XML_ENTITY_NODE:
941 if (!ctxt->check) {
942 xmlCtxtDumpSpaces(ctxt);
943 fprintf(ctxt->output, "ENTITY\n");
944 }
945 break;
946 case XML_PI_NODE:
947 if (!ctxt->check) {
948 xmlCtxtDumpSpaces(ctxt);
949 fprintf(ctxt->output, "PI %s\n", (char *) node->name);
950 }
951 break;
952 case XML_COMMENT_NODE:
953 if (!ctxt->check) {
954 xmlCtxtDumpSpaces(ctxt);
955 fprintf(ctxt->output, "COMMENT\n");
956 }
957 break;
958 case XML_DOCUMENT_NODE:
959 case XML_HTML_DOCUMENT_NODE:
960 if (!ctxt->check) {
961 xmlCtxtDumpSpaces(ctxt);
962 }
William M. Brack5a9c1fd2004-12-17 21:38:09 +0000963 fprintf(ctxt->output, "Error, DOCUMENT found here\n");
964 xmlCtxtGenericNodeCheck(ctxt, node);
965 return;
Daniel Veillard22cdb842004-10-04 14:09:17 +0000966 case XML_DOCUMENT_TYPE_NODE:
967 if (!ctxt->check) {
968 xmlCtxtDumpSpaces(ctxt);
969 fprintf(ctxt->output, "DOCUMENT_TYPE\n");
970 }
971 break;
972 case XML_DOCUMENT_FRAG_NODE:
973 if (!ctxt->check) {
974 xmlCtxtDumpSpaces(ctxt);
975 fprintf(ctxt->output, "DOCUMENT_FRAG\n");
976 }
977 break;
978 case XML_NOTATION_NODE:
979 if (!ctxt->check) {
980 xmlCtxtDumpSpaces(ctxt);
981 fprintf(ctxt->output, "NOTATION\n");
982 }
983 break;
984 case XML_DTD_NODE:
985 xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
986 return;
987 case XML_ELEMENT_DECL:
988 xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
989 return;
990 case XML_ATTRIBUTE_DECL:
991 xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
992 return;
993 case XML_ENTITY_DECL:
994 xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
995 return;
996 case XML_NAMESPACE_DECL:
997 xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
998 return;
999 case XML_XINCLUDE_START:
1000 if (!ctxt->check) {
1001 xmlCtxtDumpSpaces(ctxt);
1002 fprintf(ctxt->output, "INCLUDE START\n");
1003 }
1004 return;
1005 case XML_XINCLUDE_END:
1006 if (!ctxt->check) {
1007 xmlCtxtDumpSpaces(ctxt);
1008 fprintf(ctxt->output, "INCLUDE END\n");
1009 }
1010 return;
1011 default:
1012 if (!ctxt->check)
1013 xmlCtxtDumpSpaces(ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001014 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1015 "Unknown node type %d\n", node->type);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001016 return;
1017 }
1018 if (node->doc == NULL) {
1019 if (!ctxt->check) {
1020 xmlCtxtDumpSpaces(ctxt);
1021 }
1022 fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
1023 }
1024 ctxt->depth++;
Daniel Veillard8874b942005-08-25 13:19:21 +00001025 if ((node->type == XML_ELEMENT_NODE) && (node->nsDef != NULL))
Daniel Veillard22cdb842004-10-04 14:09:17 +00001026 xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
Daniel Veillard8874b942005-08-25 13:19:21 +00001027 if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL))
Daniel Veillard22cdb842004-10-04 14:09:17 +00001028 xmlCtxtDumpAttrList(ctxt, node->properties);
1029 if (node->type != XML_ENTITY_REF_NODE) {
1030 if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
1031 if (!ctxt->check) {
1032 xmlCtxtDumpSpaces(ctxt);
1033 fprintf(ctxt->output, "content=");
1034 xmlCtxtDumpString(ctxt, node->content);
1035 fprintf(ctxt->output, "\n");
1036 }
1037 }
1038 } else {
1039 xmlEntityPtr ent;
1040
1041 ent = xmlGetDocEntity(node->doc, node->name);
1042 if (ent != NULL)
1043 xmlCtxtDumpEntity(ctxt, ent);
1044 }
1045 ctxt->depth--;
1046
1047 /*
1048 * Do a bit of checking
1049 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001050 xmlCtxtGenericNodeCheck(ctxt, node);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001051}
1052
1053/**
1054 * xmlCtxtDumpNode:
1055 * @output: the FILE * for the output
1056 * @node: the node
1057 * @depth: the indentation level.
1058 *
1059 * Dumps debug information for the element node, it is recursive
1060 */
1061static void
1062xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1063{
1064 if (node == NULL) {
1065 if (!ctxt->check) {
1066 xmlCtxtDumpSpaces(ctxt);
1067 fprintf(ctxt->output, "node is NULL\n");
1068 }
1069 return;
1070 }
1071 xmlCtxtDumpOneNode(ctxt, node);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001072 if ((node->type != XML_NAMESPACE_DECL) &&
Daniel Veillard7837dd82005-09-06 22:16:57 +00001073 (node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
Daniel Veillard22cdb842004-10-04 14:09:17 +00001074 ctxt->depth++;
1075 xmlCtxtDumpNodeList(ctxt, node->children);
1076 ctxt->depth--;
1077 }
1078}
1079
1080/**
1081 * xmlCtxtDumpNodeList:
1082 * @output: the FILE * for the output
1083 * @node: the node list
1084 * @depth: the indentation level.
1085 *
1086 * Dumps debug information for the list of element node, it is recursive
1087 */
1088static void
1089xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1090{
1091 while (node != NULL) {
1092 xmlCtxtDumpNode(ctxt, node);
1093 node = node->next;
1094 }
1095}
1096
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001097static void
1098xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1099{
1100 if (doc == NULL) {
1101 if (!ctxt->check)
1102 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1103 return;
1104 }
1105 ctxt->node = (xmlNodePtr) doc;
1106
1107 switch (doc->type) {
1108 case XML_ELEMENT_NODE:
1109 xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
1110 "Misplaced ELEMENT node\n");
1111 break;
1112 case XML_ATTRIBUTE_NODE:
1113 xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
1114 "Misplaced ATTRIBUTE node\n");
1115 break;
1116 case XML_TEXT_NODE:
1117 xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
1118 "Misplaced TEXT node\n");
1119 break;
1120 case XML_CDATA_SECTION_NODE:
1121 xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
1122 "Misplaced CDATA node\n");
1123 break;
1124 case XML_ENTITY_REF_NODE:
1125 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
1126 "Misplaced ENTITYREF node\n");
1127 break;
1128 case XML_ENTITY_NODE:
1129 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
1130 "Misplaced ENTITY node\n");
1131 break;
1132 case XML_PI_NODE:
1133 xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
1134 "Misplaced PI node\n");
1135 break;
1136 case XML_COMMENT_NODE:
1137 xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
1138 "Misplaced COMMENT node\n");
1139 break;
1140 case XML_DOCUMENT_NODE:
1141 if (!ctxt->check)
1142 fprintf(ctxt->output, "DOCUMENT\n");
1143 break;
1144 case XML_HTML_DOCUMENT_NODE:
1145 if (!ctxt->check)
1146 fprintf(ctxt->output, "HTML DOCUMENT\n");
1147 break;
1148 case XML_DOCUMENT_TYPE_NODE:
1149 xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
1150 "Misplaced DOCTYPE node\n");
1151 break;
1152 case XML_DOCUMENT_FRAG_NODE:
1153 xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
1154 "Misplaced FRAGMENT node\n");
1155 break;
1156 case XML_NOTATION_NODE:
1157 xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
1158 "Misplaced NOTATION node\n");
1159 break;
1160 default:
1161 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1162 "Unknown node type %d\n", doc->type);
1163 }
1164}
Daniel Veillard22cdb842004-10-04 14:09:17 +00001165
1166/**
1167 * xmlCtxtDumpDocumentHead:
1168 * @output: the FILE * for the output
1169 * @doc: the document
1170 *
1171 * Dumps debug information cncerning the document, not recursive
1172 */
1173static void
1174xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1175{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001176 if (doc == NULL) return;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001177 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001178 if (!ctxt->check) {
1179 if (doc->name != NULL) {
1180 fprintf(ctxt->output, "name=");
1181 xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
1182 fprintf(ctxt->output, "\n");
1183 }
1184 if (doc->version != NULL) {
1185 fprintf(ctxt->output, "version=");
1186 xmlCtxtDumpString(ctxt, doc->version);
1187 fprintf(ctxt->output, "\n");
1188 }
1189 if (doc->encoding != NULL) {
1190 fprintf(ctxt->output, "encoding=");
1191 xmlCtxtDumpString(ctxt, doc->encoding);
1192 fprintf(ctxt->output, "\n");
1193 }
1194 if (doc->URL != NULL) {
1195 fprintf(ctxt->output, "URL=");
1196 xmlCtxtDumpString(ctxt, doc->URL);
1197 fprintf(ctxt->output, "\n");
1198 }
1199 if (doc->standalone)
1200 fprintf(ctxt->output, "standalone=true\n");
1201 }
1202 if (doc->oldNs != NULL)
1203 xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
1204}
1205
1206/**
1207 * xmlCtxtDumpDocument:
1208 * @output: the FILE * for the output
1209 * @doc: the document
1210 *
1211 * Dumps debug information for the document, it's recursive
1212 */
1213static void
1214xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1215{
1216 if (doc == NULL) {
1217 if (!ctxt->check)
1218 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1219 return;
1220 }
1221 xmlCtxtDumpDocumentHead(ctxt, doc);
1222 if (((doc->type == XML_DOCUMENT_NODE) ||
1223 (doc->type == XML_HTML_DOCUMENT_NODE))
1224 && (doc->children != NULL)) {
1225 ctxt->depth++;
1226 xmlCtxtDumpNodeList(ctxt, doc->children);
1227 ctxt->depth--;
1228 }
1229}
1230
1231static void
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01001232xmlCtxtDumpEntityCallback(void *payload, void *data,
1233 const xmlChar *name ATTRIBUTE_UNUSED)
Daniel Veillard22cdb842004-10-04 14:09:17 +00001234{
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01001235 xmlEntityPtr cur = (xmlEntityPtr) payload;
1236 xmlDebugCtxtPtr ctxt = (xmlDebugCtxtPtr) data;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001237 if (cur == NULL) {
1238 if (!ctxt->check)
1239 fprintf(ctxt->output, "Entity is NULL");
1240 return;
1241 }
1242 if (!ctxt->check) {
1243 fprintf(ctxt->output, "%s : ", (char *) cur->name);
1244 switch (cur->etype) {
1245 case XML_INTERNAL_GENERAL_ENTITY:
1246 fprintf(ctxt->output, "INTERNAL GENERAL, ");
1247 break;
1248 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1249 fprintf(ctxt->output, "EXTERNAL PARSED, ");
1250 break;
1251 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1252 fprintf(ctxt->output, "EXTERNAL UNPARSED, ");
1253 break;
1254 case XML_INTERNAL_PARAMETER_ENTITY:
1255 fprintf(ctxt->output, "INTERNAL PARAMETER, ");
1256 break;
1257 case XML_EXTERNAL_PARAMETER_ENTITY:
1258 fprintf(ctxt->output, "EXTERNAL PARAMETER, ");
1259 break;
1260 default:
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001261 xmlDebugErr2(ctxt, XML_CHECK_ENTITY_TYPE,
1262 "Unknown entity type %d\n", cur->etype);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001263 }
1264 if (cur->ExternalID != NULL)
1265 fprintf(ctxt->output, "ID \"%s\"", (char *) cur->ExternalID);
1266 if (cur->SystemID != NULL)
1267 fprintf(ctxt->output, "SYSTEM \"%s\"", (char *) cur->SystemID);
1268 if (cur->orig != NULL)
1269 fprintf(ctxt->output, "\n orig \"%s\"", (char *) cur->orig);
1270 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL))
1271 fprintf(ctxt->output, "\n content \"%s\"",
1272 (char *) cur->content);
1273 fprintf(ctxt->output, "\n");
1274 }
1275}
1276
1277/**
1278 * xmlCtxtDumpEntities:
1279 * @output: the FILE * for the output
1280 * @doc: the document
1281 *
1282 * Dumps debug information for all the entities in use by the document
1283 */
1284static void
1285xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1286{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001287 if (doc == NULL) return;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001288 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001289 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
1290 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1291 doc->intSubset->entities;
1292
1293 if (!ctxt->check)
1294 fprintf(ctxt->output, "Entities in internal subset\n");
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01001295 xmlHashScan(table, xmlCtxtDumpEntityCallback, ctxt);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001296 } else
1297 fprintf(ctxt->output, "No entities in internal subset\n");
1298 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
1299 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1300 doc->extSubset->entities;
1301
1302 if (!ctxt->check)
1303 fprintf(ctxt->output, "Entities in external subset\n");
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01001304 xmlHashScan(table, xmlCtxtDumpEntityCallback, ctxt);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001305 } else if (!ctxt->check)
1306 fprintf(ctxt->output, "No entities in external subset\n");
1307}
1308
1309/**
1310 * xmlCtxtDumpDTD:
1311 * @output: the FILE * for the output
1312 * @dtd: the DTD
1313 *
1314 * Dumps debug information for the DTD
1315 */
1316static void
1317xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
1318{
1319 if (dtd == NULL) {
1320 if (!ctxt->check)
1321 fprintf(ctxt->output, "DTD is NULL\n");
1322 return;
1323 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001324 xmlCtxtDumpDtdNode(ctxt, dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001325 if (dtd->children == NULL)
1326 fprintf(ctxt->output, " DTD is empty\n");
1327 else {
1328 ctxt->depth++;
1329 xmlCtxtDumpNodeList(ctxt, dtd->children);
1330 ctxt->depth--;
1331 }
1332}
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001333
Daniel Veillard22cdb842004-10-04 14:09:17 +00001334/************************************************************************
1335 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001336 * Public entry points for dump *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001337 * *
1338 ************************************************************************/
1339
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001340/**
1341 * xmlDebugDumpString:
1342 * @output: the FILE * for the output
1343 * @str: the string
1344 *
1345 * Dumps informations about the string, shorten it if necessary
1346 */
1347void
1348xmlDebugDumpString(FILE * output, const xmlChar * str)
1349{
Owen Taylor3473f882001-02-23 17:55:21 +00001350 int i;
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001351
Daniel Veillard7db38712002-02-07 16:39:11 +00001352 if (output == NULL)
1353 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00001354 if (str == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001355 fprintf(output, "(NULL)");
1356 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001357 }
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001358 for (i = 0; i < 40; i++)
1359 if (str[i] == 0)
1360 return;
William M. Brack76e95df2003-10-18 16:20:14 +00001361 else if (IS_BLANK_CH(str[i]))
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001362 fputc(' ', output);
1363 else if (str[i] >= 0x80)
1364 fprintf(output, "#%X", str[i]);
1365 else
1366 fputc(str[i], output);
Owen Taylor3473f882001-02-23 17:55:21 +00001367 fprintf(output, "...");
1368}
1369
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001370/**
1371 * xmlDebugDumpAttr:
1372 * @output: the FILE * for the output
1373 * @attr: the attribute
1374 * @depth: the indentation level.
1375 *
1376 * Dumps debug information for the attribute
1377 */
1378void
1379xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
Daniel Veillard22cdb842004-10-04 14:09:17 +00001380 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001381
Daniel Veillarda82b1822004-11-08 16:24:57 +00001382 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001383 xmlCtxtDumpInitCtxt(&ctxt);
1384 ctxt.output = output;
1385 ctxt.depth = depth;
1386 xmlCtxtDumpAttr(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001387 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001388}
Owen Taylor3473f882001-02-23 17:55:21 +00001389
Owen Taylor3473f882001-02-23 17:55:21 +00001390
Daniel Veillard22cdb842004-10-04 14:09:17 +00001391/**
1392 * xmlDebugDumpEntities:
1393 * @output: the FILE * for the output
1394 * @doc: the document
1395 *
1396 * Dumps debug information for all the entities in use by the document
1397 */
1398void
1399xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
1400{
1401 xmlDebugCtxt ctxt;
1402
Daniel Veillarda82b1822004-11-08 16:24:57 +00001403 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001404 xmlCtxtDumpInitCtxt(&ctxt);
1405 ctxt.output = output;
1406 xmlCtxtDumpEntities(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001407 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001408}
1409
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001410/**
1411 * xmlDebugDumpAttrList:
1412 * @output: the FILE * for the output
1413 * @attr: the attribute list
1414 * @depth: the indentation level.
1415 *
1416 * Dumps debug information for the attribute list
1417 */
1418void
1419xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
1420{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001421 xmlDebugCtxt ctxt;
1422
Daniel Veillarda82b1822004-11-08 16:24:57 +00001423 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001424 xmlCtxtDumpInitCtxt(&ctxt);
1425 ctxt.output = output;
1426 ctxt.depth = depth;
1427 xmlCtxtDumpAttrList(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001428 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001429}
1430
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001431/**
1432 * xmlDebugDumpOneNode:
1433 * @output: the FILE * for the output
1434 * @node: the node
1435 * @depth: the indentation level.
1436 *
1437 * Dumps debug information for the element node, it is not recursive
1438 */
1439void
1440xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
1441{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001442 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001443
Daniel Veillarda82b1822004-11-08 16:24:57 +00001444 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001445 xmlCtxtDumpInitCtxt(&ctxt);
1446 ctxt.output = output;
1447 ctxt.depth = depth;
1448 xmlCtxtDumpOneNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001449 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001450}
1451
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001452/**
1453 * xmlDebugDumpNode:
1454 * @output: the FILE * for the output
1455 * @node: the node
1456 * @depth: the indentation level.
1457 *
1458 * Dumps debug information for the element node, it is recursive
1459 */
1460void
1461xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
1462{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001463 xmlDebugCtxt ctxt;
1464
Daniel Veillard7db38712002-02-07 16:39:11 +00001465 if (output == NULL)
1466 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001467 xmlCtxtDumpInitCtxt(&ctxt);
1468 ctxt.output = output;
1469 ctxt.depth = depth;
1470 xmlCtxtDumpNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001471 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001472}
1473
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001474/**
1475 * xmlDebugDumpNodeList:
1476 * @output: the FILE * for the output
1477 * @node: the node list
1478 * @depth: the indentation level.
1479 *
1480 * Dumps debug information for the list of element node, it is recursive
1481 */
1482void
1483xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
1484{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001485 xmlDebugCtxt ctxt;
1486
Daniel Veillard7db38712002-02-07 16:39:11 +00001487 if (output == NULL)
1488 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001489 xmlCtxtDumpInitCtxt(&ctxt);
1490 ctxt.output = output;
1491 ctxt.depth = depth;
1492 xmlCtxtDumpNodeList(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001493 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001494}
1495
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001496/**
1497 * xmlDebugDumpDocumentHead:
1498 * @output: the FILE * for the output
1499 * @doc: the document
1500 *
1501 * Dumps debug information cncerning the document, not recursive
1502 */
1503void
1504xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
1505{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001506 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001507
Daniel Veillard22cdb842004-10-04 14:09:17 +00001508 if (output == NULL)
1509 output = stdout;
1510 xmlCtxtDumpInitCtxt(&ctxt);
Daniel Veillardcfa303a2005-08-25 14:03:56 +00001511 ctxt.options |= DUMP_TEXT_TYPE;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001512 ctxt.output = output;
1513 xmlCtxtDumpDocumentHead(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001514 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001515}
1516
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001517/**
1518 * xmlDebugDumpDocument:
1519 * @output: the FILE * for the output
1520 * @doc: the document
1521 *
1522 * Dumps debug information for the document, it's recursive
1523 */
1524void
1525xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
1526{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001527 xmlDebugCtxt ctxt;
1528
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001529 if (output == NULL)
Daniel Veillard22cdb842004-10-04 14:09:17 +00001530 output = stdout;
1531 xmlCtxtDumpInitCtxt(&ctxt);
Daniel Veillardcfa303a2005-08-25 14:03:56 +00001532 ctxt.options |= DUMP_TEXT_TYPE;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001533 ctxt.output = output;
1534 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001535 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001536}
Owen Taylor3473f882001-02-23 17:55:21 +00001537
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001538/**
1539 * xmlDebugDumpDTD:
1540 * @output: the FILE * for the output
1541 * @dtd: the DTD
1542 *
1543 * Dumps debug information for the DTD
1544 */
1545void
1546xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
1547{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001548 xmlDebugCtxt ctxt;
1549
Daniel Veillard7db38712002-02-07 16:39:11 +00001550 if (output == NULL)
1551 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001552 xmlCtxtDumpInitCtxt(&ctxt);
Daniel Veillardcfa303a2005-08-25 14:03:56 +00001553 ctxt.options |= DUMP_TEXT_TYPE;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001554 ctxt.output = output;
1555 xmlCtxtDumpDTD(&ctxt, dtd);
Daniel Veillard76821142004-10-09 20:39:04 +00001556 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001557}
1558
Daniel Veillard22cdb842004-10-04 14:09:17 +00001559/************************************************************************
1560 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001561 * Public entry points for checkings *
1562 * *
1563 ************************************************************************/
1564
1565/**
1566 * xmlDebugCheckDocument:
1567 * @output: the FILE * for the output
1568 * @doc: the document
1569 *
1570 * Check the document for potential content problems, and output
1571 * the errors to @output
1572 *
1573 * Returns the number of errors found
1574 */
1575int
1576xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
1577{
1578 xmlDebugCtxt ctxt;
1579
1580 if (output == NULL)
1581 output = stdout;
1582 xmlCtxtDumpInitCtxt(&ctxt);
1583 ctxt.output = output;
1584 ctxt.check = 1;
1585 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001586 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001587 return(ctxt.errors);
1588}
1589
1590/************************************************************************
1591 * *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001592 * Helpers for Shell *
1593 * *
1594 ************************************************************************/
Owen Taylor3473f882001-02-23 17:55:21 +00001595
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001596/**
1597 * xmlLsCountNode:
1598 * @node: the node to count
1599 *
1600 * Count the children of @node.
1601 *
1602 * Returns the number of children of @node.
1603 */
1604int
1605xmlLsCountNode(xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00001606 int ret = 0;
1607 xmlNodePtr list = NULL;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001608
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001609 if (node == NULL)
1610 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001611
1612 switch (node->type) {
1613 case XML_ELEMENT_NODE:
1614 list = node->children;
1615 break;
1616 case XML_DOCUMENT_NODE:
1617 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00001618#ifdef LIBXML_DOCB_ENABLED
1619 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001620#endif
1621 list = ((xmlDocPtr) node)->children;
1622 break;
1623 case XML_ATTRIBUTE_NODE:
1624 list = ((xmlAttrPtr) node)->children;
1625 break;
1626 case XML_TEXT_NODE:
1627 case XML_CDATA_SECTION_NODE:
1628 case XML_PI_NODE:
1629 case XML_COMMENT_NODE:
1630 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001631 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001632 }
1633 break;
1634 case XML_ENTITY_REF_NODE:
1635 case XML_DOCUMENT_TYPE_NODE:
1636 case XML_ENTITY_NODE:
1637 case XML_DOCUMENT_FRAG_NODE:
1638 case XML_NOTATION_NODE:
1639 case XML_DTD_NODE:
1640 case XML_ELEMENT_DECL:
1641 case XML_ATTRIBUTE_DECL:
1642 case XML_ENTITY_DECL:
1643 case XML_NAMESPACE_DECL:
1644 case XML_XINCLUDE_START:
1645 case XML_XINCLUDE_END:
1646 ret = 1;
1647 break;
1648 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001649 for (;list != NULL;ret++)
Owen Taylor3473f882001-02-23 17:55:21 +00001650 list = list->next;
1651 return(ret);
1652}
1653
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001654/**
1655 * xmlLsOneNode:
1656 * @output: the FILE * for the output
1657 * @node: the node to dump
1658 *
1659 * Dump to @output the type and name of @node.
1660 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001661void
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001662xmlLsOneNode(FILE *output, xmlNodePtr node) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00001663 if (output == NULL) return;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001664 if (node == NULL) {
1665 fprintf(output, "NULL\n");
1666 return;
1667 }
Owen Taylor3473f882001-02-23 17:55:21 +00001668 switch (node->type) {
1669 case XML_ELEMENT_NODE:
1670 fprintf(output, "-");
1671 break;
1672 case XML_ATTRIBUTE_NODE:
1673 fprintf(output, "a");
1674 break;
1675 case XML_TEXT_NODE:
1676 fprintf(output, "t");
1677 break;
1678 case XML_CDATA_SECTION_NODE:
Daniel Veillard75be0132002-03-13 10:03:35 +00001679 fprintf(output, "C");
Owen Taylor3473f882001-02-23 17:55:21 +00001680 break;
1681 case XML_ENTITY_REF_NODE:
1682 fprintf(output, "e");
1683 break;
1684 case XML_ENTITY_NODE:
1685 fprintf(output, "E");
1686 break;
1687 case XML_PI_NODE:
1688 fprintf(output, "p");
1689 break;
1690 case XML_COMMENT_NODE:
1691 fprintf(output, "c");
1692 break;
1693 case XML_DOCUMENT_NODE:
1694 fprintf(output, "d");
1695 break;
1696 case XML_HTML_DOCUMENT_NODE:
1697 fprintf(output, "h");
1698 break;
1699 case XML_DOCUMENT_TYPE_NODE:
1700 fprintf(output, "T");
1701 break;
1702 case XML_DOCUMENT_FRAG_NODE:
1703 fprintf(output, "F");
1704 break;
1705 case XML_NOTATION_NODE:
1706 fprintf(output, "N");
1707 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001708 case XML_NAMESPACE_DECL:
1709 fprintf(output, "n");
1710 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001711 default:
1712 fprintf(output, "?");
1713 }
Daniel Veillarde6a55192002-01-14 17:11:53 +00001714 if (node->type != XML_NAMESPACE_DECL) {
1715 if (node->properties != NULL)
1716 fprintf(output, "a");
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001717 else
Daniel Veillarde6a55192002-01-14 17:11:53 +00001718 fprintf(output, "-");
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001719 if (node->nsDef != NULL)
Daniel Veillarde6a55192002-01-14 17:11:53 +00001720 fprintf(output, "n");
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001721 else
Daniel Veillarde6a55192002-01-14 17:11:53 +00001722 fprintf(output, "-");
1723 }
Owen Taylor3473f882001-02-23 17:55:21 +00001724
1725 fprintf(output, " %8d ", xmlLsCountNode(node));
1726
1727 switch (node->type) {
1728 case XML_ELEMENT_NODE:
Ryan40db1ee2012-05-07 17:04:04 +08001729 if (node->name != NULL) {
1730 if ((node->ns != NULL) && (node->ns->prefix != NULL))
1731 fprintf(output, "%s:", node->ns->prefix);
Daniel Veillard580ced82003-03-21 21:22:48 +00001732 fprintf(output, "%s", (const char *) node->name);
Ryan40db1ee2012-05-07 17:04:04 +08001733 }
Owen Taylor3473f882001-02-23 17:55:21 +00001734 break;
1735 case XML_ATTRIBUTE_NODE:
1736 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001737 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001738 break;
1739 case XML_TEXT_NODE:
1740 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001741 xmlDebugDumpString(output, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001742 }
1743 break;
1744 case XML_CDATA_SECTION_NODE:
1745 break;
1746 case XML_ENTITY_REF_NODE:
1747 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001748 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001749 break;
1750 case XML_ENTITY_NODE:
1751 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001752 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001753 break;
1754 case XML_PI_NODE:
1755 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001756 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001757 break;
1758 case XML_COMMENT_NODE:
1759 break;
1760 case XML_DOCUMENT_NODE:
1761 break;
1762 case XML_HTML_DOCUMENT_NODE:
1763 break;
1764 case XML_DOCUMENT_TYPE_NODE:
1765 break;
1766 case XML_DOCUMENT_FRAG_NODE:
1767 break;
1768 case XML_NOTATION_NODE:
1769 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001770 case XML_NAMESPACE_DECL: {
1771 xmlNsPtr ns = (xmlNsPtr) node;
1772
1773 if (ns->prefix == NULL)
William M. Brack13dfa872004-09-18 04:52:08 +00001774 fprintf(output, "default -> %s", (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001775 else
William M. Brack13dfa872004-09-18 04:52:08 +00001776 fprintf(output, "%s -> %s", (char *)ns->prefix,
1777 (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001778 break;
1779 }
Owen Taylor3473f882001-02-23 17:55:21 +00001780 default:
1781 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001782 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001783 }
1784 fprintf(output, "\n");
1785}
1786
Daniel Veillard78d12092001-10-11 09:12:24 +00001787/**
1788 * xmlBoolToText:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001789 * @boolval: a bool to turn into text
Daniel Veillard78d12092001-10-11 09:12:24 +00001790 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001791 * Convenient way to turn bool into text
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001792 *
1793 * Returns a pointer to either "True" or "False"
1794 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001795const char *
Daniel Veillardebd38c52001-11-01 08:38:12 +00001796xmlBoolToText(int boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001797{
Daniel Veillardebd38c52001-11-01 08:38:12 +00001798 if (boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001799 return("True");
1800 else
1801 return("False");
1802}
1803
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00001804#ifdef LIBXML_XPATH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001805/****************************************************************
1806 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001807 * The XML shell related functions *
Owen Taylor3473f882001-02-23 17:55:21 +00001808 * *
1809 ****************************************************************/
1810
Daniel Veillard78d12092001-10-11 09:12:24 +00001811
1812
Owen Taylor3473f882001-02-23 17:55:21 +00001813/*
1814 * TODO: Improvement/cleanups for the XML shell
1815 * - allow to shell out an editor on a subpart
1816 * - cleanup function registrations (with help) and calling
1817 * - provide registration routines
1818 */
1819
1820/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001821 * xmlShellPrintXPathError:
Daniel Veillard78d12092001-10-11 09:12:24 +00001822 * @errorType: valid xpath error id
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001823 * @arg: the argument that cause xpath to fail
Daniel Veillard78d12092001-10-11 09:12:24 +00001824 *
1825 * Print the xpath error to libxml default error channel
1826 */
1827void
1828xmlShellPrintXPathError(int errorType, const char *arg)
1829{
1830 const char *default_arg = "Result";
1831
1832 if (!arg)
1833 arg = default_arg;
1834
1835 switch (errorType) {
1836 case XPATH_UNDEFINED:
1837 xmlGenericError(xmlGenericErrorContext,
1838 "%s: no such node\n", arg);
1839 break;
1840
1841 case XPATH_BOOLEAN:
1842 xmlGenericError(xmlGenericErrorContext,
1843 "%s is a Boolean\n", arg);
1844 break;
1845 case XPATH_NUMBER:
1846 xmlGenericError(xmlGenericErrorContext,
1847 "%s is a number\n", arg);
1848 break;
1849 case XPATH_STRING:
1850 xmlGenericError(xmlGenericErrorContext,
1851 "%s is a string\n", arg);
1852 break;
1853 case XPATH_POINT:
1854 xmlGenericError(xmlGenericErrorContext,
1855 "%s is a point\n", arg);
1856 break;
1857 case XPATH_RANGE:
1858 xmlGenericError(xmlGenericErrorContext,
1859 "%s is a range\n", arg);
1860 break;
1861 case XPATH_LOCATIONSET:
1862 xmlGenericError(xmlGenericErrorContext,
1863 "%s is a range\n", arg);
1864 break;
1865 case XPATH_USERS:
1866 xmlGenericError(xmlGenericErrorContext,
1867 "%s is user-defined\n", arg);
1868 break;
1869 case XPATH_XSLT_TREE:
1870 xmlGenericError(xmlGenericErrorContext,
1871 "%s is an XSLT value tree\n", arg);
1872 break;
1873 }
Daniel Veillarda82b1822004-11-08 16:24:57 +00001874#if 0
Daniel Veillard78d12092001-10-11 09:12:24 +00001875 xmlGenericError(xmlGenericErrorContext,
1876 "Try casting the result string function (xpath builtin)\n",
1877 arg);
Daniel Veillarda82b1822004-11-08 16:24:57 +00001878#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00001879}
1880
1881
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001882#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001883/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001884 * xmlShellPrintNodeCtxt:
1885 * @ctxt : a non-null shell context
1886 * @node : a non-null node to print to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001887 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001888 * Print node to the output FILE
1889 */
1890static void
1891xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1892{
Daniel Veillard01992e02002-10-09 10:20:30 +00001893 FILE *fp;
1894
1895 if (!node)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001896 return;
Daniel Veillard01992e02002-10-09 10:20:30 +00001897 if (ctxt == NULL)
1898 fp = stdout;
1899 else
1900 fp = ctxt->output;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001901
1902 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001903 xmlDocDump(fp, (xmlDocPtr) node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001904 else if (node->type == XML_ATTRIBUTE_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001905 xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001906 else
Daniel Veillard01992e02002-10-09 10:20:30 +00001907 xmlElemDump(fp, node->doc, node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001908
Daniel Veillard01992e02002-10-09 10:20:30 +00001909 fprintf(fp, "\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00001910}
1911
1912/**
1913 * xmlShellPrintNode:
1914 * @node : a non-null node to print to the output FILE
1915 *
1916 * Print node to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001917 */
1918void
1919xmlShellPrintNode(xmlNodePtr node)
1920{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001921 xmlShellPrintNodeCtxt(NULL, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001922}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001923#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001924
Daniel Veillard78d12092001-10-11 09:12:24 +00001925/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001926 * xmlShellPrintXPathResultCtxt:
1927 * @ctxt: a valid shell context
Daniel Veillard9d06d302002-01-22 18:15:52 +00001928 * @list: a valid result generated by an xpath evaluation
Daniel Veillard78d12092001-10-11 09:12:24 +00001929 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001930 * Prints result to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001931 */
Daniel Veillard321be0c2002-10-08 21:26:42 +00001932static void
1933xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
Daniel Veillard78d12092001-10-11 09:12:24 +00001934{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001935 if (!ctxt)
1936 return;
Daniel Veillard78d12092001-10-11 09:12:24 +00001937
1938 if (list != NULL) {
1939 switch (list->type) {
1940 case XPATH_NODESET:{
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001941#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001942 int indx;
1943
1944 if (list->nodesetval) {
1945 for (indx = 0; indx < list->nodesetval->nodeNr;
1946 indx++) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001947 xmlShellPrintNodeCtxt(ctxt,
1948 list->nodesetval->nodeTab[indx]);
Daniel Veillard78d12092001-10-11 09:12:24 +00001949 }
1950 } else {
1951 xmlGenericError(xmlGenericErrorContext,
1952 "Empty node set\n");
1953 }
1954 break;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001955#else
1956 xmlGenericError(xmlGenericErrorContext,
1957 "Node set\n");
1958#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001959 }
1960 case XPATH_BOOLEAN:
1961 xmlGenericError(xmlGenericErrorContext,
1962 "Is a Boolean:%s\n",
1963 xmlBoolToText(list->boolval));
1964 break;
1965 case XPATH_NUMBER:
1966 xmlGenericError(xmlGenericErrorContext,
1967 "Is a number:%0g\n", list->floatval);
1968 break;
1969 case XPATH_STRING:
1970 xmlGenericError(xmlGenericErrorContext,
1971 "Is a string:%s\n", list->stringval);
1972 break;
1973
1974 default:
1975 xmlShellPrintXPathError(list->type, NULL);
1976 }
1977 }
1978}
1979
1980/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001981 * xmlShellPrintXPathResult:
1982 * @list: a valid result generated by an xpath evaluation
1983 *
1984 * Prints result to the output FILE
1985 */
1986void
1987xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1988{
1989 xmlShellPrintXPathResultCtxt(NULL, list);
1990}
1991
1992/**
Owen Taylor3473f882001-02-23 17:55:21 +00001993 * xmlShellList:
1994 * @ctxt: the shell context
1995 * @arg: unused
1996 * @node: a node
1997 * @node2: unused
1998 *
1999 * Implements the XML shell function "ls"
2000 * Does an Unix like listing of the given node (like a directory)
2001 *
2002 * Returns 0
2003 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002004int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002005xmlShellList(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002006 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2007 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2008{
Owen Taylor3473f882001-02-23 17:55:21 +00002009 xmlNodePtr cur;
Daniel Veillard321be0c2002-10-08 21:26:42 +00002010 if (!ctxt)
2011 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002012 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002013 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002014 return (0);
2015 }
Owen Taylor3473f882001-02-23 17:55:21 +00002016 if ((node->type == XML_DOCUMENT_NODE) ||
2017 (node->type == XML_HTML_DOCUMENT_NODE)) {
2018 cur = ((xmlDocPtr) node)->children;
Daniel Veillarde6a55192002-01-14 17:11:53 +00002019 } else if (node->type == XML_NAMESPACE_DECL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002020 xmlLsOneNode(ctxt->output, node);
Daniel Veillarde6a55192002-01-14 17:11:53 +00002021 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002022 } else if (node->children != NULL) {
2023 cur = node->children;
2024 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002025 xmlLsOneNode(ctxt->output, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002026 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002027 }
2028 while (cur != NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002029 xmlLsOneNode(ctxt->output, cur);
Daniel Veillard78d12092001-10-11 09:12:24 +00002030 cur = cur->next;
Owen Taylor3473f882001-02-23 17:55:21 +00002031 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002032 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002033}
2034
2035/**
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002036 * xmlShellBase:
2037 * @ctxt: the shell context
2038 * @arg: unused
2039 * @node: a node
2040 * @node2: unused
2041 *
2042 * Implements the XML shell function "base"
2043 * dumps the current XML base of the node
2044 *
2045 * Returns 0
2046 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002047int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002048xmlShellBase(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002049 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2050 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2051{
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002052 xmlChar *base;
Daniel Veillard321be0c2002-10-08 21:26:42 +00002053 if (!ctxt)
2054 return 0;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002055 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002056 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002057 return (0);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002058 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002059
2060 base = xmlNodeGetBase(node->doc, node);
2061
2062 if (base == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002063 fprintf(ctxt->output, " No base found !!!\n");
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002064 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002065 fprintf(ctxt->output, "%s\n", base);
Daniel Veillard78d12092001-10-11 09:12:24 +00002066 xmlFree(base);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002067 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002068 return (0);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002069}
2070
Daniel Veillardb34321c2004-03-04 17:09:47 +00002071#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002072/**
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002073 * xmlShellSetBase:
2074 * @ctxt: the shell context
2075 * @arg: the new base
2076 * @node: a node
2077 * @node2: unused
2078 *
2079 * Implements the XML shell function "setbase"
2080 * change the current XML base of the node
2081 *
2082 * Returns 0
2083 */
2084static int
2085xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2086 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2087 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2088{
2089 xmlNodeSetBase(node, (xmlChar*) arg);
2090 return (0);
2091}
Daniel Veillard2156d432004-03-04 15:59:36 +00002092#endif
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002093
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002094#ifdef LIBXML_XPATH_ENABLED
2095/**
2096 * xmlShellRegisterNamespace:
2097 * @ctxt: the shell context
2098 * @arg: a string in prefix=nsuri format
2099 * @node: unused
2100 * @node2: unused
2101 *
2102 * Implements the XML shell function "setns"
2103 * register/unregister a prefix=namespace pair
2104 * on the XPath context
2105 *
2106 * Returns 0 on success and a negative value otherwise.
2107 */
2108static int
2109xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
2110 xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2111{
2112 xmlChar* nsListDup;
2113 xmlChar* prefix;
2114 xmlChar* href;
2115 xmlChar* next;
2116
2117 nsListDup = xmlStrdup((xmlChar *) arg);
2118 next = nsListDup;
2119 while(next != NULL) {
2120 /* skip spaces */
2121 /*while((*next) == ' ') next++;*/
2122 if((*next) == '\0') break;
2123
2124 /* find prefix */
2125 prefix = next;
2126 next = (xmlChar*)xmlStrchr(next, '=');
2127 if(next == NULL) {
2128 fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
2129 xmlFree(nsListDup);
2130 return(-1);
2131 }
2132 *(next++) = '\0';
2133
2134 /* find href */
2135 href = next;
2136 next = (xmlChar*)xmlStrchr(next, ' ');
2137 if(next != NULL) {
2138 *(next++) = '\0';
2139 }
2140
2141 /* do register namespace */
2142 if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
2143 fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
2144 xmlFree(nsListDup);
2145 return(-1);
2146 }
2147 }
2148
2149 xmlFree(nsListDup);
2150 return(0);
2151}
Daniel Veillard20887ee2005-07-04 09:27:40 +00002152/**
2153 * xmlShellRegisterRootNamespaces:
2154 * @ctxt: the shell context
2155 * @arg: unused
2156 * @node: the root element
2157 * @node2: unused
2158 *
2159 * Implements the XML shell function "setrootns"
2160 * which registers all namespaces declarations found on the root element.
2161 *
2162 * Returns 0 on success and a negative value otherwise.
2163 */
2164static int
2165xmlShellRegisterRootNamespaces(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2166 xmlNodePtr root, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2167{
2168 xmlNsPtr ns;
2169
2170 if ((root == NULL) || (root->type != XML_ELEMENT_NODE) ||
2171 (root->nsDef == NULL) || (ctxt == NULL) || (ctxt->pctxt == NULL))
2172 return(-1);
2173 ns = root->nsDef;
2174 while (ns != NULL) {
2175 if (ns->prefix == NULL)
2176 xmlXPathRegisterNs(ctxt->pctxt, BAD_CAST "defaultns", ns->href);
2177 else
2178 xmlXPathRegisterNs(ctxt->pctxt, ns->prefix, ns->href);
2179 ns = ns->next;
2180 }
2181 return(0);
2182}
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002183#endif
2184
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002185/**
Daniel Veillard1e208222002-10-22 14:25:25 +00002186 * xmlShellGrep:
2187 * @ctxt: the shell context
2188 * @arg: the string or regular expression to find
2189 * @node: a node
2190 * @node2: unused
2191 *
2192 * Implements the XML shell function "grep"
2193 * dumps informations about the node (namespace, attributes, content).
2194 *
2195 * Returns 0
2196 */
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002197static int
Daniel Veillard1e208222002-10-22 14:25:25 +00002198xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2199 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2200{
2201 if (!ctxt)
2202 return (0);
2203 if (node == NULL)
2204 return (0);
2205 if (arg == NULL)
2206 return (0);
2207#ifdef LIBXML_REGEXP_ENABLED
2208 if ((xmlStrchr((xmlChar *) arg, '?')) ||
2209 (xmlStrchr((xmlChar *) arg, '*')) ||
2210 (xmlStrchr((xmlChar *) arg, '.')) ||
2211 (xmlStrchr((xmlChar *) arg, '['))) {
2212 }
2213#endif
2214 while (node != NULL) {
2215 if (node->type == XML_COMMENT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002216 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002217
2218 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
2219 xmlShellList(ctxt, NULL, node, NULL);
2220 }
2221 } else if (node->type == XML_TEXT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002222 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002223
2224 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002225 xmlShellList(ctxt, NULL, node->parent, NULL);
Daniel Veillard1e208222002-10-22 14:25:25 +00002226 }
2227 }
2228
2229 /*
2230 * Browse the full subtree, deep first
2231 */
2232
2233 if ((node->type == XML_DOCUMENT_NODE) ||
2234 (node->type == XML_HTML_DOCUMENT_NODE)) {
2235 node = ((xmlDocPtr) node)->children;
2236 } else if ((node->children != NULL)
2237 && (node->type != XML_ENTITY_REF_NODE)) {
2238 /* deep first */
2239 node = node->children;
2240 } else if (node->next != NULL) {
2241 /* then siblings */
2242 node = node->next;
2243 } else {
2244 /* go up to parents->next if needed */
2245 while (node != NULL) {
2246 if (node->parent != NULL) {
2247 node = node->parent;
2248 }
2249 if (node->next != NULL) {
2250 node = node->next;
2251 break;
2252 }
2253 if (node->parent == NULL) {
2254 node = NULL;
2255 break;
2256 }
2257 }
2258 }
2259 }
2260 return (0);
2261}
2262
2263/**
Owen Taylor3473f882001-02-23 17:55:21 +00002264 * xmlShellDir:
2265 * @ctxt: the shell context
2266 * @arg: unused
2267 * @node: a node
2268 * @node2: unused
2269 *
2270 * Implements the XML shell function "dir"
2271 * dumps informations about the node (namespace, attributes, content).
2272 *
2273 * Returns 0
2274 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002275int
2276xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2277 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2278 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2279{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002280 if (!ctxt)
2281 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002282 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002283 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002284 return (0);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002285 }
Owen Taylor3473f882001-02-23 17:55:21 +00002286 if ((node->type == XML_DOCUMENT_NODE) ||
2287 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002288 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
Owen Taylor3473f882001-02-23 17:55:21 +00002289 } else if (node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002290 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002291 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002292 xmlDebugDumpOneNode(ctxt->output, node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002293 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002294 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002295}
2296
Daniel Veillard29b17482004-08-16 00:39:03 +00002297/**
2298 * xmlShellSetContent:
2299 * @ctxt: the shell context
2300 * @value: the content as a string
2301 * @node: a node
2302 * @node2: unused
2303 *
2304 * Implements the XML shell function "dir"
2305 * dumps informations about the node (namespace, attributes, content).
2306 *
2307 * Returns 0
2308 */
2309static int
2310xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2311 char *value, xmlNodePtr node,
2312 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2313{
2314 xmlNodePtr results;
2315 xmlParserErrors ret;
2316
2317 if (!ctxt)
2318 return (0);
2319 if (node == NULL) {
2320 fprintf(ctxt->output, "NULL\n");
2321 return (0);
2322 }
2323 if (value == NULL) {
2324 fprintf(ctxt->output, "NULL\n");
2325 return (0);
2326 }
2327
2328 ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
2329 if (ret == XML_ERR_OK) {
2330 if (node->children != NULL) {
2331 xmlFreeNodeList(node->children);
2332 node->children = NULL;
2333 node->last = NULL;
2334 }
2335 xmlAddChildList(node, results);
2336 } else {
2337 fprintf(ctxt->output, "failed to parse content\n");
2338 }
2339 return (0);
2340}
2341
Daniel Veillard522bc602004-02-21 11:53:09 +00002342#ifdef LIBXML_SCHEMAS_ENABLED
2343/**
2344 * xmlShellRNGValidate:
2345 * @ctxt: the shell context
2346 * @schemas: the path to the Relax-NG schemas
2347 * @node: a node
2348 * @node2: unused
2349 *
2350 * Implements the XML shell function "relaxng"
2351 * validating the instance against a Relax-NG schemas
2352 *
2353 * Returns 0
2354 */
2355static int
2356xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
2357 xmlNodePtr node ATTRIBUTE_UNUSED,
2358 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2359{
2360 xmlRelaxNGPtr relaxngschemas;
2361 xmlRelaxNGParserCtxtPtr ctxt;
2362 xmlRelaxNGValidCtxtPtr vctxt;
2363 int ret;
2364
2365 ctxt = xmlRelaxNGNewParserCtxt(schemas);
2366 xmlRelaxNGSetParserErrors(ctxt,
2367 (xmlRelaxNGValidityErrorFunc) fprintf,
2368 (xmlRelaxNGValidityWarningFunc) fprintf,
2369 stderr);
2370 relaxngschemas = xmlRelaxNGParse(ctxt);
2371 xmlRelaxNGFreeParserCtxt(ctxt);
2372 if (relaxngschemas == NULL) {
2373 xmlGenericError(xmlGenericErrorContext,
2374 "Relax-NG schema %s failed to compile\n", schemas);
2375 return(-1);
2376 }
2377 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2378 xmlRelaxNGSetValidErrors(vctxt,
2379 (xmlRelaxNGValidityErrorFunc) fprintf,
2380 (xmlRelaxNGValidityWarningFunc) fprintf,
2381 stderr);
2382 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2383 if (ret == 0) {
2384 fprintf(stderr, "%s validates\n", sctxt->filename);
2385 } else if (ret > 0) {
2386 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2387 } else {
2388 fprintf(stderr, "%s validation generated an internal error\n",
2389 sctxt->filename);
2390 }
2391 xmlRelaxNGFreeValidCtxt(vctxt);
2392 if (relaxngschemas != NULL)
2393 xmlRelaxNGFree(relaxngschemas);
2394 return(0);
2395}
2396#endif
2397
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002398#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002399/**
2400 * xmlShellCat:
2401 * @ctxt: the shell context
2402 * @arg: unused
2403 * @node: a node
2404 * @node2: unused
2405 *
2406 * Implements the XML shell function "cat"
2407 * dumps the serialization node content (XML or HTML).
2408 *
2409 * Returns 0
2410 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002411int
2412xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2413 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2414{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002415 if (!ctxt)
2416 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002417 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002418 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002419 return (0);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002420 }
Owen Taylor3473f882001-02-23 17:55:21 +00002421 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2422#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002423 if (node->type == XML_HTML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002424 htmlDocDump(ctxt->output, (htmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002425 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002426 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002427#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002428 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002429 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002430 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002431 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002432#endif /* LIBXML_HTML_ENABLED */
2433 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00002434 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002435 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002436 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002437 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002438 }
Daniel Veillard321be0c2002-10-08 21:26:42 +00002439 fprintf(ctxt->output, "\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002440 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002441}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002442#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002443
2444/**
2445 * xmlShellLoad:
2446 * @ctxt: the shell context
2447 * @filename: the file name
2448 * @node: unused
2449 * @node2: unused
2450 *
2451 * Implements the XML shell function "load"
2452 * loads a new document specified by the filename
2453 *
2454 * Returns 0 or -1 if loading failed
2455 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002456int
2457xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2458 xmlNodePtr node ATTRIBUTE_UNUSED,
2459 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2460{
Owen Taylor3473f882001-02-23 17:55:21 +00002461 xmlDocPtr doc;
2462 int html = 0;
2463
Daniel Veillarda82b1822004-11-08 16:24:57 +00002464 if ((ctxt == NULL) || (filename == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002465 if (ctxt->doc != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002466 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00002467
2468 if (html) {
2469#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002470 doc = htmlParseFile(filename, NULL);
2471#else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002472 fprintf(ctxt->output, "HTML support not compiled in\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002473 doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002474#endif /* LIBXML_HTML_ENABLED */
2475 } else {
Daniel Veillardebe25d42004-03-25 09:35:49 +00002476 doc = xmlReadFile(filename,NULL,0);
Owen Taylor3473f882001-02-23 17:55:21 +00002477 }
2478 if (doc != NULL) {
2479 if (ctxt->loaded == 1) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002480 xmlFreeDoc(ctxt->doc);
2481 }
2482 ctxt->loaded = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002483#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002484 xmlXPathFreeContext(ctxt->pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00002485#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002486 xmlFree(ctxt->filename);
2487 ctxt->doc = doc;
2488 ctxt->node = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002489#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002490 ctxt->pctxt = xmlXPathNewContext(doc);
Owen Taylor3473f882001-02-23 17:55:21 +00002491#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard85095e22003-04-23 13:56:44 +00002492 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002493 } else
Daniel Veillard78d12092001-10-11 09:12:24 +00002494 return (-1);
2495 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002496}
2497
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002498#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002499/**
2500 * xmlShellWrite:
2501 * @ctxt: the shell context
2502 * @filename: the file name
2503 * @node: a node in the tree
2504 * @node2: unused
2505 *
2506 * Implements the XML shell function "write"
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002507 * Write the current node to the filename, it saves the serialization
Owen Taylor3473f882001-02-23 17:55:21 +00002508 * of the subtree under the @node specified
2509 *
2510 * Returns 0 or -1 in case of error
2511 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002512int
Owen Taylor3473f882001-02-23 17:55:21 +00002513xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Daniel Veillard78d12092001-10-11 09:12:24 +00002514 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2515{
Owen Taylor3473f882001-02-23 17:55:21 +00002516 if (node == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002517 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002518 if ((filename == NULL) || (filename[0] == 0)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002519 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002520 }
2521#ifdef W_OK
2522 if (access((char *) filename, W_OK)) {
2523 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002524 "Cannot write to %s\n", filename);
2525 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002526 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002527#endif
2528 switch (node->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002529 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002530 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2531 xmlGenericError(xmlGenericErrorContext,
2532 "Failed to write to %s\n", filename);
2533 return (-1);
2534 }
2535 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002536 case XML_HTML_DOCUMENT_NODE:
2537#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002538 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2539 xmlGenericError(xmlGenericErrorContext,
2540 "Failed to write to %s\n", filename);
2541 return (-1);
2542 }
Owen Taylor3473f882001-02-23 17:55:21 +00002543#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002544 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2545 xmlGenericError(xmlGenericErrorContext,
2546 "Failed to write to %s\n", filename);
2547 return (-1);
2548 }
Owen Taylor3473f882001-02-23 17:55:21 +00002549#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002550 break;
2551 default:{
2552 FILE *f;
Owen Taylor3473f882001-02-23 17:55:21 +00002553
Daniel Veillard78d12092001-10-11 09:12:24 +00002554 f = fopen((char *) filename, "w");
2555 if (f == NULL) {
2556 xmlGenericError(xmlGenericErrorContext,
2557 "Failed to write to %s\n", filename);
2558 return (-1);
2559 }
2560 xmlElemDump(f, ctxt->doc, node);
2561 fclose(f);
2562 }
Owen Taylor3473f882001-02-23 17:55:21 +00002563 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002564 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002565}
2566
2567/**
2568 * xmlShellSave:
2569 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002570 * @filename: the file name (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002571 * @node: unused
2572 * @node2: unused
2573 *
2574 * Implements the XML shell function "save"
2575 * Write the current document to the filename, or it's original name
2576 *
2577 * Returns 0 or -1 in case of error
2578 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002579int
2580xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2581 xmlNodePtr node ATTRIBUTE_UNUSED,
2582 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2583{
Daniel Veillarda82b1822004-11-08 16:24:57 +00002584 if ((ctxt == NULL) || (ctxt->doc == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002585 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002586 if ((filename == NULL) || (filename[0] == 0))
2587 filename = ctxt->filename;
Daniel Veillarda82b1822004-11-08 16:24:57 +00002588 if (filename == NULL)
2589 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002590#ifdef W_OK
2591 if (access((char *) filename, W_OK)) {
2592 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002593 "Cannot save to %s\n", filename);
2594 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002595 }
2596#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002597 switch (ctxt->doc->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002598 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002599 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2600 xmlGenericError(xmlGenericErrorContext,
2601 "Failed to save to %s\n", filename);
2602 }
2603 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002604 case XML_HTML_DOCUMENT_NODE:
2605#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002606 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2607 xmlGenericError(xmlGenericErrorContext,
2608 "Failed to save to %s\n", filename);
2609 }
Owen Taylor3473f882001-02-23 17:55:21 +00002610#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002611 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2612 xmlGenericError(xmlGenericErrorContext,
2613 "Failed to save to %s\n", filename);
2614 }
Owen Taylor3473f882001-02-23 17:55:21 +00002615#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002616 break;
2617 default:
2618 xmlGenericError(xmlGenericErrorContext,
2619 "To save to subparts of a document use the 'write' command\n");
2620 return (-1);
2621
Owen Taylor3473f882001-02-23 17:55:21 +00002622 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002623 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002624}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002625#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002626
Daniel Veillardf54cd532004-02-25 11:52:31 +00002627#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002628/**
2629 * xmlShellValidate:
2630 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002631 * @dtd: the DTD URI (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002632 * @node: unused
2633 * @node2: unused
2634 *
2635 * Implements the XML shell function "validate"
2636 * Validate the document, if a DTD path is provided, then the validation
2637 * is done against the given DTD.
2638 *
2639 * Returns 0 or -1 in case of error
2640 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002641int
2642xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2643 xmlNodePtr node ATTRIBUTE_UNUSED,
2644 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2645{
Owen Taylor3473f882001-02-23 17:55:21 +00002646 xmlValidCtxt vctxt;
2647 int res = -1;
2648
Daniel Veillarda82b1822004-11-08 16:24:57 +00002649 if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002650 vctxt.userData = stderr;
2651 vctxt.error = (xmlValidityErrorFunc) fprintf;
2652 vctxt.warning = (xmlValidityWarningFunc) fprintf;
2653
2654 if ((dtd == NULL) || (dtd[0] == 0)) {
2655 res = xmlValidateDocument(&vctxt, ctxt->doc);
2656 } else {
2657 xmlDtdPtr subset;
2658
Daniel Veillard78d12092001-10-11 09:12:24 +00002659 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2660 if (subset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002661 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2662
Daniel Veillard78d12092001-10-11 09:12:24 +00002663 xmlFreeDtd(subset);
2664 }
Owen Taylor3473f882001-02-23 17:55:21 +00002665 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002666 return (res);
Owen Taylor3473f882001-02-23 17:55:21 +00002667}
Daniel Veillardf54cd532004-02-25 11:52:31 +00002668#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002669
2670/**
2671 * xmlShellDu:
2672 * @ctxt: the shell context
2673 * @arg: unused
2674 * @tree: a node defining a subtree
2675 * @node2: unused
2676 *
2677 * Implements the XML shell function "du"
2678 * show the structure of the subtree under node @tree
2679 * If @tree is null, the command works on the current node.
2680 *
2681 * Returns 0 or -1 in case of error
2682 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002683int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002684xmlShellDu(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002685 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2686 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2687{
Owen Taylor3473f882001-02-23 17:55:21 +00002688 xmlNodePtr node;
Daniel Veillard78d12092001-10-11 09:12:24 +00002689 int indent = 0, i;
Owen Taylor3473f882001-02-23 17:55:21 +00002690
Daniel Veillard321be0c2002-10-08 21:26:42 +00002691 if (!ctxt)
2692 return (-1);
2693
Daniel Veillard78d12092001-10-11 09:12:24 +00002694 if (tree == NULL)
2695 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002696 node = tree;
2697 while (node != NULL) {
2698 if ((node->type == XML_DOCUMENT_NODE) ||
2699 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002700 fprintf(ctxt->output, "/\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002701 } else if (node->type == XML_ELEMENT_NODE) {
2702 for (i = 0; i < indent; i++)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002703 fprintf(ctxt->output, " ");
Ryan40db1ee2012-05-07 17:04:04 +08002704 if ((node->ns) && (node->ns->prefix))
2705 fprintf(ctxt->output, "%s:", node->ns->prefix);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002706 fprintf(ctxt->output, "%s\n", node->name);
Daniel Veillard78d12092001-10-11 09:12:24 +00002707 } else {
2708 }
Owen Taylor3473f882001-02-23 17:55:21 +00002709
Daniel Veillard78d12092001-10-11 09:12:24 +00002710 /*
2711 * Browse the full subtree, deep first
2712 */
Owen Taylor3473f882001-02-23 17:55:21 +00002713
2714 if ((node->type == XML_DOCUMENT_NODE) ||
2715 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002716 node = ((xmlDocPtr) node)->children;
2717 } else if ((node->children != NULL)
2718 && (node->type != XML_ENTITY_REF_NODE)) {
2719 /* deep first */
2720 node = node->children;
2721 indent++;
2722 } else if ((node != tree) && (node->next != NULL)) {
2723 /* then siblings */
2724 node = node->next;
2725 } else if (node != tree) {
2726 /* go up to parents->next if needed */
2727 while (node != tree) {
2728 if (node->parent != NULL) {
2729 node = node->parent;
2730 indent--;
2731 }
2732 if ((node != tree) && (node->next != NULL)) {
2733 node = node->next;
2734 break;
2735 }
2736 if (node->parent == NULL) {
2737 node = NULL;
2738 break;
2739 }
2740 if (node == tree) {
2741 node = NULL;
2742 break;
2743 }
2744 }
2745 /* exit condition */
2746 if (node == tree)
2747 node = NULL;
2748 } else
2749 node = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002750 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002751 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002752}
2753
2754/**
2755 * xmlShellPwd:
2756 * @ctxt: the shell context
2757 * @buffer: the output buffer
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002758 * @node: a node
Owen Taylor3473f882001-02-23 17:55:21 +00002759 * @node2: unused
2760 *
2761 * Implements the XML shell function "pwd"
2762 * Show the full path from the root to the node, if needed building
2763 * thumblers when similar elements exists at a given ancestor level.
2764 * The output is compatible with XPath commands.
2765 *
2766 * Returns 0 or -1 in case of error
2767 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002768int
2769xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2770 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2771{
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002772 xmlChar *path;
Owen Taylor3473f882001-02-23 17:55:21 +00002773
Daniel Veillarda82b1822004-11-08 16:24:57 +00002774 if ((node == NULL) || (buffer == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002775 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002776
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002777 path = xmlGetNodePath(node);
2778 if (path == NULL)
2779 return (-1);
2780
2781 /*
2782 * This test prevents buffer overflow, because this routine
2783 * is only called by xmlShell, in which the second argument is
2784 * 500 chars long.
2785 * It is a dirty hack before a cleaner solution is found.
2786 * Documentation should mention that the second argument must
2787 * be at least 500 chars long, and could be stripped if too long.
2788 */
2789 snprintf(buffer, 499, "%s", path);
2790 buffer[499] = '0';
2791 xmlFree(path);
2792
Daniel Veillard78d12092001-10-11 09:12:24 +00002793 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002794}
2795
2796/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002797 * xmlShell:
Owen Taylor3473f882001-02-23 17:55:21 +00002798 * @doc: the initial document
2799 * @filename: the output buffer
2800 * @input: the line reading function
Daniel Veillard321be0c2002-10-08 21:26:42 +00002801 * @output: the output FILE*, defaults to stdout if NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002802 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002803 * Implements the XML shell
Owen Taylor3473f882001-02-23 17:55:21 +00002804 * This allow to load, validate, view, modify and save a document
2805 * using a environment similar to a UNIX commandline.
2806 */
2807void
2808xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Daniel Veillard78d12092001-10-11 09:12:24 +00002809 FILE * output)
2810{
Owen Taylor3473f882001-02-23 17:55:21 +00002811 char prompt[500] = "/ > ";
2812 char *cmdline = NULL, *cur;
Owen Taylor3473f882001-02-23 17:55:21 +00002813 char command[100];
2814 char arg[400];
2815 int i;
2816 xmlShellCtxtPtr ctxt;
2817 xmlXPathObjectPtr list;
2818
2819 if (doc == NULL)
2820 return;
2821 if (filename == NULL)
2822 return;
2823 if (input == NULL)
2824 return;
2825 if (output == NULL)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002826 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00002827 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Daniel Veillard78d12092001-10-11 09:12:24 +00002828 if (ctxt == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00002829 return;
2830 ctxt->loaded = 0;
2831 ctxt->doc = doc;
2832 ctxt->input = input;
2833 ctxt->output = output;
2834 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Daniel Veillard78d12092001-10-11 09:12:24 +00002835 ctxt->node = (xmlNodePtr) ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002836
2837#ifdef LIBXML_XPATH_ENABLED
2838 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2839 if (ctxt->pctxt == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002840 xmlFree(ctxt);
2841 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002842 }
2843#endif /* LIBXML_XPATH_ENABLED */
2844 while (1) {
2845 if (ctxt->node == (xmlNodePtr) ctxt->doc)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002846 snprintf(prompt, sizeof(prompt), "%s > ", "/");
Ryan40db1ee2012-05-07 17:04:04 +08002847 else if ((ctxt->node != NULL) && (ctxt->node->name) &&
2848 (ctxt->node->ns) && (ctxt->node->ns->prefix))
2849 snprintf(prompt, sizeof(prompt), "%s:%s > ",
2850 (ctxt->node->ns->prefix), ctxt->node->name);
Daniel Veillard7a985a12003-07-06 17:57:42 +00002851 else if ((ctxt->node != NULL) && (ctxt->node->name))
Daniel Veillard78d12092001-10-11 09:12:24 +00002852 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002853 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002854 snprintf(prompt, sizeof(prompt), "? > ");
Owen Taylor3473f882001-02-23 17:55:21 +00002855 prompt[sizeof(prompt) - 1] = 0;
2856
Daniel Veillard78d12092001-10-11 09:12:24 +00002857 /*
2858 * Get a new command line
2859 */
Owen Taylor3473f882001-02-23 17:55:21 +00002860 cmdline = ctxt->input(prompt);
Daniel Veillard78d12092001-10-11 09:12:24 +00002861 if (cmdline == NULL)
2862 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002863
Daniel Veillard78d12092001-10-11 09:12:24 +00002864 /*
2865 * Parse the command itself
2866 */
2867 cur = cmdline;
Daniel Veillard78d12092001-10-11 09:12:24 +00002868 while ((*cur == ' ') || (*cur == '\t'))
2869 cur++;
2870 i = 0;
2871 while ((*cur != ' ') && (*cur != '\t') &&
2872 (*cur != '\n') && (*cur != '\r')) {
2873 if (*cur == 0)
2874 break;
2875 command[i++] = *cur++;
2876 }
2877 command[i] = 0;
2878 if (i == 0)
2879 continue;
Owen Taylor3473f882001-02-23 17:55:21 +00002880
Daniel Veillard78d12092001-10-11 09:12:24 +00002881 /*
2882 * Parse the argument
2883 */
2884 while ((*cur == ' ') || (*cur == '\t'))
2885 cur++;
2886 i = 0;
2887 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2888 if (*cur == 0)
2889 break;
2890 arg[i++] = *cur++;
2891 }
2892 arg[i] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002893
Daniel Veillard78d12092001-10-11 09:12:24 +00002894 /*
2895 * start interpreting the command
2896 */
Owen Taylor3473f882001-02-23 17:55:21 +00002897 if (!strcmp(command, "exit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002898 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002899 if (!strcmp(command, "quit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002900 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002901 if (!strcmp(command, "bye"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002902 break;
Daniel Veillard5004f422001-11-08 13:53:05 +00002903 if (!strcmp(command, "help")) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002904 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2905 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2906 fprintf(ctxt->output, "\tbye leave shell\n");
2907 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2908 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2909 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2910 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2911 fprintf(ctxt->output, "\texit leave shell\n");
2912 fprintf(ctxt->output, "\thelp display this help\n");
2913 fprintf(ctxt->output, "\tfree display memory usage\n");
2914 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2915 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00002916 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 +00002917#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002918 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002919 fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2920 fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
Daniel Veillard20887ee2005-07-04 09:27:40 +00002921 fprintf(ctxt->output, "\tsetrootns register all namespace found on the root element\n");
2922 fprintf(ctxt->output, "\t the default namespace if any uses 'defaultns' prefix\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002923#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard321be0c2002-10-08 21:26:42 +00002924 fprintf(ctxt->output, "\tpwd display current working directory\n");
Ryan0cd29a32012-05-07 19:53:19 +08002925 fprintf(ctxt->output, "\twhereis display absolute path of [path] or current working directory\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00002926 fprintf(ctxt->output, "\tquit leave shell\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002927#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002928 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00002929 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002930#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf54cd532004-02-25 11:52:31 +00002931#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002932 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002933#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard522bc602004-02-21 11:53:09 +00002934#ifdef LIBXML_SCHEMAS_ENABLED
Nick Wellnhofer8bbe4502017-06-17 16:15:09 +02002935 fprintf(ctxt->output, "\trelaxng rng validate the document against the Relax-NG schemas\n");
Daniel Veillard522bc602004-02-21 11:53:09 +00002936#endif
Daniel Veillard1e208222002-10-22 14:25:25 +00002937 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002938#ifdef LIBXML_VALID_ENABLED
Daniel Veillard5004f422001-11-08 13:53:05 +00002939 } else if (!strcmp(command, "validate")) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002940 xmlShellValidate(ctxt, arg, NULL, NULL);
Daniel Veillardf54cd532004-02-25 11:52:31 +00002941#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002942 } else if (!strcmp(command, "load")) {
2943 xmlShellLoad(ctxt, arg, NULL, NULL);
Daniel Veillard522bc602004-02-21 11:53:09 +00002944#ifdef LIBXML_SCHEMAS_ENABLED
2945 } else if (!strcmp(command, "relaxng")) {
2946 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2947#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002948#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002949 } else if (!strcmp(command, "save")) {
2950 xmlShellSave(ctxt, arg, NULL, NULL);
2951 } else if (!strcmp(command, "write")) {
Daniel Veillardccc9cf92013-07-22 14:24:16 +08002952 if (arg[0] == 0)
Daniel Veillarda82b1822004-11-08 16:24:57 +00002953 xmlGenericError(xmlGenericErrorContext,
2954 "Write command requires a filename argument\n");
2955 else
Gwenn Kahzce5f9a72010-11-04 10:48:25 +01002956 xmlShellWrite(ctxt, arg, ctxt->node, NULL);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002957#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard1e208222002-10-22 14:25:25 +00002958 } else if (!strcmp(command, "grep")) {
2959 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
Daniel Veillard78d12092001-10-11 09:12:24 +00002960 } else if (!strcmp(command, "free")) {
2961 if (arg[0] == 0) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002962 xmlMemShow(ctxt->output, 0);
Daniel Veillard78d12092001-10-11 09:12:24 +00002963 } else {
2964 int len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002965
Daniel Veillard78d12092001-10-11 09:12:24 +00002966 sscanf(arg, "%d", &len);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002967 xmlMemShow(ctxt->output, len);
Daniel Veillard78d12092001-10-11 09:12:24 +00002968 }
2969 } else if (!strcmp(command, "pwd")) {
2970 char dir[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002971
Daniel Veillard78d12092001-10-11 09:12:24 +00002972 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
Daniel Veillard321be0c2002-10-08 21:26:42 +00002973 fprintf(ctxt->output, "%s\n", dir);
Daniel Veillard78d12092001-10-11 09:12:24 +00002974 } else if (!strcmp(command, "du")) {
Ryan40db1ee2012-05-07 17:04:04 +08002975 if (arg[0] == 0) {
2976 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2977 } else {
2978 ctxt->pctxt->node = ctxt->node;
2979#ifdef LIBXML_XPATH_ENABLED
2980 ctxt->pctxt->node = ctxt->node;
2981 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2982#else
2983 list = NULL;
2984#endif /* LIBXML_XPATH_ENABLED */
2985 if (list != NULL) {
2986 switch (list->type) {
2987 case XPATH_UNDEFINED:
2988 xmlGenericError(xmlGenericErrorContext,
2989 "%s: no such node\n", arg);
2990 break;
2991 case XPATH_NODESET:{
2992 int indx;
2993
2994 if (list->nodesetval == NULL)
2995 break;
2996
2997 for (indx = 0;
2998 indx < list->nodesetval->nodeNr;
2999 indx++)
3000 xmlShellDu(ctxt, NULL,
3001 list->nodesetval->
3002 nodeTab[indx], NULL);
3003 break;
3004 }
3005 case XPATH_BOOLEAN:
3006 xmlGenericError(xmlGenericErrorContext,
3007 "%s is a Boolean\n", arg);
3008 break;
3009 case XPATH_NUMBER:
3010 xmlGenericError(xmlGenericErrorContext,
3011 "%s is a number\n", arg);
3012 break;
3013 case XPATH_STRING:
3014 xmlGenericError(xmlGenericErrorContext,
3015 "%s is a string\n", arg);
3016 break;
3017 case XPATH_POINT:
3018 xmlGenericError(xmlGenericErrorContext,
3019 "%s is a point\n", arg);
3020 break;
3021 case XPATH_RANGE:
3022 xmlGenericError(xmlGenericErrorContext,
3023 "%s is a range\n", arg);
3024 break;
3025 case XPATH_LOCATIONSET:
3026 xmlGenericError(xmlGenericErrorContext,
3027 "%s is a range\n", arg);
3028 break;
3029 case XPATH_USERS:
3030 xmlGenericError(xmlGenericErrorContext,
3031 "%s is user-defined\n", arg);
3032 break;
3033 case XPATH_XSLT_TREE:
3034 xmlGenericError(xmlGenericErrorContext,
3035 "%s is an XSLT value tree\n",
3036 arg);
3037 break;
3038 }
3039#ifdef LIBXML_XPATH_ENABLED
3040 xmlXPathFreeObject(list);
3041#endif
3042 } else {
3043 xmlGenericError(xmlGenericErrorContext,
3044 "%s: no such node\n", arg);
3045 }
3046 ctxt->pctxt->node = NULL;
3047 }
Daniel Veillard78d12092001-10-11 09:12:24 +00003048 } else if (!strcmp(command, "base")) {
3049 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
Daniel Veillard29b17482004-08-16 00:39:03 +00003050 } else if (!strcmp(command, "set")) {
3051 xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00003052#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardbbaa9972004-06-16 14:08:33 +00003053 } else if (!strcmp(command, "setns")) {
3054 if (arg[0] == 0) {
3055 xmlGenericError(xmlGenericErrorContext,
3056 "setns: prefix=[nsuri] required\n");
3057 } else {
3058 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
3059 }
Daniel Veillard20887ee2005-07-04 09:27:40 +00003060 } else if (!strcmp(command, "setrootns")) {
3061 xmlNodePtr root;
3062
3063 root = xmlDocGetRootElement(ctxt->doc);
3064 xmlShellRegisterRootNamespaces(ctxt, NULL, root, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00003065 } else if (!strcmp(command, "xpath")) {
3066 if (arg[0] == 0) {
3067 xmlGenericError(xmlGenericErrorContext,
3068 "xpath: expression required\n");
3069 } else {
3070 ctxt->pctxt->node = ctxt->node;
3071 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Daniel Veillard321be0c2002-10-08 21:26:42 +00003072 xmlXPathDebugDumpObject(ctxt->output, list, 0);
Daniel Veillard2070c482002-01-22 22:12:19 +00003073 xmlXPathFreeObject(list);
3074 }
3075#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard2156d432004-03-04 15:59:36 +00003076#ifdef LIBXML_TREE_ENABLED
Daniel Veillardcfa0d812002-01-17 08:46:58 +00003077 } else if (!strcmp(command, "setbase")) {
3078 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2156d432004-03-04 15:59:36 +00003079#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00003080 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
3081 int dir = (!strcmp(command, "dir"));
3082
3083 if (arg[0] == 0) {
3084 if (dir)
3085 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
3086 else
3087 xmlShellList(ctxt, NULL, ctxt->node, NULL);
3088 } else {
3089 ctxt->pctxt->node = ctxt->node;
Daniel Veillard61d80a22001-04-27 17:13:01 +00003090#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00003091 ctxt->pctxt->node = ctxt->node;
3092 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3093#else
3094 list = NULL;
3095#endif /* LIBXML_XPATH_ENABLED */
3096 if (list != NULL) {
3097 switch (list->type) {
3098 case XPATH_UNDEFINED:
3099 xmlGenericError(xmlGenericErrorContext,
3100 "%s: no such node\n", arg);
3101 break;
3102 case XPATH_NODESET:{
3103 int indx;
3104
Daniel Veillarda6825e82001-11-07 13:33:59 +00003105 if (list->nodesetval == NULL)
3106 break;
3107
Daniel Veillard78d12092001-10-11 09:12:24 +00003108 for (indx = 0;
3109 indx < list->nodesetval->nodeNr;
3110 indx++) {
3111 if (dir)
3112 xmlShellDir(ctxt, NULL,
3113 list->nodesetval->
3114 nodeTab[indx], NULL);
3115 else
3116 xmlShellList(ctxt, NULL,
3117 list->nodesetval->
3118 nodeTab[indx], NULL);
3119 }
3120 break;
3121 }
3122 case XPATH_BOOLEAN:
3123 xmlGenericError(xmlGenericErrorContext,
3124 "%s is a Boolean\n", arg);
3125 break;
3126 case XPATH_NUMBER:
3127 xmlGenericError(xmlGenericErrorContext,
3128 "%s is a number\n", arg);
3129 break;
3130 case XPATH_STRING:
3131 xmlGenericError(xmlGenericErrorContext,
3132 "%s is a string\n", arg);
3133 break;
3134 case XPATH_POINT:
3135 xmlGenericError(xmlGenericErrorContext,
3136 "%s is a point\n", arg);
3137 break;
3138 case XPATH_RANGE:
3139 xmlGenericError(xmlGenericErrorContext,
3140 "%s is a range\n", arg);
3141 break;
3142 case XPATH_LOCATIONSET:
3143 xmlGenericError(xmlGenericErrorContext,
3144 "%s is a range\n", arg);
3145 break;
3146 case XPATH_USERS:
3147 xmlGenericError(xmlGenericErrorContext,
3148 "%s is user-defined\n", arg);
3149 break;
3150 case XPATH_XSLT_TREE:
3151 xmlGenericError(xmlGenericErrorContext,
3152 "%s is an XSLT value tree\n",
3153 arg);
3154 break;
3155 }
3156#ifdef LIBXML_XPATH_ENABLED
3157 xmlXPathFreeObject(list);
Daniel Veillard61d80a22001-04-27 17:13:01 +00003158#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00003159 } else {
3160 xmlGenericError(xmlGenericErrorContext,
3161 "%s: no such node\n", arg);
3162 }
3163 ctxt->pctxt->node = NULL;
3164 }
Ryan0cd29a32012-05-07 19:53:19 +08003165 } else if (!strcmp(command, "whereis")) {
3166 char dir[500];
3167
3168 if (arg[0] == 0) {
3169 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
3170 fprintf(ctxt->output, "%s\n", dir);
3171 } else {
3172 ctxt->pctxt->node = ctxt->node;
3173#ifdef LIBXML_XPATH_ENABLED
3174 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3175#else
3176 list = NULL;
3177#endif /* LIBXML_XPATH_ENABLED */
3178 if (list != NULL) {
3179 switch (list->type) {
3180 case XPATH_UNDEFINED:
3181 xmlGenericError(xmlGenericErrorContext,
3182 "%s: no such node\n", arg);
3183 break;
3184 case XPATH_NODESET:{
3185 int indx;
3186
3187 if (list->nodesetval == NULL)
3188 break;
3189
3190 for (indx = 0;
3191 indx < list->nodesetval->nodeNr;
3192 indx++) {
3193 if (!xmlShellPwd(ctxt, dir, list->nodesetval->
3194 nodeTab[indx], NULL))
3195 fprintf(ctxt->output, "%s\n", dir);
3196 }
3197 break;
3198 }
3199 case XPATH_BOOLEAN:
3200 xmlGenericError(xmlGenericErrorContext,
3201 "%s is a Boolean\n", arg);
3202 break;
3203 case XPATH_NUMBER:
3204 xmlGenericError(xmlGenericErrorContext,
3205 "%s is a number\n", arg);
3206 break;
3207 case XPATH_STRING:
3208 xmlGenericError(xmlGenericErrorContext,
3209 "%s is a string\n", arg);
3210 break;
3211 case XPATH_POINT:
3212 xmlGenericError(xmlGenericErrorContext,
3213 "%s is a point\n", arg);
3214 break;
3215 case XPATH_RANGE:
3216 xmlGenericError(xmlGenericErrorContext,
3217 "%s is a range\n", arg);
3218 break;
3219 case XPATH_LOCATIONSET:
3220 xmlGenericError(xmlGenericErrorContext,
3221 "%s is a range\n", arg);
3222 break;
3223 case XPATH_USERS:
3224 xmlGenericError(xmlGenericErrorContext,
3225 "%s is user-defined\n", arg);
3226 break;
3227 case XPATH_XSLT_TREE:
3228 xmlGenericError(xmlGenericErrorContext,
3229 "%s is an XSLT value tree\n",
3230 arg);
3231 break;
3232 }
3233#ifdef LIBXML_XPATH_ENABLED
3234 xmlXPathFreeObject(list);
3235#endif
3236 } else {
3237 xmlGenericError(xmlGenericErrorContext,
3238 "%s: no such node\n", arg);
3239 }
3240 ctxt->pctxt->node = NULL;
3241 }
Daniel Veillard78d12092001-10-11 09:12:24 +00003242 } else if (!strcmp(command, "cd")) {
3243 if (arg[0] == 0) {
3244 ctxt->node = (xmlNodePtr) ctxt->doc;
3245 } else {
3246#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard23243302014-07-14 17:59:31 +08003247 int l;
3248
Daniel Veillard78d12092001-10-11 09:12:24 +00003249 ctxt->pctxt->node = ctxt->node;
Daniel Veillard23243302014-07-14 17:59:31 +08003250 l = strlen(arg);
3251 if ((l >= 2) && (arg[l - 1] == '/'))
3252 arg[l - 1] = 0;
Daniel Veillard78d12092001-10-11 09:12:24 +00003253 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3254#else
3255 list = NULL;
3256#endif /* LIBXML_XPATH_ENABLED */
3257 if (list != NULL) {
3258 switch (list->type) {
3259 case XPATH_UNDEFINED:
3260 xmlGenericError(xmlGenericErrorContext,
3261 "%s: no such node\n", arg);
3262 break;
3263 case XPATH_NODESET:
Daniel Veillarda6825e82001-11-07 13:33:59 +00003264 if (list->nodesetval != NULL) {
3265 if (list->nodesetval->nodeNr == 1) {
3266 ctxt->node = list->nodesetval->nodeTab[0];
Daniel Veillard7a985a12003-07-06 17:57:42 +00003267 if ((ctxt->node != NULL) &&
3268 (ctxt->node->type ==
3269 XML_NAMESPACE_DECL)) {
3270 xmlGenericError(xmlGenericErrorContext,
3271 "cannot cd to namespace\n");
3272 ctxt->node = NULL;
3273 }
Daniel Veillarda6825e82001-11-07 13:33:59 +00003274 } else
3275 xmlGenericError(xmlGenericErrorContext,
3276 "%s is a %d Node Set\n",
3277 arg,
3278 list->nodesetval->nodeNr);
Daniel Veillard78d12092001-10-11 09:12:24 +00003279 } else
3280 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda6825e82001-11-07 13:33:59 +00003281 "%s is an empty Node Set\n",
3282 arg);
Daniel Veillard78d12092001-10-11 09:12:24 +00003283 break;
3284 case XPATH_BOOLEAN:
3285 xmlGenericError(xmlGenericErrorContext,
3286 "%s is a Boolean\n", arg);
3287 break;
3288 case XPATH_NUMBER:
3289 xmlGenericError(xmlGenericErrorContext,
3290 "%s is a number\n", arg);
3291 break;
3292 case XPATH_STRING:
3293 xmlGenericError(xmlGenericErrorContext,
3294 "%s is a string\n", arg);
3295 break;
3296 case XPATH_POINT:
3297 xmlGenericError(xmlGenericErrorContext,
3298 "%s is a point\n", arg);
3299 break;
3300 case XPATH_RANGE:
3301 xmlGenericError(xmlGenericErrorContext,
3302 "%s is a range\n", arg);
3303 break;
3304 case XPATH_LOCATIONSET:
3305 xmlGenericError(xmlGenericErrorContext,
3306 "%s is a range\n", arg);
3307 break;
3308 case XPATH_USERS:
3309 xmlGenericError(xmlGenericErrorContext,
3310 "%s is user-defined\n", arg);
3311 break;
3312 case XPATH_XSLT_TREE:
3313 xmlGenericError(xmlGenericErrorContext,
3314 "%s is an XSLT value tree\n",
3315 arg);
3316 break;
3317 }
3318#ifdef LIBXML_XPATH_ENABLED
3319 xmlXPathFreeObject(list);
3320#endif
3321 } else {
3322 xmlGenericError(xmlGenericErrorContext,
3323 "%s: no such node\n", arg);
3324 }
3325 ctxt->pctxt->node = NULL;
3326 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003327#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00003328 } else if (!strcmp(command, "cat")) {
3329 if (arg[0] == 0) {
3330 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3331 } else {
3332 ctxt->pctxt->node = ctxt->node;
3333#ifdef LIBXML_XPATH_ENABLED
3334 ctxt->pctxt->node = ctxt->node;
3335 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3336#else
3337 list = NULL;
3338#endif /* LIBXML_XPATH_ENABLED */
3339 if (list != NULL) {
3340 switch (list->type) {
3341 case XPATH_UNDEFINED:
3342 xmlGenericError(xmlGenericErrorContext,
3343 "%s: no such node\n", arg);
3344 break;
3345 case XPATH_NODESET:{
3346 int indx;
3347
Daniel Veillarda6825e82001-11-07 13:33:59 +00003348 if (list->nodesetval == NULL)
3349 break;
3350
Daniel Veillard78d12092001-10-11 09:12:24 +00003351 for (indx = 0;
3352 indx < list->nodesetval->nodeNr;
3353 indx++) {
3354 if (i > 0)
Daniel Veillard321be0c2002-10-08 21:26:42 +00003355 fprintf(ctxt->output, " -------\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00003356 xmlShellCat(ctxt, NULL,
3357 list->nodesetval->
3358 nodeTab[indx], NULL);
3359 }
3360 break;
3361 }
3362 case XPATH_BOOLEAN:
3363 xmlGenericError(xmlGenericErrorContext,
3364 "%s is a Boolean\n", arg);
3365 break;
3366 case XPATH_NUMBER:
3367 xmlGenericError(xmlGenericErrorContext,
3368 "%s is a number\n", arg);
3369 break;
3370 case XPATH_STRING:
3371 xmlGenericError(xmlGenericErrorContext,
3372 "%s is a string\n", arg);
3373 break;
3374 case XPATH_POINT:
3375 xmlGenericError(xmlGenericErrorContext,
3376 "%s is a point\n", arg);
3377 break;
3378 case XPATH_RANGE:
3379 xmlGenericError(xmlGenericErrorContext,
3380 "%s is a range\n", arg);
3381 break;
3382 case XPATH_LOCATIONSET:
3383 xmlGenericError(xmlGenericErrorContext,
3384 "%s is a range\n", arg);
3385 break;
3386 case XPATH_USERS:
3387 xmlGenericError(xmlGenericErrorContext,
3388 "%s is user-defined\n", arg);
3389 break;
3390 case XPATH_XSLT_TREE:
3391 xmlGenericError(xmlGenericErrorContext,
3392 "%s is an XSLT value tree\n",
3393 arg);
3394 break;
3395 }
3396#ifdef LIBXML_XPATH_ENABLED
3397 xmlXPathFreeObject(list);
3398#endif
3399 } else {
3400 xmlGenericError(xmlGenericErrorContext,
3401 "%s: no such node\n", arg);
3402 }
3403 ctxt->pctxt->node = NULL;
3404 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003405#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00003406 } else {
3407 xmlGenericError(xmlGenericErrorContext,
3408 "Unknown command %s\n", command);
3409 }
3410 free(cmdline); /* not xmlFree here ! */
Daniel Veillard30663512008-02-21 22:31:55 +00003411 cmdline = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003412 }
3413#ifdef LIBXML_XPATH_ENABLED
3414 xmlXPathFreeContext(ctxt->pctxt);
3415#endif /* LIBXML_XPATH_ENABLED */
3416 if (ctxt->loaded) {
3417 xmlFreeDoc(ctxt->doc);
3418 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003419 if (ctxt->filename != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003420 xmlFree(ctxt->filename);
Owen Taylor3473f882001-02-23 17:55:21 +00003421 xmlFree(ctxt);
3422 if (cmdline != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003423 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003424}
3425
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00003426#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00003427#define bottom_debugXML
3428#include "elfgcchack.h"
Owen Taylor3473f882001-02-23 17:55:21 +00003429#endif /* LIBXML_DEBUG_ENABLED */