blob: fa25c917f44cbfe0d1bbabc940735b743d7af9a6 [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;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001114 case XML_NAMESPACE_DECL:
1115 fprintf(output, "n");
1116 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001117 default:
1118 fprintf(output, "?");
1119 }
Daniel Veillarde6a55192002-01-14 17:11:53 +00001120 if (node->type != XML_NAMESPACE_DECL) {
1121 if (node->properties != NULL)
1122 fprintf(output, "a");
1123 else
1124 fprintf(output, "-");
1125 if (node->nsDef != NULL)
1126 fprintf(output, "n");
1127 else
1128 fprintf(output, "-");
1129 }
Owen Taylor3473f882001-02-23 17:55:21 +00001130
1131 fprintf(output, " %8d ", xmlLsCountNode(node));
1132
1133 switch (node->type) {
1134 case XML_ELEMENT_NODE:
1135 if (node->name != NULL)
1136 fprintf(output, "%s", node->name);
1137 break;
1138 case XML_ATTRIBUTE_NODE:
1139 if (node->name != NULL)
1140 fprintf(output, "%s", node->name);
1141 break;
1142 case XML_TEXT_NODE:
1143 if (node->content != NULL) {
1144#ifndef XML_USE_BUFFER_CONTENT
1145 xmlDebugDumpString(output, node->content);
1146#else
1147 xmlDebugDumpString(output, xmlBufferContent(node->content));
1148#endif
1149 }
1150 break;
1151 case XML_CDATA_SECTION_NODE:
1152 break;
1153 case XML_ENTITY_REF_NODE:
1154 if (node->name != NULL)
1155 fprintf(output, "%s", node->name);
1156 break;
1157 case XML_ENTITY_NODE:
1158 if (node->name != NULL)
1159 fprintf(output, "%s", node->name);
1160 break;
1161 case XML_PI_NODE:
1162 if (node->name != NULL)
1163 fprintf(output, "%s", node->name);
1164 break;
1165 case XML_COMMENT_NODE:
1166 break;
1167 case XML_DOCUMENT_NODE:
1168 break;
1169 case XML_HTML_DOCUMENT_NODE:
1170 break;
1171 case XML_DOCUMENT_TYPE_NODE:
1172 break;
1173 case XML_DOCUMENT_FRAG_NODE:
1174 break;
1175 case XML_NOTATION_NODE:
1176 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001177 case XML_NAMESPACE_DECL: {
1178 xmlNsPtr ns = (xmlNsPtr) node;
1179
1180 if (ns->prefix == NULL)
1181 fprintf(output, "default -> %s", ns->href);
1182 else
1183 fprintf(output, "%s -> %s", ns->prefix, ns->href);
1184 break;
1185 }
Owen Taylor3473f882001-02-23 17:55:21 +00001186 default:
1187 if (node->name != NULL)
1188 fprintf(output, "%s", node->name);
1189 }
1190 fprintf(output, "\n");
1191}
1192
Daniel Veillard78d12092001-10-11 09:12:24 +00001193/**
1194 * xmlBoolToText:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001195 * @boolval: a bool to turn into text
Daniel Veillard78d12092001-10-11 09:12:24 +00001196 *
1197 * Convenient way to turn bool into text
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001198 *
1199 * Returns a pointer to either "True" or "False"
1200 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001201const char *
Daniel Veillardebd38c52001-11-01 08:38:12 +00001202xmlBoolToText(int boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001203{
Daniel Veillardebd38c52001-11-01 08:38:12 +00001204 if (boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001205 return("True");
1206 else
1207 return("False");
1208}
1209
Owen Taylor3473f882001-02-23 17:55:21 +00001210/****************************************************************
1211 * *
1212 * The XML shell related functions *
1213 * *
1214 ****************************************************************/
1215
Daniel Veillard78d12092001-10-11 09:12:24 +00001216
1217
Owen Taylor3473f882001-02-23 17:55:21 +00001218/*
1219 * TODO: Improvement/cleanups for the XML shell
1220 * - allow to shell out an editor on a subpart
1221 * - cleanup function registrations (with help) and calling
1222 * - provide registration routines
1223 */
1224
1225/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001226 * xmlShellPrintXPathError:
Daniel Veillard78d12092001-10-11 09:12:24 +00001227 * @errorType: valid xpath error id
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001228 * @arg: the argument that cause xpath to fail
Daniel Veillard78d12092001-10-11 09:12:24 +00001229 *
1230 * Print the xpath error to libxml default error channel
1231 */
1232void
1233xmlShellPrintXPathError(int errorType, const char *arg)
1234{
1235 const char *default_arg = "Result";
1236
1237 if (!arg)
1238 arg = default_arg;
1239
1240 switch (errorType) {
1241 case XPATH_UNDEFINED:
1242 xmlGenericError(xmlGenericErrorContext,
1243 "%s: no such node\n", arg);
1244 break;
1245
1246 case XPATH_BOOLEAN:
1247 xmlGenericError(xmlGenericErrorContext,
1248 "%s is a Boolean\n", arg);
1249 break;
1250 case XPATH_NUMBER:
1251 xmlGenericError(xmlGenericErrorContext,
1252 "%s is a number\n", arg);
1253 break;
1254 case XPATH_STRING:
1255 xmlGenericError(xmlGenericErrorContext,
1256 "%s is a string\n", arg);
1257 break;
1258 case XPATH_POINT:
1259 xmlGenericError(xmlGenericErrorContext,
1260 "%s is a point\n", arg);
1261 break;
1262 case XPATH_RANGE:
1263 xmlGenericError(xmlGenericErrorContext,
1264 "%s is a range\n", arg);
1265 break;
1266 case XPATH_LOCATIONSET:
1267 xmlGenericError(xmlGenericErrorContext,
1268 "%s is a range\n", arg);
1269 break;
1270 case XPATH_USERS:
1271 xmlGenericError(xmlGenericErrorContext,
1272 "%s is user-defined\n", arg);
1273 break;
1274 case XPATH_XSLT_TREE:
1275 xmlGenericError(xmlGenericErrorContext,
1276 "%s is an XSLT value tree\n", arg);
1277 break;
1278 }
1279 xmlGenericError(xmlGenericErrorContext,
1280 "Try casting the result string function (xpath builtin)\n",
1281 arg);
1282}
1283
1284
1285/**
1286 * xmlShellPrintNode:
1287 * @node : a non-null node to print to stdout
1288 *
1289 * Print node to stdout
1290 */
1291void
1292xmlShellPrintNode(xmlNodePtr node)
1293{
1294 if (!node)
1295 return;
1296
1297 if (node->type == XML_DOCUMENT_NODE)
1298 xmlDocDump(stdout, (xmlDocPtr) node);
1299 else if (node->type == XML_ATTRIBUTE_NODE)
1300 xmlDebugDumpAttrList(stdout, (xmlAttrPtr) node, 0);
1301 else
1302 xmlElemDump(stdout, node->doc, node);
1303
1304 fprintf(stdout, "\n");
1305}
1306
1307
1308/**
1309 * xmlShellPrintXPathResult:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001310 * list: a valid result generated by an xpath evaluation
Daniel Veillard78d12092001-10-11 09:12:24 +00001311 *
1312 * Prints result to stdout
1313 */
1314void
1315xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1316{
1317 int i = 0;
1318
1319 if (list != NULL) {
1320 switch (list->type) {
1321 case XPATH_NODESET:{
1322 int indx;
1323
1324 if (list->nodesetval) {
1325 for (indx = 0; indx < list->nodesetval->nodeNr;
1326 indx++) {
1327 if (i > 0)
1328 fprintf(stderr, " -------\n");
1329 xmlShellPrintNode(list->nodesetval->
1330 nodeTab[indx]);
1331 }
1332 } else {
1333 xmlGenericError(xmlGenericErrorContext,
1334 "Empty node set\n");
1335 }
1336 break;
1337 }
1338 case XPATH_BOOLEAN:
1339 xmlGenericError(xmlGenericErrorContext,
1340 "Is a Boolean:%s\n",
1341 xmlBoolToText(list->boolval));
1342 break;
1343 case XPATH_NUMBER:
1344 xmlGenericError(xmlGenericErrorContext,
1345 "Is a number:%0g\n", list->floatval);
1346 break;
1347 case XPATH_STRING:
1348 xmlGenericError(xmlGenericErrorContext,
1349 "Is a string:%s\n", list->stringval);
1350 break;
1351
1352 default:
1353 xmlShellPrintXPathError(list->type, NULL);
1354 }
1355 }
1356}
1357
1358/**
Owen Taylor3473f882001-02-23 17:55:21 +00001359 * xmlShellList:
1360 * @ctxt: the shell context
1361 * @arg: unused
1362 * @node: a node
1363 * @node2: unused
1364 *
1365 * Implements the XML shell function "ls"
1366 * Does an Unix like listing of the given node (like a directory)
1367 *
1368 * Returns 0
1369 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001370int
1371xmlShellList(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1372 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1373 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1374{
Owen Taylor3473f882001-02-23 17:55:21 +00001375 xmlNodePtr cur;
1376
1377 if ((node->type == XML_DOCUMENT_NODE) ||
1378 (node->type == XML_HTML_DOCUMENT_NODE)) {
1379 cur = ((xmlDocPtr) node)->children;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001380 } else if (node->type == XML_NAMESPACE_DECL) {
1381 xmlLsOneNode(stdout, node);
1382 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001383 } else if (node->children != NULL) {
1384 cur = node->children;
1385 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001386 xmlLsOneNode(stdout, node);
1387 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001388 }
1389 while (cur != NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001390 xmlLsOneNode(stdout, cur);
1391 cur = cur->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001392 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001393 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001394}
1395
1396/**
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001397 * xmlShellBase:
1398 * @ctxt: the shell context
1399 * @arg: unused
1400 * @node: a node
1401 * @node2: unused
1402 *
1403 * Implements the XML shell function "base"
1404 * dumps the current XML base of the node
1405 *
1406 * Returns 0
1407 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001408int
1409xmlShellBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1410 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1411 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1412{
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001413 xmlChar *base;
1414
1415 base = xmlNodeGetBase(node->doc, node);
1416
1417 if (base == NULL) {
Daniel Veillardcd337f02001-11-22 18:20:37 +00001418 fprintf(stdout, " No base found !!!\n");
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001419 } else {
Daniel Veillardcd337f02001-11-22 18:20:37 +00001420 fprintf(stdout, "%s\n", base);
Daniel Veillard78d12092001-10-11 09:12:24 +00001421 xmlFree(base);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001422 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001423 return (0);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001424}
1425
1426/**
Owen Taylor3473f882001-02-23 17:55:21 +00001427 * xmlShellDir:
1428 * @ctxt: the shell context
1429 * @arg: unused
1430 * @node: a node
1431 * @node2: unused
1432 *
1433 * Implements the XML shell function "dir"
1434 * dumps informations about the node (namespace, attributes, content).
1435 *
1436 * Returns 0
1437 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001438int
1439xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1440 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1441 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1442{
Owen Taylor3473f882001-02-23 17:55:21 +00001443 if ((node->type == XML_DOCUMENT_NODE) ||
1444 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001445 xmlDebugDumpDocumentHead(stdout, (xmlDocPtr) node);
Owen Taylor3473f882001-02-23 17:55:21 +00001446 } else if (node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001447 xmlDebugDumpAttr(stdout, (xmlAttrPtr) node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001448 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001449 xmlDebugDumpOneNode(stdout, node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001450 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001451 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001452}
1453
1454/**
1455 * xmlShellCat:
1456 * @ctxt: the shell context
1457 * @arg: unused
1458 * @node: a node
1459 * @node2: unused
1460 *
1461 * Implements the XML shell function "cat"
1462 * dumps the serialization node content (XML or HTML).
1463 *
1464 * Returns 0
1465 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001466int
1467xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
1468 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
1469{
Owen Taylor3473f882001-02-23 17:55:21 +00001470 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
1471#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001472 if (node->type == XML_HTML_DOCUMENT_NODE)
1473 htmlDocDump(stdout, (htmlDocPtr) node);
1474 else
1475 htmlNodeDumpFile(stdout, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001476#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001477 if (node->type == XML_DOCUMENT_NODE)
1478 xmlDocDump(stdout, (xmlDocPtr) node);
1479 else
1480 xmlElemDump(stdout, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001481#endif /* LIBXML_HTML_ENABLED */
1482 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001483 if (node->type == XML_DOCUMENT_NODE)
1484 xmlDocDump(stdout, (xmlDocPtr) node);
1485 else
1486 xmlElemDump(stdout, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001487 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00001488 fprintf(stdout, "\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00001489 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001490}
1491
1492/**
1493 * xmlShellLoad:
1494 * @ctxt: the shell context
1495 * @filename: the file name
1496 * @node: unused
1497 * @node2: unused
1498 *
1499 * Implements the XML shell function "load"
1500 * loads a new document specified by the filename
1501 *
1502 * Returns 0 or -1 if loading failed
1503 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001504int
1505xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
1506 xmlNodePtr node ATTRIBUTE_UNUSED,
1507 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1508{
Owen Taylor3473f882001-02-23 17:55:21 +00001509 xmlDocPtr doc;
1510 int html = 0;
1511
1512 if (ctxt->doc != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001513 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00001514
1515 if (html) {
1516#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001517 doc = htmlParseFile(filename, NULL);
1518#else
Daniel Veillardcd337f02001-11-22 18:20:37 +00001519 fprintf(stdout, "HTML support not compiled in\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00001520 doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001521#endif /* LIBXML_HTML_ENABLED */
1522 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001523 doc = xmlParseFile(filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001524 }
1525 if (doc != NULL) {
1526 if (ctxt->loaded == 1) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001527 xmlFreeDoc(ctxt->doc);
1528 }
1529 ctxt->loaded = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001530#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001531 xmlXPathFreeContext(ctxt->pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001532#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001533 xmlFree(ctxt->filename);
1534 ctxt->doc = doc;
1535 ctxt->node = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001536#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001537 ctxt->pctxt = xmlXPathNewContext(doc);
Owen Taylor3473f882001-02-23 17:55:21 +00001538#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001539 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001540 } else
Daniel Veillard78d12092001-10-11 09:12:24 +00001541 return (-1);
1542 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001543}
1544
1545/**
1546 * xmlShellWrite:
1547 * @ctxt: the shell context
1548 * @filename: the file name
1549 * @node: a node in the tree
1550 * @node2: unused
1551 *
1552 * Implements the XML shell function "write"
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001553 * Write the current node to the filename, it saves the serialization
Owen Taylor3473f882001-02-23 17:55:21 +00001554 * of the subtree under the @node specified
1555 *
1556 * Returns 0 or -1 in case of error
1557 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001558int
Owen Taylor3473f882001-02-23 17:55:21 +00001559xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Daniel Veillard78d12092001-10-11 09:12:24 +00001560 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1561{
Owen Taylor3473f882001-02-23 17:55:21 +00001562 if (node == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001563 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001564 if ((filename == NULL) || (filename[0] == 0)) {
1565 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001566 "Write command requires a filename argument\n");
1567 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001568 }
1569#ifdef W_OK
1570 if (access((char *) filename, W_OK)) {
1571 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001572 "Cannot write to %s\n", filename);
1573 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001574 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001575#endif
1576 switch (node->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00001577 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00001578 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
1579 xmlGenericError(xmlGenericErrorContext,
1580 "Failed to write to %s\n", filename);
1581 return (-1);
1582 }
1583 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001584 case XML_HTML_DOCUMENT_NODE:
1585#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001586 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
1587 xmlGenericError(xmlGenericErrorContext,
1588 "Failed to write to %s\n", filename);
1589 return (-1);
1590 }
Owen Taylor3473f882001-02-23 17:55:21 +00001591#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001592 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
1593 xmlGenericError(xmlGenericErrorContext,
1594 "Failed to write to %s\n", filename);
1595 return (-1);
1596 }
Owen Taylor3473f882001-02-23 17:55:21 +00001597#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001598 break;
1599 default:{
1600 FILE *f;
Owen Taylor3473f882001-02-23 17:55:21 +00001601
Daniel Veillard78d12092001-10-11 09:12:24 +00001602 f = fopen((char *) filename, "w");
1603 if (f == NULL) {
1604 xmlGenericError(xmlGenericErrorContext,
1605 "Failed to write to %s\n", filename);
1606 return (-1);
1607 }
1608 xmlElemDump(f, ctxt->doc, node);
1609 fclose(f);
1610 }
Owen Taylor3473f882001-02-23 17:55:21 +00001611 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001612 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001613}
1614
1615/**
1616 * xmlShellSave:
1617 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001618 * @filename: the file name (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00001619 * @node: unused
1620 * @node2: unused
1621 *
1622 * Implements the XML shell function "save"
1623 * Write the current document to the filename, or it's original name
1624 *
1625 * Returns 0 or -1 in case of error
1626 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001627int
1628xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
1629 xmlNodePtr node ATTRIBUTE_UNUSED,
1630 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1631{
Owen Taylor3473f882001-02-23 17:55:21 +00001632 if (ctxt->doc == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001633 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001634 if ((filename == NULL) || (filename[0] == 0))
1635 filename = ctxt->filename;
1636#ifdef W_OK
1637 if (access((char *) filename, W_OK)) {
1638 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001639 "Cannot save to %s\n", filename);
1640 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001641 }
1642#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00001643 switch (ctxt->doc->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00001644 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00001645 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
1646 xmlGenericError(xmlGenericErrorContext,
1647 "Failed to save to %s\n", filename);
1648 }
1649 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001650 case XML_HTML_DOCUMENT_NODE:
1651#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001652 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
1653 xmlGenericError(xmlGenericErrorContext,
1654 "Failed to save to %s\n", filename);
1655 }
Owen Taylor3473f882001-02-23 17:55:21 +00001656#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001657 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
1658 xmlGenericError(xmlGenericErrorContext,
1659 "Failed to save to %s\n", filename);
1660 }
Owen Taylor3473f882001-02-23 17:55:21 +00001661#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001662 break;
1663 default:
1664 xmlGenericError(xmlGenericErrorContext,
1665 "To save to subparts of a document use the 'write' command\n");
1666 return (-1);
1667
Owen Taylor3473f882001-02-23 17:55:21 +00001668 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001669 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001670}
1671
1672/**
1673 * xmlShellValidate:
1674 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001675 * @dtd: the DTD URI (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00001676 * @node: unused
1677 * @node2: unused
1678 *
1679 * Implements the XML shell function "validate"
1680 * Validate the document, if a DTD path is provided, then the validation
1681 * is done against the given DTD.
1682 *
1683 * Returns 0 or -1 in case of error
1684 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001685int
1686xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
1687 xmlNodePtr node ATTRIBUTE_UNUSED,
1688 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1689{
Owen Taylor3473f882001-02-23 17:55:21 +00001690 xmlValidCtxt vctxt;
1691 int res = -1;
1692
1693 vctxt.userData = stderr;
1694 vctxt.error = (xmlValidityErrorFunc) fprintf;
1695 vctxt.warning = (xmlValidityWarningFunc) fprintf;
1696
1697 if ((dtd == NULL) || (dtd[0] == 0)) {
1698 res = xmlValidateDocument(&vctxt, ctxt->doc);
1699 } else {
1700 xmlDtdPtr subset;
1701
Daniel Veillard78d12092001-10-11 09:12:24 +00001702 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
1703 if (subset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001704 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
1705
Daniel Veillard78d12092001-10-11 09:12:24 +00001706 xmlFreeDtd(subset);
1707 }
Owen Taylor3473f882001-02-23 17:55:21 +00001708 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001709 return (res);
Owen Taylor3473f882001-02-23 17:55:21 +00001710}
1711
1712/**
1713 * xmlShellDu:
1714 * @ctxt: the shell context
1715 * @arg: unused
1716 * @tree: a node defining a subtree
1717 * @node2: unused
1718 *
1719 * Implements the XML shell function "du"
1720 * show the structure of the subtree under node @tree
1721 * If @tree is null, the command works on the current node.
1722 *
1723 * Returns 0 or -1 in case of error
1724 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001725int
1726xmlShellDu(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1727 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
1728 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1729{
Owen Taylor3473f882001-02-23 17:55:21 +00001730 xmlNodePtr node;
Daniel Veillard78d12092001-10-11 09:12:24 +00001731 int indent = 0, i;
Owen Taylor3473f882001-02-23 17:55:21 +00001732
Daniel Veillard78d12092001-10-11 09:12:24 +00001733 if (tree == NULL)
1734 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001735 node = tree;
1736 while (node != NULL) {
1737 if ((node->type == XML_DOCUMENT_NODE) ||
1738 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillardcd337f02001-11-22 18:20:37 +00001739 fprintf(stdout, "/\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00001740 } else if (node->type == XML_ELEMENT_NODE) {
1741 for (i = 0; i < indent; i++)
Daniel Veillardcd337f02001-11-22 18:20:37 +00001742 fprintf(stdout, " ");
1743 fprintf(stdout, "%s\n", node->name);
Daniel Veillard78d12092001-10-11 09:12:24 +00001744 } else {
1745 }
Owen Taylor3473f882001-02-23 17:55:21 +00001746
Daniel Veillard78d12092001-10-11 09:12:24 +00001747 /*
1748 * Browse the full subtree, deep first
1749 */
Owen Taylor3473f882001-02-23 17:55:21 +00001750
1751 if ((node->type == XML_DOCUMENT_NODE) ||
1752 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001753 node = ((xmlDocPtr) node)->children;
1754 } else if ((node->children != NULL)
1755 && (node->type != XML_ENTITY_REF_NODE)) {
1756 /* deep first */
1757 node = node->children;
1758 indent++;
1759 } else if ((node != tree) && (node->next != NULL)) {
1760 /* then siblings */
1761 node = node->next;
1762 } else if (node != tree) {
1763 /* go up to parents->next if needed */
1764 while (node != tree) {
1765 if (node->parent != NULL) {
1766 node = node->parent;
1767 indent--;
1768 }
1769 if ((node != tree) && (node->next != NULL)) {
1770 node = node->next;
1771 break;
1772 }
1773 if (node->parent == NULL) {
1774 node = NULL;
1775 break;
1776 }
1777 if (node == tree) {
1778 node = NULL;
1779 break;
1780 }
1781 }
1782 /* exit condition */
1783 if (node == tree)
1784 node = NULL;
1785 } else
1786 node = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001787 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001788 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001789}
1790
1791/**
1792 * xmlShellPwd:
1793 * @ctxt: the shell context
1794 * @buffer: the output buffer
1795 * @tree: a node
1796 * @node2: unused
1797 *
1798 * Implements the XML shell function "pwd"
1799 * Show the full path from the root to the node, if needed building
1800 * thumblers when similar elements exists at a given ancestor level.
1801 * The output is compatible with XPath commands.
1802 *
1803 * Returns 0 or -1 in case of error
1804 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001805int
1806xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
1807 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
1808{
Daniel Veillardc6e013a2001-11-10 10:08:57 +00001809 xmlChar *path;
Owen Taylor3473f882001-02-23 17:55:21 +00001810
Daniel Veillard78d12092001-10-11 09:12:24 +00001811 if (node == NULL)
1812 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001813
Daniel Veillardc6e013a2001-11-10 10:08:57 +00001814 path = xmlGetNodePath(node);
1815 if (path == NULL)
1816 return (-1);
1817
1818 /*
1819 * This test prevents buffer overflow, because this routine
1820 * is only called by xmlShell, in which the second argument is
1821 * 500 chars long.
1822 * It is a dirty hack before a cleaner solution is found.
1823 * Documentation should mention that the second argument must
1824 * be at least 500 chars long, and could be stripped if too long.
1825 */
1826 snprintf(buffer, 499, "%s", path);
1827 buffer[499] = '0';
1828 xmlFree(path);
1829
Daniel Veillard78d12092001-10-11 09:12:24 +00001830 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001831}
1832
1833/**
1834 * xmlShell
1835 * @doc: the initial document
1836 * @filename: the output buffer
1837 * @input: the line reading function
1838 * @output: the output FILE*
1839 *
1840 * Implements the XML shell
1841 * This allow to load, validate, view, modify and save a document
1842 * using a environment similar to a UNIX commandline.
1843 */
1844void
1845xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Daniel Veillard78d12092001-10-11 09:12:24 +00001846 FILE * output)
1847{
Owen Taylor3473f882001-02-23 17:55:21 +00001848 char prompt[500] = "/ > ";
1849 char *cmdline = NULL, *cur;
1850 int nbargs;
1851 char command[100];
1852 char arg[400];
1853 int i;
1854 xmlShellCtxtPtr ctxt;
1855 xmlXPathObjectPtr list;
1856
1857 if (doc == NULL)
1858 return;
1859 if (filename == NULL)
1860 return;
1861 if (input == NULL)
1862 return;
1863 if (output == NULL)
1864 return;
1865 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Daniel Veillard78d12092001-10-11 09:12:24 +00001866 if (ctxt == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001867 return;
1868 ctxt->loaded = 0;
1869 ctxt->doc = doc;
1870 ctxt->input = input;
1871 ctxt->output = output;
1872 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Daniel Veillard78d12092001-10-11 09:12:24 +00001873 ctxt->node = (xmlNodePtr) ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001874
1875#ifdef LIBXML_XPATH_ENABLED
1876 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
1877 if (ctxt->pctxt == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001878 xmlFree(ctxt);
1879 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001880 }
1881#endif /* LIBXML_XPATH_ENABLED */
1882 while (1) {
1883 if (ctxt->node == (xmlNodePtr) ctxt->doc)
Daniel Veillard78d12092001-10-11 09:12:24 +00001884 sprintf(prompt, "%s > ", "/");
1885 else if (ctxt->node->name)
1886 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001887 else
Daniel Veillard78d12092001-10-11 09:12:24 +00001888 sprintf(prompt, "? > ");
Owen Taylor3473f882001-02-23 17:55:21 +00001889 prompt[sizeof(prompt) - 1] = 0;
1890
Daniel Veillard78d12092001-10-11 09:12:24 +00001891 /*
1892 * Get a new command line
1893 */
Owen Taylor3473f882001-02-23 17:55:21 +00001894 cmdline = ctxt->input(prompt);
Daniel Veillard78d12092001-10-11 09:12:24 +00001895 if (cmdline == NULL)
1896 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001897
Daniel Veillard78d12092001-10-11 09:12:24 +00001898 /*
1899 * Parse the command itself
1900 */
1901 cur = cmdline;
1902 nbargs = 0;
1903 while ((*cur == ' ') || (*cur == '\t'))
1904 cur++;
1905 i = 0;
1906 while ((*cur != ' ') && (*cur != '\t') &&
1907 (*cur != '\n') && (*cur != '\r')) {
1908 if (*cur == 0)
1909 break;
1910 command[i++] = *cur++;
1911 }
1912 command[i] = 0;
1913 if (i == 0)
1914 continue;
1915 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00001916
Daniel Veillard78d12092001-10-11 09:12:24 +00001917 /*
1918 * Parse the argument
1919 */
1920 while ((*cur == ' ') || (*cur == '\t'))
1921 cur++;
1922 i = 0;
1923 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
1924 if (*cur == 0)
1925 break;
1926 arg[i++] = *cur++;
1927 }
1928 arg[i] = 0;
1929 if (i != 0)
1930 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00001931
Daniel Veillard78d12092001-10-11 09:12:24 +00001932 /*
1933 * start interpreting the command
1934 */
Owen Taylor3473f882001-02-23 17:55:21 +00001935 if (!strcmp(command, "exit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00001936 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001937 if (!strcmp(command, "quit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00001938 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001939 if (!strcmp(command, "bye"))
Daniel Veillard78d12092001-10-11 09:12:24 +00001940 break;
Daniel Veillard5004f422001-11-08 13:53:05 +00001941 if (!strcmp(command, "help")) {
Daniel Veillardcd337f02001-11-22 18:20:37 +00001942 fprintf(stdout, "\tbase display XML base of the node\n");
1943 fprintf(stdout, "\tbye leave shell\n");
1944 fprintf(stdout, "\tcat [node] display node or current node\n");
1945 fprintf(stdout, "\tcd [path] change directory to path or to root\n");
1946 fprintf(stdout, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
1947 fprintf(stdout, "\tdu [path] show the structure of the subtree under path or the current node\n");
1948 fprintf(stdout, "\texit leave shell\n");
1949 fprintf(stdout, "\thelp display this help\n");
1950 fprintf(stdout, "\tfree display memory usage\n");
1951 fprintf(stdout, "\tload [name] load a new document with name\n");
1952 fprintf(stdout, "\tls [path] list contents of path or the current directory\n");
1953 fprintf(stdout, "\tpwd display current working directory\n");
1954 fprintf(stdout, "\tquit leave shell\n");
1955 fprintf(stdout, "\tsave [name] save this document to name or the original name\n");
1956 fprintf(stdout, "\tvalidate check the document for errors\n");
1957 fprintf(stdout, "\twrite [name] write the current node to the filename\n");
Daniel Veillard5004f422001-11-08 13:53:05 +00001958 } else if (!strcmp(command, "validate")) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001959 xmlShellValidate(ctxt, arg, NULL, NULL);
1960 } else if (!strcmp(command, "load")) {
1961 xmlShellLoad(ctxt, arg, NULL, NULL);
1962 } else if (!strcmp(command, "save")) {
1963 xmlShellSave(ctxt, arg, NULL, NULL);
1964 } else if (!strcmp(command, "write")) {
1965 xmlShellWrite(ctxt, arg, NULL, NULL);
1966 } else if (!strcmp(command, "free")) {
1967 if (arg[0] == 0) {
1968 xmlMemShow(stdout, 0);
1969 } else {
1970 int len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001971
Daniel Veillard78d12092001-10-11 09:12:24 +00001972 sscanf(arg, "%d", &len);
1973 xmlMemShow(stdout, len);
1974 }
1975 } else if (!strcmp(command, "pwd")) {
1976 char dir[500];
Owen Taylor3473f882001-02-23 17:55:21 +00001977
Daniel Veillard78d12092001-10-11 09:12:24 +00001978 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
Daniel Veillardcd337f02001-11-22 18:20:37 +00001979 fprintf(stdout, "%s\n", dir);
Daniel Veillard78d12092001-10-11 09:12:24 +00001980 } else if (!strcmp(command, "du")) {
1981 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
1982 } else if (!strcmp(command, "base")) {
1983 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
1984 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
1985 int dir = (!strcmp(command, "dir"));
1986
1987 if (arg[0] == 0) {
1988 if (dir)
1989 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
1990 else
1991 xmlShellList(ctxt, NULL, ctxt->node, NULL);
1992 } else {
1993 ctxt->pctxt->node = ctxt->node;
Daniel Veillard61d80a22001-04-27 17:13:01 +00001994#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001995 ctxt->pctxt->node = ctxt->node;
1996 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
1997#else
1998 list = NULL;
1999#endif /* LIBXML_XPATH_ENABLED */
2000 if (list != NULL) {
2001 switch (list->type) {
2002 case XPATH_UNDEFINED:
2003 xmlGenericError(xmlGenericErrorContext,
2004 "%s: no such node\n", arg);
2005 break;
2006 case XPATH_NODESET:{
2007 int indx;
2008
Daniel Veillarda6825e82001-11-07 13:33:59 +00002009 if (list->nodesetval == NULL)
2010 break;
2011
Daniel Veillard78d12092001-10-11 09:12:24 +00002012 for (indx = 0;
2013 indx < list->nodesetval->nodeNr;
2014 indx++) {
2015 if (dir)
2016 xmlShellDir(ctxt, NULL,
2017 list->nodesetval->
2018 nodeTab[indx], NULL);
2019 else
2020 xmlShellList(ctxt, NULL,
2021 list->nodesetval->
2022 nodeTab[indx], NULL);
2023 }
2024 break;
2025 }
2026 case XPATH_BOOLEAN:
2027 xmlGenericError(xmlGenericErrorContext,
2028 "%s is a Boolean\n", arg);
2029 break;
2030 case XPATH_NUMBER:
2031 xmlGenericError(xmlGenericErrorContext,
2032 "%s is a number\n", arg);
2033 break;
2034 case XPATH_STRING:
2035 xmlGenericError(xmlGenericErrorContext,
2036 "%s is a string\n", arg);
2037 break;
2038 case XPATH_POINT:
2039 xmlGenericError(xmlGenericErrorContext,
2040 "%s is a point\n", arg);
2041 break;
2042 case XPATH_RANGE:
2043 xmlGenericError(xmlGenericErrorContext,
2044 "%s is a range\n", arg);
2045 break;
2046 case XPATH_LOCATIONSET:
2047 xmlGenericError(xmlGenericErrorContext,
2048 "%s is a range\n", arg);
2049 break;
2050 case XPATH_USERS:
2051 xmlGenericError(xmlGenericErrorContext,
2052 "%s is user-defined\n", arg);
2053 break;
2054 case XPATH_XSLT_TREE:
2055 xmlGenericError(xmlGenericErrorContext,
2056 "%s is an XSLT value tree\n",
2057 arg);
2058 break;
2059 }
2060#ifdef LIBXML_XPATH_ENABLED
2061 xmlXPathFreeObject(list);
Daniel Veillard61d80a22001-04-27 17:13:01 +00002062#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002063 } else {
2064 xmlGenericError(xmlGenericErrorContext,
2065 "%s: no such node\n", arg);
2066 }
2067 ctxt->pctxt->node = NULL;
2068 }
2069 } else if (!strcmp(command, "cd")) {
2070 if (arg[0] == 0) {
2071 ctxt->node = (xmlNodePtr) ctxt->doc;
2072 } else {
2073#ifdef LIBXML_XPATH_ENABLED
2074 ctxt->pctxt->node = ctxt->node;
2075 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2076#else
2077 list = NULL;
2078#endif /* LIBXML_XPATH_ENABLED */
2079 if (list != NULL) {
2080 switch (list->type) {
2081 case XPATH_UNDEFINED:
2082 xmlGenericError(xmlGenericErrorContext,
2083 "%s: no such node\n", arg);
2084 break;
2085 case XPATH_NODESET:
Daniel Veillarda6825e82001-11-07 13:33:59 +00002086 if (list->nodesetval != NULL) {
2087 if (list->nodesetval->nodeNr == 1) {
2088 ctxt->node = list->nodesetval->nodeTab[0];
2089 } else
2090 xmlGenericError(xmlGenericErrorContext,
2091 "%s is a %d Node Set\n",
2092 arg,
2093 list->nodesetval->nodeNr);
Daniel Veillard78d12092001-10-11 09:12:24 +00002094 } else
2095 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda6825e82001-11-07 13:33:59 +00002096 "%s is an empty Node Set\n",
2097 arg);
Daniel Veillard78d12092001-10-11 09:12:24 +00002098 break;
2099 case XPATH_BOOLEAN:
2100 xmlGenericError(xmlGenericErrorContext,
2101 "%s is a Boolean\n", arg);
2102 break;
2103 case XPATH_NUMBER:
2104 xmlGenericError(xmlGenericErrorContext,
2105 "%s is a number\n", arg);
2106 break;
2107 case XPATH_STRING:
2108 xmlGenericError(xmlGenericErrorContext,
2109 "%s is a string\n", arg);
2110 break;
2111 case XPATH_POINT:
2112 xmlGenericError(xmlGenericErrorContext,
2113 "%s is a point\n", arg);
2114 break;
2115 case XPATH_RANGE:
2116 xmlGenericError(xmlGenericErrorContext,
2117 "%s is a range\n", arg);
2118 break;
2119 case XPATH_LOCATIONSET:
2120 xmlGenericError(xmlGenericErrorContext,
2121 "%s is a range\n", arg);
2122 break;
2123 case XPATH_USERS:
2124 xmlGenericError(xmlGenericErrorContext,
2125 "%s is user-defined\n", arg);
2126 break;
2127 case XPATH_XSLT_TREE:
2128 xmlGenericError(xmlGenericErrorContext,
2129 "%s is an XSLT value tree\n",
2130 arg);
2131 break;
2132 }
2133#ifdef LIBXML_XPATH_ENABLED
2134 xmlXPathFreeObject(list);
2135#endif
2136 } else {
2137 xmlGenericError(xmlGenericErrorContext,
2138 "%s: no such node\n", arg);
2139 }
2140 ctxt->pctxt->node = NULL;
2141 }
2142 } else if (!strcmp(command, "cat")) {
2143 if (arg[0] == 0) {
2144 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
2145 } else {
2146 ctxt->pctxt->node = ctxt->node;
2147#ifdef LIBXML_XPATH_ENABLED
2148 ctxt->pctxt->node = ctxt->node;
2149 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2150#else
2151 list = NULL;
2152#endif /* LIBXML_XPATH_ENABLED */
2153 if (list != NULL) {
2154 switch (list->type) {
2155 case XPATH_UNDEFINED:
2156 xmlGenericError(xmlGenericErrorContext,
2157 "%s: no such node\n", arg);
2158 break;
2159 case XPATH_NODESET:{
2160 int indx;
2161
Daniel Veillarda6825e82001-11-07 13:33:59 +00002162 if (list->nodesetval == NULL)
2163 break;
2164
Daniel Veillard78d12092001-10-11 09:12:24 +00002165 for (indx = 0;
2166 indx < list->nodesetval->nodeNr;
2167 indx++) {
2168 if (i > 0)
Daniel Veillardcd337f02001-11-22 18:20:37 +00002169 fprintf(stdout, " -------\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002170 xmlShellCat(ctxt, NULL,
2171 list->nodesetval->
2172 nodeTab[indx], NULL);
2173 }
2174 break;
2175 }
2176 case XPATH_BOOLEAN:
2177 xmlGenericError(xmlGenericErrorContext,
2178 "%s is a Boolean\n", arg);
2179 break;
2180 case XPATH_NUMBER:
2181 xmlGenericError(xmlGenericErrorContext,
2182 "%s is a number\n", arg);
2183 break;
2184 case XPATH_STRING:
2185 xmlGenericError(xmlGenericErrorContext,
2186 "%s is a string\n", arg);
2187 break;
2188 case XPATH_POINT:
2189 xmlGenericError(xmlGenericErrorContext,
2190 "%s is a point\n", arg);
2191 break;
2192 case XPATH_RANGE:
2193 xmlGenericError(xmlGenericErrorContext,
2194 "%s is a range\n", arg);
2195 break;
2196 case XPATH_LOCATIONSET:
2197 xmlGenericError(xmlGenericErrorContext,
2198 "%s is a range\n", arg);
2199 break;
2200 case XPATH_USERS:
2201 xmlGenericError(xmlGenericErrorContext,
2202 "%s is user-defined\n", arg);
2203 break;
2204 case XPATH_XSLT_TREE:
2205 xmlGenericError(xmlGenericErrorContext,
2206 "%s is an XSLT value tree\n",
2207 arg);
2208 break;
2209 }
2210#ifdef LIBXML_XPATH_ENABLED
2211 xmlXPathFreeObject(list);
2212#endif
2213 } else {
2214 xmlGenericError(xmlGenericErrorContext,
2215 "%s: no such node\n", arg);
2216 }
2217 ctxt->pctxt->node = NULL;
2218 }
2219 } else {
2220 xmlGenericError(xmlGenericErrorContext,
2221 "Unknown command %s\n", command);
2222 }
2223 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00002224 }
2225#ifdef LIBXML_XPATH_ENABLED
2226 xmlXPathFreeContext(ctxt->pctxt);
2227#endif /* LIBXML_XPATH_ENABLED */
2228 if (ctxt->loaded) {
2229 xmlFreeDoc(ctxt->doc);
2230 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002231 if (ctxt->filename != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002232 xmlFree(ctxt->filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002233 xmlFree(ctxt);
2234 if (cmdline != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002235 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00002236}
2237
2238#endif /* LIBXML_DEBUG_ENABLED */