blob: 2335c4cb399a9de99048c9a6b988c33eb16f2456 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * debugXML.c : This is a set of routines used for debugging the tree
3 * produced by the XML parser.
4 *
5 * See Copyright for the status of this software.
6 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00007 * Daniel Veillard <daniel@veillard.com>
Owen Taylor3473f882001-02-23 17:55:21 +00008 */
9
Daniel Veillard34ce8be2002-03-18 19:37:11 +000010#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000011#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000012#ifdef LIBXML_DEBUG_ENABLED
13
Owen Taylor3473f882001-02-23 17:55:21 +000014#include <string.h>
15#ifdef HAVE_STDLIB_H
16#include <stdlib.h>
17#endif
18#ifdef HAVE_STRING_H
19#include <string.h>
20#endif
21#include <libxml/xmlmemory.h>
22#include <libxml/tree.h>
23#include <libxml/parser.h>
Daniel Veillard567e1b42001-08-01 15:53:47 +000024#include <libxml/parserInternals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000025#include <libxml/valid.h>
26#include <libxml/debugXML.h>
27#include <libxml/HTMLtree.h>
28#include <libxml/HTMLparser.h>
29#include <libxml/xmlerror.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000030#include <libxml/globals.h>
Daniel Veillard0ba59232002-02-10 13:20:39 +000031#include <libxml/xpathInternals.h>
Igor Zlatkovicc06bb622003-04-27 15:59:00 +000032#include <libxml/uri.h>
Daniel Veillard522bc602004-02-21 11:53:09 +000033#ifdef LIBXML_SCHEMAS_ENABLED
34#include <libxml/relaxng.h>
35#endif
Owen Taylor3473f882001-02-23 17:55:21 +000036
Daniel Veillard22cdb842004-10-04 14:09:17 +000037typedef struct _xmlDebugCtxt xmlDebugCtxt;
38typedef xmlDebugCtxt *xmlDebugCtxtPtr;
39struct _xmlDebugCtxt {
40 FILE *output; /* the output file */
41 char shift[101]; /* used for indenting */
42 int depth; /* current depth */
43 xmlDocPtr doc; /* current document */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000044 xmlNodePtr node; /* current node */
Daniel Veillard03a53c32004-10-26 16:06:51 +000045 xmlDictPtr dict; /* the doc dictionnary */
Daniel Veillard22cdb842004-10-04 14:09:17 +000046 int check; /* do just checkings */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000047 int errors; /* number of errors found */
Daniel Veillard03a53c32004-10-26 16:06:51 +000048 int nodict; /* if the document has no dictionnary */
Daniel Veillard22cdb842004-10-04 14:09:17 +000049};
50
51static void xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node);
52
53static void
54xmlCtxtDumpInitCtxt(xmlDebugCtxtPtr ctxt)
55{
56 int i;
57
58 ctxt->depth = 0;
59 ctxt->check = 0;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000060 ctxt->errors = 0;
Daniel Veillard22cdb842004-10-04 14:09:17 +000061 ctxt->output = stdout;
Daniel Veillard03a53c32004-10-26 16:06:51 +000062 ctxt->doc = NULL;
63 ctxt->node = NULL;
64 ctxt->dict = NULL;
Daniel Veillard6927b102004-10-27 17:29:04 +000065 ctxt->nodict = 0;
Daniel Veillard22cdb842004-10-04 14:09:17 +000066 for (i = 0; i < 100; i++)
67 ctxt->shift[i] = ' ';
68 ctxt->shift[100] = 0;
69}
70
71static void
William M. Brack9638d4c2004-10-15 18:25:33 +000072xmlCtxtDumpCleanCtxt(xmlDebugCtxtPtr ctxt ATTRIBUTE_UNUSED)
Daniel Veillard76821142004-10-09 20:39:04 +000073{
William M. Brack9638d4c2004-10-15 18:25:33 +000074 /* remove the ATTRIBUTE_UNUSED when this is added */
Daniel Veillard76821142004-10-09 20:39:04 +000075}
76
77/**
Daniel Veillard0d24b112004-10-11 12:28:34 +000078 * xmlNsCheckScope:
79 * @node: the node
80 * @ns: the namespace node
Daniel Veillard76821142004-10-09 20:39:04 +000081 *
Daniel Veillard0d24b112004-10-11 12:28:34 +000082 * Check that a given namespace is in scope on a node.
Daniel Veillard76821142004-10-09 20:39:04 +000083 *
Daniel Veillard0d24b112004-10-11 12:28:34 +000084 * Returns 1 if in scope, -1 in case of argument error,
85 * -2 if the namespace is not in scope, and -3 if not on
86 * an ancestor node.
Daniel Veillard76821142004-10-09 20:39:04 +000087 */
88static int
Daniel Veillard0d24b112004-10-11 12:28:34 +000089xmlNsCheckScope(xmlNodePtr node, xmlNsPtr ns)
Daniel Veillard76821142004-10-09 20:39:04 +000090{
Daniel Veillard0d24b112004-10-11 12:28:34 +000091 xmlNsPtr cur;
Daniel Veillard76821142004-10-09 20:39:04 +000092
Daniel Veillard0d24b112004-10-11 12:28:34 +000093 if ((node == NULL) || (ns == NULL))
94 return(-1);
95
96 if ((node->type != XML_ELEMENT_NODE) &&
97 (node->type != XML_ATTRIBUTE_NODE) &&
98 (node->type != XML_DOCUMENT_NODE) &&
99 (node->type != XML_TEXT_NODE) &&
100 (node->type != XML_HTML_DOCUMENT_NODE) &&
101 (node->type != XML_XINCLUDE_START))
102 return(-2);
103
104 while ((node != NULL) &&
105 ((node->type == XML_ELEMENT_NODE) ||
106 (node->type == XML_ATTRIBUTE_NODE) ||
107 (node->type == XML_TEXT_NODE) ||
108 (node->type == XML_XINCLUDE_START))) {
109 if ((node->type == XML_ELEMENT_NODE) ||
110 (node->type == XML_XINCLUDE_START)) {
111 cur = node->nsDef;
112 while (cur != NULL) {
113 if (cur == ns)
114 return(1);
115 if (xmlStrEqual(cur->prefix, ns->prefix))
116 return(-2);
117 cur = cur->next;
118 }
Daniel Veillard76821142004-10-09 20:39:04 +0000119 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000120 node = node->parent;
Daniel Veillard76821142004-10-09 20:39:04 +0000121 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000122 /* the xml namespace may be declared on the document node */
123 if ((node != NULL) &&
124 ((node->type == XML_DOCUMENT_NODE) ||
125 (node->type == XML_HTML_DOCUMENT_NODE))) {
126 xmlNsPtr oldNs = ((xmlDocPtr) node)->oldNs;
127 if (oldNs == ns)
128 return(1);
129 }
130 return(-3);
Daniel Veillard76821142004-10-09 20:39:04 +0000131}
Daniel Veillard76821142004-10-09 20:39:04 +0000132
Daniel Veillard76821142004-10-09 20:39:04 +0000133static void
Daniel Veillard22cdb842004-10-04 14:09:17 +0000134xmlCtxtDumpSpaces(xmlDebugCtxtPtr ctxt)
135{
136 if (ctxt->check)
137 return;
138 if ((ctxt->output != NULL) && (ctxt->depth > 0)) {
139 if (ctxt->depth < 50)
140 fprintf(ctxt->output, &ctxt->shift[100 - 2 * ctxt->depth]);
141 else
142 fprintf(ctxt->output, ctxt->shift);
143 }
144}
145
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000146/**
147 * xmlDebugErr:
148 * @ctxt: a debug context
149 * @error: the error code
150 *
151 * Handle a debug error.
152 */
153static void
154xmlDebugErr(xmlDebugCtxtPtr ctxt, int error, const char *msg)
155{
156 ctxt->errors++;
157 __xmlRaiseError(NULL, NULL, NULL,
158 NULL, ctxt->node, XML_FROM_CHECK,
159 error, XML_ERR_ERROR, NULL, 0,
160 NULL, NULL, NULL, 0, 0,
161 msg);
162}
163static void
164xmlDebugErr2(xmlDebugCtxtPtr ctxt, int error, const char *msg, int extra)
165{
166 ctxt->errors++;
167 __xmlRaiseError(NULL, NULL, NULL,
168 NULL, ctxt->node, XML_FROM_CHECK,
169 error, XML_ERR_ERROR, NULL, 0,
170 NULL, NULL, NULL, 0, 0,
171 msg, extra);
172}
173static void
Daniel Veillardc6095782004-10-15 14:50:10 +0000174xmlDebugErr3(xmlDebugCtxtPtr ctxt, int error, const char *msg, const char *extra)
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000175{
176 ctxt->errors++;
177 __xmlRaiseError(NULL, NULL, NULL,
178 NULL, ctxt->node, XML_FROM_CHECK,
179 error, XML_ERR_ERROR, NULL, 0,
180 NULL, NULL, NULL, 0, 0,
181 msg, extra);
182}
183
Daniel Veillard0d24b112004-10-11 12:28:34 +0000184/**
185 * xmlCtxtNsCheckScope:
186 * @ctxt: the debugging context
187 * @node: the node
188 * @ns: the namespace node
189 *
190 * Report if a given namespace is is not in scope.
191 */
192static void
193xmlCtxtNsCheckScope(xmlDebugCtxtPtr ctxt, xmlNodePtr node, xmlNsPtr ns)
194{
195 int ret;
196
197 ret = xmlNsCheckScope(node, ns);
198 if (ret == -2) {
199 if (ns->prefix == NULL)
200 xmlDebugErr(ctxt, XML_CHECK_NS_SCOPE,
201 "Reference to default namespace not in scope\n");
202 else
203 xmlDebugErr3(ctxt, XML_CHECK_NS_SCOPE,
204 "Reference to namespace '%s' not in scope\n",
205 (char *) ns->prefix);
206 }
207 if (ret == -3) {
208 if (ns->prefix == NULL)
209 xmlDebugErr(ctxt, XML_CHECK_NS_ANCESTOR,
210 "Reference to default namespace not on ancestor\n");
211 else
212 xmlDebugErr3(ctxt, XML_CHECK_NS_ANCESTOR,
213 "Reference to namespace '%s' not on ancestor\n",
214 (char *) ns->prefix);
215 }
216}
217
Daniel Veillardc6095782004-10-15 14:50:10 +0000218/**
219 * xmlCtxtCheckString:
220 * @ctxt: the debug context
221 * @str: the string
222 *
223 * Do debugging on the string, currently it just checks the UTF-8 content
224 */
225static void
226xmlCtxtCheckString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
227{
228 if (str == NULL) return;
229 if (ctxt->check) {
230 if (!xmlCheckUTF8(str)) {
Daniel Veillard03a53c32004-10-26 16:06:51 +0000231 xmlDebugErr3(ctxt, XML_CHECK_NOT_UTF8,
Daniel Veillardc6095782004-10-15 14:50:10 +0000232 "String is not UTF-8 %s", (const char *) str);
233 }
234 }
235}
236
Daniel Veillard03a53c32004-10-26 16:06:51 +0000237/**
238 * xmlCtxtCheckName:
239 * @ctxt: the debug context
240 * @name: the name
241 *
242 * Do debugging on the name, for example the dictionnary status and
243 * conformance to the Name production.
244 */
245static void
246xmlCtxtCheckName(xmlDebugCtxtPtr ctxt, const xmlChar * name)
247{
248 if (ctxt->check) {
249 if (name == NULL) {
250 xmlDebugErr(ctxt, XML_CHECK_NO_NAME, "Name is NULL");
251 return;
252 }
253 if (xmlValidateName(name, 0)) {
254 xmlDebugErr3(ctxt, XML_CHECK_NOT_NCNAME,
255 "Name is not an NCName '%s'", (const char *) name);
256 }
257 if ((ctxt->dict != NULL) &&
258 (!xmlDictOwns(ctxt->dict, name))) {
259 xmlDebugErr3(ctxt, XML_CHECK_OUTSIDE_DICT,
260 "Name is not from the document dictionnary '%s'",
261 (const char *) name);
262 }
263 }
264}
265
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000266static void
267xmlCtxtGenericNodeCheck(xmlDebugCtxtPtr ctxt, xmlNodePtr node) {
Daniel Veillard03a53c32004-10-26 16:06:51 +0000268 xmlDocPtr doc;
269 xmlDictPtr dict;
270
271 doc = node->doc;
272
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000273 if (node->parent == NULL)
274 xmlDebugErr(ctxt, XML_CHECK_NO_PARENT,
275 "Node has no parent\n");
Daniel Veillard03a53c32004-10-26 16:06:51 +0000276 if (node->doc == NULL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000277 xmlDebugErr(ctxt, XML_CHECK_NO_DOC,
278 "Node has no doc\n");
Daniel Veillard03a53c32004-10-26 16:06:51 +0000279 dict = NULL;
280 } else {
281 dict = doc->dict;
282 if ((dict == NULL) && (ctxt->nodict == 0)) {
Daniel Veillard6927b102004-10-27 17:29:04 +0000283#if 0
284 /* desactivated right now as it raises too many errors */
285 if (doc->type == XML_DOCUMENT_NODE)
286 xmlDebugErr(ctxt, XML_CHECK_NO_DICT,
287 "Document has no dictionnary\n");
288#endif
Daniel Veillard03a53c32004-10-26 16:06:51 +0000289 ctxt->nodict = 1;
290 }
291 if (ctxt->doc == NULL)
292 ctxt->doc = doc;
293
294 if (ctxt->dict == NULL) {
295 ctxt->dict = dict;
296 }
297 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000298 if ((node->parent != NULL) && (node->doc != node->parent->doc) &&
299 (!xmlStrEqual(node->name, BAD_CAST "pseudoroot")))
300 xmlDebugErr(ctxt, XML_CHECK_WRONG_DOC,
301 "Node doc differs from parent's one\n");
302 if (node->prev == NULL) {
303 if (node->type == XML_ATTRIBUTE_NODE) {
304 if ((node->parent != NULL) &&
305 (node != (xmlNodePtr) node->parent->properties))
306 xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
307 "Attr has no prev and not first of attr list\n");
308
309 } else if ((node->parent != NULL) && (node->parent->children != node))
310 xmlDebugErr(ctxt, XML_CHECK_NO_PREV,
311 "Node has no prev and not first of parent list\n");
312 } else {
313 if (node->prev->next != node)
314 xmlDebugErr(ctxt, XML_CHECK_WRONG_PREV,
315 "Node prev->next : back link wrong\n");
316 }
317 if (node->next == NULL) {
318 if ((node->parent != NULL) && (node->type != XML_ATTRIBUTE_NODE) &&
319 (node->parent->last != node))
320 xmlDebugErr(ctxt, XML_CHECK_NO_NEXT,
321 "Node has no next and not last of parent list\n");
322 } else {
323 if (node->next->prev != node)
324 xmlDebugErr(ctxt, XML_CHECK_WRONG_NEXT,
325 "Node next->prev : forward link wrong\n");
Daniel Veillard0d24b112004-10-11 12:28:34 +0000326 if (node->next->parent != node->parent)
327 xmlDebugErr(ctxt, XML_CHECK_WRONG_PARENT,
328 "Node next->prev : forward link wrong\n");
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000329 }
Daniel Veillard0d24b112004-10-11 12:28:34 +0000330 if (node->type == XML_ELEMENT_NODE) {
331 xmlNsPtr ns;
332
333 ns = node->nsDef;
334 while (ns != NULL) {
335 xmlCtxtNsCheckScope(ctxt, node, ns);
336 ns = ns->next;
337 }
338 if (node->ns != NULL)
339 xmlCtxtNsCheckScope(ctxt, node, node->ns);
340 } else if (node->type == XML_ATTRIBUTE_NODE) {
341 if (node->ns != NULL)
342 xmlCtxtNsCheckScope(ctxt, node, node->ns);
343 }
344
Daniel Veillardc6095782004-10-15 14:50:10 +0000345 if ((node->type != XML_ELEMENT_NODE) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000346 (node->type != XML_ATTRIBUTE_NODE) &&
Daniel Veillardf2e066a2005-06-30 13:04:44 +0000347 (node->type != XML_ELEMENT_DECL) &&
William M. Brack9638d4c2004-10-15 18:25:33 +0000348 (node->type != XML_ATTRIBUTE_DECL) &&
349 (node->type != XML_DTD_NODE) &&
350 (node->type != XML_HTML_DOCUMENT_NODE) &&
351 (node->type != XML_DOCUMENT_NODE)) {
Daniel Veillardc6095782004-10-15 14:50:10 +0000352 if (node->content != NULL)
William M. Brack9638d4c2004-10-15 18:25:33 +0000353 xmlCtxtCheckString(ctxt, (const xmlChar *) node->content);
Daniel Veillardc6095782004-10-15 14:50:10 +0000354 }
Daniel Veillard03a53c32004-10-26 16:06:51 +0000355 switch (node->type) {
356 case XML_ELEMENT_NODE:
357 case XML_ATTRIBUTE_NODE:
358 xmlCtxtCheckName(ctxt, node->name);
359 break;
360 case XML_TEXT_NODE:
361 if ((node->name == xmlStringText) ||
362 (node->name == xmlStringTextNoenc))
363 break;
364 /* some case of entity substitution can lead to this */
365 if ((ctxt->dict != NULL) &&
Daniel Veillard6927b102004-10-27 17:29:04 +0000366 (node->name == xmlDictLookup(ctxt->dict, BAD_CAST "nbktext",
367 7)))
Daniel Veillard03a53c32004-10-26 16:06:51 +0000368 break;
369
370 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
371 "Text node has wrong name '%s'",
372 (const char *) node->name);
373 break;
374 case XML_COMMENT_NODE:
375 if (node->name == xmlStringComment)
376 break;
377 xmlDebugErr3(ctxt, XML_CHECK_WRONG_NAME,
378 "Comment node has wrong name '%s'",
379 (const char *) node->name);
380 break;
381 case XML_PI_NODE:
382 xmlCtxtCheckName(ctxt, node->name);
383 break;
384 case XML_CDATA_SECTION_NODE:
385 if (node->name == NULL)
386 break;
387 xmlDebugErr3(ctxt, XML_CHECK_NAME_NOT_NULL,
388 "CData section has non NULL name '%s'",
389 (const char *) node->name);
390 break;
391 case XML_ENTITY_REF_NODE:
392 case XML_ENTITY_NODE:
393 case XML_DOCUMENT_TYPE_NODE:
394 case XML_DOCUMENT_FRAG_NODE:
395 case XML_NOTATION_NODE:
396 case XML_DTD_NODE:
397 case XML_ELEMENT_DECL:
398 case XML_ATTRIBUTE_DECL:
399 case XML_ENTITY_DECL:
400 case XML_NAMESPACE_DECL:
401 case XML_XINCLUDE_START:
402 case XML_XINCLUDE_END:
403#ifdef LIBXML_DOCB_ENABLED
404 case XML_DOCB_DOCUMENT_NODE:
405#endif
406 case XML_DOCUMENT_NODE:
407 case XML_HTML_DOCUMENT_NODE:
408 break;
409 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000410}
411
Daniel Veillard22cdb842004-10-04 14:09:17 +0000412static void
413xmlCtxtDumpString(xmlDebugCtxtPtr ctxt, const xmlChar * str)
414{
415 int i;
416
Daniel Veillardc6095782004-10-15 14:50:10 +0000417 if (ctxt->check) {
Daniel Veillard22cdb842004-10-04 14:09:17 +0000418 return;
Daniel Veillardc6095782004-10-15 14:50:10 +0000419 }
Daniel Veillard22cdb842004-10-04 14:09:17 +0000420 /* TODO: check UTF8 content of the string */
421 if (str == NULL) {
422 fprintf(ctxt->output, "(NULL)");
423 return;
424 }
425 for (i = 0; i < 40; i++)
426 if (str[i] == 0)
427 return;
428 else if (IS_BLANK_CH(str[i]))
429 fputc(' ', ctxt->output);
430 else if (str[i] >= 0x80)
431 fprintf(ctxt->output, "#%X", str[i]);
432 else
433 fputc(str[i], ctxt->output);
434 fprintf(ctxt->output, "...");
435}
436
437static void
438xmlCtxtDumpDtdNode(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
439{
440 xmlCtxtDumpSpaces(ctxt);
441
442 if (dtd == NULL) {
443 if (!ctxt->check)
444 fprintf(ctxt->output, "DTD node is NULL\n");
445 return;
446 }
447
448 if (dtd->type != XML_DTD_NODE) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000449 xmlDebugErr(ctxt, XML_CHECK_NOT_DTD,
450 "Node is not a DTD");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000451 return;
452 }
453 if (!ctxt->check) {
454 if (dtd->name != NULL)
455 fprintf(ctxt->output, "DTD(%s)", (char *) dtd->name);
456 else
457 fprintf(ctxt->output, "DTD");
458 if (dtd->ExternalID != NULL)
459 fprintf(ctxt->output, ", PUBLIC %s", (char *) dtd->ExternalID);
460 if (dtd->SystemID != NULL)
461 fprintf(ctxt->output, ", SYSTEM %s", (char *) dtd->SystemID);
462 fprintf(ctxt->output, "\n");
463 }
464 /*
465 * Do a bit of checking
466 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000467 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000468}
469
470static void
471xmlCtxtDumpAttrDecl(xmlDebugCtxtPtr ctxt, xmlAttributePtr attr)
472{
473 xmlCtxtDumpSpaces(ctxt);
474
475 if (attr == NULL) {
476 if (!ctxt->check)
477 fprintf(ctxt->output, "Attribute declaration is NULL\n");
478 return;
479 }
480 if (attr->type != XML_ATTRIBUTE_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000481 xmlDebugErr(ctxt, XML_CHECK_NOT_ATTR_DECL,
482 "Node is not an attribute declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000483 return;
484 }
485 if (attr->name != NULL) {
486 if (!ctxt->check)
487 fprintf(ctxt->output, "ATTRDECL(%s)", (char *) attr->name);
488 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000489 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
490 "Node attribute declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000491 if (attr->elem != NULL) {
492 if (!ctxt->check)
493 fprintf(ctxt->output, " for %s", (char *) attr->elem);
494 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000495 xmlDebugErr(ctxt, XML_CHECK_NO_ELEM,
496 "Node attribute declaration has no element name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000497 if (!ctxt->check) {
498 switch (attr->atype) {
499 case XML_ATTRIBUTE_CDATA:
500 fprintf(ctxt->output, " CDATA");
501 break;
502 case XML_ATTRIBUTE_ID:
503 fprintf(ctxt->output, " ID");
504 break;
505 case XML_ATTRIBUTE_IDREF:
506 fprintf(ctxt->output, " IDREF");
507 break;
508 case XML_ATTRIBUTE_IDREFS:
509 fprintf(ctxt->output, " IDREFS");
510 break;
511 case XML_ATTRIBUTE_ENTITY:
512 fprintf(ctxt->output, " ENTITY");
513 break;
514 case XML_ATTRIBUTE_ENTITIES:
515 fprintf(ctxt->output, " ENTITIES");
516 break;
517 case XML_ATTRIBUTE_NMTOKEN:
518 fprintf(ctxt->output, " NMTOKEN");
519 break;
520 case XML_ATTRIBUTE_NMTOKENS:
521 fprintf(ctxt->output, " NMTOKENS");
522 break;
523 case XML_ATTRIBUTE_ENUMERATION:
524 fprintf(ctxt->output, " ENUMERATION");
525 break;
526 case XML_ATTRIBUTE_NOTATION:
527 fprintf(ctxt->output, " NOTATION ");
528 break;
529 }
530 if (attr->tree != NULL) {
531 int indx;
532 xmlEnumerationPtr cur = attr->tree;
533
534 for (indx = 0; indx < 5; indx++) {
535 if (indx != 0)
536 fprintf(ctxt->output, "|%s", (char *) cur->name);
537 else
538 fprintf(ctxt->output, " (%s", (char *) cur->name);
539 cur = cur->next;
540 if (cur == NULL)
541 break;
542 }
543 if (cur == NULL)
544 fprintf(ctxt->output, ")");
545 else
546 fprintf(ctxt->output, "...)");
547 }
548 switch (attr->def) {
549 case XML_ATTRIBUTE_NONE:
550 break;
551 case XML_ATTRIBUTE_REQUIRED:
552 fprintf(ctxt->output, " REQUIRED");
553 break;
554 case XML_ATTRIBUTE_IMPLIED:
555 fprintf(ctxt->output, " IMPLIED");
556 break;
557 case XML_ATTRIBUTE_FIXED:
558 fprintf(ctxt->output, " FIXED");
559 break;
560 }
561 if (attr->defaultValue != NULL) {
562 fprintf(ctxt->output, "\"");
563 xmlCtxtDumpString(ctxt, attr->defaultValue);
564 fprintf(ctxt->output, "\"");
565 }
566 fprintf(ctxt->output, "\n");
567 }
568
569 /*
570 * Do a bit of checking
571 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000572 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000573}
574
575static void
576xmlCtxtDumpElemDecl(xmlDebugCtxtPtr ctxt, xmlElementPtr elem)
577{
578 xmlCtxtDumpSpaces(ctxt);
579
580 if (elem == NULL) {
581 if (!ctxt->check)
582 fprintf(ctxt->output, "Element declaration is NULL\n");
583 return;
584 }
585 if (elem->type != XML_ELEMENT_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000586 xmlDebugErr(ctxt, XML_CHECK_NOT_ELEM_DECL,
587 "Node is not an element declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000588 return;
589 }
590 if (elem->name != NULL) {
591 if (!ctxt->check) {
592 fprintf(ctxt->output, "ELEMDECL(");
593 xmlCtxtDumpString(ctxt, elem->name);
594 fprintf(ctxt->output, ")");
595 }
596 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000597 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
598 "Element declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000599 if (!ctxt->check) {
600 switch (elem->etype) {
601 case XML_ELEMENT_TYPE_UNDEFINED:
602 fprintf(ctxt->output, ", UNDEFINED");
603 break;
604 case XML_ELEMENT_TYPE_EMPTY:
605 fprintf(ctxt->output, ", EMPTY");
606 break;
607 case XML_ELEMENT_TYPE_ANY:
608 fprintf(ctxt->output, ", ANY");
609 break;
610 case XML_ELEMENT_TYPE_MIXED:
611 fprintf(ctxt->output, ", MIXED ");
612 break;
613 case XML_ELEMENT_TYPE_ELEMENT:
614 fprintf(ctxt->output, ", MIXED ");
615 break;
616 }
617 if ((elem->type != XML_ELEMENT_NODE) && (elem->content != NULL)) {
618 char buf[5001];
619
620 buf[0] = 0;
621 xmlSnprintfElementContent(buf, 5000, elem->content, 1);
622 buf[5000] = 0;
623 fprintf(ctxt->output, "%s", buf);
624 }
625 fprintf(ctxt->output, "\n");
626 }
627
628 /*
629 * Do a bit of checking
630 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000631 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) elem);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000632}
633
634static void
635xmlCtxtDumpEntityDecl(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
636{
637 xmlCtxtDumpSpaces(ctxt);
638
639 if (ent == NULL) {
640 if (!ctxt->check)
641 fprintf(ctxt->output, "Entity declaration is NULL\n");
642 return;
643 }
644 if (ent->type != XML_ENTITY_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000645 xmlDebugErr(ctxt, XML_CHECK_NOT_ENTITY_DECL,
646 "Node is not an entity declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000647 return;
648 }
649 if (ent->name != NULL) {
650 if (!ctxt->check) {
651 fprintf(ctxt->output, "ENTITYDECL(");
652 xmlCtxtDumpString(ctxt, ent->name);
653 fprintf(ctxt->output, ")");
654 }
655 } else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000656 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
657 "Entity declaration has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000658 if (!ctxt->check) {
659 switch (ent->etype) {
660 case XML_INTERNAL_GENERAL_ENTITY:
661 fprintf(ctxt->output, ", internal\n");
662 break;
663 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
664 fprintf(ctxt->output, ", external parsed\n");
665 break;
666 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
667 fprintf(ctxt->output, ", unparsed\n");
668 break;
669 case XML_INTERNAL_PARAMETER_ENTITY:
670 fprintf(ctxt->output, ", parameter\n");
671 break;
672 case XML_EXTERNAL_PARAMETER_ENTITY:
673 fprintf(ctxt->output, ", external parameter\n");
674 break;
675 case XML_INTERNAL_PREDEFINED_ENTITY:
676 fprintf(ctxt->output, ", predefined\n");
677 break;
678 }
679 if (ent->ExternalID) {
680 xmlCtxtDumpSpaces(ctxt);
681 fprintf(ctxt->output, " ExternalID=%s\n",
682 (char *) ent->ExternalID);
683 }
684 if (ent->SystemID) {
685 xmlCtxtDumpSpaces(ctxt);
686 fprintf(ctxt->output, " SystemID=%s\n",
687 (char *) ent->SystemID);
688 }
689 if (ent->URI != NULL) {
690 xmlCtxtDumpSpaces(ctxt);
691 fprintf(ctxt->output, " URI=%s\n", (char *) ent->URI);
692 }
693 if (ent->content) {
694 xmlCtxtDumpSpaces(ctxt);
695 fprintf(ctxt->output, " content=");
696 xmlCtxtDumpString(ctxt, ent->content);
697 fprintf(ctxt->output, "\n");
698 }
699 }
700
701 /*
702 * Do a bit of checking
703 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000704 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) ent);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000705}
706
707static void
708xmlCtxtDumpNamespace(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
709{
710 xmlCtxtDumpSpaces(ctxt);
711
712 if (ns == NULL) {
713 if (!ctxt->check)
714 fprintf(ctxt->output, "namespace node is NULL\n");
715 return;
716 }
717 if (ns->type != XML_NAMESPACE_DECL) {
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000718 xmlDebugErr(ctxt, XML_CHECK_NOT_NS_DECL,
719 "Node is not a namespace declaration");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000720 return;
721 }
722 if (ns->href == NULL) {
723 if (ns->prefix != NULL)
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000724 xmlDebugErr3(ctxt, XML_CHECK_NO_HREF,
725 "Incomplete namespace %s href=NULL\n",
Daniel Veillard22cdb842004-10-04 14:09:17 +0000726 (char *) ns->prefix);
727 else
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000728 xmlDebugErr(ctxt, XML_CHECK_NO_HREF,
729 "Incomplete default namespace href=NULL\n");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000730 } else {
731 if (!ctxt->check) {
732 if (ns->prefix != NULL)
733 fprintf(ctxt->output, "namespace %s href=",
734 (char *) ns->prefix);
735 else
736 fprintf(ctxt->output, "default namespace href=");
737
738 xmlCtxtDumpString(ctxt, ns->href);
739 fprintf(ctxt->output, "\n");
740 }
741 }
742}
743
744static void
745xmlCtxtDumpNamespaceList(xmlDebugCtxtPtr ctxt, xmlNsPtr ns)
746{
747 while (ns != NULL) {
748 xmlCtxtDumpNamespace(ctxt, ns);
749 ns = ns->next;
750 }
751}
752
753static void
754xmlCtxtDumpEntity(xmlDebugCtxtPtr ctxt, xmlEntityPtr ent)
755{
756 xmlCtxtDumpSpaces(ctxt);
757
758 if (ent == NULL) {
759 if (!ctxt->check)
760 fprintf(ctxt->output, "Entity is NULL\n");
761 return;
762 }
763 if (!ctxt->check) {
764 switch (ent->etype) {
765 case XML_INTERNAL_GENERAL_ENTITY:
766 fprintf(ctxt->output, "INTERNAL_GENERAL_ENTITY ");
767 break;
768 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
769 fprintf(ctxt->output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
770 break;
771 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
772 fprintf(ctxt->output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
773 break;
774 case XML_INTERNAL_PARAMETER_ENTITY:
775 fprintf(ctxt->output, "INTERNAL_PARAMETER_ENTITY ");
776 break;
777 case XML_EXTERNAL_PARAMETER_ENTITY:
778 fprintf(ctxt->output, "EXTERNAL_PARAMETER_ENTITY ");
779 break;
780 default:
781 fprintf(ctxt->output, "ENTITY_%d ! ", (int) ent->etype);
782 }
783 fprintf(ctxt->output, "%s\n", ent->name);
784 if (ent->ExternalID) {
785 xmlCtxtDumpSpaces(ctxt);
786 fprintf(ctxt->output, "ExternalID=%s\n",
787 (char *) ent->ExternalID);
788 }
789 if (ent->SystemID) {
790 xmlCtxtDumpSpaces(ctxt);
791 fprintf(ctxt->output, "SystemID=%s\n", (char *) ent->SystemID);
792 }
793 if (ent->URI) {
794 xmlCtxtDumpSpaces(ctxt);
795 fprintf(ctxt->output, "URI=%s\n", (char *) ent->URI);
796 }
797 if (ent->content) {
798 xmlCtxtDumpSpaces(ctxt);
799 fprintf(ctxt->output, "content=");
800 xmlCtxtDumpString(ctxt, ent->content);
801 fprintf(ctxt->output, "\n");
802 }
803 }
804}
805
806/**
807 * xmlCtxtDumpAttr:
808 * @output: the FILE * for the output
809 * @attr: the attribute
810 * @depth: the indentation level.
811 *
812 * Dumps debug information for the attribute
813 */
814static void
815xmlCtxtDumpAttr(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
816{
817 xmlCtxtDumpSpaces(ctxt);
818
819 if (attr == NULL) {
820 if (!ctxt->check)
821 fprintf(ctxt->output, "Attr is NULL");
822 return;
823 }
824 if (!ctxt->check) {
825 fprintf(ctxt->output, "ATTRIBUTE ");
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000826 xmlCtxtDumpString(ctxt, attr->name);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000827 fprintf(ctxt->output, "\n");
828 if (attr->children != NULL) {
829 ctxt->depth++;
830 xmlCtxtDumpNodeList(ctxt, attr->children);
831 ctxt->depth--;
832 }
833 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000834 if (attr->name == NULL)
835 xmlDebugErr(ctxt, XML_CHECK_NO_NAME,
836 "Attribute has no name");
Daniel Veillard22cdb842004-10-04 14:09:17 +0000837
838 /*
839 * Do a bit of checking
840 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000841 xmlCtxtGenericNodeCheck(ctxt, (xmlNodePtr) attr);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000842}
843
844/**
845 * xmlCtxtDumpAttrList:
846 * @output: the FILE * for the output
847 * @attr: the attribute list
848 * @depth: the indentation level.
849 *
850 * Dumps debug information for the attribute list
851 */
852static void
853xmlCtxtDumpAttrList(xmlDebugCtxtPtr ctxt, xmlAttrPtr attr)
854{
855 while (attr != NULL) {
856 xmlCtxtDumpAttr(ctxt, attr);
857 attr = attr->next;
858 }
859}
860
861/**
862 * xmlCtxtDumpOneNode:
863 * @output: the FILE * for the output
864 * @node: the node
865 * @depth: the indentation level.
866 *
867 * Dumps debug information for the element node, it is not recursive
868 */
869static void
870xmlCtxtDumpOneNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
871{
872 if (node == NULL) {
873 if (!ctxt->check) {
874 xmlCtxtDumpSpaces(ctxt);
875 fprintf(ctxt->output, "node is NULL\n");
876 }
877 return;
878 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000879 ctxt->node = node;
880
Daniel Veillard22cdb842004-10-04 14:09:17 +0000881 switch (node->type) {
882 case XML_ELEMENT_NODE:
883 if (!ctxt->check) {
884 xmlCtxtDumpSpaces(ctxt);
885 fprintf(ctxt->output, "ELEMENT ");
886 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
887 xmlCtxtDumpString(ctxt, node->ns->prefix);
888 fprintf(ctxt->output, ":");
889 }
890 xmlCtxtDumpString(ctxt, node->name);
891 fprintf(ctxt->output, "\n");
892 }
893 break;
894 case XML_ATTRIBUTE_NODE:
895 if (!ctxt->check)
896 xmlCtxtDumpSpaces(ctxt);
897 fprintf(ctxt->output, "Error, ATTRIBUTE found here\n");
William M. Brack5a9c1fd2004-12-17 21:38:09 +0000898 xmlCtxtGenericNodeCheck(ctxt, node);
899 return;
Daniel Veillard22cdb842004-10-04 14:09:17 +0000900 case XML_TEXT_NODE:
901 if (!ctxt->check) {
902 xmlCtxtDumpSpaces(ctxt);
903 if (node->name == (const xmlChar *) xmlStringTextNoenc)
904 fprintf(ctxt->output, "TEXT no enc\n");
905 else
906 fprintf(ctxt->output, "TEXT\n");
907 }
908 break;
909 case XML_CDATA_SECTION_NODE:
910 if (!ctxt->check) {
911 xmlCtxtDumpSpaces(ctxt);
912 fprintf(ctxt->output, "CDATA_SECTION\n");
913 }
914 break;
915 case XML_ENTITY_REF_NODE:
916 if (!ctxt->check) {
917 xmlCtxtDumpSpaces(ctxt);
918 fprintf(ctxt->output, "ENTITY_REF(%s)\n",
919 (char *) node->name);
920 }
921 break;
922 case XML_ENTITY_NODE:
923 if (!ctxt->check) {
924 xmlCtxtDumpSpaces(ctxt);
925 fprintf(ctxt->output, "ENTITY\n");
926 }
927 break;
928 case XML_PI_NODE:
929 if (!ctxt->check) {
930 xmlCtxtDumpSpaces(ctxt);
931 fprintf(ctxt->output, "PI %s\n", (char *) node->name);
932 }
933 break;
934 case XML_COMMENT_NODE:
935 if (!ctxt->check) {
936 xmlCtxtDumpSpaces(ctxt);
937 fprintf(ctxt->output, "COMMENT\n");
938 }
939 break;
940 case XML_DOCUMENT_NODE:
941 case XML_HTML_DOCUMENT_NODE:
942 if (!ctxt->check) {
943 xmlCtxtDumpSpaces(ctxt);
944 }
William M. Brack5a9c1fd2004-12-17 21:38:09 +0000945 fprintf(ctxt->output, "Error, DOCUMENT found here\n");
946 xmlCtxtGenericNodeCheck(ctxt, node);
947 return;
Daniel Veillard22cdb842004-10-04 14:09:17 +0000948 case XML_DOCUMENT_TYPE_NODE:
949 if (!ctxt->check) {
950 xmlCtxtDumpSpaces(ctxt);
951 fprintf(ctxt->output, "DOCUMENT_TYPE\n");
952 }
953 break;
954 case XML_DOCUMENT_FRAG_NODE:
955 if (!ctxt->check) {
956 xmlCtxtDumpSpaces(ctxt);
957 fprintf(ctxt->output, "DOCUMENT_FRAG\n");
958 }
959 break;
960 case XML_NOTATION_NODE:
961 if (!ctxt->check) {
962 xmlCtxtDumpSpaces(ctxt);
963 fprintf(ctxt->output, "NOTATION\n");
964 }
965 break;
966 case XML_DTD_NODE:
967 xmlCtxtDumpDtdNode(ctxt, (xmlDtdPtr) node);
968 return;
969 case XML_ELEMENT_DECL:
970 xmlCtxtDumpElemDecl(ctxt, (xmlElementPtr) node);
971 return;
972 case XML_ATTRIBUTE_DECL:
973 xmlCtxtDumpAttrDecl(ctxt, (xmlAttributePtr) node);
974 return;
975 case XML_ENTITY_DECL:
976 xmlCtxtDumpEntityDecl(ctxt, (xmlEntityPtr) node);
977 return;
978 case XML_NAMESPACE_DECL:
979 xmlCtxtDumpNamespace(ctxt, (xmlNsPtr) node);
980 return;
981 case XML_XINCLUDE_START:
982 if (!ctxt->check) {
983 xmlCtxtDumpSpaces(ctxt);
984 fprintf(ctxt->output, "INCLUDE START\n");
985 }
986 return;
987 case XML_XINCLUDE_END:
988 if (!ctxt->check) {
989 xmlCtxtDumpSpaces(ctxt);
990 fprintf(ctxt->output, "INCLUDE END\n");
991 }
992 return;
993 default:
994 if (!ctxt->check)
995 xmlCtxtDumpSpaces(ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000996 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
997 "Unknown node type %d\n", node->type);
Daniel Veillard22cdb842004-10-04 14:09:17 +0000998 return;
999 }
1000 if (node->doc == NULL) {
1001 if (!ctxt->check) {
1002 xmlCtxtDumpSpaces(ctxt);
1003 }
1004 fprintf(ctxt->output, "PBM: doc == NULL !!!\n");
1005 }
1006 ctxt->depth++;
1007 if (node->nsDef != NULL)
1008 xmlCtxtDumpNamespaceList(ctxt, node->nsDef);
1009 if (node->properties != NULL)
1010 xmlCtxtDumpAttrList(ctxt, node->properties);
1011 if (node->type != XML_ENTITY_REF_NODE) {
1012 if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
1013 if (!ctxt->check) {
1014 xmlCtxtDumpSpaces(ctxt);
1015 fprintf(ctxt->output, "content=");
1016 xmlCtxtDumpString(ctxt, node->content);
1017 fprintf(ctxt->output, "\n");
1018 }
1019 }
1020 } else {
1021 xmlEntityPtr ent;
1022
1023 ent = xmlGetDocEntity(node->doc, node->name);
1024 if (ent != NULL)
1025 xmlCtxtDumpEntity(ctxt, ent);
1026 }
1027 ctxt->depth--;
1028
1029 /*
1030 * Do a bit of checking
1031 */
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001032 xmlCtxtGenericNodeCheck(ctxt, node);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001033}
1034
1035/**
1036 * xmlCtxtDumpNode:
1037 * @output: the FILE * for the output
1038 * @node: the node
1039 * @depth: the indentation level.
1040 *
1041 * Dumps debug information for the element node, it is recursive
1042 */
1043static void
1044xmlCtxtDumpNode(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1045{
1046 if (node == NULL) {
1047 if (!ctxt->check) {
1048 xmlCtxtDumpSpaces(ctxt);
1049 fprintf(ctxt->output, "node is NULL\n");
1050 }
1051 return;
1052 }
1053 xmlCtxtDumpOneNode(ctxt, node);
1054 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) {
1055 ctxt->depth++;
1056 xmlCtxtDumpNodeList(ctxt, node->children);
1057 ctxt->depth--;
1058 }
1059}
1060
1061/**
1062 * xmlCtxtDumpNodeList:
1063 * @output: the FILE * for the output
1064 * @node: the node list
1065 * @depth: the indentation level.
1066 *
1067 * Dumps debug information for the list of element node, it is recursive
1068 */
1069static void
1070xmlCtxtDumpNodeList(xmlDebugCtxtPtr ctxt, xmlNodePtr node)
1071{
1072 while (node != NULL) {
1073 xmlCtxtDumpNode(ctxt, node);
1074 node = node->next;
1075 }
1076}
1077
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001078static void
1079xmlCtxtDumpDocHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1080{
1081 if (doc == NULL) {
1082 if (!ctxt->check)
1083 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1084 return;
1085 }
1086 ctxt->node = (xmlNodePtr) doc;
1087
1088 switch (doc->type) {
1089 case XML_ELEMENT_NODE:
1090 xmlDebugErr(ctxt, XML_CHECK_FOUND_ELEMENT,
1091 "Misplaced ELEMENT node\n");
1092 break;
1093 case XML_ATTRIBUTE_NODE:
1094 xmlDebugErr(ctxt, XML_CHECK_FOUND_ATTRIBUTE,
1095 "Misplaced ATTRIBUTE node\n");
1096 break;
1097 case XML_TEXT_NODE:
1098 xmlDebugErr(ctxt, XML_CHECK_FOUND_TEXT,
1099 "Misplaced TEXT node\n");
1100 break;
1101 case XML_CDATA_SECTION_NODE:
1102 xmlDebugErr(ctxt, XML_CHECK_FOUND_CDATA,
1103 "Misplaced CDATA node\n");
1104 break;
1105 case XML_ENTITY_REF_NODE:
1106 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITYREF,
1107 "Misplaced ENTITYREF node\n");
1108 break;
1109 case XML_ENTITY_NODE:
1110 xmlDebugErr(ctxt, XML_CHECK_FOUND_ENTITY,
1111 "Misplaced ENTITY node\n");
1112 break;
1113 case XML_PI_NODE:
1114 xmlDebugErr(ctxt, XML_CHECK_FOUND_PI,
1115 "Misplaced PI node\n");
1116 break;
1117 case XML_COMMENT_NODE:
1118 xmlDebugErr(ctxt, XML_CHECK_FOUND_COMMENT,
1119 "Misplaced COMMENT node\n");
1120 break;
1121 case XML_DOCUMENT_NODE:
1122 if (!ctxt->check)
1123 fprintf(ctxt->output, "DOCUMENT\n");
1124 break;
1125 case XML_HTML_DOCUMENT_NODE:
1126 if (!ctxt->check)
1127 fprintf(ctxt->output, "HTML DOCUMENT\n");
1128 break;
1129 case XML_DOCUMENT_TYPE_NODE:
1130 xmlDebugErr(ctxt, XML_CHECK_FOUND_DOCTYPE,
1131 "Misplaced DOCTYPE node\n");
1132 break;
1133 case XML_DOCUMENT_FRAG_NODE:
1134 xmlDebugErr(ctxt, XML_CHECK_FOUND_FRAGMENT,
1135 "Misplaced FRAGMENT node\n");
1136 break;
1137 case XML_NOTATION_NODE:
1138 xmlDebugErr(ctxt, XML_CHECK_FOUND_NOTATION,
1139 "Misplaced NOTATION node\n");
1140 break;
1141 default:
1142 xmlDebugErr2(ctxt, XML_CHECK_UNKNOWN_NODE,
1143 "Unknown node type %d\n", doc->type);
1144 }
1145}
Daniel Veillard22cdb842004-10-04 14:09:17 +00001146
1147/**
1148 * xmlCtxtDumpDocumentHead:
1149 * @output: the FILE * for the output
1150 * @doc: the document
1151 *
1152 * Dumps debug information cncerning the document, not recursive
1153 */
1154static void
1155xmlCtxtDumpDocumentHead(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1156{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001157 if (doc == NULL) return;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001158 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001159 if (!ctxt->check) {
1160 if (doc->name != NULL) {
1161 fprintf(ctxt->output, "name=");
1162 xmlCtxtDumpString(ctxt, BAD_CAST doc->name);
1163 fprintf(ctxt->output, "\n");
1164 }
1165 if (doc->version != NULL) {
1166 fprintf(ctxt->output, "version=");
1167 xmlCtxtDumpString(ctxt, doc->version);
1168 fprintf(ctxt->output, "\n");
1169 }
1170 if (doc->encoding != NULL) {
1171 fprintf(ctxt->output, "encoding=");
1172 xmlCtxtDumpString(ctxt, doc->encoding);
1173 fprintf(ctxt->output, "\n");
1174 }
1175 if (doc->URL != NULL) {
1176 fprintf(ctxt->output, "URL=");
1177 xmlCtxtDumpString(ctxt, doc->URL);
1178 fprintf(ctxt->output, "\n");
1179 }
1180 if (doc->standalone)
1181 fprintf(ctxt->output, "standalone=true\n");
1182 }
1183 if (doc->oldNs != NULL)
1184 xmlCtxtDumpNamespaceList(ctxt, doc->oldNs);
1185}
1186
1187/**
1188 * xmlCtxtDumpDocument:
1189 * @output: the FILE * for the output
1190 * @doc: the document
1191 *
1192 * Dumps debug information for the document, it's recursive
1193 */
1194static void
1195xmlCtxtDumpDocument(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1196{
1197 if (doc == NULL) {
1198 if (!ctxt->check)
1199 fprintf(ctxt->output, "DOCUMENT == NULL !\n");
1200 return;
1201 }
1202 xmlCtxtDumpDocumentHead(ctxt, doc);
1203 if (((doc->type == XML_DOCUMENT_NODE) ||
1204 (doc->type == XML_HTML_DOCUMENT_NODE))
1205 && (doc->children != NULL)) {
1206 ctxt->depth++;
1207 xmlCtxtDumpNodeList(ctxt, doc->children);
1208 ctxt->depth--;
1209 }
1210}
1211
1212static void
1213xmlCtxtDumpEntityCallback(xmlEntityPtr cur, xmlDebugCtxtPtr ctxt)
1214{
1215 if (cur == NULL) {
1216 if (!ctxt->check)
1217 fprintf(ctxt->output, "Entity is NULL");
1218 return;
1219 }
1220 if (!ctxt->check) {
1221 fprintf(ctxt->output, "%s : ", (char *) cur->name);
1222 switch (cur->etype) {
1223 case XML_INTERNAL_GENERAL_ENTITY:
1224 fprintf(ctxt->output, "INTERNAL GENERAL, ");
1225 break;
1226 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1227 fprintf(ctxt->output, "EXTERNAL PARSED, ");
1228 break;
1229 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1230 fprintf(ctxt->output, "EXTERNAL UNPARSED, ");
1231 break;
1232 case XML_INTERNAL_PARAMETER_ENTITY:
1233 fprintf(ctxt->output, "INTERNAL PARAMETER, ");
1234 break;
1235 case XML_EXTERNAL_PARAMETER_ENTITY:
1236 fprintf(ctxt->output, "EXTERNAL PARAMETER, ");
1237 break;
1238 default:
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001239 xmlDebugErr2(ctxt, XML_CHECK_ENTITY_TYPE,
1240 "Unknown entity type %d\n", cur->etype);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001241 }
1242 if (cur->ExternalID != NULL)
1243 fprintf(ctxt->output, "ID \"%s\"", (char *) cur->ExternalID);
1244 if (cur->SystemID != NULL)
1245 fprintf(ctxt->output, "SYSTEM \"%s\"", (char *) cur->SystemID);
1246 if (cur->orig != NULL)
1247 fprintf(ctxt->output, "\n orig \"%s\"", (char *) cur->orig);
1248 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL))
1249 fprintf(ctxt->output, "\n content \"%s\"",
1250 (char *) cur->content);
1251 fprintf(ctxt->output, "\n");
1252 }
1253}
1254
1255/**
1256 * xmlCtxtDumpEntities:
1257 * @output: the FILE * for the output
1258 * @doc: the document
1259 *
1260 * Dumps debug information for all the entities in use by the document
1261 */
1262static void
1263xmlCtxtDumpEntities(xmlDebugCtxtPtr ctxt, xmlDocPtr doc)
1264{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001265 if (doc == NULL) return;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001266 xmlCtxtDumpDocHead(ctxt, doc);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001267 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
1268 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1269 doc->intSubset->entities;
1270
1271 if (!ctxt->check)
1272 fprintf(ctxt->output, "Entities in internal subset\n");
1273 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1274 ctxt);
1275 } else
1276 fprintf(ctxt->output, "No entities in internal subset\n");
1277 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
1278 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1279 doc->extSubset->entities;
1280
1281 if (!ctxt->check)
1282 fprintf(ctxt->output, "Entities in external subset\n");
1283 xmlHashScan(table, (xmlHashScanner) xmlCtxtDumpEntityCallback,
1284 ctxt);
1285 } else if (!ctxt->check)
1286 fprintf(ctxt->output, "No entities in external subset\n");
1287}
1288
1289/**
1290 * xmlCtxtDumpDTD:
1291 * @output: the FILE * for the output
1292 * @dtd: the DTD
1293 *
1294 * Dumps debug information for the DTD
1295 */
1296static void
1297xmlCtxtDumpDTD(xmlDebugCtxtPtr ctxt, xmlDtdPtr dtd)
1298{
1299 if (dtd == NULL) {
1300 if (!ctxt->check)
1301 fprintf(ctxt->output, "DTD is NULL\n");
1302 return;
1303 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001304 xmlCtxtDumpDtdNode(ctxt, dtd);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001305 if (dtd->children == NULL)
1306 fprintf(ctxt->output, " DTD is empty\n");
1307 else {
1308 ctxt->depth++;
1309 xmlCtxtDumpNodeList(ctxt, dtd->children);
1310 ctxt->depth--;
1311 }
1312}
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001313
Daniel Veillard22cdb842004-10-04 14:09:17 +00001314/************************************************************************
1315 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001316 * Public entry points for dump *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001317 * *
1318 ************************************************************************/
1319
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001320/**
1321 * xmlDebugDumpString:
1322 * @output: the FILE * for the output
1323 * @str: the string
1324 *
1325 * Dumps informations about the string, shorten it if necessary
1326 */
1327void
1328xmlDebugDumpString(FILE * output, const xmlChar * str)
1329{
Owen Taylor3473f882001-02-23 17:55:21 +00001330 int i;
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001331
Daniel Veillard7db38712002-02-07 16:39:11 +00001332 if (output == NULL)
1333 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00001334 if (str == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001335 fprintf(output, "(NULL)");
1336 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001337 }
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001338 for (i = 0; i < 40; i++)
1339 if (str[i] == 0)
1340 return;
William M. Brack76e95df2003-10-18 16:20:14 +00001341 else if (IS_BLANK_CH(str[i]))
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001342 fputc(' ', output);
1343 else if (str[i] >= 0x80)
1344 fprintf(output, "#%X", str[i]);
1345 else
1346 fputc(str[i], output);
Owen Taylor3473f882001-02-23 17:55:21 +00001347 fprintf(output, "...");
1348}
1349
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001350/**
1351 * xmlDebugDumpAttr:
1352 * @output: the FILE * for the output
1353 * @attr: the attribute
1354 * @depth: the indentation level.
1355 *
1356 * Dumps debug information for the attribute
1357 */
1358void
1359xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
Daniel Veillard22cdb842004-10-04 14:09:17 +00001360 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001361
Daniel Veillarda82b1822004-11-08 16:24:57 +00001362 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001363 xmlCtxtDumpInitCtxt(&ctxt);
1364 ctxt.output = output;
1365 ctxt.depth = depth;
1366 xmlCtxtDumpAttr(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001367 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard22cdb842004-10-04 14:09:17 +00001368}
Owen Taylor3473f882001-02-23 17:55:21 +00001369
Owen Taylor3473f882001-02-23 17:55:21 +00001370
Daniel Veillard22cdb842004-10-04 14:09:17 +00001371/**
1372 * xmlDebugDumpEntities:
1373 * @output: the FILE * for the output
1374 * @doc: the document
1375 *
1376 * Dumps debug information for all the entities in use by the document
1377 */
1378void
1379xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
1380{
1381 xmlDebugCtxt ctxt;
1382
Daniel Veillarda82b1822004-11-08 16:24:57 +00001383 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001384 xmlCtxtDumpInitCtxt(&ctxt);
1385 ctxt.output = output;
1386 xmlCtxtDumpEntities(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001387 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001388}
1389
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001390/**
1391 * xmlDebugDumpAttrList:
1392 * @output: the FILE * for the output
1393 * @attr: the attribute list
1394 * @depth: the indentation level.
1395 *
1396 * Dumps debug information for the attribute list
1397 */
1398void
1399xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
1400{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001401 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 ctxt.depth = depth;
1407 xmlCtxtDumpAttrList(&ctxt, attr);
Daniel Veillard76821142004-10-09 20:39:04 +00001408 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001409}
1410
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001411/**
1412 * xmlDebugDumpOneNode:
1413 * @output: the FILE * for the output
1414 * @node: the node
1415 * @depth: the indentation level.
1416 *
1417 * Dumps debug information for the element node, it is not recursive
1418 */
1419void
1420xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
1421{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001422 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001423
Daniel Veillarda82b1822004-11-08 16:24:57 +00001424 if (output == NULL) return;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001425 xmlCtxtDumpInitCtxt(&ctxt);
1426 ctxt.output = output;
1427 ctxt.depth = depth;
1428 xmlCtxtDumpOneNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001429 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001430}
1431
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001432/**
1433 * xmlDebugDumpNode:
1434 * @output: the FILE * for the output
1435 * @node: the node
1436 * @depth: the indentation level.
1437 *
1438 * Dumps debug information for the element node, it is recursive
1439 */
1440void
1441xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
1442{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001443 xmlDebugCtxt ctxt;
1444
Daniel Veillard7db38712002-02-07 16:39:11 +00001445 if (output == NULL)
1446 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001447 xmlCtxtDumpInitCtxt(&ctxt);
1448 ctxt.output = output;
1449 ctxt.depth = depth;
1450 xmlCtxtDumpNode(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001451 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001452}
1453
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001454/**
1455 * xmlDebugDumpNodeList:
1456 * @output: the FILE * for the output
1457 * @node: the node list
1458 * @depth: the indentation level.
1459 *
1460 * Dumps debug information for the list of element node, it is recursive
1461 */
1462void
1463xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
1464{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001465 xmlDebugCtxt ctxt;
1466
Daniel Veillard7db38712002-02-07 16:39:11 +00001467 if (output == NULL)
1468 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001469 xmlCtxtDumpInitCtxt(&ctxt);
1470 ctxt.output = output;
1471 ctxt.depth = depth;
1472 xmlCtxtDumpNodeList(&ctxt, node);
Daniel Veillard76821142004-10-09 20:39:04 +00001473 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001474}
1475
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001476/**
1477 * xmlDebugDumpDocumentHead:
1478 * @output: the FILE * for the output
1479 * @doc: the document
1480 *
1481 * Dumps debug information cncerning the document, not recursive
1482 */
1483void
1484xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
1485{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001486 xmlDebugCtxt ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00001487
Daniel Veillard22cdb842004-10-04 14:09:17 +00001488 if (output == NULL)
1489 output = stdout;
1490 xmlCtxtDumpInitCtxt(&ctxt);
1491 ctxt.output = output;
1492 xmlCtxtDumpDocumentHead(&ctxt, doc);
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 * xmlDebugDumpDocument:
1498 * @output: the FILE * for the output
1499 * @doc: the document
1500 *
1501 * Dumps debug information for the document, it's recursive
1502 */
1503void
1504xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
1505{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001506 xmlDebugCtxt ctxt;
1507
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001508 if (output == NULL)
Daniel Veillard22cdb842004-10-04 14:09:17 +00001509 output = stdout;
1510 xmlCtxtDumpInitCtxt(&ctxt);
1511 ctxt.output = output;
1512 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001513 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001514}
Owen Taylor3473f882001-02-23 17:55:21 +00001515
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001516/**
1517 * xmlDebugDumpDTD:
1518 * @output: the FILE * for the output
1519 * @dtd: the DTD
1520 *
1521 * Dumps debug information for the DTD
1522 */
1523void
1524xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
1525{
Daniel Veillard22cdb842004-10-04 14:09:17 +00001526 xmlDebugCtxt ctxt;
1527
Daniel Veillard7db38712002-02-07 16:39:11 +00001528 if (output == NULL)
1529 output = stdout;
Daniel Veillard22cdb842004-10-04 14:09:17 +00001530 xmlCtxtDumpInitCtxt(&ctxt);
1531 ctxt.output = output;
1532 xmlCtxtDumpDTD(&ctxt, dtd);
Daniel Veillard76821142004-10-09 20:39:04 +00001533 xmlCtxtDumpCleanCtxt(&ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001534}
1535
Daniel Veillard22cdb842004-10-04 14:09:17 +00001536/************************************************************************
1537 * *
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001538 * Public entry points for checkings *
1539 * *
1540 ************************************************************************/
1541
1542/**
1543 * xmlDebugCheckDocument:
1544 * @output: the FILE * for the output
1545 * @doc: the document
1546 *
1547 * Check the document for potential content problems, and output
1548 * the errors to @output
1549 *
1550 * Returns the number of errors found
1551 */
1552int
1553xmlDebugCheckDocument(FILE * output, xmlDocPtr doc)
1554{
1555 xmlDebugCtxt ctxt;
1556
1557 if (output == NULL)
1558 output = stdout;
1559 xmlCtxtDumpInitCtxt(&ctxt);
1560 ctxt.output = output;
1561 ctxt.check = 1;
1562 xmlCtxtDumpDocument(&ctxt, doc);
Daniel Veillard76821142004-10-09 20:39:04 +00001563 xmlCtxtDumpCleanCtxt(&ctxt);
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00001564 return(ctxt.errors);
1565}
1566
1567/************************************************************************
1568 * *
Daniel Veillard22cdb842004-10-04 14:09:17 +00001569 * Helpers for Shell *
1570 * *
1571 ************************************************************************/
Owen Taylor3473f882001-02-23 17:55:21 +00001572
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001573/**
1574 * xmlLsCountNode:
1575 * @node: the node to count
1576 *
1577 * Count the children of @node.
1578 *
1579 * Returns the number of children of @node.
1580 */
1581int
1582xmlLsCountNode(xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00001583 int ret = 0;
1584 xmlNodePtr list = NULL;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001585
1586 if (node == NULL)
1587 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001588
1589 switch (node->type) {
1590 case XML_ELEMENT_NODE:
1591 list = node->children;
1592 break;
1593 case XML_DOCUMENT_NODE:
1594 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00001595#ifdef LIBXML_DOCB_ENABLED
1596 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001597#endif
1598 list = ((xmlDocPtr) node)->children;
1599 break;
1600 case XML_ATTRIBUTE_NODE:
1601 list = ((xmlAttrPtr) node)->children;
1602 break;
1603 case XML_TEXT_NODE:
1604 case XML_CDATA_SECTION_NODE:
1605 case XML_PI_NODE:
1606 case XML_COMMENT_NODE:
1607 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001608 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001609 }
1610 break;
1611 case XML_ENTITY_REF_NODE:
1612 case XML_DOCUMENT_TYPE_NODE:
1613 case XML_ENTITY_NODE:
1614 case XML_DOCUMENT_FRAG_NODE:
1615 case XML_NOTATION_NODE:
1616 case XML_DTD_NODE:
1617 case XML_ELEMENT_DECL:
1618 case XML_ATTRIBUTE_DECL:
1619 case XML_ENTITY_DECL:
1620 case XML_NAMESPACE_DECL:
1621 case XML_XINCLUDE_START:
1622 case XML_XINCLUDE_END:
1623 ret = 1;
1624 break;
1625 }
1626 for (;list != NULL;ret++)
1627 list = list->next;
1628 return(ret);
1629}
1630
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001631/**
1632 * xmlLsOneNode:
1633 * @output: the FILE * for the output
1634 * @node: the node to dump
1635 *
1636 * Dump to @output the type and name of @node.
1637 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001638void
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001639xmlLsOneNode(FILE *output, xmlNodePtr node) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00001640 if (output == NULL) return;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001641 if (node == NULL) {
1642 fprintf(output, "NULL\n");
1643 return;
1644 }
Owen Taylor3473f882001-02-23 17:55:21 +00001645 switch (node->type) {
1646 case XML_ELEMENT_NODE:
1647 fprintf(output, "-");
1648 break;
1649 case XML_ATTRIBUTE_NODE:
1650 fprintf(output, "a");
1651 break;
1652 case XML_TEXT_NODE:
1653 fprintf(output, "t");
1654 break;
1655 case XML_CDATA_SECTION_NODE:
Daniel Veillard75be0132002-03-13 10:03:35 +00001656 fprintf(output, "C");
Owen Taylor3473f882001-02-23 17:55:21 +00001657 break;
1658 case XML_ENTITY_REF_NODE:
1659 fprintf(output, "e");
1660 break;
1661 case XML_ENTITY_NODE:
1662 fprintf(output, "E");
1663 break;
1664 case XML_PI_NODE:
1665 fprintf(output, "p");
1666 break;
1667 case XML_COMMENT_NODE:
1668 fprintf(output, "c");
1669 break;
1670 case XML_DOCUMENT_NODE:
1671 fprintf(output, "d");
1672 break;
1673 case XML_HTML_DOCUMENT_NODE:
1674 fprintf(output, "h");
1675 break;
1676 case XML_DOCUMENT_TYPE_NODE:
1677 fprintf(output, "T");
1678 break;
1679 case XML_DOCUMENT_FRAG_NODE:
1680 fprintf(output, "F");
1681 break;
1682 case XML_NOTATION_NODE:
1683 fprintf(output, "N");
1684 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001685 case XML_NAMESPACE_DECL:
1686 fprintf(output, "n");
1687 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001688 default:
1689 fprintf(output, "?");
1690 }
Daniel Veillarde6a55192002-01-14 17:11:53 +00001691 if (node->type != XML_NAMESPACE_DECL) {
1692 if (node->properties != NULL)
1693 fprintf(output, "a");
1694 else
1695 fprintf(output, "-");
1696 if (node->nsDef != NULL)
1697 fprintf(output, "n");
1698 else
1699 fprintf(output, "-");
1700 }
Owen Taylor3473f882001-02-23 17:55:21 +00001701
1702 fprintf(output, " %8d ", xmlLsCountNode(node));
1703
1704 switch (node->type) {
1705 case XML_ELEMENT_NODE:
1706 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001707 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001708 break;
1709 case XML_ATTRIBUTE_NODE:
1710 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001711 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001712 break;
1713 case XML_TEXT_NODE:
1714 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001715 xmlDebugDumpString(output, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001716 }
1717 break;
1718 case XML_CDATA_SECTION_NODE:
1719 break;
1720 case XML_ENTITY_REF_NODE:
1721 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001722 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001723 break;
1724 case XML_ENTITY_NODE:
1725 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001726 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001727 break;
1728 case XML_PI_NODE:
1729 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001730 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001731 break;
1732 case XML_COMMENT_NODE:
1733 break;
1734 case XML_DOCUMENT_NODE:
1735 break;
1736 case XML_HTML_DOCUMENT_NODE:
1737 break;
1738 case XML_DOCUMENT_TYPE_NODE:
1739 break;
1740 case XML_DOCUMENT_FRAG_NODE:
1741 break;
1742 case XML_NOTATION_NODE:
1743 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001744 case XML_NAMESPACE_DECL: {
1745 xmlNsPtr ns = (xmlNsPtr) node;
1746
1747 if (ns->prefix == NULL)
William M. Brack13dfa872004-09-18 04:52:08 +00001748 fprintf(output, "default -> %s", (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001749 else
William M. Brack13dfa872004-09-18 04:52:08 +00001750 fprintf(output, "%s -> %s", (char *)ns->prefix,
1751 (char *)ns->href);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001752 break;
1753 }
Owen Taylor3473f882001-02-23 17:55:21 +00001754 default:
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 }
1758 fprintf(output, "\n");
1759}
1760
Daniel Veillard78d12092001-10-11 09:12:24 +00001761/**
1762 * xmlBoolToText:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001763 * @boolval: a bool to turn into text
Daniel Veillard78d12092001-10-11 09:12:24 +00001764 *
1765 * Convenient way to turn bool into text
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001766 *
1767 * Returns a pointer to either "True" or "False"
1768 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001769const char *
Daniel Veillardebd38c52001-11-01 08:38:12 +00001770xmlBoolToText(int boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001771{
Daniel Veillardebd38c52001-11-01 08:38:12 +00001772 if (boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001773 return("True");
1774 else
1775 return("False");
1776}
1777
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00001778#ifdef LIBXML_XPATH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001779/****************************************************************
1780 * *
1781 * The XML shell related functions *
1782 * *
1783 ****************************************************************/
1784
Daniel Veillard78d12092001-10-11 09:12:24 +00001785
1786
Owen Taylor3473f882001-02-23 17:55:21 +00001787/*
1788 * TODO: Improvement/cleanups for the XML shell
1789 * - allow to shell out an editor on a subpart
1790 * - cleanup function registrations (with help) and calling
1791 * - provide registration routines
1792 */
1793
1794/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001795 * xmlShellPrintXPathError:
Daniel Veillard78d12092001-10-11 09:12:24 +00001796 * @errorType: valid xpath error id
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001797 * @arg: the argument that cause xpath to fail
Daniel Veillard78d12092001-10-11 09:12:24 +00001798 *
1799 * Print the xpath error to libxml default error channel
1800 */
1801void
1802xmlShellPrintXPathError(int errorType, const char *arg)
1803{
1804 const char *default_arg = "Result";
1805
1806 if (!arg)
1807 arg = default_arg;
1808
1809 switch (errorType) {
1810 case XPATH_UNDEFINED:
1811 xmlGenericError(xmlGenericErrorContext,
1812 "%s: no such node\n", arg);
1813 break;
1814
1815 case XPATH_BOOLEAN:
1816 xmlGenericError(xmlGenericErrorContext,
1817 "%s is a Boolean\n", arg);
1818 break;
1819 case XPATH_NUMBER:
1820 xmlGenericError(xmlGenericErrorContext,
1821 "%s is a number\n", arg);
1822 break;
1823 case XPATH_STRING:
1824 xmlGenericError(xmlGenericErrorContext,
1825 "%s is a string\n", arg);
1826 break;
1827 case XPATH_POINT:
1828 xmlGenericError(xmlGenericErrorContext,
1829 "%s is a point\n", arg);
1830 break;
1831 case XPATH_RANGE:
1832 xmlGenericError(xmlGenericErrorContext,
1833 "%s is a range\n", arg);
1834 break;
1835 case XPATH_LOCATIONSET:
1836 xmlGenericError(xmlGenericErrorContext,
1837 "%s is a range\n", arg);
1838 break;
1839 case XPATH_USERS:
1840 xmlGenericError(xmlGenericErrorContext,
1841 "%s is user-defined\n", arg);
1842 break;
1843 case XPATH_XSLT_TREE:
1844 xmlGenericError(xmlGenericErrorContext,
1845 "%s is an XSLT value tree\n", arg);
1846 break;
1847 }
Daniel Veillarda82b1822004-11-08 16:24:57 +00001848#if 0
Daniel Veillard78d12092001-10-11 09:12:24 +00001849 xmlGenericError(xmlGenericErrorContext,
1850 "Try casting the result string function (xpath builtin)\n",
1851 arg);
Daniel Veillarda82b1822004-11-08 16:24:57 +00001852#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00001853}
1854
1855
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001856#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001857/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001858 * xmlShellPrintNodeCtxt:
1859 * @ctxt : a non-null shell context
1860 * @node : a non-null node to print to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001861 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001862 * Print node to the output FILE
1863 */
1864static void
1865xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1866{
Daniel Veillard01992e02002-10-09 10:20:30 +00001867 FILE *fp;
1868
1869 if (!node)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001870 return;
Daniel Veillard01992e02002-10-09 10:20:30 +00001871 if (ctxt == NULL)
1872 fp = stdout;
1873 else
1874 fp = ctxt->output;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001875
1876 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001877 xmlDocDump(fp, (xmlDocPtr) node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001878 else if (node->type == XML_ATTRIBUTE_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001879 xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001880 else
Daniel Veillard01992e02002-10-09 10:20:30 +00001881 xmlElemDump(fp, node->doc, node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001882
Daniel Veillard01992e02002-10-09 10:20:30 +00001883 fprintf(fp, "\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00001884}
1885
1886/**
1887 * xmlShellPrintNode:
1888 * @node : a non-null node to print to the output FILE
1889 *
1890 * Print node to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001891 */
1892void
1893xmlShellPrintNode(xmlNodePtr node)
1894{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001895 xmlShellPrintNodeCtxt(NULL, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001896}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001897#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001898
Daniel Veillard78d12092001-10-11 09:12:24 +00001899/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001900 * xmlShellPrintXPathResultCtxt:
1901 * @ctxt: a valid shell context
Daniel Veillard9d06d302002-01-22 18:15:52 +00001902 * @list: a valid result generated by an xpath evaluation
Daniel Veillard78d12092001-10-11 09:12:24 +00001903 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001904 * Prints result to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001905 */
Daniel Veillard321be0c2002-10-08 21:26:42 +00001906static void
1907xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
Daniel Veillard78d12092001-10-11 09:12:24 +00001908{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001909 if (!ctxt)
1910 return;
Daniel Veillard78d12092001-10-11 09:12:24 +00001911
1912 if (list != NULL) {
1913 switch (list->type) {
1914 case XPATH_NODESET:{
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001915#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001916 int indx;
1917
1918 if (list->nodesetval) {
1919 for (indx = 0; indx < list->nodesetval->nodeNr;
1920 indx++) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001921 xmlShellPrintNodeCtxt(ctxt,
1922 list->nodesetval->nodeTab[indx]);
Daniel Veillard78d12092001-10-11 09:12:24 +00001923 }
1924 } else {
1925 xmlGenericError(xmlGenericErrorContext,
1926 "Empty node set\n");
1927 }
1928 break;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001929#else
1930 xmlGenericError(xmlGenericErrorContext,
1931 "Node set\n");
1932#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001933 }
1934 case XPATH_BOOLEAN:
1935 xmlGenericError(xmlGenericErrorContext,
1936 "Is a Boolean:%s\n",
1937 xmlBoolToText(list->boolval));
1938 break;
1939 case XPATH_NUMBER:
1940 xmlGenericError(xmlGenericErrorContext,
1941 "Is a number:%0g\n", list->floatval);
1942 break;
1943 case XPATH_STRING:
1944 xmlGenericError(xmlGenericErrorContext,
1945 "Is a string:%s\n", list->stringval);
1946 break;
1947
1948 default:
1949 xmlShellPrintXPathError(list->type, NULL);
1950 }
1951 }
1952}
1953
1954/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001955 * xmlShellPrintXPathResult:
1956 * @list: a valid result generated by an xpath evaluation
1957 *
1958 * Prints result to the output FILE
1959 */
1960void
1961xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1962{
1963 xmlShellPrintXPathResultCtxt(NULL, list);
1964}
1965
1966/**
Owen Taylor3473f882001-02-23 17:55:21 +00001967 * xmlShellList:
1968 * @ctxt: the shell context
1969 * @arg: unused
1970 * @node: a node
1971 * @node2: unused
1972 *
1973 * Implements the XML shell function "ls"
1974 * Does an Unix like listing of the given node (like a directory)
1975 *
1976 * Returns 0
1977 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001978int
Daniel Veillard321be0c2002-10-08 21:26:42 +00001979xmlShellList(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00001980 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1981 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1982{
Owen Taylor3473f882001-02-23 17:55:21 +00001983 xmlNodePtr cur;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001984 if (!ctxt)
1985 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001986 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001987 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001988 return (0);
1989 }
Owen Taylor3473f882001-02-23 17:55:21 +00001990 if ((node->type == XML_DOCUMENT_NODE) ||
1991 (node->type == XML_HTML_DOCUMENT_NODE)) {
1992 cur = ((xmlDocPtr) node)->children;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001993 } else if (node->type == XML_NAMESPACE_DECL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001994 xmlLsOneNode(ctxt->output, node);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001995 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001996 } else if (node->children != NULL) {
1997 cur = node->children;
1998 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001999 xmlLsOneNode(ctxt->output, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002000 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002001 }
2002 while (cur != NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002003 xmlLsOneNode(ctxt->output, cur);
Daniel Veillard78d12092001-10-11 09:12:24 +00002004 cur = cur->next;
Owen Taylor3473f882001-02-23 17:55:21 +00002005 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002006 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002007}
2008
2009/**
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002010 * xmlShellBase:
2011 * @ctxt: the shell context
2012 * @arg: unused
2013 * @node: a node
2014 * @node2: unused
2015 *
2016 * Implements the XML shell function "base"
2017 * dumps the current XML base of the node
2018 *
2019 * Returns 0
2020 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002021int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002022xmlShellBase(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002023 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2024 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2025{
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002026 xmlChar *base;
Daniel Veillard321be0c2002-10-08 21:26:42 +00002027 if (!ctxt)
2028 return 0;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002029 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002030 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002031 return (0);
2032 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002033
2034 base = xmlNodeGetBase(node->doc, node);
2035
2036 if (base == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002037 fprintf(ctxt->output, " No base found !!!\n");
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002038 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002039 fprintf(ctxt->output, "%s\n", base);
Daniel Veillard78d12092001-10-11 09:12:24 +00002040 xmlFree(base);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002041 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002042 return (0);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002043}
2044
Daniel Veillardb34321c2004-03-04 17:09:47 +00002045#ifdef LIBXML_TREE_ENABLED
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002046/**
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002047 * xmlShellSetBase:
2048 * @ctxt: the shell context
2049 * @arg: the new base
2050 * @node: a node
2051 * @node2: unused
2052 *
2053 * Implements the XML shell function "setbase"
2054 * change the current XML base of the node
2055 *
2056 * Returns 0
2057 */
2058static int
2059xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2060 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2061 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2062{
2063 xmlNodeSetBase(node, (xmlChar*) arg);
2064 return (0);
2065}
Daniel Veillard2156d432004-03-04 15:59:36 +00002066#endif
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002067
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002068#ifdef LIBXML_XPATH_ENABLED
2069/**
2070 * xmlShellRegisterNamespace:
2071 * @ctxt: the shell context
2072 * @arg: a string in prefix=nsuri format
2073 * @node: unused
2074 * @node2: unused
2075 *
2076 * Implements the XML shell function "setns"
2077 * register/unregister a prefix=namespace pair
2078 * on the XPath context
2079 *
2080 * Returns 0 on success and a negative value otherwise.
2081 */
2082static int
2083xmlShellRegisterNamespace(xmlShellCtxtPtr ctxt, char *arg,
2084 xmlNodePtr node ATTRIBUTE_UNUSED, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2085{
2086 xmlChar* nsListDup;
2087 xmlChar* prefix;
2088 xmlChar* href;
2089 xmlChar* next;
2090
2091 nsListDup = xmlStrdup((xmlChar *) arg);
2092 next = nsListDup;
2093 while(next != NULL) {
2094 /* skip spaces */
2095 /*while((*next) == ' ') next++;*/
2096 if((*next) == '\0') break;
2097
2098 /* find prefix */
2099 prefix = next;
2100 next = (xmlChar*)xmlStrchr(next, '=');
2101 if(next == NULL) {
2102 fprintf(ctxt->output, "setns: prefix=[nsuri] required\n");
2103 xmlFree(nsListDup);
2104 return(-1);
2105 }
2106 *(next++) = '\0';
2107
2108 /* find href */
2109 href = next;
2110 next = (xmlChar*)xmlStrchr(next, ' ');
2111 if(next != NULL) {
2112 *(next++) = '\0';
2113 }
2114
2115 /* do register namespace */
2116 if(xmlXPathRegisterNs(ctxt->pctxt, prefix, href) != 0) {
2117 fprintf(ctxt->output,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
2118 xmlFree(nsListDup);
2119 return(-1);
2120 }
2121 }
2122
2123 xmlFree(nsListDup);
2124 return(0);
2125}
2126#endif
2127
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002128/**
Daniel Veillard1e208222002-10-22 14:25:25 +00002129 * xmlShellGrep:
2130 * @ctxt: the shell context
2131 * @arg: the string or regular expression to find
2132 * @node: a node
2133 * @node2: unused
2134 *
2135 * Implements the XML shell function "grep"
2136 * dumps informations about the node (namespace, attributes, content).
2137 *
2138 * Returns 0
2139 */
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002140static int
Daniel Veillard1e208222002-10-22 14:25:25 +00002141xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2142 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2143{
2144 if (!ctxt)
2145 return (0);
2146 if (node == NULL)
2147 return (0);
2148 if (arg == NULL)
2149 return (0);
2150#ifdef LIBXML_REGEXP_ENABLED
2151 if ((xmlStrchr((xmlChar *) arg, '?')) ||
2152 (xmlStrchr((xmlChar *) arg, '*')) ||
2153 (xmlStrchr((xmlChar *) arg, '.')) ||
2154 (xmlStrchr((xmlChar *) arg, '['))) {
2155 }
2156#endif
2157 while (node != NULL) {
2158 if (node->type == XML_COMMENT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002159 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002160
2161 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
2162 xmlShellList(ctxt, NULL, node, NULL);
2163 }
2164 } else if (node->type == XML_TEXT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002165 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00002166
2167 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
Daniel Veillarde645e8c2002-10-22 17:35:37 +00002168 xmlShellList(ctxt, NULL, node->parent, NULL);
Daniel Veillard1e208222002-10-22 14:25:25 +00002169 }
2170 }
2171
2172 /*
2173 * Browse the full subtree, deep first
2174 */
2175
2176 if ((node->type == XML_DOCUMENT_NODE) ||
2177 (node->type == XML_HTML_DOCUMENT_NODE)) {
2178 node = ((xmlDocPtr) node)->children;
2179 } else if ((node->children != NULL)
2180 && (node->type != XML_ENTITY_REF_NODE)) {
2181 /* deep first */
2182 node = node->children;
2183 } else if (node->next != NULL) {
2184 /* then siblings */
2185 node = node->next;
2186 } else {
2187 /* go up to parents->next if needed */
2188 while (node != NULL) {
2189 if (node->parent != NULL) {
2190 node = node->parent;
2191 }
2192 if (node->next != NULL) {
2193 node = node->next;
2194 break;
2195 }
2196 if (node->parent == NULL) {
2197 node = NULL;
2198 break;
2199 }
2200 }
2201 }
2202 }
2203 return (0);
2204}
2205
2206/**
Owen Taylor3473f882001-02-23 17:55:21 +00002207 * xmlShellDir:
2208 * @ctxt: the shell context
2209 * @arg: unused
2210 * @node: a node
2211 * @node2: unused
2212 *
2213 * Implements the XML shell function "dir"
2214 * dumps informations about the node (namespace, attributes, content).
2215 *
2216 * Returns 0
2217 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002218int
2219xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2220 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
2221 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2222{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002223 if (!ctxt)
2224 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002225 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002226 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002227 return (0);
2228 }
Owen Taylor3473f882001-02-23 17:55:21 +00002229 if ((node->type == XML_DOCUMENT_NODE) ||
2230 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002231 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
Owen Taylor3473f882001-02-23 17:55:21 +00002232 } else if (node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002233 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002234 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002235 xmlDebugDumpOneNode(ctxt->output, node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002236 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002237 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002238}
2239
Daniel Veillard29b17482004-08-16 00:39:03 +00002240/**
2241 * xmlShellSetContent:
2242 * @ctxt: the shell context
2243 * @value: the content as a string
2244 * @node: a node
2245 * @node2: unused
2246 *
2247 * Implements the XML shell function "dir"
2248 * dumps informations about the node (namespace, attributes, content).
2249 *
2250 * Returns 0
2251 */
2252static int
2253xmlShellSetContent(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
2254 char *value, xmlNodePtr node,
2255 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2256{
2257 xmlNodePtr results;
2258 xmlParserErrors ret;
2259
2260 if (!ctxt)
2261 return (0);
2262 if (node == NULL) {
2263 fprintf(ctxt->output, "NULL\n");
2264 return (0);
2265 }
2266 if (value == NULL) {
2267 fprintf(ctxt->output, "NULL\n");
2268 return (0);
2269 }
2270
2271 ret = xmlParseInNodeContext(node, value, strlen(value), 0, &results);
2272 if (ret == XML_ERR_OK) {
2273 if (node->children != NULL) {
2274 xmlFreeNodeList(node->children);
2275 node->children = NULL;
2276 node->last = NULL;
2277 }
2278 xmlAddChildList(node, results);
2279 } else {
2280 fprintf(ctxt->output, "failed to parse content\n");
2281 }
2282 return (0);
2283}
2284
Daniel Veillard522bc602004-02-21 11:53:09 +00002285#ifdef LIBXML_SCHEMAS_ENABLED
2286/**
2287 * xmlShellRNGValidate:
2288 * @ctxt: the shell context
2289 * @schemas: the path to the Relax-NG schemas
2290 * @node: a node
2291 * @node2: unused
2292 *
2293 * Implements the XML shell function "relaxng"
2294 * validating the instance against a Relax-NG schemas
2295 *
2296 * Returns 0
2297 */
2298static int
2299xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
2300 xmlNodePtr node ATTRIBUTE_UNUSED,
2301 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2302{
2303 xmlRelaxNGPtr relaxngschemas;
2304 xmlRelaxNGParserCtxtPtr ctxt;
2305 xmlRelaxNGValidCtxtPtr vctxt;
2306 int ret;
2307
2308 ctxt = xmlRelaxNGNewParserCtxt(schemas);
2309 xmlRelaxNGSetParserErrors(ctxt,
2310 (xmlRelaxNGValidityErrorFunc) fprintf,
2311 (xmlRelaxNGValidityWarningFunc) fprintf,
2312 stderr);
2313 relaxngschemas = xmlRelaxNGParse(ctxt);
2314 xmlRelaxNGFreeParserCtxt(ctxt);
2315 if (relaxngschemas == NULL) {
2316 xmlGenericError(xmlGenericErrorContext,
2317 "Relax-NG schema %s failed to compile\n", schemas);
2318 return(-1);
2319 }
2320 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2321 xmlRelaxNGSetValidErrors(vctxt,
2322 (xmlRelaxNGValidityErrorFunc) fprintf,
2323 (xmlRelaxNGValidityWarningFunc) fprintf,
2324 stderr);
2325 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
2326 if (ret == 0) {
2327 fprintf(stderr, "%s validates\n", sctxt->filename);
2328 } else if (ret > 0) {
2329 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
2330 } else {
2331 fprintf(stderr, "%s validation generated an internal error\n",
2332 sctxt->filename);
2333 }
2334 xmlRelaxNGFreeValidCtxt(vctxt);
2335 if (relaxngschemas != NULL)
2336 xmlRelaxNGFree(relaxngschemas);
2337 return(0);
2338}
2339#endif
2340
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002341#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002342/**
2343 * xmlShellCat:
2344 * @ctxt: the shell context
2345 * @arg: unused
2346 * @node: a node
2347 * @node2: unused
2348 *
2349 * Implements the XML shell function "cat"
2350 * dumps the serialization node content (XML or HTML).
2351 *
2352 * Returns 0
2353 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002354int
2355xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
2356 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2357{
Daniel Veillard321be0c2002-10-08 21:26:42 +00002358 if (!ctxt)
2359 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002360 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002361 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00002362 return (0);
2363 }
Owen Taylor3473f882001-02-23 17:55:21 +00002364 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
2365#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002366 if (node->type == XML_HTML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002367 htmlDocDump(ctxt->output, (htmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002368 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002369 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002370#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002371 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002372 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002373 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002374 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002375#endif /* LIBXML_HTML_ENABLED */
2376 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00002377 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002378 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00002379 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002380 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00002381 }
Daniel Veillard321be0c2002-10-08 21:26:42 +00002382 fprintf(ctxt->output, "\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002383 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002384}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002385#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002386
2387/**
2388 * xmlShellLoad:
2389 * @ctxt: the shell context
2390 * @filename: the file name
2391 * @node: unused
2392 * @node2: unused
2393 *
2394 * Implements the XML shell function "load"
2395 * loads a new document specified by the filename
2396 *
2397 * Returns 0 or -1 if loading failed
2398 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002399int
2400xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
2401 xmlNodePtr node ATTRIBUTE_UNUSED,
2402 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2403{
Owen Taylor3473f882001-02-23 17:55:21 +00002404 xmlDocPtr doc;
2405 int html = 0;
2406
Daniel Veillarda82b1822004-11-08 16:24:57 +00002407 if ((ctxt == NULL) || (filename == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002408 if (ctxt->doc != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002409 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00002410
2411 if (html) {
2412#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002413 doc = htmlParseFile(filename, NULL);
2414#else
Daniel Veillard321be0c2002-10-08 21:26:42 +00002415 fprintf(ctxt->output, "HTML support not compiled in\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002416 doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002417#endif /* LIBXML_HTML_ENABLED */
2418 } else {
Daniel Veillardebe25d42004-03-25 09:35:49 +00002419 doc = xmlReadFile(filename,NULL,0);
Owen Taylor3473f882001-02-23 17:55:21 +00002420 }
2421 if (doc != NULL) {
2422 if (ctxt->loaded == 1) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002423 xmlFreeDoc(ctxt->doc);
2424 }
2425 ctxt->loaded = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002426#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002427 xmlXPathFreeContext(ctxt->pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00002428#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002429 xmlFree(ctxt->filename);
2430 ctxt->doc = doc;
2431 ctxt->node = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002432#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002433 ctxt->pctxt = xmlXPathNewContext(doc);
Owen Taylor3473f882001-02-23 17:55:21 +00002434#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard85095e22003-04-23 13:56:44 +00002435 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002436 } else
Daniel Veillard78d12092001-10-11 09:12:24 +00002437 return (-1);
2438 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002439}
2440
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002441#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002442/**
2443 * xmlShellWrite:
2444 * @ctxt: the shell context
2445 * @filename: the file name
2446 * @node: a node in the tree
2447 * @node2: unused
2448 *
2449 * Implements the XML shell function "write"
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002450 * Write the current node to the filename, it saves the serialization
Owen Taylor3473f882001-02-23 17:55:21 +00002451 * of the subtree under the @node specified
2452 *
2453 * Returns 0 or -1 in case of error
2454 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002455int
Owen Taylor3473f882001-02-23 17:55:21 +00002456xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Daniel Veillard78d12092001-10-11 09:12:24 +00002457 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2458{
Owen Taylor3473f882001-02-23 17:55:21 +00002459 if (node == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002460 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002461 if ((filename == NULL) || (filename[0] == 0)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002462 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002463 }
2464#ifdef W_OK
2465 if (access((char *) filename, W_OK)) {
2466 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002467 "Cannot write to %s\n", filename);
2468 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002469 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002470#endif
2471 switch (node->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002472 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002473 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2474 xmlGenericError(xmlGenericErrorContext,
2475 "Failed to write to %s\n", filename);
2476 return (-1);
2477 }
2478 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002479 case XML_HTML_DOCUMENT_NODE:
2480#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002481 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2482 xmlGenericError(xmlGenericErrorContext,
2483 "Failed to write to %s\n", filename);
2484 return (-1);
2485 }
Owen Taylor3473f882001-02-23 17:55:21 +00002486#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002487 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
2488 xmlGenericError(xmlGenericErrorContext,
2489 "Failed to write to %s\n", filename);
2490 return (-1);
2491 }
Owen Taylor3473f882001-02-23 17:55:21 +00002492#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002493 break;
2494 default:{
2495 FILE *f;
Owen Taylor3473f882001-02-23 17:55:21 +00002496
Daniel Veillard78d12092001-10-11 09:12:24 +00002497 f = fopen((char *) filename, "w");
2498 if (f == NULL) {
2499 xmlGenericError(xmlGenericErrorContext,
2500 "Failed to write to %s\n", filename);
2501 return (-1);
2502 }
2503 xmlElemDump(f, ctxt->doc, node);
2504 fclose(f);
2505 }
Owen Taylor3473f882001-02-23 17:55:21 +00002506 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002507 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002508}
2509
2510/**
2511 * xmlShellSave:
2512 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002513 * @filename: the file name (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002514 * @node: unused
2515 * @node2: unused
2516 *
2517 * Implements the XML shell function "save"
2518 * Write the current document to the filename, or it's original name
2519 *
2520 * Returns 0 or -1 in case of error
2521 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002522int
2523xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
2524 xmlNodePtr node ATTRIBUTE_UNUSED,
2525 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2526{
Daniel Veillarda82b1822004-11-08 16:24:57 +00002527 if ((ctxt == NULL) || (ctxt->doc == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002528 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002529 if ((filename == NULL) || (filename[0] == 0))
2530 filename = ctxt->filename;
Daniel Veillarda82b1822004-11-08 16:24:57 +00002531 if (filename == NULL)
2532 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002533#ifdef W_OK
2534 if (access((char *) filename, W_OK)) {
2535 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00002536 "Cannot save to %s\n", filename);
2537 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002538 }
2539#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002540 switch (ctxt->doc->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00002541 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00002542 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2543 xmlGenericError(xmlGenericErrorContext,
2544 "Failed to save to %s\n", filename);
2545 }
2546 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002547 case XML_HTML_DOCUMENT_NODE:
2548#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002549 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
2550 xmlGenericError(xmlGenericErrorContext,
2551 "Failed to save to %s\n", filename);
2552 }
Owen Taylor3473f882001-02-23 17:55:21 +00002553#else
Daniel Veillard78d12092001-10-11 09:12:24 +00002554 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
2555 xmlGenericError(xmlGenericErrorContext,
2556 "Failed to save to %s\n", filename);
2557 }
Owen Taylor3473f882001-02-23 17:55:21 +00002558#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002559 break;
2560 default:
2561 xmlGenericError(xmlGenericErrorContext,
2562 "To save to subparts of a document use the 'write' command\n");
2563 return (-1);
2564
Owen Taylor3473f882001-02-23 17:55:21 +00002565 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002566 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002567}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002568#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002569
Daniel Veillardf54cd532004-02-25 11:52:31 +00002570#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002571/**
2572 * xmlShellValidate:
2573 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002574 * @dtd: the DTD URI (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00002575 * @node: unused
2576 * @node2: unused
2577 *
2578 * Implements the XML shell function "validate"
2579 * Validate the document, if a DTD path is provided, then the validation
2580 * is done against the given DTD.
2581 *
2582 * Returns 0 or -1 in case of error
2583 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002584int
2585xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
2586 xmlNodePtr node ATTRIBUTE_UNUSED,
2587 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2588{
Owen Taylor3473f882001-02-23 17:55:21 +00002589 xmlValidCtxt vctxt;
2590 int res = -1;
2591
Daniel Veillarda82b1822004-11-08 16:24:57 +00002592 if ((ctxt == NULL) || (ctxt->doc == NULL)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002593 vctxt.userData = stderr;
2594 vctxt.error = (xmlValidityErrorFunc) fprintf;
2595 vctxt.warning = (xmlValidityWarningFunc) fprintf;
2596
2597 if ((dtd == NULL) || (dtd[0] == 0)) {
2598 res = xmlValidateDocument(&vctxt, ctxt->doc);
2599 } else {
2600 xmlDtdPtr subset;
2601
Daniel Veillard78d12092001-10-11 09:12:24 +00002602 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
2603 if (subset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002604 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
2605
Daniel Veillard78d12092001-10-11 09:12:24 +00002606 xmlFreeDtd(subset);
2607 }
Owen Taylor3473f882001-02-23 17:55:21 +00002608 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002609 return (res);
Owen Taylor3473f882001-02-23 17:55:21 +00002610}
Daniel Veillardf54cd532004-02-25 11:52:31 +00002611#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002612
2613/**
2614 * xmlShellDu:
2615 * @ctxt: the shell context
2616 * @arg: unused
2617 * @tree: a node defining a subtree
2618 * @node2: unused
2619 *
2620 * Implements the XML shell function "du"
2621 * show the structure of the subtree under node @tree
2622 * If @tree is null, the command works on the current node.
2623 *
2624 * Returns 0 or -1 in case of error
2625 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002626int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002627xmlShellDu(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002628 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2629 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2630{
Owen Taylor3473f882001-02-23 17:55:21 +00002631 xmlNodePtr node;
Daniel Veillard78d12092001-10-11 09:12:24 +00002632 int indent = 0, i;
Owen Taylor3473f882001-02-23 17:55:21 +00002633
Daniel Veillard321be0c2002-10-08 21:26:42 +00002634 if (!ctxt)
2635 return (-1);
2636
Daniel Veillard78d12092001-10-11 09:12:24 +00002637 if (tree == NULL)
2638 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002639 node = tree;
2640 while (node != NULL) {
2641 if ((node->type == XML_DOCUMENT_NODE) ||
2642 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002643 fprintf(ctxt->output, "/\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002644 } else if (node->type == XML_ELEMENT_NODE) {
2645 for (i = 0; i < indent; i++)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002646 fprintf(ctxt->output, " ");
2647 fprintf(ctxt->output, "%s\n", node->name);
Daniel Veillard78d12092001-10-11 09:12:24 +00002648 } else {
2649 }
Owen Taylor3473f882001-02-23 17:55:21 +00002650
Daniel Veillard78d12092001-10-11 09:12:24 +00002651 /*
2652 * Browse the full subtree, deep first
2653 */
Owen Taylor3473f882001-02-23 17:55:21 +00002654
2655 if ((node->type == XML_DOCUMENT_NODE) ||
2656 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002657 node = ((xmlDocPtr) node)->children;
2658 } else if ((node->children != NULL)
2659 && (node->type != XML_ENTITY_REF_NODE)) {
2660 /* deep first */
2661 node = node->children;
2662 indent++;
2663 } else if ((node != tree) && (node->next != NULL)) {
2664 /* then siblings */
2665 node = node->next;
2666 } else if (node != tree) {
2667 /* go up to parents->next if needed */
2668 while (node != tree) {
2669 if (node->parent != NULL) {
2670 node = node->parent;
2671 indent--;
2672 }
2673 if ((node != tree) && (node->next != NULL)) {
2674 node = node->next;
2675 break;
2676 }
2677 if (node->parent == NULL) {
2678 node = NULL;
2679 break;
2680 }
2681 if (node == tree) {
2682 node = NULL;
2683 break;
2684 }
2685 }
2686 /* exit condition */
2687 if (node == tree)
2688 node = NULL;
2689 } else
2690 node = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002691 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002692 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002693}
2694
2695/**
2696 * xmlShellPwd:
2697 * @ctxt: the shell context
2698 * @buffer: the output buffer
Daniel Veillard9d06d302002-01-22 18:15:52 +00002699 * @node: a node
Owen Taylor3473f882001-02-23 17:55:21 +00002700 * @node2: unused
2701 *
2702 * Implements the XML shell function "pwd"
2703 * Show the full path from the root to the node, if needed building
2704 * thumblers when similar elements exists at a given ancestor level.
2705 * The output is compatible with XPath commands.
2706 *
2707 * Returns 0 or -1 in case of error
2708 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002709int
2710xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2711 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2712{
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002713 xmlChar *path;
Owen Taylor3473f882001-02-23 17:55:21 +00002714
Daniel Veillarda82b1822004-11-08 16:24:57 +00002715 if ((node == NULL) || (buffer == NULL))
Daniel Veillard78d12092001-10-11 09:12:24 +00002716 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002717
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002718 path = xmlGetNodePath(node);
2719 if (path == NULL)
2720 return (-1);
2721
2722 /*
2723 * This test prevents buffer overflow, because this routine
2724 * is only called by xmlShell, in which the second argument is
2725 * 500 chars long.
2726 * It is a dirty hack before a cleaner solution is found.
2727 * Documentation should mention that the second argument must
2728 * be at least 500 chars long, and could be stripped if too long.
2729 */
2730 snprintf(buffer, 499, "%s", path);
2731 buffer[499] = '0';
2732 xmlFree(path);
2733
Daniel Veillard78d12092001-10-11 09:12:24 +00002734 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002735}
2736
2737/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002738 * xmlShell:
Owen Taylor3473f882001-02-23 17:55:21 +00002739 * @doc: the initial document
2740 * @filename: the output buffer
2741 * @input: the line reading function
Daniel Veillard321be0c2002-10-08 21:26:42 +00002742 * @output: the output FILE*, defaults to stdout if NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002743 *
2744 * Implements the XML shell
2745 * This allow to load, validate, view, modify and save a document
2746 * using a environment similar to a UNIX commandline.
2747 */
2748void
2749xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Daniel Veillard78d12092001-10-11 09:12:24 +00002750 FILE * output)
2751{
Owen Taylor3473f882001-02-23 17:55:21 +00002752 char prompt[500] = "/ > ";
2753 char *cmdline = NULL, *cur;
2754 int nbargs;
2755 char command[100];
2756 char arg[400];
2757 int i;
2758 xmlShellCtxtPtr ctxt;
2759 xmlXPathObjectPtr list;
2760
2761 if (doc == NULL)
2762 return;
2763 if (filename == NULL)
2764 return;
2765 if (input == NULL)
2766 return;
2767 if (output == NULL)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002768 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00002769 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Daniel Veillard78d12092001-10-11 09:12:24 +00002770 if (ctxt == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00002771 return;
2772 ctxt->loaded = 0;
2773 ctxt->doc = doc;
2774 ctxt->input = input;
2775 ctxt->output = output;
2776 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Daniel Veillard78d12092001-10-11 09:12:24 +00002777 ctxt->node = (xmlNodePtr) ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002778
2779#ifdef LIBXML_XPATH_ENABLED
2780 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2781 if (ctxt->pctxt == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002782 xmlFree(ctxt);
2783 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002784 }
2785#endif /* LIBXML_XPATH_ENABLED */
2786 while (1) {
2787 if (ctxt->node == (xmlNodePtr) ctxt->doc)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002788 snprintf(prompt, sizeof(prompt), "%s > ", "/");
Daniel Veillard7a985a12003-07-06 17:57:42 +00002789 else if ((ctxt->node != NULL) && (ctxt->node->name))
Daniel Veillard78d12092001-10-11 09:12:24 +00002790 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002791 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002792 snprintf(prompt, sizeof(prompt), "? > ");
Owen Taylor3473f882001-02-23 17:55:21 +00002793 prompt[sizeof(prompt) - 1] = 0;
2794
Daniel Veillard78d12092001-10-11 09:12:24 +00002795 /*
2796 * Get a new command line
2797 */
Owen Taylor3473f882001-02-23 17:55:21 +00002798 cmdline = ctxt->input(prompt);
Daniel Veillard78d12092001-10-11 09:12:24 +00002799 if (cmdline == NULL)
2800 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002801
Daniel Veillard78d12092001-10-11 09:12:24 +00002802 /*
2803 * Parse the command itself
2804 */
2805 cur = cmdline;
2806 nbargs = 0;
2807 while ((*cur == ' ') || (*cur == '\t'))
2808 cur++;
2809 i = 0;
2810 while ((*cur != ' ') && (*cur != '\t') &&
2811 (*cur != '\n') && (*cur != '\r')) {
2812 if (*cur == 0)
2813 break;
2814 command[i++] = *cur++;
2815 }
2816 command[i] = 0;
2817 if (i == 0)
2818 continue;
2819 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002820
Daniel Veillard78d12092001-10-11 09:12:24 +00002821 /*
2822 * Parse the argument
2823 */
2824 while ((*cur == ' ') || (*cur == '\t'))
2825 cur++;
2826 i = 0;
2827 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2828 if (*cur == 0)
2829 break;
2830 arg[i++] = *cur++;
2831 }
2832 arg[i] = 0;
2833 if (i != 0)
2834 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002835
Daniel Veillard78d12092001-10-11 09:12:24 +00002836 /*
2837 * start interpreting the command
2838 */
Owen Taylor3473f882001-02-23 17:55:21 +00002839 if (!strcmp(command, "exit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002840 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002841 if (!strcmp(command, "quit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002842 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002843 if (!strcmp(command, "bye"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002844 break;
Daniel Veillard5004f422001-11-08 13:53:05 +00002845 if (!strcmp(command, "help")) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002846 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2847 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2848 fprintf(ctxt->output, "\tbye leave shell\n");
2849 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2850 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2851 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2852 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2853 fprintf(ctxt->output, "\texit leave shell\n");
2854 fprintf(ctxt->output, "\thelp display this help\n");
2855 fprintf(ctxt->output, "\tfree display memory usage\n");
2856 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2857 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00002858 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 +00002859#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002860 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002861 fprintf(ctxt->output, "\tsetns nsreg register a namespace to a prefix in the XPath evaluation context\n");
2862 fprintf(ctxt->output, "\t format for nsreg is: prefix=[nsuri] (i.e. prefix= unsets a prefix)\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002863#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard321be0c2002-10-08 21:26:42 +00002864 fprintf(ctxt->output, "\tpwd display current working directory\n");
2865 fprintf(ctxt->output, "\tquit leave shell\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002866#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002867 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00002868 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002869#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf54cd532004-02-25 11:52:31 +00002870#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002871 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002872#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard522bc602004-02-21 11:53:09 +00002873#ifdef LIBXML_SCHEMAS_ENABLED
2874 fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n");
2875#endif
Daniel Veillard1e208222002-10-22 14:25:25 +00002876 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002877#ifdef LIBXML_VALID_ENABLED
Daniel Veillard5004f422001-11-08 13:53:05 +00002878 } else if (!strcmp(command, "validate")) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002879 xmlShellValidate(ctxt, arg, NULL, NULL);
Daniel Veillardf54cd532004-02-25 11:52:31 +00002880#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002881 } else if (!strcmp(command, "load")) {
2882 xmlShellLoad(ctxt, arg, NULL, NULL);
Daniel Veillard522bc602004-02-21 11:53:09 +00002883#ifdef LIBXML_SCHEMAS_ENABLED
2884 } else if (!strcmp(command, "relaxng")) {
2885 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2886#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002887#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002888 } else if (!strcmp(command, "save")) {
2889 xmlShellSave(ctxt, arg, NULL, NULL);
2890 } else if (!strcmp(command, "write")) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00002891 if ((arg == NULL) || (arg[0] == 0))
2892 xmlGenericError(xmlGenericErrorContext,
2893 "Write command requires a filename argument\n");
2894 else
2895 xmlShellWrite(ctxt, arg, NULL, NULL);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002896#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard1e208222002-10-22 14:25:25 +00002897 } else if (!strcmp(command, "grep")) {
2898 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
Daniel Veillard78d12092001-10-11 09:12:24 +00002899 } else if (!strcmp(command, "free")) {
2900 if (arg[0] == 0) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002901 xmlMemShow(ctxt->output, 0);
Daniel Veillard78d12092001-10-11 09:12:24 +00002902 } else {
2903 int len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002904
Daniel Veillard78d12092001-10-11 09:12:24 +00002905 sscanf(arg, "%d", &len);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002906 xmlMemShow(ctxt->output, len);
Daniel Veillard78d12092001-10-11 09:12:24 +00002907 }
2908 } else if (!strcmp(command, "pwd")) {
2909 char dir[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002910
Daniel Veillard78d12092001-10-11 09:12:24 +00002911 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
Daniel Veillard321be0c2002-10-08 21:26:42 +00002912 fprintf(ctxt->output, "%s\n", dir);
Daniel Veillard78d12092001-10-11 09:12:24 +00002913 } else if (!strcmp(command, "du")) {
2914 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2915 } else if (!strcmp(command, "base")) {
2916 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
Daniel Veillard29b17482004-08-16 00:39:03 +00002917 } else if (!strcmp(command, "set")) {
2918 xmlShellSetContent(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002919#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardbbaa9972004-06-16 14:08:33 +00002920 } else if (!strcmp(command, "setns")) {
2921 if (arg[0] == 0) {
2922 xmlGenericError(xmlGenericErrorContext,
2923 "setns: prefix=[nsuri] required\n");
2924 } else {
2925 xmlShellRegisterNamespace(ctxt, arg, NULL, NULL);
2926 }
Daniel Veillard2070c482002-01-22 22:12:19 +00002927 } else if (!strcmp(command, "xpath")) {
2928 if (arg[0] == 0) {
2929 xmlGenericError(xmlGenericErrorContext,
2930 "xpath: expression required\n");
2931 } else {
2932 ctxt->pctxt->node = ctxt->node;
2933 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002934 xmlXPathDebugDumpObject(ctxt->output, list, 0);
Daniel Veillard2070c482002-01-22 22:12:19 +00002935 xmlXPathFreeObject(list);
2936 }
2937#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard2156d432004-03-04 15:59:36 +00002938#ifdef LIBXML_TREE_ENABLED
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002939 } else if (!strcmp(command, "setbase")) {
2940 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2156d432004-03-04 15:59:36 +00002941#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002942 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
2943 int dir = (!strcmp(command, "dir"));
2944
2945 if (arg[0] == 0) {
2946 if (dir)
2947 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
2948 else
2949 xmlShellList(ctxt, NULL, ctxt->node, NULL);
2950 } else {
2951 ctxt->pctxt->node = ctxt->node;
Daniel Veillard61d80a22001-04-27 17:13:01 +00002952#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002953 ctxt->pctxt->node = ctxt->node;
2954 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2955#else
2956 list = NULL;
2957#endif /* LIBXML_XPATH_ENABLED */
2958 if (list != NULL) {
2959 switch (list->type) {
2960 case XPATH_UNDEFINED:
2961 xmlGenericError(xmlGenericErrorContext,
2962 "%s: no such node\n", arg);
2963 break;
2964 case XPATH_NODESET:{
2965 int indx;
2966
Daniel Veillarda6825e82001-11-07 13:33:59 +00002967 if (list->nodesetval == NULL)
2968 break;
2969
Daniel Veillard78d12092001-10-11 09:12:24 +00002970 for (indx = 0;
2971 indx < list->nodesetval->nodeNr;
2972 indx++) {
2973 if (dir)
2974 xmlShellDir(ctxt, NULL,
2975 list->nodesetval->
2976 nodeTab[indx], NULL);
2977 else
2978 xmlShellList(ctxt, NULL,
2979 list->nodesetval->
2980 nodeTab[indx], NULL);
2981 }
2982 break;
2983 }
2984 case XPATH_BOOLEAN:
2985 xmlGenericError(xmlGenericErrorContext,
2986 "%s is a Boolean\n", arg);
2987 break;
2988 case XPATH_NUMBER:
2989 xmlGenericError(xmlGenericErrorContext,
2990 "%s is a number\n", arg);
2991 break;
2992 case XPATH_STRING:
2993 xmlGenericError(xmlGenericErrorContext,
2994 "%s is a string\n", arg);
2995 break;
2996 case XPATH_POINT:
2997 xmlGenericError(xmlGenericErrorContext,
2998 "%s is a point\n", arg);
2999 break;
3000 case XPATH_RANGE:
3001 xmlGenericError(xmlGenericErrorContext,
3002 "%s is a range\n", arg);
3003 break;
3004 case XPATH_LOCATIONSET:
3005 xmlGenericError(xmlGenericErrorContext,
3006 "%s is a range\n", arg);
3007 break;
3008 case XPATH_USERS:
3009 xmlGenericError(xmlGenericErrorContext,
3010 "%s is user-defined\n", arg);
3011 break;
3012 case XPATH_XSLT_TREE:
3013 xmlGenericError(xmlGenericErrorContext,
3014 "%s is an XSLT value tree\n",
3015 arg);
3016 break;
3017 }
3018#ifdef LIBXML_XPATH_ENABLED
3019 xmlXPathFreeObject(list);
Daniel Veillard61d80a22001-04-27 17:13:01 +00003020#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00003021 } else {
3022 xmlGenericError(xmlGenericErrorContext,
3023 "%s: no such node\n", arg);
3024 }
3025 ctxt->pctxt->node = NULL;
3026 }
3027 } else if (!strcmp(command, "cd")) {
3028 if (arg[0] == 0) {
3029 ctxt->node = (xmlNodePtr) ctxt->doc;
3030 } else {
3031#ifdef LIBXML_XPATH_ENABLED
3032 ctxt->pctxt->node = ctxt->node;
3033 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3034#else
3035 list = NULL;
3036#endif /* LIBXML_XPATH_ENABLED */
3037 if (list != NULL) {
3038 switch (list->type) {
3039 case XPATH_UNDEFINED:
3040 xmlGenericError(xmlGenericErrorContext,
3041 "%s: no such node\n", arg);
3042 break;
3043 case XPATH_NODESET:
Daniel Veillarda6825e82001-11-07 13:33:59 +00003044 if (list->nodesetval != NULL) {
3045 if (list->nodesetval->nodeNr == 1) {
3046 ctxt->node = list->nodesetval->nodeTab[0];
Daniel Veillard7a985a12003-07-06 17:57:42 +00003047 if ((ctxt->node != NULL) &&
3048 (ctxt->node->type ==
3049 XML_NAMESPACE_DECL)) {
3050 xmlGenericError(xmlGenericErrorContext,
3051 "cannot cd to namespace\n");
3052 ctxt->node = NULL;
3053 }
Daniel Veillarda6825e82001-11-07 13:33:59 +00003054 } else
3055 xmlGenericError(xmlGenericErrorContext,
3056 "%s is a %d Node Set\n",
3057 arg,
3058 list->nodesetval->nodeNr);
Daniel Veillard78d12092001-10-11 09:12:24 +00003059 } else
3060 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda6825e82001-11-07 13:33:59 +00003061 "%s is an empty Node Set\n",
3062 arg);
Daniel Veillard78d12092001-10-11 09:12:24 +00003063 break;
3064 case XPATH_BOOLEAN:
3065 xmlGenericError(xmlGenericErrorContext,
3066 "%s is a Boolean\n", arg);
3067 break;
3068 case XPATH_NUMBER:
3069 xmlGenericError(xmlGenericErrorContext,
3070 "%s is a number\n", arg);
3071 break;
3072 case XPATH_STRING:
3073 xmlGenericError(xmlGenericErrorContext,
3074 "%s is a string\n", arg);
3075 break;
3076 case XPATH_POINT:
3077 xmlGenericError(xmlGenericErrorContext,
3078 "%s is a point\n", arg);
3079 break;
3080 case XPATH_RANGE:
3081 xmlGenericError(xmlGenericErrorContext,
3082 "%s is a range\n", arg);
3083 break;
3084 case XPATH_LOCATIONSET:
3085 xmlGenericError(xmlGenericErrorContext,
3086 "%s is a range\n", arg);
3087 break;
3088 case XPATH_USERS:
3089 xmlGenericError(xmlGenericErrorContext,
3090 "%s is user-defined\n", arg);
3091 break;
3092 case XPATH_XSLT_TREE:
3093 xmlGenericError(xmlGenericErrorContext,
3094 "%s is an XSLT value tree\n",
3095 arg);
3096 break;
3097 }
3098#ifdef LIBXML_XPATH_ENABLED
3099 xmlXPathFreeObject(list);
3100#endif
3101 } else {
3102 xmlGenericError(xmlGenericErrorContext,
3103 "%s: no such node\n", arg);
3104 }
3105 ctxt->pctxt->node = NULL;
3106 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003107#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00003108 } else if (!strcmp(command, "cat")) {
3109 if (arg[0] == 0) {
3110 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
3111 } else {
3112 ctxt->pctxt->node = ctxt->node;
3113#ifdef LIBXML_XPATH_ENABLED
3114 ctxt->pctxt->node = ctxt->node;
3115 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
3116#else
3117 list = NULL;
3118#endif /* LIBXML_XPATH_ENABLED */
3119 if (list != NULL) {
3120 switch (list->type) {
3121 case XPATH_UNDEFINED:
3122 xmlGenericError(xmlGenericErrorContext,
3123 "%s: no such node\n", arg);
3124 break;
3125 case XPATH_NODESET:{
3126 int indx;
3127
Daniel Veillarda6825e82001-11-07 13:33:59 +00003128 if (list->nodesetval == NULL)
3129 break;
3130
Daniel Veillard78d12092001-10-11 09:12:24 +00003131 for (indx = 0;
3132 indx < list->nodesetval->nodeNr;
3133 indx++) {
3134 if (i > 0)
Daniel Veillard321be0c2002-10-08 21:26:42 +00003135 fprintf(ctxt->output, " -------\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00003136 xmlShellCat(ctxt, NULL,
3137 list->nodesetval->
3138 nodeTab[indx], NULL);
3139 }
3140 break;
3141 }
3142 case XPATH_BOOLEAN:
3143 xmlGenericError(xmlGenericErrorContext,
3144 "%s is a Boolean\n", arg);
3145 break;
3146 case XPATH_NUMBER:
3147 xmlGenericError(xmlGenericErrorContext,
3148 "%s is a number\n", arg);
3149 break;
3150 case XPATH_STRING:
3151 xmlGenericError(xmlGenericErrorContext,
3152 "%s is a string\n", arg);
3153 break;
3154 case XPATH_POINT:
3155 xmlGenericError(xmlGenericErrorContext,
3156 "%s is a point\n", arg);
3157 break;
3158 case XPATH_RANGE:
3159 xmlGenericError(xmlGenericErrorContext,
3160 "%s is a range\n", arg);
3161 break;
3162 case XPATH_LOCATIONSET:
3163 xmlGenericError(xmlGenericErrorContext,
3164 "%s is a range\n", arg);
3165 break;
3166 case XPATH_USERS:
3167 xmlGenericError(xmlGenericErrorContext,
3168 "%s is user-defined\n", arg);
3169 break;
3170 case XPATH_XSLT_TREE:
3171 xmlGenericError(xmlGenericErrorContext,
3172 "%s is an XSLT value tree\n",
3173 arg);
3174 break;
3175 }
3176#ifdef LIBXML_XPATH_ENABLED
3177 xmlXPathFreeObject(list);
3178#endif
3179 } else {
3180 xmlGenericError(xmlGenericErrorContext,
3181 "%s: no such node\n", arg);
3182 }
3183 ctxt->pctxt->node = NULL;
3184 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003185#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00003186 } else {
3187 xmlGenericError(xmlGenericErrorContext,
3188 "Unknown command %s\n", command);
3189 }
3190 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003191 }
3192#ifdef LIBXML_XPATH_ENABLED
3193 xmlXPathFreeContext(ctxt->pctxt);
3194#endif /* LIBXML_XPATH_ENABLED */
3195 if (ctxt->loaded) {
3196 xmlFreeDoc(ctxt->doc);
3197 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00003198 if (ctxt->filename != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003199 xmlFree(ctxt->filename);
Owen Taylor3473f882001-02-23 17:55:21 +00003200 xmlFree(ctxt);
3201 if (cmdline != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00003202 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003203}
3204
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00003205#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +00003206#define bottom_debugXML
3207#include "elfgcchack.h"
Owen Taylor3473f882001-02-23 17:55:21 +00003208#endif /* LIBXML_DEBUG_ENABLED */