blob: e3b5c3da36ed3cc0630824f28a138f776019f1c2 [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
Bjorn Reese70a9da52001-04-21 16:57:29 +000010#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000011#ifdef LIBXML_DEBUG_ENABLED
12
Owen Taylor3473f882001-02-23 17:55:21 +000013#include <string.h>
14#ifdef HAVE_STDLIB_H
15#include <stdlib.h>
16#endif
17#ifdef HAVE_STRING_H
18#include <string.h>
19#endif
20#include <libxml/xmlmemory.h>
21#include <libxml/tree.h>
22#include <libxml/parser.h>
Daniel Veillard567e1b42001-08-01 15:53:47 +000023#include <libxml/parserInternals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000024#include <libxml/valid.h>
25#include <libxml/debugXML.h>
26#include <libxml/HTMLtree.h>
27#include <libxml/HTMLparser.h>
28#include <libxml/xmlerror.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000029#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000030
Daniel Veillard5e2dace2001-07-18 19:30:27 +000031/**
32 * xmlDebugDumpString:
33 * @output: the FILE * for the output
34 * @str: the string
35 *
36 * Dumps informations about the string, shorten it if necessary
37 */
38void
39xmlDebugDumpString(FILE * output, const xmlChar * str)
40{
Owen Taylor3473f882001-02-23 17:55:21 +000041 int i;
Daniel Veillard5e2dace2001-07-18 19:30:27 +000042
Owen Taylor3473f882001-02-23 17:55:21 +000043 if (str == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +000044 fprintf(output, "(NULL)");
45 return;
Owen Taylor3473f882001-02-23 17:55:21 +000046 }
Daniel Veillard5e2dace2001-07-18 19:30:27 +000047 for (i = 0; i < 40; i++)
48 if (str[i] == 0)
49 return;
50 else if (IS_BLANK(str[i]))
51 fputc(' ', output);
52 else if (str[i] >= 0x80)
53 fprintf(output, "#%X", str[i]);
54 else
55 fputc(str[i], output);
Owen Taylor3473f882001-02-23 17:55:21 +000056 fprintf(output, "...");
57}
58
Daniel Veillard56a4cb82001-03-24 17:00:36 +000059static void
60xmlDebugDumpDtdNode(FILE *output, xmlDtdPtr dtd, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +000061 int i;
62 char shift[100];
63
64 for (i = 0;((i < depth) && (i < 25));i++)
65 shift[2 * i] = shift[2 * i + 1] = ' ';
66 shift[2 * i] = shift[2 * i + 1] = 0;
67
68 fprintf(output, shift);
69
70 if (dtd->type != XML_DTD_NODE) {
71 fprintf(output, "PBM: not a DTD\n");
72 return;
73 }
74 if (dtd->name != NULL)
75 fprintf(output, "DTD(%s)", dtd->name);
76 else
77 fprintf(output, "DTD");
78 if (dtd->ExternalID != NULL)
79 fprintf(output, ", PUBLIC %s", dtd->ExternalID);
80 if (dtd->SystemID != NULL)
81 fprintf(output, ", SYSTEM %s", dtd->SystemID);
82 fprintf(output, "\n");
83 /*
84 * Do a bit of checking
85 */
86 if (dtd->parent == NULL)
Daniel Veillardcbaf3992001-12-31 16:16:02 +000087 fprintf(output, "PBM: DTD has no parent\n");
Owen Taylor3473f882001-02-23 17:55:21 +000088 if (dtd->doc == NULL)
Daniel Veillardcbaf3992001-12-31 16:16:02 +000089 fprintf(output, "PBM: DTD has no doc\n");
Owen Taylor3473f882001-02-23 17:55:21 +000090 if ((dtd->parent != NULL) && (dtd->doc != dtd->parent->doc))
Daniel Veillardcbaf3992001-12-31 16:16:02 +000091 fprintf(output, "PBM: DTD doc differs from parent's one\n");
Owen Taylor3473f882001-02-23 17:55:21 +000092 if (dtd->prev == NULL) {
93 if ((dtd->parent != NULL) && (dtd->parent->children != (xmlNodePtr)dtd))
Daniel Veillardcbaf3992001-12-31 16:16:02 +000094 fprintf(output, "PBM: DTD has no prev and not first of list\n");
Owen Taylor3473f882001-02-23 17:55:21 +000095 } else {
96 if (dtd->prev->next != (xmlNodePtr) dtd)
Daniel Veillardcbaf3992001-12-31 16:16:02 +000097 fprintf(output, "PBM: DTD prev->next : back link wrong\n");
Owen Taylor3473f882001-02-23 17:55:21 +000098 }
99 if (dtd->next == NULL) {
100 if ((dtd->parent != NULL) && (dtd->parent->last != (xmlNodePtr) dtd))
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000101 fprintf(output, "PBM: DTD has no next and not last of list\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000102 } else {
103 if (dtd->next->prev != (xmlNodePtr) dtd)
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000104 fprintf(output, "PBM: DTD next->prev : forward link wrong\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000105 }
106}
107
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000108static void
109xmlDebugDumpAttrDecl(FILE *output, xmlAttributePtr attr, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000110 int i;
111 char shift[100];
112
113 for (i = 0;((i < depth) && (i < 25));i++)
114 shift[2 * i] = shift[2 * i + 1] = ' ';
115 shift[2 * i] = shift[2 * i + 1] = 0;
116
117 fprintf(output, shift);
118
119 if (attr->type != XML_ATTRIBUTE_DECL) {
120 fprintf(output, "PBM: not a Attr\n");
121 return;
122 }
123 if (attr->name != NULL)
124 fprintf(output, "ATTRDECL(%s)", attr->name);
125 else
126 fprintf(output, "PBM ATTRDECL noname!!!");
127 if (attr->elem != NULL)
128 fprintf(output, " for %s", attr->elem);
129 else
130 fprintf(output, " PBM noelem!!!");
131 switch (attr->atype) {
132 case XML_ATTRIBUTE_CDATA:
133 fprintf(output, " CDATA");
134 break;
135 case XML_ATTRIBUTE_ID:
136 fprintf(output, " ID");
137 break;
138 case XML_ATTRIBUTE_IDREF:
139 fprintf(output, " IDREF");
140 break;
141 case XML_ATTRIBUTE_IDREFS:
142 fprintf(output, " IDREFS");
143 break;
144 case XML_ATTRIBUTE_ENTITY:
145 fprintf(output, " ENTITY");
146 break;
147 case XML_ATTRIBUTE_ENTITIES:
148 fprintf(output, " ENTITIES");
149 break;
150 case XML_ATTRIBUTE_NMTOKEN:
151 fprintf(output, " NMTOKEN");
152 break;
153 case XML_ATTRIBUTE_NMTOKENS:
154 fprintf(output, " NMTOKENS");
155 break;
156 case XML_ATTRIBUTE_ENUMERATION:
157 fprintf(output, " ENUMERATION");
158 break;
159 case XML_ATTRIBUTE_NOTATION:
160 fprintf(output, " NOTATION ");
161 break;
162 }
163 if (attr->tree != NULL) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000164 int indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000165 xmlEnumerationPtr cur = attr->tree;
166
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000167 for (indx = 0;indx < 5; indx++) {
168 if (indx != 0)
Owen Taylor3473f882001-02-23 17:55:21 +0000169 fprintf(output, "|%s", cur->name);
170 else
171 fprintf(output, " (%s", cur->name);
172 cur = cur->next;
173 if (cur == NULL) break;
174 }
175 if (cur == NULL)
176 fprintf(output, ")");
177 else
178 fprintf(output, "...)");
179 }
180 switch (attr->def) {
181 case XML_ATTRIBUTE_NONE:
182 break;
183 case XML_ATTRIBUTE_REQUIRED:
184 fprintf(output, " REQUIRED");
185 break;
186 case XML_ATTRIBUTE_IMPLIED:
187 fprintf(output, " IMPLIED");
188 break;
189 case XML_ATTRIBUTE_FIXED:
190 fprintf(output, " FIXED");
191 break;
192 }
193 if (attr->defaultValue != NULL) {
194 fprintf(output, "\"");
195 xmlDebugDumpString(output, attr->defaultValue);
196 fprintf(output, "\"");
197 }
Daniel Veillardcd337f02001-11-22 18:20:37 +0000198 fprintf(output, "\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000199
200 /*
201 * Do a bit of checking
202 */
203 if (attr->parent == NULL)
204 fprintf(output, "PBM: Attr has no parent\n");
205 if (attr->doc == NULL)
206 fprintf(output, "PBM: Attr has no doc\n");
207 if ((attr->parent != NULL) && (attr->doc != attr->parent->doc))
208 fprintf(output, "PBM: Attr doc differs from parent's one\n");
209 if (attr->prev == NULL) {
210 if ((attr->parent != NULL) && (attr->parent->children != (xmlNodePtr)attr))
211 fprintf(output, "PBM: Attr has no prev and not first of list\n");
212 } else {
213 if (attr->prev->next != (xmlNodePtr) attr)
214 fprintf(output, "PBM: Attr prev->next : back link wrong\n");
215 }
216 if (attr->next == NULL) {
217 if ((attr->parent != NULL) && (attr->parent->last != (xmlNodePtr) attr))
218 fprintf(output, "PBM: Attr has no next and not last of list\n");
219 } else {
220 if (attr->next->prev != (xmlNodePtr) attr)
221 fprintf(output, "PBM: Attr next->prev : forward link wrong\n");
222 }
223}
224
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000225static void
226xmlDebugDumpElemDecl(FILE *output, xmlElementPtr elem, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000227 int i;
228 char shift[100];
229
230 for (i = 0;((i < depth) && (i < 25));i++)
231 shift[2 * i] = shift[2 * i + 1] = ' ';
232 shift[2 * i] = shift[2 * i + 1] = 0;
233
234 fprintf(output, shift);
235
236 if (elem->type != XML_ELEMENT_DECL) {
237 fprintf(output, "PBM: not a Elem\n");
238 return;
239 }
240 if (elem->name != NULL) {
241 fprintf(output, "ELEMDECL(");
242 xmlDebugDumpString(output, elem->name);
243 fprintf(output, ")");
244 } else
245 fprintf(output, "PBM ELEMDECL noname!!!");
246 switch (elem->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +0000247 case XML_ELEMENT_TYPE_UNDEFINED:
248 fprintf(output, ", UNDEFINED");
249 break;
Owen Taylor3473f882001-02-23 17:55:21 +0000250 case XML_ELEMENT_TYPE_EMPTY:
251 fprintf(output, ", EMPTY");
252 break;
253 case XML_ELEMENT_TYPE_ANY:
254 fprintf(output, ", ANY");
255 break;
256 case XML_ELEMENT_TYPE_MIXED:
257 fprintf(output, ", MIXED ");
258 break;
259 case XML_ELEMENT_TYPE_ELEMENT:
260 fprintf(output, ", MIXED ");
261 break;
262 }
Daniel Veillard7db37732001-07-12 01:20:08 +0000263 if ((elem->type != XML_ELEMENT_NODE) &&
264 (elem->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000265 char buf[5001];
266
267 buf[0] = 0;
Daniel Veillardd3d06722001-08-15 12:06:36 +0000268 xmlSnprintfElementContent(buf, 5000, elem->content, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000269 buf[5000] = 0;
270 fprintf(output, "%s", buf);
271 }
Daniel Veillardcd337f02001-11-22 18:20:37 +0000272 fprintf(output, "\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000273
274 /*
275 * Do a bit of checking
276 */
277 if (elem->parent == NULL)
278 fprintf(output, "PBM: Elem has no parent\n");
279 if (elem->doc == NULL)
280 fprintf(output, "PBM: Elem has no doc\n");
281 if ((elem->parent != NULL) && (elem->doc != elem->parent->doc))
282 fprintf(output, "PBM: Elem doc differs from parent's one\n");
283 if (elem->prev == NULL) {
284 if ((elem->parent != NULL) && (elem->parent->children != (xmlNodePtr)elem))
285 fprintf(output, "PBM: Elem has no prev and not first of list\n");
286 } else {
287 if (elem->prev->next != (xmlNodePtr) elem)
288 fprintf(output, "PBM: Elem prev->next : back link wrong\n");
289 }
290 if (elem->next == NULL) {
291 if ((elem->parent != NULL) && (elem->parent->last != (xmlNodePtr) elem))
292 fprintf(output, "PBM: Elem has no next and not last of list\n");
293 } else {
294 if (elem->next->prev != (xmlNodePtr) elem)
295 fprintf(output, "PBM: Elem next->prev : forward link wrong\n");
296 }
297}
298
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000299static void
300xmlDebugDumpEntityDecl(FILE *output, xmlEntityPtr ent, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000301 int i;
302 char shift[100];
303
304 for (i = 0;((i < depth) && (i < 25));i++)
305 shift[2 * i] = shift[2 * i + 1] = ' ';
306 shift[2 * i] = shift[2 * i + 1] = 0;
307
308 fprintf(output, shift);
309
310 if (ent->type != XML_ENTITY_DECL) {
311 fprintf(output, "PBM: not a Entity decl\n");
312 return;
313 }
314 if (ent->name != NULL) {
315 fprintf(output, "ENTITYDECL(");
316 xmlDebugDumpString(output, ent->name);
317 fprintf(output, ")");
318 } else
319 fprintf(output, "PBM ENTITYDECL noname!!!");
320 switch (ent->etype) {
321 case XML_INTERNAL_GENERAL_ENTITY:
322 fprintf(output, ", internal\n");
323 break;
324 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
325 fprintf(output, ", external parsed\n");
326 break;
327 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
328 fprintf(output, ", unparsed\n");
329 break;
330 case XML_INTERNAL_PARAMETER_ENTITY:
331 fprintf(output, ", parameter\n");
332 break;
333 case XML_EXTERNAL_PARAMETER_ENTITY:
334 fprintf(output, ", external parameter\n");
335 break;
336 case XML_INTERNAL_PREDEFINED_ENTITY:
337 fprintf(output, ", predefined\n");
338 break;
339 }
340 if (ent->ExternalID) {
341 fprintf(output, shift);
342 fprintf(output, " ExternalID=%s\n", ent->ExternalID);
343 }
344 if (ent->SystemID) {
345 fprintf(output, shift);
346 fprintf(output, " SystemID=%s\n", ent->SystemID);
347 }
348 if (ent->URI != NULL) {
349 fprintf(output, shift);
350 fprintf(output, " URI=%s\n", ent->URI);
351 }
352 if (ent->content) {
353 fprintf(output, shift);
354 fprintf(output, " content=");
355 xmlDebugDumpString(output, ent->content);
356 fprintf(output, "\n");
357 }
358
359 /*
360 * Do a bit of checking
361 */
362 if (ent->parent == NULL)
363 fprintf(output, "PBM: Ent has no parent\n");
364 if (ent->doc == NULL)
365 fprintf(output, "PBM: Ent has no doc\n");
366 if ((ent->parent != NULL) && (ent->doc != ent->parent->doc))
367 fprintf(output, "PBM: Ent doc differs from parent's one\n");
368 if (ent->prev == NULL) {
369 if ((ent->parent != NULL) && (ent->parent->children != (xmlNodePtr)ent))
370 fprintf(output, "PBM: Ent has no prev and not first of list\n");
371 } else {
372 if (ent->prev->next != (xmlNodePtr) ent)
373 fprintf(output, "PBM: Ent prev->next : back link wrong\n");
374 }
375 if (ent->next == NULL) {
376 if ((ent->parent != NULL) && (ent->parent->last != (xmlNodePtr) ent))
377 fprintf(output, "PBM: Ent has no next and not last of list\n");
378 } else {
379 if (ent->next->prev != (xmlNodePtr) ent)
380 fprintf(output, "PBM: Ent next->prev : forward link wrong\n");
381 }
382}
383
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000384static void
385xmlDebugDumpNamespace(FILE *output, xmlNsPtr ns, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000386 int i;
387 char shift[100];
388
389 for (i = 0;((i < depth) && (i < 25));i++)
390 shift[2 * i] = shift[2 * i + 1] = ' ';
391 shift[2 * i] = shift[2 * i + 1] = 0;
392
393 fprintf(output, shift);
394 if (ns->type != XML_NAMESPACE_DECL) {
395 fprintf(output, "invalid namespace node %d\n", ns->type);
396 return;
397 }
398 if (ns->href == NULL) {
399 if (ns->prefix != NULL)
400 fprintf(output, "incomplete namespace %s href=NULL\n", ns->prefix);
401 else
402 fprintf(output, "incomplete default namespace href=NULL\n");
403 } else {
404 if (ns->prefix != NULL)
405 fprintf(output, "namespace %s href=", ns->prefix);
406 else
407 fprintf(output, "default namespace href=");
408
409 xmlDebugDumpString(output, ns->href);
410 fprintf(output, "\n");
411 }
412}
413
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000414static void
415xmlDebugDumpNamespaceList(FILE *output, xmlNsPtr ns, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000416 while (ns != NULL) {
417 xmlDebugDumpNamespace(output, ns, depth);
418 ns = ns->next;
419 }
420}
421
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000422static void
423xmlDebugDumpEntity(FILE *output, xmlEntityPtr ent, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000424 int i;
425 char shift[100];
426
427 for (i = 0;((i < depth) && (i < 25));i++)
428 shift[2 * i] = shift[2 * i + 1] = ' ';
429 shift[2 * i] = shift[2 * i + 1] = 0;
430
431 fprintf(output, shift);
432 switch (ent->etype) {
433 case XML_INTERNAL_GENERAL_ENTITY:
434 fprintf(output, "INTERNAL_GENERAL_ENTITY ");
435 break;
436 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
437 fprintf(output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
438 break;
439 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
440 fprintf(output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
441 break;
442 case XML_INTERNAL_PARAMETER_ENTITY:
443 fprintf(output, "INTERNAL_PARAMETER_ENTITY ");
444 break;
445 case XML_EXTERNAL_PARAMETER_ENTITY:
446 fprintf(output, "EXTERNAL_PARAMETER_ENTITY ");
447 break;
448 default:
449 fprintf(output, "ENTITY_%d ! ", ent->etype);
450 }
451 fprintf(output, "%s\n", ent->name);
452 if (ent->ExternalID) {
453 fprintf(output, shift);
454 fprintf(output, "ExternalID=%s\n", ent->ExternalID);
455 }
456 if (ent->SystemID) {
457 fprintf(output, shift);
458 fprintf(output, "SystemID=%s\n", ent->SystemID);
459 }
460 if (ent->URI) {
461 fprintf(output, shift);
462 fprintf(output, "URI=%s\n", ent->URI);
463 }
464 if (ent->content) {
465 fprintf(output, shift);
466 fprintf(output, "content=");
467 xmlDebugDumpString(output, ent->content);
468 fprintf(output, "\n");
469 }
470}
471
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000472/**
473 * xmlDebugDumpAttr:
474 * @output: the FILE * for the output
475 * @attr: the attribute
476 * @depth: the indentation level.
477 *
478 * Dumps debug information for the attribute
479 */
480void
481xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000482 int i;
483 char shift[100];
484
485 for (i = 0;((i < depth) && (i < 25));i++)
486 shift[2 * i] = shift[2 * i + 1] = ' ';
487 shift[2 * i] = shift[2 * i + 1] = 0;
488
489 fprintf(output, shift);
490
491 fprintf(output, "ATTRIBUTE ");
492 xmlDebugDumpString(output, attr->name);
493 fprintf(output, "\n");
494 if (attr->children != NULL)
495 xmlDebugDumpNodeList(output, attr->children, depth + 1);
496
497 /*
498 * Do a bit of checking
499 */
500 if (attr->parent == NULL)
501 fprintf(output, "PBM: Attr has no parent\n");
502 if (attr->doc == NULL)
503 fprintf(output, "PBM: Attr has no doc\n");
504 if ((attr->parent != NULL) && (attr->doc != attr->parent->doc))
505 fprintf(output, "PBM: Attr doc differs from parent's one\n");
506 if (attr->prev == NULL) {
507 if ((attr->parent != NULL) && (attr->parent->properties != attr))
508 fprintf(output, "PBM: Attr has no prev and not first of list\n");
509 } else {
510 if (attr->prev->next != attr)
511 fprintf(output, "PBM: Attr prev->next : back link wrong\n");
512 }
513 if (attr->next != NULL) {
514 if (attr->next->prev != attr)
515 fprintf(output, "PBM: Attr next->prev : forward link wrong\n");
516 }
517}
518
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000519/**
520 * xmlDebugDumpAttrList:
521 * @output: the FILE * for the output
522 * @attr: the attribute list
523 * @depth: the indentation level.
524 *
525 * Dumps debug information for the attribute list
526 */
527void
528xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
529{
Owen Taylor3473f882001-02-23 17:55:21 +0000530 while (attr != NULL) {
531 xmlDebugDumpAttr(output, attr, depth);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000532 attr = attr->next;
Owen Taylor3473f882001-02-23 17:55:21 +0000533 }
534}
535
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000536/**
537 * xmlDebugDumpOneNode:
538 * @output: the FILE * for the output
539 * @node: the node
540 * @depth: the indentation level.
541 *
542 * Dumps debug information for the element node, it is not recursive
543 */
544void
545xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
546{
Owen Taylor3473f882001-02-23 17:55:21 +0000547 int i;
548 char shift[100];
549
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000550 for (i = 0; ((i < depth) && (i < 25)); i++)
Owen Taylor3473f882001-02-23 17:55:21 +0000551 shift[2 * i] = shift[2 * i + 1] = ' ';
552 shift[2 * i] = shift[2 * i + 1] = 0;
553
554 switch (node->type) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000555 case XML_ELEMENT_NODE:
556 fprintf(output, shift);
557 fprintf(output, "ELEMENT ");
558 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
559 xmlDebugDumpString(output, node->ns->prefix);
560 fprintf(output, ":");
561 }
562 xmlDebugDumpString(output, node->name);
563 fprintf(output, "\n");
564 break;
565 case XML_ATTRIBUTE_NODE:
566 fprintf(output, shift);
567 fprintf(output, "Error, ATTRIBUTE found here\n");
568 break;
569 case XML_TEXT_NODE:
570 fprintf(output, shift);
Daniel Veillardb44025c2001-10-11 22:55:55 +0000571 if (node->name == (const xmlChar *) xmlStringTextNoenc)
Daniel Veillard567e1b42001-08-01 15:53:47 +0000572 fprintf(output, "TEXT no enc\n");
573 else
574 fprintf(output, "TEXT\n");
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000575 break;
576 case XML_CDATA_SECTION_NODE:
577 fprintf(output, shift);
578 fprintf(output, "CDATA_SECTION\n");
579 break;
580 case XML_ENTITY_REF_NODE:
581 fprintf(output, shift);
582 fprintf(output, "ENTITY_REF(%s)\n", node->name);
583 break;
584 case XML_ENTITY_NODE:
585 fprintf(output, shift);
586 fprintf(output, "ENTITY\n");
587 break;
588 case XML_PI_NODE:
589 fprintf(output, shift);
590 fprintf(output, "PI %s\n", node->name);
591 break;
592 case XML_COMMENT_NODE:
593 fprintf(output, shift);
594 fprintf(output, "COMMENT\n");
595 break;
596 case XML_DOCUMENT_NODE:
597 case XML_HTML_DOCUMENT_NODE:
598 fprintf(output, shift);
599 fprintf(output, "Error, DOCUMENT found here\n");
600 break;
601 case XML_DOCUMENT_TYPE_NODE:
602 fprintf(output, shift);
603 fprintf(output, "DOCUMENT_TYPE\n");
604 break;
605 case XML_DOCUMENT_FRAG_NODE:
606 fprintf(output, shift);
607 fprintf(output, "DOCUMENT_FRAG\n");
608 break;
609 case XML_NOTATION_NODE:
610 fprintf(output, shift);
611 fprintf(output, "NOTATION\n");
612 break;
613 case XML_DTD_NODE:
614 xmlDebugDumpDtdNode(output, (xmlDtdPtr) node, depth);
615 return;
616 case XML_ELEMENT_DECL:
617 xmlDebugDumpElemDecl(output, (xmlElementPtr) node, depth);
618 return;
619 case XML_ATTRIBUTE_DECL:
620 xmlDebugDumpAttrDecl(output, (xmlAttributePtr) node, depth);
621 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000622 case XML_ENTITY_DECL:
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000623 xmlDebugDumpEntityDecl(output, (xmlEntityPtr) node, depth);
624 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000625 case XML_NAMESPACE_DECL:
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000626 xmlDebugDumpNamespace(output, (xmlNsPtr) node, depth);
627 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000628 case XML_XINCLUDE_START:
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000629 fprintf(output, shift);
630 fprintf(output, "INCLUDE START\n");
631 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000632 case XML_XINCLUDE_END:
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000633 fprintf(output, shift);
634 fprintf(output, "INCLUDE END\n");
635 return;
636 default:
637 fprintf(output, shift);
638 fprintf(output, "NODE_%d !!!\n", node->type);
639 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000640 }
641 if (node->doc == NULL) {
642 fprintf(output, shift);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000643 fprintf(output, "doc == NULL !!!\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000644 }
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000645 if (node->nsDef != NULL)
Owen Taylor3473f882001-02-23 17:55:21 +0000646 xmlDebugDumpNamespaceList(output, node->nsDef, depth + 1);
647 if (node->properties != NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000648 xmlDebugDumpAttrList(output, node->properties, depth + 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000649 if (node->type != XML_ENTITY_REF_NODE) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000650 if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
651 shift[2 * i] = shift[2 * i + 1] = ' ';
652 shift[2 * i + 2] = shift[2 * i + 3] = 0;
653 fprintf(output, shift);
654 fprintf(output, "content=");
655#ifndef XML_USE_BUFFER_CONTENT
656 xmlDebugDumpString(output, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +0000657#else
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000658 xmlDebugDumpString(output, xmlBufferContent(node->content));
Owen Taylor3473f882001-02-23 17:55:21 +0000659#endif
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000660 fprintf(output, "\n");
661 }
Owen Taylor3473f882001-02-23 17:55:21 +0000662 } else {
663 xmlEntityPtr ent;
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000664
665 ent = xmlGetDocEntity(node->doc, node->name);
666 if (ent != NULL)
667 xmlDebugDumpEntity(output, ent, depth + 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000668 }
669 /*
670 * Do a bit of checking
671 */
672 if (node->parent == NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000673 fprintf(output, "PBM: Node has no parent\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000674 if (node->doc == NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000675 fprintf(output, "PBM: Node has no doc\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000676 if ((node->parent != NULL) && (node->doc != node->parent->doc))
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000677 fprintf(output, "PBM: Node doc differs from parent's one\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000678 if (node->prev == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000679 if ((node->parent != NULL) && (node->parent->children != node))
680 fprintf(output,
681 "PBM: Node has no prev and not first of list\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000682 } else {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000683 if (node->prev->next != node)
684 fprintf(output, "PBM: Node prev->next : back link wrong\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000685 }
686 if (node->next == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000687 if ((node->parent != NULL) && (node->parent->last != node))
688 fprintf(output,
689 "PBM: Node has no next and not last of list\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000690 } else {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000691 if (node->next->prev != node)
692 fprintf(output, "PBM: Node next->prev : forward link wrong\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000693 }
694}
695
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000696/**
697 * xmlDebugDumpNode:
698 * @output: the FILE * for the output
699 * @node: the node
700 * @depth: the indentation level.
701 *
702 * Dumps debug information for the element node, it is recursive
703 */
704void
705xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
706{
Owen Taylor3473f882001-02-23 17:55:21 +0000707 xmlDebugDumpOneNode(output, node, depth);
708 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE))
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000709 xmlDebugDumpNodeList(output, node->children, depth + 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000710}
711
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000712/**
713 * xmlDebugDumpNodeList:
714 * @output: the FILE * for the output
715 * @node: the node list
716 * @depth: the indentation level.
717 *
718 * Dumps debug information for the list of element node, it is recursive
719 */
720void
721xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
722{
Owen Taylor3473f882001-02-23 17:55:21 +0000723 while (node != NULL) {
724 xmlDebugDumpNode(output, node, depth);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000725 node = node->next;
Owen Taylor3473f882001-02-23 17:55:21 +0000726 }
727}
728
729
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000730/**
731 * xmlDebugDumpDocumentHead:
732 * @output: the FILE * for the output
733 * @doc: the document
734 *
735 * Dumps debug information cncerning the document, not recursive
736 */
737void
738xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
739{
740 if (output == NULL)
741 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +0000742 if (doc == NULL) {
743 fprintf(output, "DOCUMENT == NULL !\n");
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000744 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000745 }
746
747 switch (doc->type) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000748 case XML_ELEMENT_NODE:
749 fprintf(output, "Error, ELEMENT found here ");
750 break;
751 case XML_ATTRIBUTE_NODE:
752 fprintf(output, "Error, ATTRIBUTE found here\n");
753 break;
754 case XML_TEXT_NODE:
755 fprintf(output, "Error, TEXT\n");
756 break;
757 case XML_CDATA_SECTION_NODE:
758 fprintf(output, "Error, CDATA_SECTION\n");
759 break;
760 case XML_ENTITY_REF_NODE:
761 fprintf(output, "Error, ENTITY_REF\n");
762 break;
763 case XML_ENTITY_NODE:
764 fprintf(output, "Error, ENTITY\n");
765 break;
766 case XML_PI_NODE:
767 fprintf(output, "Error, PI\n");
768 break;
769 case XML_COMMENT_NODE:
770 fprintf(output, "Error, COMMENT\n");
771 break;
772 case XML_DOCUMENT_NODE:
773 fprintf(output, "DOCUMENT\n");
774 break;
775 case XML_HTML_DOCUMENT_NODE:
776 fprintf(output, "HTML DOCUMENT\n");
777 break;
778 case XML_DOCUMENT_TYPE_NODE:
779 fprintf(output, "Error, DOCUMENT_TYPE\n");
780 break;
781 case XML_DOCUMENT_FRAG_NODE:
782 fprintf(output, "Error, DOCUMENT_FRAG\n");
783 break;
784 case XML_NOTATION_NODE:
785 fprintf(output, "Error, NOTATION\n");
786 break;
787 default:
788 fprintf(output, "NODE_%d\n", doc->type);
Owen Taylor3473f882001-02-23 17:55:21 +0000789 }
790 if (doc->name != NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000791 fprintf(output, "name=");
Owen Taylor3473f882001-02-23 17:55:21 +0000792 xmlDebugDumpString(output, BAD_CAST doc->name);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000793 fprintf(output, "\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000794 }
795 if (doc->version != NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000796 fprintf(output, "version=");
Owen Taylor3473f882001-02-23 17:55:21 +0000797 xmlDebugDumpString(output, doc->version);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000798 fprintf(output, "\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000799 }
800 if (doc->encoding != NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000801 fprintf(output, "encoding=");
Owen Taylor3473f882001-02-23 17:55:21 +0000802 xmlDebugDumpString(output, doc->encoding);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000803 fprintf(output, "\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000804 }
805 if (doc->URL != NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000806 fprintf(output, "URL=");
Owen Taylor3473f882001-02-23 17:55:21 +0000807 xmlDebugDumpString(output, doc->URL);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000808 fprintf(output, "\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000809 }
810 if (doc->standalone)
811 fprintf(output, "standalone=true\n");
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000812 if (doc->oldNs != NULL)
Owen Taylor3473f882001-02-23 17:55:21 +0000813 xmlDebugDumpNamespaceList(output, doc->oldNs, 0);
814}
815
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000816/**
817 * xmlDebugDumpDocument:
818 * @output: the FILE * for the output
819 * @doc: the document
820 *
821 * Dumps debug information for the document, it's recursive
822 */
823void
824xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
825{
826 if (output == NULL)
827 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +0000828 if (doc == NULL) {
829 fprintf(output, "DOCUMENT == NULL !\n");
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000830 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000831 }
832 xmlDebugDumpDocumentHead(output, doc);
833 if (((doc->type == XML_DOCUMENT_NODE) ||
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000834 (doc->type == XML_HTML_DOCUMENT_NODE)) && (doc->children != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +0000835 xmlDebugDumpNodeList(output, doc->children, 1);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000836}
Owen Taylor3473f882001-02-23 17:55:21 +0000837
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000838/**
839 * xmlDebugDumpDTD:
840 * @output: the FILE * for the output
841 * @dtd: the DTD
842 *
843 * Dumps debug information for the DTD
844 */
845void
846xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
847{
Owen Taylor3473f882001-02-23 17:55:21 +0000848 if (dtd == NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000849 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000850 if (dtd->type != XML_DTD_NODE) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000851 fprintf(output, "PBM: not a DTD\n");
852 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000853 }
854 if (dtd->name != NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000855 fprintf(output, "DTD(%s)", dtd->name);
Owen Taylor3473f882001-02-23 17:55:21 +0000856 else
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000857 fprintf(output, "DTD");
Owen Taylor3473f882001-02-23 17:55:21 +0000858 if (dtd->ExternalID != NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000859 fprintf(output, ", PUBLIC %s", dtd->ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +0000860 if (dtd->SystemID != NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000861 fprintf(output, ", SYSTEM %s", dtd->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +0000862 fprintf(output, "\n");
863 /*
864 * Do a bit of checking
865 */
866 if ((dtd->parent != NULL) && (dtd->doc != dtd->parent->doc))
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000867 fprintf(output, "PBM: DTD doc differs from parent's one\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000868 if (dtd->prev == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000869 if ((dtd->parent != NULL)
870 && (dtd->parent->children != (xmlNodePtr) dtd))
871 fprintf(output,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000872 "PBM: DTD has no prev and not first of list\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000873 } else {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000874 if (dtd->prev->next != (xmlNodePtr) dtd)
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000875 fprintf(output, "PBM: DTD prev->next : back link wrong\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000876 }
877 if (dtd->next == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000878 if ((dtd->parent != NULL)
879 && (dtd->parent->last != (xmlNodePtr) dtd))
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000880 fprintf(output, "PBM: DTD has no next and not last of list\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000881 } else {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000882 if (dtd->next->prev != (xmlNodePtr) dtd)
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000883 fprintf(output, "PBM: DTD next->prev : forward link wrong\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000884 }
885 if (dtd->children == NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000886 fprintf(output, " DTD is empty\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000887 else
888 xmlDebugDumpNodeList(output, dtd->children, 1);
889}
890
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000891static void
892xmlDebugDumpEntityCallback(xmlEntityPtr cur, FILE *output) {
Owen Taylor3473f882001-02-23 17:55:21 +0000893 fprintf(output, "%s : ", cur->name);
894 switch (cur->etype) {
895 case XML_INTERNAL_GENERAL_ENTITY:
896 fprintf(output, "INTERNAL GENERAL, ");
897 break;
898 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
899 fprintf(output, "EXTERNAL PARSED, ");
900 break;
901 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
902 fprintf(output, "EXTERNAL UNPARSED, ");
903 break;
904 case XML_INTERNAL_PARAMETER_ENTITY:
905 fprintf(output, "INTERNAL PARAMETER, ");
906 break;
907 case XML_EXTERNAL_PARAMETER_ENTITY:
908 fprintf(output, "EXTERNAL PARAMETER, ");
909 break;
910 default:
911 fprintf(output, "UNKNOWN TYPE %d",
912 cur->etype);
913 }
914 if (cur->ExternalID != NULL)
915 fprintf(output, "ID \"%s\"", cur->ExternalID);
916 if (cur->SystemID != NULL)
917 fprintf(output, "SYSTEM \"%s\"", cur->SystemID);
918 if (cur->orig != NULL)
919 fprintf(output, "\n orig \"%s\"", cur->orig);
Daniel Veillard7db37732001-07-12 01:20:08 +0000920 if ((cur->type != XML_ELEMENT_NODE) &&
921 (cur->content != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +0000922 fprintf(output, "\n content \"%s\"", cur->content);
923 fprintf(output, "\n");
924}
925
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000926/**
927 * xmlDebugDumpEntities:
928 * @output: the FILE * for the output
929 * @doc: the document
930 *
931 * Dumps debug information for all the entities in use by the document
932 */
933void
934xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
935{
936 if (output == NULL)
937 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +0000938 if (doc == NULL) {
939 fprintf(output, "DOCUMENT == NULL !\n");
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000940 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000941 }
942
943 switch (doc->type) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000944 case XML_ELEMENT_NODE:
945 fprintf(output, "Error, ELEMENT found here ");
946 break;
947 case XML_ATTRIBUTE_NODE:
948 fprintf(output, "Error, ATTRIBUTE found here\n");
949 break;
950 case XML_TEXT_NODE:
951 fprintf(output, "Error, TEXT\n");
952 break;
953 case XML_CDATA_SECTION_NODE:
954 fprintf(output, "Error, CDATA_SECTION\n");
955 break;
956 case XML_ENTITY_REF_NODE:
957 fprintf(output, "Error, ENTITY_REF\n");
958 break;
959 case XML_ENTITY_NODE:
960 fprintf(output, "Error, ENTITY\n");
961 break;
962 case XML_PI_NODE:
963 fprintf(output, "Error, PI\n");
964 break;
965 case XML_COMMENT_NODE:
966 fprintf(output, "Error, COMMENT\n");
967 break;
968 case XML_DOCUMENT_NODE:
969 fprintf(output, "DOCUMENT\n");
970 break;
971 case XML_HTML_DOCUMENT_NODE:
972 fprintf(output, "HTML DOCUMENT\n");
973 break;
974 case XML_DOCUMENT_TYPE_NODE:
975 fprintf(output, "Error, DOCUMENT_TYPE\n");
976 break;
977 case XML_DOCUMENT_FRAG_NODE:
978 fprintf(output, "Error, DOCUMENT_FRAG\n");
979 break;
980 case XML_NOTATION_NODE:
981 fprintf(output, "Error, NOTATION\n");
982 break;
983 default:
984 fprintf(output, "NODE_%d\n", doc->type);
Owen Taylor3473f882001-02-23 17:55:21 +0000985 }
986 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000987 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
988 doc->intSubset->entities;
989
990 fprintf(output, "Entities in internal subset\n");
991 xmlHashScan(table, (xmlHashScanner) xmlDebugDumpEntityCallback,
992 output);
Owen Taylor3473f882001-02-23 17:55:21 +0000993 } else
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000994 fprintf(output, "No entities in internal subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000995 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000996 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
997 doc->extSubset->entities;
998
999 fprintf(output, "Entities in external subset\n");
1000 xmlHashScan(table, (xmlHashScanner) xmlDebugDumpEntityCallback,
1001 output);
Owen Taylor3473f882001-02-23 17:55:21 +00001002 } else
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001003 fprintf(output, "No entities in external subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001004}
1005
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001006/**
1007 * xmlLsCountNode:
1008 * @node: the node to count
1009 *
1010 * Count the children of @node.
1011 *
1012 * Returns the number of children of @node.
1013 */
1014int
1015xmlLsCountNode(xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00001016 int ret = 0;
1017 xmlNodePtr list = NULL;
1018
1019 switch (node->type) {
1020 case XML_ELEMENT_NODE:
1021 list = node->children;
1022 break;
1023 case XML_DOCUMENT_NODE:
1024 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00001025#ifdef LIBXML_DOCB_ENABLED
1026 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001027#endif
1028 list = ((xmlDocPtr) node)->children;
1029 break;
1030 case XML_ATTRIBUTE_NODE:
1031 list = ((xmlAttrPtr) node)->children;
1032 break;
1033 case XML_TEXT_NODE:
1034 case XML_CDATA_SECTION_NODE:
1035 case XML_PI_NODE:
1036 case XML_COMMENT_NODE:
1037 if (node->content != NULL) {
1038#ifndef XML_USE_BUFFER_CONTENT
1039 ret = xmlStrlen(node->content);
1040#else
1041 ret = xmlBufferLength(node->content);
1042#endif
1043 }
1044 break;
1045 case XML_ENTITY_REF_NODE:
1046 case XML_DOCUMENT_TYPE_NODE:
1047 case XML_ENTITY_NODE:
1048 case XML_DOCUMENT_FRAG_NODE:
1049 case XML_NOTATION_NODE:
1050 case XML_DTD_NODE:
1051 case XML_ELEMENT_DECL:
1052 case XML_ATTRIBUTE_DECL:
1053 case XML_ENTITY_DECL:
1054 case XML_NAMESPACE_DECL:
1055 case XML_XINCLUDE_START:
1056 case XML_XINCLUDE_END:
1057 ret = 1;
1058 break;
1059 }
1060 for (;list != NULL;ret++)
1061 list = list->next;
1062 return(ret);
1063}
1064
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001065/**
1066 * xmlLsOneNode:
1067 * @output: the FILE * for the output
1068 * @node: the node to dump
1069 *
1070 * Dump to @output the type and name of @node.
1071 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001072void
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001073xmlLsOneNode(FILE *output, xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00001074 switch (node->type) {
1075 case XML_ELEMENT_NODE:
1076 fprintf(output, "-");
1077 break;
1078 case XML_ATTRIBUTE_NODE:
1079 fprintf(output, "a");
1080 break;
1081 case XML_TEXT_NODE:
1082 fprintf(output, "t");
1083 break;
1084 case XML_CDATA_SECTION_NODE:
1085 fprintf(output, "c");
1086 break;
1087 case XML_ENTITY_REF_NODE:
1088 fprintf(output, "e");
1089 break;
1090 case XML_ENTITY_NODE:
1091 fprintf(output, "E");
1092 break;
1093 case XML_PI_NODE:
1094 fprintf(output, "p");
1095 break;
1096 case XML_COMMENT_NODE:
1097 fprintf(output, "c");
1098 break;
1099 case XML_DOCUMENT_NODE:
1100 fprintf(output, "d");
1101 break;
1102 case XML_HTML_DOCUMENT_NODE:
1103 fprintf(output, "h");
1104 break;
1105 case XML_DOCUMENT_TYPE_NODE:
1106 fprintf(output, "T");
1107 break;
1108 case XML_DOCUMENT_FRAG_NODE:
1109 fprintf(output, "F");
1110 break;
1111 case XML_NOTATION_NODE:
1112 fprintf(output, "N");
1113 break;
1114 default:
1115 fprintf(output, "?");
1116 }
1117 if (node->properties != NULL)
1118 fprintf(output, "a");
1119 else
1120 fprintf(output, "-");
1121 if (node->nsDef != NULL)
1122 fprintf(output, "n");
1123 else
1124 fprintf(output, "-");
1125
1126 fprintf(output, " %8d ", xmlLsCountNode(node));
1127
1128 switch (node->type) {
1129 case XML_ELEMENT_NODE:
1130 if (node->name != NULL)
1131 fprintf(output, "%s", node->name);
1132 break;
1133 case XML_ATTRIBUTE_NODE:
1134 if (node->name != NULL)
1135 fprintf(output, "%s", node->name);
1136 break;
1137 case XML_TEXT_NODE:
1138 if (node->content != NULL) {
1139#ifndef XML_USE_BUFFER_CONTENT
1140 xmlDebugDumpString(output, node->content);
1141#else
1142 xmlDebugDumpString(output, xmlBufferContent(node->content));
1143#endif
1144 }
1145 break;
1146 case XML_CDATA_SECTION_NODE:
1147 break;
1148 case XML_ENTITY_REF_NODE:
1149 if (node->name != NULL)
1150 fprintf(output, "%s", node->name);
1151 break;
1152 case XML_ENTITY_NODE:
1153 if (node->name != NULL)
1154 fprintf(output, "%s", node->name);
1155 break;
1156 case XML_PI_NODE:
1157 if (node->name != NULL)
1158 fprintf(output, "%s", node->name);
1159 break;
1160 case XML_COMMENT_NODE:
1161 break;
1162 case XML_DOCUMENT_NODE:
1163 break;
1164 case XML_HTML_DOCUMENT_NODE:
1165 break;
1166 case XML_DOCUMENT_TYPE_NODE:
1167 break;
1168 case XML_DOCUMENT_FRAG_NODE:
1169 break;
1170 case XML_NOTATION_NODE:
1171 break;
1172 default:
1173 if (node->name != NULL)
1174 fprintf(output, "%s", node->name);
1175 }
1176 fprintf(output, "\n");
1177}
1178
Daniel Veillard78d12092001-10-11 09:12:24 +00001179/**
1180 * xmlBoolToText:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001181 * @boolval: a bool to turn into text
Daniel Veillard78d12092001-10-11 09:12:24 +00001182 *
1183 * Convenient way to turn bool into text
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001184 *
1185 * Returns a pointer to either "True" or "False"
1186 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001187const char *
Daniel Veillardebd38c52001-11-01 08:38:12 +00001188xmlBoolToText(int boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001189{
Daniel Veillardebd38c52001-11-01 08:38:12 +00001190 if (boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001191 return("True");
1192 else
1193 return("False");
1194}
1195
Owen Taylor3473f882001-02-23 17:55:21 +00001196/****************************************************************
1197 * *
1198 * The XML shell related functions *
1199 * *
1200 ****************************************************************/
1201
Daniel Veillard78d12092001-10-11 09:12:24 +00001202
1203
Owen Taylor3473f882001-02-23 17:55:21 +00001204/*
1205 * TODO: Improvement/cleanups for the XML shell
1206 * - allow to shell out an editor on a subpart
1207 * - cleanup function registrations (with help) and calling
1208 * - provide registration routines
1209 */
1210
1211/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001212 * xmlShellPrintXPathError:
Daniel Veillard78d12092001-10-11 09:12:24 +00001213 * @errorType: valid xpath error id
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001214 * @arg: the argument that cause xpath to fail
Daniel Veillard78d12092001-10-11 09:12:24 +00001215 *
1216 * Print the xpath error to libxml default error channel
1217 */
1218void
1219xmlShellPrintXPathError(int errorType, const char *arg)
1220{
1221 const char *default_arg = "Result";
1222
1223 if (!arg)
1224 arg = default_arg;
1225
1226 switch (errorType) {
1227 case XPATH_UNDEFINED:
1228 xmlGenericError(xmlGenericErrorContext,
1229 "%s: no such node\n", arg);
1230 break;
1231
1232 case XPATH_BOOLEAN:
1233 xmlGenericError(xmlGenericErrorContext,
1234 "%s is a Boolean\n", arg);
1235 break;
1236 case XPATH_NUMBER:
1237 xmlGenericError(xmlGenericErrorContext,
1238 "%s is a number\n", arg);
1239 break;
1240 case XPATH_STRING:
1241 xmlGenericError(xmlGenericErrorContext,
1242 "%s is a string\n", arg);
1243 break;
1244 case XPATH_POINT:
1245 xmlGenericError(xmlGenericErrorContext,
1246 "%s is a point\n", arg);
1247 break;
1248 case XPATH_RANGE:
1249 xmlGenericError(xmlGenericErrorContext,
1250 "%s is a range\n", arg);
1251 break;
1252 case XPATH_LOCATIONSET:
1253 xmlGenericError(xmlGenericErrorContext,
1254 "%s is a range\n", arg);
1255 break;
1256 case XPATH_USERS:
1257 xmlGenericError(xmlGenericErrorContext,
1258 "%s is user-defined\n", arg);
1259 break;
1260 case XPATH_XSLT_TREE:
1261 xmlGenericError(xmlGenericErrorContext,
1262 "%s is an XSLT value tree\n", arg);
1263 break;
1264 }
1265 xmlGenericError(xmlGenericErrorContext,
1266 "Try casting the result string function (xpath builtin)\n",
1267 arg);
1268}
1269
1270
1271/**
1272 * xmlShellPrintNode:
1273 * @node : a non-null node to print to stdout
1274 *
1275 * Print node to stdout
1276 */
1277void
1278xmlShellPrintNode(xmlNodePtr node)
1279{
1280 if (!node)
1281 return;
1282
1283 if (node->type == XML_DOCUMENT_NODE)
1284 xmlDocDump(stdout, (xmlDocPtr) node);
1285 else if (node->type == XML_ATTRIBUTE_NODE)
1286 xmlDebugDumpAttrList(stdout, (xmlAttrPtr) node, 0);
1287 else
1288 xmlElemDump(stdout, node->doc, node);
1289
1290 fprintf(stdout, "\n");
1291}
1292
1293
1294/**
1295 * xmlShellPrintXPathResult:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001296 * list: a valid result generated by an xpath evaluation
Daniel Veillard78d12092001-10-11 09:12:24 +00001297 *
1298 * Prints result to stdout
1299 */
1300void
1301xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1302{
1303 int i = 0;
1304
1305 if (list != NULL) {
1306 switch (list->type) {
1307 case XPATH_NODESET:{
1308 int indx;
1309
1310 if (list->nodesetval) {
1311 for (indx = 0; indx < list->nodesetval->nodeNr;
1312 indx++) {
1313 if (i > 0)
1314 fprintf(stderr, " -------\n");
1315 xmlShellPrintNode(list->nodesetval->
1316 nodeTab[indx]);
1317 }
1318 } else {
1319 xmlGenericError(xmlGenericErrorContext,
1320 "Empty node set\n");
1321 }
1322 break;
1323 }
1324 case XPATH_BOOLEAN:
1325 xmlGenericError(xmlGenericErrorContext,
1326 "Is a Boolean:%s\n",
1327 xmlBoolToText(list->boolval));
1328 break;
1329 case XPATH_NUMBER:
1330 xmlGenericError(xmlGenericErrorContext,
1331 "Is a number:%0g\n", list->floatval);
1332 break;
1333 case XPATH_STRING:
1334 xmlGenericError(xmlGenericErrorContext,
1335 "Is a string:%s\n", list->stringval);
1336 break;
1337
1338 default:
1339 xmlShellPrintXPathError(list->type, NULL);
1340 }
1341 }
1342}
1343
1344/**
Owen Taylor3473f882001-02-23 17:55:21 +00001345 * xmlShellList:
1346 * @ctxt: the shell context
1347 * @arg: unused
1348 * @node: a node
1349 * @node2: unused
1350 *
1351 * Implements the XML shell function "ls"
1352 * Does an Unix like listing of the given node (like a directory)
1353 *
1354 * Returns 0
1355 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001356int
1357xmlShellList(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1358 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1359 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1360{
Owen Taylor3473f882001-02-23 17:55:21 +00001361 xmlNodePtr cur;
1362
1363 if ((node->type == XML_DOCUMENT_NODE) ||
1364 (node->type == XML_HTML_DOCUMENT_NODE)) {
1365 cur = ((xmlDocPtr) node)->children;
1366 } else if (node->children != NULL) {
1367 cur = node->children;
1368 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001369 xmlLsOneNode(stdout, node);
1370 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001371 }
1372 while (cur != NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001373 xmlLsOneNode(stdout, cur);
1374 cur = cur->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001375 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001376 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001377}
1378
1379/**
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001380 * xmlShellBase:
1381 * @ctxt: the shell context
1382 * @arg: unused
1383 * @node: a node
1384 * @node2: unused
1385 *
1386 * Implements the XML shell function "base"
1387 * dumps the current XML base of the node
1388 *
1389 * Returns 0
1390 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001391int
1392xmlShellBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1393 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1394 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1395{
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001396 xmlChar *base;
1397
1398 base = xmlNodeGetBase(node->doc, node);
1399
1400 if (base == NULL) {
Daniel Veillardcd337f02001-11-22 18:20:37 +00001401 fprintf(stdout, " No base found !!!\n");
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001402 } else {
Daniel Veillardcd337f02001-11-22 18:20:37 +00001403 fprintf(stdout, "%s\n", base);
Daniel Veillard78d12092001-10-11 09:12:24 +00001404 xmlFree(base);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001405 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001406 return (0);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001407}
1408
1409/**
Owen Taylor3473f882001-02-23 17:55:21 +00001410 * xmlShellDir:
1411 * @ctxt: the shell context
1412 * @arg: unused
1413 * @node: a node
1414 * @node2: unused
1415 *
1416 * Implements the XML shell function "dir"
1417 * dumps informations about the node (namespace, attributes, content).
1418 *
1419 * Returns 0
1420 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001421int
1422xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1423 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1424 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1425{
Owen Taylor3473f882001-02-23 17:55:21 +00001426 if ((node->type == XML_DOCUMENT_NODE) ||
1427 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001428 xmlDebugDumpDocumentHead(stdout, (xmlDocPtr) node);
Owen Taylor3473f882001-02-23 17:55:21 +00001429 } else if (node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001430 xmlDebugDumpAttr(stdout, (xmlAttrPtr) node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001431 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001432 xmlDebugDumpOneNode(stdout, node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001433 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001434 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001435}
1436
1437/**
1438 * xmlShellCat:
1439 * @ctxt: the shell context
1440 * @arg: unused
1441 * @node: a node
1442 * @node2: unused
1443 *
1444 * Implements the XML shell function "cat"
1445 * dumps the serialization node content (XML or HTML).
1446 *
1447 * Returns 0
1448 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001449int
1450xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
1451 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
1452{
Owen Taylor3473f882001-02-23 17:55:21 +00001453 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
1454#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001455 if (node->type == XML_HTML_DOCUMENT_NODE)
1456 htmlDocDump(stdout, (htmlDocPtr) node);
1457 else
1458 htmlNodeDumpFile(stdout, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001459#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001460 if (node->type == XML_DOCUMENT_NODE)
1461 xmlDocDump(stdout, (xmlDocPtr) node);
1462 else
1463 xmlElemDump(stdout, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001464#endif /* LIBXML_HTML_ENABLED */
1465 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001466 if (node->type == XML_DOCUMENT_NODE)
1467 xmlDocDump(stdout, (xmlDocPtr) node);
1468 else
1469 xmlElemDump(stdout, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001470 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00001471 fprintf(stdout, "\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00001472 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001473}
1474
1475/**
1476 * xmlShellLoad:
1477 * @ctxt: the shell context
1478 * @filename: the file name
1479 * @node: unused
1480 * @node2: unused
1481 *
1482 * Implements the XML shell function "load"
1483 * loads a new document specified by the filename
1484 *
1485 * Returns 0 or -1 if loading failed
1486 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001487int
1488xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
1489 xmlNodePtr node ATTRIBUTE_UNUSED,
1490 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1491{
Owen Taylor3473f882001-02-23 17:55:21 +00001492 xmlDocPtr doc;
1493 int html = 0;
1494
1495 if (ctxt->doc != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001496 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00001497
1498 if (html) {
1499#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001500 doc = htmlParseFile(filename, NULL);
1501#else
Daniel Veillardcd337f02001-11-22 18:20:37 +00001502 fprintf(stdout, "HTML support not compiled in\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00001503 doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001504#endif /* LIBXML_HTML_ENABLED */
1505 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001506 doc = xmlParseFile(filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001507 }
1508 if (doc != NULL) {
1509 if (ctxt->loaded == 1) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001510 xmlFreeDoc(ctxt->doc);
1511 }
1512 ctxt->loaded = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001513#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001514 xmlXPathFreeContext(ctxt->pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001515#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001516 xmlFree(ctxt->filename);
1517 ctxt->doc = doc;
1518 ctxt->node = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001519#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001520 ctxt->pctxt = xmlXPathNewContext(doc);
Owen Taylor3473f882001-02-23 17:55:21 +00001521#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001522 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001523 } else
Daniel Veillard78d12092001-10-11 09:12:24 +00001524 return (-1);
1525 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001526}
1527
1528/**
1529 * xmlShellWrite:
1530 * @ctxt: the shell context
1531 * @filename: the file name
1532 * @node: a node in the tree
1533 * @node2: unused
1534 *
1535 * Implements the XML shell function "write"
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001536 * Write the current node to the filename, it saves the serialization
Owen Taylor3473f882001-02-23 17:55:21 +00001537 * of the subtree under the @node specified
1538 *
1539 * Returns 0 or -1 in case of error
1540 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001541int
Owen Taylor3473f882001-02-23 17:55:21 +00001542xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Daniel Veillard78d12092001-10-11 09:12:24 +00001543 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1544{
Owen Taylor3473f882001-02-23 17:55:21 +00001545 if (node == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001546 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001547 if ((filename == NULL) || (filename[0] == 0)) {
1548 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001549 "Write command requires a filename argument\n");
1550 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001551 }
1552#ifdef W_OK
1553 if (access((char *) filename, W_OK)) {
1554 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001555 "Cannot write to %s\n", filename);
1556 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001557 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001558#endif
1559 switch (node->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00001560 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00001561 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
1562 xmlGenericError(xmlGenericErrorContext,
1563 "Failed to write to %s\n", filename);
1564 return (-1);
1565 }
1566 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001567 case XML_HTML_DOCUMENT_NODE:
1568#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001569 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
1570 xmlGenericError(xmlGenericErrorContext,
1571 "Failed to write to %s\n", filename);
1572 return (-1);
1573 }
Owen Taylor3473f882001-02-23 17:55:21 +00001574#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001575 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
1576 xmlGenericError(xmlGenericErrorContext,
1577 "Failed to write to %s\n", filename);
1578 return (-1);
1579 }
Owen Taylor3473f882001-02-23 17:55:21 +00001580#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001581 break;
1582 default:{
1583 FILE *f;
Owen Taylor3473f882001-02-23 17:55:21 +00001584
Daniel Veillard78d12092001-10-11 09:12:24 +00001585 f = fopen((char *) filename, "w");
1586 if (f == NULL) {
1587 xmlGenericError(xmlGenericErrorContext,
1588 "Failed to write to %s\n", filename);
1589 return (-1);
1590 }
1591 xmlElemDump(f, ctxt->doc, node);
1592 fclose(f);
1593 }
Owen Taylor3473f882001-02-23 17:55:21 +00001594 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001595 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001596}
1597
1598/**
1599 * xmlShellSave:
1600 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001601 * @filename: the file name (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00001602 * @node: unused
1603 * @node2: unused
1604 *
1605 * Implements the XML shell function "save"
1606 * Write the current document to the filename, or it's original name
1607 *
1608 * Returns 0 or -1 in case of error
1609 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001610int
1611xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
1612 xmlNodePtr node ATTRIBUTE_UNUSED,
1613 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1614{
Owen Taylor3473f882001-02-23 17:55:21 +00001615 if (ctxt->doc == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001616 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001617 if ((filename == NULL) || (filename[0] == 0))
1618 filename = ctxt->filename;
1619#ifdef W_OK
1620 if (access((char *) filename, W_OK)) {
1621 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001622 "Cannot save to %s\n", filename);
1623 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001624 }
1625#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00001626 switch (ctxt->doc->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00001627 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00001628 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
1629 xmlGenericError(xmlGenericErrorContext,
1630 "Failed to save to %s\n", filename);
1631 }
1632 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001633 case XML_HTML_DOCUMENT_NODE:
1634#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001635 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
1636 xmlGenericError(xmlGenericErrorContext,
1637 "Failed to save to %s\n", filename);
1638 }
Owen Taylor3473f882001-02-23 17:55:21 +00001639#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001640 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
1641 xmlGenericError(xmlGenericErrorContext,
1642 "Failed to save to %s\n", filename);
1643 }
Owen Taylor3473f882001-02-23 17:55:21 +00001644#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001645 break;
1646 default:
1647 xmlGenericError(xmlGenericErrorContext,
1648 "To save to subparts of a document use the 'write' command\n");
1649 return (-1);
1650
Owen Taylor3473f882001-02-23 17:55:21 +00001651 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001652 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001653}
1654
1655/**
1656 * xmlShellValidate:
1657 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001658 * @dtd: the DTD URI (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00001659 * @node: unused
1660 * @node2: unused
1661 *
1662 * Implements the XML shell function "validate"
1663 * Validate the document, if a DTD path is provided, then the validation
1664 * is done against the given DTD.
1665 *
1666 * Returns 0 or -1 in case of error
1667 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001668int
1669xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
1670 xmlNodePtr node ATTRIBUTE_UNUSED,
1671 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1672{
Owen Taylor3473f882001-02-23 17:55:21 +00001673 xmlValidCtxt vctxt;
1674 int res = -1;
1675
1676 vctxt.userData = stderr;
1677 vctxt.error = (xmlValidityErrorFunc) fprintf;
1678 vctxt.warning = (xmlValidityWarningFunc) fprintf;
1679
1680 if ((dtd == NULL) || (dtd[0] == 0)) {
1681 res = xmlValidateDocument(&vctxt, ctxt->doc);
1682 } else {
1683 xmlDtdPtr subset;
1684
Daniel Veillard78d12092001-10-11 09:12:24 +00001685 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
1686 if (subset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001687 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
1688
Daniel Veillard78d12092001-10-11 09:12:24 +00001689 xmlFreeDtd(subset);
1690 }
Owen Taylor3473f882001-02-23 17:55:21 +00001691 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001692 return (res);
Owen Taylor3473f882001-02-23 17:55:21 +00001693}
1694
1695/**
1696 * xmlShellDu:
1697 * @ctxt: the shell context
1698 * @arg: unused
1699 * @tree: a node defining a subtree
1700 * @node2: unused
1701 *
1702 * Implements the XML shell function "du"
1703 * show the structure of the subtree under node @tree
1704 * If @tree is null, the command works on the current node.
1705 *
1706 * Returns 0 or -1 in case of error
1707 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001708int
1709xmlShellDu(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1710 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
1711 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1712{
Owen Taylor3473f882001-02-23 17:55:21 +00001713 xmlNodePtr node;
Daniel Veillard78d12092001-10-11 09:12:24 +00001714 int indent = 0, i;
Owen Taylor3473f882001-02-23 17:55:21 +00001715
Daniel Veillard78d12092001-10-11 09:12:24 +00001716 if (tree == NULL)
1717 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001718 node = tree;
1719 while (node != NULL) {
1720 if ((node->type == XML_DOCUMENT_NODE) ||
1721 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillardcd337f02001-11-22 18:20:37 +00001722 fprintf(stdout, "/\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00001723 } else if (node->type == XML_ELEMENT_NODE) {
1724 for (i = 0; i < indent; i++)
Daniel Veillardcd337f02001-11-22 18:20:37 +00001725 fprintf(stdout, " ");
1726 fprintf(stdout, "%s\n", node->name);
Daniel Veillard78d12092001-10-11 09:12:24 +00001727 } else {
1728 }
Owen Taylor3473f882001-02-23 17:55:21 +00001729
Daniel Veillard78d12092001-10-11 09:12:24 +00001730 /*
1731 * Browse the full subtree, deep first
1732 */
Owen Taylor3473f882001-02-23 17:55:21 +00001733
1734 if ((node->type == XML_DOCUMENT_NODE) ||
1735 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001736 node = ((xmlDocPtr) node)->children;
1737 } else if ((node->children != NULL)
1738 && (node->type != XML_ENTITY_REF_NODE)) {
1739 /* deep first */
1740 node = node->children;
1741 indent++;
1742 } else if ((node != tree) && (node->next != NULL)) {
1743 /* then siblings */
1744 node = node->next;
1745 } else if (node != tree) {
1746 /* go up to parents->next if needed */
1747 while (node != tree) {
1748 if (node->parent != NULL) {
1749 node = node->parent;
1750 indent--;
1751 }
1752 if ((node != tree) && (node->next != NULL)) {
1753 node = node->next;
1754 break;
1755 }
1756 if (node->parent == NULL) {
1757 node = NULL;
1758 break;
1759 }
1760 if (node == tree) {
1761 node = NULL;
1762 break;
1763 }
1764 }
1765 /* exit condition */
1766 if (node == tree)
1767 node = NULL;
1768 } else
1769 node = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001770 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001771 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001772}
1773
1774/**
1775 * xmlShellPwd:
1776 * @ctxt: the shell context
1777 * @buffer: the output buffer
1778 * @tree: a node
1779 * @node2: unused
1780 *
1781 * Implements the XML shell function "pwd"
1782 * Show the full path from the root to the node, if needed building
1783 * thumblers when similar elements exists at a given ancestor level.
1784 * The output is compatible with XPath commands.
1785 *
1786 * Returns 0 or -1 in case of error
1787 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001788int
1789xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
1790 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
1791{
Daniel Veillardc6e013a2001-11-10 10:08:57 +00001792 xmlChar *path;
Owen Taylor3473f882001-02-23 17:55:21 +00001793
Daniel Veillard78d12092001-10-11 09:12:24 +00001794 if (node == NULL)
1795 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001796
Daniel Veillardc6e013a2001-11-10 10:08:57 +00001797 path = xmlGetNodePath(node);
1798 if (path == NULL)
1799 return (-1);
1800
1801 /*
1802 * This test prevents buffer overflow, because this routine
1803 * is only called by xmlShell, in which the second argument is
1804 * 500 chars long.
1805 * It is a dirty hack before a cleaner solution is found.
1806 * Documentation should mention that the second argument must
1807 * be at least 500 chars long, and could be stripped if too long.
1808 */
1809 snprintf(buffer, 499, "%s", path);
1810 buffer[499] = '0';
1811 xmlFree(path);
1812
Daniel Veillard78d12092001-10-11 09:12:24 +00001813 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001814}
1815
1816/**
1817 * xmlShell
1818 * @doc: the initial document
1819 * @filename: the output buffer
1820 * @input: the line reading function
1821 * @output: the output FILE*
1822 *
1823 * Implements the XML shell
1824 * This allow to load, validate, view, modify and save a document
1825 * using a environment similar to a UNIX commandline.
1826 */
1827void
1828xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Daniel Veillard78d12092001-10-11 09:12:24 +00001829 FILE * output)
1830{
Owen Taylor3473f882001-02-23 17:55:21 +00001831 char prompt[500] = "/ > ";
1832 char *cmdline = NULL, *cur;
1833 int nbargs;
1834 char command[100];
1835 char arg[400];
1836 int i;
1837 xmlShellCtxtPtr ctxt;
1838 xmlXPathObjectPtr list;
1839
1840 if (doc == NULL)
1841 return;
1842 if (filename == NULL)
1843 return;
1844 if (input == NULL)
1845 return;
1846 if (output == NULL)
1847 return;
1848 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Daniel Veillard78d12092001-10-11 09:12:24 +00001849 if (ctxt == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001850 return;
1851 ctxt->loaded = 0;
1852 ctxt->doc = doc;
1853 ctxt->input = input;
1854 ctxt->output = output;
1855 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Daniel Veillard78d12092001-10-11 09:12:24 +00001856 ctxt->node = (xmlNodePtr) ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001857
1858#ifdef LIBXML_XPATH_ENABLED
1859 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
1860 if (ctxt->pctxt == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001861 xmlFree(ctxt);
1862 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001863 }
1864#endif /* LIBXML_XPATH_ENABLED */
1865 while (1) {
1866 if (ctxt->node == (xmlNodePtr) ctxt->doc)
Daniel Veillard78d12092001-10-11 09:12:24 +00001867 sprintf(prompt, "%s > ", "/");
1868 else if (ctxt->node->name)
1869 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001870 else
Daniel Veillard78d12092001-10-11 09:12:24 +00001871 sprintf(prompt, "? > ");
Owen Taylor3473f882001-02-23 17:55:21 +00001872 prompt[sizeof(prompt) - 1] = 0;
1873
Daniel Veillard78d12092001-10-11 09:12:24 +00001874 /*
1875 * Get a new command line
1876 */
Owen Taylor3473f882001-02-23 17:55:21 +00001877 cmdline = ctxt->input(prompt);
Daniel Veillard78d12092001-10-11 09:12:24 +00001878 if (cmdline == NULL)
1879 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001880
Daniel Veillard78d12092001-10-11 09:12:24 +00001881 /*
1882 * Parse the command itself
1883 */
1884 cur = cmdline;
1885 nbargs = 0;
1886 while ((*cur == ' ') || (*cur == '\t'))
1887 cur++;
1888 i = 0;
1889 while ((*cur != ' ') && (*cur != '\t') &&
1890 (*cur != '\n') && (*cur != '\r')) {
1891 if (*cur == 0)
1892 break;
1893 command[i++] = *cur++;
1894 }
1895 command[i] = 0;
1896 if (i == 0)
1897 continue;
1898 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00001899
Daniel Veillard78d12092001-10-11 09:12:24 +00001900 /*
1901 * Parse the argument
1902 */
1903 while ((*cur == ' ') || (*cur == '\t'))
1904 cur++;
1905 i = 0;
1906 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
1907 if (*cur == 0)
1908 break;
1909 arg[i++] = *cur++;
1910 }
1911 arg[i] = 0;
1912 if (i != 0)
1913 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00001914
Daniel Veillard78d12092001-10-11 09:12:24 +00001915 /*
1916 * start interpreting the command
1917 */
Owen Taylor3473f882001-02-23 17:55:21 +00001918 if (!strcmp(command, "exit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00001919 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001920 if (!strcmp(command, "quit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00001921 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001922 if (!strcmp(command, "bye"))
Daniel Veillard78d12092001-10-11 09:12:24 +00001923 break;
Daniel Veillard5004f422001-11-08 13:53:05 +00001924 if (!strcmp(command, "help")) {
Daniel Veillardcd337f02001-11-22 18:20:37 +00001925 fprintf(stdout, "\tbase display XML base of the node\n");
1926 fprintf(stdout, "\tbye leave shell\n");
1927 fprintf(stdout, "\tcat [node] display node or current node\n");
1928 fprintf(stdout, "\tcd [path] change directory to path or to root\n");
1929 fprintf(stdout, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
1930 fprintf(stdout, "\tdu [path] show the structure of the subtree under path or the current node\n");
1931 fprintf(stdout, "\texit leave shell\n");
1932 fprintf(stdout, "\thelp display this help\n");
1933 fprintf(stdout, "\tfree display memory usage\n");
1934 fprintf(stdout, "\tload [name] load a new document with name\n");
1935 fprintf(stdout, "\tls [path] list contents of path or the current directory\n");
1936 fprintf(stdout, "\tpwd display current working directory\n");
1937 fprintf(stdout, "\tquit leave shell\n");
1938 fprintf(stdout, "\tsave [name] save this document to name or the original name\n");
1939 fprintf(stdout, "\tvalidate check the document for errors\n");
1940 fprintf(stdout, "\twrite [name] write the current node to the filename\n");
Daniel Veillard5004f422001-11-08 13:53:05 +00001941 } else if (!strcmp(command, "validate")) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001942 xmlShellValidate(ctxt, arg, NULL, NULL);
1943 } else if (!strcmp(command, "load")) {
1944 xmlShellLoad(ctxt, arg, NULL, NULL);
1945 } else if (!strcmp(command, "save")) {
1946 xmlShellSave(ctxt, arg, NULL, NULL);
1947 } else if (!strcmp(command, "write")) {
1948 xmlShellWrite(ctxt, arg, NULL, NULL);
1949 } else if (!strcmp(command, "free")) {
1950 if (arg[0] == 0) {
1951 xmlMemShow(stdout, 0);
1952 } else {
1953 int len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001954
Daniel Veillard78d12092001-10-11 09:12:24 +00001955 sscanf(arg, "%d", &len);
1956 xmlMemShow(stdout, len);
1957 }
1958 } else if (!strcmp(command, "pwd")) {
1959 char dir[500];
Owen Taylor3473f882001-02-23 17:55:21 +00001960
Daniel Veillard78d12092001-10-11 09:12:24 +00001961 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
Daniel Veillardcd337f02001-11-22 18:20:37 +00001962 fprintf(stdout, "%s\n", dir);
Daniel Veillard78d12092001-10-11 09:12:24 +00001963 } else if (!strcmp(command, "du")) {
1964 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
1965 } else if (!strcmp(command, "base")) {
1966 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
1967 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
1968 int dir = (!strcmp(command, "dir"));
1969
1970 if (arg[0] == 0) {
1971 if (dir)
1972 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
1973 else
1974 xmlShellList(ctxt, NULL, ctxt->node, NULL);
1975 } else {
1976 ctxt->pctxt->node = ctxt->node;
Daniel Veillard61d80a22001-04-27 17:13:01 +00001977#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001978 ctxt->pctxt->node = ctxt->node;
1979 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
1980#else
1981 list = NULL;
1982#endif /* LIBXML_XPATH_ENABLED */
1983 if (list != NULL) {
1984 switch (list->type) {
1985 case XPATH_UNDEFINED:
1986 xmlGenericError(xmlGenericErrorContext,
1987 "%s: no such node\n", arg);
1988 break;
1989 case XPATH_NODESET:{
1990 int indx;
1991
Daniel Veillarda6825e82001-11-07 13:33:59 +00001992 if (list->nodesetval == NULL)
1993 break;
1994
Daniel Veillard78d12092001-10-11 09:12:24 +00001995 for (indx = 0;
1996 indx < list->nodesetval->nodeNr;
1997 indx++) {
1998 if (dir)
1999 xmlShellDir(ctxt, NULL,
2000 list->nodesetval->
2001 nodeTab[indx], NULL);
2002 else
2003 xmlShellList(ctxt, NULL,
2004 list->nodesetval->
2005 nodeTab[indx], NULL);
2006 }
2007 break;
2008 }
2009 case XPATH_BOOLEAN:
2010 xmlGenericError(xmlGenericErrorContext,
2011 "%s is a Boolean\n", arg);
2012 break;
2013 case XPATH_NUMBER:
2014 xmlGenericError(xmlGenericErrorContext,
2015 "%s is a number\n", arg);
2016 break;
2017 case XPATH_STRING:
2018 xmlGenericError(xmlGenericErrorContext,
2019 "%s is a string\n", arg);
2020 break;
2021 case XPATH_POINT:
2022 xmlGenericError(xmlGenericErrorContext,
2023 "%s is a point\n", arg);
2024 break;
2025 case XPATH_RANGE:
2026 xmlGenericError(xmlGenericErrorContext,
2027 "%s is a range\n", arg);
2028 break;
2029 case XPATH_LOCATIONSET:
2030 xmlGenericError(xmlGenericErrorContext,
2031 "%s is a range\n", arg);
2032 break;
2033 case XPATH_USERS:
2034 xmlGenericError(xmlGenericErrorContext,
2035 "%s is user-defined\n", arg);
2036 break;
2037 case XPATH_XSLT_TREE:
2038 xmlGenericError(xmlGenericErrorContext,
2039 "%s is an XSLT value tree\n",
2040 arg);
2041 break;
2042 }
2043#ifdef LIBXML_XPATH_ENABLED
2044 xmlXPathFreeObject(list);
Daniel Veillard61d80a22001-04-27 17:13:01 +00002045#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002046 } else {
2047 xmlGenericError(xmlGenericErrorContext,
2048 "%s: no such node\n", arg);
2049 }
2050 ctxt->pctxt->node = NULL;
2051 }
2052 } else if (!strcmp(command, "cd")) {
2053 if (arg[0] == 0) {
2054 ctxt->node = (xmlNodePtr) ctxt->doc;
2055 } else {
2056#ifdef LIBXML_XPATH_ENABLED
2057 ctxt->pctxt->node = ctxt->node;
2058 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2059#else
2060 list = NULL;
2061#endif /* LIBXML_XPATH_ENABLED */
2062 if (list != NULL) {
2063 switch (list->type) {
2064 case XPATH_UNDEFINED:
2065 xmlGenericError(xmlGenericErrorContext,
2066 "%s: no such node\n", arg);
2067 break;
2068 case XPATH_NODESET:
Daniel Veillarda6825e82001-11-07 13:33:59 +00002069 if (list->nodesetval != NULL) {
2070 if (list->nodesetval->nodeNr == 1) {
2071 ctxt->node = list->nodesetval->nodeTab[0];
2072 } else
2073 xmlGenericError(xmlGenericErrorContext,
2074 "%s is a %d Node Set\n",
2075 arg,
2076 list->nodesetval->nodeNr);
Daniel Veillard78d12092001-10-11 09:12:24 +00002077 } else
2078 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda6825e82001-11-07 13:33:59 +00002079 "%s is an empty Node Set\n",
2080 arg);
Daniel Veillard78d12092001-10-11 09:12:24 +00002081 break;
2082 case XPATH_BOOLEAN:
2083 xmlGenericError(xmlGenericErrorContext,
2084 "%s is a Boolean\n", arg);
2085 break;
2086 case XPATH_NUMBER:
2087 xmlGenericError(xmlGenericErrorContext,
2088 "%s is a number\n", arg);
2089 break;
2090 case XPATH_STRING:
2091 xmlGenericError(xmlGenericErrorContext,
2092 "%s is a string\n", arg);
2093 break;
2094 case XPATH_POINT:
2095 xmlGenericError(xmlGenericErrorContext,
2096 "%s is a point\n", arg);
2097 break;
2098 case XPATH_RANGE:
2099 xmlGenericError(xmlGenericErrorContext,
2100 "%s is a range\n", arg);
2101 break;
2102 case XPATH_LOCATIONSET:
2103 xmlGenericError(xmlGenericErrorContext,
2104 "%s is a range\n", arg);
2105 break;
2106 case XPATH_USERS:
2107 xmlGenericError(xmlGenericErrorContext,
2108 "%s is user-defined\n", arg);
2109 break;
2110 case XPATH_XSLT_TREE:
2111 xmlGenericError(xmlGenericErrorContext,
2112 "%s is an XSLT value tree\n",
2113 arg);
2114 break;
2115 }
2116#ifdef LIBXML_XPATH_ENABLED
2117 xmlXPathFreeObject(list);
2118#endif
2119 } else {
2120 xmlGenericError(xmlGenericErrorContext,
2121 "%s: no such node\n", arg);
2122 }
2123 ctxt->pctxt->node = NULL;
2124 }
2125 } else if (!strcmp(command, "cat")) {
2126 if (arg[0] == 0) {
2127 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
2128 } else {
2129 ctxt->pctxt->node = ctxt->node;
2130#ifdef LIBXML_XPATH_ENABLED
2131 ctxt->pctxt->node = ctxt->node;
2132 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2133#else
2134 list = NULL;
2135#endif /* LIBXML_XPATH_ENABLED */
2136 if (list != NULL) {
2137 switch (list->type) {
2138 case XPATH_UNDEFINED:
2139 xmlGenericError(xmlGenericErrorContext,
2140 "%s: no such node\n", arg);
2141 break;
2142 case XPATH_NODESET:{
2143 int indx;
2144
Daniel Veillarda6825e82001-11-07 13:33:59 +00002145 if (list->nodesetval == NULL)
2146 break;
2147
Daniel Veillard78d12092001-10-11 09:12:24 +00002148 for (indx = 0;
2149 indx < list->nodesetval->nodeNr;
2150 indx++) {
2151 if (i > 0)
Daniel Veillardcd337f02001-11-22 18:20:37 +00002152 fprintf(stdout, " -------\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002153 xmlShellCat(ctxt, NULL,
2154 list->nodesetval->
2155 nodeTab[indx], NULL);
2156 }
2157 break;
2158 }
2159 case XPATH_BOOLEAN:
2160 xmlGenericError(xmlGenericErrorContext,
2161 "%s is a Boolean\n", arg);
2162 break;
2163 case XPATH_NUMBER:
2164 xmlGenericError(xmlGenericErrorContext,
2165 "%s is a number\n", arg);
2166 break;
2167 case XPATH_STRING:
2168 xmlGenericError(xmlGenericErrorContext,
2169 "%s is a string\n", arg);
2170 break;
2171 case XPATH_POINT:
2172 xmlGenericError(xmlGenericErrorContext,
2173 "%s is a point\n", arg);
2174 break;
2175 case XPATH_RANGE:
2176 xmlGenericError(xmlGenericErrorContext,
2177 "%s is a range\n", arg);
2178 break;
2179 case XPATH_LOCATIONSET:
2180 xmlGenericError(xmlGenericErrorContext,
2181 "%s is a range\n", arg);
2182 break;
2183 case XPATH_USERS:
2184 xmlGenericError(xmlGenericErrorContext,
2185 "%s is user-defined\n", arg);
2186 break;
2187 case XPATH_XSLT_TREE:
2188 xmlGenericError(xmlGenericErrorContext,
2189 "%s is an XSLT value tree\n",
2190 arg);
2191 break;
2192 }
2193#ifdef LIBXML_XPATH_ENABLED
2194 xmlXPathFreeObject(list);
2195#endif
2196 } else {
2197 xmlGenericError(xmlGenericErrorContext,
2198 "%s: no such node\n", arg);
2199 }
2200 ctxt->pctxt->node = NULL;
2201 }
2202 } else {
2203 xmlGenericError(xmlGenericErrorContext,
2204 "Unknown command %s\n", command);
2205 }
2206 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00002207 }
2208#ifdef LIBXML_XPATH_ENABLED
2209 xmlXPathFreeContext(ctxt->pctxt);
2210#endif /* LIBXML_XPATH_ENABLED */
2211 if (ctxt->loaded) {
2212 xmlFreeDoc(ctxt->doc);
2213 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002214 if (ctxt->filename != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002215 xmlFree(ctxt->filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002216 xmlFree(ctxt);
2217 if (cmdline != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002218 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00002219}
2220
2221#endif /* LIBXML_DEBUG_ENABLED */