blob: b1461a928a3a3bca8454c21de6f3b9facf48b568 [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)
87 fprintf(output, "PBM: Dtd has no parent\n");
88 if (dtd->doc == NULL)
89 fprintf(output, "PBM: Dtd has no doc\n");
90 if ((dtd->parent != NULL) && (dtd->doc != dtd->parent->doc))
91 fprintf(output, "PBM: Dtd doc differs from parent's one\n");
92 if (dtd->prev == NULL) {
93 if ((dtd->parent != NULL) && (dtd->parent->children != (xmlNodePtr)dtd))
94 fprintf(output, "PBM: Dtd has no prev and not first of list\n");
95 } else {
96 if (dtd->prev->next != (xmlNodePtr) dtd)
97 fprintf(output, "PBM: Dtd prev->next : back link wrong\n");
98 }
99 if (dtd->next == NULL) {
100 if ((dtd->parent != NULL) && (dtd->parent->last != (xmlNodePtr) dtd))
101 fprintf(output, "PBM: Dtd has no next and not last of list\n");
102 } else {
103 if (dtd->next->prev != (xmlNodePtr) dtd)
104 fprintf(output, "PBM: Dtd next->prev : forward link wrong\n");
105 }
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 }
198 printf("\n");
199
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 }
272 printf("\n");
273
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 Veillard5e2dace2001-07-18 19:30:27 +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,
872 "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)
875 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))
880 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)
883 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 Veillard78d12092001-10-11 09:12:24 +00001006int xmlLsCountNode(xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00001007 int ret = 0;
1008 xmlNodePtr list = NULL;
1009
1010 switch (node->type) {
1011 case XML_ELEMENT_NODE:
1012 list = node->children;
1013 break;
1014 case XML_DOCUMENT_NODE:
1015 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00001016#ifdef LIBXML_DOCB_ENABLED
1017 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001018#endif
1019 list = ((xmlDocPtr) node)->children;
1020 break;
1021 case XML_ATTRIBUTE_NODE:
1022 list = ((xmlAttrPtr) node)->children;
1023 break;
1024 case XML_TEXT_NODE:
1025 case XML_CDATA_SECTION_NODE:
1026 case XML_PI_NODE:
1027 case XML_COMMENT_NODE:
1028 if (node->content != NULL) {
1029#ifndef XML_USE_BUFFER_CONTENT
1030 ret = xmlStrlen(node->content);
1031#else
1032 ret = xmlBufferLength(node->content);
1033#endif
1034 }
1035 break;
1036 case XML_ENTITY_REF_NODE:
1037 case XML_DOCUMENT_TYPE_NODE:
1038 case XML_ENTITY_NODE:
1039 case XML_DOCUMENT_FRAG_NODE:
1040 case XML_NOTATION_NODE:
1041 case XML_DTD_NODE:
1042 case XML_ELEMENT_DECL:
1043 case XML_ATTRIBUTE_DECL:
1044 case XML_ENTITY_DECL:
1045 case XML_NAMESPACE_DECL:
1046 case XML_XINCLUDE_START:
1047 case XML_XINCLUDE_END:
1048 ret = 1;
1049 break;
1050 }
1051 for (;list != NULL;ret++)
1052 list = list->next;
1053 return(ret);
1054}
1055
Daniel Veillard78d12092001-10-11 09:12:24 +00001056void
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001057xmlLsOneNode(FILE *output, xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00001058 switch (node->type) {
1059 case XML_ELEMENT_NODE:
1060 fprintf(output, "-");
1061 break;
1062 case XML_ATTRIBUTE_NODE:
1063 fprintf(output, "a");
1064 break;
1065 case XML_TEXT_NODE:
1066 fprintf(output, "t");
1067 break;
1068 case XML_CDATA_SECTION_NODE:
1069 fprintf(output, "c");
1070 break;
1071 case XML_ENTITY_REF_NODE:
1072 fprintf(output, "e");
1073 break;
1074 case XML_ENTITY_NODE:
1075 fprintf(output, "E");
1076 break;
1077 case XML_PI_NODE:
1078 fprintf(output, "p");
1079 break;
1080 case XML_COMMENT_NODE:
1081 fprintf(output, "c");
1082 break;
1083 case XML_DOCUMENT_NODE:
1084 fprintf(output, "d");
1085 break;
1086 case XML_HTML_DOCUMENT_NODE:
1087 fprintf(output, "h");
1088 break;
1089 case XML_DOCUMENT_TYPE_NODE:
1090 fprintf(output, "T");
1091 break;
1092 case XML_DOCUMENT_FRAG_NODE:
1093 fprintf(output, "F");
1094 break;
1095 case XML_NOTATION_NODE:
1096 fprintf(output, "N");
1097 break;
1098 default:
1099 fprintf(output, "?");
1100 }
1101 if (node->properties != NULL)
1102 fprintf(output, "a");
1103 else
1104 fprintf(output, "-");
1105 if (node->nsDef != NULL)
1106 fprintf(output, "n");
1107 else
1108 fprintf(output, "-");
1109
1110 fprintf(output, " %8d ", xmlLsCountNode(node));
1111
1112 switch (node->type) {
1113 case XML_ELEMENT_NODE:
1114 if (node->name != NULL)
1115 fprintf(output, "%s", node->name);
1116 break;
1117 case XML_ATTRIBUTE_NODE:
1118 if (node->name != NULL)
1119 fprintf(output, "%s", node->name);
1120 break;
1121 case XML_TEXT_NODE:
1122 if (node->content != NULL) {
1123#ifndef XML_USE_BUFFER_CONTENT
1124 xmlDebugDumpString(output, node->content);
1125#else
1126 xmlDebugDumpString(output, xmlBufferContent(node->content));
1127#endif
1128 }
1129 break;
1130 case XML_CDATA_SECTION_NODE:
1131 break;
1132 case XML_ENTITY_REF_NODE:
1133 if (node->name != NULL)
1134 fprintf(output, "%s", node->name);
1135 break;
1136 case XML_ENTITY_NODE:
1137 if (node->name != NULL)
1138 fprintf(output, "%s", node->name);
1139 break;
1140 case XML_PI_NODE:
1141 if (node->name != NULL)
1142 fprintf(output, "%s", node->name);
1143 break;
1144 case XML_COMMENT_NODE:
1145 break;
1146 case XML_DOCUMENT_NODE:
1147 break;
1148 case XML_HTML_DOCUMENT_NODE:
1149 break;
1150 case XML_DOCUMENT_TYPE_NODE:
1151 break;
1152 case XML_DOCUMENT_FRAG_NODE:
1153 break;
1154 case XML_NOTATION_NODE:
1155 break;
1156 default:
1157 if (node->name != NULL)
1158 fprintf(output, "%s", node->name);
1159 }
1160 fprintf(output, "\n");
1161}
1162
Daniel Veillard78d12092001-10-11 09:12:24 +00001163/**
1164 * xmlBoolToText:
1165 * @bool : a bool to turn into text
1166 *
1167 * Convenient way to turn bool into text
1168*/
1169const char *
1170xmlBoolToText(int bool)
1171{
1172 if (bool)
1173 return("True");
1174 else
1175 return("False");
1176}
1177
1178
1179/**
1180 * xmlGetLineNo:
1181 * @node : valid node
1182 *
1183 * Get line number of node
1184 *
1185 * Returns the line number if sucessfull, -1 otherwise
1186 */
1187long
1188xmlGetLineNo(xmlNodePtr node)
1189{
1190 long result = -1;
1191
1192 if (!node)
1193 return result;
1194 if (node->type == XML_ELEMENT_NODE)
1195 result = (long) node->content;
1196 else if ((node->prev != NULL) &&
1197 (node->prev->type == XML_ELEMENT_NODE))
1198 result = (long) node->prev->content;
1199 else if ((node->parent != NULL) &&
1200 (node->parent->type == XML_ELEMENT_NODE))
1201 result = (long) node->parent->content;
1202
1203 return result;
1204}
1205
Owen Taylor3473f882001-02-23 17:55:21 +00001206/****************************************************************
1207 * *
1208 * The XML shell related functions *
1209 * *
1210 ****************************************************************/
1211
Daniel Veillard78d12092001-10-11 09:12:24 +00001212
1213
Owen Taylor3473f882001-02-23 17:55:21 +00001214/*
1215 * TODO: Improvement/cleanups for the XML shell
1216 * - allow to shell out an editor on a subpart
1217 * - cleanup function registrations (with help) and calling
1218 * - provide registration routines
1219 */
1220
1221/**
Daniel Veillard78d12092001-10-11 09:12:24 +00001222 * xmlShellPrintXpathError:
1223 * @errorType: valid xpath error id
1224 * @arg : the argument that cause xpath to fail
1225 *
1226 * Print the xpath error to libxml default error channel
1227 */
1228void
1229xmlShellPrintXPathError(int errorType, const char *arg)
1230{
1231 const char *default_arg = "Result";
1232
1233 if (!arg)
1234 arg = default_arg;
1235
1236 switch (errorType) {
1237 case XPATH_UNDEFINED:
1238 xmlGenericError(xmlGenericErrorContext,
1239 "%s: no such node\n", arg);
1240 break;
1241
1242 case XPATH_BOOLEAN:
1243 xmlGenericError(xmlGenericErrorContext,
1244 "%s is a Boolean\n", arg);
1245 break;
1246 case XPATH_NUMBER:
1247 xmlGenericError(xmlGenericErrorContext,
1248 "%s is a number\n", arg);
1249 break;
1250 case XPATH_STRING:
1251 xmlGenericError(xmlGenericErrorContext,
1252 "%s is a string\n", arg);
1253 break;
1254 case XPATH_POINT:
1255 xmlGenericError(xmlGenericErrorContext,
1256 "%s is a point\n", arg);
1257 break;
1258 case XPATH_RANGE:
1259 xmlGenericError(xmlGenericErrorContext,
1260 "%s is a range\n", arg);
1261 break;
1262 case XPATH_LOCATIONSET:
1263 xmlGenericError(xmlGenericErrorContext,
1264 "%s is a range\n", arg);
1265 break;
1266 case XPATH_USERS:
1267 xmlGenericError(xmlGenericErrorContext,
1268 "%s is user-defined\n", arg);
1269 break;
1270 case XPATH_XSLT_TREE:
1271 xmlGenericError(xmlGenericErrorContext,
1272 "%s is an XSLT value tree\n", arg);
1273 break;
1274 }
1275 xmlGenericError(xmlGenericErrorContext,
1276 "Try casting the result string function (xpath builtin)\n",
1277 arg);
1278}
1279
1280
1281/**
1282 * xmlShellPrintNode:
1283 * @node : a non-null node to print to stdout
1284 *
1285 * Print node to stdout
1286 */
1287void
1288xmlShellPrintNode(xmlNodePtr node)
1289{
1290 if (!node)
1291 return;
1292
1293 if (node->type == XML_DOCUMENT_NODE)
1294 xmlDocDump(stdout, (xmlDocPtr) node);
1295 else if (node->type == XML_ATTRIBUTE_NODE)
1296 xmlDebugDumpAttrList(stdout, (xmlAttrPtr) node, 0);
1297 else
1298 xmlElemDump(stdout, node->doc, node);
1299
1300 fprintf(stdout, "\n");
1301}
1302
1303
1304/**
1305 * xmlShellPrintXPathResult:
1306 * list : a valid result generated by an xpath evaluation
1307 *
1308 * Prints result to stdout
1309 */
1310void
1311xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1312{
1313 int i = 0;
1314
1315 if (list != NULL) {
1316 switch (list->type) {
1317 case XPATH_NODESET:{
1318 int indx;
1319
1320 if (list->nodesetval) {
1321 for (indx = 0; indx < list->nodesetval->nodeNr;
1322 indx++) {
1323 if (i > 0)
1324 fprintf(stderr, " -------\n");
1325 xmlShellPrintNode(list->nodesetval->
1326 nodeTab[indx]);
1327 }
1328 } else {
1329 xmlGenericError(xmlGenericErrorContext,
1330 "Empty node set\n");
1331 }
1332 break;
1333 }
1334 case XPATH_BOOLEAN:
1335 xmlGenericError(xmlGenericErrorContext,
1336 "Is a Boolean:%s\n",
1337 xmlBoolToText(list->boolval));
1338 break;
1339 case XPATH_NUMBER:
1340 xmlGenericError(xmlGenericErrorContext,
1341 "Is a number:%0g\n", list->floatval);
1342 break;
1343 case XPATH_STRING:
1344 xmlGenericError(xmlGenericErrorContext,
1345 "Is a string:%s\n", list->stringval);
1346 break;
1347
1348 default:
1349 xmlShellPrintXPathError(list->type, NULL);
1350 }
1351 }
1352}
1353
1354/**
Owen Taylor3473f882001-02-23 17:55:21 +00001355 * xmlShellList:
1356 * @ctxt: the shell context
1357 * @arg: unused
1358 * @node: a node
1359 * @node2: unused
1360 *
1361 * Implements the XML shell function "ls"
1362 * Does an Unix like listing of the given node (like a directory)
1363 *
1364 * Returns 0
1365 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001366int
1367xmlShellList(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1368 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1369 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1370{
Owen Taylor3473f882001-02-23 17:55:21 +00001371 xmlNodePtr cur;
1372
1373 if ((node->type == XML_DOCUMENT_NODE) ||
1374 (node->type == XML_HTML_DOCUMENT_NODE)) {
1375 cur = ((xmlDocPtr) node)->children;
1376 } else if (node->children != NULL) {
1377 cur = node->children;
1378 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001379 xmlLsOneNode(stdout, node);
1380 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001381 }
1382 while (cur != NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001383 xmlLsOneNode(stdout, cur);
1384 cur = cur->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001385 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001386 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001387}
1388
1389/**
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001390 * xmlShellBase:
1391 * @ctxt: the shell context
1392 * @arg: unused
1393 * @node: a node
1394 * @node2: unused
1395 *
1396 * Implements the XML shell function "base"
1397 * dumps the current XML base of the node
1398 *
1399 * Returns 0
1400 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001401int
1402xmlShellBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1403 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1404 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1405{
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001406 xmlChar *base;
1407
1408 base = xmlNodeGetBase(node->doc, node);
1409
1410 if (base == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001411 printf(" No base found !!!\n");
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001412 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001413 printf("%s\n", base);
1414 xmlFree(base);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001415 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001416 return (0);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001417}
1418
1419/**
Owen Taylor3473f882001-02-23 17:55:21 +00001420 * xmlShellDir:
1421 * @ctxt: the shell context
1422 * @arg: unused
1423 * @node: a node
1424 * @node2: unused
1425 *
1426 * Implements the XML shell function "dir"
1427 * dumps informations about the node (namespace, attributes, content).
1428 *
1429 * Returns 0
1430 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001431int
1432xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1433 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1434 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1435{
Owen Taylor3473f882001-02-23 17:55:21 +00001436 if ((node->type == XML_DOCUMENT_NODE) ||
1437 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001438 xmlDebugDumpDocumentHead(stdout, (xmlDocPtr) node);
Owen Taylor3473f882001-02-23 17:55:21 +00001439 } else if (node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001440 xmlDebugDumpAttr(stdout, (xmlAttrPtr) node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001441 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001442 xmlDebugDumpOneNode(stdout, node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001443 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001444 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001445}
1446
1447/**
1448 * xmlShellCat:
1449 * @ctxt: the shell context
1450 * @arg: unused
1451 * @node: a node
1452 * @node2: unused
1453 *
1454 * Implements the XML shell function "cat"
1455 * dumps the serialization node content (XML or HTML).
1456 *
1457 * Returns 0
1458 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001459int
1460xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
1461 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
1462{
Owen Taylor3473f882001-02-23 17:55:21 +00001463 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
1464#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001465 if (node->type == XML_HTML_DOCUMENT_NODE)
1466 htmlDocDump(stdout, (htmlDocPtr) node);
1467 else
1468 htmlNodeDumpFile(stdout, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001469#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001470 if (node->type == XML_DOCUMENT_NODE)
1471 xmlDocDump(stdout, (xmlDocPtr) node);
1472 else
1473 xmlElemDump(stdout, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001474#endif /* LIBXML_HTML_ENABLED */
1475 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001476 if (node->type == XML_DOCUMENT_NODE)
1477 xmlDocDump(stdout, (xmlDocPtr) node);
1478 else
1479 xmlElemDump(stdout, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001480 }
1481 printf("\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00001482 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001483}
1484
1485/**
1486 * xmlShellLoad:
1487 * @ctxt: the shell context
1488 * @filename: the file name
1489 * @node: unused
1490 * @node2: unused
1491 *
1492 * Implements the XML shell function "load"
1493 * loads a new document specified by the filename
1494 *
1495 * Returns 0 or -1 if loading failed
1496 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001497int
1498xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
1499 xmlNodePtr node ATTRIBUTE_UNUSED,
1500 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1501{
Owen Taylor3473f882001-02-23 17:55:21 +00001502 xmlDocPtr doc;
1503 int html = 0;
1504
1505 if (ctxt->doc != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001506 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00001507
1508 if (html) {
1509#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001510 doc = htmlParseFile(filename, NULL);
1511#else
1512 printf("HTML support not compiled in\n");
1513 doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001514#endif /* LIBXML_HTML_ENABLED */
1515 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001516 doc = xmlParseFile(filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001517 }
1518 if (doc != NULL) {
1519 if (ctxt->loaded == 1) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001520 xmlFreeDoc(ctxt->doc);
1521 }
1522 ctxt->loaded = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001523#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001524 xmlXPathFreeContext(ctxt->pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001525#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001526 xmlFree(ctxt->filename);
1527 ctxt->doc = doc;
1528 ctxt->node = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001529#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001530 ctxt->pctxt = xmlXPathNewContext(doc);
Owen Taylor3473f882001-02-23 17:55:21 +00001531#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001532 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001533 } else
Daniel Veillard78d12092001-10-11 09:12:24 +00001534 return (-1);
1535 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001536}
1537
1538/**
1539 * xmlShellWrite:
1540 * @ctxt: the shell context
1541 * @filename: the file name
1542 * @node: a node in the tree
1543 * @node2: unused
1544 *
1545 * Implements the XML shell function "write"
1546 * Write the current node to the filename, it saves the serailization
1547 * of the subtree under the @node specified
1548 *
1549 * Returns 0 or -1 in case of error
1550 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001551int
Owen Taylor3473f882001-02-23 17:55:21 +00001552xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Daniel Veillard78d12092001-10-11 09:12:24 +00001553 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1554{
Owen Taylor3473f882001-02-23 17:55:21 +00001555 if (node == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001556 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001557 if ((filename == NULL) || (filename[0] == 0)) {
1558 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001559 "Write command requires a filename argument\n");
1560 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001561 }
1562#ifdef W_OK
1563 if (access((char *) filename, W_OK)) {
1564 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001565 "Cannot write to %s\n", filename);
1566 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001567 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001568#endif
1569 switch (node->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00001570 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00001571 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
1572 xmlGenericError(xmlGenericErrorContext,
1573 "Failed to write to %s\n", filename);
1574 return (-1);
1575 }
1576 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001577 case XML_HTML_DOCUMENT_NODE:
1578#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001579 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
1580 xmlGenericError(xmlGenericErrorContext,
1581 "Failed to write to %s\n", filename);
1582 return (-1);
1583 }
Owen Taylor3473f882001-02-23 17:55:21 +00001584#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001585 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
1586 xmlGenericError(xmlGenericErrorContext,
1587 "Failed to write to %s\n", filename);
1588 return (-1);
1589 }
Owen Taylor3473f882001-02-23 17:55:21 +00001590#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001591 break;
1592 default:{
1593 FILE *f;
Owen Taylor3473f882001-02-23 17:55:21 +00001594
Daniel Veillard78d12092001-10-11 09:12:24 +00001595 f = fopen((char *) filename, "w");
1596 if (f == NULL) {
1597 xmlGenericError(xmlGenericErrorContext,
1598 "Failed to write to %s\n", filename);
1599 return (-1);
1600 }
1601 xmlElemDump(f, ctxt->doc, node);
1602 fclose(f);
1603 }
Owen Taylor3473f882001-02-23 17:55:21 +00001604 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001605 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001606}
1607
1608/**
1609 * xmlShellSave:
1610 * @ctxt: the shell context
1611 * @filename: the file name (optionnal)
1612 * @node: unused
1613 * @node2: unused
1614 *
1615 * Implements the XML shell function "save"
1616 * Write the current document to the filename, or it's original name
1617 *
1618 * Returns 0 or -1 in case of error
1619 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001620int
1621xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
1622 xmlNodePtr node ATTRIBUTE_UNUSED,
1623 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1624{
Owen Taylor3473f882001-02-23 17:55:21 +00001625 if (ctxt->doc == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001626 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001627 if ((filename == NULL) || (filename[0] == 0))
1628 filename = ctxt->filename;
1629#ifdef W_OK
1630 if (access((char *) filename, W_OK)) {
1631 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001632 "Cannot save to %s\n", filename);
1633 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001634 }
1635#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00001636 switch (ctxt->doc->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00001637 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00001638 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
1639 xmlGenericError(xmlGenericErrorContext,
1640 "Failed to save to %s\n", filename);
1641 }
1642 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001643 case XML_HTML_DOCUMENT_NODE:
1644#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001645 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
1646 xmlGenericError(xmlGenericErrorContext,
1647 "Failed to save to %s\n", filename);
1648 }
Owen Taylor3473f882001-02-23 17:55:21 +00001649#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001650 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
1651 xmlGenericError(xmlGenericErrorContext,
1652 "Failed to save to %s\n", filename);
1653 }
Owen Taylor3473f882001-02-23 17:55:21 +00001654#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001655 break;
1656 default:
1657 xmlGenericError(xmlGenericErrorContext,
1658 "To save to subparts of a document use the 'write' command\n");
1659 return (-1);
1660
Owen Taylor3473f882001-02-23 17:55:21 +00001661 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001662 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001663}
1664
1665/**
1666 * xmlShellValidate:
1667 * @ctxt: the shell context
1668 * @dtd: the DTD URI (optionnal)
1669 * @node: unused
1670 * @node2: unused
1671 *
1672 * Implements the XML shell function "validate"
1673 * Validate the document, if a DTD path is provided, then the validation
1674 * is done against the given DTD.
1675 *
1676 * Returns 0 or -1 in case of error
1677 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001678int
1679xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
1680 xmlNodePtr node ATTRIBUTE_UNUSED,
1681 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1682{
Owen Taylor3473f882001-02-23 17:55:21 +00001683 xmlValidCtxt vctxt;
1684 int res = -1;
1685
1686 vctxt.userData = stderr;
1687 vctxt.error = (xmlValidityErrorFunc) fprintf;
1688 vctxt.warning = (xmlValidityWarningFunc) fprintf;
1689
1690 if ((dtd == NULL) || (dtd[0] == 0)) {
1691 res = xmlValidateDocument(&vctxt, ctxt->doc);
1692 } else {
1693 xmlDtdPtr subset;
1694
Daniel Veillard78d12092001-10-11 09:12:24 +00001695 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
1696 if (subset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001697 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
1698
Daniel Veillard78d12092001-10-11 09:12:24 +00001699 xmlFreeDtd(subset);
1700 }
Owen Taylor3473f882001-02-23 17:55:21 +00001701 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001702 return (res);
Owen Taylor3473f882001-02-23 17:55:21 +00001703}
1704
1705/**
1706 * xmlShellDu:
1707 * @ctxt: the shell context
1708 * @arg: unused
1709 * @tree: a node defining a subtree
1710 * @node2: unused
1711 *
1712 * Implements the XML shell function "du"
1713 * show the structure of the subtree under node @tree
1714 * If @tree is null, the command works on the current node.
1715 *
1716 * Returns 0 or -1 in case of error
1717 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001718int
1719xmlShellDu(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1720 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
1721 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1722{
Owen Taylor3473f882001-02-23 17:55:21 +00001723 xmlNodePtr node;
Daniel Veillard78d12092001-10-11 09:12:24 +00001724 int indent = 0, i;
Owen Taylor3473f882001-02-23 17:55:21 +00001725
Daniel Veillard78d12092001-10-11 09:12:24 +00001726 if (tree == NULL)
1727 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001728 node = tree;
1729 while (node != NULL) {
1730 if ((node->type == XML_DOCUMENT_NODE) ||
1731 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001732 printf("/\n");
1733 } else if (node->type == XML_ELEMENT_NODE) {
1734 for (i = 0; i < indent; i++)
1735 printf(" ");
1736 printf("%s\n", node->name);
1737 } else {
1738 }
Owen Taylor3473f882001-02-23 17:55:21 +00001739
Daniel Veillard78d12092001-10-11 09:12:24 +00001740 /*
1741 * Browse the full subtree, deep first
1742 */
Owen Taylor3473f882001-02-23 17:55:21 +00001743
1744 if ((node->type == XML_DOCUMENT_NODE) ||
1745 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001746 node = ((xmlDocPtr) node)->children;
1747 } else if ((node->children != NULL)
1748 && (node->type != XML_ENTITY_REF_NODE)) {
1749 /* deep first */
1750 node = node->children;
1751 indent++;
1752 } else if ((node != tree) && (node->next != NULL)) {
1753 /* then siblings */
1754 node = node->next;
1755 } else if (node != tree) {
1756 /* go up to parents->next if needed */
1757 while (node != tree) {
1758 if (node->parent != NULL) {
1759 node = node->parent;
1760 indent--;
1761 }
1762 if ((node != tree) && (node->next != NULL)) {
1763 node = node->next;
1764 break;
1765 }
1766 if (node->parent == NULL) {
1767 node = NULL;
1768 break;
1769 }
1770 if (node == tree) {
1771 node = NULL;
1772 break;
1773 }
1774 }
1775 /* exit condition */
1776 if (node == tree)
1777 node = NULL;
1778 } else
1779 node = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001780 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001781 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001782}
1783
1784/**
1785 * xmlShellPwd:
1786 * @ctxt: the shell context
1787 * @buffer: the output buffer
1788 * @tree: a node
1789 * @node2: unused
1790 *
1791 * Implements the XML shell function "pwd"
1792 * Show the full path from the root to the node, if needed building
1793 * thumblers when similar elements exists at a given ancestor level.
1794 * The output is compatible with XPath commands.
1795 *
1796 * Returns 0 or -1 in case of error
1797 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001798int
1799xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
1800 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
1801{
Owen Taylor3473f882001-02-23 17:55:21 +00001802 xmlNodePtr cur, tmp, next;
1803 char buf[500];
1804 char sep;
1805 const char *name;
Daniel Veillard78d12092001-10-11 09:12:24 +00001806 char nametemp[100];
Owen Taylor3473f882001-02-23 17:55:21 +00001807 int occur = 0;
1808
1809 buffer[0] = 0;
Daniel Veillard78d12092001-10-11 09:12:24 +00001810 if (node == NULL)
1811 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001812 cur = node;
1813 do {
Daniel Veillard78d12092001-10-11 09:12:24 +00001814 name = "";
1815 sep = '?';
1816 occur = 0;
1817 if ((cur->type == XML_DOCUMENT_NODE) ||
1818 (cur->type == XML_HTML_DOCUMENT_NODE)) {
1819 sep = '/';
1820 next = NULL;
1821 } else if (cur->type == XML_ELEMENT_NODE) {
1822 sep = '/';
1823 name = (const char *) cur->name;
1824 if (cur->ns) {
1825 snprintf(nametemp, 99, "%s:%s", cur->ns->prefix,
1826 cur->name);
1827 name = nametemp;
1828 }
1829 next = cur->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00001830
Daniel Veillard78d12092001-10-11 09:12:24 +00001831 /*
1832 * Thumbler index computation
1833 */
1834 tmp = cur->prev;
Owen Taylor3473f882001-02-23 17:55:21 +00001835 while (tmp != NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001836 if (xmlStrEqual(cur->name, tmp->name))
1837 occur++;
1838 tmp = tmp->prev;
1839 }
1840 if (occur == 0) {
1841 tmp = cur->next;
1842 while (tmp != NULL) {
1843 if (xmlStrEqual(cur->name, tmp->name))
1844 occur++;
1845 tmp = tmp->next;
1846 }
1847 if (occur != 0)
1848 occur = 1;
1849 } else
1850 occur++;
1851 } else if (cur->type == XML_ATTRIBUTE_NODE) {
1852 sep = '@';
1853 name = (const char *) (((xmlAttrPtr) cur)->name);
1854 next = ((xmlAttrPtr) cur)->parent;
1855 } else {
1856 next = cur->parent;
1857 }
1858 if (occur == 0)
1859 snprintf(buf, sizeof(buf), "%c%s%s", sep, name, buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00001860 else
Daniel Veillard78d12092001-10-11 09:12:24 +00001861 snprintf(buf, sizeof(buf), "%c%s[%d]%s",
1862 sep, name, occur, buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00001863 buf[sizeof(buf) - 1] = 0;
1864 /*
1865 * This test prevents buffer overflow, because this routine
1866 * is only called by xmlShell, in which the second argument is
1867 * 500 chars long.
1868 * It is a dirty hack before a cleaner solution is found.
1869 * Documentation should mention that the second argument must
1870 * be at least 500 chars long, and could be stripped if too long.
1871 */
1872 if (strlen(buffer) + strlen(buf) > 499)
Daniel Veillard78d12092001-10-11 09:12:24 +00001873 break;
1874 strcpy(buffer, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001875 cur = next;
1876 } while (cur != NULL);
Daniel Veillard78d12092001-10-11 09:12:24 +00001877 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001878}
1879
1880/**
1881 * xmlShell
1882 * @doc: the initial document
1883 * @filename: the output buffer
1884 * @input: the line reading function
1885 * @output: the output FILE*
1886 *
1887 * Implements the XML shell
1888 * This allow to load, validate, view, modify and save a document
1889 * using a environment similar to a UNIX commandline.
1890 */
1891void
1892xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Daniel Veillard78d12092001-10-11 09:12:24 +00001893 FILE * output)
1894{
Owen Taylor3473f882001-02-23 17:55:21 +00001895 char prompt[500] = "/ > ";
1896 char *cmdline = NULL, *cur;
1897 int nbargs;
1898 char command[100];
1899 char arg[400];
1900 int i;
1901 xmlShellCtxtPtr ctxt;
1902 xmlXPathObjectPtr list;
1903
1904 if (doc == NULL)
1905 return;
1906 if (filename == NULL)
1907 return;
1908 if (input == NULL)
1909 return;
1910 if (output == NULL)
1911 return;
1912 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Daniel Veillard78d12092001-10-11 09:12:24 +00001913 if (ctxt == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001914 return;
1915 ctxt->loaded = 0;
1916 ctxt->doc = doc;
1917 ctxt->input = input;
1918 ctxt->output = output;
1919 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Daniel Veillard78d12092001-10-11 09:12:24 +00001920 ctxt->node = (xmlNodePtr) ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001921
1922#ifdef LIBXML_XPATH_ENABLED
1923 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
1924 if (ctxt->pctxt == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001925 xmlFree(ctxt);
1926 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001927 }
1928#endif /* LIBXML_XPATH_ENABLED */
1929 while (1) {
1930 if (ctxt->node == (xmlNodePtr) ctxt->doc)
Daniel Veillard78d12092001-10-11 09:12:24 +00001931 sprintf(prompt, "%s > ", "/");
1932 else if (ctxt->node->name)
1933 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001934 else
Daniel Veillard78d12092001-10-11 09:12:24 +00001935 sprintf(prompt, "? > ");
Owen Taylor3473f882001-02-23 17:55:21 +00001936 prompt[sizeof(prompt) - 1] = 0;
1937
Daniel Veillard78d12092001-10-11 09:12:24 +00001938 /*
1939 * Get a new command line
1940 */
Owen Taylor3473f882001-02-23 17:55:21 +00001941 cmdline = ctxt->input(prompt);
Daniel Veillard78d12092001-10-11 09:12:24 +00001942 if (cmdline == NULL)
1943 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001944
Daniel Veillard78d12092001-10-11 09:12:24 +00001945 /*
1946 * Parse the command itself
1947 */
1948 cur = cmdline;
1949 nbargs = 0;
1950 while ((*cur == ' ') || (*cur == '\t'))
1951 cur++;
1952 i = 0;
1953 while ((*cur != ' ') && (*cur != '\t') &&
1954 (*cur != '\n') && (*cur != '\r')) {
1955 if (*cur == 0)
1956 break;
1957 command[i++] = *cur++;
1958 }
1959 command[i] = 0;
1960 if (i == 0)
1961 continue;
1962 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00001963
Daniel Veillard78d12092001-10-11 09:12:24 +00001964 /*
1965 * Parse the argument
1966 */
1967 while ((*cur == ' ') || (*cur == '\t'))
1968 cur++;
1969 i = 0;
1970 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
1971 if (*cur == 0)
1972 break;
1973 arg[i++] = *cur++;
1974 }
1975 arg[i] = 0;
1976 if (i != 0)
1977 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00001978
Daniel Veillard78d12092001-10-11 09:12:24 +00001979 /*
1980 * start interpreting the command
1981 */
Owen Taylor3473f882001-02-23 17:55:21 +00001982 if (!strcmp(command, "exit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00001983 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001984 if (!strcmp(command, "quit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00001985 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001986 if (!strcmp(command, "bye"))
Daniel Veillard78d12092001-10-11 09:12:24 +00001987 break;
1988 if (!strcmp(command, "validate")) {
1989 xmlShellValidate(ctxt, arg, NULL, NULL);
1990 } else if (!strcmp(command, "load")) {
1991 xmlShellLoad(ctxt, arg, NULL, NULL);
1992 } else if (!strcmp(command, "save")) {
1993 xmlShellSave(ctxt, arg, NULL, NULL);
1994 } else if (!strcmp(command, "write")) {
1995 xmlShellWrite(ctxt, arg, NULL, NULL);
1996 } else if (!strcmp(command, "free")) {
1997 if (arg[0] == 0) {
1998 xmlMemShow(stdout, 0);
1999 } else {
2000 int len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002001
Daniel Veillard78d12092001-10-11 09:12:24 +00002002 sscanf(arg, "%d", &len);
2003 xmlMemShow(stdout, len);
2004 }
2005 } else if (!strcmp(command, "pwd")) {
2006 char dir[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002007
Daniel Veillard78d12092001-10-11 09:12:24 +00002008 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
2009 printf("%s\n", dir);
2010 } else if (!strcmp(command, "du")) {
2011 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2012 } else if (!strcmp(command, "base")) {
2013 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
2014 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
2015 int dir = (!strcmp(command, "dir"));
2016
2017 if (arg[0] == 0) {
2018 if (dir)
2019 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
2020 else
2021 xmlShellList(ctxt, NULL, ctxt->node, NULL);
2022 } else {
2023 ctxt->pctxt->node = ctxt->node;
Daniel Veillard61d80a22001-04-27 17:13:01 +00002024#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002025 ctxt->pctxt->node = ctxt->node;
2026 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2027#else
2028 list = NULL;
2029#endif /* LIBXML_XPATH_ENABLED */
2030 if (list != NULL) {
2031 switch (list->type) {
2032 case XPATH_UNDEFINED:
2033 xmlGenericError(xmlGenericErrorContext,
2034 "%s: no such node\n", arg);
2035 break;
2036 case XPATH_NODESET:{
2037 int indx;
2038
2039 for (indx = 0;
2040 indx < list->nodesetval->nodeNr;
2041 indx++) {
2042 if (dir)
2043 xmlShellDir(ctxt, NULL,
2044 list->nodesetval->
2045 nodeTab[indx], NULL);
2046 else
2047 xmlShellList(ctxt, NULL,
2048 list->nodesetval->
2049 nodeTab[indx], NULL);
2050 }
2051 break;
2052 }
2053 case XPATH_BOOLEAN:
2054 xmlGenericError(xmlGenericErrorContext,
2055 "%s is a Boolean\n", arg);
2056 break;
2057 case XPATH_NUMBER:
2058 xmlGenericError(xmlGenericErrorContext,
2059 "%s is a number\n", arg);
2060 break;
2061 case XPATH_STRING:
2062 xmlGenericError(xmlGenericErrorContext,
2063 "%s is a string\n", arg);
2064 break;
2065 case XPATH_POINT:
2066 xmlGenericError(xmlGenericErrorContext,
2067 "%s is a point\n", arg);
2068 break;
2069 case XPATH_RANGE:
2070 xmlGenericError(xmlGenericErrorContext,
2071 "%s is a range\n", arg);
2072 break;
2073 case XPATH_LOCATIONSET:
2074 xmlGenericError(xmlGenericErrorContext,
2075 "%s is a range\n", arg);
2076 break;
2077 case XPATH_USERS:
2078 xmlGenericError(xmlGenericErrorContext,
2079 "%s is user-defined\n", arg);
2080 break;
2081 case XPATH_XSLT_TREE:
2082 xmlGenericError(xmlGenericErrorContext,
2083 "%s is an XSLT value tree\n",
2084 arg);
2085 break;
2086 }
2087#ifdef LIBXML_XPATH_ENABLED
2088 xmlXPathFreeObject(list);
Daniel Veillard61d80a22001-04-27 17:13:01 +00002089#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002090 } else {
2091 xmlGenericError(xmlGenericErrorContext,
2092 "%s: no such node\n", arg);
2093 }
2094 ctxt->pctxt->node = NULL;
2095 }
2096 } else if (!strcmp(command, "cd")) {
2097 if (arg[0] == 0) {
2098 ctxt->node = (xmlNodePtr) ctxt->doc;
2099 } else {
2100#ifdef LIBXML_XPATH_ENABLED
2101 ctxt->pctxt->node = ctxt->node;
2102 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2103#else
2104 list = NULL;
2105#endif /* LIBXML_XPATH_ENABLED */
2106 if (list != NULL) {
2107 switch (list->type) {
2108 case XPATH_UNDEFINED:
2109 xmlGenericError(xmlGenericErrorContext,
2110 "%s: no such node\n", arg);
2111 break;
2112 case XPATH_NODESET:
2113 if (list->nodesetval->nodeNr == 1) {
2114 ctxt->node = list->nodesetval->nodeTab[0];
2115 } else
2116 xmlGenericError(xmlGenericErrorContext,
2117 "%s is a %d Node Set\n",
2118 arg,
2119 list->nodesetval->nodeNr);
2120 break;
2121 case XPATH_BOOLEAN:
2122 xmlGenericError(xmlGenericErrorContext,
2123 "%s is a Boolean\n", arg);
2124 break;
2125 case XPATH_NUMBER:
2126 xmlGenericError(xmlGenericErrorContext,
2127 "%s is a number\n", arg);
2128 break;
2129 case XPATH_STRING:
2130 xmlGenericError(xmlGenericErrorContext,
2131 "%s is a string\n", arg);
2132 break;
2133 case XPATH_POINT:
2134 xmlGenericError(xmlGenericErrorContext,
2135 "%s is a point\n", arg);
2136 break;
2137 case XPATH_RANGE:
2138 xmlGenericError(xmlGenericErrorContext,
2139 "%s is a range\n", arg);
2140 break;
2141 case XPATH_LOCATIONSET:
2142 xmlGenericError(xmlGenericErrorContext,
2143 "%s is a range\n", arg);
2144 break;
2145 case XPATH_USERS:
2146 xmlGenericError(xmlGenericErrorContext,
2147 "%s is user-defined\n", arg);
2148 break;
2149 case XPATH_XSLT_TREE:
2150 xmlGenericError(xmlGenericErrorContext,
2151 "%s is an XSLT value tree\n",
2152 arg);
2153 break;
2154 }
2155#ifdef LIBXML_XPATH_ENABLED
2156 xmlXPathFreeObject(list);
2157#endif
2158 } else {
2159 xmlGenericError(xmlGenericErrorContext,
2160 "%s: no such node\n", arg);
2161 }
2162 ctxt->pctxt->node = NULL;
2163 }
2164 } else if (!strcmp(command, "cat")) {
2165 if (arg[0] == 0) {
2166 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
2167 } else {
2168 ctxt->pctxt->node = ctxt->node;
2169#ifdef LIBXML_XPATH_ENABLED
2170 ctxt->pctxt->node = ctxt->node;
2171 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2172#else
2173 list = NULL;
2174#endif /* LIBXML_XPATH_ENABLED */
2175 if (list != NULL) {
2176 switch (list->type) {
2177 case XPATH_UNDEFINED:
2178 xmlGenericError(xmlGenericErrorContext,
2179 "%s: no such node\n", arg);
2180 break;
2181 case XPATH_NODESET:{
2182 int indx;
2183
2184 for (indx = 0;
2185 indx < list->nodesetval->nodeNr;
2186 indx++) {
2187 if (i > 0)
2188 printf(" -------\n");
2189 xmlShellCat(ctxt, NULL,
2190 list->nodesetval->
2191 nodeTab[indx], NULL);
2192 }
2193 break;
2194 }
2195 case XPATH_BOOLEAN:
2196 xmlGenericError(xmlGenericErrorContext,
2197 "%s is a Boolean\n", arg);
2198 break;
2199 case XPATH_NUMBER:
2200 xmlGenericError(xmlGenericErrorContext,
2201 "%s is a number\n", arg);
2202 break;
2203 case XPATH_STRING:
2204 xmlGenericError(xmlGenericErrorContext,
2205 "%s is a string\n", arg);
2206 break;
2207 case XPATH_POINT:
2208 xmlGenericError(xmlGenericErrorContext,
2209 "%s is a point\n", arg);
2210 break;
2211 case XPATH_RANGE:
2212 xmlGenericError(xmlGenericErrorContext,
2213 "%s is a range\n", arg);
2214 break;
2215 case XPATH_LOCATIONSET:
2216 xmlGenericError(xmlGenericErrorContext,
2217 "%s is a range\n", arg);
2218 break;
2219 case XPATH_USERS:
2220 xmlGenericError(xmlGenericErrorContext,
2221 "%s is user-defined\n", arg);
2222 break;
2223 case XPATH_XSLT_TREE:
2224 xmlGenericError(xmlGenericErrorContext,
2225 "%s is an XSLT value tree\n",
2226 arg);
2227 break;
2228 }
2229#ifdef LIBXML_XPATH_ENABLED
2230 xmlXPathFreeObject(list);
2231#endif
2232 } else {
2233 xmlGenericError(xmlGenericErrorContext,
2234 "%s: no such node\n", arg);
2235 }
2236 ctxt->pctxt->node = NULL;
2237 }
2238 } else {
2239 xmlGenericError(xmlGenericErrorContext,
2240 "Unknown command %s\n", command);
2241 }
2242 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00002243 }
2244#ifdef LIBXML_XPATH_ENABLED
2245 xmlXPathFreeContext(ctxt->pctxt);
2246#endif /* LIBXML_XPATH_ENABLED */
2247 if (ctxt->loaded) {
2248 xmlFreeDoc(ctxt->doc);
2249 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002250 if (ctxt->filename != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002251 xmlFree(ctxt->filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002252 xmlFree(ctxt);
2253 if (cmdline != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002254 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00002255}
2256
2257#endif /* LIBXML_DEBUG_ENABLED */