blob: fd65092e0111652570ba8fbb1e31471f12833489 [file] [log] [blame]
Daniel Veillard5099ae81999-04-21 20:12:07 +00001/*
2 * tester.c : a small tester program for parsing using the SAX API.
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel.Veillard@w3.org
7 */
8
9#ifdef WIN32
Daniel Veillard3c558c31999-12-22 11:30:41 +000010#include "win32config.h"
Daniel Veillard5099ae81999-04-21 20:12:07 +000011#else
Daniel Veillard7f7d1111999-09-22 09:46:25 +000012#include "config.h"
Daniel Veillard5099ae81999-04-21 20:12:07 +000013#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000014
15#include <stdio.h>
16#include <string.h>
17#include <stdarg.h>
18
19#ifdef HAVE_SYS_TYPES_H
Daniel Veillard5099ae81999-04-21 20:12:07 +000020#include <sys/types.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000021#endif
Daniel Veillard5099ae81999-04-21 20:12:07 +000022#ifdef HAVE_SYS_STAT_H
23#include <sys/stat.h>
24#endif
25#ifdef HAVE_FCNTL_H
26#include <fcntl.h>
27#endif
28#ifdef HAVE_UNISTD_H
29#include <unistd.h>
30#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000031#ifdef HAVE_STDLIB_H
Daniel Veillard5099ae81999-04-21 20:12:07 +000032#include <stdlib.h>
Daniel Veillard7f7d1111999-09-22 09:46:25 +000033#endif
Daniel Veillard7a66ee61999-09-26 11:31:02 +000034#ifdef HAVE_STRING_H
35#include <string.h>
36#endif
Daniel Veillard7f7d1111999-09-22 09:46:25 +000037
Daniel Veillard5099ae81999-04-21 20:12:07 +000038
39#include "parser.h"
Daniel Veillard011b63c1999-06-02 17:44:04 +000040#include "parserInternals.h" /* only for xmlNewInputFromFile() */
Daniel Veillard5099ae81999-04-21 20:12:07 +000041#include "tree.h"
42#include "debugXML.h"
Daniel Veillardf5c2c871999-12-01 09:51:45 +000043#include "xmlmemory.h"
Daniel Veillard5099ae81999-04-21 20:12:07 +000044
45static int debug = 0;
46static int copy = 0;
47static int recovery = 0;
Daniel Veillarddbfd6411999-12-28 16:35:14 +000048static int push = 0;
Daniel Veillard5099ae81999-04-21 20:12:07 +000049
50xmlSAXHandler emptySAXHandlerStruct = {
51 NULL, /* internalSubset */
52 NULL, /* isStandalone */
53 NULL, /* hasInternalSubset */
54 NULL, /* hasExternalSubset */
55 NULL, /* resolveEntity */
56 NULL, /* getEntity */
57 NULL, /* entityDecl */
58 NULL, /* notationDecl */
59 NULL, /* attributeDecl */
60 NULL, /* elementDecl */
61 NULL, /* unparsedEntityDecl */
62 NULL, /* setDocumentLocator */
63 NULL, /* startDocument */
64 NULL, /* endDocument */
65 NULL, /* startElement */
66 NULL, /* endElement */
67 NULL, /* reference */
68 NULL, /* characters */
69 NULL, /* ignorableWhitespace */
70 NULL, /* processingInstruction */
71 NULL, /* comment */
72 NULL, /* xmlParserWarning */
73 NULL, /* xmlParserError */
74 NULL, /* xmlParserError */
Daniel Veillardb05deb71999-08-10 19:04:08 +000075 NULL, /* getParameterEntity */
Daniel Veillard5099ae81999-04-21 20:12:07 +000076};
77
78xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
Daniel Veillard011b63c1999-06-02 17:44:04 +000079extern xmlSAXHandlerPtr debugSAXHandler;
Daniel Veillard5099ae81999-04-21 20:12:07 +000080
Daniel Veillard5099ae81999-04-21 20:12:07 +000081/************************************************************************
82 * *
83 * Debug Handlers *
84 * *
85 ************************************************************************/
86
87/**
88 * isStandaloneDebug:
89 * @ctxt: An XML parser context
90 *
91 * Is this document tagged standalone ?
92 *
93 * Returns 1 if true
94 */
95int
Daniel Veillardb96e6431999-08-29 21:02:19 +000096isStandaloneDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +000097{
Daniel Veillard27d88741999-05-29 11:51:49 +000098 fprintf(stdout, "SAX.isStandalone()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +000099 return(0);
100}
101
102/**
103 * hasInternalSubsetDebug:
104 * @ctxt: An XML parser context
105 *
106 * Does this document has an internal subset
107 *
108 * Returns 1 if true
109 */
110int
Daniel Veillardb96e6431999-08-29 21:02:19 +0000111hasInternalSubsetDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000112{
Daniel Veillard27d88741999-05-29 11:51:49 +0000113 fprintf(stdout, "SAX.hasInternalSubset()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000114 return(0);
115}
116
117/**
118 * hasExternalSubsetDebug:
119 * @ctxt: An XML parser context
120 *
121 * Does this document has an external subset
122 *
123 * Returns 1 if true
124 */
125int
Daniel Veillardb96e6431999-08-29 21:02:19 +0000126hasExternalSubsetDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000127{
Daniel Veillard27d88741999-05-29 11:51:49 +0000128 fprintf(stdout, "SAX.hasExternalSubset()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000129 return(0);
130}
131
132/**
133 * hasInternalSubsetDebug:
134 * @ctxt: An XML parser context
135 *
136 * Does this document has an internal subset
137 */
138void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000139internalSubsetDebug(void *ctx, const xmlChar *name,
140 const xmlChar *ExternalID, const xmlChar *SystemID)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000141{
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000142 /* xmlDtdPtr externalSubset; */
Daniel Veillard011b63c1999-06-02 17:44:04 +0000143
Daniel Veillard27d88741999-05-29 11:51:49 +0000144 fprintf(stdout, "SAX.internalSubset(%s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000145 name, ExternalID, SystemID);
Daniel Veillard011b63c1999-06-02 17:44:04 +0000146
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000147/***********
Daniel Veillard011b63c1999-06-02 17:44:04 +0000148 if ((ExternalID != NULL) || (SystemID != NULL)) {
Daniel Veillardb05deb71999-08-10 19:04:08 +0000149 externalSubset = xmlParseDTD(ExternalID, SystemID);
150 if (externalSubset != NULL) {
151 xmlFreeDtd(externalSubset);
152 }
Daniel Veillard011b63c1999-06-02 17:44:04 +0000153 }
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000154 ***********/
Daniel Veillard5099ae81999-04-21 20:12:07 +0000155}
156
157/**
158 * resolveEntityDebug:
159 * @ctxt: An XML parser context
160 * @publicId: The public ID of the entity
161 * @systemId: The system ID of the entity
162 *
163 * Special entity resolver, better left to the parser, it has
164 * more context than the application layer.
165 * The default behaviour is to NOT resolve the entities, in that case
166 * the ENTITY_REF nodes are built in the structure (and the parameter
167 * values).
168 *
169 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
170 */
171xmlParserInputPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000172resolveEntityDebug(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000173{
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000174 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
Daniel Veillardb96e6431999-08-29 21:02:19 +0000175
Daniel Veillard14fff061999-06-22 21:49:07 +0000176
177 fprintf(stdout, "SAX.resolveEntity(");
178 if (publicId != NULL)
179 fprintf(stdout, "%s", (char *)publicId);
180 else
181 fprintf(stdout, " ");
182 if (systemId != NULL)
183 fprintf(stdout, ", %s)\n", (char *)systemId);
184 else
185 fprintf(stdout, ", )\n");
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000186/*********
Daniel Veillard011b63c1999-06-02 17:44:04 +0000187 if (systemId != NULL) {
Daniel Veillardb96e6431999-08-29 21:02:19 +0000188 return(xmlNewInputFromFile(ctxt, (char *) systemId));
Daniel Veillard011b63c1999-06-02 17:44:04 +0000189 }
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000190 *********/
Daniel Veillard5099ae81999-04-21 20:12:07 +0000191 return(NULL);
192}
193
194/**
195 * getEntityDebug:
196 * @ctxt: An XML parser context
197 * @name: The entity name
198 *
199 * Get an entity by name
200 *
201 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
202 */
203xmlEntityPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000204getEntityDebug(void *ctx, const xmlChar *name)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000205{
Daniel Veillard27d88741999-05-29 11:51:49 +0000206 fprintf(stdout, "SAX.getEntity(%s)\n", name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000207 return(NULL);
208}
209
Daniel Veillardb05deb71999-08-10 19:04:08 +0000210/**
211 * getParameterEntityDebug:
212 * @ctxt: An XML parser context
213 * @name: The entity name
214 *
215 * Get a parameter entity by name
216 *
217 * Returns the xmlParserInputPtr
218 */
219xmlEntityPtr
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000220getParameterEntityDebug(void *ctx, const xmlChar *name)
Daniel Veillardb05deb71999-08-10 19:04:08 +0000221{
222 fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
223 return(NULL);
224}
225
Daniel Veillard5099ae81999-04-21 20:12:07 +0000226
227/**
228 * entityDeclDebug:
229 * @ctxt: An XML parser context
230 * @name: the entity name
231 * @type: the entity type
232 * @publicId: The public ID of the entity
233 * @systemId: The system ID of the entity
234 * @content: the entity value (without processing).
235 *
236 * An entity definition has been parsed
237 */
238void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000239entityDeclDebug(void *ctx, const xmlChar *name, int type,
240 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000241{
Daniel Veillard27d88741999-05-29 11:51:49 +0000242 fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000243 name, type, publicId, systemId, content);
244}
245
246/**
247 * attributeDeclDebug:
248 * @ctxt: An XML parser context
249 * @name: the attribute name
250 * @type: the attribute type
251 *
252 * An attribute definition has been parsed
253 */
254void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000255attributeDeclDebug(void *ctx, const xmlChar *elem, const xmlChar *name,
256 int type, int def, const xmlChar *defaultValue,
Daniel Veillard5099ae81999-04-21 20:12:07 +0000257 xmlEnumerationPtr tree)
258{
Daniel Veillard27d88741999-05-29 11:51:49 +0000259 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000260 elem, name, type, def, defaultValue);
261}
262
263/**
264 * elementDeclDebug:
265 * @ctxt: An XML parser context
266 * @name: the element name
267 * @type: the element type
268 * @content: the element value (without processing).
269 *
270 * An element definition has been parsed
271 */
272void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000273elementDeclDebug(void *ctx, const xmlChar *name, int type,
Daniel Veillard5099ae81999-04-21 20:12:07 +0000274 xmlElementContentPtr content)
275{
Daniel Veillard27d88741999-05-29 11:51:49 +0000276 fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000277 name, type);
278}
279
280/**
281 * notationDeclDebug:
282 * @ctxt: An XML parser context
283 * @name: The name of the notation
284 * @publicId: The public ID of the entity
285 * @systemId: The system ID of the entity
286 *
287 * What to do when a notation declaration has been parsed.
Daniel Veillard5099ae81999-04-21 20:12:07 +0000288 */
289void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000290notationDeclDebug(void *ctx, const xmlChar *name,
291 const xmlChar *publicId, const xmlChar *systemId)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000292{
Daniel Veillard27d88741999-05-29 11:51:49 +0000293 fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000294 (char *) name, (char *) publicId, (char *) systemId);
295}
296
297/**
298 * unparsedEntityDeclDebug:
299 * @ctxt: An XML parser context
300 * @name: The name of the entity
301 * @publicId: The public ID of the entity
302 * @systemId: The system ID of the entity
303 * @notationName: the name of the notation
304 *
305 * What to do when an unparsed entity declaration is parsed
Daniel Veillard5099ae81999-04-21 20:12:07 +0000306 */
307void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000308unparsedEntityDeclDebug(void *ctx, const xmlChar *name,
309 const xmlChar *publicId, const xmlChar *systemId,
310 const xmlChar *notationName)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000311{
Daniel Veillard27d88741999-05-29 11:51:49 +0000312 fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000313 (char *) name, (char *) publicId, (char *) systemId,
314 (char *) notationName);
315}
316
317/**
318 * setDocumentLocatorDebug:
319 * @ctxt: An XML parser context
320 * @loc: A SAX Locator
321 *
322 * Receive the document locator at startup, actually xmlDefaultSAXLocator
323 * Everything is available on the context, so this is useless in our case.
324 */
325void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000326setDocumentLocatorDebug(void *ctx, xmlSAXLocatorPtr loc)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000327{
Daniel Veillard27d88741999-05-29 11:51:49 +0000328 fprintf(stdout, "SAX.setDocumentLocator()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000329}
330
331/**
332 * startDocumentDebug:
333 * @ctxt: An XML parser context
334 *
335 * called when the document start being processed.
336 */
337void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000338startDocumentDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000339{
Daniel Veillard27d88741999-05-29 11:51:49 +0000340 fprintf(stdout, "SAX.startDocument()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000341}
342
343/**
344 * endDocumentDebug:
345 * @ctxt: An XML parser context
346 *
347 * called when the document end has been detected.
348 */
349void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000350endDocumentDebug(void *ctx)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000351{
Daniel Veillard27d88741999-05-29 11:51:49 +0000352 fprintf(stdout, "SAX.endDocument()\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000353}
354
355/**
356 * startElementDebug:
357 * @ctxt: An XML parser context
358 * @name: The element name
359 *
360 * called when an opening tag has been processed.
Daniel Veillard5099ae81999-04-21 20:12:07 +0000361 */
362void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000363startElementDebug(void *ctx, const xmlChar *name, const xmlChar **atts)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000364{
365 int i;
366
Daniel Veillard27d88741999-05-29 11:51:49 +0000367 fprintf(stdout, "SAX.startElement(%s", (char *) name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000368 if (atts != NULL) {
369 for (i = 0;(atts[i] != NULL);i++) {
Daniel Veillard27d88741999-05-29 11:51:49 +0000370 fprintf(stdout, ", %s='", atts[i++]);
371 fprintf(stdout, "%s'", atts[i]);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000372 }
373 }
Daniel Veillard27d88741999-05-29 11:51:49 +0000374 fprintf(stdout, ")\n");
Daniel Veillard5099ae81999-04-21 20:12:07 +0000375}
376
377/**
378 * endElementDebug:
379 * @ctxt: An XML parser context
380 * @name: The element name
381 *
382 * called when the end of an element has been detected.
383 */
384void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000385endElementDebug(void *ctx, const xmlChar *name)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000386{
Daniel Veillard27d88741999-05-29 11:51:49 +0000387 fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000388}
389
390/**
391 * charactersDebug:
392 * @ctxt: An XML parser context
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000393 * @ch: a xmlChar string
394 * @len: the number of xmlChar
Daniel Veillard5099ae81999-04-21 20:12:07 +0000395 *
396 * receiving some chars from the parser.
397 * Question: how much at a time ???
398 */
399void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000400charactersDebug(void *ctx, const xmlChar *ch, int len)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000401{
Daniel Veillarde2d034d1999-07-27 19:52:06 +0000402 int i;
403
404 fprintf(stdout, "SAX.characters(");
405 for (i = 0;(i < len) && (i < 30);i++)
406 fprintf(stdout, "%c", ch[i]);
407 fprintf(stdout, ", %d)\n", len);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000408}
409
410/**
411 * referenceDebug:
412 * @ctxt: An XML parser context
413 * @name: The entity name
414 *
415 * called when an entity reference is detected.
416 */
417void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000418referenceDebug(void *ctx, const xmlChar *name)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000419{
Daniel Veillard27d88741999-05-29 11:51:49 +0000420 fprintf(stdout, "SAX.reference(%s)\n", name);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000421}
422
423/**
424 * ignorableWhitespaceDebug:
425 * @ctxt: An XML parser context
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000426 * @ch: a xmlChar string
Daniel Veillard5099ae81999-04-21 20:12:07 +0000427 * @start: the first char in the string
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000428 * @len: the number of xmlChar
Daniel Veillard5099ae81999-04-21 20:12:07 +0000429 *
430 * receiving some ignorable whitespaces from the parser.
431 * Question: how much at a time ???
432 */
433void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000434ignorableWhitespaceDebug(void *ctx, const xmlChar *ch, int len)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000435{
Daniel Veillard27d88741999-05-29 11:51:49 +0000436 fprintf(stdout, "SAX.ignorableWhitespace(%.30s, %d)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000437 (char *) ch, len);
438}
439
440/**
441 * processingInstructionDebug:
442 * @ctxt: An XML parser context
443 * @target: the target name
444 * @data: the PI data's
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000445 * @len: the number of xmlChar
Daniel Veillard5099ae81999-04-21 20:12:07 +0000446 *
447 * A processing instruction has been parsed.
448 */
449void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000450processingInstructionDebug(void *ctx, const xmlChar *target,
451 const xmlChar *data)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000452{
Daniel Veillard27d88741999-05-29 11:51:49 +0000453 fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
Daniel Veillard5099ae81999-04-21 20:12:07 +0000454 (char *) target, (char *) data);
455}
456
457/**
458 * commentDebug:
459 * @ctxt: An XML parser context
460 * @value: the comment content
461 *
462 * A comment has been parsed.
463 */
464void
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000465commentDebug(void *ctx, const xmlChar *value)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000466{
Daniel Veillard27d88741999-05-29 11:51:49 +0000467 fprintf(stdout, "SAX.comment(%s)\n", value);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000468}
469
470/**
471 * warningDebug:
472 * @ctxt: An XML parser context
473 * @msg: the message to display/transmit
474 * @...: extra parameters for the message display
475 *
476 * Display and format a warning messages, gives file, line, position and
477 * extra parameters.
478 */
479void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000480warningDebug(void *ctx, const char *msg, ...)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000481{
482 va_list args;
483
484 va_start(args, msg);
Daniel Veillard27d88741999-05-29 11:51:49 +0000485 fprintf(stdout, "SAX.warning: ");
486 vfprintf(stdout, msg, args);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000487 va_end(args);
488}
489
490/**
491 * errorDebug:
492 * @ctxt: An XML parser context
493 * @msg: the message to display/transmit
494 * @...: extra parameters for the message display
495 *
496 * Display and format a error messages, gives file, line, position and
497 * extra parameters.
498 */
499void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000500errorDebug(void *ctx, const char *msg, ...)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000501{
502 va_list args;
503
504 va_start(args, msg);
Daniel Veillard27d88741999-05-29 11:51:49 +0000505 fprintf(stdout, "SAX.error: ");
506 vfprintf(stdout, msg, args);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000507 va_end(args);
508}
509
510/**
511 * fatalErrorDebug:
512 * @ctxt: An XML parser context
513 * @msg: the message to display/transmit
514 * @...: extra parameters for the message display
515 *
516 * Display and format a fatalError messages, gives file, line, position and
517 * extra parameters.
518 */
519void
Daniel Veillardb96e6431999-08-29 21:02:19 +0000520fatalErrorDebug(void *ctx, const char *msg, ...)
Daniel Veillard5099ae81999-04-21 20:12:07 +0000521{
522 va_list args;
523
524 va_start(args, msg);
Daniel Veillard27d88741999-05-29 11:51:49 +0000525 fprintf(stdout, "SAX.fatalError: ");
526 vfprintf(stdout, msg, args);
Daniel Veillard5099ae81999-04-21 20:12:07 +0000527 va_end(args);
528}
529
530xmlSAXHandler debugSAXHandlerStruct = {
531 internalSubsetDebug,
532 isStandaloneDebug,
533 hasInternalSubsetDebug,
534 hasExternalSubsetDebug,
535 resolveEntityDebug,
536 getEntityDebug,
537 entityDeclDebug,
538 notationDeclDebug,
539 attributeDeclDebug,
540 elementDeclDebug,
541 unparsedEntityDeclDebug,
542 setDocumentLocatorDebug,
543 startDocumentDebug,
544 endDocumentDebug,
545 startElementDebug,
546 endElementDebug,
547 referenceDebug,
548 charactersDebug,
549 ignorableWhitespaceDebug,
550 processingInstructionDebug,
551 commentDebug,
552 warningDebug,
553 errorDebug,
554 fatalErrorDebug,
Daniel Veillardb05deb71999-08-10 19:04:08 +0000555 getParameterEntityDebug,
Daniel Veillard5099ae81999-04-21 20:12:07 +0000556};
557
558xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
559
560/************************************************************************
561 * *
562 * Debug *
563 * *
564 ************************************************************************/
565
566void parseAndPrintFile(char *filename) {
Daniel Veillard7a66ee61999-09-26 11:31:02 +0000567 int res;
Daniel Veillard5099ae81999-04-21 20:12:07 +0000568
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000569 if (push) {
570 FILE *f;
Daniel Veillard5099ae81999-04-21 20:12:07 +0000571
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000572 /*
573 * Empty callbacks for checking
574 */
575 f = fopen(filename, "r");
576 if (f != NULL) {
577 int res;
578 char chars[10];
579 xmlParserCtxtPtr ctxt;
580
581 res = fread(chars, 1, 4, f);
582 if (res > 0) {
583 ctxt = xmlCreatePushParserCtxt(emptySAXHandler, NULL,
584 chars, res, filename);
585 while ((res = fread(chars, 1, 3, f)) > 0) {
586 xmlParseChunk(ctxt, chars, res, 0);
587 }
588 xmlParseChunk(ctxt, chars, 0, 1);
589 xmlFreeParserCtxt(ctxt);
590 }
591 fclose(f);
592 } else {
593 fprintf(stderr, "Cannot read file %s\n", filename);
594 }
595 /*
596 * Debug callback
597 */
598 f = fopen(filename, "r");
599 if (f != NULL) {
600 int res;
601 char chars[10];
602 xmlParserCtxtPtr ctxt;
603
604 res = fread(chars, 1, 4, f);
605 if (res > 0) {
606 ctxt = xmlCreatePushParserCtxt(debugSAXHandler, NULL,
607 chars, res, filename);
608 while ((res = fread(chars, 1, 3, f)) > 0) {
609 xmlParseChunk(ctxt, chars, res, 0);
610 }
611 res = xmlParseChunk(ctxt, chars, 0, 1);
612 xmlFreeParserCtxt(ctxt);
613 if (res != 0) {
614 fprintf(stdout,
615 "xmlSAXUserParseFile returned error %d\n", res);
616 }
617 }
618 fclose(f);
619 }
620 } else {
621 /*
622 * Empty callbacks for checking
623 */
624 res = xmlSAXUserParseFile(emptySAXHandler, NULL, filename);
625 if (res != 0) {
626 fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
627 }
628
629 /*
630 * Debug callback
631 */
632 res = xmlSAXUserParseFile(debugSAXHandler, NULL, filename);
633 if (res != 0) {
634 fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
635 }
Daniel Veillard5099ae81999-04-21 20:12:07 +0000636 }
637}
638
Daniel Veillard5099ae81999-04-21 20:12:07 +0000639
640int main(int argc, char **argv) {
641 int i;
642 int files = 0;
643
644 for (i = 1; i < argc ; i++) {
645 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
646 debug++;
647 else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
648 copy++;
649 else if ((!strcmp(argv[i], "-recover")) ||
650 (!strcmp(argv[i], "--recover")))
651 recovery++;
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000652 else if ((!strcmp(argv[i], "-push")) ||
653 (!strcmp(argv[i], "--push")))
654 push++;
Daniel Veillard5099ae81999-04-21 20:12:07 +0000655 }
656 for (i = 1; i < argc ; i++) {
657 if (argv[i][0] != '-') {
658 parseAndPrintFile(argv[i]);
659 files ++;
660 }
661 }
Daniel Veillardf5c2c871999-12-01 09:51:45 +0000662 xmlCleanupParser();
663 xmlMemoryDump();
Daniel Veillard5099ae81999-04-21 20:12:07 +0000664
665 return(0);
666}