blob: fabcd12878014c28c500562f13b11b80a2825343 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * debugXML.c : This is a set of routines used for debugging the tree
3 * produced by the XML parser.
4 *
5 * See Copyright for the status of this software.
6 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00007 * Daniel Veillard <daniel@veillard.com>
Owen Taylor3473f882001-02-23 17:55:21 +00008 */
9
Daniel Veillard34ce8be2002-03-18 19:37:11 +000010#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000011#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000012#ifdef LIBXML_DEBUG_ENABLED
13
Owen Taylor3473f882001-02-23 17:55:21 +000014#include <string.h>
15#ifdef HAVE_STDLIB_H
16#include <stdlib.h>
17#endif
18#ifdef HAVE_STRING_H
19#include <string.h>
20#endif
21#include <libxml/xmlmemory.h>
22#include <libxml/tree.h>
23#include <libxml/parser.h>
Daniel Veillard567e1b42001-08-01 15:53:47 +000024#include <libxml/parserInternals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000025#include <libxml/valid.h>
26#include <libxml/debugXML.h>
27#include <libxml/HTMLtree.h>
28#include <libxml/HTMLparser.h>
29#include <libxml/xmlerror.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000030#include <libxml/globals.h>
Daniel Veillard0ba59232002-02-10 13:20:39 +000031#include <libxml/xpathInternals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000032
Daniel Veillard5e2dace2001-07-18 19:30:27 +000033/**
34 * xmlDebugDumpString:
35 * @output: the FILE * for the output
36 * @str: the string
37 *
38 * Dumps informations about the string, shorten it if necessary
39 */
40void
41xmlDebugDumpString(FILE * output, const xmlChar * str)
42{
Owen Taylor3473f882001-02-23 17:55:21 +000043 int i;
Daniel Veillard5e2dace2001-07-18 19:30:27 +000044
Daniel Veillard7db38712002-02-07 16:39:11 +000045 if (output == NULL)
46 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +000047 if (str == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +000048 fprintf(output, "(NULL)");
49 return;
Owen Taylor3473f882001-02-23 17:55:21 +000050 }
Daniel Veillard5e2dace2001-07-18 19:30:27 +000051 for (i = 0; i < 40; i++)
52 if (str[i] == 0)
53 return;
54 else if (IS_BLANK(str[i]))
55 fputc(' ', output);
56 else if (str[i] >= 0x80)
57 fprintf(output, "#%X", str[i]);
58 else
59 fputc(str[i], output);
Owen Taylor3473f882001-02-23 17:55:21 +000060 fprintf(output, "...");
61}
62
Daniel Veillard56a4cb82001-03-24 17:00:36 +000063static void
64xmlDebugDumpDtdNode(FILE *output, xmlDtdPtr dtd, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +000065 int i;
66 char shift[100];
67
68 for (i = 0;((i < depth) && (i < 25));i++)
69 shift[2 * i] = shift[2 * i + 1] = ' ';
70 shift[2 * i] = shift[2 * i + 1] = 0;
71
72 fprintf(output, shift);
73
Daniel Veillard5e926fa2002-01-22 21:44:25 +000074 if (dtd == NULL) {
75 fprintf(output, "DTD node is NULL\n");
76 return;
77 }
78
Owen Taylor3473f882001-02-23 17:55:21 +000079 if (dtd->type != XML_DTD_NODE) {
80 fprintf(output, "PBM: not a DTD\n");
81 return;
82 }
83 if (dtd->name != NULL)
84 fprintf(output, "DTD(%s)", dtd->name);
85 else
86 fprintf(output, "DTD");
87 if (dtd->ExternalID != NULL)
88 fprintf(output, ", PUBLIC %s", dtd->ExternalID);
89 if (dtd->SystemID != NULL)
90 fprintf(output, ", SYSTEM %s", dtd->SystemID);
91 fprintf(output, "\n");
92 /*
93 * Do a bit of checking
94 */
95 if (dtd->parent == NULL)
Daniel Veillardcbaf3992001-12-31 16:16:02 +000096 fprintf(output, "PBM: DTD has no parent\n");
Owen Taylor3473f882001-02-23 17:55:21 +000097 if (dtd->doc == NULL)
Daniel Veillardcbaf3992001-12-31 16:16:02 +000098 fprintf(output, "PBM: DTD has no doc\n");
Owen Taylor3473f882001-02-23 17:55:21 +000099 if ((dtd->parent != NULL) && (dtd->doc != dtd->parent->doc))
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000100 fprintf(output, "PBM: DTD doc differs from parent's one\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000101 if (dtd->prev == NULL) {
102 if ((dtd->parent != NULL) && (dtd->parent->children != (xmlNodePtr)dtd))
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000103 fprintf(output, "PBM: DTD has no prev and not first of list\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000104 } else {
105 if (dtd->prev->next != (xmlNodePtr) dtd)
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000106 fprintf(output, "PBM: DTD prev->next : back link wrong\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000107 }
108 if (dtd->next == NULL) {
109 if ((dtd->parent != NULL) && (dtd->parent->last != (xmlNodePtr) dtd))
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000110 fprintf(output, "PBM: DTD has no next and not last of list\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000111 } else {
112 if (dtd->next->prev != (xmlNodePtr) dtd)
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000113 fprintf(output, "PBM: DTD next->prev : forward link wrong\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000114 }
115}
116
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000117static void
118xmlDebugDumpAttrDecl(FILE *output, xmlAttributePtr attr, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000119 int i;
120 char shift[100];
121
122 for (i = 0;((i < depth) && (i < 25));i++)
123 shift[2 * i] = shift[2 * i + 1] = ' ';
124 shift[2 * i] = shift[2 * i + 1] = 0;
125
126 fprintf(output, shift);
127
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000128 if (attr == NULL) {
129 fprintf(output, "Attribute declaration is NULL\n");
130 return;
131 }
Owen Taylor3473f882001-02-23 17:55:21 +0000132 if (attr->type != XML_ATTRIBUTE_DECL) {
133 fprintf(output, "PBM: not a Attr\n");
134 return;
135 }
136 if (attr->name != NULL)
137 fprintf(output, "ATTRDECL(%s)", attr->name);
138 else
139 fprintf(output, "PBM ATTRDECL noname!!!");
140 if (attr->elem != NULL)
141 fprintf(output, " for %s", attr->elem);
142 else
143 fprintf(output, " PBM noelem!!!");
144 switch (attr->atype) {
145 case XML_ATTRIBUTE_CDATA:
146 fprintf(output, " CDATA");
147 break;
148 case XML_ATTRIBUTE_ID:
149 fprintf(output, " ID");
150 break;
151 case XML_ATTRIBUTE_IDREF:
152 fprintf(output, " IDREF");
153 break;
154 case XML_ATTRIBUTE_IDREFS:
155 fprintf(output, " IDREFS");
156 break;
157 case XML_ATTRIBUTE_ENTITY:
158 fprintf(output, " ENTITY");
159 break;
160 case XML_ATTRIBUTE_ENTITIES:
161 fprintf(output, " ENTITIES");
162 break;
163 case XML_ATTRIBUTE_NMTOKEN:
164 fprintf(output, " NMTOKEN");
165 break;
166 case XML_ATTRIBUTE_NMTOKENS:
167 fprintf(output, " NMTOKENS");
168 break;
169 case XML_ATTRIBUTE_ENUMERATION:
170 fprintf(output, " ENUMERATION");
171 break;
172 case XML_ATTRIBUTE_NOTATION:
173 fprintf(output, " NOTATION ");
174 break;
175 }
176 if (attr->tree != NULL) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000177 int indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000178 xmlEnumerationPtr cur = attr->tree;
179
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000180 for (indx = 0;indx < 5; indx++) {
181 if (indx != 0)
Owen Taylor3473f882001-02-23 17:55:21 +0000182 fprintf(output, "|%s", cur->name);
183 else
184 fprintf(output, " (%s", cur->name);
185 cur = cur->next;
186 if (cur == NULL) break;
187 }
188 if (cur == NULL)
189 fprintf(output, ")");
190 else
191 fprintf(output, "...)");
192 }
193 switch (attr->def) {
194 case XML_ATTRIBUTE_NONE:
195 break;
196 case XML_ATTRIBUTE_REQUIRED:
197 fprintf(output, " REQUIRED");
198 break;
199 case XML_ATTRIBUTE_IMPLIED:
200 fprintf(output, " IMPLIED");
201 break;
202 case XML_ATTRIBUTE_FIXED:
203 fprintf(output, " FIXED");
204 break;
205 }
206 if (attr->defaultValue != NULL) {
207 fprintf(output, "\"");
208 xmlDebugDumpString(output, attr->defaultValue);
209 fprintf(output, "\"");
210 }
Daniel Veillardcd337f02001-11-22 18:20:37 +0000211 fprintf(output, "\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000212
213 /*
214 * Do a bit of checking
215 */
216 if (attr->parent == NULL)
217 fprintf(output, "PBM: Attr has no parent\n");
218 if (attr->doc == NULL)
219 fprintf(output, "PBM: Attr has no doc\n");
220 if ((attr->parent != NULL) && (attr->doc != attr->parent->doc))
221 fprintf(output, "PBM: Attr doc differs from parent's one\n");
222 if (attr->prev == NULL) {
223 if ((attr->parent != NULL) && (attr->parent->children != (xmlNodePtr)attr))
224 fprintf(output, "PBM: Attr has no prev and not first of list\n");
225 } else {
226 if (attr->prev->next != (xmlNodePtr) attr)
227 fprintf(output, "PBM: Attr prev->next : back link wrong\n");
228 }
229 if (attr->next == NULL) {
230 if ((attr->parent != NULL) && (attr->parent->last != (xmlNodePtr) attr))
231 fprintf(output, "PBM: Attr has no next and not last of list\n");
232 } else {
233 if (attr->next->prev != (xmlNodePtr) attr)
234 fprintf(output, "PBM: Attr next->prev : forward link wrong\n");
235 }
236}
237
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000238static void
239xmlDebugDumpElemDecl(FILE *output, xmlElementPtr elem, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000240 int i;
241 char shift[100];
242
243 for (i = 0;((i < depth) && (i < 25));i++)
244 shift[2 * i] = shift[2 * i + 1] = ' ';
245 shift[2 * i] = shift[2 * i + 1] = 0;
246
247 fprintf(output, shift);
248
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000249 if (elem == NULL) {
250 fprintf(output, "Element declaration is NULL\n");
251 return;
252 }
Owen Taylor3473f882001-02-23 17:55:21 +0000253 if (elem->type != XML_ELEMENT_DECL) {
254 fprintf(output, "PBM: not a Elem\n");
255 return;
256 }
257 if (elem->name != NULL) {
258 fprintf(output, "ELEMDECL(");
259 xmlDebugDumpString(output, elem->name);
260 fprintf(output, ")");
261 } else
262 fprintf(output, "PBM ELEMDECL noname!!!");
263 switch (elem->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +0000264 case XML_ELEMENT_TYPE_UNDEFINED:
265 fprintf(output, ", UNDEFINED");
266 break;
Owen Taylor3473f882001-02-23 17:55:21 +0000267 case XML_ELEMENT_TYPE_EMPTY:
268 fprintf(output, ", EMPTY");
269 break;
270 case XML_ELEMENT_TYPE_ANY:
271 fprintf(output, ", ANY");
272 break;
273 case XML_ELEMENT_TYPE_MIXED:
274 fprintf(output, ", MIXED ");
275 break;
276 case XML_ELEMENT_TYPE_ELEMENT:
277 fprintf(output, ", MIXED ");
278 break;
279 }
Daniel Veillard7db37732001-07-12 01:20:08 +0000280 if ((elem->type != XML_ELEMENT_NODE) &&
281 (elem->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000282 char buf[5001];
283
284 buf[0] = 0;
Daniel Veillardd3d06722001-08-15 12:06:36 +0000285 xmlSnprintfElementContent(buf, 5000, elem->content, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000286 buf[5000] = 0;
287 fprintf(output, "%s", buf);
288 }
Daniel Veillardcd337f02001-11-22 18:20:37 +0000289 fprintf(output, "\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000290
291 /*
292 * Do a bit of checking
293 */
294 if (elem->parent == NULL)
295 fprintf(output, "PBM: Elem has no parent\n");
296 if (elem->doc == NULL)
297 fprintf(output, "PBM: Elem has no doc\n");
298 if ((elem->parent != NULL) && (elem->doc != elem->parent->doc))
299 fprintf(output, "PBM: Elem doc differs from parent's one\n");
300 if (elem->prev == NULL) {
301 if ((elem->parent != NULL) && (elem->parent->children != (xmlNodePtr)elem))
302 fprintf(output, "PBM: Elem has no prev and not first of list\n");
303 } else {
304 if (elem->prev->next != (xmlNodePtr) elem)
305 fprintf(output, "PBM: Elem prev->next : back link wrong\n");
306 }
307 if (elem->next == NULL) {
308 if ((elem->parent != NULL) && (elem->parent->last != (xmlNodePtr) elem))
309 fprintf(output, "PBM: Elem has no next and not last of list\n");
310 } else {
311 if (elem->next->prev != (xmlNodePtr) elem)
312 fprintf(output, "PBM: Elem next->prev : forward link wrong\n");
313 }
314}
315
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000316static void
317xmlDebugDumpEntityDecl(FILE *output, xmlEntityPtr ent, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000318 int i;
319 char shift[100];
320
321 for (i = 0;((i < depth) && (i < 25));i++)
322 shift[2 * i] = shift[2 * i + 1] = ' ';
323 shift[2 * i] = shift[2 * i + 1] = 0;
324
325 fprintf(output, shift);
326
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000327 if (ent == NULL) {
328 fprintf(output, "Entity declaration is NULL\n");
329 return;
330 }
Owen Taylor3473f882001-02-23 17:55:21 +0000331 if (ent->type != XML_ENTITY_DECL) {
332 fprintf(output, "PBM: not a Entity decl\n");
333 return;
334 }
335 if (ent->name != NULL) {
336 fprintf(output, "ENTITYDECL(");
337 xmlDebugDumpString(output, ent->name);
338 fprintf(output, ")");
339 } else
340 fprintf(output, "PBM ENTITYDECL noname!!!");
341 switch (ent->etype) {
342 case XML_INTERNAL_GENERAL_ENTITY:
343 fprintf(output, ", internal\n");
344 break;
345 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
346 fprintf(output, ", external parsed\n");
347 break;
348 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
349 fprintf(output, ", unparsed\n");
350 break;
351 case XML_INTERNAL_PARAMETER_ENTITY:
352 fprintf(output, ", parameter\n");
353 break;
354 case XML_EXTERNAL_PARAMETER_ENTITY:
355 fprintf(output, ", external parameter\n");
356 break;
357 case XML_INTERNAL_PREDEFINED_ENTITY:
358 fprintf(output, ", predefined\n");
359 break;
360 }
361 if (ent->ExternalID) {
362 fprintf(output, shift);
363 fprintf(output, " ExternalID=%s\n", ent->ExternalID);
364 }
365 if (ent->SystemID) {
366 fprintf(output, shift);
367 fprintf(output, " SystemID=%s\n", ent->SystemID);
368 }
369 if (ent->URI != NULL) {
370 fprintf(output, shift);
371 fprintf(output, " URI=%s\n", ent->URI);
372 }
373 if (ent->content) {
374 fprintf(output, shift);
375 fprintf(output, " content=");
376 xmlDebugDumpString(output, ent->content);
377 fprintf(output, "\n");
378 }
379
380 /*
381 * Do a bit of checking
382 */
383 if (ent->parent == NULL)
384 fprintf(output, "PBM: Ent has no parent\n");
385 if (ent->doc == NULL)
386 fprintf(output, "PBM: Ent has no doc\n");
387 if ((ent->parent != NULL) && (ent->doc != ent->parent->doc))
388 fprintf(output, "PBM: Ent doc differs from parent's one\n");
389 if (ent->prev == NULL) {
390 if ((ent->parent != NULL) && (ent->parent->children != (xmlNodePtr)ent))
391 fprintf(output, "PBM: Ent has no prev and not first of list\n");
392 } else {
393 if (ent->prev->next != (xmlNodePtr) ent)
394 fprintf(output, "PBM: Ent prev->next : back link wrong\n");
395 }
396 if (ent->next == NULL) {
397 if ((ent->parent != NULL) && (ent->parent->last != (xmlNodePtr) ent))
398 fprintf(output, "PBM: Ent has no next and not last of list\n");
399 } else {
400 if (ent->next->prev != (xmlNodePtr) ent)
401 fprintf(output, "PBM: Ent next->prev : forward link wrong\n");
402 }
403}
404
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000405static void
406xmlDebugDumpNamespace(FILE *output, xmlNsPtr ns, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000407 int i;
408 char shift[100];
409
410 for (i = 0;((i < depth) && (i < 25));i++)
411 shift[2 * i] = shift[2 * i + 1] = ' ';
412 shift[2 * i] = shift[2 * i + 1] = 0;
413
414 fprintf(output, shift);
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000415
416 if (ns == NULL) {
417 fprintf(output, "namespace node is NULL\n");
418 return;
419 }
Owen Taylor3473f882001-02-23 17:55:21 +0000420 if (ns->type != XML_NAMESPACE_DECL) {
421 fprintf(output, "invalid namespace node %d\n", ns->type);
422 return;
423 }
424 if (ns->href == NULL) {
425 if (ns->prefix != NULL)
426 fprintf(output, "incomplete namespace %s href=NULL\n", ns->prefix);
427 else
428 fprintf(output, "incomplete default namespace href=NULL\n");
429 } else {
430 if (ns->prefix != NULL)
431 fprintf(output, "namespace %s href=", ns->prefix);
432 else
433 fprintf(output, "default namespace href=");
434
435 xmlDebugDumpString(output, ns->href);
436 fprintf(output, "\n");
437 }
438}
439
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000440static void
441xmlDebugDumpNamespaceList(FILE *output, xmlNsPtr ns, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000442 while (ns != NULL) {
443 xmlDebugDumpNamespace(output, ns, depth);
444 ns = ns->next;
445 }
446}
447
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000448static void
449xmlDebugDumpEntity(FILE *output, xmlEntityPtr ent, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000450 int i;
451 char shift[100];
452
453 for (i = 0;((i < depth) && (i < 25));i++)
454 shift[2 * i] = shift[2 * i + 1] = ' ';
455 shift[2 * i] = shift[2 * i + 1] = 0;
456
457 fprintf(output, shift);
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000458
459 if (ent == NULL) {
460 fprintf(output, "Entity is NULL\n");
461 return;
462 }
Owen Taylor3473f882001-02-23 17:55:21 +0000463 switch (ent->etype) {
464 case XML_INTERNAL_GENERAL_ENTITY:
465 fprintf(output, "INTERNAL_GENERAL_ENTITY ");
466 break;
467 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
468 fprintf(output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
469 break;
470 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
471 fprintf(output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
472 break;
473 case XML_INTERNAL_PARAMETER_ENTITY:
474 fprintf(output, "INTERNAL_PARAMETER_ENTITY ");
475 break;
476 case XML_EXTERNAL_PARAMETER_ENTITY:
477 fprintf(output, "EXTERNAL_PARAMETER_ENTITY ");
478 break;
479 default:
480 fprintf(output, "ENTITY_%d ! ", ent->etype);
481 }
482 fprintf(output, "%s\n", ent->name);
483 if (ent->ExternalID) {
484 fprintf(output, shift);
485 fprintf(output, "ExternalID=%s\n", ent->ExternalID);
486 }
487 if (ent->SystemID) {
488 fprintf(output, shift);
489 fprintf(output, "SystemID=%s\n", ent->SystemID);
490 }
491 if (ent->URI) {
492 fprintf(output, shift);
493 fprintf(output, "URI=%s\n", ent->URI);
494 }
495 if (ent->content) {
496 fprintf(output, shift);
497 fprintf(output, "content=");
498 xmlDebugDumpString(output, ent->content);
499 fprintf(output, "\n");
500 }
501}
502
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000503/**
504 * xmlDebugDumpAttr:
505 * @output: the FILE * for the output
506 * @attr: the attribute
507 * @depth: the indentation level.
508 *
509 * Dumps debug information for the attribute
510 */
511void
512xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000513 int i;
514 char shift[100];
515
516 for (i = 0;((i < depth) && (i < 25));i++)
517 shift[2 * i] = shift[2 * i + 1] = ' ';
518 shift[2 * i] = shift[2 * i + 1] = 0;
519
520 fprintf(output, shift);
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000521
522 if (attr == NULL) {
523 fprintf(output, "Attr is NULL");
524 return;
525 }
Owen Taylor3473f882001-02-23 17:55:21 +0000526 fprintf(output, "ATTRIBUTE ");
527 xmlDebugDumpString(output, attr->name);
528 fprintf(output, "\n");
529 if (attr->children != NULL)
530 xmlDebugDumpNodeList(output, attr->children, depth + 1);
531
532 /*
533 * Do a bit of checking
534 */
535 if (attr->parent == NULL)
536 fprintf(output, "PBM: Attr has no parent\n");
537 if (attr->doc == NULL)
538 fprintf(output, "PBM: Attr has no doc\n");
539 if ((attr->parent != NULL) && (attr->doc != attr->parent->doc))
540 fprintf(output, "PBM: Attr doc differs from parent's one\n");
541 if (attr->prev == NULL) {
542 if ((attr->parent != NULL) && (attr->parent->properties != attr))
543 fprintf(output, "PBM: Attr has no prev and not first of list\n");
544 } else {
545 if (attr->prev->next != attr)
546 fprintf(output, "PBM: Attr prev->next : back link wrong\n");
547 }
548 if (attr->next != NULL) {
549 if (attr->next->prev != attr)
550 fprintf(output, "PBM: Attr next->prev : forward link wrong\n");
551 }
552}
553
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000554/**
555 * xmlDebugDumpAttrList:
556 * @output: the FILE * for the output
557 * @attr: the attribute list
558 * @depth: the indentation level.
559 *
560 * Dumps debug information for the attribute list
561 */
562void
563xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
564{
Daniel Veillard7db38712002-02-07 16:39:11 +0000565 if (output == NULL)
566 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +0000567 while (attr != NULL) {
568 xmlDebugDumpAttr(output, attr, depth);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000569 attr = attr->next;
Owen Taylor3473f882001-02-23 17:55:21 +0000570 }
571}
572
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000573/**
574 * xmlDebugDumpOneNode:
575 * @output: the FILE * for the output
576 * @node: the node
577 * @depth: the indentation level.
578 *
579 * Dumps debug information for the element node, it is not recursive
580 */
581void
582xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
583{
Owen Taylor3473f882001-02-23 17:55:21 +0000584 int i;
585 char shift[100];
586
Daniel Veillard7db38712002-02-07 16:39:11 +0000587 if (output == NULL)
588 output = stdout;
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000589 for (i = 0; ((i < depth) && (i < 25)); i++)
Owen Taylor3473f882001-02-23 17:55:21 +0000590 shift[2 * i] = shift[2 * i + 1] = ' ';
591 shift[2 * i] = shift[2 * i + 1] = 0;
592
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000593 if (node == NULL) {
594 fprintf(output, shift);
595 fprintf(output, "node is NULL\n");
596 return;
597 }
Owen Taylor3473f882001-02-23 17:55:21 +0000598 switch (node->type) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000599 case XML_ELEMENT_NODE:
600 fprintf(output, shift);
601 fprintf(output, "ELEMENT ");
602 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
603 xmlDebugDumpString(output, node->ns->prefix);
604 fprintf(output, ":");
605 }
606 xmlDebugDumpString(output, node->name);
607 fprintf(output, "\n");
608 break;
609 case XML_ATTRIBUTE_NODE:
610 fprintf(output, shift);
611 fprintf(output, "Error, ATTRIBUTE found here\n");
612 break;
613 case XML_TEXT_NODE:
614 fprintf(output, shift);
Daniel Veillardb44025c2001-10-11 22:55:55 +0000615 if (node->name == (const xmlChar *) xmlStringTextNoenc)
Daniel Veillard567e1b42001-08-01 15:53:47 +0000616 fprintf(output, "TEXT no enc\n");
617 else
618 fprintf(output, "TEXT\n");
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000619 break;
620 case XML_CDATA_SECTION_NODE:
621 fprintf(output, shift);
622 fprintf(output, "CDATA_SECTION\n");
623 break;
624 case XML_ENTITY_REF_NODE:
625 fprintf(output, shift);
626 fprintf(output, "ENTITY_REF(%s)\n", node->name);
627 break;
628 case XML_ENTITY_NODE:
629 fprintf(output, shift);
630 fprintf(output, "ENTITY\n");
631 break;
632 case XML_PI_NODE:
633 fprintf(output, shift);
634 fprintf(output, "PI %s\n", node->name);
635 break;
636 case XML_COMMENT_NODE:
637 fprintf(output, shift);
638 fprintf(output, "COMMENT\n");
639 break;
640 case XML_DOCUMENT_NODE:
641 case XML_HTML_DOCUMENT_NODE:
642 fprintf(output, shift);
643 fprintf(output, "Error, DOCUMENT found here\n");
644 break;
645 case XML_DOCUMENT_TYPE_NODE:
646 fprintf(output, shift);
647 fprintf(output, "DOCUMENT_TYPE\n");
648 break;
649 case XML_DOCUMENT_FRAG_NODE:
650 fprintf(output, shift);
651 fprintf(output, "DOCUMENT_FRAG\n");
652 break;
653 case XML_NOTATION_NODE:
654 fprintf(output, shift);
655 fprintf(output, "NOTATION\n");
656 break;
657 case XML_DTD_NODE:
658 xmlDebugDumpDtdNode(output, (xmlDtdPtr) node, depth);
659 return;
660 case XML_ELEMENT_DECL:
661 xmlDebugDumpElemDecl(output, (xmlElementPtr) node, depth);
662 return;
663 case XML_ATTRIBUTE_DECL:
664 xmlDebugDumpAttrDecl(output, (xmlAttributePtr) node, depth);
665 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000666 case XML_ENTITY_DECL:
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000667 xmlDebugDumpEntityDecl(output, (xmlEntityPtr) node, depth);
668 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000669 case XML_NAMESPACE_DECL:
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000670 xmlDebugDumpNamespace(output, (xmlNsPtr) node, depth);
671 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000672 case XML_XINCLUDE_START:
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000673 fprintf(output, shift);
674 fprintf(output, "INCLUDE START\n");
675 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000676 case XML_XINCLUDE_END:
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000677 fprintf(output, shift);
678 fprintf(output, "INCLUDE END\n");
679 return;
680 default:
681 fprintf(output, shift);
682 fprintf(output, "NODE_%d !!!\n", node->type);
683 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000684 }
685 if (node->doc == NULL) {
686 fprintf(output, shift);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000687 fprintf(output, "doc == NULL !!!\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000688 }
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000689 if (node->nsDef != NULL)
Owen Taylor3473f882001-02-23 17:55:21 +0000690 xmlDebugDumpNamespaceList(output, node->nsDef, depth + 1);
691 if (node->properties != NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000692 xmlDebugDumpAttrList(output, node->properties, depth + 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000693 if (node->type != XML_ENTITY_REF_NODE) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000694 if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
695 shift[2 * i] = shift[2 * i + 1] = ' ';
696 shift[2 * i + 2] = shift[2 * i + 3] = 0;
697 fprintf(output, shift);
698 fprintf(output, "content=");
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000699 xmlDebugDumpString(output, node->content);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000700 fprintf(output, "\n");
701 }
Owen Taylor3473f882001-02-23 17:55:21 +0000702 } else {
703 xmlEntityPtr ent;
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000704
705 ent = xmlGetDocEntity(node->doc, node->name);
706 if (ent != NULL)
707 xmlDebugDumpEntity(output, ent, depth + 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000708 }
709 /*
710 * Do a bit of checking
711 */
712 if (node->parent == NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000713 fprintf(output, "PBM: Node has no parent\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000714 if (node->doc == NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000715 fprintf(output, "PBM: Node has no doc\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000716 if ((node->parent != NULL) && (node->doc != node->parent->doc))
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000717 fprintf(output, "PBM: Node doc differs from parent's one\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000718 if (node->prev == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000719 if ((node->parent != NULL) && (node->parent->children != node))
720 fprintf(output,
721 "PBM: Node has no prev and not first of list\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000722 } else {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000723 if (node->prev->next != node)
724 fprintf(output, "PBM: Node prev->next : back link wrong\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000725 }
726 if (node->next == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000727 if ((node->parent != NULL) && (node->parent->last != node))
728 fprintf(output,
729 "PBM: Node has no next and not last of list\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000730 } else {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000731 if (node->next->prev != node)
732 fprintf(output, "PBM: Node next->prev : forward link wrong\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000733 }
734}
735
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000736/**
737 * xmlDebugDumpNode:
738 * @output: the FILE * for the output
739 * @node: the node
740 * @depth: the indentation level.
741 *
742 * Dumps debug information for the element node, it is recursive
743 */
744void
745xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
746{
Daniel Veillard7db38712002-02-07 16:39:11 +0000747 if (output == NULL)
748 output = stdout;
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000749 if (node == NULL) {
750 int i;
751 char shift[100];
752
753 for (i = 0; ((i < depth) && (i < 25)); i++)
754 shift[2 * i] = shift[2 * i + 1] = ' ';
755 shift[2 * i] = shift[2 * i + 1] = 0;
756
757 fprintf(output, shift);
758 fprintf(output, "node is NULL\n");
759 return;
760 }
Owen Taylor3473f882001-02-23 17:55:21 +0000761 xmlDebugDumpOneNode(output, node, depth);
762 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE))
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000763 xmlDebugDumpNodeList(output, node->children, depth + 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000764}
765
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000766/**
767 * xmlDebugDumpNodeList:
768 * @output: the FILE * for the output
769 * @node: the node list
770 * @depth: the indentation level.
771 *
772 * Dumps debug information for the list of element node, it is recursive
773 */
774void
775xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
776{
Daniel Veillard7db38712002-02-07 16:39:11 +0000777 if (output == NULL)
778 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +0000779 while (node != NULL) {
780 xmlDebugDumpNode(output, node, depth);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000781 node = node->next;
Owen Taylor3473f882001-02-23 17:55:21 +0000782 }
783}
784
785
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000786/**
787 * xmlDebugDumpDocumentHead:
788 * @output: the FILE * for the output
789 * @doc: the document
790 *
791 * Dumps debug information cncerning the document, not recursive
792 */
793void
794xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
795{
796 if (output == NULL)
797 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +0000798 if (doc == NULL) {
799 fprintf(output, "DOCUMENT == NULL !\n");
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000800 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000801 }
802
803 switch (doc->type) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000804 case XML_ELEMENT_NODE:
805 fprintf(output, "Error, ELEMENT found here ");
806 break;
807 case XML_ATTRIBUTE_NODE:
808 fprintf(output, "Error, ATTRIBUTE found here\n");
809 break;
810 case XML_TEXT_NODE:
811 fprintf(output, "Error, TEXT\n");
812 break;
813 case XML_CDATA_SECTION_NODE:
814 fprintf(output, "Error, CDATA_SECTION\n");
815 break;
816 case XML_ENTITY_REF_NODE:
817 fprintf(output, "Error, ENTITY_REF\n");
818 break;
819 case XML_ENTITY_NODE:
820 fprintf(output, "Error, ENTITY\n");
821 break;
822 case XML_PI_NODE:
823 fprintf(output, "Error, PI\n");
824 break;
825 case XML_COMMENT_NODE:
826 fprintf(output, "Error, COMMENT\n");
827 break;
828 case XML_DOCUMENT_NODE:
829 fprintf(output, "DOCUMENT\n");
830 break;
831 case XML_HTML_DOCUMENT_NODE:
832 fprintf(output, "HTML DOCUMENT\n");
833 break;
834 case XML_DOCUMENT_TYPE_NODE:
835 fprintf(output, "Error, DOCUMENT_TYPE\n");
836 break;
837 case XML_DOCUMENT_FRAG_NODE:
838 fprintf(output, "Error, DOCUMENT_FRAG\n");
839 break;
840 case XML_NOTATION_NODE:
841 fprintf(output, "Error, NOTATION\n");
842 break;
843 default:
844 fprintf(output, "NODE_%d\n", doc->type);
Owen Taylor3473f882001-02-23 17:55:21 +0000845 }
846 if (doc->name != NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000847 fprintf(output, "name=");
Owen Taylor3473f882001-02-23 17:55:21 +0000848 xmlDebugDumpString(output, BAD_CAST doc->name);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000849 fprintf(output, "\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000850 }
851 if (doc->version != NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000852 fprintf(output, "version=");
Owen Taylor3473f882001-02-23 17:55:21 +0000853 xmlDebugDumpString(output, doc->version);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000854 fprintf(output, "\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000855 }
856 if (doc->encoding != NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000857 fprintf(output, "encoding=");
Owen Taylor3473f882001-02-23 17:55:21 +0000858 xmlDebugDumpString(output, doc->encoding);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000859 fprintf(output, "\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000860 }
861 if (doc->URL != NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000862 fprintf(output, "URL=");
Owen Taylor3473f882001-02-23 17:55:21 +0000863 xmlDebugDumpString(output, doc->URL);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000864 fprintf(output, "\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000865 }
866 if (doc->standalone)
867 fprintf(output, "standalone=true\n");
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000868 if (doc->oldNs != NULL)
Owen Taylor3473f882001-02-23 17:55:21 +0000869 xmlDebugDumpNamespaceList(output, doc->oldNs, 0);
870}
871
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000872/**
873 * xmlDebugDumpDocument:
874 * @output: the FILE * for the output
875 * @doc: the document
876 *
877 * Dumps debug information for the document, it's recursive
878 */
879void
880xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
881{
882 if (output == NULL)
883 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +0000884 if (doc == NULL) {
885 fprintf(output, "DOCUMENT == NULL !\n");
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000886 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000887 }
888 xmlDebugDumpDocumentHead(output, doc);
889 if (((doc->type == XML_DOCUMENT_NODE) ||
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000890 (doc->type == XML_HTML_DOCUMENT_NODE)) && (doc->children != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +0000891 xmlDebugDumpNodeList(output, doc->children, 1);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000892}
Owen Taylor3473f882001-02-23 17:55:21 +0000893
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000894/**
895 * xmlDebugDumpDTD:
896 * @output: the FILE * for the output
897 * @dtd: the DTD
898 *
899 * Dumps debug information for the DTD
900 */
901void
902xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
903{
Daniel Veillard7db38712002-02-07 16:39:11 +0000904 if (output == NULL)
905 output = stdout;
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000906 if (dtd == NULL) {
907 fprintf(output, "DTD is NULL\n");
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000908 return;
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000909 }
Owen Taylor3473f882001-02-23 17:55:21 +0000910 if (dtd->type != XML_DTD_NODE) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000911 fprintf(output, "PBM: not a DTD\n");
912 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000913 }
914 if (dtd->name != NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000915 fprintf(output, "DTD(%s)", dtd->name);
Owen Taylor3473f882001-02-23 17:55:21 +0000916 else
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000917 fprintf(output, "DTD");
Owen Taylor3473f882001-02-23 17:55:21 +0000918 if (dtd->ExternalID != NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000919 fprintf(output, ", PUBLIC %s", dtd->ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +0000920 if (dtd->SystemID != NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000921 fprintf(output, ", SYSTEM %s", dtd->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +0000922 fprintf(output, "\n");
923 /*
924 * Do a bit of checking
925 */
926 if ((dtd->parent != NULL) && (dtd->doc != dtd->parent->doc))
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000927 fprintf(output, "PBM: DTD doc differs from parent's one\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000928 if (dtd->prev == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000929 if ((dtd->parent != NULL)
930 && (dtd->parent->children != (xmlNodePtr) dtd))
931 fprintf(output,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000932 "PBM: DTD has no prev and not first of list\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000933 } else {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000934 if (dtd->prev->next != (xmlNodePtr) dtd)
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000935 fprintf(output, "PBM: DTD prev->next : back link wrong\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000936 }
937 if (dtd->next == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000938 if ((dtd->parent != NULL)
939 && (dtd->parent->last != (xmlNodePtr) dtd))
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000940 fprintf(output, "PBM: DTD has no next and not last of list\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000941 } else {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000942 if (dtd->next->prev != (xmlNodePtr) dtd)
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000943 fprintf(output, "PBM: DTD next->prev : forward link wrong\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000944 }
945 if (dtd->children == NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000946 fprintf(output, " DTD is empty\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000947 else
948 xmlDebugDumpNodeList(output, dtd->children, 1);
949}
950
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000951static void
952xmlDebugDumpEntityCallback(xmlEntityPtr cur, FILE *output) {
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000953 if (cur == NULL) {
954 fprintf(output, "Entity is NULL");
955 return;
956 }
Owen Taylor3473f882001-02-23 17:55:21 +0000957 fprintf(output, "%s : ", cur->name);
958 switch (cur->etype) {
959 case XML_INTERNAL_GENERAL_ENTITY:
960 fprintf(output, "INTERNAL GENERAL, ");
961 break;
962 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
963 fprintf(output, "EXTERNAL PARSED, ");
964 break;
965 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
966 fprintf(output, "EXTERNAL UNPARSED, ");
967 break;
968 case XML_INTERNAL_PARAMETER_ENTITY:
969 fprintf(output, "INTERNAL PARAMETER, ");
970 break;
971 case XML_EXTERNAL_PARAMETER_ENTITY:
972 fprintf(output, "EXTERNAL PARAMETER, ");
973 break;
974 default:
975 fprintf(output, "UNKNOWN TYPE %d",
976 cur->etype);
977 }
978 if (cur->ExternalID != NULL)
979 fprintf(output, "ID \"%s\"", cur->ExternalID);
980 if (cur->SystemID != NULL)
981 fprintf(output, "SYSTEM \"%s\"", cur->SystemID);
982 if (cur->orig != NULL)
983 fprintf(output, "\n orig \"%s\"", cur->orig);
Daniel Veillard7db37732001-07-12 01:20:08 +0000984 if ((cur->type != XML_ELEMENT_NODE) &&
985 (cur->content != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +0000986 fprintf(output, "\n content \"%s\"", cur->content);
987 fprintf(output, "\n");
988}
989
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000990/**
991 * xmlDebugDumpEntities:
992 * @output: the FILE * for the output
993 * @doc: the document
994 *
995 * Dumps debug information for all the entities in use by the document
996 */
997void
998xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
999{
1000 if (output == NULL)
1001 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00001002 if (doc == NULL) {
1003 fprintf(output, "DOCUMENT == NULL !\n");
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001004 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001005 }
1006
1007 switch (doc->type) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001008 case XML_ELEMENT_NODE:
1009 fprintf(output, "Error, ELEMENT found here ");
1010 break;
1011 case XML_ATTRIBUTE_NODE:
1012 fprintf(output, "Error, ATTRIBUTE found here\n");
1013 break;
1014 case XML_TEXT_NODE:
1015 fprintf(output, "Error, TEXT\n");
1016 break;
1017 case XML_CDATA_SECTION_NODE:
1018 fprintf(output, "Error, CDATA_SECTION\n");
1019 break;
1020 case XML_ENTITY_REF_NODE:
1021 fprintf(output, "Error, ENTITY_REF\n");
1022 break;
1023 case XML_ENTITY_NODE:
1024 fprintf(output, "Error, ENTITY\n");
1025 break;
1026 case XML_PI_NODE:
1027 fprintf(output, "Error, PI\n");
1028 break;
1029 case XML_COMMENT_NODE:
1030 fprintf(output, "Error, COMMENT\n");
1031 break;
1032 case XML_DOCUMENT_NODE:
1033 fprintf(output, "DOCUMENT\n");
1034 break;
1035 case XML_HTML_DOCUMENT_NODE:
1036 fprintf(output, "HTML DOCUMENT\n");
1037 break;
1038 case XML_DOCUMENT_TYPE_NODE:
1039 fprintf(output, "Error, DOCUMENT_TYPE\n");
1040 break;
1041 case XML_DOCUMENT_FRAG_NODE:
1042 fprintf(output, "Error, DOCUMENT_FRAG\n");
1043 break;
1044 case XML_NOTATION_NODE:
1045 fprintf(output, "Error, NOTATION\n");
1046 break;
1047 default:
1048 fprintf(output, "NODE_%d\n", doc->type);
Owen Taylor3473f882001-02-23 17:55:21 +00001049 }
1050 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001051 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1052 doc->intSubset->entities;
1053
1054 fprintf(output, "Entities in internal subset\n");
1055 xmlHashScan(table, (xmlHashScanner) xmlDebugDumpEntityCallback,
1056 output);
Owen Taylor3473f882001-02-23 17:55:21 +00001057 } else
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001058 fprintf(output, "No entities in internal subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001059 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001060 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1061 doc->extSubset->entities;
1062
1063 fprintf(output, "Entities in external subset\n");
1064 xmlHashScan(table, (xmlHashScanner) xmlDebugDumpEntityCallback,
1065 output);
Owen Taylor3473f882001-02-23 17:55:21 +00001066 } else
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001067 fprintf(output, "No entities in external subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001068}
1069
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001070/**
1071 * xmlLsCountNode:
1072 * @node: the node to count
1073 *
1074 * Count the children of @node.
1075 *
1076 * Returns the number of children of @node.
1077 */
1078int
1079xmlLsCountNode(xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00001080 int ret = 0;
1081 xmlNodePtr list = NULL;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001082
1083 if (node == NULL)
1084 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001085
1086 switch (node->type) {
1087 case XML_ELEMENT_NODE:
1088 list = node->children;
1089 break;
1090 case XML_DOCUMENT_NODE:
1091 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00001092#ifdef LIBXML_DOCB_ENABLED
1093 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001094#endif
1095 list = ((xmlDocPtr) node)->children;
1096 break;
1097 case XML_ATTRIBUTE_NODE:
1098 list = ((xmlAttrPtr) node)->children;
1099 break;
1100 case XML_TEXT_NODE:
1101 case XML_CDATA_SECTION_NODE:
1102 case XML_PI_NODE:
1103 case XML_COMMENT_NODE:
1104 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001105 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001106 }
1107 break;
1108 case XML_ENTITY_REF_NODE:
1109 case XML_DOCUMENT_TYPE_NODE:
1110 case XML_ENTITY_NODE:
1111 case XML_DOCUMENT_FRAG_NODE:
1112 case XML_NOTATION_NODE:
1113 case XML_DTD_NODE:
1114 case XML_ELEMENT_DECL:
1115 case XML_ATTRIBUTE_DECL:
1116 case XML_ENTITY_DECL:
1117 case XML_NAMESPACE_DECL:
1118 case XML_XINCLUDE_START:
1119 case XML_XINCLUDE_END:
1120 ret = 1;
1121 break;
1122 }
1123 for (;list != NULL;ret++)
1124 list = list->next;
1125 return(ret);
1126}
1127
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001128/**
1129 * xmlLsOneNode:
1130 * @output: the FILE * for the output
1131 * @node: the node to dump
1132 *
1133 * Dump to @output the type and name of @node.
1134 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001135void
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001136xmlLsOneNode(FILE *output, xmlNodePtr node) {
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001137 if (node == NULL) {
1138 fprintf(output, "NULL\n");
1139 return;
1140 }
Owen Taylor3473f882001-02-23 17:55:21 +00001141 switch (node->type) {
1142 case XML_ELEMENT_NODE:
1143 fprintf(output, "-");
1144 break;
1145 case XML_ATTRIBUTE_NODE:
1146 fprintf(output, "a");
1147 break;
1148 case XML_TEXT_NODE:
1149 fprintf(output, "t");
1150 break;
1151 case XML_CDATA_SECTION_NODE:
Daniel Veillard75be0132002-03-13 10:03:35 +00001152 fprintf(output, "C");
Owen Taylor3473f882001-02-23 17:55:21 +00001153 break;
1154 case XML_ENTITY_REF_NODE:
1155 fprintf(output, "e");
1156 break;
1157 case XML_ENTITY_NODE:
1158 fprintf(output, "E");
1159 break;
1160 case XML_PI_NODE:
1161 fprintf(output, "p");
1162 break;
1163 case XML_COMMENT_NODE:
1164 fprintf(output, "c");
1165 break;
1166 case XML_DOCUMENT_NODE:
1167 fprintf(output, "d");
1168 break;
1169 case XML_HTML_DOCUMENT_NODE:
1170 fprintf(output, "h");
1171 break;
1172 case XML_DOCUMENT_TYPE_NODE:
1173 fprintf(output, "T");
1174 break;
1175 case XML_DOCUMENT_FRAG_NODE:
1176 fprintf(output, "F");
1177 break;
1178 case XML_NOTATION_NODE:
1179 fprintf(output, "N");
1180 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001181 case XML_NAMESPACE_DECL:
1182 fprintf(output, "n");
1183 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001184 default:
1185 fprintf(output, "?");
1186 }
Daniel Veillarde6a55192002-01-14 17:11:53 +00001187 if (node->type != XML_NAMESPACE_DECL) {
1188 if (node->properties != NULL)
1189 fprintf(output, "a");
1190 else
1191 fprintf(output, "-");
1192 if (node->nsDef != NULL)
1193 fprintf(output, "n");
1194 else
1195 fprintf(output, "-");
1196 }
Owen Taylor3473f882001-02-23 17:55:21 +00001197
1198 fprintf(output, " %8d ", xmlLsCountNode(node));
1199
1200 switch (node->type) {
1201 case XML_ELEMENT_NODE:
1202 if (node->name != NULL)
1203 fprintf(output, "%s", node->name);
1204 break;
1205 case XML_ATTRIBUTE_NODE:
1206 if (node->name != NULL)
1207 fprintf(output, "%s", node->name);
1208 break;
1209 case XML_TEXT_NODE:
1210 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001211 xmlDebugDumpString(output, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001212 }
1213 break;
1214 case XML_CDATA_SECTION_NODE:
1215 break;
1216 case XML_ENTITY_REF_NODE:
1217 if (node->name != NULL)
1218 fprintf(output, "%s", node->name);
1219 break;
1220 case XML_ENTITY_NODE:
1221 if (node->name != NULL)
1222 fprintf(output, "%s", node->name);
1223 break;
1224 case XML_PI_NODE:
1225 if (node->name != NULL)
1226 fprintf(output, "%s", node->name);
1227 break;
1228 case XML_COMMENT_NODE:
1229 break;
1230 case XML_DOCUMENT_NODE:
1231 break;
1232 case XML_HTML_DOCUMENT_NODE:
1233 break;
1234 case XML_DOCUMENT_TYPE_NODE:
1235 break;
1236 case XML_DOCUMENT_FRAG_NODE:
1237 break;
1238 case XML_NOTATION_NODE:
1239 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001240 case XML_NAMESPACE_DECL: {
1241 xmlNsPtr ns = (xmlNsPtr) node;
1242
1243 if (ns->prefix == NULL)
1244 fprintf(output, "default -> %s", ns->href);
1245 else
1246 fprintf(output, "%s -> %s", ns->prefix, ns->href);
1247 break;
1248 }
Owen Taylor3473f882001-02-23 17:55:21 +00001249 default:
1250 if (node->name != NULL)
1251 fprintf(output, "%s", node->name);
1252 }
1253 fprintf(output, "\n");
1254}
1255
Daniel Veillard78d12092001-10-11 09:12:24 +00001256/**
1257 * xmlBoolToText:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001258 * @boolval: a bool to turn into text
Daniel Veillard78d12092001-10-11 09:12:24 +00001259 *
1260 * Convenient way to turn bool into text
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001261 *
1262 * Returns a pointer to either "True" or "False"
1263 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001264const char *
Daniel Veillardebd38c52001-11-01 08:38:12 +00001265xmlBoolToText(int boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001266{
Daniel Veillardebd38c52001-11-01 08:38:12 +00001267 if (boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001268 return("True");
1269 else
1270 return("False");
1271}
1272
Owen Taylor3473f882001-02-23 17:55:21 +00001273/****************************************************************
1274 * *
1275 * The XML shell related functions *
1276 * *
1277 ****************************************************************/
1278
Daniel Veillard78d12092001-10-11 09:12:24 +00001279
1280
Owen Taylor3473f882001-02-23 17:55:21 +00001281/*
1282 * TODO: Improvement/cleanups for the XML shell
1283 * - allow to shell out an editor on a subpart
1284 * - cleanup function registrations (with help) and calling
1285 * - provide registration routines
1286 */
1287
1288/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001289 * xmlShellPrintXPathError:
Daniel Veillard78d12092001-10-11 09:12:24 +00001290 * @errorType: valid xpath error id
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001291 * @arg: the argument that cause xpath to fail
Daniel Veillard78d12092001-10-11 09:12:24 +00001292 *
1293 * Print the xpath error to libxml default error channel
1294 */
1295void
1296xmlShellPrintXPathError(int errorType, const char *arg)
1297{
1298 const char *default_arg = "Result";
1299
1300 if (!arg)
1301 arg = default_arg;
1302
1303 switch (errorType) {
1304 case XPATH_UNDEFINED:
1305 xmlGenericError(xmlGenericErrorContext,
1306 "%s: no such node\n", arg);
1307 break;
1308
1309 case XPATH_BOOLEAN:
1310 xmlGenericError(xmlGenericErrorContext,
1311 "%s is a Boolean\n", arg);
1312 break;
1313 case XPATH_NUMBER:
1314 xmlGenericError(xmlGenericErrorContext,
1315 "%s is a number\n", arg);
1316 break;
1317 case XPATH_STRING:
1318 xmlGenericError(xmlGenericErrorContext,
1319 "%s is a string\n", arg);
1320 break;
1321 case XPATH_POINT:
1322 xmlGenericError(xmlGenericErrorContext,
1323 "%s is a point\n", arg);
1324 break;
1325 case XPATH_RANGE:
1326 xmlGenericError(xmlGenericErrorContext,
1327 "%s is a range\n", arg);
1328 break;
1329 case XPATH_LOCATIONSET:
1330 xmlGenericError(xmlGenericErrorContext,
1331 "%s is a range\n", arg);
1332 break;
1333 case XPATH_USERS:
1334 xmlGenericError(xmlGenericErrorContext,
1335 "%s is user-defined\n", arg);
1336 break;
1337 case XPATH_XSLT_TREE:
1338 xmlGenericError(xmlGenericErrorContext,
1339 "%s is an XSLT value tree\n", arg);
1340 break;
1341 }
1342 xmlGenericError(xmlGenericErrorContext,
1343 "Try casting the result string function (xpath builtin)\n",
1344 arg);
1345}
1346
1347
1348/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001349 * xmlShellPrintNodeCtxt:
1350 * @ctxt : a non-null shell context
1351 * @node : a non-null node to print to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001352 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001353 * Print node to the output FILE
1354 */
1355static void
1356xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1357{
1358 if (!ctxt || !node)
1359 return;
1360
1361 if (node->type == XML_DOCUMENT_NODE)
1362 xmlDocDump(ctxt->output, (xmlDocPtr) node);
1363 else if (node->type == XML_ATTRIBUTE_NODE)
1364 xmlDebugDumpAttrList(ctxt->output, (xmlAttrPtr) node, 0);
1365 else
1366 xmlElemDump(ctxt->output, node->doc, node);
1367
1368 fprintf(ctxt->output, "\n");
1369}
1370
1371/**
1372 * xmlShellPrintNode:
1373 * @node : a non-null node to print to the output FILE
1374 *
1375 * Print node to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001376 */
1377void
1378xmlShellPrintNode(xmlNodePtr node)
1379{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001380 xmlShellPrintNodeCtxt(NULL, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001381}
1382
Daniel Veillard78d12092001-10-11 09:12:24 +00001383/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001384 * xmlShellPrintXPathResultCtxt:
1385 * @ctxt: a valid shell context
Daniel Veillard9d06d302002-01-22 18:15:52 +00001386 * @list: a valid result generated by an xpath evaluation
Daniel Veillard78d12092001-10-11 09:12:24 +00001387 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001388 * Prints result to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001389 */
Daniel Veillard321be0c2002-10-08 21:26:42 +00001390static void
1391xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
Daniel Veillard78d12092001-10-11 09:12:24 +00001392{
1393 int i = 0;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001394 if (!ctxt)
1395 return;
Daniel Veillard78d12092001-10-11 09:12:24 +00001396
1397 if (list != NULL) {
1398 switch (list->type) {
1399 case XPATH_NODESET:{
1400 int indx;
1401
1402 if (list->nodesetval) {
1403 for (indx = 0; indx < list->nodesetval->nodeNr;
1404 indx++) {
1405 if (i > 0)
1406 fprintf(stderr, " -------\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00001407 xmlShellPrintNodeCtxt(ctxt,
1408 list->nodesetval->nodeTab[indx]);
Daniel Veillard78d12092001-10-11 09:12:24 +00001409 }
1410 } else {
1411 xmlGenericError(xmlGenericErrorContext,
1412 "Empty node set\n");
1413 }
1414 break;
1415 }
1416 case XPATH_BOOLEAN:
1417 xmlGenericError(xmlGenericErrorContext,
1418 "Is a Boolean:%s\n",
1419 xmlBoolToText(list->boolval));
1420 break;
1421 case XPATH_NUMBER:
1422 xmlGenericError(xmlGenericErrorContext,
1423 "Is a number:%0g\n", list->floatval);
1424 break;
1425 case XPATH_STRING:
1426 xmlGenericError(xmlGenericErrorContext,
1427 "Is a string:%s\n", list->stringval);
1428 break;
1429
1430 default:
1431 xmlShellPrintXPathError(list->type, NULL);
1432 }
1433 }
1434}
1435
1436/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001437 * xmlShellPrintXPathResult:
1438 * @list: a valid result generated by an xpath evaluation
1439 *
1440 * Prints result to the output FILE
1441 */
1442void
1443xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1444{
1445 xmlShellPrintXPathResultCtxt(NULL, list);
1446}
1447
1448/**
Owen Taylor3473f882001-02-23 17:55:21 +00001449 * xmlShellList:
1450 * @ctxt: the shell context
1451 * @arg: unused
1452 * @node: a node
1453 * @node2: unused
1454 *
1455 * Implements the XML shell function "ls"
1456 * Does an Unix like listing of the given node (like a directory)
1457 *
1458 * Returns 0
1459 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001460int
Daniel Veillard321be0c2002-10-08 21:26:42 +00001461xmlShellList(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00001462 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1463 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1464{
Owen Taylor3473f882001-02-23 17:55:21 +00001465 xmlNodePtr cur;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001466 if (!ctxt)
1467 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001468 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001469 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001470 return (0);
1471 }
Owen Taylor3473f882001-02-23 17:55:21 +00001472 if ((node->type == XML_DOCUMENT_NODE) ||
1473 (node->type == XML_HTML_DOCUMENT_NODE)) {
1474 cur = ((xmlDocPtr) node)->children;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001475 } else if (node->type == XML_NAMESPACE_DECL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001476 xmlLsOneNode(ctxt->output, node);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001477 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001478 } else if (node->children != NULL) {
1479 cur = node->children;
1480 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001481 xmlLsOneNode(ctxt->output, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001482 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001483 }
1484 while (cur != NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001485 xmlLsOneNode(ctxt->output, cur);
Daniel Veillard78d12092001-10-11 09:12:24 +00001486 cur = cur->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001487 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001488 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001489}
1490
1491/**
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001492 * xmlShellBase:
1493 * @ctxt: the shell context
1494 * @arg: unused
1495 * @node: a node
1496 * @node2: unused
1497 *
1498 * Implements the XML shell function "base"
1499 * dumps the current XML base of the node
1500 *
1501 * Returns 0
1502 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001503int
Daniel Veillard321be0c2002-10-08 21:26:42 +00001504xmlShellBase(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00001505 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1506 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1507{
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001508 xmlChar *base;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001509 if (!ctxt)
1510 return 0;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001511 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001512 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001513 return (0);
1514 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001515
1516 base = xmlNodeGetBase(node->doc, node);
1517
1518 if (base == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001519 fprintf(ctxt->output, " No base found !!!\n");
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001520 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001521 fprintf(ctxt->output, "%s\n", base);
Daniel Veillard78d12092001-10-11 09:12:24 +00001522 xmlFree(base);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001523 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001524 return (0);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001525}
1526
1527/**
Daniel Veillardcfa0d812002-01-17 08:46:58 +00001528 * xmlShellSetBase:
1529 * @ctxt: the shell context
1530 * @arg: the new base
1531 * @node: a node
1532 * @node2: unused
1533 *
1534 * Implements the XML shell function "setbase"
1535 * change the current XML base of the node
1536 *
1537 * Returns 0
1538 */
1539static int
1540xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1541 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1542 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1543{
1544 xmlNodeSetBase(node, (xmlChar*) arg);
1545 return (0);
1546}
1547
1548/**
Owen Taylor3473f882001-02-23 17:55:21 +00001549 * xmlShellDir:
1550 * @ctxt: the shell context
1551 * @arg: unused
1552 * @node: a node
1553 * @node2: unused
1554 *
1555 * Implements the XML shell function "dir"
1556 * dumps informations about the node (namespace, attributes, content).
1557 *
1558 * Returns 0
1559 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001560int
1561xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1562 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1563 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1564{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001565 if (!ctxt)
1566 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001567 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001568 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001569 return (0);
1570 }
Owen Taylor3473f882001-02-23 17:55:21 +00001571 if ((node->type == XML_DOCUMENT_NODE) ||
1572 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001573 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
Owen Taylor3473f882001-02-23 17:55:21 +00001574 } else if (node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001575 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001576 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001577 xmlDebugDumpOneNode(ctxt->output, node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001578 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001579 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001580}
1581
1582/**
1583 * xmlShellCat:
1584 * @ctxt: the shell context
1585 * @arg: unused
1586 * @node: a node
1587 * @node2: unused
1588 *
1589 * Implements the XML shell function "cat"
1590 * dumps the serialization node content (XML or HTML).
1591 *
1592 * Returns 0
1593 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001594int
1595xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
1596 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
1597{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001598 if (!ctxt)
1599 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001600 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001601 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001602 return (0);
1603 }
Owen Taylor3473f882001-02-23 17:55:21 +00001604 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
1605#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001606 if (node->type == XML_HTML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001607 htmlDocDump(ctxt->output, (htmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001608 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00001609 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001610#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001611 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001612 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001613 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00001614 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001615#endif /* LIBXML_HTML_ENABLED */
1616 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001617 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001618 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001619 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00001620 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001621 }
Daniel Veillard321be0c2002-10-08 21:26:42 +00001622 fprintf(ctxt->output, "\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00001623 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001624}
1625
1626/**
1627 * xmlShellLoad:
1628 * @ctxt: the shell context
1629 * @filename: the file name
1630 * @node: unused
1631 * @node2: unused
1632 *
1633 * Implements the XML shell function "load"
1634 * loads a new document specified by the filename
1635 *
1636 * Returns 0 or -1 if loading failed
1637 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001638int
1639xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
1640 xmlNodePtr node ATTRIBUTE_UNUSED,
1641 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1642{
Owen Taylor3473f882001-02-23 17:55:21 +00001643 xmlDocPtr doc;
1644 int html = 0;
1645
1646 if (ctxt->doc != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001647 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00001648
1649 if (html) {
1650#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001651 doc = htmlParseFile(filename, NULL);
1652#else
Daniel Veillard321be0c2002-10-08 21:26:42 +00001653 fprintf(ctxt->output, "HTML support not compiled in\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00001654 doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001655#endif /* LIBXML_HTML_ENABLED */
1656 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001657 doc = xmlParseFile(filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001658 }
1659 if (doc != NULL) {
1660 if (ctxt->loaded == 1) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001661 xmlFreeDoc(ctxt->doc);
1662 }
1663 ctxt->loaded = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001664#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001665 xmlXPathFreeContext(ctxt->pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001666#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001667 xmlFree(ctxt->filename);
1668 ctxt->doc = doc;
1669 ctxt->node = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001670#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001671 ctxt->pctxt = xmlXPathNewContext(doc);
Owen Taylor3473f882001-02-23 17:55:21 +00001672#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001673 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001674 } else
Daniel Veillard78d12092001-10-11 09:12:24 +00001675 return (-1);
1676 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001677}
1678
1679/**
1680 * xmlShellWrite:
1681 * @ctxt: the shell context
1682 * @filename: the file name
1683 * @node: a node in the tree
1684 * @node2: unused
1685 *
1686 * Implements the XML shell function "write"
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001687 * Write the current node to the filename, it saves the serialization
Owen Taylor3473f882001-02-23 17:55:21 +00001688 * of the subtree under the @node specified
1689 *
1690 * Returns 0 or -1 in case of error
1691 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001692int
Owen Taylor3473f882001-02-23 17:55:21 +00001693xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Daniel Veillard78d12092001-10-11 09:12:24 +00001694 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1695{
Owen Taylor3473f882001-02-23 17:55:21 +00001696 if (node == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001697 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001698 if ((filename == NULL) || (filename[0] == 0)) {
1699 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001700 "Write command requires a filename argument\n");
1701 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001702 }
1703#ifdef W_OK
1704 if (access((char *) filename, W_OK)) {
1705 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001706 "Cannot write to %s\n", filename);
1707 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001708 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001709#endif
1710 switch (node->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00001711 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00001712 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
1713 xmlGenericError(xmlGenericErrorContext,
1714 "Failed to write to %s\n", filename);
1715 return (-1);
1716 }
1717 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001718 case XML_HTML_DOCUMENT_NODE:
1719#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001720 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
1721 xmlGenericError(xmlGenericErrorContext,
1722 "Failed to write to %s\n", filename);
1723 return (-1);
1724 }
Owen Taylor3473f882001-02-23 17:55:21 +00001725#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001726 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
1727 xmlGenericError(xmlGenericErrorContext,
1728 "Failed to write to %s\n", filename);
1729 return (-1);
1730 }
Owen Taylor3473f882001-02-23 17:55:21 +00001731#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001732 break;
1733 default:{
1734 FILE *f;
Owen Taylor3473f882001-02-23 17:55:21 +00001735
Daniel Veillard78d12092001-10-11 09:12:24 +00001736 f = fopen((char *) filename, "w");
1737 if (f == NULL) {
1738 xmlGenericError(xmlGenericErrorContext,
1739 "Failed to write to %s\n", filename);
1740 return (-1);
1741 }
1742 xmlElemDump(f, ctxt->doc, node);
1743 fclose(f);
1744 }
Owen Taylor3473f882001-02-23 17:55:21 +00001745 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001746 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001747}
1748
1749/**
1750 * xmlShellSave:
1751 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001752 * @filename: the file name (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00001753 * @node: unused
1754 * @node2: unused
1755 *
1756 * Implements the XML shell function "save"
1757 * Write the current document to the filename, or it's original name
1758 *
1759 * Returns 0 or -1 in case of error
1760 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001761int
1762xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
1763 xmlNodePtr node ATTRIBUTE_UNUSED,
1764 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1765{
Owen Taylor3473f882001-02-23 17:55:21 +00001766 if (ctxt->doc == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001767 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001768 if ((filename == NULL) || (filename[0] == 0))
1769 filename = ctxt->filename;
1770#ifdef W_OK
1771 if (access((char *) filename, W_OK)) {
1772 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001773 "Cannot save to %s\n", filename);
1774 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001775 }
1776#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00001777 switch (ctxt->doc->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00001778 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00001779 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
1780 xmlGenericError(xmlGenericErrorContext,
1781 "Failed to save to %s\n", filename);
1782 }
1783 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001784 case XML_HTML_DOCUMENT_NODE:
1785#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001786 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
1787 xmlGenericError(xmlGenericErrorContext,
1788 "Failed to save to %s\n", filename);
1789 }
Owen Taylor3473f882001-02-23 17:55:21 +00001790#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001791 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
1792 xmlGenericError(xmlGenericErrorContext,
1793 "Failed to save to %s\n", filename);
1794 }
Owen Taylor3473f882001-02-23 17:55:21 +00001795#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001796 break;
1797 default:
1798 xmlGenericError(xmlGenericErrorContext,
1799 "To save to subparts of a document use the 'write' command\n");
1800 return (-1);
1801
Owen Taylor3473f882001-02-23 17:55:21 +00001802 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001803 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001804}
1805
1806/**
1807 * xmlShellValidate:
1808 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001809 * @dtd: the DTD URI (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00001810 * @node: unused
1811 * @node2: unused
1812 *
1813 * Implements the XML shell function "validate"
1814 * Validate the document, if a DTD path is provided, then the validation
1815 * is done against the given DTD.
1816 *
1817 * Returns 0 or -1 in case of error
1818 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001819int
1820xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
1821 xmlNodePtr node ATTRIBUTE_UNUSED,
1822 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1823{
Owen Taylor3473f882001-02-23 17:55:21 +00001824 xmlValidCtxt vctxt;
1825 int res = -1;
1826
1827 vctxt.userData = stderr;
1828 vctxt.error = (xmlValidityErrorFunc) fprintf;
1829 vctxt.warning = (xmlValidityWarningFunc) fprintf;
1830
1831 if ((dtd == NULL) || (dtd[0] == 0)) {
1832 res = xmlValidateDocument(&vctxt, ctxt->doc);
1833 } else {
1834 xmlDtdPtr subset;
1835
Daniel Veillard78d12092001-10-11 09:12:24 +00001836 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
1837 if (subset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001838 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
1839
Daniel Veillard78d12092001-10-11 09:12:24 +00001840 xmlFreeDtd(subset);
1841 }
Owen Taylor3473f882001-02-23 17:55:21 +00001842 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001843 return (res);
Owen Taylor3473f882001-02-23 17:55:21 +00001844}
1845
1846/**
1847 * xmlShellDu:
1848 * @ctxt: the shell context
1849 * @arg: unused
1850 * @tree: a node defining a subtree
1851 * @node2: unused
1852 *
1853 * Implements the XML shell function "du"
1854 * show the structure of the subtree under node @tree
1855 * If @tree is null, the command works on the current node.
1856 *
1857 * Returns 0 or -1 in case of error
1858 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001859int
Daniel Veillard321be0c2002-10-08 21:26:42 +00001860xmlShellDu(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00001861 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
1862 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1863{
Owen Taylor3473f882001-02-23 17:55:21 +00001864 xmlNodePtr node;
Daniel Veillard78d12092001-10-11 09:12:24 +00001865 int indent = 0, i;
Owen Taylor3473f882001-02-23 17:55:21 +00001866
Daniel Veillard321be0c2002-10-08 21:26:42 +00001867 if (!ctxt)
1868 return (-1);
1869
Daniel Veillard78d12092001-10-11 09:12:24 +00001870 if (tree == NULL)
1871 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001872 node = tree;
1873 while (node != NULL) {
1874 if ((node->type == XML_DOCUMENT_NODE) ||
1875 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001876 fprintf(ctxt->output, "/\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00001877 } else if (node->type == XML_ELEMENT_NODE) {
1878 for (i = 0; i < indent; i++)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001879 fprintf(ctxt->output, " ");
1880 fprintf(ctxt->output, "%s\n", node->name);
Daniel Veillard78d12092001-10-11 09:12:24 +00001881 } else {
1882 }
Owen Taylor3473f882001-02-23 17:55:21 +00001883
Daniel Veillard78d12092001-10-11 09:12:24 +00001884 /*
1885 * Browse the full subtree, deep first
1886 */
Owen Taylor3473f882001-02-23 17:55:21 +00001887
1888 if ((node->type == XML_DOCUMENT_NODE) ||
1889 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001890 node = ((xmlDocPtr) node)->children;
1891 } else if ((node->children != NULL)
1892 && (node->type != XML_ENTITY_REF_NODE)) {
1893 /* deep first */
1894 node = node->children;
1895 indent++;
1896 } else if ((node != tree) && (node->next != NULL)) {
1897 /* then siblings */
1898 node = node->next;
1899 } else if (node != tree) {
1900 /* go up to parents->next if needed */
1901 while (node != tree) {
1902 if (node->parent != NULL) {
1903 node = node->parent;
1904 indent--;
1905 }
1906 if ((node != tree) && (node->next != NULL)) {
1907 node = node->next;
1908 break;
1909 }
1910 if (node->parent == NULL) {
1911 node = NULL;
1912 break;
1913 }
1914 if (node == tree) {
1915 node = NULL;
1916 break;
1917 }
1918 }
1919 /* exit condition */
1920 if (node == tree)
1921 node = NULL;
1922 } else
1923 node = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001924 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001925 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001926}
1927
1928/**
1929 * xmlShellPwd:
1930 * @ctxt: the shell context
1931 * @buffer: the output buffer
Daniel Veillard9d06d302002-01-22 18:15:52 +00001932 * @node: a node
Owen Taylor3473f882001-02-23 17:55:21 +00001933 * @node2: unused
1934 *
1935 * Implements the XML shell function "pwd"
1936 * Show the full path from the root to the node, if needed building
1937 * thumblers when similar elements exists at a given ancestor level.
1938 * The output is compatible with XPath commands.
1939 *
1940 * Returns 0 or -1 in case of error
1941 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001942int
1943xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
1944 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
1945{
Daniel Veillardc6e013a2001-11-10 10:08:57 +00001946 xmlChar *path;
Owen Taylor3473f882001-02-23 17:55:21 +00001947
Daniel Veillard78d12092001-10-11 09:12:24 +00001948 if (node == NULL)
1949 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001950
Daniel Veillardc6e013a2001-11-10 10:08:57 +00001951 path = xmlGetNodePath(node);
1952 if (path == NULL)
1953 return (-1);
1954
1955 /*
1956 * This test prevents buffer overflow, because this routine
1957 * is only called by xmlShell, in which the second argument is
1958 * 500 chars long.
1959 * It is a dirty hack before a cleaner solution is found.
1960 * Documentation should mention that the second argument must
1961 * be at least 500 chars long, and could be stripped if too long.
1962 */
1963 snprintf(buffer, 499, "%s", path);
1964 buffer[499] = '0';
1965 xmlFree(path);
1966
Daniel Veillard78d12092001-10-11 09:12:24 +00001967 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001968}
1969
1970/**
1971 * xmlShell
1972 * @doc: the initial document
1973 * @filename: the output buffer
1974 * @input: the line reading function
Daniel Veillard321be0c2002-10-08 21:26:42 +00001975 * @output: the output FILE*, defaults to stdout if NULL
Owen Taylor3473f882001-02-23 17:55:21 +00001976 *
1977 * Implements the XML shell
1978 * This allow to load, validate, view, modify and save a document
1979 * using a environment similar to a UNIX commandline.
1980 */
1981void
1982xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Daniel Veillard78d12092001-10-11 09:12:24 +00001983 FILE * output)
1984{
Owen Taylor3473f882001-02-23 17:55:21 +00001985 char prompt[500] = "/ > ";
1986 char *cmdline = NULL, *cur;
1987 int nbargs;
1988 char command[100];
1989 char arg[400];
1990 int i;
1991 xmlShellCtxtPtr ctxt;
1992 xmlXPathObjectPtr list;
1993
1994 if (doc == NULL)
1995 return;
1996 if (filename == NULL)
1997 return;
1998 if (input == NULL)
1999 return;
2000 if (output == NULL)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002001 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00002002 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Daniel Veillard78d12092001-10-11 09:12:24 +00002003 if (ctxt == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00002004 return;
2005 ctxt->loaded = 0;
2006 ctxt->doc = doc;
2007 ctxt->input = input;
2008 ctxt->output = output;
2009 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Daniel Veillard78d12092001-10-11 09:12:24 +00002010 ctxt->node = (xmlNodePtr) ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002011
2012#ifdef LIBXML_XPATH_ENABLED
2013 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2014 if (ctxt->pctxt == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002015 xmlFree(ctxt);
2016 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002017 }
2018#endif /* LIBXML_XPATH_ENABLED */
2019 while (1) {
2020 if (ctxt->node == (xmlNodePtr) ctxt->doc)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002021 snprintf(prompt, sizeof(prompt), "%s > ", "/");
Daniel Veillard78d12092001-10-11 09:12:24 +00002022 else if (ctxt->node->name)
2023 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002024 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002025 snprintf(prompt, sizeof(prompt), "? > ");
Owen Taylor3473f882001-02-23 17:55:21 +00002026 prompt[sizeof(prompt) - 1] = 0;
2027
Daniel Veillard78d12092001-10-11 09:12:24 +00002028 /*
2029 * Get a new command line
2030 */
Owen Taylor3473f882001-02-23 17:55:21 +00002031 cmdline = ctxt->input(prompt);
Daniel Veillard78d12092001-10-11 09:12:24 +00002032 if (cmdline == NULL)
2033 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002034
Daniel Veillard78d12092001-10-11 09:12:24 +00002035 /*
2036 * Parse the command itself
2037 */
2038 cur = cmdline;
2039 nbargs = 0;
2040 while ((*cur == ' ') || (*cur == '\t'))
2041 cur++;
2042 i = 0;
2043 while ((*cur != ' ') && (*cur != '\t') &&
2044 (*cur != '\n') && (*cur != '\r')) {
2045 if (*cur == 0)
2046 break;
2047 command[i++] = *cur++;
2048 }
2049 command[i] = 0;
2050 if (i == 0)
2051 continue;
2052 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002053
Daniel Veillard78d12092001-10-11 09:12:24 +00002054 /*
2055 * Parse the argument
2056 */
2057 while ((*cur == ' ') || (*cur == '\t'))
2058 cur++;
2059 i = 0;
2060 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2061 if (*cur == 0)
2062 break;
2063 arg[i++] = *cur++;
2064 }
2065 arg[i] = 0;
2066 if (i != 0)
2067 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002068
Daniel Veillard78d12092001-10-11 09:12:24 +00002069 /*
2070 * start interpreting the command
2071 */
Owen Taylor3473f882001-02-23 17:55:21 +00002072 if (!strcmp(command, "exit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002073 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002074 if (!strcmp(command, "quit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002075 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002076 if (!strcmp(command, "bye"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002077 break;
Daniel Veillard5004f422001-11-08 13:53:05 +00002078 if (!strcmp(command, "help")) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002079 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2080 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2081 fprintf(ctxt->output, "\tbye leave shell\n");
2082 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2083 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2084 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2085 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2086 fprintf(ctxt->output, "\texit leave shell\n");
2087 fprintf(ctxt->output, "\thelp display this help\n");
2088 fprintf(ctxt->output, "\tfree display memory usage\n");
2089 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2090 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002091#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002092 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002093#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard321be0c2002-10-08 21:26:42 +00002094 fprintf(ctxt->output, "\tpwd display current working directory\n");
2095 fprintf(ctxt->output, "\tquit leave shell\n");
2096 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
2097 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
2098 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
Daniel Veillard5004f422001-11-08 13:53:05 +00002099 } else if (!strcmp(command, "validate")) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002100 xmlShellValidate(ctxt, arg, NULL, NULL);
2101 } else if (!strcmp(command, "load")) {
2102 xmlShellLoad(ctxt, arg, NULL, NULL);
2103 } else if (!strcmp(command, "save")) {
2104 xmlShellSave(ctxt, arg, NULL, NULL);
2105 } else if (!strcmp(command, "write")) {
2106 xmlShellWrite(ctxt, arg, NULL, NULL);
2107 } else if (!strcmp(command, "free")) {
2108 if (arg[0] == 0) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002109 xmlMemShow(ctxt->output, 0);
Daniel Veillard78d12092001-10-11 09:12:24 +00002110 } else {
2111 int len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002112
Daniel Veillard78d12092001-10-11 09:12:24 +00002113 sscanf(arg, "%d", &len);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002114 xmlMemShow(ctxt->output, len);
Daniel Veillard78d12092001-10-11 09:12:24 +00002115 }
2116 } else if (!strcmp(command, "pwd")) {
2117 char dir[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002118
Daniel Veillard78d12092001-10-11 09:12:24 +00002119 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
Daniel Veillard321be0c2002-10-08 21:26:42 +00002120 fprintf(ctxt->output, "%s\n", dir);
Daniel Veillard78d12092001-10-11 09:12:24 +00002121 } else if (!strcmp(command, "du")) {
2122 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2123 } else if (!strcmp(command, "base")) {
2124 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002125#ifdef LIBXML_XPATH_ENABLED
2126 } else if (!strcmp(command, "xpath")) {
2127 if (arg[0] == 0) {
2128 xmlGenericError(xmlGenericErrorContext,
2129 "xpath: expression required\n");
2130 } else {
2131 ctxt->pctxt->node = ctxt->node;
2132 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002133 xmlXPathDebugDumpObject(ctxt->output, list, 0);
Daniel Veillard2070c482002-01-22 22:12:19 +00002134 xmlXPathFreeObject(list);
2135 }
2136#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002137 } else if (!strcmp(command, "setbase")) {
2138 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
Daniel Veillard78d12092001-10-11 09:12:24 +00002139 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
2140 int dir = (!strcmp(command, "dir"));
2141
2142 if (arg[0] == 0) {
2143 if (dir)
2144 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
2145 else
2146 xmlShellList(ctxt, NULL, ctxt->node, NULL);
2147 } else {
2148 ctxt->pctxt->node = ctxt->node;
Daniel Veillard61d80a22001-04-27 17:13:01 +00002149#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002150 ctxt->pctxt->node = ctxt->node;
2151 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2152#else
2153 list = NULL;
2154#endif /* LIBXML_XPATH_ENABLED */
2155 if (list != NULL) {
2156 switch (list->type) {
2157 case XPATH_UNDEFINED:
2158 xmlGenericError(xmlGenericErrorContext,
2159 "%s: no such node\n", arg);
2160 break;
2161 case XPATH_NODESET:{
2162 int indx;
2163
Daniel Veillarda6825e82001-11-07 13:33:59 +00002164 if (list->nodesetval == NULL)
2165 break;
2166
Daniel Veillard78d12092001-10-11 09:12:24 +00002167 for (indx = 0;
2168 indx < list->nodesetval->nodeNr;
2169 indx++) {
2170 if (dir)
2171 xmlShellDir(ctxt, NULL,
2172 list->nodesetval->
2173 nodeTab[indx], NULL);
2174 else
2175 xmlShellList(ctxt, NULL,
2176 list->nodesetval->
2177 nodeTab[indx], NULL);
2178 }
2179 break;
2180 }
2181 case XPATH_BOOLEAN:
2182 xmlGenericError(xmlGenericErrorContext,
2183 "%s is a Boolean\n", arg);
2184 break;
2185 case XPATH_NUMBER:
2186 xmlGenericError(xmlGenericErrorContext,
2187 "%s is a number\n", arg);
2188 break;
2189 case XPATH_STRING:
2190 xmlGenericError(xmlGenericErrorContext,
2191 "%s is a string\n", arg);
2192 break;
2193 case XPATH_POINT:
2194 xmlGenericError(xmlGenericErrorContext,
2195 "%s is a point\n", arg);
2196 break;
2197 case XPATH_RANGE:
2198 xmlGenericError(xmlGenericErrorContext,
2199 "%s is a range\n", arg);
2200 break;
2201 case XPATH_LOCATIONSET:
2202 xmlGenericError(xmlGenericErrorContext,
2203 "%s is a range\n", arg);
2204 break;
2205 case XPATH_USERS:
2206 xmlGenericError(xmlGenericErrorContext,
2207 "%s is user-defined\n", arg);
2208 break;
2209 case XPATH_XSLT_TREE:
2210 xmlGenericError(xmlGenericErrorContext,
2211 "%s is an XSLT value tree\n",
2212 arg);
2213 break;
2214 }
2215#ifdef LIBXML_XPATH_ENABLED
2216 xmlXPathFreeObject(list);
Daniel Veillard61d80a22001-04-27 17:13:01 +00002217#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002218 } else {
2219 xmlGenericError(xmlGenericErrorContext,
2220 "%s: no such node\n", arg);
2221 }
2222 ctxt->pctxt->node = NULL;
2223 }
2224 } else if (!strcmp(command, "cd")) {
2225 if (arg[0] == 0) {
2226 ctxt->node = (xmlNodePtr) ctxt->doc;
2227 } else {
2228#ifdef LIBXML_XPATH_ENABLED
2229 ctxt->pctxt->node = ctxt->node;
2230 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2231#else
2232 list = NULL;
2233#endif /* LIBXML_XPATH_ENABLED */
2234 if (list != NULL) {
2235 switch (list->type) {
2236 case XPATH_UNDEFINED:
2237 xmlGenericError(xmlGenericErrorContext,
2238 "%s: no such node\n", arg);
2239 break;
2240 case XPATH_NODESET:
Daniel Veillarda6825e82001-11-07 13:33:59 +00002241 if (list->nodesetval != NULL) {
2242 if (list->nodesetval->nodeNr == 1) {
2243 ctxt->node = list->nodesetval->nodeTab[0];
2244 } else
2245 xmlGenericError(xmlGenericErrorContext,
2246 "%s is a %d Node Set\n",
2247 arg,
2248 list->nodesetval->nodeNr);
Daniel Veillard78d12092001-10-11 09:12:24 +00002249 } else
2250 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda6825e82001-11-07 13:33:59 +00002251 "%s is an empty Node Set\n",
2252 arg);
Daniel Veillard78d12092001-10-11 09:12:24 +00002253 break;
2254 case XPATH_BOOLEAN:
2255 xmlGenericError(xmlGenericErrorContext,
2256 "%s is a Boolean\n", arg);
2257 break;
2258 case XPATH_NUMBER:
2259 xmlGenericError(xmlGenericErrorContext,
2260 "%s is a number\n", arg);
2261 break;
2262 case XPATH_STRING:
2263 xmlGenericError(xmlGenericErrorContext,
2264 "%s is a string\n", arg);
2265 break;
2266 case XPATH_POINT:
2267 xmlGenericError(xmlGenericErrorContext,
2268 "%s is a point\n", arg);
2269 break;
2270 case XPATH_RANGE:
2271 xmlGenericError(xmlGenericErrorContext,
2272 "%s is a range\n", arg);
2273 break;
2274 case XPATH_LOCATIONSET:
2275 xmlGenericError(xmlGenericErrorContext,
2276 "%s is a range\n", arg);
2277 break;
2278 case XPATH_USERS:
2279 xmlGenericError(xmlGenericErrorContext,
2280 "%s is user-defined\n", arg);
2281 break;
2282 case XPATH_XSLT_TREE:
2283 xmlGenericError(xmlGenericErrorContext,
2284 "%s is an XSLT value tree\n",
2285 arg);
2286 break;
2287 }
2288#ifdef LIBXML_XPATH_ENABLED
2289 xmlXPathFreeObject(list);
2290#endif
2291 } else {
2292 xmlGenericError(xmlGenericErrorContext,
2293 "%s: no such node\n", arg);
2294 }
2295 ctxt->pctxt->node = NULL;
2296 }
2297 } else if (!strcmp(command, "cat")) {
2298 if (arg[0] == 0) {
2299 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
2300 } else {
2301 ctxt->pctxt->node = ctxt->node;
2302#ifdef LIBXML_XPATH_ENABLED
2303 ctxt->pctxt->node = ctxt->node;
2304 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2305#else
2306 list = NULL;
2307#endif /* LIBXML_XPATH_ENABLED */
2308 if (list != NULL) {
2309 switch (list->type) {
2310 case XPATH_UNDEFINED:
2311 xmlGenericError(xmlGenericErrorContext,
2312 "%s: no such node\n", arg);
2313 break;
2314 case XPATH_NODESET:{
2315 int indx;
2316
Daniel Veillarda6825e82001-11-07 13:33:59 +00002317 if (list->nodesetval == NULL)
2318 break;
2319
Daniel Veillard78d12092001-10-11 09:12:24 +00002320 for (indx = 0;
2321 indx < list->nodesetval->nodeNr;
2322 indx++) {
2323 if (i > 0)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002324 fprintf(ctxt->output, " -------\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002325 xmlShellCat(ctxt, NULL,
2326 list->nodesetval->
2327 nodeTab[indx], NULL);
2328 }
2329 break;
2330 }
2331 case XPATH_BOOLEAN:
2332 xmlGenericError(xmlGenericErrorContext,
2333 "%s is a Boolean\n", arg);
2334 break;
2335 case XPATH_NUMBER:
2336 xmlGenericError(xmlGenericErrorContext,
2337 "%s is a number\n", arg);
2338 break;
2339 case XPATH_STRING:
2340 xmlGenericError(xmlGenericErrorContext,
2341 "%s is a string\n", arg);
2342 break;
2343 case XPATH_POINT:
2344 xmlGenericError(xmlGenericErrorContext,
2345 "%s is a point\n", arg);
2346 break;
2347 case XPATH_RANGE:
2348 xmlGenericError(xmlGenericErrorContext,
2349 "%s is a range\n", arg);
2350 break;
2351 case XPATH_LOCATIONSET:
2352 xmlGenericError(xmlGenericErrorContext,
2353 "%s is a range\n", arg);
2354 break;
2355 case XPATH_USERS:
2356 xmlGenericError(xmlGenericErrorContext,
2357 "%s is user-defined\n", arg);
2358 break;
2359 case XPATH_XSLT_TREE:
2360 xmlGenericError(xmlGenericErrorContext,
2361 "%s is an XSLT value tree\n",
2362 arg);
2363 break;
2364 }
2365#ifdef LIBXML_XPATH_ENABLED
2366 xmlXPathFreeObject(list);
2367#endif
2368 } else {
2369 xmlGenericError(xmlGenericErrorContext,
2370 "%s: no such node\n", arg);
2371 }
2372 ctxt->pctxt->node = NULL;
2373 }
2374 } else {
2375 xmlGenericError(xmlGenericErrorContext,
2376 "Unknown command %s\n", command);
2377 }
2378 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00002379 }
2380#ifdef LIBXML_XPATH_ENABLED
2381 xmlXPathFreeContext(ctxt->pctxt);
2382#endif /* LIBXML_XPATH_ENABLED */
2383 if (ctxt->loaded) {
2384 xmlFreeDoc(ctxt->doc);
2385 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002386 if (ctxt->filename != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002387 xmlFree(ctxt->filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002388 xmlFree(ctxt);
2389 if (cmdline != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002390 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00002391}
2392
2393#endif /* LIBXML_DEBUG_ENABLED */