blob: 0da87e5a06cd3d389edc090ace7bf378c8c6925a [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
Daniel Veillard2156d432004-03-04 15:59:36 +00001541#ifdef XML_TREE_ENABLED
Daniel Veillardb8c9be92001-07-09 16:01:19 +00001542/**
Daniel Veillardcfa0d812002-01-17 08:46:58 +00001543 * xmlShellSetBase:
1544 * @ctxt: the shell context
1545 * @arg: the new base
1546 * @node: a node
1547 * @node2: unused
1548 *
1549 * Implements the XML shell function "setbase"
1550 * change the current XML base of the node
1551 *
1552 * Returns 0
1553 */
1554static int
1555xmlShellSetBase(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1556 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1557 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1558{
1559 xmlNodeSetBase(node, (xmlChar*) arg);
1560 return (0);
1561}
Daniel Veillard2156d432004-03-04 15:59:36 +00001562#endif
Daniel Veillardcfa0d812002-01-17 08:46:58 +00001563
1564/**
Daniel Veillard1e208222002-10-22 14:25:25 +00001565 * xmlShellGrep:
1566 * @ctxt: the shell context
1567 * @arg: the string or regular expression to find
1568 * @node: a node
1569 * @node2: unused
1570 *
1571 * Implements the XML shell function "grep"
1572 * dumps informations about the node (namespace, attributes, content).
1573 *
1574 * Returns 0
1575 */
Daniel Veillarde645e8c2002-10-22 17:35:37 +00001576static int
Daniel Veillard1e208222002-10-22 14:25:25 +00001577xmlShellGrep(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1578 char *arg, xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
1579{
1580 if (!ctxt)
1581 return (0);
1582 if (node == NULL)
1583 return (0);
1584 if (arg == NULL)
1585 return (0);
1586#ifdef LIBXML_REGEXP_ENABLED
1587 if ((xmlStrchr((xmlChar *) arg, '?')) ||
1588 (xmlStrchr((xmlChar *) arg, '*')) ||
1589 (xmlStrchr((xmlChar *) arg, '.')) ||
1590 (xmlStrchr((xmlChar *) arg, '['))) {
1591 }
1592#endif
1593 while (node != NULL) {
1594 if (node->type == XML_COMMENT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00001595 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00001596
1597 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node));
1598 xmlShellList(ctxt, NULL, node, NULL);
1599 }
1600 } else if (node->type == XML_TEXT_NODE) {
Daniel Veillarde645e8c2002-10-22 17:35:37 +00001601 if (xmlStrstr(node->content, (xmlChar *) arg)) {
Daniel Veillard1e208222002-10-22 14:25:25 +00001602
1603 fprintf(ctxt->output, "%s : ", xmlGetNodePath(node->parent));
Daniel Veillarde645e8c2002-10-22 17:35:37 +00001604 xmlShellList(ctxt, NULL, node->parent, NULL);
Daniel Veillard1e208222002-10-22 14:25:25 +00001605 }
1606 }
1607
1608 /*
1609 * Browse the full subtree, deep first
1610 */
1611
1612 if ((node->type == XML_DOCUMENT_NODE) ||
1613 (node->type == XML_HTML_DOCUMENT_NODE)) {
1614 node = ((xmlDocPtr) node)->children;
1615 } else if ((node->children != NULL)
1616 && (node->type != XML_ENTITY_REF_NODE)) {
1617 /* deep first */
1618 node = node->children;
1619 } else if (node->next != NULL) {
1620 /* then siblings */
1621 node = node->next;
1622 } else {
1623 /* go up to parents->next if needed */
1624 while (node != NULL) {
1625 if (node->parent != NULL) {
1626 node = node->parent;
1627 }
1628 if (node->next != NULL) {
1629 node = node->next;
1630 break;
1631 }
1632 if (node->parent == NULL) {
1633 node = NULL;
1634 break;
1635 }
1636 }
1637 }
1638 }
1639 return (0);
1640}
1641
1642/**
Owen Taylor3473f882001-02-23 17:55:21 +00001643 * xmlShellDir:
1644 * @ctxt: the shell context
1645 * @arg: unused
1646 * @node: a node
1647 * @node2: unused
1648 *
1649 * Implements the XML shell function "dir"
1650 * dumps informations about the node (namespace, attributes, content).
1651 *
1652 * Returns 0
1653 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001654int
1655xmlShellDir(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED,
1656 char *arg ATTRIBUTE_UNUSED, xmlNodePtr node,
1657 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1658{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001659 if (!ctxt)
1660 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001661 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001662 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001663 return (0);
1664 }
Owen Taylor3473f882001-02-23 17:55:21 +00001665 if ((node->type == XML_DOCUMENT_NODE) ||
1666 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001667 xmlDebugDumpDocumentHead(ctxt->output, (xmlDocPtr) node);
Owen Taylor3473f882001-02-23 17:55:21 +00001668 } else if (node->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001669 xmlDebugDumpAttr(ctxt->output, (xmlAttrPtr) node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001670 } else {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001671 xmlDebugDumpOneNode(ctxt->output, node, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00001672 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001673 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001674}
1675
Daniel Veillard522bc602004-02-21 11:53:09 +00001676#ifdef LIBXML_SCHEMAS_ENABLED
1677/**
1678 * xmlShellRNGValidate:
1679 * @ctxt: the shell context
1680 * @schemas: the path to the Relax-NG schemas
1681 * @node: a node
1682 * @node2: unused
1683 *
1684 * Implements the XML shell function "relaxng"
1685 * validating the instance against a Relax-NG schemas
1686 *
1687 * Returns 0
1688 */
1689static int
1690xmlShellRNGValidate(xmlShellCtxtPtr sctxt, char *schemas,
1691 xmlNodePtr node ATTRIBUTE_UNUSED,
1692 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1693{
1694 xmlRelaxNGPtr relaxngschemas;
1695 xmlRelaxNGParserCtxtPtr ctxt;
1696 xmlRelaxNGValidCtxtPtr vctxt;
1697 int ret;
1698
1699 ctxt = xmlRelaxNGNewParserCtxt(schemas);
1700 xmlRelaxNGSetParserErrors(ctxt,
1701 (xmlRelaxNGValidityErrorFunc) fprintf,
1702 (xmlRelaxNGValidityWarningFunc) fprintf,
1703 stderr);
1704 relaxngschemas = xmlRelaxNGParse(ctxt);
1705 xmlRelaxNGFreeParserCtxt(ctxt);
1706 if (relaxngschemas == NULL) {
1707 xmlGenericError(xmlGenericErrorContext,
1708 "Relax-NG schema %s failed to compile\n", schemas);
1709 return(-1);
1710 }
1711 vctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
1712 xmlRelaxNGSetValidErrors(vctxt,
1713 (xmlRelaxNGValidityErrorFunc) fprintf,
1714 (xmlRelaxNGValidityWarningFunc) fprintf,
1715 stderr);
1716 ret = xmlRelaxNGValidateDoc(vctxt, sctxt->doc);
1717 if (ret == 0) {
1718 fprintf(stderr, "%s validates\n", sctxt->filename);
1719 } else if (ret > 0) {
1720 fprintf(stderr, "%s fails to validate\n", sctxt->filename);
1721 } else {
1722 fprintf(stderr, "%s validation generated an internal error\n",
1723 sctxt->filename);
1724 }
1725 xmlRelaxNGFreeValidCtxt(vctxt);
1726 if (relaxngschemas != NULL)
1727 xmlRelaxNGFree(relaxngschemas);
1728 return(0);
1729}
1730#endif
1731
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001732#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001733/**
1734 * xmlShellCat:
1735 * @ctxt: the shell context
1736 * @arg: unused
1737 * @node: a node
1738 * @node2: unused
1739 *
1740 * Implements the XML shell function "cat"
1741 * dumps the serialization node content (XML or HTML).
1742 *
1743 * Returns 0
1744 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001745int
1746xmlShellCat(xmlShellCtxtPtr ctxt, char *arg ATTRIBUTE_UNUSED,
1747 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
1748{
Daniel Veillard321be0c2002-10-08 21:26:42 +00001749 if (!ctxt)
1750 return (0);
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001751 if (node == NULL) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00001752 fprintf(ctxt->output, "NULL\n");
Daniel Veillard5e926fa2002-01-22 21:44:25 +00001753 return (0);
1754 }
Owen Taylor3473f882001-02-23 17:55:21 +00001755 if (ctxt->doc->type == XML_HTML_DOCUMENT_NODE) {
1756#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001757 if (node->type == XML_HTML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001758 htmlDocDump(ctxt->output, (htmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001759 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00001760 htmlNodeDumpFile(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001761#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001762 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001763 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001764 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00001765 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001766#endif /* LIBXML_HTML_ENABLED */
1767 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001768 if (node->type == XML_DOCUMENT_NODE)
Daniel Veillard321be0c2002-10-08 21:26:42 +00001769 xmlDocDump(ctxt->output, (xmlDocPtr) node);
Daniel Veillard78d12092001-10-11 09:12:24 +00001770 else
Daniel Veillard321be0c2002-10-08 21:26:42 +00001771 xmlElemDump(ctxt->output, ctxt->doc, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001772 }
Daniel Veillard321be0c2002-10-08 21:26:42 +00001773 fprintf(ctxt->output, "\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00001774 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001775}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001776#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001777
1778/**
1779 * xmlShellLoad:
1780 * @ctxt: the shell context
1781 * @filename: the file name
1782 * @node: unused
1783 * @node2: unused
1784 *
1785 * Implements the XML shell function "load"
1786 * loads a new document specified by the filename
1787 *
1788 * Returns 0 or -1 if loading failed
1789 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001790int
1791xmlShellLoad(xmlShellCtxtPtr ctxt, char *filename,
1792 xmlNodePtr node ATTRIBUTE_UNUSED,
1793 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1794{
Owen Taylor3473f882001-02-23 17:55:21 +00001795 xmlDocPtr doc;
1796 int html = 0;
1797
1798 if (ctxt->doc != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001799 html = (ctxt->doc->type == XML_HTML_DOCUMENT_NODE);
Owen Taylor3473f882001-02-23 17:55:21 +00001800
1801 if (html) {
1802#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001803 doc = htmlParseFile(filename, NULL);
1804#else
Daniel Veillard321be0c2002-10-08 21:26:42 +00001805 fprintf(ctxt->output, "HTML support not compiled in\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00001806 doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001807#endif /* LIBXML_HTML_ENABLED */
1808 } else {
Daniel Veillard78d12092001-10-11 09:12:24 +00001809 doc = xmlParseFile(filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001810 }
1811 if (doc != NULL) {
1812 if (ctxt->loaded == 1) {
Daniel Veillard78d12092001-10-11 09:12:24 +00001813 xmlFreeDoc(ctxt->doc);
1814 }
1815 ctxt->loaded = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001816#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001817 xmlXPathFreeContext(ctxt->pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001818#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001819 xmlFree(ctxt->filename);
1820 ctxt->doc = doc;
1821 ctxt->node = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00001822#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001823 ctxt->pctxt = xmlXPathNewContext(doc);
Owen Taylor3473f882001-02-23 17:55:21 +00001824#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard85095e22003-04-23 13:56:44 +00001825 ctxt->filename = (char *) xmlCanonicPath((xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001826 } else
Daniel Veillard78d12092001-10-11 09:12:24 +00001827 return (-1);
1828 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001829}
1830
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001831#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001832/**
1833 * xmlShellWrite:
1834 * @ctxt: the shell context
1835 * @filename: the file name
1836 * @node: a node in the tree
1837 * @node2: unused
1838 *
1839 * Implements the XML shell function "write"
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001840 * Write the current node to the filename, it saves the serialization
Owen Taylor3473f882001-02-23 17:55:21 +00001841 * of the subtree under the @node specified
1842 *
1843 * Returns 0 or -1 in case of error
1844 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001845int
Owen Taylor3473f882001-02-23 17:55:21 +00001846xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node,
Daniel Veillard78d12092001-10-11 09:12:24 +00001847 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1848{
Owen Taylor3473f882001-02-23 17:55:21 +00001849 if (node == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001850 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001851 if ((filename == NULL) || (filename[0] == 0)) {
1852 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001853 "Write command requires a filename argument\n");
1854 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001855 }
1856#ifdef W_OK
1857 if (access((char *) filename, W_OK)) {
1858 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001859 "Cannot write to %s\n", filename);
1860 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001861 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001862#endif
1863 switch (node->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00001864 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00001865 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
1866 xmlGenericError(xmlGenericErrorContext,
1867 "Failed to write to %s\n", filename);
1868 return (-1);
1869 }
1870 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001871 case XML_HTML_DOCUMENT_NODE:
1872#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001873 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
1874 xmlGenericError(xmlGenericErrorContext,
1875 "Failed to write to %s\n", filename);
1876 return (-1);
1877 }
Owen Taylor3473f882001-02-23 17:55:21 +00001878#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001879 if (xmlSaveFile((char *) filename, ctxt->doc) < -1) {
1880 xmlGenericError(xmlGenericErrorContext,
1881 "Failed to write to %s\n", filename);
1882 return (-1);
1883 }
Owen Taylor3473f882001-02-23 17:55:21 +00001884#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001885 break;
1886 default:{
1887 FILE *f;
Owen Taylor3473f882001-02-23 17:55:21 +00001888
Daniel Veillard78d12092001-10-11 09:12:24 +00001889 f = fopen((char *) filename, "w");
1890 if (f == NULL) {
1891 xmlGenericError(xmlGenericErrorContext,
1892 "Failed to write to %s\n", filename);
1893 return (-1);
1894 }
1895 xmlElemDump(f, ctxt->doc, node);
1896 fclose(f);
1897 }
Owen Taylor3473f882001-02-23 17:55:21 +00001898 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001899 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001900}
1901
1902/**
1903 * xmlShellSave:
1904 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001905 * @filename: the file name (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00001906 * @node: unused
1907 * @node2: unused
1908 *
1909 * Implements the XML shell function "save"
1910 * Write the current document to the filename, or it's original name
1911 *
1912 * Returns 0 or -1 in case of error
1913 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001914int
1915xmlShellSave(xmlShellCtxtPtr ctxt, char *filename,
1916 xmlNodePtr node ATTRIBUTE_UNUSED,
1917 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1918{
Owen Taylor3473f882001-02-23 17:55:21 +00001919 if (ctxt->doc == NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00001920 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001921 if ((filename == NULL) || (filename[0] == 0))
1922 filename = ctxt->filename;
1923#ifdef W_OK
1924 if (access((char *) filename, W_OK)) {
1925 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard78d12092001-10-11 09:12:24 +00001926 "Cannot save to %s\n", filename);
1927 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001928 }
1929#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00001930 switch (ctxt->doc->type) {
Owen Taylor3473f882001-02-23 17:55:21 +00001931 case XML_DOCUMENT_NODE:
Daniel Veillard78d12092001-10-11 09:12:24 +00001932 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
1933 xmlGenericError(xmlGenericErrorContext,
1934 "Failed to save to %s\n", filename);
1935 }
1936 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001937 case XML_HTML_DOCUMENT_NODE:
1938#ifdef LIBXML_HTML_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00001939 if (htmlSaveFile((char *) filename, ctxt->doc) < 0) {
1940 xmlGenericError(xmlGenericErrorContext,
1941 "Failed to save to %s\n", filename);
1942 }
Owen Taylor3473f882001-02-23 17:55:21 +00001943#else
Daniel Veillard78d12092001-10-11 09:12:24 +00001944 if (xmlSaveFile((char *) filename, ctxt->doc) < 0) {
1945 xmlGenericError(xmlGenericErrorContext,
1946 "Failed to save to %s\n", filename);
1947 }
Owen Taylor3473f882001-02-23 17:55:21 +00001948#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00001949 break;
1950 default:
1951 xmlGenericError(xmlGenericErrorContext,
1952 "To save to subparts of a document use the 'write' command\n");
1953 return (-1);
1954
Owen Taylor3473f882001-02-23 17:55:21 +00001955 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001956 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00001957}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001958#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001959
Daniel Veillardf54cd532004-02-25 11:52:31 +00001960#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001961/**
1962 * xmlShellValidate:
1963 * @ctxt: the shell context
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001964 * @dtd: the DTD URI (optional)
Owen Taylor3473f882001-02-23 17:55:21 +00001965 * @node: unused
1966 * @node2: unused
1967 *
1968 * Implements the XML shell function "validate"
1969 * Validate the document, if a DTD path is provided, then the validation
1970 * is done against the given DTD.
1971 *
1972 * Returns 0 or -1 in case of error
1973 */
Daniel Veillard78d12092001-10-11 09:12:24 +00001974int
1975xmlShellValidate(xmlShellCtxtPtr ctxt, char *dtd,
1976 xmlNodePtr node ATTRIBUTE_UNUSED,
1977 xmlNodePtr node2 ATTRIBUTE_UNUSED)
1978{
Owen Taylor3473f882001-02-23 17:55:21 +00001979 xmlValidCtxt vctxt;
1980 int res = -1;
1981
1982 vctxt.userData = stderr;
1983 vctxt.error = (xmlValidityErrorFunc) fprintf;
1984 vctxt.warning = (xmlValidityWarningFunc) fprintf;
1985
1986 if ((dtd == NULL) || (dtd[0] == 0)) {
1987 res = xmlValidateDocument(&vctxt, ctxt->doc);
1988 } else {
1989 xmlDtdPtr subset;
1990
Daniel Veillard78d12092001-10-11 09:12:24 +00001991 subset = xmlParseDTD(NULL, (xmlChar *) dtd);
1992 if (subset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001993 res = xmlValidateDtd(&vctxt, ctxt->doc, subset);
1994
Daniel Veillard78d12092001-10-11 09:12:24 +00001995 xmlFreeDtd(subset);
1996 }
Owen Taylor3473f882001-02-23 17:55:21 +00001997 }
Daniel Veillard78d12092001-10-11 09:12:24 +00001998 return (res);
Owen Taylor3473f882001-02-23 17:55:21 +00001999}
Daniel Veillardf54cd532004-02-25 11:52:31 +00002000#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002001
2002/**
2003 * xmlShellDu:
2004 * @ctxt: the shell context
2005 * @arg: unused
2006 * @tree: a node defining a subtree
2007 * @node2: unused
2008 *
2009 * Implements the XML shell function "du"
2010 * show the structure of the subtree under node @tree
2011 * If @tree is null, the command works on the current node.
2012 *
2013 * Returns 0 or -1 in case of error
2014 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002015int
Daniel Veillard321be0c2002-10-08 21:26:42 +00002016xmlShellDu(xmlShellCtxtPtr ctxt,
Daniel Veillard78d12092001-10-11 09:12:24 +00002017 char *arg ATTRIBUTE_UNUSED, xmlNodePtr tree,
2018 xmlNodePtr node2 ATTRIBUTE_UNUSED)
2019{
Owen Taylor3473f882001-02-23 17:55:21 +00002020 xmlNodePtr node;
Daniel Veillard78d12092001-10-11 09:12:24 +00002021 int indent = 0, i;
Owen Taylor3473f882001-02-23 17:55:21 +00002022
Daniel Veillard321be0c2002-10-08 21:26:42 +00002023 if (!ctxt)
2024 return (-1);
2025
Daniel Veillard78d12092001-10-11 09:12:24 +00002026 if (tree == NULL)
2027 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002028 node = tree;
2029 while (node != NULL) {
2030 if ((node->type == XML_DOCUMENT_NODE) ||
2031 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002032 fprintf(ctxt->output, "/\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002033 } else if (node->type == XML_ELEMENT_NODE) {
2034 for (i = 0; i < indent; i++)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002035 fprintf(ctxt->output, " ");
2036 fprintf(ctxt->output, "%s\n", node->name);
Daniel Veillard78d12092001-10-11 09:12:24 +00002037 } else {
2038 }
Owen Taylor3473f882001-02-23 17:55:21 +00002039
Daniel Veillard78d12092001-10-11 09:12:24 +00002040 /*
2041 * Browse the full subtree, deep first
2042 */
Owen Taylor3473f882001-02-23 17:55:21 +00002043
2044 if ((node->type == XML_DOCUMENT_NODE) ||
2045 (node->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002046 node = ((xmlDocPtr) node)->children;
2047 } else if ((node->children != NULL)
2048 && (node->type != XML_ENTITY_REF_NODE)) {
2049 /* deep first */
2050 node = node->children;
2051 indent++;
2052 } else if ((node != tree) && (node->next != NULL)) {
2053 /* then siblings */
2054 node = node->next;
2055 } else if (node != tree) {
2056 /* go up to parents->next if needed */
2057 while (node != tree) {
2058 if (node->parent != NULL) {
2059 node = node->parent;
2060 indent--;
2061 }
2062 if ((node != tree) && (node->next != NULL)) {
2063 node = node->next;
2064 break;
2065 }
2066 if (node->parent == NULL) {
2067 node = NULL;
2068 break;
2069 }
2070 if (node == tree) {
2071 node = NULL;
2072 break;
2073 }
2074 }
2075 /* exit condition */
2076 if (node == tree)
2077 node = NULL;
2078 } else
2079 node = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002080 }
Daniel Veillard78d12092001-10-11 09:12:24 +00002081 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002082}
2083
2084/**
2085 * xmlShellPwd:
2086 * @ctxt: the shell context
2087 * @buffer: the output buffer
Daniel Veillard9d06d302002-01-22 18:15:52 +00002088 * @node: a node
Owen Taylor3473f882001-02-23 17:55:21 +00002089 * @node2: unused
2090 *
2091 * Implements the XML shell function "pwd"
2092 * Show the full path from the root to the node, if needed building
2093 * thumblers when similar elements exists at a given ancestor level.
2094 * The output is compatible with XPath commands.
2095 *
2096 * Returns 0 or -1 in case of error
2097 */
Daniel Veillard78d12092001-10-11 09:12:24 +00002098int
2099xmlShellPwd(xmlShellCtxtPtr ctxt ATTRIBUTE_UNUSED, char *buffer,
2100 xmlNodePtr node, xmlNodePtr node2 ATTRIBUTE_UNUSED)
2101{
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002102 xmlChar *path;
Owen Taylor3473f882001-02-23 17:55:21 +00002103
Daniel Veillard78d12092001-10-11 09:12:24 +00002104 if (node == NULL)
2105 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002106
Daniel Veillardc6e013a2001-11-10 10:08:57 +00002107 path = xmlGetNodePath(node);
2108 if (path == NULL)
2109 return (-1);
2110
2111 /*
2112 * This test prevents buffer overflow, because this routine
2113 * is only called by xmlShell, in which the second argument is
2114 * 500 chars long.
2115 * It is a dirty hack before a cleaner solution is found.
2116 * Documentation should mention that the second argument must
2117 * be at least 500 chars long, and could be stripped if too long.
2118 */
2119 snprintf(buffer, 499, "%s", path);
2120 buffer[499] = '0';
2121 xmlFree(path);
2122
Daniel Veillard78d12092001-10-11 09:12:24 +00002123 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00002124}
2125
2126/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002127 * xmlShell:
Owen Taylor3473f882001-02-23 17:55:21 +00002128 * @doc: the initial document
2129 * @filename: the output buffer
2130 * @input: the line reading function
Daniel Veillard321be0c2002-10-08 21:26:42 +00002131 * @output: the output FILE*, defaults to stdout if NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002132 *
2133 * Implements the XML shell
2134 * This allow to load, validate, view, modify and save a document
2135 * using a environment similar to a UNIX commandline.
2136 */
2137void
2138xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
Daniel Veillard78d12092001-10-11 09:12:24 +00002139 FILE * output)
2140{
Owen Taylor3473f882001-02-23 17:55:21 +00002141 char prompt[500] = "/ > ";
2142 char *cmdline = NULL, *cur;
2143 int nbargs;
2144 char command[100];
2145 char arg[400];
2146 int i;
2147 xmlShellCtxtPtr ctxt;
2148 xmlXPathObjectPtr list;
2149
2150 if (doc == NULL)
2151 return;
2152 if (filename == NULL)
2153 return;
2154 if (input == NULL)
2155 return;
2156 if (output == NULL)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002157 output = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00002158 ctxt = (xmlShellCtxtPtr) xmlMalloc(sizeof(xmlShellCtxt));
Daniel Veillard78d12092001-10-11 09:12:24 +00002159 if (ctxt == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00002160 return;
2161 ctxt->loaded = 0;
2162 ctxt->doc = doc;
2163 ctxt->input = input;
2164 ctxt->output = output;
2165 ctxt->filename = (char *) xmlStrdup((xmlChar *) filename);
Daniel Veillard78d12092001-10-11 09:12:24 +00002166 ctxt->node = (xmlNodePtr) ctxt->doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002167
2168#ifdef LIBXML_XPATH_ENABLED
2169 ctxt->pctxt = xmlXPathNewContext(ctxt->doc);
2170 if (ctxt->pctxt == NULL) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002171 xmlFree(ctxt);
2172 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002173 }
2174#endif /* LIBXML_XPATH_ENABLED */
2175 while (1) {
2176 if (ctxt->node == (xmlNodePtr) ctxt->doc)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002177 snprintf(prompt, sizeof(prompt), "%s > ", "/");
Daniel Veillard7a985a12003-07-06 17:57:42 +00002178 else if ((ctxt->node != NULL) && (ctxt->node->name))
Daniel Veillard78d12092001-10-11 09:12:24 +00002179 snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name);
Owen Taylor3473f882001-02-23 17:55:21 +00002180 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00002181 snprintf(prompt, sizeof(prompt), "? > ");
Owen Taylor3473f882001-02-23 17:55:21 +00002182 prompt[sizeof(prompt) - 1] = 0;
2183
Daniel Veillard78d12092001-10-11 09:12:24 +00002184 /*
2185 * Get a new command line
2186 */
Owen Taylor3473f882001-02-23 17:55:21 +00002187 cmdline = ctxt->input(prompt);
Daniel Veillard78d12092001-10-11 09:12:24 +00002188 if (cmdline == NULL)
2189 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002190
Daniel Veillard78d12092001-10-11 09:12:24 +00002191 /*
2192 * Parse the command itself
2193 */
2194 cur = cmdline;
2195 nbargs = 0;
2196 while ((*cur == ' ') || (*cur == '\t'))
2197 cur++;
2198 i = 0;
2199 while ((*cur != ' ') && (*cur != '\t') &&
2200 (*cur != '\n') && (*cur != '\r')) {
2201 if (*cur == 0)
2202 break;
2203 command[i++] = *cur++;
2204 }
2205 command[i] = 0;
2206 if (i == 0)
2207 continue;
2208 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002209
Daniel Veillard78d12092001-10-11 09:12:24 +00002210 /*
2211 * Parse the argument
2212 */
2213 while ((*cur == ' ') || (*cur == '\t'))
2214 cur++;
2215 i = 0;
2216 while ((*cur != '\n') && (*cur != '\r') && (*cur != 0)) {
2217 if (*cur == 0)
2218 break;
2219 arg[i++] = *cur++;
2220 }
2221 arg[i] = 0;
2222 if (i != 0)
2223 nbargs++;
Owen Taylor3473f882001-02-23 17:55:21 +00002224
Daniel Veillard78d12092001-10-11 09:12:24 +00002225 /*
2226 * start interpreting the command
2227 */
Owen Taylor3473f882001-02-23 17:55:21 +00002228 if (!strcmp(command, "exit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002229 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002230 if (!strcmp(command, "quit"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002231 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002232 if (!strcmp(command, "bye"))
Daniel Veillard78d12092001-10-11 09:12:24 +00002233 break;
Daniel Veillard5004f422001-11-08 13:53:05 +00002234 if (!strcmp(command, "help")) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002235 fprintf(ctxt->output, "\tbase display XML base of the node\n");
2236 fprintf(ctxt->output, "\tsetbase URI change the XML base of the node\n");
2237 fprintf(ctxt->output, "\tbye leave shell\n");
2238 fprintf(ctxt->output, "\tcat [node] display node or current node\n");
2239 fprintf(ctxt->output, "\tcd [path] change directory to path or to root\n");
2240 fprintf(ctxt->output, "\tdir [path] dumps informations about the node (namespace, attributes, content)\n");
2241 fprintf(ctxt->output, "\tdu [path] show the structure of the subtree under path or the current node\n");
2242 fprintf(ctxt->output, "\texit leave shell\n");
2243 fprintf(ctxt->output, "\thelp display this help\n");
2244 fprintf(ctxt->output, "\tfree display memory usage\n");
2245 fprintf(ctxt->output, "\tload [name] load a new document with name\n");
2246 fprintf(ctxt->output, "\tls [path] list contents of path or the current directory\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002247#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002248 fprintf(ctxt->output, "\txpath expr evaluate the XPath expression in that context and print the result\n");
Daniel Veillard2070c482002-01-22 22:12:19 +00002249#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard321be0c2002-10-08 21:26:42 +00002250 fprintf(ctxt->output, "\tpwd display current working directory\n");
2251 fprintf(ctxt->output, "\tquit leave shell\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002252#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard321be0c2002-10-08 21:26:42 +00002253 fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
Daniel Veillard321be0c2002-10-08 21:26:42 +00002254 fprintf(ctxt->output, "\twrite [name] write the current node to the filename\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002255#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf54cd532004-02-25 11:52:31 +00002256#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002257 fprintf(ctxt->output, "\tvalidate check the document for errors\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002258#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard522bc602004-02-21 11:53:09 +00002259#ifdef LIBXML_SCHEMAS_ENABLED
2260 fprintf(ctxt->output, "\trelaxng rng validate the document agaisnt the Relax-NG schemas\n");
2261#endif
Daniel Veillard1e208222002-10-22 14:25:25 +00002262 fprintf(ctxt->output, "\tgrep string search for a string in the subtree\n");
Daniel Veillardf54cd532004-02-25 11:52:31 +00002263#ifdef LIBXML_VALID_ENABLED
Daniel Veillard5004f422001-11-08 13:53:05 +00002264 } else if (!strcmp(command, "validate")) {
Daniel Veillard78d12092001-10-11 09:12:24 +00002265 xmlShellValidate(ctxt, arg, NULL, NULL);
Daniel Veillardf54cd532004-02-25 11:52:31 +00002266#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002267 } else if (!strcmp(command, "load")) {
2268 xmlShellLoad(ctxt, arg, NULL, NULL);
Daniel Veillard522bc602004-02-21 11:53:09 +00002269#ifdef LIBXML_SCHEMAS_ENABLED
2270 } else if (!strcmp(command, "relaxng")) {
2271 xmlShellRNGValidate(ctxt, arg, NULL, NULL);
2272#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002273#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002274 } else if (!strcmp(command, "save")) {
2275 xmlShellSave(ctxt, arg, NULL, NULL);
2276 } else if (!strcmp(command, "write")) {
2277 xmlShellWrite(ctxt, arg, NULL, NULL);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002278#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard1e208222002-10-22 14:25:25 +00002279 } else if (!strcmp(command, "grep")) {
2280 xmlShellGrep(ctxt, arg, ctxt->node, NULL);
Daniel Veillard78d12092001-10-11 09:12:24 +00002281 } else if (!strcmp(command, "free")) {
2282 if (arg[0] == 0) {
Daniel Veillard321be0c2002-10-08 21:26:42 +00002283 xmlMemShow(ctxt->output, 0);
Daniel Veillard78d12092001-10-11 09:12:24 +00002284 } else {
2285 int len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002286
Daniel Veillard78d12092001-10-11 09:12:24 +00002287 sscanf(arg, "%d", &len);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002288 xmlMemShow(ctxt->output, len);
Daniel Veillard78d12092001-10-11 09:12:24 +00002289 }
2290 } else if (!strcmp(command, "pwd")) {
2291 char dir[500];
Owen Taylor3473f882001-02-23 17:55:21 +00002292
Daniel Veillard78d12092001-10-11 09:12:24 +00002293 if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
Daniel Veillard321be0c2002-10-08 21:26:42 +00002294 fprintf(ctxt->output, "%s\n", dir);
Daniel Veillard78d12092001-10-11 09:12:24 +00002295 } else if (!strcmp(command, "du")) {
2296 xmlShellDu(ctxt, NULL, ctxt->node, NULL);
2297 } else if (!strcmp(command, "base")) {
2298 xmlShellBase(ctxt, NULL, ctxt->node, NULL);
Daniel Veillard2070c482002-01-22 22:12:19 +00002299#ifdef LIBXML_XPATH_ENABLED
2300 } else if (!strcmp(command, "xpath")) {
2301 if (arg[0] == 0) {
2302 xmlGenericError(xmlGenericErrorContext,
2303 "xpath: expression required\n");
2304 } else {
2305 ctxt->pctxt->node = ctxt->node;
2306 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
Daniel Veillard321be0c2002-10-08 21:26:42 +00002307 xmlXPathDebugDumpObject(ctxt->output, list, 0);
Daniel Veillard2070c482002-01-22 22:12:19 +00002308 xmlXPathFreeObject(list);
2309 }
2310#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard2156d432004-03-04 15:59:36 +00002311#ifdef LIBXML_TREE_ENABLED
Daniel Veillardcfa0d812002-01-17 08:46:58 +00002312 } else if (!strcmp(command, "setbase")) {
2313 xmlShellSetBase(ctxt, arg, ctxt->node, NULL);
Daniel Veillard2156d432004-03-04 15:59:36 +00002314#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002315 } else if ((!strcmp(command, "ls")) || (!strcmp(command, "dir"))) {
2316 int dir = (!strcmp(command, "dir"));
2317
2318 if (arg[0] == 0) {
2319 if (dir)
2320 xmlShellDir(ctxt, NULL, ctxt->node, NULL);
2321 else
2322 xmlShellList(ctxt, NULL, ctxt->node, NULL);
2323 } else {
2324 ctxt->pctxt->node = ctxt->node;
Daniel Veillard61d80a22001-04-27 17:13:01 +00002325#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002326 ctxt->pctxt->node = ctxt->node;
2327 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2328#else
2329 list = NULL;
2330#endif /* LIBXML_XPATH_ENABLED */
2331 if (list != NULL) {
2332 switch (list->type) {
2333 case XPATH_UNDEFINED:
2334 xmlGenericError(xmlGenericErrorContext,
2335 "%s: no such node\n", arg);
2336 break;
2337 case XPATH_NODESET:{
2338 int indx;
2339
Daniel Veillarda6825e82001-11-07 13:33:59 +00002340 if (list->nodesetval == NULL)
2341 break;
2342
Daniel Veillard78d12092001-10-11 09:12:24 +00002343 for (indx = 0;
2344 indx < list->nodesetval->nodeNr;
2345 indx++) {
2346 if (dir)
2347 xmlShellDir(ctxt, NULL,
2348 list->nodesetval->
2349 nodeTab[indx], NULL);
2350 else
2351 xmlShellList(ctxt, NULL,
2352 list->nodesetval->
2353 nodeTab[indx], NULL);
2354 }
2355 break;
2356 }
2357 case XPATH_BOOLEAN:
2358 xmlGenericError(xmlGenericErrorContext,
2359 "%s is a Boolean\n", arg);
2360 break;
2361 case XPATH_NUMBER:
2362 xmlGenericError(xmlGenericErrorContext,
2363 "%s is a number\n", arg);
2364 break;
2365 case XPATH_STRING:
2366 xmlGenericError(xmlGenericErrorContext,
2367 "%s is a string\n", arg);
2368 break;
2369 case XPATH_POINT:
2370 xmlGenericError(xmlGenericErrorContext,
2371 "%s is a point\n", arg);
2372 break;
2373 case XPATH_RANGE:
2374 xmlGenericError(xmlGenericErrorContext,
2375 "%s is a range\n", arg);
2376 break;
2377 case XPATH_LOCATIONSET:
2378 xmlGenericError(xmlGenericErrorContext,
2379 "%s is a range\n", arg);
2380 break;
2381 case XPATH_USERS:
2382 xmlGenericError(xmlGenericErrorContext,
2383 "%s is user-defined\n", arg);
2384 break;
2385 case XPATH_XSLT_TREE:
2386 xmlGenericError(xmlGenericErrorContext,
2387 "%s is an XSLT value tree\n",
2388 arg);
2389 break;
2390 }
2391#ifdef LIBXML_XPATH_ENABLED
2392 xmlXPathFreeObject(list);
Daniel Veillard61d80a22001-04-27 17:13:01 +00002393#endif
Daniel Veillard78d12092001-10-11 09:12:24 +00002394 } else {
2395 xmlGenericError(xmlGenericErrorContext,
2396 "%s: no such node\n", arg);
2397 }
2398 ctxt->pctxt->node = NULL;
2399 }
2400 } else if (!strcmp(command, "cd")) {
2401 if (arg[0] == 0) {
2402 ctxt->node = (xmlNodePtr) ctxt->doc;
2403 } else {
2404#ifdef LIBXML_XPATH_ENABLED
2405 ctxt->pctxt->node = ctxt->node;
2406 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2407#else
2408 list = NULL;
2409#endif /* LIBXML_XPATH_ENABLED */
2410 if (list != NULL) {
2411 switch (list->type) {
2412 case XPATH_UNDEFINED:
2413 xmlGenericError(xmlGenericErrorContext,
2414 "%s: no such node\n", arg);
2415 break;
2416 case XPATH_NODESET:
Daniel Veillarda6825e82001-11-07 13:33:59 +00002417 if (list->nodesetval != NULL) {
2418 if (list->nodesetval->nodeNr == 1) {
2419 ctxt->node = list->nodesetval->nodeTab[0];
Daniel Veillard7a985a12003-07-06 17:57:42 +00002420 if ((ctxt->node != NULL) &&
2421 (ctxt->node->type ==
2422 XML_NAMESPACE_DECL)) {
2423 xmlGenericError(xmlGenericErrorContext,
2424 "cannot cd to namespace\n");
2425 ctxt->node = NULL;
2426 }
Daniel Veillarda6825e82001-11-07 13:33:59 +00002427 } else
2428 xmlGenericError(xmlGenericErrorContext,
2429 "%s is a %d Node Set\n",
2430 arg,
2431 list->nodesetval->nodeNr);
Daniel Veillard78d12092001-10-11 09:12:24 +00002432 } else
2433 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda6825e82001-11-07 13:33:59 +00002434 "%s is an empty Node Set\n",
2435 arg);
Daniel Veillard78d12092001-10-11 09:12:24 +00002436 break;
2437 case XPATH_BOOLEAN:
2438 xmlGenericError(xmlGenericErrorContext,
2439 "%s is a Boolean\n", arg);
2440 break;
2441 case XPATH_NUMBER:
2442 xmlGenericError(xmlGenericErrorContext,
2443 "%s is a number\n", arg);
2444 break;
2445 case XPATH_STRING:
2446 xmlGenericError(xmlGenericErrorContext,
2447 "%s is a string\n", arg);
2448 break;
2449 case XPATH_POINT:
2450 xmlGenericError(xmlGenericErrorContext,
2451 "%s is a point\n", arg);
2452 break;
2453 case XPATH_RANGE:
2454 xmlGenericError(xmlGenericErrorContext,
2455 "%s is a range\n", arg);
2456 break;
2457 case XPATH_LOCATIONSET:
2458 xmlGenericError(xmlGenericErrorContext,
2459 "%s is a range\n", arg);
2460 break;
2461 case XPATH_USERS:
2462 xmlGenericError(xmlGenericErrorContext,
2463 "%s is user-defined\n", arg);
2464 break;
2465 case XPATH_XSLT_TREE:
2466 xmlGenericError(xmlGenericErrorContext,
2467 "%s is an XSLT value tree\n",
2468 arg);
2469 break;
2470 }
2471#ifdef LIBXML_XPATH_ENABLED
2472 xmlXPathFreeObject(list);
2473#endif
2474 } else {
2475 xmlGenericError(xmlGenericErrorContext,
2476 "%s: no such node\n", arg);
2477 }
2478 ctxt->pctxt->node = NULL;
2479 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002480#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard78d12092001-10-11 09:12:24 +00002481 } else if (!strcmp(command, "cat")) {
2482 if (arg[0] == 0) {
2483 xmlShellCat(ctxt, NULL, ctxt->node, NULL);
2484 } else {
2485 ctxt->pctxt->node = ctxt->node;
2486#ifdef LIBXML_XPATH_ENABLED
2487 ctxt->pctxt->node = ctxt->node;
2488 list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
2489#else
2490 list = NULL;
2491#endif /* LIBXML_XPATH_ENABLED */
2492 if (list != NULL) {
2493 switch (list->type) {
2494 case XPATH_UNDEFINED:
2495 xmlGenericError(xmlGenericErrorContext,
2496 "%s: no such node\n", arg);
2497 break;
2498 case XPATH_NODESET:{
2499 int indx;
2500
Daniel Veillarda6825e82001-11-07 13:33:59 +00002501 if (list->nodesetval == NULL)
2502 break;
2503
Daniel Veillard78d12092001-10-11 09:12:24 +00002504 for (indx = 0;
2505 indx < list->nodesetval->nodeNr;
2506 indx++) {
2507 if (i > 0)
Daniel Veillard321be0c2002-10-08 21:26:42 +00002508 fprintf(ctxt->output, " -------\n");
Daniel Veillard78d12092001-10-11 09:12:24 +00002509 xmlShellCat(ctxt, NULL,
2510 list->nodesetval->
2511 nodeTab[indx], NULL);
2512 }
2513 break;
2514 }
2515 case XPATH_BOOLEAN:
2516 xmlGenericError(xmlGenericErrorContext,
2517 "%s is a Boolean\n", arg);
2518 break;
2519 case XPATH_NUMBER:
2520 xmlGenericError(xmlGenericErrorContext,
2521 "%s is a number\n", arg);
2522 break;
2523 case XPATH_STRING:
2524 xmlGenericError(xmlGenericErrorContext,
2525 "%s is a string\n", arg);
2526 break;
2527 case XPATH_POINT:
2528 xmlGenericError(xmlGenericErrorContext,
2529 "%s is a point\n", arg);
2530 break;
2531 case XPATH_RANGE:
2532 xmlGenericError(xmlGenericErrorContext,
2533 "%s is a range\n", arg);
2534 break;
2535 case XPATH_LOCATIONSET:
2536 xmlGenericError(xmlGenericErrorContext,
2537 "%s is a range\n", arg);
2538 break;
2539 case XPATH_USERS:
2540 xmlGenericError(xmlGenericErrorContext,
2541 "%s is user-defined\n", arg);
2542 break;
2543 case XPATH_XSLT_TREE:
2544 xmlGenericError(xmlGenericErrorContext,
2545 "%s is an XSLT value tree\n",
2546 arg);
2547 break;
2548 }
2549#ifdef LIBXML_XPATH_ENABLED
2550 xmlXPathFreeObject(list);
2551#endif
2552 } else {
2553 xmlGenericError(xmlGenericErrorContext,
2554 "%s: no such node\n", arg);
2555 }
2556 ctxt->pctxt->node = NULL;
2557 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002558#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard78d12092001-10-11 09:12:24 +00002559 } else {
2560 xmlGenericError(xmlGenericErrorContext,
2561 "Unknown command %s\n", command);
2562 }
2563 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00002564 }
2565#ifdef LIBXML_XPATH_ENABLED
2566 xmlXPathFreeContext(ctxt->pctxt);
2567#endif /* LIBXML_XPATH_ENABLED */
2568 if (ctxt->loaded) {
2569 xmlFreeDoc(ctxt->doc);
2570 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00002571 if (ctxt->filename != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002572 xmlFree(ctxt->filename);
Owen Taylor3473f882001-02-23 17:55:21 +00002573 xmlFree(ctxt);
2574 if (cmdline != NULL)
Daniel Veillard78d12092001-10-11 09:12:24 +00002575 free(cmdline); /* not xmlFree here ! */
Owen Taylor3473f882001-02-23 17:55:21 +00002576}
2577
2578#endif /* LIBXML_DEBUG_ENABLED */