blob: 25c1d15ef64e0b3d425cd84ef9b0da313e469fb3 [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>
Igor Zlatkovicc06bb622003-04-27 15:59:00 +000032#include <libxml/uri.h>
Daniel Veillard522bc602004-02-21 11:53:09 +000033#ifdef LIBXML_SCHEMAS_ENABLED
34#include <libxml/relaxng.h>
35#endif
Owen Taylor3473f882001-02-23 17:55:21 +000036
Daniel Veillard5e2dace2001-07-18 19:30:27 +000037/**
38 * xmlDebugDumpString:
39 * @output: the FILE * for the output
40 * @str: the string
41 *
42 * Dumps informations about the string, shorten it if necessary
43 */
44void
45xmlDebugDumpString(FILE * output, const xmlChar * str)
46{
Owen Taylor3473f882001-02-23 17:55:21 +000047 int i;
Daniel Veillard5e2dace2001-07-18 19:30:27 +000048
Daniel Veillard7db38712002-02-07 16:39:11 +000049 if (output == NULL)
50 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +000051 if (str == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +000052 fprintf(output, "(NULL)");
53 return;
Owen Taylor3473f882001-02-23 17:55:21 +000054 }
Daniel Veillard5e2dace2001-07-18 19:30:27 +000055 for (i = 0; i < 40; i++)
56 if (str[i] == 0)
57 return;
William M. Brack76e95df2003-10-18 16:20:14 +000058 else if (IS_BLANK_CH(str[i]))
Daniel Veillard5e2dace2001-07-18 19:30:27 +000059 fputc(' ', output);
60 else if (str[i] >= 0x80)
61 fprintf(output, "#%X", str[i]);
62 else
63 fputc(str[i], output);
Owen Taylor3473f882001-02-23 17:55:21 +000064 fprintf(output, "...");
65}
66
Daniel Veillard56a4cb82001-03-24 17:00:36 +000067static void
68xmlDebugDumpDtdNode(FILE *output, xmlDtdPtr dtd, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +000069 int i;
70 char shift[100];
71
72 for (i = 0;((i < depth) && (i < 25));i++)
73 shift[2 * i] = shift[2 * i + 1] = ' ';
74 shift[2 * i] = shift[2 * i + 1] = 0;
75
76 fprintf(output, shift);
77
Daniel Veillard5e926fa2002-01-22 21:44:25 +000078 if (dtd == NULL) {
79 fprintf(output, "DTD node is NULL\n");
80 return;
81 }
82
Owen Taylor3473f882001-02-23 17:55:21 +000083 if (dtd->type != XML_DTD_NODE) {
84 fprintf(output, "PBM: not a DTD\n");
85 return;
86 }
87 if (dtd->name != NULL)
88 fprintf(output, "DTD(%s)", dtd->name);
89 else
90 fprintf(output, "DTD");
91 if (dtd->ExternalID != NULL)
92 fprintf(output, ", PUBLIC %s", dtd->ExternalID);
93 if (dtd->SystemID != NULL)
94 fprintf(output, ", SYSTEM %s", dtd->SystemID);
95 fprintf(output, "\n");
96 /*
97 * Do a bit of checking
98 */
99 if (dtd->parent == NULL)
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000100 fprintf(output, "PBM: DTD has no parent\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000101 if (dtd->doc == NULL)
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000102 fprintf(output, "PBM: DTD has no doc\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000103 if ((dtd->parent != NULL) && (dtd->doc != dtd->parent->doc))
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000104 fprintf(output, "PBM: DTD doc differs from parent's one\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000105 if (dtd->prev == NULL) {
106 if ((dtd->parent != NULL) && (dtd->parent->children != (xmlNodePtr)dtd))
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000107 fprintf(output, "PBM: DTD has no prev and not first of list\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000108 } else {
109 if (dtd->prev->next != (xmlNodePtr) dtd)
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000110 fprintf(output, "PBM: DTD prev->next : back link wrong\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000111 }
112 if (dtd->next == NULL) {
113 if ((dtd->parent != NULL) && (dtd->parent->last != (xmlNodePtr) dtd))
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000114 fprintf(output, "PBM: DTD has no next and not last of list\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000115 } else {
116 if (dtd->next->prev != (xmlNodePtr) dtd)
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000117 fprintf(output, "PBM: DTD next->prev : forward link wrong\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000118 }
119}
120
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000121static void
122xmlDebugDumpAttrDecl(FILE *output, xmlAttributePtr attr, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000123 int i;
124 char shift[100];
125
126 for (i = 0;((i < depth) && (i < 25));i++)
127 shift[2 * i] = shift[2 * i + 1] = ' ';
128 shift[2 * i] = shift[2 * i + 1] = 0;
129
130 fprintf(output, shift);
131
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000132 if (attr == NULL) {
133 fprintf(output, "Attribute declaration is NULL\n");
134 return;
135 }
Owen Taylor3473f882001-02-23 17:55:21 +0000136 if (attr->type != XML_ATTRIBUTE_DECL) {
137 fprintf(output, "PBM: not a Attr\n");
138 return;
139 }
140 if (attr->name != NULL)
141 fprintf(output, "ATTRDECL(%s)", attr->name);
142 else
143 fprintf(output, "PBM ATTRDECL noname!!!");
144 if (attr->elem != NULL)
145 fprintf(output, " for %s", attr->elem);
146 else
147 fprintf(output, " PBM noelem!!!");
148 switch (attr->atype) {
149 case XML_ATTRIBUTE_CDATA:
150 fprintf(output, " CDATA");
151 break;
152 case XML_ATTRIBUTE_ID:
153 fprintf(output, " ID");
154 break;
155 case XML_ATTRIBUTE_IDREF:
156 fprintf(output, " IDREF");
157 break;
158 case XML_ATTRIBUTE_IDREFS:
159 fprintf(output, " IDREFS");
160 break;
161 case XML_ATTRIBUTE_ENTITY:
162 fprintf(output, " ENTITY");
163 break;
164 case XML_ATTRIBUTE_ENTITIES:
165 fprintf(output, " ENTITIES");
166 break;
167 case XML_ATTRIBUTE_NMTOKEN:
168 fprintf(output, " NMTOKEN");
169 break;
170 case XML_ATTRIBUTE_NMTOKENS:
171 fprintf(output, " NMTOKENS");
172 break;
173 case XML_ATTRIBUTE_ENUMERATION:
174 fprintf(output, " ENUMERATION");
175 break;
176 case XML_ATTRIBUTE_NOTATION:
177 fprintf(output, " NOTATION ");
178 break;
179 }
180 if (attr->tree != NULL) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000181 int indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000182 xmlEnumerationPtr cur = attr->tree;
183
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000184 for (indx = 0;indx < 5; indx++) {
185 if (indx != 0)
Owen Taylor3473f882001-02-23 17:55:21 +0000186 fprintf(output, "|%s", cur->name);
187 else
188 fprintf(output, " (%s", cur->name);
189 cur = cur->next;
190 if (cur == NULL) break;
191 }
192 if (cur == NULL)
193 fprintf(output, ")");
194 else
195 fprintf(output, "...)");
196 }
197 switch (attr->def) {
198 case XML_ATTRIBUTE_NONE:
199 break;
200 case XML_ATTRIBUTE_REQUIRED:
201 fprintf(output, " REQUIRED");
202 break;
203 case XML_ATTRIBUTE_IMPLIED:
204 fprintf(output, " IMPLIED");
205 break;
206 case XML_ATTRIBUTE_FIXED:
207 fprintf(output, " FIXED");
208 break;
209 }
210 if (attr->defaultValue != NULL) {
211 fprintf(output, "\"");
212 xmlDebugDumpString(output, attr->defaultValue);
213 fprintf(output, "\"");
214 }
Daniel Veillardcd337f02001-11-22 18:20:37 +0000215 fprintf(output, "\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000216
217 /*
218 * Do a bit of checking
219 */
220 if (attr->parent == NULL)
221 fprintf(output, "PBM: Attr has no parent\n");
222 if (attr->doc == NULL)
223 fprintf(output, "PBM: Attr has no doc\n");
224 if ((attr->parent != NULL) && (attr->doc != attr->parent->doc))
225 fprintf(output, "PBM: Attr doc differs from parent's one\n");
226 if (attr->prev == NULL) {
227 if ((attr->parent != NULL) && (attr->parent->children != (xmlNodePtr)attr))
228 fprintf(output, "PBM: Attr has no prev and not first of list\n");
229 } else {
230 if (attr->prev->next != (xmlNodePtr) attr)
231 fprintf(output, "PBM: Attr prev->next : back link wrong\n");
232 }
233 if (attr->next == NULL) {
234 if ((attr->parent != NULL) && (attr->parent->last != (xmlNodePtr) attr))
235 fprintf(output, "PBM: Attr has no next and not last of list\n");
236 } else {
237 if (attr->next->prev != (xmlNodePtr) attr)
238 fprintf(output, "PBM: Attr next->prev : forward link wrong\n");
239 }
240}
241
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000242static void
243xmlDebugDumpElemDecl(FILE *output, xmlElementPtr elem, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000244 int i;
245 char shift[100];
246
247 for (i = 0;((i < depth) && (i < 25));i++)
248 shift[2 * i] = shift[2 * i + 1] = ' ';
249 shift[2 * i] = shift[2 * i + 1] = 0;
250
251 fprintf(output, shift);
252
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000253 if (elem == NULL) {
254 fprintf(output, "Element declaration is NULL\n");
255 return;
256 }
Owen Taylor3473f882001-02-23 17:55:21 +0000257 if (elem->type != XML_ELEMENT_DECL) {
258 fprintf(output, "PBM: not a Elem\n");
259 return;
260 }
261 if (elem->name != NULL) {
262 fprintf(output, "ELEMDECL(");
263 xmlDebugDumpString(output, elem->name);
264 fprintf(output, ")");
265 } else
266 fprintf(output, "PBM ELEMDECL noname!!!");
267 switch (elem->etype) {
Daniel Veillarda10efa82001-04-18 13:09:01 +0000268 case XML_ELEMENT_TYPE_UNDEFINED:
269 fprintf(output, ", UNDEFINED");
270 break;
Owen Taylor3473f882001-02-23 17:55:21 +0000271 case XML_ELEMENT_TYPE_EMPTY:
272 fprintf(output, ", EMPTY");
273 break;
274 case XML_ELEMENT_TYPE_ANY:
275 fprintf(output, ", ANY");
276 break;
277 case XML_ELEMENT_TYPE_MIXED:
278 fprintf(output, ", MIXED ");
279 break;
280 case XML_ELEMENT_TYPE_ELEMENT:
281 fprintf(output, ", MIXED ");
282 break;
283 }
Daniel Veillard7db37732001-07-12 01:20:08 +0000284 if ((elem->type != XML_ELEMENT_NODE) &&
285 (elem->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000286 char buf[5001];
287
288 buf[0] = 0;
Daniel Veillardd3d06722001-08-15 12:06:36 +0000289 xmlSnprintfElementContent(buf, 5000, elem->content, 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000290 buf[5000] = 0;
291 fprintf(output, "%s", buf);
292 }
Daniel Veillardcd337f02001-11-22 18:20:37 +0000293 fprintf(output, "\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000294
295 /*
296 * Do a bit of checking
297 */
298 if (elem->parent == NULL)
299 fprintf(output, "PBM: Elem has no parent\n");
300 if (elem->doc == NULL)
301 fprintf(output, "PBM: Elem has no doc\n");
302 if ((elem->parent != NULL) && (elem->doc != elem->parent->doc))
303 fprintf(output, "PBM: Elem doc differs from parent's one\n");
304 if (elem->prev == NULL) {
305 if ((elem->parent != NULL) && (elem->parent->children != (xmlNodePtr)elem))
306 fprintf(output, "PBM: Elem has no prev and not first of list\n");
307 } else {
308 if (elem->prev->next != (xmlNodePtr) elem)
309 fprintf(output, "PBM: Elem prev->next : back link wrong\n");
310 }
311 if (elem->next == NULL) {
312 if ((elem->parent != NULL) && (elem->parent->last != (xmlNodePtr) elem))
313 fprintf(output, "PBM: Elem has no next and not last of list\n");
314 } else {
315 if (elem->next->prev != (xmlNodePtr) elem)
316 fprintf(output, "PBM: Elem next->prev : forward link wrong\n");
317 }
318}
319
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000320static void
321xmlDebugDumpEntityDecl(FILE *output, xmlEntityPtr ent, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000322 int i;
323 char shift[100];
324
325 for (i = 0;((i < depth) && (i < 25));i++)
326 shift[2 * i] = shift[2 * i + 1] = ' ';
327 shift[2 * i] = shift[2 * i + 1] = 0;
328
329 fprintf(output, shift);
330
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000331 if (ent == NULL) {
332 fprintf(output, "Entity declaration is NULL\n");
333 return;
334 }
Owen Taylor3473f882001-02-23 17:55:21 +0000335 if (ent->type != XML_ENTITY_DECL) {
336 fprintf(output, "PBM: not a Entity decl\n");
337 return;
338 }
339 if (ent->name != NULL) {
340 fprintf(output, "ENTITYDECL(");
341 xmlDebugDumpString(output, ent->name);
342 fprintf(output, ")");
343 } else
344 fprintf(output, "PBM ENTITYDECL noname!!!");
345 switch (ent->etype) {
346 case XML_INTERNAL_GENERAL_ENTITY:
347 fprintf(output, ", internal\n");
348 break;
349 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
350 fprintf(output, ", external parsed\n");
351 break;
352 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
353 fprintf(output, ", unparsed\n");
354 break;
355 case XML_INTERNAL_PARAMETER_ENTITY:
356 fprintf(output, ", parameter\n");
357 break;
358 case XML_EXTERNAL_PARAMETER_ENTITY:
359 fprintf(output, ", external parameter\n");
360 break;
361 case XML_INTERNAL_PREDEFINED_ENTITY:
362 fprintf(output, ", predefined\n");
363 break;
364 }
365 if (ent->ExternalID) {
366 fprintf(output, shift);
367 fprintf(output, " ExternalID=%s\n", ent->ExternalID);
368 }
369 if (ent->SystemID) {
370 fprintf(output, shift);
371 fprintf(output, " SystemID=%s\n", ent->SystemID);
372 }
373 if (ent->URI != NULL) {
374 fprintf(output, shift);
375 fprintf(output, " URI=%s\n", ent->URI);
376 }
377 if (ent->content) {
378 fprintf(output, shift);
379 fprintf(output, " content=");
380 xmlDebugDumpString(output, ent->content);
381 fprintf(output, "\n");
382 }
383
384 /*
385 * Do a bit of checking
386 */
387 if (ent->parent == NULL)
388 fprintf(output, "PBM: Ent has no parent\n");
389 if (ent->doc == NULL)
390 fprintf(output, "PBM: Ent has no doc\n");
391 if ((ent->parent != NULL) && (ent->doc != ent->parent->doc))
392 fprintf(output, "PBM: Ent doc differs from parent's one\n");
393 if (ent->prev == NULL) {
394 if ((ent->parent != NULL) && (ent->parent->children != (xmlNodePtr)ent))
395 fprintf(output, "PBM: Ent has no prev and not first of list\n");
396 } else {
397 if (ent->prev->next != (xmlNodePtr) ent)
398 fprintf(output, "PBM: Ent prev->next : back link wrong\n");
399 }
400 if (ent->next == NULL) {
401 if ((ent->parent != NULL) && (ent->parent->last != (xmlNodePtr) ent))
402 fprintf(output, "PBM: Ent has no next and not last of list\n");
403 } else {
404 if (ent->next->prev != (xmlNodePtr) ent)
405 fprintf(output, "PBM: Ent next->prev : forward link wrong\n");
406 }
407}
408
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000409static void
410xmlDebugDumpNamespace(FILE *output, xmlNsPtr ns, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000411 int i;
412 char shift[100];
413
414 for (i = 0;((i < depth) && (i < 25));i++)
415 shift[2 * i] = shift[2 * i + 1] = ' ';
416 shift[2 * i] = shift[2 * i + 1] = 0;
417
418 fprintf(output, shift);
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000419
420 if (ns == NULL) {
421 fprintf(output, "namespace node is NULL\n");
422 return;
423 }
Owen Taylor3473f882001-02-23 17:55:21 +0000424 if (ns->type != XML_NAMESPACE_DECL) {
425 fprintf(output, "invalid namespace node %d\n", ns->type);
426 return;
427 }
428 if (ns->href == NULL) {
429 if (ns->prefix != NULL)
430 fprintf(output, "incomplete namespace %s href=NULL\n", ns->prefix);
431 else
432 fprintf(output, "incomplete default namespace href=NULL\n");
433 } else {
434 if (ns->prefix != NULL)
435 fprintf(output, "namespace %s href=", ns->prefix);
436 else
437 fprintf(output, "default namespace href=");
438
439 xmlDebugDumpString(output, ns->href);
440 fprintf(output, "\n");
441 }
442}
443
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000444static void
445xmlDebugDumpNamespaceList(FILE *output, xmlNsPtr ns, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000446 while (ns != NULL) {
447 xmlDebugDumpNamespace(output, ns, depth);
448 ns = ns->next;
449 }
450}
451
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000452static void
453xmlDebugDumpEntity(FILE *output, xmlEntityPtr ent, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000454 int i;
455 char shift[100];
456
457 for (i = 0;((i < depth) && (i < 25));i++)
458 shift[2 * i] = shift[2 * i + 1] = ' ';
459 shift[2 * i] = shift[2 * i + 1] = 0;
460
461 fprintf(output, shift);
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000462
463 if (ent == NULL) {
464 fprintf(output, "Entity is NULL\n");
465 return;
466 }
Owen Taylor3473f882001-02-23 17:55:21 +0000467 switch (ent->etype) {
468 case XML_INTERNAL_GENERAL_ENTITY:
469 fprintf(output, "INTERNAL_GENERAL_ENTITY ");
470 break;
471 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
472 fprintf(output, "EXTERNAL_GENERAL_PARSED_ENTITY ");
473 break;
474 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
475 fprintf(output, "EXTERNAL_GENERAL_UNPARSED_ENTITY ");
476 break;
477 case XML_INTERNAL_PARAMETER_ENTITY:
478 fprintf(output, "INTERNAL_PARAMETER_ENTITY ");
479 break;
480 case XML_EXTERNAL_PARAMETER_ENTITY:
481 fprintf(output, "EXTERNAL_PARAMETER_ENTITY ");
482 break;
483 default:
484 fprintf(output, "ENTITY_%d ! ", ent->etype);
485 }
486 fprintf(output, "%s\n", ent->name);
487 if (ent->ExternalID) {
488 fprintf(output, shift);
489 fprintf(output, "ExternalID=%s\n", ent->ExternalID);
490 }
491 if (ent->SystemID) {
492 fprintf(output, shift);
493 fprintf(output, "SystemID=%s\n", ent->SystemID);
494 }
495 if (ent->URI) {
496 fprintf(output, shift);
497 fprintf(output, "URI=%s\n", ent->URI);
498 }
499 if (ent->content) {
500 fprintf(output, shift);
501 fprintf(output, "content=");
502 xmlDebugDumpString(output, ent->content);
503 fprintf(output, "\n");
504 }
505}
506
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000507/**
508 * xmlDebugDumpAttr:
509 * @output: the FILE * for the output
510 * @attr: the attribute
511 * @depth: the indentation level.
512 *
513 * Dumps debug information for the attribute
514 */
515void
516xmlDebugDumpAttr(FILE *output, xmlAttrPtr attr, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000517 int i;
518 char shift[100];
519
520 for (i = 0;((i < depth) && (i < 25));i++)
521 shift[2 * i] = shift[2 * i + 1] = ' ';
522 shift[2 * i] = shift[2 * i + 1] = 0;
523
524 fprintf(output, shift);
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000525
526 if (attr == NULL) {
527 fprintf(output, "Attr is NULL");
528 return;
529 }
Owen Taylor3473f882001-02-23 17:55:21 +0000530 fprintf(output, "ATTRIBUTE ");
531 xmlDebugDumpString(output, attr->name);
532 fprintf(output, "\n");
533 if (attr->children != NULL)
534 xmlDebugDumpNodeList(output, attr->children, depth + 1);
535
536 /*
537 * Do a bit of checking
538 */
539 if (attr->parent == NULL)
540 fprintf(output, "PBM: Attr has no parent\n");
541 if (attr->doc == NULL)
542 fprintf(output, "PBM: Attr has no doc\n");
543 if ((attr->parent != NULL) && (attr->doc != attr->parent->doc))
544 fprintf(output, "PBM: Attr doc differs from parent's one\n");
545 if (attr->prev == NULL) {
546 if ((attr->parent != NULL) && (attr->parent->properties != attr))
547 fprintf(output, "PBM: Attr has no prev and not first of list\n");
548 } else {
549 if (attr->prev->next != attr)
550 fprintf(output, "PBM: Attr prev->next : back link wrong\n");
551 }
552 if (attr->next != NULL) {
553 if (attr->next->prev != attr)
554 fprintf(output, "PBM: Attr next->prev : forward link wrong\n");
555 }
556}
557
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000558/**
559 * xmlDebugDumpAttrList:
560 * @output: the FILE * for the output
561 * @attr: the attribute list
562 * @depth: the indentation level.
563 *
564 * Dumps debug information for the attribute list
565 */
566void
567xmlDebugDumpAttrList(FILE * output, xmlAttrPtr attr, int depth)
568{
Daniel Veillard7db38712002-02-07 16:39:11 +0000569 if (output == NULL)
570 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +0000571 while (attr != NULL) {
572 xmlDebugDumpAttr(output, attr, depth);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000573 attr = attr->next;
Owen Taylor3473f882001-02-23 17:55:21 +0000574 }
575}
576
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000577/**
578 * xmlDebugDumpOneNode:
579 * @output: the FILE * for the output
580 * @node: the node
581 * @depth: the indentation level.
582 *
583 * Dumps debug information for the element node, it is not recursive
584 */
585void
586xmlDebugDumpOneNode(FILE * output, xmlNodePtr node, int depth)
587{
Owen Taylor3473f882001-02-23 17:55:21 +0000588 int i;
589 char shift[100];
590
Daniel Veillard7db38712002-02-07 16:39:11 +0000591 if (output == NULL)
592 output = stdout;
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000593 for (i = 0; ((i < depth) && (i < 25)); i++)
Owen Taylor3473f882001-02-23 17:55:21 +0000594 shift[2 * i] = shift[2 * i + 1] = ' ';
595 shift[2 * i] = shift[2 * i + 1] = 0;
596
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000597 if (node == NULL) {
598 fprintf(output, shift);
599 fprintf(output, "node is NULL\n");
600 return;
601 }
Owen Taylor3473f882001-02-23 17:55:21 +0000602 switch (node->type) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000603 case XML_ELEMENT_NODE:
604 fprintf(output, shift);
605 fprintf(output, "ELEMENT ");
606 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
607 xmlDebugDumpString(output, node->ns->prefix);
608 fprintf(output, ":");
609 }
610 xmlDebugDumpString(output, node->name);
611 fprintf(output, "\n");
612 break;
613 case XML_ATTRIBUTE_NODE:
614 fprintf(output, shift);
615 fprintf(output, "Error, ATTRIBUTE found here\n");
616 break;
617 case XML_TEXT_NODE:
618 fprintf(output, shift);
Daniel Veillardb44025c2001-10-11 22:55:55 +0000619 if (node->name == (const xmlChar *) xmlStringTextNoenc)
Daniel Veillard567e1b42001-08-01 15:53:47 +0000620 fprintf(output, "TEXT no enc\n");
621 else
622 fprintf(output, "TEXT\n");
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000623 break;
624 case XML_CDATA_SECTION_NODE:
625 fprintf(output, shift);
626 fprintf(output, "CDATA_SECTION\n");
627 break;
628 case XML_ENTITY_REF_NODE:
629 fprintf(output, shift);
630 fprintf(output, "ENTITY_REF(%s)\n", node->name);
631 break;
632 case XML_ENTITY_NODE:
633 fprintf(output, shift);
634 fprintf(output, "ENTITY\n");
635 break;
636 case XML_PI_NODE:
637 fprintf(output, shift);
638 fprintf(output, "PI %s\n", node->name);
639 break;
640 case XML_COMMENT_NODE:
641 fprintf(output, shift);
642 fprintf(output, "COMMENT\n");
643 break;
644 case XML_DOCUMENT_NODE:
645 case XML_HTML_DOCUMENT_NODE:
646 fprintf(output, shift);
647 fprintf(output, "Error, DOCUMENT found here\n");
648 break;
649 case XML_DOCUMENT_TYPE_NODE:
650 fprintf(output, shift);
651 fprintf(output, "DOCUMENT_TYPE\n");
652 break;
653 case XML_DOCUMENT_FRAG_NODE:
654 fprintf(output, shift);
655 fprintf(output, "DOCUMENT_FRAG\n");
656 break;
657 case XML_NOTATION_NODE:
658 fprintf(output, shift);
659 fprintf(output, "NOTATION\n");
660 break;
661 case XML_DTD_NODE:
662 xmlDebugDumpDtdNode(output, (xmlDtdPtr) node, depth);
663 return;
664 case XML_ELEMENT_DECL:
665 xmlDebugDumpElemDecl(output, (xmlElementPtr) node, depth);
666 return;
667 case XML_ATTRIBUTE_DECL:
668 xmlDebugDumpAttrDecl(output, (xmlAttributePtr) node, depth);
669 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000670 case XML_ENTITY_DECL:
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000671 xmlDebugDumpEntityDecl(output, (xmlEntityPtr) node, depth);
672 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000673 case XML_NAMESPACE_DECL:
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000674 xmlDebugDumpNamespace(output, (xmlNsPtr) node, depth);
675 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000676 case XML_XINCLUDE_START:
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000677 fprintf(output, shift);
678 fprintf(output, "INCLUDE START\n");
679 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000680 case XML_XINCLUDE_END:
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000681 fprintf(output, shift);
682 fprintf(output, "INCLUDE END\n");
683 return;
684 default:
685 fprintf(output, shift);
686 fprintf(output, "NODE_%d !!!\n", node->type);
687 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000688 }
689 if (node->doc == NULL) {
690 fprintf(output, shift);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000691 fprintf(output, "doc == NULL !!!\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000692 }
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000693 if (node->nsDef != NULL)
Owen Taylor3473f882001-02-23 17:55:21 +0000694 xmlDebugDumpNamespaceList(output, node->nsDef, depth + 1);
695 if (node->properties != NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000696 xmlDebugDumpAttrList(output, node->properties, depth + 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000697 if (node->type != XML_ENTITY_REF_NODE) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000698 if ((node->type != XML_ELEMENT_NODE) && (node->content != NULL)) {
699 shift[2 * i] = shift[2 * i + 1] = ' ';
700 shift[2 * i + 2] = shift[2 * i + 3] = 0;
701 fprintf(output, shift);
702 fprintf(output, "content=");
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000703 xmlDebugDumpString(output, node->content);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000704 fprintf(output, "\n");
705 }
Owen Taylor3473f882001-02-23 17:55:21 +0000706 } else {
707 xmlEntityPtr ent;
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000708
709 ent = xmlGetDocEntity(node->doc, node->name);
710 if (ent != NULL)
711 xmlDebugDumpEntity(output, ent, depth + 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000712 }
713 /*
714 * Do a bit of checking
715 */
716 if (node->parent == NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000717 fprintf(output, "PBM: Node has no parent\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000718 if (node->doc == NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000719 fprintf(output, "PBM: Node has no doc\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000720 if ((node->parent != NULL) && (node->doc != node->parent->doc))
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000721 fprintf(output, "PBM: Node doc differs from parent's one\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000722 if (node->prev == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000723 if ((node->parent != NULL) && (node->parent->children != node))
724 fprintf(output,
725 "PBM: Node has no prev and not first of list\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000726 } else {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000727 if (node->prev->next != node)
728 fprintf(output, "PBM: Node prev->next : back link wrong\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000729 }
730 if (node->next == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000731 if ((node->parent != NULL) && (node->parent->last != node))
732 fprintf(output,
733 "PBM: Node has no next and not last of list\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000734 } else {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000735 if (node->next->prev != node)
736 fprintf(output, "PBM: Node next->prev : forward link wrong\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000737 }
738}
739
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000740/**
741 * xmlDebugDumpNode:
742 * @output: the FILE * for the output
743 * @node: the node
744 * @depth: the indentation level.
745 *
746 * Dumps debug information for the element node, it is recursive
747 */
748void
749xmlDebugDumpNode(FILE * output, xmlNodePtr node, int depth)
750{
Daniel Veillard7db38712002-02-07 16:39:11 +0000751 if (output == NULL)
752 output = stdout;
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000753 if (node == NULL) {
754 int i;
755 char shift[100];
756
757 for (i = 0; ((i < depth) && (i < 25)); i++)
758 shift[2 * i] = shift[2 * i + 1] = ' ';
759 shift[2 * i] = shift[2 * i + 1] = 0;
760
761 fprintf(output, shift);
762 fprintf(output, "node is NULL\n");
763 return;
764 }
Owen Taylor3473f882001-02-23 17:55:21 +0000765 xmlDebugDumpOneNode(output, node, depth);
766 if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE))
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000767 xmlDebugDumpNodeList(output, node->children, depth + 1);
Owen Taylor3473f882001-02-23 17:55:21 +0000768}
769
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000770/**
771 * xmlDebugDumpNodeList:
772 * @output: the FILE * for the output
773 * @node: the node list
774 * @depth: the indentation level.
775 *
776 * Dumps debug information for the list of element node, it is recursive
777 */
778void
779xmlDebugDumpNodeList(FILE * output, xmlNodePtr node, int depth)
780{
Daniel Veillard7db38712002-02-07 16:39:11 +0000781 if (output == NULL)
782 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +0000783 while (node != NULL) {
784 xmlDebugDumpNode(output, node, depth);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000785 node = node->next;
Owen Taylor3473f882001-02-23 17:55:21 +0000786 }
787}
788
789
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000790/**
791 * xmlDebugDumpDocumentHead:
792 * @output: the FILE * for the output
793 * @doc: the document
794 *
795 * Dumps debug information cncerning the document, not recursive
796 */
797void
798xmlDebugDumpDocumentHead(FILE * output, xmlDocPtr doc)
799{
800 if (output == NULL)
801 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +0000802 if (doc == NULL) {
803 fprintf(output, "DOCUMENT == NULL !\n");
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000804 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000805 }
806
807 switch (doc->type) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000808 case XML_ELEMENT_NODE:
809 fprintf(output, "Error, ELEMENT found here ");
810 break;
811 case XML_ATTRIBUTE_NODE:
812 fprintf(output, "Error, ATTRIBUTE found here\n");
813 break;
814 case XML_TEXT_NODE:
815 fprintf(output, "Error, TEXT\n");
816 break;
817 case XML_CDATA_SECTION_NODE:
818 fprintf(output, "Error, CDATA_SECTION\n");
819 break;
820 case XML_ENTITY_REF_NODE:
821 fprintf(output, "Error, ENTITY_REF\n");
822 break;
823 case XML_ENTITY_NODE:
824 fprintf(output, "Error, ENTITY\n");
825 break;
826 case XML_PI_NODE:
827 fprintf(output, "Error, PI\n");
828 break;
829 case XML_COMMENT_NODE:
830 fprintf(output, "Error, COMMENT\n");
831 break;
832 case XML_DOCUMENT_NODE:
833 fprintf(output, "DOCUMENT\n");
834 break;
835 case XML_HTML_DOCUMENT_NODE:
836 fprintf(output, "HTML DOCUMENT\n");
837 break;
838 case XML_DOCUMENT_TYPE_NODE:
839 fprintf(output, "Error, DOCUMENT_TYPE\n");
840 break;
841 case XML_DOCUMENT_FRAG_NODE:
842 fprintf(output, "Error, DOCUMENT_FRAG\n");
843 break;
844 case XML_NOTATION_NODE:
845 fprintf(output, "Error, NOTATION\n");
846 break;
847 default:
848 fprintf(output, "NODE_%d\n", doc->type);
Owen Taylor3473f882001-02-23 17:55:21 +0000849 }
850 if (doc->name != NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000851 fprintf(output, "name=");
Owen Taylor3473f882001-02-23 17:55:21 +0000852 xmlDebugDumpString(output, BAD_CAST doc->name);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000853 fprintf(output, "\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000854 }
855 if (doc->version != NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000856 fprintf(output, "version=");
Owen Taylor3473f882001-02-23 17:55:21 +0000857 xmlDebugDumpString(output, doc->version);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000858 fprintf(output, "\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000859 }
860 if (doc->encoding != NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000861 fprintf(output, "encoding=");
Owen Taylor3473f882001-02-23 17:55:21 +0000862 xmlDebugDumpString(output, doc->encoding);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000863 fprintf(output, "\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000864 }
865 if (doc->URL != NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000866 fprintf(output, "URL=");
Owen Taylor3473f882001-02-23 17:55:21 +0000867 xmlDebugDumpString(output, doc->URL);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000868 fprintf(output, "\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000869 }
870 if (doc->standalone)
871 fprintf(output, "standalone=true\n");
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000872 if (doc->oldNs != NULL)
Owen Taylor3473f882001-02-23 17:55:21 +0000873 xmlDebugDumpNamespaceList(output, doc->oldNs, 0);
874}
875
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000876/**
877 * xmlDebugDumpDocument:
878 * @output: the FILE * for the output
879 * @doc: the document
880 *
881 * Dumps debug information for the document, it's recursive
882 */
883void
884xmlDebugDumpDocument(FILE * output, xmlDocPtr doc)
885{
886 if (output == NULL)
887 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +0000888 if (doc == NULL) {
889 fprintf(output, "DOCUMENT == NULL !\n");
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000890 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000891 }
892 xmlDebugDumpDocumentHead(output, doc);
893 if (((doc->type == XML_DOCUMENT_NODE) ||
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000894 (doc->type == XML_HTML_DOCUMENT_NODE)) && (doc->children != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +0000895 xmlDebugDumpNodeList(output, doc->children, 1);
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000896}
Owen Taylor3473f882001-02-23 17:55:21 +0000897
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000898/**
899 * xmlDebugDumpDTD:
900 * @output: the FILE * for the output
901 * @dtd: the DTD
902 *
903 * Dumps debug information for the DTD
904 */
905void
906xmlDebugDumpDTD(FILE * output, xmlDtdPtr dtd)
907{
Daniel Veillard7db38712002-02-07 16:39:11 +0000908 if (output == NULL)
909 output = stdout;
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000910 if (dtd == NULL) {
911 fprintf(output, "DTD is NULL\n");
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000912 return;
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000913 }
Owen Taylor3473f882001-02-23 17:55:21 +0000914 if (dtd->type != XML_DTD_NODE) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000915 fprintf(output, "PBM: not a DTD\n");
916 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000917 }
918 if (dtd->name != NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000919 fprintf(output, "DTD(%s)", dtd->name);
Owen Taylor3473f882001-02-23 17:55:21 +0000920 else
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000921 fprintf(output, "DTD");
Owen Taylor3473f882001-02-23 17:55:21 +0000922 if (dtd->ExternalID != NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000923 fprintf(output, ", PUBLIC %s", dtd->ExternalID);
Owen Taylor3473f882001-02-23 17:55:21 +0000924 if (dtd->SystemID != NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000925 fprintf(output, ", SYSTEM %s", dtd->SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +0000926 fprintf(output, "\n");
927 /*
928 * Do a bit of checking
929 */
930 if ((dtd->parent != NULL) && (dtd->doc != dtd->parent->doc))
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000931 fprintf(output, "PBM: DTD doc differs from parent's one\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000932 if (dtd->prev == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000933 if ((dtd->parent != NULL)
934 && (dtd->parent->children != (xmlNodePtr) dtd))
935 fprintf(output,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000936 "PBM: DTD has no prev and not first of list\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000937 } else {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000938 if (dtd->prev->next != (xmlNodePtr) dtd)
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000939 fprintf(output, "PBM: DTD prev->next : back link wrong\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000940 }
941 if (dtd->next == NULL) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000942 if ((dtd->parent != NULL)
943 && (dtd->parent->last != (xmlNodePtr) dtd))
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000944 fprintf(output, "PBM: DTD has no next and not last of list\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000945 } else {
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000946 if (dtd->next->prev != (xmlNodePtr) dtd)
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000947 fprintf(output, "PBM: DTD next->prev : forward link wrong\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000948 }
949 if (dtd->children == NULL)
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000950 fprintf(output, " DTD is empty\n");
Owen Taylor3473f882001-02-23 17:55:21 +0000951 else
952 xmlDebugDumpNodeList(output, dtd->children, 1);
953}
954
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000955static void
956xmlDebugDumpEntityCallback(xmlEntityPtr cur, FILE *output) {
Daniel Veillard5e926fa2002-01-22 21:44:25 +0000957 if (cur == NULL) {
958 fprintf(output, "Entity is NULL");
959 return;
960 }
Owen Taylor3473f882001-02-23 17:55:21 +0000961 fprintf(output, "%s : ", cur->name);
962 switch (cur->etype) {
963 case XML_INTERNAL_GENERAL_ENTITY:
964 fprintf(output, "INTERNAL GENERAL, ");
965 break;
966 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
967 fprintf(output, "EXTERNAL PARSED, ");
968 break;
969 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
970 fprintf(output, "EXTERNAL UNPARSED, ");
971 break;
972 case XML_INTERNAL_PARAMETER_ENTITY:
973 fprintf(output, "INTERNAL PARAMETER, ");
974 break;
975 case XML_EXTERNAL_PARAMETER_ENTITY:
976 fprintf(output, "EXTERNAL PARAMETER, ");
977 break;
978 default:
979 fprintf(output, "UNKNOWN TYPE %d",
980 cur->etype);
981 }
982 if (cur->ExternalID != NULL)
983 fprintf(output, "ID \"%s\"", cur->ExternalID);
984 if (cur->SystemID != NULL)
985 fprintf(output, "SYSTEM \"%s\"", cur->SystemID);
986 if (cur->orig != NULL)
987 fprintf(output, "\n orig \"%s\"", cur->orig);
Daniel Veillard7db37732001-07-12 01:20:08 +0000988 if ((cur->type != XML_ELEMENT_NODE) &&
989 (cur->content != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +0000990 fprintf(output, "\n content \"%s\"", cur->content);
991 fprintf(output, "\n");
992}
993
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000994/**
995 * xmlDebugDumpEntities:
996 * @output: the FILE * for the output
997 * @doc: the document
998 *
999 * Dumps debug information for all the entities in use by the document
1000 */
1001void
1002xmlDebugDumpEntities(FILE * output, xmlDocPtr doc)
1003{
1004 if (output == NULL)
1005 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00001006 if (doc == NULL) {
1007 fprintf(output, "DOCUMENT == NULL !\n");
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001008 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001009 }
1010
1011 switch (doc->type) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001012 case XML_ELEMENT_NODE:
1013 fprintf(output, "Error, ELEMENT found here ");
1014 break;
1015 case XML_ATTRIBUTE_NODE:
1016 fprintf(output, "Error, ATTRIBUTE found here\n");
1017 break;
1018 case XML_TEXT_NODE:
1019 fprintf(output, "Error, TEXT\n");
1020 break;
1021 case XML_CDATA_SECTION_NODE:
1022 fprintf(output, "Error, CDATA_SECTION\n");
1023 break;
1024 case XML_ENTITY_REF_NODE:
1025 fprintf(output, "Error, ENTITY_REF\n");
1026 break;
1027 case XML_ENTITY_NODE:
1028 fprintf(output, "Error, ENTITY\n");
1029 break;
1030 case XML_PI_NODE:
1031 fprintf(output, "Error, PI\n");
1032 break;
1033 case XML_COMMENT_NODE:
1034 fprintf(output, "Error, COMMENT\n");
1035 break;
1036 case XML_DOCUMENT_NODE:
1037 fprintf(output, "DOCUMENT\n");
1038 break;
1039 case XML_HTML_DOCUMENT_NODE:
1040 fprintf(output, "HTML DOCUMENT\n");
1041 break;
1042 case XML_DOCUMENT_TYPE_NODE:
1043 fprintf(output, "Error, DOCUMENT_TYPE\n");
1044 break;
1045 case XML_DOCUMENT_FRAG_NODE:
1046 fprintf(output, "Error, DOCUMENT_FRAG\n");
1047 break;
1048 case XML_NOTATION_NODE:
1049 fprintf(output, "Error, NOTATION\n");
1050 break;
1051 default:
1052 fprintf(output, "NODE_%d\n", doc->type);
Owen Taylor3473f882001-02-23 17:55:21 +00001053 }
1054 if ((doc->intSubset != NULL) && (doc->intSubset->entities != NULL)) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001055 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1056 doc->intSubset->entities;
1057
1058 fprintf(output, "Entities in internal subset\n");
1059 xmlHashScan(table, (xmlHashScanner) xmlDebugDumpEntityCallback,
1060 output);
Owen Taylor3473f882001-02-23 17:55:21 +00001061 } else
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001062 fprintf(output, "No entities in internal subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001063 if ((doc->extSubset != NULL) && (doc->extSubset->entities != NULL)) {
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001064 xmlEntitiesTablePtr table = (xmlEntitiesTablePtr)
1065 doc->extSubset->entities;
1066
1067 fprintf(output, "Entities in external subset\n");
1068 xmlHashScan(table, (xmlHashScanner) xmlDebugDumpEntityCallback,
1069 output);
Owen Taylor3473f882001-02-23 17:55:21 +00001070 } else
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001071 fprintf(output, "No entities in external subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001072}
1073
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001074/**
1075 * xmlLsCountNode:
1076 * @node: the node to count
1077 *
1078 * Count the children of @node.
1079 *
1080 * Returns the number of children of @node.
1081 */
1082int
1083xmlLsCountNode(xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00001084 int ret = 0;
1085 xmlNodePtr list = NULL;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001086
1087 if (node == NULL)
1088 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001089
1090 switch (node->type) {
1091 case XML_ELEMENT_NODE:
1092 list = node->children;
1093 break;
1094 case XML_DOCUMENT_NODE:
1095 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00001096#ifdef LIBXML_DOCB_ENABLED
1097 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00001098#endif
1099 list = ((xmlDocPtr) node)->children;
1100 break;
1101 case XML_ATTRIBUTE_NODE:
1102 list = ((xmlAttrPtr) node)->children;
1103 break;
1104 case XML_TEXT_NODE:
1105 case XML_CDATA_SECTION_NODE:
1106 case XML_PI_NODE:
1107 case XML_COMMENT_NODE:
1108 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001109 ret = xmlStrlen(node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001110 }
1111 break;
1112 case XML_ENTITY_REF_NODE:
1113 case XML_DOCUMENT_TYPE_NODE:
1114 case XML_ENTITY_NODE:
1115 case XML_DOCUMENT_FRAG_NODE:
1116 case XML_NOTATION_NODE:
1117 case XML_DTD_NODE:
1118 case XML_ELEMENT_DECL:
1119 case XML_ATTRIBUTE_DECL:
1120 case XML_ENTITY_DECL:
1121 case XML_NAMESPACE_DECL:
1122 case XML_XINCLUDE_START:
1123 case XML_XINCLUDE_END:
1124 ret = 1;
1125 break;
1126 }
1127 for (;list != NULL;ret++)
1128 list = list->next;
1129 return(ret);
1130}
1131
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001132/**
1133 * xmlLsOneNode:
1134 * @output: the FILE * for the output
1135 * @node: the node to dump
1136 *
1137 * Dump to @output the type and name of @node.
1138 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001139void
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001140xmlLsOneNode(FILE *output, xmlNodePtr node) {
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001141 if (node == NULL) {
1142 fprintf(output, "NULL\n");
1143 return;
1144 }
Owen Taylor3473f882001-02-23 17:55:21 +00001145 switch (node->type) {
1146 case XML_ELEMENT_NODE:
1147 fprintf(output, "-");
1148 break;
1149 case XML_ATTRIBUTE_NODE:
1150 fprintf(output, "a");
1151 break;
1152 case XML_TEXT_NODE:
1153 fprintf(output, "t");
1154 break;
1155 case XML_CDATA_SECTION_NODE:
Daniel Veillard75be0132002-03-13 10:03:35 +00001156 fprintf(output, "C");
Owen Taylor3473f882001-02-23 17:55:21 +00001157 break;
1158 case XML_ENTITY_REF_NODE:
1159 fprintf(output, "e");
1160 break;
1161 case XML_ENTITY_NODE:
1162 fprintf(output, "E");
1163 break;
1164 case XML_PI_NODE:
1165 fprintf(output, "p");
1166 break;
1167 case XML_COMMENT_NODE:
1168 fprintf(output, "c");
1169 break;
1170 case XML_DOCUMENT_NODE:
1171 fprintf(output, "d");
1172 break;
1173 case XML_HTML_DOCUMENT_NODE:
1174 fprintf(output, "h");
1175 break;
1176 case XML_DOCUMENT_TYPE_NODE:
1177 fprintf(output, "T");
1178 break;
1179 case XML_DOCUMENT_FRAG_NODE:
1180 fprintf(output, "F");
1181 break;
1182 case XML_NOTATION_NODE:
1183 fprintf(output, "N");
1184 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001185 case XML_NAMESPACE_DECL:
1186 fprintf(output, "n");
1187 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001188 default:
1189 fprintf(output, "?");
1190 }
Daniel Veillarde6a55192002-01-14 17:11:53 +00001191 if (node->type != XML_NAMESPACE_DECL) {
1192 if (node->properties != NULL)
1193 fprintf(output, "a");
1194 else
1195 fprintf(output, "-");
1196 if (node->nsDef != NULL)
1197 fprintf(output, "n");
1198 else
1199 fprintf(output, "-");
1200 }
Owen Taylor3473f882001-02-23 17:55:21 +00001201
1202 fprintf(output, " %8d ", xmlLsCountNode(node));
1203
1204 switch (node->type) {
1205 case XML_ELEMENT_NODE:
1206 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001207 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001208 break;
1209 case XML_ATTRIBUTE_NODE:
1210 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001211 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001212 break;
1213 case XML_TEXT_NODE:
1214 if (node->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001215 xmlDebugDumpString(output, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001216 }
1217 break;
1218 case XML_CDATA_SECTION_NODE:
1219 break;
1220 case XML_ENTITY_REF_NODE:
1221 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001222 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001223 break;
1224 case XML_ENTITY_NODE:
1225 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001226 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001227 break;
1228 case XML_PI_NODE:
1229 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001230 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001231 break;
1232 case XML_COMMENT_NODE:
1233 break;
1234 case XML_DOCUMENT_NODE:
1235 break;
1236 case XML_HTML_DOCUMENT_NODE:
1237 break;
1238 case XML_DOCUMENT_TYPE_NODE:
1239 break;
1240 case XML_DOCUMENT_FRAG_NODE:
1241 break;
1242 case XML_NOTATION_NODE:
1243 break;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001244 case XML_NAMESPACE_DECL: {
1245 xmlNsPtr ns = (xmlNsPtr) node;
1246
1247 if (ns->prefix == NULL)
1248 fprintf(output, "default -> %s", ns->href);
1249 else
1250 fprintf(output, "%s -> %s", ns->prefix, ns->href);
1251 break;
1252 }
Owen Taylor3473f882001-02-23 17:55:21 +00001253 default:
1254 if (node->name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001255 fprintf(output, "%s", (const char *) node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001256 }
1257 fprintf(output, "\n");
1258}
1259
Daniel Veillard78d12092001-10-11 09:12:24 +00001260/**
1261 * xmlBoolToText:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001262 * @boolval: a bool to turn into text
Daniel Veillard78d12092001-10-11 09:12:24 +00001263 *
1264 * Convenient way to turn bool into text
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001265 *
1266 * Returns a pointer to either "True" or "False"
1267 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001268const char *
Daniel Veillardebd38c52001-11-01 08:38:12 +00001269xmlBoolToText(int boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001270{
Daniel Veillardebd38c52001-11-01 08:38:12 +00001271 if (boolval)
Daniel Veillard78d12092001-10-11 09:12:24 +00001272 return("True");
1273 else
1274 return("False");
1275}
1276
Owen Taylor3473f882001-02-23 17:55:21 +00001277/****************************************************************
1278 * *
1279 * The XML shell related functions *
1280 * *
1281 ****************************************************************/
1282
Daniel Veillard78d12092001-10-11 09:12:24 +00001283
1284
Owen Taylor3473f882001-02-23 17:55:21 +00001285/*
1286 * TODO: Improvement/cleanups for the XML shell
1287 * - allow to shell out an editor on a subpart
1288 * - cleanup function registrations (with help) and calling
1289 * - provide registration routines
1290 */
1291
1292/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001293 * xmlShellPrintXPathError:
Daniel Veillard78d12092001-10-11 09:12:24 +00001294 * @errorType: valid xpath error id
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001295 * @arg: the argument that cause xpath to fail
Daniel Veillard78d12092001-10-11 09:12:24 +00001296 *
1297 * Print the xpath error to libxml default error channel
1298 */
1299void
1300xmlShellPrintXPathError(int errorType, const char *arg)
1301{
1302 const char *default_arg = "Result";
1303
1304 if (!arg)
1305 arg = default_arg;
1306
1307 switch (errorType) {
1308 case XPATH_UNDEFINED:
1309 xmlGenericError(xmlGenericErrorContext,
1310 "%s: no such node\n", arg);
1311 break;
1312
1313 case XPATH_BOOLEAN:
1314 xmlGenericError(xmlGenericErrorContext,
1315 "%s is a Boolean\n", arg);
1316 break;
1317 case XPATH_NUMBER:
1318 xmlGenericError(xmlGenericErrorContext,
1319 "%s is a number\n", arg);
1320 break;
1321 case XPATH_STRING:
1322 xmlGenericError(xmlGenericErrorContext,
1323 "%s is a string\n", arg);
1324 break;
1325 case XPATH_POINT:
1326 xmlGenericError(xmlGenericErrorContext,
1327 "%s is a point\n", arg);
1328 break;
1329 case XPATH_RANGE:
1330 xmlGenericError(xmlGenericErrorContext,
1331 "%s is a range\n", arg);
1332 break;
1333 case XPATH_LOCATIONSET:
1334 xmlGenericError(xmlGenericErrorContext,
1335 "%s is a range\n", arg);
1336 break;
1337 case XPATH_USERS:
1338 xmlGenericError(xmlGenericErrorContext,
1339 "%s is user-defined\n", arg);
1340 break;
1341 case XPATH_XSLT_TREE:
1342 xmlGenericError(xmlGenericErrorContext,
1343 "%s is an XSLT value tree\n", arg);
1344 break;
1345 }
1346 xmlGenericError(xmlGenericErrorContext,
1347 "Try casting the result string function (xpath builtin)\n",
1348 arg);
1349}
1350
1351
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001352#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001353/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001354 * xmlShellPrintNodeCtxt:
1355 * @ctxt : a non-null shell context
1356 * @node : a non-null node to print to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001357 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001358 * Print node to the output FILE
1359 */
1360static void
1361xmlShellPrintNodeCtxt(xmlShellCtxtPtr ctxt,xmlNodePtr node)
1362{
Daniel Veillard01992e02002-10-09 10:20:30 +00001363 FILE *fp;
1364
1365 if (!node)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001366 return;
Daniel Veillard01992e02002-10-09 10:20:30 +00001367 if (ctxt == NULL)
1368 fp = stdout;
1369 else
1370 fp = ctxt->output;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001371
1372 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001373 xmlDocDump(fp, (xmlDocPtr) node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001374 else if (node->type == XML_ATTRIBUTE_NODE)
Daniel Veillard01992e02002-10-09 10:20:30 +00001375 xmlDebugDumpAttrList(fp, (xmlAttrPtr) node, 0);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001376 else
Daniel Veillard01992e02002-10-09 10:20:30 +00001377 xmlElemDump(fp, node->doc, node);
Daniel Veillard321be0c2002-10-08 21:26:42 +00001378
Daniel Veillard01992e02002-10-09 10:20:30 +00001379 fprintf(fp, "\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00001380}
1381
1382/**
1383 * xmlShellPrintNode:
1384 * @node : a non-null node to print to the output FILE
1385 *
1386 * Print node to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001387 */
1388void
1389xmlShellPrintNode(xmlNodePtr node)
1390{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001391 xmlShellPrintNodeCtxt(NULL, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001392}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001393#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001394
Daniel Veillard78d12092001-10-11 09:12:24 +00001395/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001396 * xmlShellPrintXPathResultCtxt:
1397 * @ctxt: a valid shell context
Daniel Veillard9d06d302002-01-22 18:15:52 +00001398 * @list: a valid result generated by an xpath evaluation
Daniel Veillard78d12092001-10-11 09:12:24 +00001399 *
Daniel Veillard321be0c2002-10-08 21:26:42 +00001400 * Prints result to the output FILE
Daniel Veillard78d12092001-10-11 09:12:24 +00001401 */
Daniel Veillard321be0c2002-10-08 21:26:42 +00001402static void
1403xmlShellPrintXPathResultCtxt(xmlShellCtxtPtr ctxt,xmlXPathObjectPtr list)
Daniel Veillard78d12092001-10-11 09:12:24 +00001404{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001405 if (!ctxt)
1406 return;
Daniel Veillard78d12092001-10-11 09:12:24 +00001407
1408 if (list != NULL) {
1409 switch (list->type) {
1410 case XPATH_NODESET:{
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001411#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001412 int indx;
1413
1414 if (list->nodesetval) {
1415 for (indx = 0; indx < list->nodesetval->nodeNr;
1416 indx++) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001417 xmlShellPrintNodeCtxt(ctxt,
1418 list->nodesetval->nodeTab[indx]);
Daniel Veillard78d12092001-10-11 09:12:24 +00001419 }
1420 } else {
1421 xmlGenericError(xmlGenericErrorContext,
1422 "Empty node set\n");
1423 }
1424 break;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001425#else
1426 xmlGenericError(xmlGenericErrorContext,
1427 "Node set\n");
1428#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001429 }
1430 case XPATH_BOOLEAN:
1431 xmlGenericError(xmlGenericErrorContext,
1432 "Is a Boolean:%s\n",
1433 xmlBoolToText(list->boolval));
1434 break;
1435 case XPATH_NUMBER:
1436 xmlGenericError(xmlGenericErrorContext,
1437 "Is a number:%0g\n", list->floatval);
1438 break;
1439 case XPATH_STRING:
1440 xmlGenericError(xmlGenericErrorContext,
1441 "Is a string:%s\n", list->stringval);
1442 break;
1443
1444 default:
1445 xmlShellPrintXPathError(list->type, NULL);
1446 }
1447 }
1448}
1449
1450/**
Daniel Veillard321be0c2002-10-08 21:26:42 +00001451 * xmlShellPrintXPathResult:
1452 * @list: a valid result generated by an xpath evaluation
1453 *
1454 * Prints result to the output FILE
1455 */
1456void
1457xmlShellPrintXPathResult(xmlXPathObjectPtr list)
1458{
1459 xmlShellPrintXPathResultCtxt(NULL, list);
1460}
1461
1462/**
Owen Taylor3473f882001-02-23 17:55:21 +00001463 * xmlShellList:
1464 * @ctxt: the shell context
1465 * @arg: unused
1466 * @node: a node
1467 * @node2: unused
1468 *
1469 * Implements the XML shell function "ls"
1470 * Does an Unix like listing of the given node (like a directory)
1471 *
1472 * Returns 0
1473 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001474int
Daniel Veillard321be0c2002-10-08 21:26:42 +00001475xmlShellList(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00001476 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1477 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1478{
Owen Taylor3473f882001-02-23 17:55:21 +00001479 xmlNodePtr cur;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001480 if (!ctxt)
1481 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001482 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001483 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001484 return (0);
1485 }
Owen Taylor3473f882001-02-23 17:55:21 +00001486 if ((node->type == XML_DOCUMENT_NODE) ||
1487 (node->type == XML_HTML_DOCUMENT_NODE)) {
1488 cur = ((xmlDocPtr) node)->children;
Daniel Veillarde6a55192002-01-14 17:11:53 +00001489 } else if (node->type == XML_NAMESPACE_DECL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001490 xmlLsOneNode(ctxt->output, node);
Daniel Veillarde6a55192002-01-14 17:11:53 +00001491 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001492 } else if (node->children != NULL) {
1493 cur = node->children;
1494 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001495 xmlLsOneNode(ctxt->output, node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001496 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001497 }
1498 while (cur != NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001499 xmlLsOneNode(ctxt->output, cur);
Daniel Veillard78d12092001-10-11 09:12:24 +00001500 cur = cur->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001501 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001502 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001503}
1504
1505/**
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001506 * xmlShellBase:
1507 * @ctxt: the shell context
1508 * @arg: unused
1509 * @node: a node
1510 * @node2: unused
1511 *
1512 * Implements the XML shell function "base"
1513 * dumps the current XML base of the node
1514 *
1515 * Returns 0
1516 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001517int
Daniel Veillard321be0c2002-10-08 21:26:42 +00001518xmlShellBase(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00001519 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1520 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1521{
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001522 xmlChar *base;
Daniel Veillard321be0c2002-10-08 21:26:42 +00001523 if (!ctxt)
1524 return 0;
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001525 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001526 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001527 return (0);
1528 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001529
1530 base = xmlNodeGetBase(node->doc, node);
1531
1532 if (base == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001533 fprintf(ctxt->output, " No base found !!!\n");
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001534 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001535 fprintf(ctxt->output, "%s\n", base);
Daniel Veillard78d12092001-10-11 09:12:24 +00001536 xmlFree(base);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001537 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001538 return (0);
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001539}
1540
1541/**
Daniel Veillardcfa0d812002-01-17 08:46:58 +00001542 * xmlShellSetBase:
1543 * @ctxt: the shell context
1544 * @arg: the new base
1545 * @node: a node
1546 * @node2: unused
1547 *
1548 * Implements the XML shell function "setbase"
1549 * change the current XML base of the node
1550 *
1551 * Returns 0
1552 */
1553static int
1554xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1555 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1556 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1557{
1558 xmlNodeSetBase(node, (xmlChar*) arg);
1559 return (0);
1560}
1561
1562/**
Daniel Veillard1e208222002-10-22 14:25:25 +00001563 * xmlShellGrep:
1564 * @ctxt: the shell context
1565 * @arg: the string or regular expression to find
1566 * @node: a node
1567 * @node2: unused
1568 *
1569 * Implements the XML shell function "grep"
1570 * dumps informations about the node (namespace, attributes, content).
1571 *
1572 * Returns 0
1573 */
Daniel Veillarde645e8c2002-10-22 17:35:37 +00001574static int
Daniel Veillard1e208222002-10-22 14:25:25 +00001575xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1576 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
1577{
1578 if (!ctxt)
1579 return (0);
1580 if (node == NULL)
1581 return (0);
1582 if (arg == NULL)
1583 return (0);
1584#ifdef LIBXML_REGEXP_ENABLED
1585 if ((xmlStrchr((xmlChar *) arg, '?')) ||
1586 (xmlStrchr((xmlChar *) arg, '*')) ||
1587 (xmlStrchr((xmlChar *) arg, '.')) ||
1588 (xmlStrchr((xmlChar *) arg, '['))) {
1589 }
1590#endif
1591 while (node != NULL) {
1592 if (node->type == XML_COMMENT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00001593 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00001594
1595 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
1596 xmlShellList(ctxt, NULL, node, NULL);
1597 }
1598 } else if (node->type == XML_TEXT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00001599 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00001600
1601 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
Daniel Veillarde645e8c2002-10-22 17:35:37 +00001602 xmlShellList(ctxt, NULL, node->parent, NULL);
Daniel Veillard1e208222002-10-22 14:25:25 +00001603 }
1604 }
1605
1606 /*
1607 * Browse the full subtree, deep first
1608 */
1609
1610 if ((node->type == XML_DOCUMENT_NODE) ||
1611 (node->type == XML_HTML_DOCUMENT_NODE)) {
1612 node = ((xmlDocPtr) node)->children;
1613 } else if ((node->children != NULL)
1614 && (node->type != XML_ENTITY_REF_NODE)) {
1615 /* deep first */
1616 node = node->children;
1617 } else if (node->next != NULL) {
1618 /* then siblings */
1619 node = node->next;
1620 } else {
1621 /* go up to parents->next if needed */
1622 while (node != NULL) {
1623 if (node->parent != NULL) {
1624 node = node->parent;
1625 }
1626 if (node->next != NULL) {
1627 node = node->next;
1628 break;
1629 }
1630 if (node->parent == NULL) {
1631 node = NULL;
1632 break;
1633 }
1634 }
1635 }
1636 }
1637 return (0);
1638}
1639
1640/**
Owen Taylor3473f882001-02-23 17:55:21 +00001641 * xmlShellDir:
1642 * @ctxt: the shell context
1643 * @arg: unused
1644 * @node: a node
1645 * @node2: unused
1646 *
1647 * Implements the XML shell function "dir"
1648 * dumps informations about the node (namespace, attributes, content).
1649 *
1650 * Returns 0
1651 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001652int
1653xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1654 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1655 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1656{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001657 if (!ctxt)
1658 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001659 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001660 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001661 return (0);
1662 }
Owen Taylor3473f882001-02-23 17:55:21 +00001663 if ((node->type == XML_DOCUMENT_NODE) ||
1664 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001665 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
Owen Taylor3473f882001-02-23 17:55:21 +00001666 } else if (node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001667 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001668 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001669 xmlDebugDumpOneNode(ctxt->output, node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001670 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001671 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001672}
1673
Daniel Veillard522bc602004-02-21 11:53:09 +00001674#ifdef LIBXML_SCHEMAS_ENABLED
1675/**
1676 * xmlShellRNGValidate:
1677 * @ctxt: the shell context
1678 * @schemas: the path to the Relax-NG schemas
1679 * @node: a node
1680 * @node2: unused
1681 *
1682 * Implements the XML shell function "relaxng"
1683 * validating the instance against a Relax-NG schemas
1684 *
1685 * Returns 0
1686 */
1687static int
1688xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
1689 xmlNodePtr node ATTRIBUTE_UNUSED,
1690 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1691{
1692 xmlRelaxNGPtr relaxngschemas;
1693 xmlRelaxNGParserCtxtPtr ctxt;
1694 xmlRelaxNGValidCtxtPtr vctxt;
1695 int ret;
1696
1697 ctxt = xmlRelaxNGNewParserCtxt(schemas);
1698 xmlRelaxNGSetParserErrors(ctxt,
1699 (xmlRelaxNGValidityErrorFunc) fprintf,
1700 (xmlRelaxNGValidityWarningFunc) fprintf,
1701 stderr);
1702 relaxngschemas = xmlRelaxNGParse(ctxt);
1703 xmlRelaxNGFreeParserCtxt(ctxt);
1704 if (relaxngschemas == NULL) {
1705 xmlGenericError(xmlGenericErrorContext,
1706 "Relax-NG schema %s failed to compile\n", schemas);
1707 return(-1);
1708 }
1709 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
1710 xmlRelaxNGSetValidErrors(vctxt,
1711 (xmlRelaxNGValidityErrorFunc) fprintf,
1712 (xmlRelaxNGValidityWarningFunc) fprintf,
1713 stderr);
1714 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
1715 if (ret == 0) {
1716 fprintf(stderr, "%s validates\n", sctxt->filename);
1717 } else if (ret > 0) {
1718 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
1719 } else {
1720 fprintf(stderr, "%s validation generated an internal error\n",
1721 sctxt->filename);
1722 }
1723 xmlRelaxNGFreeValidCtxt(vctxt);
1724 if (relaxngschemas != NULL)
1725 xmlRelaxNGFree(relaxngschemas);
1726 return(0);
1727}
1728#endif
1729
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001730#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001731/**
1732 * xmlShellCat:
1733 * @ctxt: the shell context
1734 * @arg: unused
1735 * @node: a node
1736 * @node2: unused
1737 *
1738 * Implements the XML shell function "cat"
1739 * dumps the serialization node content (XML or HTML).
1740 *
1741 * Returns 0
1742 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001743int
1744xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
1745 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
1746{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001747 if (!ctxt)
1748 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001749 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001750 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001751 return (0);
1752 }
Owen Taylor3473f882001-02-23 17:55:21 +00001753 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
1754#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001755 if (node->type == XML_HTML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001756 htmlDocDump(ctxt->output, (htmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001757 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00001758 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001759#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001760 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001761 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001762 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00001763 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001764#endif /* LIBXML_HTML_ENABLED */
1765 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001766 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001767 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001768 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00001769 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001770 }
Daniel Veillard321be0c2002-10-08 21:26:42 +00001771 fprintf(ctxt->output, "\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00001772 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001773}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001774#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001775
1776/**
1777 * xmlShellLoad:
1778 * @ctxt: the shell context
1779 * @filename: the file name
1780 * @node: unused
1781 * @node2: unused
1782 *
1783 * Implements the XML shell function "load"
1784 * loads a new document specified by the filename
1785 *
1786 * Returns 0 or -1 if loading failed
1787 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001788int
1789xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
1790 xmlNodePtr node ATTRIBUTE_UNUSED,
1791 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1792{
Owen Taylor3473f882001-02-23 17:55:21 +00001793 xmlDocPtr doc;
1794 int html = 0;
1795
1796 if (ctxt->doc != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001797 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00001798
1799 if (html) {
1800#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001801 doc = htmlParseFile(filename, NULL);
1802#else
Daniel Veillard321be0c2002-10-08 21:26:42 +00001803 fprintf(ctxt->output, "HTML support not compiled in\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00001804 doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001805#endif /* LIBXML_HTML_ENABLED */
1806 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001807 doc = xmlParseFile(filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001808 }
1809 if (doc != NULL) {
1810 if (ctxt->loaded == 1) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001811 xmlFreeDoc(ctxt->doc);
1812 }
1813 ctxt->loaded = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001814#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001815 xmlXPathFreeContext(ctxt->pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001816#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001817 xmlFree(ctxt->filename);
1818 ctxt->doc = doc;
1819 ctxt->node = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001820#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001821 ctxt->pctxt = xmlXPathNewContext(doc);
Owen Taylor3473f882001-02-23 17:55:21 +00001822#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard85095e22003-04-23 13:56:44 +00001823 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001824 } else
Daniel Veillard78d12092001-10-11 09:12:24 +00001825 return (-1);
1826 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001827}
1828
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001829#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001830/**
1831 * xmlShellWrite:
1832 * @ctxt: the shell context
1833 * @filename: the file name
1834 * @node: a node in the tree
1835 * @node2: unused
1836 *
1837 * Implements the XML shell function "write"
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001838 * Write the current node to the filename, it saves the serialization
Owen Taylor3473f882001-02-23 17:55:21 +00001839 * of the subtree under the @node specified
1840 *
1841 * Returns 0 or -1 in case of error
1842 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001843int
Owen Taylor3473f882001-02-23 17:55:21 +00001844xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Daniel Veillard78d12092001-10-11 09:12:24 +00001845 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1846{
Owen Taylor3473f882001-02-23 17:55:21 +00001847 if (node == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001848 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001849 if ((filename == NULL) || (filename[0] == 0)) {
1850 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001851 "Write command requires a filename argument\n");
1852 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001853 }
1854#ifdef W_OK
1855 if (access((char *) filename, W_OK)) {
1856 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001857 "Cannot write to %s\n", filename);
1858 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001859 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001860#endif
1861 switch (node->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00001862 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00001863 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
1864 xmlGenericError(xmlGenericErrorContext,
1865 "Failed to write to %s\n", filename);
1866 return (-1);
1867 }
1868 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001869 case XML_HTML_DOCUMENT_NODE:
1870#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001871 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
1872 xmlGenericError(xmlGenericErrorContext,
1873 "Failed to write to %s\n", filename);
1874 return (-1);
1875 }
Owen Taylor3473f882001-02-23 17:55:21 +00001876#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001877 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
1878 xmlGenericError(xmlGenericErrorContext,
1879 "Failed to write to %s\n", filename);
1880 return (-1);
1881 }
Owen Taylor3473f882001-02-23 17:55:21 +00001882#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001883 break;
1884 default:{
1885 FILE *f;
Owen Taylor3473f882001-02-23 17:55:21 +00001886
Daniel Veillard78d12092001-10-11 09:12:24 +00001887 f = fopen((char *) filename, "w");
1888 if (f == NULL) {
1889 xmlGenericError(xmlGenericErrorContext,
1890 "Failed to write to %s\n", filename);
1891 return (-1);
1892 }
1893 xmlElemDump(f, ctxt->doc, node);
1894 fclose(f);
1895 }
Owen Taylor3473f882001-02-23 17:55:21 +00001896 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001897 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001898}
1899
1900/**
1901 * xmlShellSave:
1902 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001903 * @filename: the file name (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00001904 * @node: unused
1905 * @node2: unused
1906 *
1907 * Implements the XML shell function "save"
1908 * Write the current document to the filename, or it's original name
1909 *
1910 * Returns 0 or -1 in case of error
1911 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001912int
1913xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
1914 xmlNodePtr node ATTRIBUTE_UNUSED,
1915 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1916{
Owen Taylor3473f882001-02-23 17:55:21 +00001917 if (ctxt->doc == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001918 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001919 if ((filename == NULL) || (filename[0] == 0))
1920 filename = ctxt->filename;
1921#ifdef W_OK
1922 if (access((char *) filename, W_OK)) {
1923 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001924 "Cannot save to %s\n", filename);
1925 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001926 }
1927#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00001928 switch (ctxt->doc->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00001929 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00001930 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
1931 xmlGenericError(xmlGenericErrorContext,
1932 "Failed to save to %s\n", filename);
1933 }
1934 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001935 case XML_HTML_DOCUMENT_NODE:
1936#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001937 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
1938 xmlGenericError(xmlGenericErrorContext,
1939 "Failed to save to %s\n", filename);
1940 }
Owen Taylor3473f882001-02-23 17:55:21 +00001941#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001942 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
1943 xmlGenericError(xmlGenericErrorContext,
1944 "Failed to save to %s\n", filename);
1945 }
Owen Taylor3473f882001-02-23 17:55:21 +00001946#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001947 break;
1948 default:
1949 xmlGenericError(xmlGenericErrorContext,
1950 "To save to subparts of a document use the 'write' command\n");
1951 return (-1);
1952
Owen Taylor3473f882001-02-23 17:55:21 +00001953 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001954 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001955}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001956#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001957
Daniel Veillardf54cd532004-02-25 11:52:31 +00001958#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001959/**
1960 * xmlShellValidate:
1961 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001962 * @dtd: the DTD URI (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00001963 * @node: unused
1964 * @node2: unused
1965 *
1966 * Implements the XML shell function "validate"
1967 * Validate the document, if a DTD path is provided, then the validation
1968 * is done against the given DTD.
1969 *
1970 * Returns 0 or -1 in case of error
1971 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001972int
1973xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
1974 xmlNodePtr node ATTRIBUTE_UNUSED,
1975 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1976{
Owen Taylor3473f882001-02-23 17:55:21 +00001977 xmlValidCtxt vctxt;
1978 int res = -1;
1979
1980 vctxt.userData = stderr;
1981 vctxt.error = (xmlValidityErrorFunc) fprintf;
1982 vctxt.warning = (xmlValidityWarningFunc) fprintf;
1983
1984 if ((dtd == NULL) || (dtd[0] == 0)) {
1985 res = xmlValidateDocument(&vctxt, ctxt->doc);
1986 } else {
1987 xmlDtdPtr subset;
1988
Daniel Veillard78d12092001-10-11 09:12:24 +00001989 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
1990 if (subset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001991 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
1992
Daniel Veillard78d12092001-10-11 09:12:24 +00001993 xmlFreeDtd(subset);
1994 }
Owen Taylor3473f882001-02-23 17:55:21 +00001995 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001996 return (res);
Owen Taylor3473f882001-02-23 17:55:21 +00001997}
Daniel Veillardf54cd532004-02-25 11:52:31 +00001998#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001999
2000/**
2001 * xmlShellDu:
2002 * @ctxt: the shell context
2003 * @arg: unused
2004 * @tree: a node defining a subtree
2005 * @node2: unused
2006 *
2007 * Implements the XML shell function "du"
2008 * show the structure of the subtree under node @tree
2009 * If @tree is null, the command works on the current node.
2010 *
2011 * Returns 0 or -1 in case of error
2012 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002013int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002014xmlShellDu(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002015 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2016 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2017{
Owen Taylor3473f882001-02-23 17:55:21 +00002018 xmlNodePtr node;
Daniel Veillard78d12092001-10-11 09:12:24 +00002019 int indent = 0, i;
Owen Taylor3473f882001-02-23 17:55:21 +00002020
Daniel Veillard321be0c2002-10-08 21:26:42 +00002021 if (!ctxt)
2022 return (-1);
2023
Daniel Veillard78d12092001-10-11 09:12:24 +00002024 if (tree == NULL)
2025 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002026 node = tree;
2027 while (node != NULL) {
2028 if ((node->type == XML_DOCUMENT_NODE) ||
2029 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002030 fprintf(ctxt->output, "/\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002031 } else if (node->type == XML_ELEMENT_NODE) {
2032 for (i = 0; i < indent; i++)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002033 fprintf(ctxt->output, " ");
2034 fprintf(ctxt->output, "%s\n", node->name);
Daniel Veillard78d12092001-10-11 09:12:24 +00002035 } else {
2036 }
Owen Taylor3473f882001-02-23 17:55:21 +00002037
Daniel Veillard78d12092001-10-11 09:12:24 +00002038 /*
2039 * Browse the full subtree, deep first
2040 */
Owen Taylor3473f882001-02-23 17:55:21 +00002041
2042 if ((node->type == XML_DOCUMENT_NODE) ||
2043 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002044 node = ((xmlDocPtr) node)->children;
2045 } else if ((node->children != NULL)
2046 && (node->type != XML_ENTITY_REF_NODE)) {
2047 /* deep first */
2048 node = node->children;
2049 indent++;
2050 } else if ((node != tree) && (node->next != NULL)) {
2051 /* then siblings */
2052 node = node->next;
2053 } else if (node != tree) {
2054 /* go up to parents->next if needed */
2055 while (node != tree) {
2056 if (node->parent != NULL) {
2057 node = node->parent;
2058 indent--;
2059 }
2060 if ((node != tree) && (node->next != NULL)) {
2061 node = node->next;
2062 break;
2063 }
2064 if (node->parent == NULL) {
2065 node = NULL;
2066 break;
2067 }
2068 if (node == tree) {
2069 node = NULL;
2070 break;
2071 }
2072 }
2073 /* exit condition */
2074 if (node == tree)
2075 node = NULL;
2076 } else
2077 node = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002078 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002079 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002080}
2081
2082/**
2083 * xmlShellPwd:
2084 * @ctxt: the shell context
2085 * @buffer: the output buffer
Daniel Veillard9d06d302002-01-22 18:15:52 +00002086 * @node: a node
Owen Taylor3473f882001-02-23 17:55:21 +00002087 * @node2: unused
2088 *
2089 * Implements the XML shell function "pwd"
2090 * Show the full path from the root to the node, if needed building
2091 * thumblers when similar elements exists at a given ancestor level.
2092 * The output is compatible with XPath commands.
2093 *
2094 * Returns 0 or -1 in case of error
2095 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002096int
2097xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2098 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2099{
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002100 xmlChar *path;
Owen Taylor3473f882001-02-23 17:55:21 +00002101
Daniel Veillard78d12092001-10-11 09:12:24 +00002102 if (node == NULL)
2103 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002104
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002105 path = xmlGetNodePath(node);
2106 if (path == NULL)
2107 return (-1);
2108
2109 /*
2110 * This test prevents buffer overflow, because this routine
2111 * is only called by xmlShell, in which the second argument is
2112 * 500 chars long.
2113 * It is a dirty hack before a cleaner solution is found.
2114 * Documentation should mention that the second argument must
2115 * be at least 500 chars long, and could be stripped if too long.
2116 */
2117 snprintf(buffer, 499, "%s", path);
2118 buffer[499] = '0';
2119 xmlFree(path);
2120
Daniel Veillard78d12092001-10-11 09:12:24 +00002121 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002122}
2123
2124/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002125 * xmlShell:
Owen Taylor3473f882001-02-23 17:55:21 +00002126 * @doc: the initial document
2127 * @filename: the output buffer
2128 * @input: the line reading function
Daniel Veillard321be0c2002-10-08 21:26:42 +00002129 * @output: the output FILE*, defaults to stdout if NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002130 *
2131 * Implements the XML shell
2132 * This allow to load, validate, view, modify and save a document
2133 * using a environment similar to a UNIX commandline.
2134 */
2135void
2136xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Daniel Veillard78d12092001-10-11 09:12:24 +00002137 FILE * output)
2138{
Owen Taylor3473f882001-02-23 17:55:21 +00002139 char prompt[500] = "/ > ";
2140 char *cmdline = NULL, *cur;
2141 int nbargs;
2142 char command[100];
2143 char arg[400];
2144 int i;
2145 xmlShellCtxtPtr ctxt;
2146 xmlXPathObjectPtr list;
2147
2148 if (doc == NULL)
2149 return;
2150 if (filename == NULL)
2151 return;
2152 if (input == NULL)
2153 return;
2154 if (output == NULL)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002155 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00002156 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Daniel Veillard78d12092001-10-11 09:12:24 +00002157 if (ctxt == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00002158 return;
2159 ctxt->loaded = 0;
2160 ctxt->doc = doc;
2161 ctxt->input = input;
2162 ctxt->output = output;
2163 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Daniel Veillard78d12092001-10-11 09:12:24 +00002164 ctxt->node = (xmlNodePtr) ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002165
2166#ifdef LIBXML_XPATH_ENABLED
2167 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2168 if (ctxt->pctxt == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002169 xmlFree(ctxt);
2170 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002171 }
2172#endif /* LIBXML_XPATH_ENABLED */
2173 while (1) {
2174 if (ctxt->node == (xmlNodePtr) ctxt->doc)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002175 snprintf(prompt, sizeof(prompt), "%s > ", "/");
Daniel Veillard7a985a12003-07-06 17:57:42 +00002176 else if ((ctxt->node != NULL) && (ctxt->node->name))
Daniel Veillard78d12092001-10-11 09:12:24 +00002177 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002178 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002179 snprintf(prompt, sizeof(prompt), "? > ");
Owen Taylor3473f882001-02-23 17:55:21 +00002180 prompt[sizeof(prompt) - 1] = 0;
2181
Daniel Veillard78d12092001-10-11 09:12:24 +00002182 /*
2183 * Get a new command line
2184 */
Owen Taylor3473f882001-02-23 17:55:21 +00002185 cmdline = ctxt->input(prompt);
Daniel Veillard78d12092001-10-11 09:12:24 +00002186 if (cmdline == NULL)
2187 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002188
Daniel Veillard78d12092001-10-11 09:12:24 +00002189 /*
2190 * Parse the command itself
2191 */
2192 cur = cmdline;
2193 nbargs = 0;
2194 while ((*cur == ' ') || (*cur == '\t'))
2195 cur++;
2196 i = 0;
2197 while ((*cur != ' ') && (*cur != '\t') &&
2198 (*cur != '\n') && (*cur != '\r')) {
2199 if (*cur == 0)
2200 break;
2201 command[i++] = *cur++;
2202 }
2203 command[i] = 0;
2204 if (i == 0)
2205 continue;
2206 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002207
Daniel Veillard78d12092001-10-11 09:12:24 +00002208 /*
2209 * Parse the argument
2210 */
2211 while ((*cur == ' ') || (*cur == '\t'))
2212 cur++;
2213 i = 0;
2214 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2215 if (*cur == 0)
2216 break;
2217 arg[i++] = *cur++;
2218 }
2219 arg[i] = 0;
2220 if (i != 0)
2221 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002222
Daniel Veillard78d12092001-10-11 09:12:24 +00002223 /*
2224 * start interpreting the command
2225 */
Owen Taylor3473f882001-02-23 17:55:21 +00002226 if (!strcmp(command, "exit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002227 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002228 if (!strcmp(command, "quit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002229 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002230 if (!strcmp(command, "bye"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002231 break;
Daniel Veillard5004f422001-11-08 13:53:05 +00002232 if (!strcmp(command, "help")) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002233 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2234 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2235 fprintf(ctxt->output, "\tbye leave shell\n");
2236 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2237 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2238 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2239 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2240 fprintf(ctxt->output, "\texit leave shell\n");
2241 fprintf(ctxt->output, "\thelp display this help\n");
2242 fprintf(ctxt->output, "\tfree display memory usage\n");
2243 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2244 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002245#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002246 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002247#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard321be0c2002-10-08 21:26:42 +00002248 fprintf(ctxt->output, "\tpwd display current working directory\n");
2249 fprintf(ctxt->output, "\tquit leave shell\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002250#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002251 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00002252 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002253#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf54cd532004-02-25 11:52:31 +00002254#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002255 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002256#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard522bc602004-02-21 11:53:09 +00002257#ifdef LIBXML_SCHEMAS_ENABLED
2258 fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n");
2259#endif
Daniel Veillard1e208222002-10-22 14:25:25 +00002260 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002261#ifdef LIBXML_VALID_ENABLED
Daniel Veillard5004f422001-11-08 13:53:05 +00002262 } else if (!strcmp(command, "validate")) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002263 xmlShellValidate(ctxt, arg, NULL, NULL);
Daniel Veillardf54cd532004-02-25 11:52:31 +00002264#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002265 } else if (!strcmp(command, "load")) {
2266 xmlShellLoad(ctxt, arg, NULL, NULL);
Daniel Veillard522bc602004-02-21 11:53:09 +00002267#ifdef LIBXML_SCHEMAS_ENABLED
2268 } else if (!strcmp(command, "relaxng")) {
2269 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2270#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002271#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002272 } else if (!strcmp(command, "save")) {
2273 xmlShellSave(ctxt, arg, NULL, NULL);
2274 } else if (!strcmp(command, "write")) {
2275 xmlShellWrite(ctxt, arg, NULL, NULL);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002276#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard1e208222002-10-22 14:25:25 +00002277 } else if (!strcmp(command, "grep")) {
2278 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
Daniel Veillard78d12092001-10-11 09:12:24 +00002279 } else if (!strcmp(command, "free")) {
2280 if (arg[0] == 0) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002281 xmlMemShow(ctxt->output, 0);
Daniel Veillard78d12092001-10-11 09:12:24 +00002282 } else {
2283 int len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002284
Daniel Veillard78d12092001-10-11 09:12:24 +00002285 sscanf(arg, "%d", &len);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002286 xmlMemShow(ctxt->output, len);
Daniel Veillard78d12092001-10-11 09:12:24 +00002287 }
2288 } else if (!strcmp(command, "pwd")) {
2289 char dir[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002290
Daniel Veillard78d12092001-10-11 09:12:24 +00002291 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
Daniel Veillard321be0c2002-10-08 21:26:42 +00002292 fprintf(ctxt->output, "%s\n", dir);
Daniel Veillard78d12092001-10-11 09:12:24 +00002293 } else if (!strcmp(command, "du")) {
2294 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2295 } else if (!strcmp(command, "base")) {
2296 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002297#ifdef LIBXML_XPATH_ENABLED
2298 } else if (!strcmp(command, "xpath")) {
2299 if (arg[0] == 0) {
2300 xmlGenericError(xmlGenericErrorContext,
2301 "xpath: expression required\n");
2302 } else {
2303 ctxt->pctxt->node = ctxt->node;
2304 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002305 xmlXPathDebugDumpObject(ctxt->output, list, 0);
Daniel Veillard2070c482002-01-22 22:12:19 +00002306 xmlXPathFreeObject(list);
2307 }
2308#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002309 } else if (!strcmp(command, "setbase")) {
2310 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
Daniel Veillard78d12092001-10-11 09:12:24 +00002311 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
2312 int dir = (!strcmp(command, "dir"));
2313
2314 if (arg[0] == 0) {
2315 if (dir)
2316 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
2317 else
2318 xmlShellList(ctxt, NULL, ctxt->node, NULL);
2319 } else {
2320 ctxt->pctxt->node = ctxt->node;
Daniel Veillard61d80a22001-04-27 17:13:01 +00002321#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002322 ctxt->pctxt->node = ctxt->node;
2323 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2324#else
2325 list = NULL;
2326#endif /* LIBXML_XPATH_ENABLED */
2327 if (list != NULL) {
2328 switch (list->type) {
2329 case XPATH_UNDEFINED:
2330 xmlGenericError(xmlGenericErrorContext,
2331 "%s: no such node\n", arg);
2332 break;
2333 case XPATH_NODESET:{
2334 int indx;
2335
Daniel Veillarda6825e82001-11-07 13:33:59 +00002336 if (list->nodesetval == NULL)
2337 break;
2338
Daniel Veillard78d12092001-10-11 09:12:24 +00002339 for (indx = 0;
2340 indx < list->nodesetval->nodeNr;
2341 indx++) {
2342 if (dir)
2343 xmlShellDir(ctxt, NULL,
2344 list->nodesetval->
2345 nodeTab[indx], NULL);
2346 else
2347 xmlShellList(ctxt, NULL,
2348 list->nodesetval->
2349 nodeTab[indx], NULL);
2350 }
2351 break;
2352 }
2353 case XPATH_BOOLEAN:
2354 xmlGenericError(xmlGenericErrorContext,
2355 "%s is a Boolean\n", arg);
2356 break;
2357 case XPATH_NUMBER:
2358 xmlGenericError(xmlGenericErrorContext,
2359 "%s is a number\n", arg);
2360 break;
2361 case XPATH_STRING:
2362 xmlGenericError(xmlGenericErrorContext,
2363 "%s is a string\n", arg);
2364 break;
2365 case XPATH_POINT:
2366 xmlGenericError(xmlGenericErrorContext,
2367 "%s is a point\n", arg);
2368 break;
2369 case XPATH_RANGE:
2370 xmlGenericError(xmlGenericErrorContext,
2371 "%s is a range\n", arg);
2372 break;
2373 case XPATH_LOCATIONSET:
2374 xmlGenericError(xmlGenericErrorContext,
2375 "%s is a range\n", arg);
2376 break;
2377 case XPATH_USERS:
2378 xmlGenericError(xmlGenericErrorContext,
2379 "%s is user-defined\n", arg);
2380 break;
2381 case XPATH_XSLT_TREE:
2382 xmlGenericError(xmlGenericErrorContext,
2383 "%s is an XSLT value tree\n",
2384 arg);
2385 break;
2386 }
2387#ifdef LIBXML_XPATH_ENABLED
2388 xmlXPathFreeObject(list);
Daniel Veillard61d80a22001-04-27 17:13:01 +00002389#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002390 } else {
2391 xmlGenericError(xmlGenericErrorContext,
2392 "%s: no such node\n", arg);
2393 }
2394 ctxt->pctxt->node = NULL;
2395 }
2396 } else if (!strcmp(command, "cd")) {
2397 if (arg[0] == 0) {
2398 ctxt->node = (xmlNodePtr) ctxt->doc;
2399 } else {
2400#ifdef LIBXML_XPATH_ENABLED
2401 ctxt->pctxt->node = ctxt->node;
2402 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2403#else
2404 list = NULL;
2405#endif /* LIBXML_XPATH_ENABLED */
2406 if (list != NULL) {
2407 switch (list->type) {
2408 case XPATH_UNDEFINED:
2409 xmlGenericError(xmlGenericErrorContext,
2410 "%s: no such node\n", arg);
2411 break;
2412 case XPATH_NODESET:
Daniel Veillarda6825e82001-11-07 13:33:59 +00002413 if (list->nodesetval != NULL) {
2414 if (list->nodesetval->nodeNr == 1) {
2415 ctxt->node = list->nodesetval->nodeTab[0];
Daniel Veillard7a985a12003-07-06 17:57:42 +00002416 if ((ctxt->node != NULL) &&
2417 (ctxt->node->type ==
2418 XML_NAMESPACE_DECL)) {
2419 xmlGenericError(xmlGenericErrorContext,
2420 "cannot cd to namespace\n");
2421 ctxt->node = NULL;
2422 }
Daniel Veillarda6825e82001-11-07 13:33:59 +00002423 } else
2424 xmlGenericError(xmlGenericErrorContext,
2425 "%s is a %d Node Set\n",
2426 arg,
2427 list->nodesetval->nodeNr);
Daniel Veillard78d12092001-10-11 09:12:24 +00002428 } else
2429 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda6825e82001-11-07 13:33:59 +00002430 "%s is an empty Node Set\n",
2431 arg);
Daniel Veillard78d12092001-10-11 09:12:24 +00002432 break;
2433 case XPATH_BOOLEAN:
2434 xmlGenericError(xmlGenericErrorContext,
2435 "%s is a Boolean\n", arg);
2436 break;
2437 case XPATH_NUMBER:
2438 xmlGenericError(xmlGenericErrorContext,
2439 "%s is a number\n", arg);
2440 break;
2441 case XPATH_STRING:
2442 xmlGenericError(xmlGenericErrorContext,
2443 "%s is a string\n", arg);
2444 break;
2445 case XPATH_POINT:
2446 xmlGenericError(xmlGenericErrorContext,
2447 "%s is a point\n", arg);
2448 break;
2449 case XPATH_RANGE:
2450 xmlGenericError(xmlGenericErrorContext,
2451 "%s is a range\n", arg);
2452 break;
2453 case XPATH_LOCATIONSET:
2454 xmlGenericError(xmlGenericErrorContext,
2455 "%s is a range\n", arg);
2456 break;
2457 case XPATH_USERS:
2458 xmlGenericError(xmlGenericErrorContext,
2459 "%s is user-defined\n", arg);
2460 break;
2461 case XPATH_XSLT_TREE:
2462 xmlGenericError(xmlGenericErrorContext,
2463 "%s is an XSLT value tree\n",
2464 arg);
2465 break;
2466 }
2467#ifdef LIBXML_XPATH_ENABLED
2468 xmlXPathFreeObject(list);
2469#endif
2470 } else {
2471 xmlGenericError(xmlGenericErrorContext,
2472 "%s: no such node\n", arg);
2473 }
2474 ctxt->pctxt->node = NULL;
2475 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002476#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002477 } else if (!strcmp(command, "cat")) {
2478 if (arg[0] == 0) {
2479 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
2480 } else {
2481 ctxt->pctxt->node = ctxt->node;
2482#ifdef LIBXML_XPATH_ENABLED
2483 ctxt->pctxt->node = ctxt->node;
2484 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2485#else
2486 list = NULL;
2487#endif /* LIBXML_XPATH_ENABLED */
2488 if (list != NULL) {
2489 switch (list->type) {
2490 case XPATH_UNDEFINED:
2491 xmlGenericError(xmlGenericErrorContext,
2492 "%s: no such node\n", arg);
2493 break;
2494 case XPATH_NODESET:{
2495 int indx;
2496
Daniel Veillarda6825e82001-11-07 13:33:59 +00002497 if (list->nodesetval == NULL)
2498 break;
2499
Daniel Veillard78d12092001-10-11 09:12:24 +00002500 for (indx = 0;
2501 indx < list->nodesetval->nodeNr;
2502 indx++) {
2503 if (i > 0)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002504 fprintf(ctxt->output, " -------\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002505 xmlShellCat(ctxt, NULL,
2506 list->nodesetval->
2507 nodeTab[indx], NULL);
2508 }
2509 break;
2510 }
2511 case XPATH_BOOLEAN:
2512 xmlGenericError(xmlGenericErrorContext,
2513 "%s is a Boolean\n", arg);
2514 break;
2515 case XPATH_NUMBER:
2516 xmlGenericError(xmlGenericErrorContext,
2517 "%s is a number\n", arg);
2518 break;
2519 case XPATH_STRING:
2520 xmlGenericError(xmlGenericErrorContext,
2521 "%s is a string\n", arg);
2522 break;
2523 case XPATH_POINT:
2524 xmlGenericError(xmlGenericErrorContext,
2525 "%s is a point\n", arg);
2526 break;
2527 case XPATH_RANGE:
2528 xmlGenericError(xmlGenericErrorContext,
2529 "%s is a range\n", arg);
2530 break;
2531 case XPATH_LOCATIONSET:
2532 xmlGenericError(xmlGenericErrorContext,
2533 "%s is a range\n", arg);
2534 break;
2535 case XPATH_USERS:
2536 xmlGenericError(xmlGenericErrorContext,
2537 "%s is user-defined\n", arg);
2538 break;
2539 case XPATH_XSLT_TREE:
2540 xmlGenericError(xmlGenericErrorContext,
2541 "%s is an XSLT value tree\n",
2542 arg);
2543 break;
2544 }
2545#ifdef LIBXML_XPATH_ENABLED
2546 xmlXPathFreeObject(list);
2547#endif
2548 } else {
2549 xmlGenericError(xmlGenericErrorContext,
2550 "%s: no such node\n", arg);
2551 }
2552 ctxt->pctxt->node = NULL;
2553 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002554#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002555 } else {
2556 xmlGenericError(xmlGenericErrorContext,
2557 "Unknown command %s\n", command);
2558 }
2559 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00002560 }
2561#ifdef LIBXML_XPATH_ENABLED
2562 xmlXPathFreeContext(ctxt->pctxt);
2563#endif /* LIBXML_XPATH_ENABLED */
2564 if (ctxt->loaded) {
2565 xmlFreeDoc(ctxt->doc);
2566 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002567 if (ctxt->filename != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002568 xmlFree(ctxt->filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002569 xmlFree(ctxt);
2570 if (cmdline != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002571 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00002572}
2573
2574#endif /* LIBXML_DEBUG_ENABLED */