blob: 2a57385b8da22127d725fd0866a01ac9f5af4c60 [file] [log] [blame]
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001/*
2 * xmllint.c : a small tester program for XML input.
3 *
4 * See Copyright for the status of this software.
5 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00006 * daniel@veillard.com
Daniel Veillardce8b83b2000-04-05 18:38:42 +00007 */
8
Bjorn Reese70a9da52001-04-21 16:57:29 +00009#include "libxml.h"
Daniel Veillardce8b83b2000-04-05 18:38:42 +000010
Daniel Veillardce8b83b2000-04-05 18:38:42 +000011#include <string.h>
Daniel Veillardce8b83b2000-04-05 18:38:42 +000012#include <stdarg.h>
Daniel Veillard8a1b1852003-01-05 22:37:17 +000013#include <assert.h>
14
Daniel Veillard3c5ed912002-01-08 10:36:16 +000015#if defined (_WIN32) && !defined(__CYGWIN__)
Daniel Veillard07cb8222003-09-10 10:51:05 +000016#if defined (_MSC_VER) || defined(__BORLANDC__)
Daniel Veillard2d90de42001-04-16 17:46:18 +000017#include <winsock2.h>
18#pragma comment(lib, "ws2_32.lib")
19#define gettimeofday(p1,p2)
20#endif /* _MSC_VER */
Igor Zlatkovic19b87642003-08-28 12:32:04 +000021#endif /* _WIN32 */
22
Daniel Veillarded472f32001-12-13 08:48:14 +000023#ifdef HAVE_SYS_TIME_H
Daniel Veillard48b2f892001-02-25 16:11:03 +000024#include <sys/time.h>
Daniel Veillarded472f32001-12-13 08:48:14 +000025#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +000026#ifdef HAVE_TIME_H
27#include <time.h>
28#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +000029
Daniel Veillard1638a472003-08-14 01:23:25 +000030#ifdef __MINGW32__
31#define _WINSOCKAPI_
32#include <wsockcompat.h>
33#include <winsock2.h>
Daniel Veillardc284c642005-03-31 10:24:24 +000034#undef XML_SOCKLEN_T
35#define XML_SOCKLEN_T unsigned int
Daniel Veillard1638a472003-08-14 01:23:25 +000036#endif
37
Daniel Veillard90bc3712002-03-07 15:12:58 +000038#ifdef HAVE_SYS_TIMEB_H
39#include <sys/timeb.h>
40#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +000041
42#ifdef HAVE_SYS_TYPES_H
43#include <sys/types.h>
44#endif
45#ifdef HAVE_SYS_STAT_H
46#include <sys/stat.h>
47#endif
48#ifdef HAVE_FCNTL_H
49#include <fcntl.h>
50#endif
51#ifdef HAVE_UNISTD_H
52#include <unistd.h>
53#endif
Daniel Veillard46e370e2000-07-21 20:32:03 +000054#ifdef HAVE_SYS_MMAN_H
55#include <sys/mman.h>
Daniel Veillard87b95392000-08-12 21:12:04 +000056/* seems needed for Solaris */
57#ifndef MAP_FAILED
58#define MAP_FAILED ((void *) -1)
59#endif
Daniel Veillard46e370e2000-07-21 20:32:03 +000060#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +000061#ifdef HAVE_STDLIB_H
62#include <stdlib.h>
63#endif
64#ifdef HAVE_LIBREADLINE
65#include <readline/readline.h>
66#ifdef HAVE_LIBHISTORY
67#include <readline/history.h>
68#endif
69#endif
70
71#include <libxml/xmlmemory.h>
72#include <libxml/parser.h>
73#include <libxml/parserInternals.h>
74#include <libxml/HTMLparser.h>
75#include <libxml/HTMLtree.h>
76#include <libxml/tree.h>
77#include <libxml/xpath.h>
78#include <libxml/debugXML.h>
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +000079#include <libxml/xmlerror.h>
Daniel Veillard9e8bfae2000-11-06 16:43:11 +000080#ifdef LIBXML_XINCLUDE_ENABLED
81#include <libxml/xinclude.h>
82#endif
Daniel Veillard81418e32001-05-22 15:08:55 +000083#ifdef LIBXML_CATALOG_ENABLED
84#include <libxml/catalog.h>
85#endif
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000086#include <libxml/globals.h>
Daniel Veillard7704fb12003-01-03 16:19:51 +000087#include <libxml/xmlreader.h>
Daniel Veillard71531f32003-02-05 13:19:53 +000088#ifdef LIBXML_SCHEMAS_ENABLED
89#include <libxml/relaxng.h>
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000090#include <libxml/xmlschemas.h>
Daniel Veillard71531f32003-02-05 13:19:53 +000091#endif
Daniel Veillardb3de70c2003-12-02 22:32:15 +000092#ifdef LIBXML_PATTERN_ENABLED
93#include <libxml/pattern.h>
94#endif
Daniel Veillard6ebf3c42004-08-22 13:11:39 +000095#ifdef LIBXML_C14N_ENABLED
96#include <libxml/c14n.h>
97#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +000098
Daniel Veillard3be27512003-01-26 19:49:04 +000099#ifndef XML_XML_DEFAULT_CATALOG
100#define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog"
101#endif
102
William M. Brack8304d872004-06-08 13:29:32 +0000103typedef enum {
104 XMLLINT_RETURN_OK = 0, /* No error */
105 XMLLINT_ERR_UNCLASS, /* Unclassified */
106 XMLLINT_ERR_DTD, /* Error in DTD */
107 XMLLINT_ERR_VALID, /* Validation error */
108 XMLLINT_ERR_RDFILE, /* CtxtReadFile error */
109 XMLLINT_ERR_SCHEMACOMP, /* Schema compilation */
110 XMLLINT_ERR_OUT, /* Error writing output */
111 XMLLINT_ERR_SCHEMAPAT, /* Error in schema pattern */
112 XMLLINT_ERR_RDREGIS, /* Error in Reader registration */
113 XMLLINT_ERR_MEM /* Out of memory error */
114} xmllintReturnCode;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000115#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000116static int shell = 0;
117static int debugent = 0;
118#endif
Daniel Veillard8326e732003-01-07 00:19:07 +0000119static int debug = 0;
Daniel Veillard87076042004-05-03 22:54:49 +0000120static int maxmem = 0;
Daniel Veillard652327a2003-09-29 18:02:38 +0000121#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000122static int copy = 0;
Daniel Veillard652327a2003-09-29 18:02:38 +0000123#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000124static int recovery = 0;
125static int noent = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000126static int noblanks = 0;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000127static int noout = 0;
128static int nowrap = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000129#ifdef LIBXML_OUTPUT_ENABLED
130static int format = 0;
131static const char *output = NULL;
132static int compress = 0;
133#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4432df22003-09-28 18:58:27 +0000134#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000135static int valid = 0;
136static int postvalid = 0;
Daniel Veillardcd429612000-10-11 15:57:05 +0000137static char * dtdvalid = NULL;
Daniel Veillard66f68e72003-08-18 16:39:51 +0000138static char * dtdvalidfpi = NULL;
Daniel Veillard4432df22003-09-28 18:58:27 +0000139#endif
Daniel Veillard71531f32003-02-05 13:19:53 +0000140#ifdef LIBXML_SCHEMAS_ENABLED
141static char * relaxng = NULL;
142static xmlRelaxNGPtr relaxngschemas = NULL;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +0000143static char * schema = NULL;
144static xmlSchemaPtr wxschemas = NULL;
Daniel Veillard71531f32003-02-05 13:19:53 +0000145#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000146static int repeat = 0;
147static int insert = 0;
Daniel Veillard656ce942004-04-30 23:11:45 +0000148#if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000149static int html = 0;
Daniel Veillard42fd4122003-11-04 08:47:48 +0000150static int xmlout = 0;
Daniel Veillard4432df22003-09-28 18:58:27 +0000151#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000152static int htmlout = 0;
Daniel Veillard73b013f2003-09-30 12:36:01 +0000153#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000154static int push = 0;
Daniel Veillard73b013f2003-09-30 12:36:01 +0000155#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +0000156#ifdef HAVE_SYS_MMAN_H
157static int memory = 0;
158#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +0000159static int testIO = 0;
Daniel Veillardbe803962000-06-28 23:40:59 +0000160static char *encoding = NULL;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000161#ifdef LIBXML_XINCLUDE_ENABLED
162static int xinclude = 0;
163#endif
Daniel Veillard48da9102001-08-07 01:10:10 +0000164static int dtdattrs = 0;
Daniel Veillard10ea86c2001-06-20 13:55:33 +0000165static int loaddtd = 0;
William M. Brack8304d872004-06-08 13:29:32 +0000166static xmllintReturnCode progresult = XMLLINT_RETURN_OK;
Daniel Veillard48b2f892001-02-25 16:11:03 +0000167static int timing = 0;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000168static int generate = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000169static int dropdtd = 0;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000170#ifdef LIBXML_CATALOG_ENABLED
171static int catalogs = 0;
172static int nocatalogs = 0;
173#endif
Daniel Veillard25048d82004-08-14 22:37:54 +0000174#ifdef LIBXML_C14N_ENABLED
175static int canonical = 0;
Aleksey Sanin2650df12005-06-06 17:16:50 +0000176static int exc_canonical = 0;
Daniel Veillard25048d82004-08-14 22:37:54 +0000177#endif
Daniel Veillard81273902003-09-30 00:43:48 +0000178#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000179static int stream = 0;
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000180static int walker = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000181#endif /* LIBXML_READER_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000182static int chkregister = 0;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +0000183static int nbregister = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000184#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +0000185static int sax1 = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000186#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000187#ifdef LIBXML_PATTERN_ENABLED
188static const char *pattern = NULL;
189static xmlPatternPtr patternc = NULL;
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000190static xmlStreamCtxtPtr patstream = NULL;
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000191#endif
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000192static int options = 0;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000193
Daniel Veillard87076042004-05-03 22:54:49 +0000194/************************************************************************
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000195 * *
196 * Entity loading control and customization. *
197 * *
198 ************************************************************************/
199#define MAX_PATHS 64
200static xmlChar *paths[MAX_PATHS + 1];
201static int nbpaths = 0;
202static int load_trace = 0;
203
204static
205void parsePath(const xmlChar *path) {
206 const xmlChar *cur;
207
208 if (path == NULL)
209 return;
210 while (*path != 0) {
211 if (nbpaths >= MAX_PATHS) {
212 fprintf(stderr, "MAX_PATHS reached: too many paths\n");
213 return;
214 }
215 cur = path;
216 while ((*cur == ' ') || (*cur == ':'))
217 cur++;
218 path = cur;
219 while ((*cur != 0) && (*cur != ' ') && (*cur != ':'))
220 cur++;
221 if (cur != path) {
222 paths[nbpaths] = xmlStrndup(path, cur - path);
223 if (paths[nbpaths] != NULL)
224 nbpaths++;
225 path = cur;
226 }
227 }
228}
229
230xmlExternalEntityLoader defaultEntityLoader = NULL;
231
232static xmlParserInputPtr
233xmllintExternalEntityLoader(const char *URL, const char *ID,
234 xmlParserCtxtPtr ctxt) {
235 xmlParserInputPtr ret;
236 warningSAXFunc warning = NULL;
237
238 int i;
239 const char *lastsegment = URL;
240 const char *iter = URL;
241
242 if (nbpaths > 0) {
243 while (*iter != 0) {
244 if (*iter == '/')
245 lastsegment = iter + 1;
246 iter++;
247 }
248 }
249
250 if ((ctxt != NULL) && (ctxt->sax != NULL)) {
251 warning = ctxt->sax->warning;
252 ctxt->sax->warning = NULL;
253 }
254
255 if (defaultEntityLoader != NULL) {
256 ret = defaultEntityLoader(URL, ID, ctxt);
257 if (ret != NULL) {
258 if (warning != NULL)
259 ctxt->sax->warning = warning;
260 if (load_trace) {
261 fprintf \
262 (stderr,
263 "Loaded URL=\"%s\" ID=\"%s\"\n",
264 URL ? URL : "(null)",
265 ID ? ID : "(null)");
266 }
267 return(ret);
268 }
269 }
270 for (i = 0;i < nbpaths;i++) {
271 xmlChar *newURL;
272
273 newURL = xmlStrdup((const xmlChar *) paths[i]);
274 newURL = xmlStrcat(newURL, (const xmlChar *) "/");
275 newURL = xmlStrcat(newURL, (const xmlChar *) lastsegment);
276 if (newURL != NULL) {
277 ret = defaultEntityLoader((const char *)newURL, ID, ctxt);
278 if (ret != NULL) {
279 if (warning != NULL)
280 ctxt->sax->warning = warning;
281 if (load_trace) {
282 fprintf \
283 (stderr,
284 "Loaded URL=\"%s\" ID=\"%s\"\n",
285 newURL,
286 ID ? ID : "(null)");
287 }
288 xmlFree(newURL);
289 return(ret);
290 }
291 xmlFree(newURL);
292 }
293 }
294 if (warning != NULL) {
295 ctxt->sax->warning = warning;
296 if (URL != NULL)
297 warning(ctxt, "failed to load external entity \"%s\"\n", URL);
298 else if (ID != NULL)
299 warning(ctxt, "failed to load external entity \"%s\"\n", ID);
300 }
301 return(NULL);
302}
303/************************************************************************
Daniel Veillard87076042004-05-03 22:54:49 +0000304 * *
305 * Memory allocation consumption debugging *
306 * *
307 ************************************************************************/
308
Daniel Veillard3af3b592004-05-05 19:22:30 +0000309static void
310OOM(void)
311{
Daniel Veillard87076042004-05-03 22:54:49 +0000312 fprintf(stderr, "Ran out of memory needs > %d bytes\n", maxmem);
William M. Brack8304d872004-06-08 13:29:32 +0000313 progresult = XMLLINT_ERR_MEM;
Daniel Veillard87076042004-05-03 22:54:49 +0000314}
315
Daniel Veillard3af3b592004-05-05 19:22:30 +0000316static void
317myFreeFunc(void *mem)
318{
Daniel Veillard87076042004-05-03 22:54:49 +0000319 xmlMemFree(mem);
320}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000321static void *
322myMallocFunc(size_t size)
323{
Daniel Veillard87076042004-05-03 22:54:49 +0000324 void *ret;
325
326 ret = xmlMemMalloc(size);
327 if (ret != NULL) {
328 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000329 OOM();
330 xmlMemFree(ret);
331 return (NULL);
332 }
Daniel Veillard87076042004-05-03 22:54:49 +0000333 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000334 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000335}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000336static void *
337myReallocFunc(void *mem, size_t size)
338{
Daniel Veillard87076042004-05-03 22:54:49 +0000339 void *ret;
340
341 ret = xmlMemRealloc(mem, size);
342 if (ret != NULL) {
343 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000344 OOM();
345 xmlMemFree(ret);
346 return (NULL);
347 }
Daniel Veillard87076042004-05-03 22:54:49 +0000348 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000349 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000350}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000351static char *
352myStrdupFunc(const char *str)
353{
Daniel Veillard87076042004-05-03 22:54:49 +0000354 char *ret;
355
356 ret = xmlMemoryStrdup(str);
357 if (ret != NULL) {
358 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000359 OOM();
360 xmlFree(ret);
361 return (NULL);
362 }
Daniel Veillard87076042004-05-03 22:54:49 +0000363 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000364 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000365}
Daniel Veillard87076042004-05-03 22:54:49 +0000366/************************************************************************
367 * *
368 * Internal timing routines to remove the necessity to have *
369 * unix-specific function calls. *
370 * *
371 ************************************************************************/
Daniel Veillard01db67c2001-12-18 07:09:59 +0000372
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000373#ifndef HAVE_GETTIMEOFDAY
374#ifdef HAVE_SYS_TIMEB_H
375#ifdef HAVE_SYS_TIME_H
376#ifdef HAVE_FTIME
377
Daniel Veillard01c13b52002-12-10 15:19:08 +0000378static int
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000379my_gettimeofday(struct timeval *tvp, void *tzp)
380{
381 struct timeb timebuffer;
382
383 ftime(&timebuffer);
384 if (tvp) {
385 tvp->tv_sec = timebuffer.time;
386 tvp->tv_usec = timebuffer.millitm * 1000L;
387 }
388 return (0);
389}
390#define HAVE_GETTIMEOFDAY 1
391#define gettimeofday my_gettimeofday
392
393#endif /* HAVE_FTIME */
394#endif /* HAVE_SYS_TIME_H */
395#endif /* HAVE_SYS_TIMEB_H */
396#endif /* !HAVE_GETTIMEOFDAY */
397
Daniel Veillard01db67c2001-12-18 07:09:59 +0000398#if defined(HAVE_GETTIMEOFDAY)
399static struct timeval begin, end;
400
401/*
402 * startTimer: call where you want to start timing
403 */
404static void
405startTimer(void)
406{
407 gettimeofday(&begin, NULL);
408}
409
410/*
411 * endTimer: call where you want to stop timing and to print out a
412 * message about the timing performed; format is a printf
413 * type argument
414 */
415static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000416endTimer(const char *fmt, ...)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000417{
418 long msec;
419 va_list ap;
420
421 gettimeofday(&end, NULL);
422 msec = end.tv_sec - begin.tv_sec;
423 msec *= 1000;
424 msec += (end.tv_usec - begin.tv_usec) / 1000;
425
426#ifndef HAVE_STDARG_H
427#error "endTimer required stdarg functions"
428#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000429 va_start(ap, fmt);
430 vfprintf(stderr, fmt, ap);
Daniel Veillard01db67c2001-12-18 07:09:59 +0000431 va_end(ap);
432
433 fprintf(stderr, " took %ld ms\n", msec);
434}
435#elif defined(HAVE_TIME_H)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000436/*
437 * No gettimeofday function, so we have to make do with calling clock.
438 * This is obviously less accurate, but there's little we can do about
439 * that.
440 */
Daniel Veillard90bc3712002-03-07 15:12:58 +0000441#ifndef CLOCKS_PER_SEC
442#define CLOCKS_PER_SEC 100
443#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +0000444
445static clock_t begin, end;
446static void
447startTimer(void)
448{
449 begin = clock();
450}
451static void
452endTimer(const char *fmt, ...)
453{
454 long msec;
455 va_list ap;
456
457 end = clock();
458 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
459
460#ifndef HAVE_STDARG_H
461#error "endTimer required stdarg functions"
462#endif
463 va_start(ap, fmt);
464 vfprintf(stderr, fmt, ap);
465 va_end(ap);
466 fprintf(stderr, " took %ld ms\n", msec);
467}
468#else
469
470/*
471 * We don't have a gettimeofday or time.h, so we just don't do timing
472 */
473static void
474startTimer(void)
475{
476 /*
477 * Do nothing
478 */
479}
480static void
481endTimer(char *format, ...)
482{
483 /*
484 * We cannot do anything because we don't have a timing function
485 */
486#ifdef HAVE_STDARG_H
487 va_start(ap, format);
488 vfprintf(stderr, format, ap);
489 va_end(ap);
490 fprintf(stderr, " was not timed\n", msec);
491#else
492 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
493 * this ?!
494 */
495#endif
496}
497#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000498/************************************************************************
499 * *
500 * HTML ouput *
501 * *
502 ************************************************************************/
503char buffer[50000];
504
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000505static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000506xmlHTMLEncodeSend(void) {
507 char *result;
508
509 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
510 if (result) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000511 xmlGenericError(xmlGenericErrorContext, "%s", result);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000512 xmlFree(result);
513 }
514 buffer[0] = 0;
515}
516
517/**
518 * xmlHTMLPrintFileInfo:
519 * @input: an xmlParserInputPtr input
520 *
521 * Displays the associated file and line informations for the current input
522 */
523
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000524static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000525xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000526 int len;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000527 xmlGenericError(xmlGenericErrorContext, "<p>");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000528
529 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000530 if (input != NULL) {
531 if (input->filename) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000532 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000533 input->line);
534 } else {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000535 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000536 }
537 }
538 xmlHTMLEncodeSend();
539}
540
541/**
542 * xmlHTMLPrintFileContext:
543 * @input: an xmlParserInputPtr input
544 *
545 * Displays current context within the input content for error tracking
546 */
547
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000548static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000549xmlHTMLPrintFileContext(xmlParserInputPtr input) {
550 const xmlChar *cur, *base;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000551 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000552 int n;
553
554 if (input == NULL) return;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000555 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000556 cur = input->cur;
557 base = input->base;
558 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
559 cur--;
560 }
561 n = 0;
562 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
563 cur--;
564 if ((*cur == '\n') || (*cur == '\r')) cur++;
565 base = cur;
566 n = 0;
567 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000568 len = strlen(buffer);
569 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
570 (unsigned char) *cur++);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000571 n++;
572 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000573 len = strlen(buffer);
574 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000575 cur = input->cur;
576 while ((*cur == '\n') || (*cur == '\r'))
577 cur--;
578 n = 0;
579 while ((cur != base) && (n++ < 80)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000580 len = strlen(buffer);
581 snprintf(&buffer[len], sizeof(buffer) - len, " ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000582 base++;
583 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000584 len = strlen(buffer);
585 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000586 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000587 xmlGenericError(xmlGenericErrorContext, "</pre>");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000588}
589
590/**
591 * xmlHTMLError:
592 * @ctx: an XML parser context
593 * @msg: the message to display/transmit
594 * @...: extra parameters for the message display
595 *
596 * Display and format an error messages, gives file, line, position and
597 * extra parameters.
598 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000599static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000600xmlHTMLError(void *ctx, const char *msg, ...)
601{
602 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
603 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000604 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000605 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000606
607 buffer[0] = 0;
608 input = ctxt->input;
609 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000610 input = ctxt->inputTab[ctxt->inputNr - 2];
611 }
612
613 xmlHTMLPrintFileInfo(input);
614
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000615 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000616 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000617 len = strlen(buffer);
618 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000619 va_end(args);
620 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000621 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000622
623 xmlHTMLPrintFileContext(input);
624 xmlHTMLEncodeSend();
625}
626
627/**
628 * xmlHTMLWarning:
629 * @ctx: an XML parser context
630 * @msg: the message to display/transmit
631 * @...: extra parameters for the message display
632 *
633 * Display and format a warning messages, gives file, line, position and
634 * extra parameters.
635 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000636static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000637xmlHTMLWarning(void *ctx, const char *msg, ...)
638{
639 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
640 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000641 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000642 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000643
644 buffer[0] = 0;
645 input = ctxt->input;
646 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000647 input = ctxt->inputTab[ctxt->inputNr - 2];
648 }
649
650
651 xmlHTMLPrintFileInfo(input);
652
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000653 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000654 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000655 len = strlen(buffer);
656 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000657 va_end(args);
658 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000659 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000660
661 xmlHTMLPrintFileContext(input);
662 xmlHTMLEncodeSend();
663}
664
665/**
666 * xmlHTMLValidityError:
667 * @ctx: an XML parser context
668 * @msg: the message to display/transmit
669 * @...: extra parameters for the message display
670 *
671 * Display and format an validity error messages, gives file,
672 * line, position and extra parameters.
673 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000674static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000675xmlHTMLValidityError(void *ctx, const char *msg, ...)
676{
677 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
678 xmlParserInputPtr input;
679 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000680 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000681
682 buffer[0] = 0;
683 input = ctxt->input;
684 if ((input->filename == NULL) && (ctxt->inputNr > 1))
685 input = ctxt->inputTab[ctxt->inputNr - 2];
686
687 xmlHTMLPrintFileInfo(input);
688
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000689 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000690 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000691 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000692 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000693 va_end(args);
694 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000695 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000696
697 xmlHTMLPrintFileContext(input);
698 xmlHTMLEncodeSend();
Daniel Veillard9fcb4912005-03-16 12:57:31 +0000699 progresult = XMLLINT_ERR_VALID;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000700}
701
702/**
703 * xmlHTMLValidityWarning:
704 * @ctx: an XML parser context
705 * @msg: the message to display/transmit
706 * @...: extra parameters for the message display
707 *
708 * Display and format a validity warning messages, gives file, line,
709 * position and extra parameters.
710 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000711static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000712xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
713{
714 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
715 xmlParserInputPtr input;
716 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000717 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000718
719 buffer[0] = 0;
720 input = ctxt->input;
721 if ((input->filename == NULL) && (ctxt->inputNr > 1))
722 input = ctxt->inputTab[ctxt->inputNr - 2];
723
724 xmlHTMLPrintFileInfo(input);
725
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000726 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000727 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000728 len = strlen(buffer);
729 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000730 va_end(args);
731 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000732 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000733
734 xmlHTMLPrintFileContext(input);
735 xmlHTMLEncodeSend();
736}
737
738/************************************************************************
739 * *
740 * Shell Interface *
741 * *
742 ************************************************************************/
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000743#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardd0cf7f62004-11-09 16:17:02 +0000744#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000745/**
746 * xmlShellReadline:
747 * @prompt: the prompt value
748 *
749 * Read a string
750 *
751 * Returns a pointer to it or NULL on EOF the caller is expected to
752 * free the returned string.
753 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000754static char *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000755xmlShellReadline(char *prompt) {
756#ifdef HAVE_LIBREADLINE
757 char *line_read;
758
759 /* Get a line from the user. */
760 line_read = readline (prompt);
761
762 /* If the line has any text in it, save it on the history. */
763 if (line_read && *line_read)
764 add_history (line_read);
765
766 return (line_read);
767#else
768 char line_read[501];
Daniel Veillard29e43992001-12-13 22:21:58 +0000769 char *ret;
770 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000771
772 if (prompt != NULL)
773 fprintf(stdout, "%s", prompt);
774 if (!fgets(line_read, 500, stdin))
775 return(NULL);
776 line_read[500] = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000777 len = strlen(line_read);
778 ret = (char *) malloc(len + 1);
779 if (ret != NULL) {
780 memcpy (ret, line_read, len + 1);
781 }
782 return(ret);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000783#endif
784}
Daniel Veillardd0cf7f62004-11-09 16:17:02 +0000785#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000786#endif /* LIBXML_DEBUG_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000787
788/************************************************************************
789 * *
Daniel Veillard5e873c42000-04-12 13:27:38 +0000790 * I/O Interfaces *
791 * *
792 ************************************************************************/
793
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000794static int myRead(FILE *f, char * buf, int len) {
795 return(fread(buf, 1, len, f));
Daniel Veillard5e873c42000-04-12 13:27:38 +0000796}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000797static void myClose(FILE *f) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000798 if (f != stdin) {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000799 fclose(f);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000800 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000801}
802
Daniel Veillard81273902003-09-30 00:43:48 +0000803#ifdef LIBXML_READER_ENABLED
Daniel Veillard5e873c42000-04-12 13:27:38 +0000804/************************************************************************
805 * *
Daniel Veillard7704fb12003-01-03 16:19:51 +0000806 * Stream Test processing *
807 * *
808 ************************************************************************/
Daniel Veillard7704fb12003-01-03 16:19:51 +0000809static void processNode(xmlTextReaderPtr reader) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000810 const xmlChar *name, *value;
Daniel Veillard16ef8002005-01-31 00:27:50 +0000811 int type, empty;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000812
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000813 type = xmlTextReaderNodeType(reader);
Daniel Veillard16ef8002005-01-31 00:27:50 +0000814 empty = xmlTextReaderIsEmptyElement(reader);
Daniel Veillard99737f52003-03-22 14:55:50 +0000815
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000816 if (debug) {
817 name = xmlTextReaderConstName(reader);
818 if (name == NULL)
819 name = BAD_CAST "--";
Daniel Veillard7704fb12003-01-03 16:19:51 +0000820
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000821 value = xmlTextReaderConstValue(reader);
822
823
824 printf("%d %d %s %d %d",
825 xmlTextReaderDepth(reader),
826 type,
827 name,
Daniel Veillard16ef8002005-01-31 00:27:50 +0000828 empty,
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000829 xmlTextReaderHasValue(reader));
830 if (value == NULL)
831 printf("\n");
832 else {
833 printf(" %s\n", value);
834 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000835 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000836#ifdef LIBXML_PATTERN_ENABLED
837 if (patternc) {
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000838 xmlChar *path = NULL;
839 int match = -1;
840
841 if (type == XML_READER_TYPE_ELEMENT) {
842 /* do the check only on element start */
843 match = xmlPatternMatch(patternc, xmlTextReaderCurrentNode(reader));
844
845 if (match) {
846 path = xmlGetNodePath(xmlTextReaderCurrentNode(reader));
847 printf("Node %s matches pattern %s\n", path, pattern);
848 }
849 }
850 if (patstream != NULL) {
851 int ret;
852
853 if (type == XML_READER_TYPE_ELEMENT) {
854 ret = xmlStreamPush(patstream,
855 xmlTextReaderConstLocalName(reader),
856 xmlTextReaderConstNamespaceUri(reader));
857 if (ret < 0) {
858 fprintf(stderr, "xmlStreamPush() failure\n");
859 xmlFreeStreamCtxt(patstream);
860 patstream = NULL;
861 } else if (ret != match) {
862 if (path == NULL) {
863 path = xmlGetNodePath(
864 xmlTextReaderCurrentNode(reader));
865 }
866 fprintf(stderr,
867 "xmlPatternMatch and xmlStreamPush disagree\n");
868 fprintf(stderr,
869 " pattern %s node %s\n",
870 pattern, path);
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000871 }
872
873
Daniel Veillard16ef8002005-01-31 00:27:50 +0000874 }
875 if ((type == XML_READER_TYPE_END_ELEMENT) ||
876 ((type == XML_READER_TYPE_ELEMENT) && (empty))) {
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000877 ret = xmlStreamPop(patstream);
878 if (ret < 0) {
879 fprintf(stderr, "xmlStreamPop() failure\n");
880 xmlFreeStreamCtxt(patstream);
881 patstream = NULL;
882 }
883 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000884 }
Daniel Veillardf9d16912005-01-30 22:36:30 +0000885 if (path != NULL)
886 xmlFree(path);
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000887 }
888#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000889}
890
891static void streamFile(char *filename) {
892 xmlTextReaderPtr reader;
893 int ret;
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000894#ifdef HAVE_SYS_MMAN_H
895 int fd = -1;
896 struct stat info;
897 const char *base = NULL;
898 xmlParserInputBufferPtr input = NULL;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000899
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000900 if (memory) {
901 if (stat(filename, &info) < 0)
902 return;
903 if ((fd = open(filename, O_RDONLY)) < 0)
904 return;
905 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
906 if (base == (void *) MAP_FAILED)
907 return;
908
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000909 reader = xmlReaderForMemory(base, info.st_size, filename,
910 NULL, options);
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000911 } else
912#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000913 reader = xmlReaderForFile(filename, NULL, options);
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000914#ifdef LIBXML_PATTERN_ENABLED
Daniel Veillardd4301ab2005-02-03 22:24:10 +0000915 if (pattern != NULL) {
916 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
917 if (patternc == NULL) {
918 xmlGenericError(xmlGenericErrorContext,
919 "Pattern %s failed to compile\n", pattern);
920 progresult = XMLLINT_ERR_SCHEMAPAT;
921 pattern = NULL;
922 }
923 }
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000924 if (patternc != NULL) {
925 patstream = xmlPatternGetStreamCtxt(patternc);
926 if (patstream != NULL) {
927 ret = xmlStreamPush(patstream, NULL, NULL);
928 if (ret < 0) {
929 fprintf(stderr, "xmlStreamPush() failure\n");
930 xmlFreeStreamCtxt(patstream);
931 patstream = NULL;
932 }
933 }
934 }
935#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000936
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000937
Daniel Veillard7704fb12003-01-03 16:19:51 +0000938 if (reader != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +0000939#ifdef LIBXML_VALID_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000940 if (valid)
941 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +0000942 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000943#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce192eb2003-04-16 15:58:05 +0000944 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000945#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +0000946 if (relaxng != NULL) {
Daniel Veillard81514ba2003-09-16 23:17:26 +0000947 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000948 startTimer();
949 }
950 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
951 if (ret < 0) {
952 xmlGenericError(xmlGenericErrorContext,
953 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +0000954 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +0000955 relaxng = NULL;
956 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000957 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000958 endTimer("Compiling the schemas");
959 }
960 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000961#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000962
963 /*
964 * Process all nodes in sequence
965 */
Daniel Veillard81514ba2003-09-16 23:17:26 +0000966 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000967 startTimer();
968 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000969 ret = xmlTextReaderRead(reader);
970 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000971 if ((debug)
972#ifdef LIBXML_PATTERN_ENABLED
973 || (patternc)
974#endif
975 )
Daniel Veillard7704fb12003-01-03 16:19:51 +0000976 processNode(reader);
977 ret = xmlTextReaderRead(reader);
978 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000979 if ((timing) && (!repeat)) {
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000980#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf54cd532004-02-25 11:52:31 +0000981 if (relaxng != NULL)
Daniel Veillard49138f12004-02-19 12:58:36 +0000982 endTimer("Parsing and validating");
983 else
Daniel Veillardf54cd532004-02-25 11:52:31 +0000984#endif
Daniel Veillard4432df22003-09-28 18:58:27 +0000985#ifdef LIBXML_VALID_ENABLED
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000986 if (valid)
Daniel Veillardce192eb2003-04-16 15:58:05 +0000987 endTimer("Parsing and validating");
988 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000989#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +0000990 endTimer("Parsing");
Daniel Veillardce192eb2003-04-16 15:58:05 +0000991 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000992
Daniel Veillard4432df22003-09-28 18:58:27 +0000993#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf6bad792003-04-11 19:38:54 +0000994 if (valid) {
995 if (xmlTextReaderIsValid(reader) != 1) {
996 xmlGenericError(xmlGenericErrorContext,
997 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +0000998 progresult = XMLLINT_ERR_VALID;
Daniel Veillardf6bad792003-04-11 19:38:54 +0000999 }
1000 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001001#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001002#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001003 if (relaxng != NULL) {
1004 if (xmlTextReaderIsValid(reader) != 1) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001005 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001006 progresult = XMLLINT_ERR_VALID;
Daniel Veillardf4e55762003-04-15 23:32:22 +00001007 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001008 fprintf(stderr, "%s validates\n", filename);
Daniel Veillardf4e55762003-04-15 23:32:22 +00001009 }
1010 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001011#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001012 /*
1013 * Done, cleanup and status
1014 */
1015 xmlFreeTextReader(reader);
1016 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001017 fprintf(stderr, "%s : failed to parse\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001018 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001019 }
1020 } else {
1021 fprintf(stderr, "Unable to open %s\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001022 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001023 }
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001024#ifdef LIBXML_PATTERN_ENABLED
1025 if (patstream != NULL) {
1026 xmlFreeStreamCtxt(patstream);
1027 patstream = NULL;
1028 }
1029#endif
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001030#ifdef HAVE_SYS_MMAN_H
1031 if (memory) {
1032 xmlFreeParserInputBuffer(input);
1033 munmap((char *) base, info.st_size);
1034 close(fd);
1035 }
1036#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001037}
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001038
1039static void walkDoc(xmlDocPtr doc) {
1040 xmlTextReaderPtr reader;
1041 int ret;
1042
Daniel Veillardd4301ab2005-02-03 22:24:10 +00001043#ifdef LIBXML_PATTERN_ENABLED
1044 xmlNodePtr root;
1045 const xmlChar *namespaces[22];
1046 int i;
1047 xmlNsPtr ns;
1048
1049 root = xmlDocGetRootElement(doc);
1050 for (ns = root->nsDef, i = 0;ns != NULL && i < 20;ns=ns->next) {
1051 namespaces[i++] = ns->href;
1052 namespaces[i++] = ns->prefix;
1053 }
1054 namespaces[i++] = NULL;
1055 namespaces[i++] = NULL;
1056
1057 if (pattern != NULL) {
1058 patternc = xmlPatterncompile((const xmlChar *) pattern, doc->dict,
1059 0, &namespaces[0]);
1060 if (patternc == NULL) {
1061 xmlGenericError(xmlGenericErrorContext,
1062 "Pattern %s failed to compile\n", pattern);
1063 progresult = XMLLINT_ERR_SCHEMAPAT;
1064 pattern = NULL;
1065 }
1066 }
Daniel Veillard2b2e02d2005-02-05 23:20:22 +00001067 if (patternc != NULL) {
1068 patstream = xmlPatternGetStreamCtxt(patternc);
1069 if (patstream != NULL) {
1070 ret = xmlStreamPush(patstream, NULL, NULL);
1071 if (ret < 0) {
1072 fprintf(stderr, "xmlStreamPush() failure\n");
1073 xmlFreeStreamCtxt(patstream);
1074 patstream = NULL;
1075 }
1076 }
1077 }
Daniel Veillardd4301ab2005-02-03 22:24:10 +00001078#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001079 reader = xmlReaderWalker(doc);
1080 if (reader != NULL) {
1081 if ((timing) && (!repeat)) {
1082 startTimer();
1083 }
1084 ret = xmlTextReaderRead(reader);
1085 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001086 if ((debug)
1087#ifdef LIBXML_PATTERN_ENABLED
1088 || (patternc)
1089#endif
1090 )
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001091 processNode(reader);
1092 ret = xmlTextReaderRead(reader);
1093 }
1094 if ((timing) && (!repeat)) {
1095 endTimer("walking through the doc");
1096 }
1097 xmlFreeTextReader(reader);
1098 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001099 fprintf(stderr, "failed to walk through the doc\n");
William M. Brack8304d872004-06-08 13:29:32 +00001100 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001101 }
1102 } else {
1103 fprintf(stderr, "Failed to crate a reader from the document\n");
William M. Brack8304d872004-06-08 13:29:32 +00001104 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001105 }
Daniel Veillard2b2e02d2005-02-05 23:20:22 +00001106#ifdef LIBXML_PATTERN_ENABLED
1107 if (patstream != NULL) {
1108 xmlFreeStreamCtxt(patstream);
1109 patstream = NULL;
1110 }
1111#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001112}
Daniel Veillard81273902003-09-30 00:43:48 +00001113#endif /* LIBXML_READER_ENABLED */
Daniel Veillard7704fb12003-01-03 16:19:51 +00001114
1115/************************************************************************
1116 * *
1117 * Tree Test processing *
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001118 * *
1119 ************************************************************************/
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001120static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
Daniel Veillard652327a2003-09-29 18:02:38 +00001121 xmlDocPtr doc = NULL;
1122#ifdef LIBXML_TREE_ENABLED
1123 xmlDocPtr tmp;
1124#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001125
Daniel Veillard48b2f892001-02-25 16:11:03 +00001126 if ((timing) && (!repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00001127 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001128
1129
Daniel Veillard652327a2003-09-29 18:02:38 +00001130#ifdef LIBXML_TREE_ENABLED
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001131 if (filename == NULL) {
1132 if (generate) {
1133 xmlNodePtr n;
1134
1135 doc = xmlNewDoc(BAD_CAST "1.0");
Daniel Veillard95ddcd32004-10-26 21:53:55 +00001136 n = xmlNewDocNode(doc, NULL, BAD_CAST "info", NULL);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001137 xmlNodeSetContent(n, BAD_CAST "abc");
1138 xmlDocSetRootElement(doc, n);
1139 }
1140 }
Daniel Veillard652327a2003-09-29 18:02:38 +00001141#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001142#ifdef LIBXML_HTML_ENABLED
Daniel Veillard73b013f2003-09-30 12:36:01 +00001143#ifdef LIBXML_PUSH_ENABLED
William M. Brack78637da2003-07-31 14:47:38 +00001144 else if ((html) && (push)) {
1145 FILE *f;
1146
William M. Brack3403add2004-06-27 02:07:51 +00001147#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
1148 f = fopen(filename, "rb");
1149#else
1150 f = fopen(filename, "r");
1151#endif
William M. Brack78637da2003-07-31 14:47:38 +00001152 if (f != NULL) {
1153 int res, size = 3;
1154 char chars[4096];
1155 htmlParserCtxtPtr ctxt;
1156
1157 /* if (repeat) */
1158 size = 4096;
1159 res = fread(chars, 1, 4, f);
1160 if (res > 0) {
1161 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
William M. Brack1d75c8a2003-10-27 13:48:16 +00001162 chars, res, filename, XML_CHAR_ENCODING_NONE);
William M. Brack78637da2003-07-31 14:47:38 +00001163 while ((res = fread(chars, 1, size, f)) > 0) {
1164 htmlParseChunk(ctxt, chars, res, 0);
1165 }
1166 htmlParseChunk(ctxt, chars, 0, 1);
1167 doc = ctxt->myDoc;
1168 htmlFreeParserCtxt(ctxt);
1169 }
1170 fclose(f);
1171 }
1172 }
Daniel Veillard73b013f2003-09-30 12:36:01 +00001173#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001174 else if (html) {
Daniel Veillard9475a352003-09-26 12:47:50 +00001175 doc = htmlReadFile(filename, NULL, options);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001176 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001177#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001178 else {
Daniel Veillard73b013f2003-09-30 12:36:01 +00001179#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001180 /*
1181 * build an XML tree from a string;
1182 */
1183 if (push) {
1184 FILE *f;
1185
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001186 /* '-' Usually means stdin -<sven@zen.org> */
1187 if ((filename[0] == '-') && (filename[1] == 0)) {
1188 f = stdin;
1189 } else {
William M. Brack3403add2004-06-27 02:07:51 +00001190#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
1191 f = fopen(filename, "rb");
1192#else
1193 f = fopen(filename, "r");
1194#endif
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001195 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001196 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +00001197 int ret;
Daniel Veillarda880b122003-04-21 21:36:41 +00001198 int res, size = 1024;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001199 char chars[1024];
1200 xmlParserCtxtPtr ctxt;
1201
Daniel Veillarda880b122003-04-21 21:36:41 +00001202 /* if (repeat) size = 1024; */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001203 res = fread(chars, 1, 4, f);
1204 if (res > 0) {
1205 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
1206 chars, res, filename);
Daniel Veillard500a1de2004-03-22 15:22:58 +00001207 xmlCtxtUseOptions(ctxt, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001208 while ((res = fread(chars, 1, size, f)) > 0) {
1209 xmlParseChunk(ctxt, chars, res, 0);
1210 }
1211 xmlParseChunk(ctxt, chars, 0, 1);
1212 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +00001213 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001214 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +00001215 if (!ret) {
1216 xmlFreeDoc(doc);
1217 doc = NULL;
1218 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001219 }
1220 }
Daniel Veillard73b013f2003-09-30 12:36:01 +00001221 } else
1222#endif /* LIBXML_PUSH_ENABLED */
1223 if (testIO) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001224 if ((filename[0] == '-') && (filename[1] == 0)) {
Daniel Veillard60942de2003-09-25 21:05:58 +00001225 doc = xmlReadFd(0, NULL, NULL, options);
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001226 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001227 FILE *f;
Daniel Veillard5e873c42000-04-12 13:27:38 +00001228
William M. Brack3403add2004-06-27 02:07:51 +00001229#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
1230 f = fopen(filename, "rb");
1231#else
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001232 f = fopen(filename, "r");
William M. Brack3403add2004-06-27 02:07:51 +00001233#endif
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001234 if (f != NULL) {
1235 if (rectxt == NULL)
1236 doc = xmlReadIO((xmlInputReadCallback) myRead,
1237 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +00001238 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001239 else
1240 doc = xmlCtxtReadIO(rectxt,
1241 (xmlInputReadCallback) myRead,
1242 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +00001243 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001244 } else
Daniel Veillard5e873c42000-04-12 13:27:38 +00001245 doc = NULL;
Daniel Veillard5e873c42000-04-12 13:27:38 +00001246 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001247 } else if (htmlout) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001248 xmlParserCtxtPtr ctxt;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001249
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001250 if (rectxt == NULL)
1251 ctxt = xmlNewParserCtxt();
1252 else
1253 ctxt = rectxt;
Daniel Veillard88a172f2000-08-04 18:23:10 +00001254 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001255 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +00001256 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001257 ctxt->sax->error = xmlHTMLError;
1258 ctxt->sax->warning = xmlHTMLWarning;
1259 ctxt->vctxt.error = xmlHTMLValidityError;
1260 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001261
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001262 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001263
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001264 if (rectxt == NULL)
1265 xmlFreeParserCtxt(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001266 }
Daniel Veillard46e370e2000-07-21 20:32:03 +00001267#ifdef HAVE_SYS_MMAN_H
1268 } else if (memory) {
1269 int fd;
1270 struct stat info;
1271 const char *base;
1272 if (stat(filename, &info) < 0)
1273 return;
1274 if ((fd = open(filename, O_RDONLY)) < 0)
1275 return;
1276 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +00001277 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +00001278 return;
1279
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001280 if (rectxt == NULL)
Daniel Veillard60942de2003-09-25 21:05:58 +00001281 doc = xmlReadMemory((char *) base, info.st_size,
1282 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001283 else
Daniel Veillard60942de2003-09-25 21:05:58 +00001284 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
1285 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001286
Daniel Veillard46e370e2000-07-21 20:32:03 +00001287 munmap((char *) base, info.st_size);
1288#endif
Daniel Veillard4432df22003-09-28 18:58:27 +00001289#ifdef LIBXML_VALID_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00001290 } else if (valid) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001291 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001292
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001293 if (rectxt == NULL)
1294 ctxt = xmlNewParserCtxt();
1295 else
1296 ctxt = rectxt;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001297 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001298 doc = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001299 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001300 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
1301
1302 if (ctxt->valid == 0)
William M. Brack8304d872004-06-08 13:29:32 +00001303 progresult = XMLLINT_ERR_RDFILE;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001304 if (rectxt == NULL)
1305 xmlFreeParserCtxt(ctxt);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001306 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001307#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00001308 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001309 if (rectxt != NULL)
1310 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
1311 else
1312 doc = xmlReadFile(filename, NULL, options);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001313 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001314 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001315
Daniel Veillard88a172f2000-08-04 18:23:10 +00001316 /*
1317 * If we don't have a document we might as well give up. Do we
1318 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001319 if (doc == NULL) {
William M. Brack8304d872004-06-08 13:29:32 +00001320 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard88a172f2000-08-04 18:23:10 +00001321 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001322 }
1323
Daniel Veillard48b2f892001-02-25 16:11:03 +00001324 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001325 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001326 }
1327
Daniel Veillard29e43992001-12-13 22:21:58 +00001328 /*
1329 * Remove DOCTYPE nodes
1330 */
1331 if (dropdtd) {
1332 xmlDtdPtr dtd;
1333
1334 dtd = xmlGetIntSubset(doc);
1335 if (dtd != NULL) {
1336 xmlUnlinkNode((xmlNodePtr)dtd);
1337 xmlFreeDtd(dtd);
1338 }
1339 }
1340
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001341#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +00001342 if (xinclude) {
1343 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001344 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001345 }
William M. Brack4e1c2db2005-02-11 10:58:55 +00001346 if (xmlXIncludeProcessFlags(doc, options) < 0)
1347 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001348 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001349 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001350 }
1351 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001352#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +00001353
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001354#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00001355#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001356 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001357 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001358 */
1359 if (shell)
1360 xmlShell(doc, filename, xmlShellReadline, stdout);
1361#endif
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00001362#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001363
Daniel Veillard652327a2003-09-29 18:02:38 +00001364#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001365 /*
1366 * test intermediate copy if needed.
1367 */
1368 if (copy) {
1369 tmp = doc;
Daniel Veillard4edd3ed2004-09-20 20:03:01 +00001370 if (timing) {
1371 startTimer();
1372 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001373 doc = xmlCopyDoc(doc, 1);
Daniel Veillard4edd3ed2004-09-20 20:03:01 +00001374 if (timing) {
1375 endTimer("Copying");
1376 }
1377 if (timing) {
1378 startTimer();
1379 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001380 xmlFreeDoc(tmp);
Daniel Veillard4edd3ed2004-09-20 20:03:01 +00001381 if (timing) {
1382 endTimer("Freeing original");
1383 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001384 }
Daniel Veillard652327a2003-09-29 18:02:38 +00001385#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001386
Daniel Veillard4432df22003-09-28 18:58:27 +00001387#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001388 if ((insert) && (!html)) {
1389 const xmlChar* list[256];
1390 int nb, i;
1391 xmlNodePtr node;
1392
1393 if (doc->children != NULL) {
1394 node = doc->children;
1395 while ((node != NULL) && (node->last == NULL)) node = node->next;
1396 if (node != NULL) {
1397 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
1398 if (nb < 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001399 fprintf(stderr, "could not get valid list of elements\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001400 } else if (nb == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001401 fprintf(stderr, "No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001402 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001403 fprintf(stderr, "%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001404 nb);
1405 for (i = 0;i < nb;i++) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001406 fprintf(stderr, "%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001407 }
1408 }
1409 }
1410 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001411 }else
1412#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001413#ifdef LIBXML_READER_ENABLED
1414 if (walker) {
1415 walkDoc(doc);
1416 }
1417#endif /* LIBXML_READER_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001418#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001419 if (noout == 0) {
Daniel Veillard3df01182003-12-10 10:17:51 +00001420 int ret;
1421
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001422 /*
1423 * print it.
1424 */
1425#ifdef LIBXML_DEBUG_ENABLED
1426 if (!debug) {
1427#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00001428 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001429 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001430 }
Daniel Veillard656ce942004-04-30 23:11:45 +00001431#ifdef LIBXML_HTML_ENABLED
Daniel Veillard42fd4122003-11-04 08:47:48 +00001432 if ((html) && (!xmlout)) {
1433 if (compress) {
1434 htmlSaveFile(output ? output : "-", doc);
1435 }
1436 else if (encoding != NULL) {
1437 if ( format ) {
1438 htmlSaveFileFormat(output ? output : "-", doc, encoding, 1);
1439 }
1440 else {
1441 htmlSaveFileFormat(output ? output : "-", doc, encoding, 0);
1442 }
1443 }
1444 else if (format) {
1445 htmlSaveFileFormat(output ? output : "-", doc, NULL, 1);
1446 }
1447 else {
1448 FILE *out;
1449 if (output == NULL)
1450 out = stdout;
1451 else {
1452 out = fopen(output,"wb");
1453 }
1454 if (out != NULL) {
1455 if (htmlDocDump(out, doc) < 0)
William M. Brack8304d872004-06-08 13:29:32 +00001456 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00001457
1458 if (output != NULL)
1459 fclose(out);
1460 } else {
1461 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00001462 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00001463 }
1464 }
1465 if ((timing) && (!repeat)) {
1466 endTimer("Saving");
1467 }
1468 } else
1469#endif
Daniel Veillard25048d82004-08-14 22:37:54 +00001470#ifdef LIBXML_C14N_ENABLED
1471 if (canonical) {
1472 xmlChar *result = NULL;
1473 int size;
1474
1475 size = xmlC14NDocDumpMemory(doc, NULL, 0, NULL, 1, &result);
1476 if (size >= 0) {
1477 write(1, result, size);
1478 xmlFree(result);
1479 } else {
1480 fprintf(stderr, "Failed to canonicalize\n");
1481 progresult = XMLLINT_ERR_OUT;
1482 }
1483 } else
Aleksey Sanin2650df12005-06-06 17:16:50 +00001484 if (exc_canonical) {
1485 xmlChar *result = NULL;
1486 int size;
1487
1488 size = xmlC14NDocDumpMemory(doc, NULL, 1, NULL, 1, &result);
1489 if (size >= 0) {
1490 write(1, result, size);
1491 xmlFree(result);
1492 } else {
1493 fprintf(stderr, "Failed to canonicalize\n");
1494 progresult = XMLLINT_ERR_OUT;
1495 }
1496 } else
Daniel Veillard25048d82004-08-14 22:37:54 +00001497#endif
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001498#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001499 if (memory) {
1500 xmlChar *result;
1501 int len;
1502
1503 if (encoding != NULL) {
Daniel Veillardd536f702001-11-08 17:32:47 +00001504 if ( format ) {
1505 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
1506 } else {
1507 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
1508 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001509 } else {
Daniel Veillard90493a92001-08-14 14:12:47 +00001510 if (format)
1511 xmlDocDumpFormatMemory(doc, &result, &len, 1);
1512 else
1513 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001514 }
1515 if (result == NULL) {
1516 fprintf(stderr, "Failed to save\n");
Daniel Veillard25048d82004-08-14 22:37:54 +00001517 progresult = XMLLINT_ERR_OUT;
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001518 } else {
1519 write(1, result, len);
1520 xmlFree(result);
1521 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001522 } else
1523#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001524 if (compress) {
1525 xmlSaveFile(output ? output : "-", doc);
1526 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001527 else if (encoding != NULL) {
1528 if ( format ) {
Daniel Veillard3df01182003-12-10 10:17:51 +00001529 ret = xmlSaveFormatFileEnc(output ? output : "-", doc,
1530 encoding, 1);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001531 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001532 else {
Daniel Veillard3df01182003-12-10 10:17:51 +00001533 ret = xmlSaveFileEnc(output ? output : "-", doc, encoding);
1534 }
1535 if (ret < 0) {
1536 fprintf(stderr, "failed save to %s\n",
1537 output ? output : "-");
William M. Brack8304d872004-06-08 13:29:32 +00001538 progresult = XMLLINT_ERR_OUT;
Daniel Veillardd536f702001-11-08 17:32:47 +00001539 }
1540 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001541 else if (format) {
Daniel Veillard3df01182003-12-10 10:17:51 +00001542 ret = xmlSaveFormatFile(output ? output : "-", doc, 1);
1543 if (ret < 0) {
1544 fprintf(stderr, "failed save to %s\n",
1545 output ? output : "-");
William M. Brack8304d872004-06-08 13:29:32 +00001546 progresult = XMLLINT_ERR_OUT;
Daniel Veillard3df01182003-12-10 10:17:51 +00001547 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001548 }
1549 else {
1550 FILE *out;
1551 if (output == NULL)
1552 out = stdout;
1553 else {
1554 out = fopen(output,"wb");
1555 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001556 if (out != NULL) {
Daniel Veillard828ce832003-10-08 19:19:10 +00001557 if (xmlDocDump(out, doc) < 0)
William M. Brack8304d872004-06-08 13:29:32 +00001558 progresult = XMLLINT_ERR_OUT;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001559
Daniel Veillard05d987b2003-10-08 11:54:57 +00001560 if (output != NULL)
1561 fclose(out);
1562 } else {
1563 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00001564 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00001565 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001566 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001567 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001568 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001569 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001570#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001571 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001572 FILE *out;
1573 if (output == NULL)
1574 out = stdout;
1575 else {
1576 out = fopen(output,"wb");
1577 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001578 if (out != NULL) {
1579 xmlDebugDumpDocument(out, doc);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001580
Daniel Veillard05d987b2003-10-08 11:54:57 +00001581 if (output != NULL)
1582 fclose(out);
1583 } else {
1584 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00001585 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00001586 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001587 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001588#endif
1589 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001590#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001591
Daniel Veillard4432df22003-09-28 18:58:27 +00001592#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001593 /*
1594 * A posteriori validation test
1595 */
Daniel Veillard66f68e72003-08-18 16:39:51 +00001596 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
Daniel Veillardcd429612000-10-11 15:57:05 +00001597 xmlDtdPtr dtd;
1598
Daniel Veillard48b2f892001-02-25 16:11:03 +00001599 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001600 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001601 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00001602 if (dtdvalid != NULL)
1603 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
1604 else
1605 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001606 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001607 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001608 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001609 if (dtd == NULL) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001610 if (dtdvalid != NULL)
1611 xmlGenericError(xmlGenericErrorContext,
1612 "Could not parse DTD %s\n", dtdvalid);
1613 else
1614 xmlGenericError(xmlGenericErrorContext,
1615 "Could not parse DTD %s\n", dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00001616 progresult = XMLLINT_ERR_DTD;
Daniel Veillardcd429612000-10-11 15:57:05 +00001617 } else {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001618 xmlValidCtxtPtr cvp;
1619
1620 if ((cvp = xmlNewValidCtxt()) == NULL) {
1621 xmlGenericError(xmlGenericErrorContext,
1622 "Couldn't allocate validation context\n");
1623 exit(-1);
1624 }
1625 cvp->userData = (void *) stderr;
1626 cvp->error = (xmlValidityErrorFunc) fprintf;
1627 cvp->warning = (xmlValidityWarningFunc) fprintf;
1628
Daniel Veillard48b2f892001-02-25 16:11:03 +00001629 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001630 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001631 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001632 if (!xmlValidateDtd(cvp, doc, dtd)) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001633 if (dtdvalid != NULL)
1634 xmlGenericError(xmlGenericErrorContext,
1635 "Document %s does not validate against %s\n",
1636 filename, dtdvalid);
1637 else
1638 xmlGenericError(xmlGenericErrorContext,
1639 "Document %s does not validate against %s\n",
1640 filename, dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00001641 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00001642 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001643 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001644 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001645 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001646 xmlFreeValidCtxt(cvp);
Daniel Veillardcd429612000-10-11 15:57:05 +00001647 xmlFreeDtd(dtd);
1648 }
1649 } else if (postvalid) {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001650 xmlValidCtxtPtr cvp;
1651
1652 if ((cvp = xmlNewValidCtxt()) == NULL) {
1653 xmlGenericError(xmlGenericErrorContext,
1654 "Couldn't allocate validation context\n");
1655 exit(-1);
1656 }
1657
Daniel Veillard48b2f892001-02-25 16:11:03 +00001658 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001659 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001660 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001661 cvp->userData = (void *) stderr;
1662 cvp->error = (xmlValidityErrorFunc) fprintf;
1663 cvp->warning = (xmlValidityWarningFunc) fprintf;
1664 if (!xmlValidateDocument(cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001665 xmlGenericError(xmlGenericErrorContext,
1666 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001667 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00001668 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001669 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001670 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001671 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001672 xmlFreeValidCtxt(cvp);
Daniel Veillard4432df22003-09-28 18:58:27 +00001673 }
1674#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00001675#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001676 if (relaxngschemas != NULL) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001677 xmlRelaxNGValidCtxtPtr ctxt;
1678 int ret;
1679
Daniel Veillard42f12e92003-03-07 18:32:59 +00001680 if ((timing) && (!repeat)) {
1681 startTimer();
1682 }
1683
Daniel Veillard71531f32003-02-05 13:19:53 +00001684 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
1685 xmlRelaxNGSetValidErrors(ctxt,
1686 (xmlRelaxNGValidityErrorFunc) fprintf,
1687 (xmlRelaxNGValidityWarningFunc) fprintf,
1688 stderr);
1689 ret = xmlRelaxNGValidateDoc(ctxt, doc);
1690 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001691 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard71531f32003-02-05 13:19:53 +00001692 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001693 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001694 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00001695 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001696 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard71531f32003-02-05 13:19:53 +00001697 filename);
William M. Brack8304d872004-06-08 13:29:32 +00001698 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00001699 }
1700 xmlRelaxNGFreeValidCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001701 if ((timing) && (!repeat)) {
1702 endTimer("Validating");
1703 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001704 } else if (wxschemas != NULL) {
1705 xmlSchemaValidCtxtPtr ctxt;
1706 int ret;
1707
1708 if ((timing) && (!repeat)) {
1709 startTimer();
1710 }
1711
1712 ctxt = xmlSchemaNewValidCtxt(wxschemas);
1713 xmlSchemaSetValidErrors(ctxt,
1714 (xmlSchemaValidityErrorFunc) fprintf,
1715 (xmlSchemaValidityWarningFunc) fprintf,
1716 stderr);
1717 ret = xmlSchemaValidateDoc(ctxt, doc);
1718 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001719 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001720 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001721 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001722 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001723 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001724 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001725 filename);
William M. Brack8304d872004-06-08 13:29:32 +00001726 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001727 }
1728 xmlSchemaFreeValidCtxt(ctxt);
1729 if ((timing) && (!repeat)) {
1730 endTimer("Validating");
1731 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001732 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001733#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001734
1735#ifdef LIBXML_DEBUG_ENABLED
1736 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001737 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001738#endif
1739
1740 /*
1741 * free it.
1742 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00001743 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001744 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001745 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001746 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001747 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001748 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001749 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001750}
1751
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001752/************************************************************************
1753 * *
1754 * Usage and Main *
1755 * *
1756 ************************************************************************/
1757
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001758static void showVersion(const char *name) {
1759 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
1760 fprintf(stderr, " compiled with: ");
Daniel Veillard4432df22003-09-28 18:58:27 +00001761#ifdef LIBXML_VALID_ENABLED
1762 fprintf(stderr, "DTDValid ");
1763#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001764#ifdef LIBXML_FTP_ENABLED
1765 fprintf(stderr, "FTP ");
1766#endif
1767#ifdef LIBXML_HTTP_ENABLED
1768 fprintf(stderr, "HTTP ");
1769#endif
1770#ifdef LIBXML_HTML_ENABLED
1771 fprintf(stderr, "HTML ");
1772#endif
1773#ifdef LIBXML_C14N_ENABLED
1774 fprintf(stderr, "C14N ");
1775#endif
1776#ifdef LIBXML_CATALOG_ENABLED
1777 fprintf(stderr, "Catalog ");
1778#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001779#ifdef LIBXML_XPATH_ENABLED
1780 fprintf(stderr, "XPath ");
1781#endif
1782#ifdef LIBXML_XPTR_ENABLED
1783 fprintf(stderr, "XPointer ");
1784#endif
1785#ifdef LIBXML_XINCLUDE_ENABLED
1786 fprintf(stderr, "XInclude ");
1787#endif
1788#ifdef LIBXML_ICONV_ENABLED
1789 fprintf(stderr, "Iconv ");
1790#endif
1791#ifdef DEBUG_MEMORY_LOCATION
1792 fprintf(stderr, "MemDebug ");
1793#endif
1794#ifdef LIBXML_UNICODE_ENABLED
1795 fprintf(stderr, "Unicode ");
1796#endif
1797#ifdef LIBXML_REGEXP_ENABLED
1798 fprintf(stderr, "Regexps ");
1799#endif
1800#ifdef LIBXML_AUTOMATA_ENABLED
1801 fprintf(stderr, "Automata ");
1802#endif
1803#ifdef LIBXML_SCHEMAS_ENABLED
1804 fprintf(stderr, "Schemas ");
1805#endif
Daniel Veillardce1648b2005-01-04 15:10:22 +00001806#ifdef LIBXML_MODULES_ENABLED
1807 fprintf(stderr, "Modules ");
1808#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001809 fprintf(stderr, "\n");
1810}
1811
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001812static void usage(const char *name) {
1813 printf("Usage : %s [options] XMLfiles ...\n", name);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001814#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001815 printf("\tParse the XML files and output the result of the parsing\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001816#else
1817 printf("\tParse the XML files\n");
1818#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001819 printf("\t--version : display the version of the XML library used\n");
1820#ifdef LIBXML_DEBUG_ENABLED
1821 printf("\t--debug : dump a debug tree of the in-memory document\n");
1822 printf("\t--shell : run a navigating shell\n");
1823 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00001824#else
Daniel Veillard81273902003-09-30 00:43:48 +00001825#ifdef LIBXML_READER_ENABLED
Daniel Veillard8326e732003-01-07 00:19:07 +00001826 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001827#endif /* LIBXML_READER_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001828#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001829#ifdef LIBXML_TREE_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001830 printf("\t--copy : used to test the internal copy implementation\n");
Daniel Veillard652327a2003-09-29 18:02:38 +00001831#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001832 printf("\t--recover : output what was parsable on broken XML documents\n");
1833 printf("\t--noent : substitute entity references by their value\n");
1834 printf("\t--noout : don't output the result tree\n");
Daniel Veillard0bff36d2004-08-31 09:37:03 +00001835 printf("\t--path 'paths': provide a set of paths for resources\n");
1836 printf("\t--load-trace : print trace of all external entites loaded\n");
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001837 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001838 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001839 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001840#ifdef LIBXML_VALID_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001841 printf("\t--valid : validate the document in addition to std well-formed check\n");
1842 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
1843 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
Daniel Veillard66f68e72003-08-18 16:39:51 +00001844 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001845#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001846 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001847 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001848 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
1849 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001850#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001851#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001852 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001853#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001854#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001855#ifdef LIBXML_HTML_ENABLED
1856 printf("\t--html : use the HTML parser\n");
Daniel Veillard42fd4122003-11-04 08:47:48 +00001857 printf("\t--xmlout : force to use the XML serializer when using --html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001858#endif
Daniel Veillard73b013f2003-09-30 12:36:01 +00001859#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001860 printf("\t--push : use the push mode of the parser\n");
Daniel Veillard73b013f2003-09-30 12:36:01 +00001861#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001862#ifdef HAVE_SYS_MMAN_H
1863 printf("\t--memory : parse from memory\n");
1864#endif
Daniel Veillard87076042004-05-03 22:54:49 +00001865 printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001866 printf("\t--nowarning : do not emit warnings from parser/validator\n");
1867 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001868 printf("\t--nocdata : replace cdata section with text nodes\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001869#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001870 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001871 printf("\t--encode encoding : output in the given encoding\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001872 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
1873#endif /* LIBXML_OUTPUT_ENABLED */
Aleksey Sanin2650df12005-06-06 17:16:50 +00001874 printf("\t--c14n : save in W3C canonical format (with comments)\n");
1875 printf("\t--exc-c14n : save in W3C exclusive canonical format (with comments)\n");
Daniel Veillard25048d82004-08-14 22:37:54 +00001876#ifdef LIBXML_C14N_ENABLED
1877#endif /* LIBXML_C14N_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001878 printf("\t--nsclean : remove redundant namespace declarations\n");
1879 printf("\t--testIO : test user I/O support\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001880#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00001881 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
1882 printf("\t otherwise XML Catalogs starting from \n");
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001883 printf("\t %s are activated by default\n", XML_XML_DEFAULT_CATALOG);
Daniel Veillard05c13a22001-09-09 08:38:09 +00001884 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001885#endif
1886 printf("\t--auto : generate a small doc on the fly\n");
1887#ifdef LIBXML_XINCLUDE_ENABLED
1888 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00001889 printf("\t--noxincludenode : same but do not generate XInclude nodes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001890#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001891 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00001892 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard81273902003-09-30 00:43:48 +00001893#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001894 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001895 printf("\t--walker : create a reader and walk though the resulting doc\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001896#endif /* LIBXML_READER_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001897#ifdef LIBXML_PATTERN_ENABLED
1898 printf("\t--pattern pattern_value : test the pattern support\n");
1899#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001900 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillardef4d3bc2003-02-07 12:38:22 +00001901#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard71531f32003-02-05 13:19:53 +00001902 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001903 printf("\t--schema schema : do validation against the WXS schema\n");
Daniel Veillard71531f32003-02-05 13:19:53 +00001904#endif
Daniel Veillarda42f25f2002-01-25 14:15:40 +00001905 printf("\nLibxml project home page: http://xmlsoft.org/\n");
1906 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001907}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001908
1909static void registerNode(xmlNodePtr node)
1910{
1911 node->_private = malloc(sizeof(long));
1912 *(long*)node->_private = (long) 0x81726354;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00001913 nbregister++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001914}
1915
1916static void deregisterNode(xmlNodePtr node)
1917{
1918 assert(node->_private != NULL);
1919 assert(*(long*)node->_private == (long) 0x81726354);
1920 free(node->_private);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00001921 nbregister--;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001922}
1923
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001924int
1925main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001926 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001927 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00001928 int version = 0;
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001929 const char* indent;
1930
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001931 if (argc <= 1) {
1932 usage(argv[0]);
1933 return(1);
1934 }
Daniel Veillardbe803962000-06-28 23:40:59 +00001935 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001936 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001937 if (!strcmp(argv[i], "-"))
1938 break;
1939
1940 if (argv[i][0] != '-')
1941 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001942 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
1943 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00001944 else
1945#ifdef LIBXML_DEBUG_ENABLED
1946 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001947 (!strcmp(argv[i], "--shell"))) {
1948 shell++;
1949 noout = 1;
1950 } else
1951#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001952#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001953 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
1954 copy++;
Daniel Veillard652327a2003-09-29 18:02:38 +00001955 else
1956#endif /* LIBXML_TREE_ENABLED */
1957 if ((!strcmp(argv[i], "-recover")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001958 (!strcmp(argv[i], "--recover"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001959 recovery++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001960 options |= XML_PARSE_RECOVER;
1961 } else if ((!strcmp(argv[i], "-noent")) ||
1962 (!strcmp(argv[i], "--noent"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001963 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001964 options |= XML_PARSE_NOENT;
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001965 } else if ((!strcmp(argv[i], "-nsclean")) ||
1966 (!strcmp(argv[i], "--nsclean"))) {
1967 options |= XML_PARSE_NSCLEAN;
1968 } else if ((!strcmp(argv[i], "-nocdata")) ||
1969 (!strcmp(argv[i], "--nocdata"))) {
1970 options |= XML_PARSE_NOCDATA;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001971 } else if ((!strcmp(argv[i], "-nodict")) ||
1972 (!strcmp(argv[i], "--nodict"))) {
1973 options |= XML_PARSE_NODICT;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001974 } else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00001975 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001976 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00001977 version = 1;
1978 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001979 (!strcmp(argv[i], "--noout")))
1980 noout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001981#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001982 else if ((!strcmp(argv[i], "-o")) ||
1983 (!strcmp(argv[i], "-output")) ||
1984 (!strcmp(argv[i], "--output"))) {
1985 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00001986 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001987 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001988#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001989 else if ((!strcmp(argv[i], "-htmlout")) ||
1990 (!strcmp(argv[i], "--htmlout")))
1991 htmlout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001992 else if ((!strcmp(argv[i], "-nowrap")) ||
1993 (!strcmp(argv[i], "--nowrap")))
1994 nowrap++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001995#ifdef LIBXML_HTML_ENABLED
1996 else if ((!strcmp(argv[i], "-html")) ||
1997 (!strcmp(argv[i], "--html"))) {
1998 html++;
1999 }
Daniel Veillard42fd4122003-11-04 08:47:48 +00002000 else if ((!strcmp(argv[i], "-xmlout")) ||
2001 (!strcmp(argv[i], "--xmlout"))) {
2002 xmlout++;
2003 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002004#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002005 else if ((!strcmp(argv[i], "-loaddtd")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002006 (!strcmp(argv[i], "--loaddtd"))) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002007 loaddtd++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002008 options |= XML_PARSE_DTDLOAD;
2009 } else if ((!strcmp(argv[i], "-dtdattr")) ||
Daniel Veillard48da9102001-08-07 01:10:10 +00002010 (!strcmp(argv[i], "--dtdattr"))) {
2011 loaddtd++;
2012 dtdattrs++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002013 options |= XML_PARSE_DTDATTR;
Daniel Veillard4432df22003-09-28 18:58:27 +00002014 }
2015#ifdef LIBXML_VALID_ENABLED
2016 else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002017 (!strcmp(argv[i], "--valid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002018 valid++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002019 options |= XML_PARSE_DTDVALID;
2020 } else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002021 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002022 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002023 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00002024 options |= XML_PARSE_DTDLOAD;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002025 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00002026 (!strcmp(argv[i], "--dtdvalid"))) {
2027 i++;
2028 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002029 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00002030 options |= XML_PARSE_DTDLOAD;
Daniel Veillard66f68e72003-08-18 16:39:51 +00002031 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
2032 (!strcmp(argv[i], "--dtdvalidfpi"))) {
2033 i++;
2034 dtdvalidfpi = argv[i];
2035 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00002036 options |= XML_PARSE_DTDLOAD;
Daniel Veillardcd429612000-10-11 15:57:05 +00002037 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002038#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard29e43992001-12-13 22:21:58 +00002039 else if ((!strcmp(argv[i], "-dropdtd")) ||
2040 (!strcmp(argv[i], "--dropdtd")))
2041 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002042 else if ((!strcmp(argv[i], "-insert")) ||
2043 (!strcmp(argv[i], "--insert")))
2044 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00002045 else if ((!strcmp(argv[i], "-timing")) ||
2046 (!strcmp(argv[i], "--timing")))
2047 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002048 else if ((!strcmp(argv[i], "-auto")) ||
2049 (!strcmp(argv[i], "--auto")))
2050 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002051 else if ((!strcmp(argv[i], "-repeat")) ||
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00002052 (!strcmp(argv[i], "--repeat"))) {
2053 if (repeat)
2054 repeat *= 10;
2055 else
2056 repeat = 100;
Daniel Veillard73b013f2003-09-30 12:36:01 +00002057 }
2058#ifdef LIBXML_PUSH_ENABLED
2059 else if ((!strcmp(argv[i], "-push")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002060 (!strcmp(argv[i], "--push")))
2061 push++;
Daniel Veillard73b013f2003-09-30 12:36:01 +00002062#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +00002063#ifdef HAVE_SYS_MMAN_H
2064 else if ((!strcmp(argv[i], "-memory")) ||
2065 (!strcmp(argv[i], "--memory")))
2066 memory++;
2067#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00002068 else if ((!strcmp(argv[i], "-testIO")) ||
2069 (!strcmp(argv[i], "--testIO")))
2070 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002071#ifdef LIBXML_XINCLUDE_ENABLED
2072 else if ((!strcmp(argv[i], "-xinclude")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002073 (!strcmp(argv[i], "--xinclude"))) {
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002074 xinclude++;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002075 options |= XML_PARSE_XINCLUDE;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002076 }
Daniel Veillardc14c3892004-08-16 12:34:50 +00002077 else if ((!strcmp(argv[i], "-noxincludenode")) ||
2078 (!strcmp(argv[i], "--noxincludenode"))) {
2079 xinclude++;
2080 options |= XML_PARSE_XINCLUDE;
2081 options |= XML_PARSE_NOXINCNODE;
2082 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002083#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002084#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002085#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002086 else if ((!strcmp(argv[i], "-compress")) ||
2087 (!strcmp(argv[i], "--compress"))) {
2088 compress++;
2089 xmlSetCompressMode(9);
2090 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002091#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002092#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002093 else if ((!strcmp(argv[i], "-nowarning")) ||
2094 (!strcmp(argv[i], "--nowarning"))) {
2095 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00002096 xmlPedanticParserDefault(0);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002097 options |= XML_PARSE_NOWARNING;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002098 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00002099 else if ((!strcmp(argv[i], "-pedantic")) ||
2100 (!strcmp(argv[i], "--pedantic"))) {
2101 xmlGetWarningsDefaultValue = 1;
2102 xmlPedanticParserDefault(1);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002103 options |= XML_PARSE_PEDANTIC;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00002104 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00002105#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00002106 else if ((!strcmp(argv[i], "-debugent")) ||
2107 (!strcmp(argv[i], "--debugent"))) {
2108 debugent++;
2109 xmlParserDebugEntities = 1;
2110 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00002111#endif
Daniel Veillard25048d82004-08-14 22:37:54 +00002112#ifdef LIBXML_C14N_ENABLED
2113 else if ((!strcmp(argv[i], "-c14n")) ||
2114 (!strcmp(argv[i], "--c14n"))) {
2115 canonical++;
2116 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
2117 }
Aleksey Sanin2650df12005-06-06 17:16:50 +00002118 else if ((!strcmp(argv[i], "-exc-c14n")) ||
2119 (!strcmp(argv[i], "--exc-c14n"))) {
2120 exc_canonical++;
2121 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
2122 }
Daniel Veillard25048d82004-08-14 22:37:54 +00002123#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00002124#ifdef LIBXML_CATALOG_ENABLED
2125 else if ((!strcmp(argv[i], "-catalogs")) ||
2126 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002127 catalogs++;
2128 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
2129 (!strcmp(argv[i], "--nocatalogs"))) {
2130 nocatalogs++;
Daniel Veillard81418e32001-05-22 15:08:55 +00002131 }
2132#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00002133 else if ((!strcmp(argv[i], "-encode")) ||
2134 (!strcmp(argv[i], "--encode"))) {
2135 i++;
2136 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00002137 /*
2138 * OK it's for testing purposes
2139 */
2140 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00002141 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002142 else if ((!strcmp(argv[i], "-noblanks")) ||
2143 (!strcmp(argv[i], "--noblanks"))) {
2144 noblanks++;
2145 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00002146 }
Daniel Veillard87076042004-05-03 22:54:49 +00002147 else if ((!strcmp(argv[i], "-maxmem")) ||
2148 (!strcmp(argv[i], "--maxmem"))) {
2149 i++;
2150 if (sscanf(argv[i], "%d", &maxmem) == 1) {
2151 xmlMemSetup(myFreeFunc, myMallocFunc, myReallocFunc,
2152 myStrdupFunc);
2153 } else {
2154 maxmem = 0;
2155 }
2156 }
Daniel Veillard90493a92001-08-14 14:12:47 +00002157 else if ((!strcmp(argv[i], "-format")) ||
2158 (!strcmp(argv[i], "--format"))) {
2159 noblanks++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002160#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00002161 format++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002162#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard90493a92001-08-14 14:12:47 +00002163 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00002164 }
Daniel Veillard81273902003-09-30 00:43:48 +00002165#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00002166 else if ((!strcmp(argv[i], "-stream")) ||
2167 (!strcmp(argv[i], "--stream"))) {
2168 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002169 }
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002170 else if ((!strcmp(argv[i], "-walker")) ||
2171 (!strcmp(argv[i], "--walker"))) {
2172 walker++;
2173 noout++;
2174 }
Daniel Veillard81273902003-09-30 00:43:48 +00002175#endif /* LIBXML_READER_ENABLED */
2176#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00002177 else if ((!strcmp(argv[i], "-sax1")) ||
2178 (!strcmp(argv[i], "--sax1"))) {
2179 sax1++;
2180 }
Daniel Veillard81273902003-09-30 00:43:48 +00002181#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002182 else if ((!strcmp(argv[i], "-chkregister")) ||
2183 (!strcmp(argv[i], "--chkregister"))) {
2184 chkregister++;
Daniel Veillard71531f32003-02-05 13:19:53 +00002185#ifdef LIBXML_SCHEMAS_ENABLED
2186 } else if ((!strcmp(argv[i], "-relaxng")) ||
2187 (!strcmp(argv[i], "--relaxng"))) {
2188 i++;
2189 relaxng = argv[i];
2190 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002191 options |= XML_PARSE_NOENT;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002192 } else if ((!strcmp(argv[i], "-schema")) ||
2193 (!strcmp(argv[i], "--schema"))) {
2194 i++;
2195 schema = argv[i];
2196 noent++;
Daniel Veillard71531f32003-02-05 13:19:53 +00002197#endif
Daniel Veillarde8b09e42003-05-13 22:14:13 +00002198 } else if ((!strcmp(argv[i], "-nonet")) ||
2199 (!strcmp(argv[i], "--nonet"))) {
Daniel Veillard61b93382003-11-03 14:28:31 +00002200 options |= XML_PARSE_NONET;
Daniel Veillard0bff36d2004-08-31 09:37:03 +00002201 } else if ((!strcmp(argv[i], "-load-trace")) ||
2202 (!strcmp(argv[i], "--load-trace"))) {
2203 load_trace++;
2204 } else if ((!strcmp(argv[i], "-path")) ||
2205 (!strcmp(argv[i], "--path"))) {
2206 i++;
2207 parsePath(BAD_CAST argv[i]);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002208#ifdef LIBXML_PATTERN_ENABLED
2209 } else if ((!strcmp(argv[i], "-pattern")) ||
2210 (!strcmp(argv[i], "--pattern"))) {
2211 i++;
2212 pattern = argv[i];
2213#endif
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002214 } else {
2215 fprintf(stderr, "Unknown option %s\n", argv[i]);
2216 usage(argv[0]);
2217 return(1);
2218 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002219 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002220
2221#ifdef LIBXML_CATALOG_ENABLED
2222 if (nocatalogs == 0) {
2223 if (catalogs) {
2224 const char *catal;
2225
2226 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00002227 if (catal != NULL) {
2228 xmlLoadCatalogs(catal);
2229 } else {
2230 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
2231 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002232 }
2233 }
2234#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002235
Daniel Veillard81273902003-09-30 00:43:48 +00002236#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00002237 if (sax1)
2238 xmlSAXDefaultVersion(1);
2239 else
2240 xmlSAXDefaultVersion(2);
Daniel Veillard81273902003-09-30 00:43:48 +00002241#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00002242
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002243 if (chkregister) {
2244 xmlRegisterNodeDefault(registerNode);
2245 xmlDeregisterNodeDefault(deregisterNode);
2246 }
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00002247
2248 indent = getenv("XMLLINT_INDENT");
2249 if(indent != NULL) {
2250 xmlTreeIndentString = indent;
2251 }
2252
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002253
Daniel Veillard0bff36d2004-08-31 09:37:03 +00002254 defaultEntityLoader = xmlGetExternalEntityLoader();
2255 xmlSetExternalEntityLoader(xmllintExternalEntityLoader);
2256
Daniel Veillardd9bad132001-07-23 19:39:43 +00002257 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00002258 if (loaddtd != 0)
2259 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
2260 if (dtdattrs)
2261 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002262 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillard4432df22003-09-28 18:58:27 +00002263#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002264 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard4432df22003-09-28 18:58:27 +00002265#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002266 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00002267 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002268 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00002269 xmlGenericError(xmlGenericErrorContext,
2270 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
2271 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002272 "<html><head><title>%s output</title></head>\n",
2273 argv[0]);
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00002274 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002275 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
2276 argv[0]);
2277 }
Daniel Veillard71531f32003-02-05 13:19:53 +00002278
2279#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard81273902003-09-30 00:43:48 +00002280 if ((relaxng != NULL)
2281#ifdef LIBXML_READER_ENABLED
2282 && (stream == 0)
2283#endif /* LIBXML_READER_ENABLED */
2284 ) {
Daniel Veillard71531f32003-02-05 13:19:53 +00002285 xmlRelaxNGParserCtxtPtr ctxt;
2286
Daniel Veillardce192eb2003-04-16 15:58:05 +00002287 /* forces loading the DTDs */
2288 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002289 options |= XML_PARSE_DTDLOAD;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002290 if (timing) {
2291 startTimer();
2292 }
Daniel Veillard71531f32003-02-05 13:19:53 +00002293 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
2294 xmlRelaxNGSetParserErrors(ctxt,
2295 (xmlRelaxNGValidityErrorFunc) fprintf,
2296 (xmlRelaxNGValidityWarningFunc) fprintf,
2297 stderr);
2298 relaxngschemas = xmlRelaxNGParse(ctxt);
Daniel Veillardce192eb2003-04-16 15:58:05 +00002299 if (relaxngschemas == NULL) {
2300 xmlGenericError(xmlGenericErrorContext,
2301 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +00002302 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +00002303 relaxng = NULL;
2304 }
Daniel Veillard71531f32003-02-05 13:19:53 +00002305 xmlRelaxNGFreeParserCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002306 if (timing) {
2307 endTimer("Compiling the schemas");
2308 }
Daniel Veillardebe25d42004-03-25 09:35:49 +00002309 } else if ((schema != NULL)
2310#ifdef LIBXML_READER_ENABLED
2311 && (stream == 0)
2312#endif
2313 ) {
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002314 xmlSchemaParserCtxtPtr ctxt;
2315
2316 if (timing) {
2317 startTimer();
2318 }
2319 ctxt = xmlSchemaNewParserCtxt(schema);
2320 xmlSchemaSetParserErrors(ctxt,
2321 (xmlSchemaValidityErrorFunc) fprintf,
2322 (xmlSchemaValidityWarningFunc) fprintf,
2323 stderr);
2324 wxschemas = xmlSchemaParse(ctxt);
2325 if (wxschemas == NULL) {
2326 xmlGenericError(xmlGenericErrorContext,
2327 "WXS schema %s failed to compile\n", schema);
William M. Brack8304d872004-06-08 13:29:32 +00002328 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002329 schema = NULL;
2330 }
2331 xmlSchemaFreeParserCtxt(ctxt);
2332 if (timing) {
2333 endTimer("Compiling the schemas");
2334 }
Daniel Veillard71531f32003-02-05 13:19:53 +00002335 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002336#endif /* LIBXML_SCHEMAS_ENABLED */
2337#ifdef LIBXML_PATTERN_ENABLED
Daniel Veillardd4301ab2005-02-03 22:24:10 +00002338 if ((pattern != NULL) && (walker == 0)) {
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00002339 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002340 if (patternc == NULL) {
2341 xmlGenericError(xmlGenericErrorContext,
2342 "Pattern %s failed to compile\n", pattern);
William M. Brack8304d872004-06-08 13:29:32 +00002343 progresult = XMLLINT_ERR_SCHEMAPAT;
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002344 pattern = NULL;
2345 }
2346 }
2347#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002348 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00002349 if ((!strcmp(argv[i], "-encode")) ||
2350 (!strcmp(argv[i], "--encode"))) {
2351 i++;
2352 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002353 } else if ((!strcmp(argv[i], "-o")) ||
2354 (!strcmp(argv[i], "-output")) ||
2355 (!strcmp(argv[i], "--output"))) {
2356 i++;
2357 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00002358 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002359#ifdef LIBXML_VALID_ENABLED
Daniel Veillardcd429612000-10-11 15:57:05 +00002360 if ((!strcmp(argv[i], "-dtdvalid")) ||
2361 (!strcmp(argv[i], "--dtdvalid"))) {
2362 i++;
2363 continue;
Daniel Veillard0bff36d2004-08-31 09:37:03 +00002364 }
2365 if ((!strcmp(argv[i], "-path")) ||
2366 (!strcmp(argv[i], "--path"))) {
2367 i++;
2368 continue;
Daniel Veillardcd429612000-10-11 15:57:05 +00002369 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00002370 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
2371 (!strcmp(argv[i], "--dtdvalidfpi"))) {
2372 i++;
2373 continue;
2374 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002375#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00002376 if ((!strcmp(argv[i], "-relaxng")) ||
2377 (!strcmp(argv[i], "--relaxng"))) {
2378 i++;
2379 continue;
2380 }
Daniel Veillard87076042004-05-03 22:54:49 +00002381 if ((!strcmp(argv[i], "-maxmem")) ||
2382 (!strcmp(argv[i], "--maxmem"))) {
2383 i++;
2384 continue;
2385 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002386 if ((!strcmp(argv[i], "-schema")) ||
2387 (!strcmp(argv[i], "--schema"))) {
2388 i++;
2389 continue;
2390 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002391#ifdef LIBXML_PATTERN_ENABLED
2392 if ((!strcmp(argv[i], "-pattern")) ||
2393 (!strcmp(argv[i], "--pattern"))) {
2394 i++;
2395 continue;
2396 }
2397#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00002398 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00002399 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002400 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002401 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002402 if (repeat) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002403 xmlParserCtxtPtr ctxt = NULL;
2404
2405 for (acount = 0;acount < repeat;acount++) {
Daniel Veillard81273902003-09-30 00:43:48 +00002406#ifdef LIBXML_READER_ENABLED
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002407 if (stream != 0) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00002408 streamFile(argv[i]);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002409 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00002410#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002411 if (ctxt == NULL)
2412 ctxt = xmlNewParserCtxt();
2413 parseAndPrintFile(argv[i], ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00002414#ifdef LIBXML_READER_ENABLED
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002415 }
Daniel Veillard81273902003-09-30 00:43:48 +00002416#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002417 }
2418 if (ctxt != NULL)
2419 xmlFreeParserCtxt(ctxt);
Daniel Veillard7704fb12003-01-03 16:19:51 +00002420 } else {
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002421 nbregister = 0;
2422
Daniel Veillard81273902003-09-30 00:43:48 +00002423#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00002424 if (stream != 0)
2425 streamFile(argv[i]);
2426 else
Daniel Veillard81273902003-09-30 00:43:48 +00002427#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002428 parseAndPrintFile(argv[i], NULL);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002429
2430 if ((chkregister) && (nbregister != 0)) {
2431 fprintf(stderr, "Registration count off: %d\n", nbregister);
William M. Brack8304d872004-06-08 13:29:32 +00002432 progresult = XMLLINT_ERR_RDREGIS;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002433 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00002434 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002435 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00002436 if ((timing) && (repeat)) {
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00002437 endTimer("%d iterations", repeat);
Daniel Veillarda7866932001-12-04 13:14:44 +00002438 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002439 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002440 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002441 if (generate)
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002442 parseAndPrintFile(NULL, NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002443 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00002444 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002445 }
Daniel Veillard845cce42002-01-09 11:51:37 +00002446 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002447 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002448 }
Daniel Veillard71531f32003-02-05 13:19:53 +00002449#ifdef LIBXML_SCHEMAS_ENABLED
2450 if (relaxngschemas != NULL)
2451 xmlRelaxNGFree(relaxngschemas);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002452 if (wxschemas != NULL)
2453 xmlSchemaFree(wxschemas);
Daniel Veillard71531f32003-02-05 13:19:53 +00002454 xmlRelaxNGCleanupTypes();
2455#endif
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002456#ifdef LIBXML_PATTERN_ENABLED
2457 if (patternc != NULL)
2458 xmlFreePattern(patternc);
2459#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002460 xmlCleanupParser();
2461 xmlMemoryDump();
2462
Daniel Veillardf7cd4812001-02-23 18:44:52 +00002463 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002464}
Daniel Veillard88a172f2000-08-04 18:23:10 +00002465