blob: ff717bf1dddd36f361d4a30f622e665f6aacb916 [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:
Daniel Veillardebd38c52001-11-01 08:38:12 +00001165 * @boolval : a bool to turn into text
Daniel Veillard78d12092001-10-11 09:12:24 +00001166 *
1167 * Convenient way to turn bool into text
1168*/
1169const char *
Daniel Veillardebd38c52001-11-01 08:38:12 +00001170xmlBoolToText(int boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001171{
Daniel Veillardebd38c52001-11-01 08:38:12 +00001172 if (boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001173 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) &&
Daniel Veillard66870c72001-11-05 19:27:49 +00001197 ((node->prev->type == XML_ELEMENT_NODE) ||
1198 (node->prev->type == XML_TEXT_NODE)))
1199 result = xmlGetLineNo(node->prev);
Daniel Veillard78d12092001-10-11 09:12:24 +00001200 else if ((node->parent != NULL) &&
Daniel Veillard66870c72001-11-05 19:27:49 +00001201 ((node->parent->type == XML_ELEMENT_NODE) ||
1202 (node->parent->type == XML_TEXT_NODE)))
1203 result = xmlGetLineNo(node->parent);
Daniel Veillard78d12092001-10-11 09:12:24 +00001204
1205 return result;
1206}
1207
Owen Taylor3473f882001-02-23 17:55:21 +00001208/****************************************************************
1209 * *
1210 * The XML shell related functions *
1211 * *
1212 ****************************************************************/
1213
Daniel Veillard78d12092001-10-11 09:12:24 +00001214
1215
Owen Taylor3473f882001-02-23 17:55:21 +00001216/*
1217 * TODO: Improvement/cleanups for the XML shell
1218 * - allow to shell out an editor on a subpart
1219 * - cleanup function registrations (with help) and calling
1220 * - provide registration routines
1221 */
1222
1223/**
Daniel Veillard78d12092001-10-11 09:12:24 +00001224 * xmlShellPrintXpathError:
1225 * @errorType: valid xpath error id
1226 * @arg : the argument that cause xpath to fail
1227 *
1228 * Print the xpath error to libxml default error channel
1229 */
1230void
1231xmlShellPrintXPathError(int errorType, const char *arg)
1232{
1233 const char *default_arg = "Result";
1234
1235 if (!arg)
1236 arg = default_arg;
1237
1238 switch (errorType) {
1239 case XPATH_UNDEFINED:
1240 xmlGenericError(xmlGenericErrorContext,
1241 "%s: no such node\n", arg);
1242 break;
1243
1244 case XPATH_BOOLEAN:
1245 xmlGenericError(xmlGenericErrorContext,
1246 "%s is a Boolean\n", arg);
1247 break;
1248 case XPATH_NUMBER:
1249 xmlGenericError(xmlGenericErrorContext,
1250 "%s is a number\n", arg);
1251 break;
1252 case XPATH_STRING:
1253 xmlGenericError(xmlGenericErrorContext,
1254 "%s is a string\n", arg);
1255 break;
1256 case XPATH_POINT:
1257 xmlGenericError(xmlGenericErrorContext,
1258 "%s is a point\n", arg);
1259 break;
1260 case XPATH_RANGE:
1261 xmlGenericError(xmlGenericErrorContext,
1262 "%s is a range\n", arg);
1263 break;
1264 case XPATH_LOCATIONSET:
1265 xmlGenericError(xmlGenericErrorContext,
1266 "%s is a range\n", arg);
1267 break;
1268 case XPATH_USERS:
1269 xmlGenericError(xmlGenericErrorContext,
1270 "%s is user-defined\n", arg);
1271 break;
1272 case XPATH_XSLT_TREE:
1273 xmlGenericError(xmlGenericErrorContext,
1274 "%s is an XSLT value tree\n", arg);
1275 break;
1276 }
1277 xmlGenericError(xmlGenericErrorContext,
1278 "Try casting the result string function (xpath builtin)\n",
1279 arg);
1280}
1281
1282
1283/**
1284 * xmlShellPrintNode:
1285 * @node : a non-null node to print to stdout
1286 *
1287 * Print node to stdout
1288 */
1289void
1290xmlShellPrintNode(xmlNodePtr node)
1291{
1292 if (!node)
1293 return;
1294
1295 if (node->type == XML_DOCUMENT_NODE)
1296 xmlDocDump(stdout, (xmlDocPtr) node);
1297 else if (node->type == XML_ATTRIBUTE_NODE)
1298 xmlDebugDumpAttrList(stdout, (xmlAttrPtr) node, 0);
1299 else
1300 xmlElemDump(stdout, node->doc, node);
1301
1302 fprintf(stdout, "\n");
1303}
1304
1305
1306/**
1307 * xmlShellPrintXPathResult:
1308 * list : a valid result generated by an xpath evaluation
1309 *
1310 * Prints result to stdout
1311 */
1312void
1313xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1314{
1315 int i = 0;
1316
1317 if (list != NULL) {
1318 switch (list->type) {
1319 case XPATH_NODESET:{
1320 int indx;
1321
1322 if (list->nodesetval) {
1323 for (indx = 0; indx < list->nodesetval->nodeNr;
1324 indx++) {
1325 if (i > 0)
1326 fprintf(stderr, " -------\n");
1327 xmlShellPrintNode(list->nodesetval->
1328 nodeTab[indx]);
1329 }
1330 } else {
1331 xmlGenericError(xmlGenericErrorContext,
1332 "Empty node set\n");
1333 }
1334 break;
1335 }
1336 case XPATH_BOOLEAN:
1337 xmlGenericError(xmlGenericErrorContext,
1338 "Is a Boolean:%s\n",
1339 xmlBoolToText(list->boolval));
1340 break;
1341 case XPATH_NUMBER:
1342 xmlGenericError(xmlGenericErrorContext,
1343 "Is a number:%0g\n", list->floatval);
1344 break;
1345 case XPATH_STRING:
1346 xmlGenericError(xmlGenericErrorContext,
1347 "Is a string:%s\n", list->stringval);
1348 break;
1349
1350 default:
1351 xmlShellPrintXPathError(list->type, NULL);
1352 }
1353 }
1354}
1355
1356/**
Owen Taylor3473f882001-02-23 17:55:21 +00001357 * xmlShellList:
1358 * @ctxt: the shell context
1359 * @arg: unused
1360 * @node: a node
1361 * @node2: unused
1362 *
1363 * Implements the XML shell function "ls"
1364 * Does an Unix like listing of the given node (like a directory)
1365 *
1366 * Returns 0
1367 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001368int
1369xmlShellList(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1370 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1371 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1372{
Owen Taylor3473f882001-02-23 17:55:21 +00001373 xmlNodePtr cur;
1374
1375 if ((node->type == XML_DOCUMENT_NODE) ||
1376 (node->type == XML_HTML_DOCUMENT_NODE)) {
1377 cur = ((xmlDocPtr) node)->children;
1378 } else if (node->children != NULL) {
1379 cur = node->children;
1380 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001381 xmlLsOneNode(stdout, node);
1382 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001383 }
1384 while (cur != NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001385 xmlLsOneNode(stdout, cur);
1386 cur = cur->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001387 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001388 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001389}
1390
1391/**
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001392 * xmlShellBase:
1393 * @ctxt: the shell context
1394 * @arg: unused
1395 * @node: a node
1396 * @node2: unused
1397 *
1398 * Implements the XML shell function "base"
1399 * dumps the current XML base of the node
1400 *
1401 * Returns 0
1402 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001403int
1404xmlShellBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1405 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1406 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1407{
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001408 xmlChar *base;
1409
1410 base = xmlNodeGetBase(node->doc, node);
1411
1412 if (base == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001413 printf(" No base found !!!\n");
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001414 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001415 printf("%s\n", base);
1416 xmlFree(base);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001417 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001418 return (0);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001419}
1420
1421/**
Owen Taylor3473f882001-02-23 17:55:21 +00001422 * xmlShellDir:
1423 * @ctxt: the shell context
1424 * @arg: unused
1425 * @node: a node
1426 * @node2: unused
1427 *
1428 * Implements the XML shell function "dir"
1429 * dumps informations about the node (namespace, attributes, content).
1430 *
1431 * Returns 0
1432 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001433int
1434xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1435 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1436 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1437{
Owen Taylor3473f882001-02-23 17:55:21 +00001438 if ((node->type == XML_DOCUMENT_NODE) ||
1439 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001440 xmlDebugDumpDocumentHead(stdout, (xmlDocPtr) node);
Owen Taylor3473f882001-02-23 17:55:21 +00001441 } else if (node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001442 xmlDebugDumpAttr(stdout, (xmlAttrPtr) node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001443 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001444 xmlDebugDumpOneNode(stdout, node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001445 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001446 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001447}
1448
1449/**
1450 * xmlShellCat:
1451 * @ctxt: the shell context
1452 * @arg: unused
1453 * @node: a node
1454 * @node2: unused
1455 *
1456 * Implements the XML shell function "cat"
1457 * dumps the serialization node content (XML or HTML).
1458 *
1459 * Returns 0
1460 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001461int
1462xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
1463 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
1464{
Owen Taylor3473f882001-02-23 17:55:21 +00001465 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
1466#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001467 if (node->type == XML_HTML_DOCUMENT_NODE)
1468 htmlDocDump(stdout, (htmlDocPtr) node);
1469 else
1470 htmlNodeDumpFile(stdout, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001471#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001472 if (node->type == XML_DOCUMENT_NODE)
1473 xmlDocDump(stdout, (xmlDocPtr) node);
1474 else
1475 xmlElemDump(stdout, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001476#endif /* LIBXML_HTML_ENABLED */
1477 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001478 if (node->type == XML_DOCUMENT_NODE)
1479 xmlDocDump(stdout, (xmlDocPtr) node);
1480 else
1481 xmlElemDump(stdout, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001482 }
1483 printf("\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00001484 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001485}
1486
1487/**
1488 * xmlShellLoad:
1489 * @ctxt: the shell context
1490 * @filename: the file name
1491 * @node: unused
1492 * @node2: unused
1493 *
1494 * Implements the XML shell function "load"
1495 * loads a new document specified by the filename
1496 *
1497 * Returns 0 or -1 if loading failed
1498 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001499int
1500xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
1501 xmlNodePtr node ATTRIBUTE_UNUSED,
1502 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1503{
Owen Taylor3473f882001-02-23 17:55:21 +00001504 xmlDocPtr doc;
1505 int html = 0;
1506
1507 if (ctxt->doc != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001508 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00001509
1510 if (html) {
1511#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001512 doc = htmlParseFile(filename, NULL);
1513#else
1514 printf("HTML support not compiled in\n");
1515 doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001516#endif /* LIBXML_HTML_ENABLED */
1517 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001518 doc = xmlParseFile(filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001519 }
1520 if (doc != NULL) {
1521 if (ctxt->loaded == 1) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001522 xmlFreeDoc(ctxt->doc);
1523 }
1524 ctxt->loaded = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001525#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001526 xmlXPathFreeContext(ctxt->pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001527#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001528 xmlFree(ctxt->filename);
1529 ctxt->doc = doc;
1530 ctxt->node = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001531#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001532 ctxt->pctxt = xmlXPathNewContext(doc);
Owen Taylor3473f882001-02-23 17:55:21 +00001533#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001534 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001535 } else
Daniel Veillard78d12092001-10-11 09:12:24 +00001536 return (-1);
1537 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001538}
1539
1540/**
1541 * xmlShellWrite:
1542 * @ctxt: the shell context
1543 * @filename: the file name
1544 * @node: a node in the tree
1545 * @node2: unused
1546 *
1547 * Implements the XML shell function "write"
1548 * Write the current node to the filename, it saves the serailization
1549 * of the subtree under the @node specified
1550 *
1551 * Returns 0 or -1 in case of error
1552 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001553int
Owen Taylor3473f882001-02-23 17:55:21 +00001554xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Daniel Veillard78d12092001-10-11 09:12:24 +00001555 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1556{
Owen Taylor3473f882001-02-23 17:55:21 +00001557 if (node == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001558 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001559 if ((filename == NULL) || (filename[0] == 0)) {
1560 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001561 "Write command requires a filename argument\n");
1562 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001563 }
1564#ifdef W_OK
1565 if (access((char *) filename, W_OK)) {
1566 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001567 "Cannot write to %s\n", filename);
1568 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001569 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001570#endif
1571 switch (node->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00001572 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00001573 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
1574 xmlGenericError(xmlGenericErrorContext,
1575 "Failed to write to %s\n", filename);
1576 return (-1);
1577 }
1578 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001579 case XML_HTML_DOCUMENT_NODE:
1580#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001581 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
1582 xmlGenericError(xmlGenericErrorContext,
1583 "Failed to write to %s\n", filename);
1584 return (-1);
1585 }
Owen Taylor3473f882001-02-23 17:55:21 +00001586#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001587 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
1588 xmlGenericError(xmlGenericErrorContext,
1589 "Failed to write to %s\n", filename);
1590 return (-1);
1591 }
Owen Taylor3473f882001-02-23 17:55:21 +00001592#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001593 break;
1594 default:{
1595 FILE *f;
Owen Taylor3473f882001-02-23 17:55:21 +00001596
Daniel Veillard78d12092001-10-11 09:12:24 +00001597 f = fopen((char *) filename, "w");
1598 if (f == NULL) {
1599 xmlGenericError(xmlGenericErrorContext,
1600 "Failed to write to %s\n", filename);
1601 return (-1);
1602 }
1603 xmlElemDump(f, ctxt->doc, node);
1604 fclose(f);
1605 }
Owen Taylor3473f882001-02-23 17:55:21 +00001606 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001607 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001608}
1609
1610/**
1611 * xmlShellSave:
1612 * @ctxt: the shell context
1613 * @filename: the file name (optionnal)
1614 * @node: unused
1615 * @node2: unused
1616 *
1617 * Implements the XML shell function "save"
1618 * Write the current document to the filename, or it's original name
1619 *
1620 * Returns 0 or -1 in case of error
1621 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001622int
1623xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
1624 xmlNodePtr node ATTRIBUTE_UNUSED,
1625 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1626{
Owen Taylor3473f882001-02-23 17:55:21 +00001627 if (ctxt->doc == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001628 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001629 if ((filename == NULL) || (filename[0] == 0))
1630 filename = ctxt->filename;
1631#ifdef W_OK
1632 if (access((char *) filename, W_OK)) {
1633 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001634 "Cannot save to %s\n", filename);
1635 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001636 }
1637#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00001638 switch (ctxt->doc->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00001639 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00001640 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
1641 xmlGenericError(xmlGenericErrorContext,
1642 "Failed to save to %s\n", filename);
1643 }
1644 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001645 case XML_HTML_DOCUMENT_NODE:
1646#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001647 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
1648 xmlGenericError(xmlGenericErrorContext,
1649 "Failed to save to %s\n", filename);
1650 }
Owen Taylor3473f882001-02-23 17:55:21 +00001651#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001652 if (xmlSaveFile((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#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001657 break;
1658 default:
1659 xmlGenericError(xmlGenericErrorContext,
1660 "To save to subparts of a document use the 'write' command\n");
1661 return (-1);
1662
Owen Taylor3473f882001-02-23 17:55:21 +00001663 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001664 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001665}
1666
1667/**
1668 * xmlShellValidate:
1669 * @ctxt: the shell context
1670 * @dtd: the DTD URI (optionnal)
1671 * @node: unused
1672 * @node2: unused
1673 *
1674 * Implements the XML shell function "validate"
1675 * Validate the document, if a DTD path is provided, then the validation
1676 * is done against the given DTD.
1677 *
1678 * Returns 0 or -1 in case of error
1679 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001680int
1681xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
1682 xmlNodePtr node ATTRIBUTE_UNUSED,
1683 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1684{
Owen Taylor3473f882001-02-23 17:55:21 +00001685 xmlValidCtxt vctxt;
1686 int res = -1;
1687
1688 vctxt.userData = stderr;
1689 vctxt.error = (xmlValidityErrorFunc) fprintf;
1690 vctxt.warning = (xmlValidityWarningFunc) fprintf;
1691
1692 if ((dtd == NULL) || (dtd[0] == 0)) {
1693 res = xmlValidateDocument(&vctxt, ctxt->doc);
1694 } else {
1695 xmlDtdPtr subset;
1696
Daniel Veillard78d12092001-10-11 09:12:24 +00001697 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
1698 if (subset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001699 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
1700
Daniel Veillard78d12092001-10-11 09:12:24 +00001701 xmlFreeDtd(subset);
1702 }
Owen Taylor3473f882001-02-23 17:55:21 +00001703 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001704 return (res);
Owen Taylor3473f882001-02-23 17:55:21 +00001705}
1706
1707/**
1708 * xmlShellDu:
1709 * @ctxt: the shell context
1710 * @arg: unused
1711 * @tree: a node defining a subtree
1712 * @node2: unused
1713 *
1714 * Implements the XML shell function "du"
1715 * show the structure of the subtree under node @tree
1716 * If @tree is null, the command works on the current node.
1717 *
1718 * Returns 0 or -1 in case of error
1719 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001720int
1721xmlShellDu(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1722 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
1723 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1724{
Owen Taylor3473f882001-02-23 17:55:21 +00001725 xmlNodePtr node;
Daniel Veillard78d12092001-10-11 09:12:24 +00001726 int indent = 0, i;
Owen Taylor3473f882001-02-23 17:55:21 +00001727
Daniel Veillard78d12092001-10-11 09:12:24 +00001728 if (tree == NULL)
1729 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001730 node = tree;
1731 while (node != NULL) {
1732 if ((node->type == XML_DOCUMENT_NODE) ||
1733 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001734 printf("/\n");
1735 } else if (node->type == XML_ELEMENT_NODE) {
1736 for (i = 0; i < indent; i++)
1737 printf(" ");
1738 printf("%s\n", node->name);
1739 } else {
1740 }
Owen Taylor3473f882001-02-23 17:55:21 +00001741
Daniel Veillard78d12092001-10-11 09:12:24 +00001742 /*
1743 * Browse the full subtree, deep first
1744 */
Owen Taylor3473f882001-02-23 17:55:21 +00001745
1746 if ((node->type == XML_DOCUMENT_NODE) ||
1747 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001748 node = ((xmlDocPtr) node)->children;
1749 } else if ((node->children != NULL)
1750 && (node->type != XML_ENTITY_REF_NODE)) {
1751 /* deep first */
1752 node = node->children;
1753 indent++;
1754 } else if ((node != tree) && (node->next != NULL)) {
1755 /* then siblings */
1756 node = node->next;
1757 } else if (node != tree) {
1758 /* go up to parents->next if needed */
1759 while (node != tree) {
1760 if (node->parent != NULL) {
1761 node = node->parent;
1762 indent--;
1763 }
1764 if ((node != tree) && (node->next != NULL)) {
1765 node = node->next;
1766 break;
1767 }
1768 if (node->parent == NULL) {
1769 node = NULL;
1770 break;
1771 }
1772 if (node == tree) {
1773 node = NULL;
1774 break;
1775 }
1776 }
1777 /* exit condition */
1778 if (node == tree)
1779 node = NULL;
1780 } else
1781 node = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001782 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001783 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001784}
1785
1786/**
1787 * xmlShellPwd:
1788 * @ctxt: the shell context
1789 * @buffer: the output buffer
1790 * @tree: a node
1791 * @node2: unused
1792 *
1793 * Implements the XML shell function "pwd"
1794 * Show the full path from the root to the node, if needed building
1795 * thumblers when similar elements exists at a given ancestor level.
1796 * The output is compatible with XPath commands.
1797 *
1798 * Returns 0 or -1 in case of error
1799 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001800int
1801xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
1802 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
1803{
Owen Taylor3473f882001-02-23 17:55:21 +00001804 xmlNodePtr cur, tmp, next;
1805 char buf[500];
1806 char sep;
1807 const char *name;
Daniel Veillard78d12092001-10-11 09:12:24 +00001808 char nametemp[100];
Owen Taylor3473f882001-02-23 17:55:21 +00001809 int occur = 0;
1810
1811 buffer[0] = 0;
Daniel Veillard78d12092001-10-11 09:12:24 +00001812 if (node == NULL)
1813 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001814 cur = node;
1815 do {
Daniel Veillard78d12092001-10-11 09:12:24 +00001816 name = "";
1817 sep = '?';
1818 occur = 0;
1819 if ((cur->type == XML_DOCUMENT_NODE) ||
1820 (cur->type == XML_HTML_DOCUMENT_NODE)) {
1821 sep = '/';
1822 next = NULL;
1823 } else if (cur->type == XML_ELEMENT_NODE) {
1824 sep = '/';
1825 name = (const char *) cur->name;
1826 if (cur->ns) {
1827 snprintf(nametemp, 99, "%s:%s", cur->ns->prefix,
1828 cur->name);
1829 name = nametemp;
1830 }
1831 next = cur->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00001832
Daniel Veillard78d12092001-10-11 09:12:24 +00001833 /*
1834 * Thumbler index computation
1835 */
1836 tmp = cur->prev;
Owen Taylor3473f882001-02-23 17:55:21 +00001837 while (tmp != NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001838 if (xmlStrEqual(cur->name, tmp->name))
1839 occur++;
1840 tmp = tmp->prev;
1841 }
1842 if (occur == 0) {
1843 tmp = cur->next;
1844 while (tmp != NULL) {
1845 if (xmlStrEqual(cur->name, tmp->name))
1846 occur++;
1847 tmp = tmp->next;
1848 }
1849 if (occur != 0)
1850 occur = 1;
1851 } else
1852 occur++;
1853 } else if (cur->type == XML_ATTRIBUTE_NODE) {
1854 sep = '@';
1855 name = (const char *) (((xmlAttrPtr) cur)->name);
1856 next = ((xmlAttrPtr) cur)->parent;
1857 } else {
1858 next = cur->parent;
1859 }
1860 if (occur == 0)
1861 snprintf(buf, sizeof(buf), "%c%s%s", sep, name, buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00001862 else
Daniel Veillard78d12092001-10-11 09:12:24 +00001863 snprintf(buf, sizeof(buf), "%c%s[%d]%s",
1864 sep, name, occur, buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00001865 buf[sizeof(buf) - 1] = 0;
1866 /*
1867 * This test prevents buffer overflow, because this routine
1868 * is only called by xmlShell, in which the second argument is
1869 * 500 chars long.
1870 * It is a dirty hack before a cleaner solution is found.
1871 * Documentation should mention that the second argument must
1872 * be at least 500 chars long, and could be stripped if too long.
1873 */
1874 if (strlen(buffer) + strlen(buf) > 499)
Daniel Veillard78d12092001-10-11 09:12:24 +00001875 break;
1876 strcpy(buffer, buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001877 cur = next;
1878 } while (cur != NULL);
Daniel Veillard78d12092001-10-11 09:12:24 +00001879 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001880}
1881
1882/**
1883 * xmlShell
1884 * @doc: the initial document
1885 * @filename: the output buffer
1886 * @input: the line reading function
1887 * @output: the output FILE*
1888 *
1889 * Implements the XML shell
1890 * This allow to load, validate, view, modify and save a document
1891 * using a environment similar to a UNIX commandline.
1892 */
1893void
1894xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Daniel Veillard78d12092001-10-11 09:12:24 +00001895 FILE * output)
1896{
Owen Taylor3473f882001-02-23 17:55:21 +00001897 char prompt[500] = "/ > ";
1898 char *cmdline = NULL, *cur;
1899 int nbargs;
1900 char command[100];
1901 char arg[400];
1902 int i;
1903 xmlShellCtxtPtr ctxt;
1904 xmlXPathObjectPtr list;
1905
1906 if (doc == NULL)
1907 return;
1908 if (filename == NULL)
1909 return;
1910 if (input == NULL)
1911 return;
1912 if (output == NULL)
1913 return;
1914 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Daniel Veillard78d12092001-10-11 09:12:24 +00001915 if (ctxt == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001916 return;
1917 ctxt->loaded = 0;
1918 ctxt->doc = doc;
1919 ctxt->input = input;
1920 ctxt->output = output;
1921 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Daniel Veillard78d12092001-10-11 09:12:24 +00001922 ctxt->node = (xmlNodePtr) ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001923
1924#ifdef LIBXML_XPATH_ENABLED
1925 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
1926 if (ctxt->pctxt == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001927 xmlFree(ctxt);
1928 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001929 }
1930#endif /* LIBXML_XPATH_ENABLED */
1931 while (1) {
1932 if (ctxt->node == (xmlNodePtr) ctxt->doc)
Daniel Veillard78d12092001-10-11 09:12:24 +00001933 sprintf(prompt, "%s > ", "/");
1934 else if (ctxt->node->name)
1935 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001936 else
Daniel Veillard78d12092001-10-11 09:12:24 +00001937 sprintf(prompt, "? > ");
Owen Taylor3473f882001-02-23 17:55:21 +00001938 prompt[sizeof(prompt) - 1] = 0;
1939
Daniel Veillard78d12092001-10-11 09:12:24 +00001940 /*
1941 * Get a new command line
1942 */
Owen Taylor3473f882001-02-23 17:55:21 +00001943 cmdline = ctxt->input(prompt);
Daniel Veillard78d12092001-10-11 09:12:24 +00001944 if (cmdline == NULL)
1945 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001946
Daniel Veillard78d12092001-10-11 09:12:24 +00001947 /*
1948 * Parse the command itself
1949 */
1950 cur = cmdline;
1951 nbargs = 0;
1952 while ((*cur == ' ') || (*cur == '\t'))
1953 cur++;
1954 i = 0;
1955 while ((*cur != ' ') && (*cur != '\t') &&
1956 (*cur != '\n') && (*cur != '\r')) {
1957 if (*cur == 0)
1958 break;
1959 command[i++] = *cur++;
1960 }
1961 command[i] = 0;
1962 if (i == 0)
1963 continue;
1964 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00001965
Daniel Veillard78d12092001-10-11 09:12:24 +00001966 /*
1967 * Parse the argument
1968 */
1969 while ((*cur == ' ') || (*cur == '\t'))
1970 cur++;
1971 i = 0;
1972 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
1973 if (*cur == 0)
1974 break;
1975 arg[i++] = *cur++;
1976 }
1977 arg[i] = 0;
1978 if (i != 0)
1979 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00001980
Daniel Veillard78d12092001-10-11 09:12:24 +00001981 /*
1982 * start interpreting the command
1983 */
Owen Taylor3473f882001-02-23 17:55:21 +00001984 if (!strcmp(command, "exit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00001985 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001986 if (!strcmp(command, "quit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00001987 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001988 if (!strcmp(command, "bye"))
Daniel Veillard78d12092001-10-11 09:12:24 +00001989 break;
Daniel Veillard5004f422001-11-08 13:53:05 +00001990 if (!strcmp(command, "help")) {
1991 printf("\tbase display XML base of the node\n");
1992 printf("\tbye leave shell\n");
1993 printf("\tcat [node] display node or current node\n");
1994 printf("\tcd [path] change directory to path or to root\n");
1995 printf("\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
1996 printf("\tdu [path] show the structure of the subtree under path or the current node\n");
1997 printf("\texit leave shell\n");
1998 printf("\thelp display this help\n");
1999 printf("\tfree display memory usage\n");
2000 printf("\tload [name] load a new document with name\n");
2001 printf("\tls [path] list contents of path or the current directory\n");
2002 printf("\tpwd display current working directory\n");
2003 printf("\tquit leave shell\n");
2004 printf("\tsave [name] save this document to name or the original name\n");
2005 printf("\tvalidate check the document for errors\n");
2006 printf("\twrite [name] write the current node to the filename\n");
2007 } else if (!strcmp(command, "validate")) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002008 xmlShellValidate(ctxt, arg, NULL, NULL);
2009 } else if (!strcmp(command, "load")) {
2010 xmlShellLoad(ctxt, arg, NULL, NULL);
2011 } else if (!strcmp(command, "save")) {
2012 xmlShellSave(ctxt, arg, NULL, NULL);
2013 } else if (!strcmp(command, "write")) {
2014 xmlShellWrite(ctxt, arg, NULL, NULL);
2015 } else if (!strcmp(command, "free")) {
2016 if (arg[0] == 0) {
2017 xmlMemShow(stdout, 0);
2018 } else {
2019 int len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002020
Daniel Veillard78d12092001-10-11 09:12:24 +00002021 sscanf(arg, "%d", &len);
2022 xmlMemShow(stdout, len);
2023 }
2024 } else if (!strcmp(command, "pwd")) {
2025 char dir[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002026
Daniel Veillard78d12092001-10-11 09:12:24 +00002027 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
2028 printf("%s\n", dir);
2029 } else if (!strcmp(command, "du")) {
2030 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2031 } else if (!strcmp(command, "base")) {
2032 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
2033 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
2034 int dir = (!strcmp(command, "dir"));
2035
2036 if (arg[0] == 0) {
2037 if (dir)
2038 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
2039 else
2040 xmlShellList(ctxt, NULL, ctxt->node, NULL);
2041 } else {
2042 ctxt->pctxt->node = ctxt->node;
Daniel Veillard61d80a22001-04-27 17:13:01 +00002043#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002044 ctxt->pctxt->node = ctxt->node;
2045 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2046#else
2047 list = NULL;
2048#endif /* LIBXML_XPATH_ENABLED */
2049 if (list != NULL) {
2050 switch (list->type) {
2051 case XPATH_UNDEFINED:
2052 xmlGenericError(xmlGenericErrorContext,
2053 "%s: no such node\n", arg);
2054 break;
2055 case XPATH_NODESET:{
2056 int indx;
2057
Daniel Veillarda6825e82001-11-07 13:33:59 +00002058 if (list->nodesetval == NULL)
2059 break;
2060
Daniel Veillard78d12092001-10-11 09:12:24 +00002061 for (indx = 0;
2062 indx < list->nodesetval->nodeNr;
2063 indx++) {
2064 if (dir)
2065 xmlShellDir(ctxt, NULL,
2066 list->nodesetval->
2067 nodeTab[indx], NULL);
2068 else
2069 xmlShellList(ctxt, NULL,
2070 list->nodesetval->
2071 nodeTab[indx], NULL);
2072 }
2073 break;
2074 }
2075 case XPATH_BOOLEAN:
2076 xmlGenericError(xmlGenericErrorContext,
2077 "%s is a Boolean\n", arg);
2078 break;
2079 case XPATH_NUMBER:
2080 xmlGenericError(xmlGenericErrorContext,
2081 "%s is a number\n", arg);
2082 break;
2083 case XPATH_STRING:
2084 xmlGenericError(xmlGenericErrorContext,
2085 "%s is a string\n", arg);
2086 break;
2087 case XPATH_POINT:
2088 xmlGenericError(xmlGenericErrorContext,
2089 "%s is a point\n", arg);
2090 break;
2091 case XPATH_RANGE:
2092 xmlGenericError(xmlGenericErrorContext,
2093 "%s is a range\n", arg);
2094 break;
2095 case XPATH_LOCATIONSET:
2096 xmlGenericError(xmlGenericErrorContext,
2097 "%s is a range\n", arg);
2098 break;
2099 case XPATH_USERS:
2100 xmlGenericError(xmlGenericErrorContext,
2101 "%s is user-defined\n", arg);
2102 break;
2103 case XPATH_XSLT_TREE:
2104 xmlGenericError(xmlGenericErrorContext,
2105 "%s is an XSLT value tree\n",
2106 arg);
2107 break;
2108 }
2109#ifdef LIBXML_XPATH_ENABLED
2110 xmlXPathFreeObject(list);
Daniel Veillard61d80a22001-04-27 17:13:01 +00002111#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002112 } else {
2113 xmlGenericError(xmlGenericErrorContext,
2114 "%s: no such node\n", arg);
2115 }
2116 ctxt->pctxt->node = NULL;
2117 }
2118 } else if (!strcmp(command, "cd")) {
2119 if (arg[0] == 0) {
2120 ctxt->node = (xmlNodePtr) ctxt->doc;
2121 } else {
2122#ifdef LIBXML_XPATH_ENABLED
2123 ctxt->pctxt->node = ctxt->node;
2124 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2125#else
2126 list = NULL;
2127#endif /* LIBXML_XPATH_ENABLED */
2128 if (list != NULL) {
2129 switch (list->type) {
2130 case XPATH_UNDEFINED:
2131 xmlGenericError(xmlGenericErrorContext,
2132 "%s: no such node\n", arg);
2133 break;
2134 case XPATH_NODESET:
Daniel Veillarda6825e82001-11-07 13:33:59 +00002135 if (list->nodesetval != NULL) {
2136 if (list->nodesetval->nodeNr == 1) {
2137 ctxt->node = list->nodesetval->nodeTab[0];
2138 } else
2139 xmlGenericError(xmlGenericErrorContext,
2140 "%s is a %d Node Set\n",
2141 arg,
2142 list->nodesetval->nodeNr);
Daniel Veillard78d12092001-10-11 09:12:24 +00002143 } else
2144 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda6825e82001-11-07 13:33:59 +00002145 "%s is an empty Node Set\n",
2146 arg);
Daniel Veillard78d12092001-10-11 09:12:24 +00002147 break;
2148 case XPATH_BOOLEAN:
2149 xmlGenericError(xmlGenericErrorContext,
2150 "%s is a Boolean\n", arg);
2151 break;
2152 case XPATH_NUMBER:
2153 xmlGenericError(xmlGenericErrorContext,
2154 "%s is a number\n", arg);
2155 break;
2156 case XPATH_STRING:
2157 xmlGenericError(xmlGenericErrorContext,
2158 "%s is a string\n", arg);
2159 break;
2160 case XPATH_POINT:
2161 xmlGenericError(xmlGenericErrorContext,
2162 "%s is a point\n", arg);
2163 break;
2164 case XPATH_RANGE:
2165 xmlGenericError(xmlGenericErrorContext,
2166 "%s is a range\n", arg);
2167 break;
2168 case XPATH_LOCATIONSET:
2169 xmlGenericError(xmlGenericErrorContext,
2170 "%s is a range\n", arg);
2171 break;
2172 case XPATH_USERS:
2173 xmlGenericError(xmlGenericErrorContext,
2174 "%s is user-defined\n", arg);
2175 break;
2176 case XPATH_XSLT_TREE:
2177 xmlGenericError(xmlGenericErrorContext,
2178 "%s is an XSLT value tree\n",
2179 arg);
2180 break;
2181 }
2182#ifdef LIBXML_XPATH_ENABLED
2183 xmlXPathFreeObject(list);
2184#endif
2185 } else {
2186 xmlGenericError(xmlGenericErrorContext,
2187 "%s: no such node\n", arg);
2188 }
2189 ctxt->pctxt->node = NULL;
2190 }
2191 } else if (!strcmp(command, "cat")) {
2192 if (arg[0] == 0) {
2193 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
2194 } else {
2195 ctxt->pctxt->node = ctxt->node;
2196#ifdef LIBXML_XPATH_ENABLED
2197 ctxt->pctxt->node = ctxt->node;
2198 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2199#else
2200 list = NULL;
2201#endif /* LIBXML_XPATH_ENABLED */
2202 if (list != NULL) {
2203 switch (list->type) {
2204 case XPATH_UNDEFINED:
2205 xmlGenericError(xmlGenericErrorContext,
2206 "%s: no such node\n", arg);
2207 break;
2208 case XPATH_NODESET:{
2209 int indx;
2210
Daniel Veillarda6825e82001-11-07 13:33:59 +00002211 if (list->nodesetval == NULL)
2212 break;
2213
Daniel Veillard78d12092001-10-11 09:12:24 +00002214 for (indx = 0;
2215 indx < list->nodesetval->nodeNr;
2216 indx++) {
2217 if (i > 0)
2218 printf(" -------\n");
2219 xmlShellCat(ctxt, NULL,
2220 list->nodesetval->
2221 nodeTab[indx], NULL);
2222 }
2223 break;
2224 }
2225 case XPATH_BOOLEAN:
2226 xmlGenericError(xmlGenericErrorContext,
2227 "%s is a Boolean\n", arg);
2228 break;
2229 case XPATH_NUMBER:
2230 xmlGenericError(xmlGenericErrorContext,
2231 "%s is a number\n", arg);
2232 break;
2233 case XPATH_STRING:
2234 xmlGenericError(xmlGenericErrorContext,
2235 "%s is a string\n", arg);
2236 break;
2237 case XPATH_POINT:
2238 xmlGenericError(xmlGenericErrorContext,
2239 "%s is a point\n", arg);
2240 break;
2241 case XPATH_RANGE:
2242 xmlGenericError(xmlGenericErrorContext,
2243 "%s is a range\n", arg);
2244 break;
2245 case XPATH_LOCATIONSET:
2246 xmlGenericError(xmlGenericErrorContext,
2247 "%s is a range\n", arg);
2248 break;
2249 case XPATH_USERS:
2250 xmlGenericError(xmlGenericErrorContext,
2251 "%s is user-defined\n", arg);
2252 break;
2253 case XPATH_XSLT_TREE:
2254 xmlGenericError(xmlGenericErrorContext,
2255 "%s is an XSLT value tree\n",
2256 arg);
2257 break;
2258 }
2259#ifdef LIBXML_XPATH_ENABLED
2260 xmlXPathFreeObject(list);
2261#endif
2262 } else {
2263 xmlGenericError(xmlGenericErrorContext,
2264 "%s: no such node\n", arg);
2265 }
2266 ctxt->pctxt->node = NULL;
2267 }
2268 } else {
2269 xmlGenericError(xmlGenericErrorContext,
2270 "Unknown command %s\n", command);
2271 }
2272 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00002273 }
2274#ifdef LIBXML_XPATH_ENABLED
2275 xmlXPathFreeContext(ctxt->pctxt);
2276#endif /* LIBXML_XPATH_ENABLED */
2277 if (ctxt->loaded) {
2278 xmlFreeDoc(ctxt->doc);
2279 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002280 if (ctxt->filename != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002281 xmlFree(ctxt->filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002282 xmlFree(ctxt);
2283 if (cmdline != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002284 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00002285}
2286
2287#endif /* LIBXML_DEBUG_ENABLED */