blob: dbc5d35d1c0170d7aacc383fa56eee0d209a2a3e [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 Veillardf0af8ec2005-07-08 17:27:33 +0000193static int sax = 0;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000194
Daniel Veillard87076042004-05-03 22:54:49 +0000195/************************************************************************
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000196 * *
197 * Entity loading control and customization. *
198 * *
199 ************************************************************************/
200#define MAX_PATHS 64
201static xmlChar *paths[MAX_PATHS + 1];
202static int nbpaths = 0;
203static int load_trace = 0;
204
205static
206void parsePath(const xmlChar *path) {
207 const xmlChar *cur;
208
209 if (path == NULL)
210 return;
211 while (*path != 0) {
212 if (nbpaths >= MAX_PATHS) {
213 fprintf(stderr, "MAX_PATHS reached: too many paths\n");
214 return;
215 }
216 cur = path;
217 while ((*cur == ' ') || (*cur == ':'))
218 cur++;
219 path = cur;
220 while ((*cur != 0) && (*cur != ' ') && (*cur != ':'))
221 cur++;
222 if (cur != path) {
223 paths[nbpaths] = xmlStrndup(path, cur - path);
224 if (paths[nbpaths] != NULL)
225 nbpaths++;
226 path = cur;
227 }
228 }
229}
230
231xmlExternalEntityLoader defaultEntityLoader = NULL;
232
233static xmlParserInputPtr
234xmllintExternalEntityLoader(const char *URL, const char *ID,
235 xmlParserCtxtPtr ctxt) {
236 xmlParserInputPtr ret;
237 warningSAXFunc warning = NULL;
238
239 int i;
240 const char *lastsegment = URL;
241 const char *iter = URL;
242
243 if (nbpaths > 0) {
244 while (*iter != 0) {
245 if (*iter == '/')
246 lastsegment = iter + 1;
247 iter++;
248 }
249 }
250
251 if ((ctxt != NULL) && (ctxt->sax != NULL)) {
252 warning = ctxt->sax->warning;
253 ctxt->sax->warning = NULL;
254 }
255
256 if (defaultEntityLoader != NULL) {
257 ret = defaultEntityLoader(URL, ID, ctxt);
258 if (ret != NULL) {
259 if (warning != NULL)
260 ctxt->sax->warning = warning;
261 if (load_trace) {
262 fprintf \
263 (stderr,
264 "Loaded URL=\"%s\" ID=\"%s\"\n",
265 URL ? URL : "(null)",
266 ID ? ID : "(null)");
267 }
268 return(ret);
269 }
270 }
271 for (i = 0;i < nbpaths;i++) {
272 xmlChar *newURL;
273
274 newURL = xmlStrdup((const xmlChar *) paths[i]);
275 newURL = xmlStrcat(newURL, (const xmlChar *) "/");
276 newURL = xmlStrcat(newURL, (const xmlChar *) lastsegment);
277 if (newURL != NULL) {
278 ret = defaultEntityLoader((const char *)newURL, ID, ctxt);
279 if (ret != NULL) {
280 if (warning != NULL)
281 ctxt->sax->warning = warning;
282 if (load_trace) {
283 fprintf \
284 (stderr,
285 "Loaded URL=\"%s\" ID=\"%s\"\n",
286 newURL,
287 ID ? ID : "(null)");
288 }
289 xmlFree(newURL);
290 return(ret);
291 }
292 xmlFree(newURL);
293 }
294 }
295 if (warning != NULL) {
296 ctxt->sax->warning = warning;
297 if (URL != NULL)
298 warning(ctxt, "failed to load external entity \"%s\"\n", URL);
299 else if (ID != NULL)
300 warning(ctxt, "failed to load external entity \"%s\"\n", ID);
301 }
302 return(NULL);
303}
304/************************************************************************
Daniel Veillard87076042004-05-03 22:54:49 +0000305 * *
306 * Memory allocation consumption debugging *
307 * *
308 ************************************************************************/
309
Daniel Veillard3af3b592004-05-05 19:22:30 +0000310static void
311OOM(void)
312{
Daniel Veillard87076042004-05-03 22:54:49 +0000313 fprintf(stderr, "Ran out of memory needs > %d bytes\n", maxmem);
William M. Brack8304d872004-06-08 13:29:32 +0000314 progresult = XMLLINT_ERR_MEM;
Daniel Veillard87076042004-05-03 22:54:49 +0000315}
316
Daniel Veillard3af3b592004-05-05 19:22:30 +0000317static void
318myFreeFunc(void *mem)
319{
Daniel Veillard87076042004-05-03 22:54:49 +0000320 xmlMemFree(mem);
321}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000322static void *
323myMallocFunc(size_t size)
324{
Daniel Veillard87076042004-05-03 22:54:49 +0000325 void *ret;
326
327 ret = xmlMemMalloc(size);
328 if (ret != NULL) {
329 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000330 OOM();
331 xmlMemFree(ret);
332 return (NULL);
333 }
Daniel Veillard87076042004-05-03 22:54:49 +0000334 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000335 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000336}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000337static void *
338myReallocFunc(void *mem, size_t size)
339{
Daniel Veillard87076042004-05-03 22:54:49 +0000340 void *ret;
341
342 ret = xmlMemRealloc(mem, size);
343 if (ret != NULL) {
344 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000345 OOM();
346 xmlMemFree(ret);
347 return (NULL);
348 }
Daniel Veillard87076042004-05-03 22:54:49 +0000349 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000350 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000351}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000352static char *
353myStrdupFunc(const char *str)
354{
Daniel Veillard87076042004-05-03 22:54:49 +0000355 char *ret;
356
357 ret = xmlMemoryStrdup(str);
358 if (ret != NULL) {
359 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000360 OOM();
361 xmlFree(ret);
362 return (NULL);
363 }
Daniel Veillard87076042004-05-03 22:54:49 +0000364 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000365 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000366}
Daniel Veillard87076042004-05-03 22:54:49 +0000367/************************************************************************
368 * *
369 * Internal timing routines to remove the necessity to have *
370 * unix-specific function calls. *
371 * *
372 ************************************************************************/
Daniel Veillard01db67c2001-12-18 07:09:59 +0000373
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000374#ifndef HAVE_GETTIMEOFDAY
375#ifdef HAVE_SYS_TIMEB_H
376#ifdef HAVE_SYS_TIME_H
377#ifdef HAVE_FTIME
378
Daniel Veillard01c13b52002-12-10 15:19:08 +0000379static int
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000380my_gettimeofday(struct timeval *tvp, void *tzp)
381{
382 struct timeb timebuffer;
383
384 ftime(&timebuffer);
385 if (tvp) {
386 tvp->tv_sec = timebuffer.time;
387 tvp->tv_usec = timebuffer.millitm * 1000L;
388 }
389 return (0);
390}
391#define HAVE_GETTIMEOFDAY 1
392#define gettimeofday my_gettimeofday
393
394#endif /* HAVE_FTIME */
395#endif /* HAVE_SYS_TIME_H */
396#endif /* HAVE_SYS_TIMEB_H */
397#endif /* !HAVE_GETTIMEOFDAY */
398
Daniel Veillard01db67c2001-12-18 07:09:59 +0000399#if defined(HAVE_GETTIMEOFDAY)
400static struct timeval begin, end;
401
402/*
403 * startTimer: call where you want to start timing
404 */
405static void
406startTimer(void)
407{
408 gettimeofday(&begin, NULL);
409}
410
411/*
412 * endTimer: call where you want to stop timing and to print out a
413 * message about the timing performed; format is a printf
414 * type argument
415 */
416static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000417endTimer(const char *fmt, ...)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000418{
419 long msec;
420 va_list ap;
421
422 gettimeofday(&end, NULL);
423 msec = end.tv_sec - begin.tv_sec;
424 msec *= 1000;
425 msec += (end.tv_usec - begin.tv_usec) / 1000;
426
427#ifndef HAVE_STDARG_H
428#error "endTimer required stdarg functions"
429#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000430 va_start(ap, fmt);
431 vfprintf(stderr, fmt, ap);
Daniel Veillard01db67c2001-12-18 07:09:59 +0000432 va_end(ap);
433
434 fprintf(stderr, " took %ld ms\n", msec);
435}
436#elif defined(HAVE_TIME_H)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000437/*
438 * No gettimeofday function, so we have to make do with calling clock.
439 * This is obviously less accurate, but there's little we can do about
440 * that.
441 */
Daniel Veillard90bc3712002-03-07 15:12:58 +0000442#ifndef CLOCKS_PER_SEC
443#define CLOCKS_PER_SEC 100
444#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +0000445
446static clock_t begin, end;
447static void
448startTimer(void)
449{
450 begin = clock();
451}
452static void
453endTimer(const char *fmt, ...)
454{
455 long msec;
456 va_list ap;
457
458 end = clock();
459 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
460
461#ifndef HAVE_STDARG_H
462#error "endTimer required stdarg functions"
463#endif
464 va_start(ap, fmt);
465 vfprintf(stderr, fmt, ap);
466 va_end(ap);
467 fprintf(stderr, " took %ld ms\n", msec);
468}
469#else
470
471/*
472 * We don't have a gettimeofday or time.h, so we just don't do timing
473 */
474static void
475startTimer(void)
476{
477 /*
478 * Do nothing
479 */
480}
481static void
482endTimer(char *format, ...)
483{
484 /*
485 * We cannot do anything because we don't have a timing function
486 */
487#ifdef HAVE_STDARG_H
488 va_start(ap, format);
489 vfprintf(stderr, format, ap);
490 va_end(ap);
491 fprintf(stderr, " was not timed\n", msec);
492#else
493 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
494 * this ?!
495 */
496#endif
497}
498#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000499/************************************************************************
500 * *
501 * HTML ouput *
502 * *
503 ************************************************************************/
504char buffer[50000];
505
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000506static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000507xmlHTMLEncodeSend(void) {
508 char *result;
509
510 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
511 if (result) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000512 xmlGenericError(xmlGenericErrorContext, "%s", result);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000513 xmlFree(result);
514 }
515 buffer[0] = 0;
516}
517
518/**
519 * xmlHTMLPrintFileInfo:
520 * @input: an xmlParserInputPtr input
521 *
522 * Displays the associated file and line informations for the current input
523 */
524
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000525static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000526xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000527 int len;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000528 xmlGenericError(xmlGenericErrorContext, "<p>");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000529
530 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000531 if (input != NULL) {
532 if (input->filename) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000533 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000534 input->line);
535 } else {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000536 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000537 }
538 }
539 xmlHTMLEncodeSend();
540}
541
542/**
543 * xmlHTMLPrintFileContext:
544 * @input: an xmlParserInputPtr input
545 *
546 * Displays current context within the input content for error tracking
547 */
548
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000549static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000550xmlHTMLPrintFileContext(xmlParserInputPtr input) {
551 const xmlChar *cur, *base;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000552 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000553 int n;
554
555 if (input == NULL) return;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000556 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000557 cur = input->cur;
558 base = input->base;
559 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
560 cur--;
561 }
562 n = 0;
563 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
564 cur--;
565 if ((*cur == '\n') || (*cur == '\r')) cur++;
566 base = cur;
567 n = 0;
568 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000569 len = strlen(buffer);
570 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
571 (unsigned char) *cur++);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000572 n++;
573 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000574 len = strlen(buffer);
575 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000576 cur = input->cur;
577 while ((*cur == '\n') || (*cur == '\r'))
578 cur--;
579 n = 0;
580 while ((cur != base) && (n++ < 80)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000581 len = strlen(buffer);
582 snprintf(&buffer[len], sizeof(buffer) - len, " ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000583 base++;
584 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000585 len = strlen(buffer);
586 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000587 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000588 xmlGenericError(xmlGenericErrorContext, "</pre>");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000589}
590
591/**
592 * xmlHTMLError:
593 * @ctx: an XML parser context
594 * @msg: the message to display/transmit
595 * @...: extra parameters for the message display
596 *
597 * Display and format an error messages, gives file, line, position and
598 * extra parameters.
599 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000600static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000601xmlHTMLError(void *ctx, const char *msg, ...)
602{
603 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
604 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000605 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000606 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000607
608 buffer[0] = 0;
609 input = ctxt->input;
610 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000611 input = ctxt->inputTab[ctxt->inputNr - 2];
612 }
613
614 xmlHTMLPrintFileInfo(input);
615
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000616 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000617 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000618 len = strlen(buffer);
619 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000620 va_end(args);
621 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000622 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000623
624 xmlHTMLPrintFileContext(input);
625 xmlHTMLEncodeSend();
626}
627
628/**
629 * xmlHTMLWarning:
630 * @ctx: an XML parser context
631 * @msg: the message to display/transmit
632 * @...: extra parameters for the message display
633 *
634 * Display and format a warning messages, gives file, line, position and
635 * extra parameters.
636 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000637static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000638xmlHTMLWarning(void *ctx, const char *msg, ...)
639{
640 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
641 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000642 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000643 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000644
645 buffer[0] = 0;
646 input = ctxt->input;
647 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000648 input = ctxt->inputTab[ctxt->inputNr - 2];
649 }
650
651
652 xmlHTMLPrintFileInfo(input);
653
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000654 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000655 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000656 len = strlen(buffer);
657 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000658 va_end(args);
659 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000660 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000661
662 xmlHTMLPrintFileContext(input);
663 xmlHTMLEncodeSend();
664}
665
666/**
667 * xmlHTMLValidityError:
668 * @ctx: an XML parser context
669 * @msg: the message to display/transmit
670 * @...: extra parameters for the message display
671 *
672 * Display and format an validity error messages, gives file,
673 * line, position and extra parameters.
674 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000675static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000676xmlHTMLValidityError(void *ctx, const char *msg, ...)
677{
678 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
679 xmlParserInputPtr input;
680 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000681 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000682
683 buffer[0] = 0;
684 input = ctxt->input;
685 if ((input->filename == NULL) && (ctxt->inputNr > 1))
686 input = ctxt->inputTab[ctxt->inputNr - 2];
687
688 xmlHTMLPrintFileInfo(input);
689
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000690 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000691 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000692 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000693 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000694 va_end(args);
695 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000696 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000697
698 xmlHTMLPrintFileContext(input);
699 xmlHTMLEncodeSend();
Daniel Veillard9fcb4912005-03-16 12:57:31 +0000700 progresult = XMLLINT_ERR_VALID;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000701}
702
703/**
704 * xmlHTMLValidityWarning:
705 * @ctx: an XML parser context
706 * @msg: the message to display/transmit
707 * @...: extra parameters for the message display
708 *
709 * Display and format a validity warning messages, gives file, line,
710 * position and extra parameters.
711 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000712static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000713xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
714{
715 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
716 xmlParserInputPtr input;
717 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000718 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000719
720 buffer[0] = 0;
721 input = ctxt->input;
722 if ((input->filename == NULL) && (ctxt->inputNr > 1))
723 input = ctxt->inputTab[ctxt->inputNr - 2];
724
725 xmlHTMLPrintFileInfo(input);
726
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000727 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000728 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000729 len = strlen(buffer);
730 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000731 va_end(args);
732 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000733 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000734
735 xmlHTMLPrintFileContext(input);
736 xmlHTMLEncodeSend();
737}
738
739/************************************************************************
740 * *
741 * Shell Interface *
742 * *
743 ************************************************************************/
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000744#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardd0cf7f62004-11-09 16:17:02 +0000745#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000746/**
747 * xmlShellReadline:
748 * @prompt: the prompt value
749 *
750 * Read a string
751 *
752 * Returns a pointer to it or NULL on EOF the caller is expected to
753 * free the returned string.
754 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000755static char *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000756xmlShellReadline(char *prompt) {
757#ifdef HAVE_LIBREADLINE
758 char *line_read;
759
760 /* Get a line from the user. */
761 line_read = readline (prompt);
762
763 /* If the line has any text in it, save it on the history. */
764 if (line_read && *line_read)
765 add_history (line_read);
766
767 return (line_read);
768#else
769 char line_read[501];
Daniel Veillard29e43992001-12-13 22:21:58 +0000770 char *ret;
771 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000772
773 if (prompt != NULL)
774 fprintf(stdout, "%s", prompt);
775 if (!fgets(line_read, 500, stdin))
776 return(NULL);
777 line_read[500] = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000778 len = strlen(line_read);
779 ret = (char *) malloc(len + 1);
780 if (ret != NULL) {
781 memcpy (ret, line_read, len + 1);
782 }
783 return(ret);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000784#endif
785}
Daniel Veillardd0cf7f62004-11-09 16:17:02 +0000786#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000787#endif /* LIBXML_DEBUG_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000788
789/************************************************************************
790 * *
Daniel Veillard5e873c42000-04-12 13:27:38 +0000791 * I/O Interfaces *
792 * *
793 ************************************************************************/
794
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000795static int myRead(FILE *f, char * buf, int len) {
796 return(fread(buf, 1, len, f));
Daniel Veillard5e873c42000-04-12 13:27:38 +0000797}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000798static void myClose(FILE *f) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000799 if (f != stdin) {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000800 fclose(f);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000801 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000802}
803
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000804/************************************************************************
805 * *
806 * SAX based tests *
807 * *
808 ************************************************************************/
809
810/*
811 * empty SAX block
812 */
813xmlSAXHandler emptySAXHandlerStruct = {
814 NULL, /* internalSubset */
815 NULL, /* isStandalone */
816 NULL, /* hasInternalSubset */
817 NULL, /* hasExternalSubset */
818 NULL, /* resolveEntity */
819 NULL, /* getEntity */
820 NULL, /* entityDecl */
821 NULL, /* notationDecl */
822 NULL, /* attributeDecl */
823 NULL, /* elementDecl */
824 NULL, /* unparsedEntityDecl */
825 NULL, /* setDocumentLocator */
826 NULL, /* startDocument */
827 NULL, /* endDocument */
828 NULL, /* startElement */
829 NULL, /* endElement */
830 NULL, /* reference */
831 NULL, /* characters */
832 NULL, /* ignorableWhitespace */
833 NULL, /* processingInstruction */
834 NULL, /* comment */
835 NULL, /* xmlParserWarning */
836 NULL, /* xmlParserError */
837 NULL, /* xmlParserError */
838 NULL, /* getParameterEntity */
839 NULL, /* cdataBlock; */
840 NULL, /* externalSubset; */
841 1,
842 NULL,
843 NULL, /* startElementNs */
844 NULL, /* endElementNs */
845 NULL /* xmlStructuredErrorFunc */
846};
847
848xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
849extern xmlSAXHandlerPtr debugSAXHandler;
850static int callbacks;
851
852/**
853 * isStandaloneDebug:
854 * @ctxt: An XML parser context
855 *
856 * Is this document tagged standalone ?
857 *
858 * Returns 1 if true
859 */
860static int
861isStandaloneDebug(void *ctx ATTRIBUTE_UNUSED)
862{
863 callbacks++;
864 if (noout)
865 return(0);
866 fprintf(stdout, "SAX.isStandalone()\n");
867 return(0);
868}
869
870/**
871 * hasInternalSubsetDebug:
872 * @ctxt: An XML parser context
873 *
874 * Does this document has an internal subset
875 *
876 * Returns 1 if true
877 */
878static int
879hasInternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
880{
881 callbacks++;
882 if (noout)
883 return(0);
884 fprintf(stdout, "SAX.hasInternalSubset()\n");
885 return(0);
886}
887
888/**
889 * hasExternalSubsetDebug:
890 * @ctxt: An XML parser context
891 *
892 * Does this document has an external subset
893 *
894 * Returns 1 if true
895 */
896static int
897hasExternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
898{
899 callbacks++;
900 if (noout)
901 return(0);
902 fprintf(stdout, "SAX.hasExternalSubset()\n");
903 return(0);
904}
905
906/**
907 * internalSubsetDebug:
908 * @ctxt: An XML parser context
909 *
910 * Does this document has an internal subset
911 */
912static void
913internalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
914 const xmlChar *ExternalID, const xmlChar *SystemID)
915{
916 callbacks++;
917 if (noout)
918 return;
919 fprintf(stdout, "SAX.internalSubset(%s,", name);
920 if (ExternalID == NULL)
921 fprintf(stdout, " ,");
922 else
923 fprintf(stdout, " %s,", ExternalID);
924 if (SystemID == NULL)
925 fprintf(stdout, " )\n");
926 else
927 fprintf(stdout, " %s)\n", SystemID);
928}
929
930/**
931 * externalSubsetDebug:
932 * @ctxt: An XML parser context
933 *
934 * Does this document has an external subset
935 */
936static void
937externalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
938 const xmlChar *ExternalID, const xmlChar *SystemID)
939{
940 callbacks++;
941 if (noout)
942 return;
943 fprintf(stdout, "SAX.externalSubset(%s,", name);
944 if (ExternalID == NULL)
945 fprintf(stdout, " ,");
946 else
947 fprintf(stdout, " %s,", ExternalID);
948 if (SystemID == NULL)
949 fprintf(stdout, " )\n");
950 else
951 fprintf(stdout, " %s)\n", SystemID);
952}
953
954/**
955 * resolveEntityDebug:
956 * @ctxt: An XML parser context
957 * @publicId: The public ID of the entity
958 * @systemId: The system ID of the entity
959 *
960 * Special entity resolver, better left to the parser, it has
961 * more context than the application layer.
962 * The default behaviour is to NOT resolve the entities, in that case
963 * the ENTITY_REF nodes are built in the structure (and the parameter
964 * values).
965 *
966 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
967 */
968static xmlParserInputPtr
969resolveEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *publicId, const xmlChar *systemId)
970{
971 callbacks++;
972 if (noout)
973 return(NULL);
974 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
975
976
977 fprintf(stdout, "SAX.resolveEntity(");
978 if (publicId != NULL)
979 fprintf(stdout, "%s", (char *)publicId);
980 else
981 fprintf(stdout, " ");
982 if (systemId != NULL)
983 fprintf(stdout, ", %s)\n", (char *)systemId);
984 else
985 fprintf(stdout, ", )\n");
986 return(NULL);
987}
988
989/**
990 * getEntityDebug:
991 * @ctxt: An XML parser context
992 * @name: The entity name
993 *
994 * Get an entity by name
995 *
996 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
997 */
998static xmlEntityPtr
999getEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1000{
1001 callbacks++;
1002 if (noout)
1003 return(NULL);
1004 fprintf(stdout, "SAX.getEntity(%s)\n", name);
1005 return(NULL);
1006}
1007
1008/**
1009 * getParameterEntityDebug:
1010 * @ctxt: An XML parser context
1011 * @name: The entity name
1012 *
1013 * Get a parameter entity by name
1014 *
1015 * Returns the xmlParserInputPtr
1016 */
1017static xmlEntityPtr
1018getParameterEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1019{
1020 callbacks++;
1021 if (noout)
1022 return(NULL);
1023 fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
1024 return(NULL);
1025}
1026
1027
1028/**
1029 * entityDeclDebug:
1030 * @ctxt: An XML parser context
1031 * @name: the entity name
1032 * @type: the entity type
1033 * @publicId: The public ID of the entity
1034 * @systemId: The system ID of the entity
1035 * @content: the entity value (without processing).
1036 *
1037 * An entity definition has been parsed
1038 */
1039static void
1040entityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
1041 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
1042{
1043const xmlChar *nullstr = BAD_CAST "(null)";
1044 /* not all libraries handle printing null pointers nicely */
1045 if (publicId == NULL)
1046 publicId = nullstr;
1047 if (systemId == NULL)
1048 systemId = nullstr;
1049 if (content == NULL)
1050 content = (xmlChar *)nullstr;
1051 callbacks++;
1052 if (noout)
1053 return;
1054 fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
1055 name, type, publicId, systemId, content);
1056}
1057
1058/**
1059 * attributeDeclDebug:
1060 * @ctxt: An XML parser context
1061 * @name: the attribute name
1062 * @type: the attribute type
1063 *
1064 * An attribute definition has been parsed
1065 */
1066static void
1067attributeDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar * elem,
1068 const xmlChar * name, int type, int def,
1069 const xmlChar * defaultValue, xmlEnumerationPtr tree)
1070{
1071 callbacks++;
1072 if (noout)
1073 return;
1074 if (defaultValue == NULL)
1075 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, NULL, ...)\n",
1076 elem, name, type, def);
1077 else
1078 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
1079 elem, name, type, def, defaultValue);
1080 xmlFreeEnumeration(tree);
1081}
1082
1083/**
1084 * elementDeclDebug:
1085 * @ctxt: An XML parser context
1086 * @name: the element name
1087 * @type: the element type
1088 * @content: the element value (without processing).
1089 *
1090 * An element definition has been parsed
1091 */
1092static void
1093elementDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
1094 xmlElementContentPtr content ATTRIBUTE_UNUSED)
1095{
1096 callbacks++;
1097 if (noout)
1098 return;
1099 fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
1100 name, type);
1101}
1102
1103/**
1104 * notationDeclDebug:
1105 * @ctxt: An XML parser context
1106 * @name: The name of the notation
1107 * @publicId: The public ID of the entity
1108 * @systemId: The system ID of the entity
1109 *
1110 * What to do when a notation declaration has been parsed.
1111 */
1112static void
1113notationDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
1114 const xmlChar *publicId, const xmlChar *systemId)
1115{
1116 callbacks++;
1117 if (noout)
1118 return;
1119 fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
1120 (char *) name, (char *) publicId, (char *) systemId);
1121}
1122
1123/**
1124 * unparsedEntityDeclDebug:
1125 * @ctxt: An XML parser context
1126 * @name: The name of the entity
1127 * @publicId: The public ID of the entity
1128 * @systemId: The system ID of the entity
1129 * @notationName: the name of the notation
1130 *
1131 * What to do when an unparsed entity declaration is parsed
1132 */
1133static void
1134unparsedEntityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
1135 const xmlChar *publicId, const xmlChar *systemId,
1136 const xmlChar *notationName)
1137{
1138const xmlChar *nullstr = BAD_CAST "(null)";
1139
1140 if (publicId == NULL)
1141 publicId = nullstr;
1142 if (systemId == NULL)
1143 systemId = nullstr;
1144 if (notationName == NULL)
1145 notationName = nullstr;
1146 callbacks++;
1147 if (noout)
1148 return;
1149 fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
1150 (char *) name, (char *) publicId, (char *) systemId,
1151 (char *) notationName);
1152}
1153
1154/**
1155 * setDocumentLocatorDebug:
1156 * @ctxt: An XML parser context
1157 * @loc: A SAX Locator
1158 *
1159 * Receive the document locator at startup, actually xmlDefaultSAXLocator
1160 * Everything is available on the context, so this is useless in our case.
1161 */
1162static void
1163setDocumentLocatorDebug(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
1164{
1165 callbacks++;
1166 if (noout)
1167 return;
1168 fprintf(stdout, "SAX.setDocumentLocator()\n");
1169}
1170
1171/**
1172 * startDocumentDebug:
1173 * @ctxt: An XML parser context
1174 *
1175 * called when the document start being processed.
1176 */
1177static void
1178startDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
1179{
1180 callbacks++;
1181 if (noout)
1182 return;
1183 fprintf(stdout, "SAX.startDocument()\n");
1184}
1185
1186/**
1187 * endDocumentDebug:
1188 * @ctxt: An XML parser context
1189 *
1190 * called when the document end has been detected.
1191 */
1192static void
1193endDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
1194{
1195 callbacks++;
1196 if (noout)
1197 return;
1198 fprintf(stdout, "SAX.endDocument()\n");
1199}
1200
1201/**
1202 * startElementDebug:
1203 * @ctxt: An XML parser context
1204 * @name: The element name
1205 *
1206 * called when an opening tag has been processed.
1207 */
1208static void
1209startElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, const xmlChar **atts)
1210{
1211 int i;
1212
1213 callbacks++;
1214 if (noout)
1215 return;
1216 fprintf(stdout, "SAX.startElement(%s", (char *) name);
1217 if (atts != NULL) {
1218 for (i = 0;(atts[i] != NULL);i++) {
1219 fprintf(stdout, ", %s='", atts[i++]);
1220 if (atts[i] != NULL)
1221 fprintf(stdout, "%s'", atts[i]);
1222 }
1223 }
1224 fprintf(stdout, ")\n");
1225}
1226
1227/**
1228 * endElementDebug:
1229 * @ctxt: An XML parser context
1230 * @name: The element name
1231 *
1232 * called when the end of an element has been detected.
1233 */
1234static void
1235endElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1236{
1237 callbacks++;
1238 if (noout)
1239 return;
1240 fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
1241}
1242
1243/**
1244 * charactersDebug:
1245 * @ctxt: An XML parser context
1246 * @ch: a xmlChar string
1247 * @len: the number of xmlChar
1248 *
1249 * receiving some chars from the parser.
1250 * Question: how much at a time ???
1251 */
1252static void
1253charactersDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
1254{
1255 char out[40];
1256 int i;
1257
1258 callbacks++;
1259 if (noout)
1260 return;
1261 for (i = 0;(i<len) && (i < 30);i++)
1262 out[i] = ch[i];
1263 out[i] = 0;
1264
1265 fprintf(stdout, "SAX.characters(%s, %d)\n", out, len);
1266}
1267
1268/**
1269 * referenceDebug:
1270 * @ctxt: An XML parser context
1271 * @name: The entity name
1272 *
1273 * called when an entity reference is detected.
1274 */
1275static void
1276referenceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1277{
1278 callbacks++;
1279 if (noout)
1280 return;
1281 fprintf(stdout, "SAX.reference(%s)\n", name);
1282}
1283
1284/**
1285 * ignorableWhitespaceDebug:
1286 * @ctxt: An XML parser context
1287 * @ch: a xmlChar string
1288 * @start: the first char in the string
1289 * @len: the number of xmlChar
1290 *
1291 * receiving some ignorable whitespaces from the parser.
1292 * Question: how much at a time ???
1293 */
1294static void
1295ignorableWhitespaceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
1296{
1297 char out[40];
1298 int i;
1299
1300 callbacks++;
1301 if (noout)
1302 return;
1303 for (i = 0;(i<len) && (i < 30);i++)
1304 out[i] = ch[i];
1305 out[i] = 0;
1306 fprintf(stdout, "SAX.ignorableWhitespace(%s, %d)\n", out, len);
1307}
1308
1309/**
1310 * processingInstructionDebug:
1311 * @ctxt: An XML parser context
1312 * @target: the target name
1313 * @data: the PI data's
1314 * @len: the number of xmlChar
1315 *
1316 * A processing instruction has been parsed.
1317 */
1318static void
1319processingInstructionDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *target,
1320 const xmlChar *data)
1321{
1322 callbacks++;
1323 if (noout)
1324 return;
1325 if (data != NULL)
1326 fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
1327 (char *) target, (char *) data);
1328 else
1329 fprintf(stdout, "SAX.processingInstruction(%s, NULL)\n",
1330 (char *) target);
1331}
1332
1333/**
1334 * cdataBlockDebug:
1335 * @ctx: the user data (XML parser context)
1336 * @value: The pcdata content
1337 * @len: the block length
1338 *
1339 * called when a pcdata block has been parsed
1340 */
1341static void
1342cdataBlockDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value, int len)
1343{
1344 callbacks++;
1345 if (noout)
1346 return;
1347 fprintf(stdout, "SAX.pcdata(%.20s, %d)\n",
1348 (char *) value, len);
1349}
1350
1351/**
1352 * commentDebug:
1353 * @ctxt: An XML parser context
1354 * @value: the comment content
1355 *
1356 * A comment has been parsed.
1357 */
1358static void
1359commentDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value)
1360{
1361 callbacks++;
1362 if (noout)
1363 return;
1364 fprintf(stdout, "SAX.comment(%s)\n", value);
1365}
1366
1367/**
1368 * warningDebug:
1369 * @ctxt: An XML parser context
1370 * @msg: the message to display/transmit
1371 * @...: extra parameters for the message display
1372 *
1373 * Display and format a warning messages, gives file, line, position and
1374 * extra parameters.
1375 */
1376static void
1377warningDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1378{
1379 va_list args;
1380
1381 callbacks++;
1382 if (noout)
1383 return;
1384 va_start(args, msg);
1385 fprintf(stdout, "SAX.warning: ");
1386 vfprintf(stdout, msg, args);
1387 va_end(args);
1388}
1389
1390/**
1391 * errorDebug:
1392 * @ctxt: An XML parser context
1393 * @msg: the message to display/transmit
1394 * @...: extra parameters for the message display
1395 *
1396 * Display and format a error messages, gives file, line, position and
1397 * extra parameters.
1398 */
1399static void
1400errorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1401{
1402 va_list args;
1403
1404 callbacks++;
1405 if (noout)
1406 return;
1407 va_start(args, msg);
1408 fprintf(stdout, "SAX.error: ");
1409 vfprintf(stdout, msg, args);
1410 va_end(args);
1411}
1412
1413/**
1414 * fatalErrorDebug:
1415 * @ctxt: An XML parser context
1416 * @msg: the message to display/transmit
1417 * @...: extra parameters for the message display
1418 *
1419 * Display and format a fatalError messages, gives file, line, position and
1420 * extra parameters.
1421 */
1422static void
1423fatalErrorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1424{
1425 va_list args;
1426
1427 callbacks++;
1428 if (noout)
1429 return;
1430 va_start(args, msg);
1431 fprintf(stdout, "SAX.fatalError: ");
1432 vfprintf(stdout, msg, args);
1433 va_end(args);
1434}
1435
1436xmlSAXHandler debugSAXHandlerStruct = {
1437 internalSubsetDebug,
1438 isStandaloneDebug,
1439 hasInternalSubsetDebug,
1440 hasExternalSubsetDebug,
1441 resolveEntityDebug,
1442 getEntityDebug,
1443 entityDeclDebug,
1444 notationDeclDebug,
1445 attributeDeclDebug,
1446 elementDeclDebug,
1447 unparsedEntityDeclDebug,
1448 setDocumentLocatorDebug,
1449 startDocumentDebug,
1450 endDocumentDebug,
1451 startElementDebug,
1452 endElementDebug,
1453 referenceDebug,
1454 charactersDebug,
1455 ignorableWhitespaceDebug,
1456 processingInstructionDebug,
1457 commentDebug,
1458 warningDebug,
1459 errorDebug,
1460 fatalErrorDebug,
1461 getParameterEntityDebug,
1462 cdataBlockDebug,
1463 externalSubsetDebug,
1464 1,
1465 NULL,
1466 NULL,
1467 NULL,
1468 NULL
1469};
1470
1471xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
1472
1473/*
1474 * SAX2 specific callbacks
1475 */
1476/**
1477 * startElementNsDebug:
1478 * @ctxt: An XML parser context
1479 * @name: The element name
1480 *
1481 * called when an opening tag has been processed.
1482 */
1483static void
1484startElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
1485 const xmlChar *localname,
1486 const xmlChar *prefix,
1487 const xmlChar *URI,
1488 int nb_namespaces,
1489 const xmlChar **namespaces,
1490 int nb_attributes,
1491 int nb_defaulted,
1492 const xmlChar **attributes)
1493{
1494 int i;
1495
1496 callbacks++;
1497 if (noout)
1498 return;
1499 fprintf(stdout, "SAX.startElementNs(%s", (char *) localname);
1500 if (prefix == NULL)
1501 fprintf(stdout, ", NULL");
1502 else
1503 fprintf(stdout, ", %s", (char *) prefix);
1504 if (URI == NULL)
1505 fprintf(stdout, ", NULL");
1506 else
1507 fprintf(stdout, ", '%s'", (char *) URI);
1508 fprintf(stdout, ", %d", nb_namespaces);
1509
1510 if (namespaces != NULL) {
1511 for (i = 0;i < nb_namespaces * 2;i++) {
1512 fprintf(stdout, ", xmlns");
1513 if (namespaces[i] != NULL)
1514 fprintf(stdout, ":%s", namespaces[i]);
1515 i++;
1516 fprintf(stdout, "='%s'", namespaces[i]);
1517 }
1518 }
1519 fprintf(stdout, ", %d, %d", nb_attributes, nb_defaulted);
1520 if (attributes != NULL) {
1521 for (i = 0;i < nb_attributes * 5;i += 5) {
1522 if (attributes[i + 1] != NULL)
1523 fprintf(stdout, ", %s:%s='", attributes[i + 1], attributes[i]);
1524 else
1525 fprintf(stdout, ", %s='", attributes[i]);
1526 fprintf(stdout, "%.4s...', %d", attributes[i + 3],
1527 (int)(attributes[i + 4] - attributes[i + 3]));
1528 }
1529 }
1530 fprintf(stdout, ")\n");
1531}
1532
1533/**
1534 * endElementDebug:
1535 * @ctxt: An XML parser context
1536 * @name: The element name
1537 *
1538 * called when the end of an element has been detected.
1539 */
1540static void
1541endElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
1542 const xmlChar *localname,
1543 const xmlChar *prefix,
1544 const xmlChar *URI)
1545{
1546 callbacks++;
1547 if (noout)
1548 return;
1549 fprintf(stdout, "SAX.endElementNs(%s", (char *) localname);
1550 if (prefix == NULL)
1551 fprintf(stdout, ", NULL");
1552 else
1553 fprintf(stdout, ", %s", (char *) prefix);
1554 if (URI == NULL)
1555 fprintf(stdout, ", NULL)\n");
1556 else
1557 fprintf(stdout, ", '%s')\n", (char *) URI);
1558}
1559
1560xmlSAXHandler debugSAX2HandlerStruct = {
1561 internalSubsetDebug,
1562 isStandaloneDebug,
1563 hasInternalSubsetDebug,
1564 hasExternalSubsetDebug,
1565 resolveEntityDebug,
1566 getEntityDebug,
1567 entityDeclDebug,
1568 notationDeclDebug,
1569 attributeDeclDebug,
1570 elementDeclDebug,
1571 unparsedEntityDeclDebug,
1572 setDocumentLocatorDebug,
1573 startDocumentDebug,
1574 endDocumentDebug,
1575 NULL,
1576 NULL,
1577 referenceDebug,
1578 charactersDebug,
1579 ignorableWhitespaceDebug,
1580 processingInstructionDebug,
1581 commentDebug,
1582 warningDebug,
1583 errorDebug,
1584 fatalErrorDebug,
1585 getParameterEntityDebug,
1586 cdataBlockDebug,
1587 externalSubsetDebug,
1588 XML_SAX2_MAGIC,
1589 NULL,
1590 startElementNsDebug,
1591 endElementNsDebug,
1592 NULL
1593};
1594
1595xmlSAXHandlerPtr debugSAX2Handler = &debugSAX2HandlerStruct;
1596
1597static void
1598testSAX(const char *filename) {
1599 xmlSAXHandlerPtr handler;
1600 const char *user_data = "user_data"; /* mostly for debugging */
1601 xmlParserInputBufferPtr buf = NULL;
1602 xmlParserInputPtr inputStream;
1603 xmlParserCtxtPtr ctxt = NULL;
1604 xmlSAXHandlerPtr old_sax = NULL;
1605
1606 callbacks = 0;
1607
1608 if (noout) {
1609 handler = emptySAXHandler;
1610 } else if (sax1) {
1611 handler = debugSAXHandler;
1612 } else {
1613 handler = debugSAX2Handler;
1614 }
1615
1616 /*
1617 * it's not the simplest code but the most generic in term of I/O
1618 */
1619 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
1620 if (buf == NULL) {
1621 goto error;
1622 }
1623
1624#ifdef LIBXML_SCHEMAS_ENABLED
1625 if (wxschemas != NULL) {
1626 int ret;
1627 xmlSchemaValidCtxtPtr vctxt;
1628
1629 vctxt = xmlSchemaNewValidCtxt(wxschemas);
1630 xmlSchemaSetValidErrors(vctxt,
1631 (xmlSchemaValidityErrorFunc) fprintf,
1632 (xmlSchemaValidityWarningFunc) fprintf,
1633 stderr);
1634
1635 ret = xmlSchemaValidateStream(vctxt, buf, 0, handler, user_data);
1636 if (ret == 0) {
1637 fprintf(stderr, "%s validates\n", filename);
1638 } else if (ret > 0) {
1639 fprintf(stderr, "%s fails to validate\n", filename);
1640 progresult = XMLLINT_ERR_VALID;
1641 } else {
1642 fprintf(stderr, "%s validation generated an internal error\n",
1643 filename);
1644 progresult = XMLLINT_ERR_VALID;
1645 }
1646 xmlSchemaFreeValidCtxt(vctxt);
1647 } else
1648#endif
1649 {
1650 /*
1651 * Create the parser context amd hook the input
1652 */
1653 ctxt = xmlNewParserCtxt();
1654 if (ctxt == NULL) {
1655 xmlFreeParserInputBuffer(buf);
1656 goto error;
1657 }
1658 old_sax = ctxt->sax;
1659 ctxt->sax = handler;
1660 ctxt->userData = (void *) user_data;
1661 inputStream = xmlNewIOInputStream(ctxt, buf, XML_CHAR_ENCODING_NONE);
1662 if (inputStream == NULL) {
1663 xmlFreeParserInputBuffer(buf);
1664 goto error;
1665 }
1666 inputPush(ctxt, inputStream);
1667
1668 /* do the parsing */
1669 xmlParseDocument(ctxt);
1670
1671 if (ctxt->myDoc != NULL) {
1672 fprintf(stderr, "SAX generated a doc !\n");
1673 xmlFreeDoc(ctxt->myDoc);
1674 ctxt->myDoc = NULL;
1675 }
1676 }
1677
1678error:
1679 if (ctxt != NULL) {
1680 ctxt->sax = old_sax;
1681 xmlFreeParserCtxt(ctxt);
1682 }
1683}
1684
Daniel Veillard5e873c42000-04-12 13:27:38 +00001685/************************************************************************
1686 * *
Daniel Veillard7704fb12003-01-03 16:19:51 +00001687 * Stream Test processing *
1688 * *
1689 ************************************************************************/
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001690#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001691static void processNode(xmlTextReaderPtr reader) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001692 const xmlChar *name, *value;
Daniel Veillard16ef8002005-01-31 00:27:50 +00001693 int type, empty;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001694
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001695 type = xmlTextReaderNodeType(reader);
Daniel Veillard16ef8002005-01-31 00:27:50 +00001696 empty = xmlTextReaderIsEmptyElement(reader);
Daniel Veillard99737f52003-03-22 14:55:50 +00001697
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001698 if (debug) {
1699 name = xmlTextReaderConstName(reader);
1700 if (name == NULL)
1701 name = BAD_CAST "--";
Daniel Veillard7704fb12003-01-03 16:19:51 +00001702
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001703 value = xmlTextReaderConstValue(reader);
1704
1705
1706 printf("%d %d %s %d %d",
1707 xmlTextReaderDepth(reader),
1708 type,
1709 name,
Daniel Veillard16ef8002005-01-31 00:27:50 +00001710 empty,
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001711 xmlTextReaderHasValue(reader));
1712 if (value == NULL)
1713 printf("\n");
1714 else {
1715 printf(" %s\n", value);
1716 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00001717 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001718#ifdef LIBXML_PATTERN_ENABLED
1719 if (patternc) {
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001720 xmlChar *path = NULL;
1721 int match = -1;
1722
1723 if (type == XML_READER_TYPE_ELEMENT) {
1724 /* do the check only on element start */
1725 match = xmlPatternMatch(patternc, xmlTextReaderCurrentNode(reader));
1726
1727 if (match) {
1728 path = xmlGetNodePath(xmlTextReaderCurrentNode(reader));
1729 printf("Node %s matches pattern %s\n", path, pattern);
1730 }
1731 }
1732 if (patstream != NULL) {
1733 int ret;
1734
1735 if (type == XML_READER_TYPE_ELEMENT) {
1736 ret = xmlStreamPush(patstream,
1737 xmlTextReaderConstLocalName(reader),
1738 xmlTextReaderConstNamespaceUri(reader));
1739 if (ret < 0) {
1740 fprintf(stderr, "xmlStreamPush() failure\n");
1741 xmlFreeStreamCtxt(patstream);
1742 patstream = NULL;
1743 } else if (ret != match) {
1744 if (path == NULL) {
1745 path = xmlGetNodePath(
1746 xmlTextReaderCurrentNode(reader));
1747 }
1748 fprintf(stderr,
1749 "xmlPatternMatch and xmlStreamPush disagree\n");
1750 fprintf(stderr,
1751 " pattern %s node %s\n",
1752 pattern, path);
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001753 }
1754
1755
Daniel Veillard16ef8002005-01-31 00:27:50 +00001756 }
1757 if ((type == XML_READER_TYPE_END_ELEMENT) ||
1758 ((type == XML_READER_TYPE_ELEMENT) && (empty))) {
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001759 ret = xmlStreamPop(patstream);
1760 if (ret < 0) {
1761 fprintf(stderr, "xmlStreamPop() failure\n");
1762 xmlFreeStreamCtxt(patstream);
1763 patstream = NULL;
1764 }
1765 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001766 }
Daniel Veillardf9d16912005-01-30 22:36:30 +00001767 if (path != NULL)
1768 xmlFree(path);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001769 }
1770#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001771}
1772
1773static void streamFile(char *filename) {
1774 xmlTextReaderPtr reader;
1775 int ret;
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001776#ifdef HAVE_SYS_MMAN_H
1777 int fd = -1;
1778 struct stat info;
1779 const char *base = NULL;
1780 xmlParserInputBufferPtr input = NULL;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001781
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001782 if (memory) {
1783 if (stat(filename, &info) < 0)
1784 return;
1785 if ((fd = open(filename, O_RDONLY)) < 0)
1786 return;
1787 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
1788 if (base == (void *) MAP_FAILED)
1789 return;
1790
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001791 reader = xmlReaderForMemory(base, info.st_size, filename,
1792 NULL, options);
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001793 } else
1794#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001795 reader = xmlReaderForFile(filename, NULL, options);
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001796#ifdef LIBXML_PATTERN_ENABLED
Daniel Veillardd4301ab2005-02-03 22:24:10 +00001797 if (pattern != NULL) {
1798 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
1799 if (patternc == NULL) {
1800 xmlGenericError(xmlGenericErrorContext,
1801 "Pattern %s failed to compile\n", pattern);
1802 progresult = XMLLINT_ERR_SCHEMAPAT;
1803 pattern = NULL;
1804 }
1805 }
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001806 if (patternc != NULL) {
1807 patstream = xmlPatternGetStreamCtxt(patternc);
1808 if (patstream != NULL) {
1809 ret = xmlStreamPush(patstream, NULL, NULL);
1810 if (ret < 0) {
1811 fprintf(stderr, "xmlStreamPush() failure\n");
1812 xmlFreeStreamCtxt(patstream);
1813 patstream = NULL;
1814 }
1815 }
1816 }
1817#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001818
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001819
Daniel Veillard7704fb12003-01-03 16:19:51 +00001820 if (reader != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001821#ifdef LIBXML_VALID_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001822 if (valid)
1823 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00001824 else
Daniel Veillard4432df22003-09-28 18:58:27 +00001825#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce192eb2003-04-16 15:58:05 +00001826 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001827#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +00001828 if (relaxng != NULL) {
Daniel Veillard81514ba2003-09-16 23:17:26 +00001829 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00001830 startTimer();
1831 }
1832 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
1833 if (ret < 0) {
1834 xmlGenericError(xmlGenericErrorContext,
1835 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +00001836 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +00001837 relaxng = NULL;
1838 }
Daniel Veillard81514ba2003-09-16 23:17:26 +00001839 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00001840 endTimer("Compiling the schemas");
1841 }
1842 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001843#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001844
1845 /*
1846 * Process all nodes in sequence
1847 */
Daniel Veillard81514ba2003-09-16 23:17:26 +00001848 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00001849 startTimer();
1850 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00001851 ret = xmlTextReaderRead(reader);
1852 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001853 if ((debug)
1854#ifdef LIBXML_PATTERN_ENABLED
1855 || (patternc)
1856#endif
1857 )
Daniel Veillard7704fb12003-01-03 16:19:51 +00001858 processNode(reader);
1859 ret = xmlTextReaderRead(reader);
1860 }
Daniel Veillard81514ba2003-09-16 23:17:26 +00001861 if ((timing) && (!repeat)) {
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001862#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf54cd532004-02-25 11:52:31 +00001863 if (relaxng != NULL)
Daniel Veillard49138f12004-02-19 12:58:36 +00001864 endTimer("Parsing and validating");
1865 else
Daniel Veillardf54cd532004-02-25 11:52:31 +00001866#endif
Daniel Veillard4432df22003-09-28 18:58:27 +00001867#ifdef LIBXML_VALID_ENABLED
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001868 if (valid)
Daniel Veillardce192eb2003-04-16 15:58:05 +00001869 endTimer("Parsing and validating");
1870 else
Daniel Veillard4432df22003-09-28 18:58:27 +00001871#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +00001872 endTimer("Parsing");
Daniel Veillardce192eb2003-04-16 15:58:05 +00001873 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00001874
Daniel Veillard4432df22003-09-28 18:58:27 +00001875#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf6bad792003-04-11 19:38:54 +00001876 if (valid) {
1877 if (xmlTextReaderIsValid(reader) != 1) {
1878 xmlGenericError(xmlGenericErrorContext,
1879 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001880 progresult = XMLLINT_ERR_VALID;
Daniel Veillardf6bad792003-04-11 19:38:54 +00001881 }
1882 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001883#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001884#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +00001885 if (relaxng != NULL) {
1886 if (xmlTextReaderIsValid(reader) != 1) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001887 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001888 progresult = XMLLINT_ERR_VALID;
Daniel Veillardf4e55762003-04-15 23:32:22 +00001889 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001890 fprintf(stderr, "%s validates\n", filename);
Daniel Veillardf4e55762003-04-15 23:32:22 +00001891 }
1892 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001893#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001894 /*
1895 * Done, cleanup and status
1896 */
1897 xmlFreeTextReader(reader);
1898 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001899 fprintf(stderr, "%s : failed to parse\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001900 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001901 }
1902 } else {
1903 fprintf(stderr, "Unable to open %s\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001904 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001905 }
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001906#ifdef LIBXML_PATTERN_ENABLED
1907 if (patstream != NULL) {
1908 xmlFreeStreamCtxt(patstream);
1909 patstream = NULL;
1910 }
1911#endif
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001912#ifdef HAVE_SYS_MMAN_H
1913 if (memory) {
1914 xmlFreeParserInputBuffer(input);
1915 munmap((char *) base, info.st_size);
1916 close(fd);
1917 }
1918#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001919}
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001920
1921static void walkDoc(xmlDocPtr doc) {
1922 xmlTextReaderPtr reader;
1923 int ret;
1924
Daniel Veillardd4301ab2005-02-03 22:24:10 +00001925#ifdef LIBXML_PATTERN_ENABLED
1926 xmlNodePtr root;
1927 const xmlChar *namespaces[22];
1928 int i;
1929 xmlNsPtr ns;
1930
1931 root = xmlDocGetRootElement(doc);
1932 for (ns = root->nsDef, i = 0;ns != NULL && i < 20;ns=ns->next) {
1933 namespaces[i++] = ns->href;
1934 namespaces[i++] = ns->prefix;
1935 }
1936 namespaces[i++] = NULL;
1937 namespaces[i++] = NULL;
1938
1939 if (pattern != NULL) {
1940 patternc = xmlPatterncompile((const xmlChar *) pattern, doc->dict,
1941 0, &namespaces[0]);
1942 if (patternc == NULL) {
1943 xmlGenericError(xmlGenericErrorContext,
1944 "Pattern %s failed to compile\n", pattern);
1945 progresult = XMLLINT_ERR_SCHEMAPAT;
1946 pattern = NULL;
1947 }
1948 }
Daniel Veillard2b2e02d2005-02-05 23:20:22 +00001949 if (patternc != NULL) {
1950 patstream = xmlPatternGetStreamCtxt(patternc);
1951 if (patstream != NULL) {
1952 ret = xmlStreamPush(patstream, NULL, NULL);
1953 if (ret < 0) {
1954 fprintf(stderr, "xmlStreamPush() failure\n");
1955 xmlFreeStreamCtxt(patstream);
1956 patstream = NULL;
1957 }
1958 }
1959 }
Daniel Veillardd4301ab2005-02-03 22:24:10 +00001960#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001961 reader = xmlReaderWalker(doc);
1962 if (reader != NULL) {
1963 if ((timing) && (!repeat)) {
1964 startTimer();
1965 }
1966 ret = xmlTextReaderRead(reader);
1967 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001968 if ((debug)
1969#ifdef LIBXML_PATTERN_ENABLED
1970 || (patternc)
1971#endif
1972 )
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001973 processNode(reader);
1974 ret = xmlTextReaderRead(reader);
1975 }
1976 if ((timing) && (!repeat)) {
1977 endTimer("walking through the doc");
1978 }
1979 xmlFreeTextReader(reader);
1980 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001981 fprintf(stderr, "failed to walk through the doc\n");
William M. Brack8304d872004-06-08 13:29:32 +00001982 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001983 }
1984 } else {
1985 fprintf(stderr, "Failed to crate a reader from the document\n");
William M. Brack8304d872004-06-08 13:29:32 +00001986 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001987 }
Daniel Veillard2b2e02d2005-02-05 23:20:22 +00001988#ifdef LIBXML_PATTERN_ENABLED
1989 if (patstream != NULL) {
1990 xmlFreeStreamCtxt(patstream);
1991 patstream = NULL;
1992 }
1993#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001994}
Daniel Veillard81273902003-09-30 00:43:48 +00001995#endif /* LIBXML_READER_ENABLED */
Daniel Veillard7704fb12003-01-03 16:19:51 +00001996
1997/************************************************************************
1998 * *
1999 * Tree Test processing *
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002000 * *
2001 ************************************************************************/
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002002static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
Daniel Veillard652327a2003-09-29 18:02:38 +00002003 xmlDocPtr doc = NULL;
2004#ifdef LIBXML_TREE_ENABLED
2005 xmlDocPtr tmp;
2006#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002007
Daniel Veillard48b2f892001-02-25 16:11:03 +00002008 if ((timing) && (!repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00002009 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002010
2011
Daniel Veillard652327a2003-09-29 18:02:38 +00002012#ifdef LIBXML_TREE_ENABLED
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002013 if (filename == NULL) {
2014 if (generate) {
2015 xmlNodePtr n;
2016
2017 doc = xmlNewDoc(BAD_CAST "1.0");
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002018 n = xmlNewDocNode(doc, NULL, BAD_CAST "info", NULL);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002019 xmlNodeSetContent(n, BAD_CAST "abc");
2020 xmlDocSetRootElement(doc, n);
2021 }
2022 }
Daniel Veillard652327a2003-09-29 18:02:38 +00002023#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002024#ifdef LIBXML_HTML_ENABLED
Daniel Veillard73b013f2003-09-30 12:36:01 +00002025#ifdef LIBXML_PUSH_ENABLED
William M. Brack78637da2003-07-31 14:47:38 +00002026 else if ((html) && (push)) {
2027 FILE *f;
2028
William M. Brack3403add2004-06-27 02:07:51 +00002029#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2030 f = fopen(filename, "rb");
2031#else
2032 f = fopen(filename, "r");
2033#endif
William M. Brack78637da2003-07-31 14:47:38 +00002034 if (f != NULL) {
2035 int res, size = 3;
2036 char chars[4096];
2037 htmlParserCtxtPtr ctxt;
2038
2039 /* if (repeat) */
2040 size = 4096;
2041 res = fread(chars, 1, 4, f);
2042 if (res > 0) {
2043 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
William M. Brack1d75c8a2003-10-27 13:48:16 +00002044 chars, res, filename, XML_CHAR_ENCODING_NONE);
William M. Brack78637da2003-07-31 14:47:38 +00002045 while ((res = fread(chars, 1, size, f)) > 0) {
2046 htmlParseChunk(ctxt, chars, res, 0);
2047 }
2048 htmlParseChunk(ctxt, chars, 0, 1);
2049 doc = ctxt->myDoc;
2050 htmlFreeParserCtxt(ctxt);
2051 }
2052 fclose(f);
2053 }
2054 }
Daniel Veillard73b013f2003-09-30 12:36:01 +00002055#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002056 else if (html) {
Daniel Veillard9475a352003-09-26 12:47:50 +00002057 doc = htmlReadFile(filename, NULL, options);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002058 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002059#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002060 else {
Daniel Veillard73b013f2003-09-30 12:36:01 +00002061#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002062 /*
2063 * build an XML tree from a string;
2064 */
2065 if (push) {
2066 FILE *f;
2067
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002068 /* '-' Usually means stdin -<sven@zen.org> */
2069 if ((filename[0] == '-') && (filename[1] == 0)) {
2070 f = stdin;
2071 } else {
William M. Brack3403add2004-06-27 02:07:51 +00002072#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2073 f = fopen(filename, "rb");
2074#else
2075 f = fopen(filename, "r");
2076#endif
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002077 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002078 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +00002079 int ret;
Daniel Veillarda880b122003-04-21 21:36:41 +00002080 int res, size = 1024;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002081 char chars[1024];
2082 xmlParserCtxtPtr ctxt;
2083
Daniel Veillarda880b122003-04-21 21:36:41 +00002084 /* if (repeat) size = 1024; */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002085 res = fread(chars, 1, 4, f);
2086 if (res > 0) {
2087 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
2088 chars, res, filename);
Daniel Veillard500a1de2004-03-22 15:22:58 +00002089 xmlCtxtUseOptions(ctxt, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002090 while ((res = fread(chars, 1, size, f)) > 0) {
2091 xmlParseChunk(ctxt, chars, res, 0);
2092 }
2093 xmlParseChunk(ctxt, chars, 0, 1);
2094 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +00002095 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002096 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +00002097 if (!ret) {
2098 xmlFreeDoc(doc);
2099 doc = NULL;
2100 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002101 }
2102 }
Daniel Veillard73b013f2003-09-30 12:36:01 +00002103 } else
2104#endif /* LIBXML_PUSH_ENABLED */
2105 if (testIO) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002106 if ((filename[0] == '-') && (filename[1] == 0)) {
Daniel Veillard60942de2003-09-25 21:05:58 +00002107 doc = xmlReadFd(0, NULL, NULL, options);
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002108 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002109 FILE *f;
Daniel Veillard5e873c42000-04-12 13:27:38 +00002110
William M. Brack3403add2004-06-27 02:07:51 +00002111#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2112 f = fopen(filename, "rb");
2113#else
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002114 f = fopen(filename, "r");
William M. Brack3403add2004-06-27 02:07:51 +00002115#endif
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002116 if (f != NULL) {
2117 if (rectxt == NULL)
2118 doc = xmlReadIO((xmlInputReadCallback) myRead,
2119 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +00002120 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002121 else
2122 doc = xmlCtxtReadIO(rectxt,
2123 (xmlInputReadCallback) myRead,
2124 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +00002125 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002126 } else
Daniel Veillard5e873c42000-04-12 13:27:38 +00002127 doc = NULL;
Daniel Veillard5e873c42000-04-12 13:27:38 +00002128 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002129 } else if (htmlout) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002130 xmlParserCtxtPtr ctxt;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002131
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002132 if (rectxt == NULL)
2133 ctxt = xmlNewParserCtxt();
2134 else
2135 ctxt = rectxt;
Daniel Veillard88a172f2000-08-04 18:23:10 +00002136 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002137 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +00002138 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002139 ctxt->sax->error = xmlHTMLError;
2140 ctxt->sax->warning = xmlHTMLWarning;
2141 ctxt->vctxt.error = xmlHTMLValidityError;
2142 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002143
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002144 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002145
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002146 if (rectxt == NULL)
2147 xmlFreeParserCtxt(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002148 }
Daniel Veillard46e370e2000-07-21 20:32:03 +00002149#ifdef HAVE_SYS_MMAN_H
2150 } else if (memory) {
2151 int fd;
2152 struct stat info;
2153 const char *base;
2154 if (stat(filename, &info) < 0)
2155 return;
2156 if ((fd = open(filename, O_RDONLY)) < 0)
2157 return;
2158 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +00002159 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +00002160 return;
2161
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002162 if (rectxt == NULL)
Daniel Veillard60942de2003-09-25 21:05:58 +00002163 doc = xmlReadMemory((char *) base, info.st_size,
2164 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002165 else
Daniel Veillard60942de2003-09-25 21:05:58 +00002166 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
2167 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002168
Daniel Veillard46e370e2000-07-21 20:32:03 +00002169 munmap((char *) base, info.st_size);
2170#endif
Daniel Veillard4432df22003-09-28 18:58:27 +00002171#ifdef LIBXML_VALID_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00002172 } else if (valid) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002173 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00002174
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002175 if (rectxt == NULL)
2176 ctxt = xmlNewParserCtxt();
2177 else
2178 ctxt = rectxt;
Daniel Veillardea7751d2002-12-20 00:16:24 +00002179 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002180 doc = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00002181 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002182 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
2183
2184 if (ctxt->valid == 0)
William M. Brack8304d872004-06-08 13:29:32 +00002185 progresult = XMLLINT_ERR_RDFILE;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002186 if (rectxt == NULL)
2187 xmlFreeParserCtxt(ctxt);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002188 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002189#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00002190 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002191 if (rectxt != NULL)
2192 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
Daniel Veillard81562d22005-06-15 13:27:56 +00002193 else {
2194#ifdef LIBXML_SAX1_ENABLED
2195 if (sax1)
2196 doc = xmlParseFile(filename);
2197 else
2198#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002199 doc = xmlReadFile(filename, NULL, options);
Daniel Veillard81562d22005-06-15 13:27:56 +00002200 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00002201 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002202 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002203
Daniel Veillard88a172f2000-08-04 18:23:10 +00002204 /*
2205 * If we don't have a document we might as well give up. Do we
2206 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002207 if (doc == NULL) {
William M. Brack8304d872004-06-08 13:29:32 +00002208 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard88a172f2000-08-04 18:23:10 +00002209 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002210 }
2211
Daniel Veillard48b2f892001-02-25 16:11:03 +00002212 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002213 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002214 }
2215
Daniel Veillard29e43992001-12-13 22:21:58 +00002216 /*
2217 * Remove DOCTYPE nodes
2218 */
2219 if (dropdtd) {
2220 xmlDtdPtr dtd;
2221
2222 dtd = xmlGetIntSubset(doc);
2223 if (dtd != NULL) {
2224 xmlUnlinkNode((xmlNodePtr)dtd);
2225 xmlFreeDtd(dtd);
2226 }
2227 }
2228
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002229#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +00002230 if (xinclude) {
2231 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002232 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002233 }
William M. Brack4e1c2db2005-02-11 10:58:55 +00002234 if (xmlXIncludeProcessFlags(doc, options) < 0)
2235 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard48b2f892001-02-25 16:11:03 +00002236 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002237 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002238 }
2239 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002240#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +00002241
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002242#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00002243#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002244 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002245 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002246 */
2247 if (shell)
2248 xmlShell(doc, filename, xmlShellReadline, stdout);
2249#endif
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00002250#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002251
Daniel Veillard652327a2003-09-29 18:02:38 +00002252#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002253 /*
2254 * test intermediate copy if needed.
2255 */
2256 if (copy) {
2257 tmp = doc;
Daniel Veillard4edd3ed2004-09-20 20:03:01 +00002258 if (timing) {
2259 startTimer();
2260 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002261 doc = xmlCopyDoc(doc, 1);
Daniel Veillard4edd3ed2004-09-20 20:03:01 +00002262 if (timing) {
2263 endTimer("Copying");
2264 }
2265 if (timing) {
2266 startTimer();
2267 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002268 xmlFreeDoc(tmp);
Daniel Veillard4edd3ed2004-09-20 20:03:01 +00002269 if (timing) {
2270 endTimer("Freeing original");
2271 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002272 }
Daniel Veillard652327a2003-09-29 18:02:38 +00002273#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002274
Daniel Veillard4432df22003-09-28 18:58:27 +00002275#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002276 if ((insert) && (!html)) {
2277 const xmlChar* list[256];
2278 int nb, i;
2279 xmlNodePtr node;
2280
2281 if (doc->children != NULL) {
2282 node = doc->children;
2283 while ((node != NULL) && (node->last == NULL)) node = node->next;
2284 if (node != NULL) {
2285 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
2286 if (nb < 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002287 fprintf(stderr, "could not get valid list of elements\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002288 } else if (nb == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002289 fprintf(stderr, "No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002290 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002291 fprintf(stderr, "%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002292 nb);
2293 for (i = 0;i < nb;i++) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002294 fprintf(stderr, "%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002295 }
2296 }
2297 }
2298 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002299 }else
2300#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002301#ifdef LIBXML_READER_ENABLED
2302 if (walker) {
2303 walkDoc(doc);
2304 }
2305#endif /* LIBXML_READER_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002306#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00002307 if (noout == 0) {
Daniel Veillard3df01182003-12-10 10:17:51 +00002308 int ret;
2309
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002310 /*
2311 * print it.
2312 */
2313#ifdef LIBXML_DEBUG_ENABLED
2314 if (!debug) {
2315#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00002316 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002317 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002318 }
Daniel Veillard656ce942004-04-30 23:11:45 +00002319#ifdef LIBXML_HTML_ENABLED
Daniel Veillard42fd4122003-11-04 08:47:48 +00002320 if ((html) && (!xmlout)) {
2321 if (compress) {
2322 htmlSaveFile(output ? output : "-", doc);
2323 }
2324 else if (encoding != NULL) {
2325 if ( format ) {
2326 htmlSaveFileFormat(output ? output : "-", doc, encoding, 1);
2327 }
2328 else {
2329 htmlSaveFileFormat(output ? output : "-", doc, encoding, 0);
2330 }
2331 }
2332 else if (format) {
2333 htmlSaveFileFormat(output ? output : "-", doc, NULL, 1);
2334 }
2335 else {
2336 FILE *out;
2337 if (output == NULL)
2338 out = stdout;
2339 else {
2340 out = fopen(output,"wb");
2341 }
2342 if (out != NULL) {
2343 if (htmlDocDump(out, doc) < 0)
William M. Brack8304d872004-06-08 13:29:32 +00002344 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00002345
2346 if (output != NULL)
2347 fclose(out);
2348 } else {
2349 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00002350 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00002351 }
2352 }
2353 if ((timing) && (!repeat)) {
2354 endTimer("Saving");
2355 }
2356 } else
2357#endif
Daniel Veillard25048d82004-08-14 22:37:54 +00002358#ifdef LIBXML_C14N_ENABLED
2359 if (canonical) {
2360 xmlChar *result = NULL;
2361 int size;
2362
2363 size = xmlC14NDocDumpMemory(doc, NULL, 0, NULL, 1, &result);
2364 if (size >= 0) {
2365 write(1, result, size);
2366 xmlFree(result);
2367 } else {
2368 fprintf(stderr, "Failed to canonicalize\n");
2369 progresult = XMLLINT_ERR_OUT;
2370 }
2371 } else
Aleksey Sanin2650df12005-06-06 17:16:50 +00002372 if (exc_canonical) {
2373 xmlChar *result = NULL;
2374 int size;
2375
2376 size = xmlC14NDocDumpMemory(doc, NULL, 1, NULL, 1, &result);
2377 if (size >= 0) {
2378 write(1, result, size);
2379 xmlFree(result);
2380 } else {
2381 fprintf(stderr, "Failed to canonicalize\n");
2382 progresult = XMLLINT_ERR_OUT;
2383 }
2384 } else
Daniel Veillard25048d82004-08-14 22:37:54 +00002385#endif
Daniel Veillard3b2c2612001-04-04 00:09:00 +00002386#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002387 if (memory) {
2388 xmlChar *result;
2389 int len;
2390
2391 if (encoding != NULL) {
Daniel Veillardd536f702001-11-08 17:32:47 +00002392 if ( format ) {
2393 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
2394 } else {
2395 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
2396 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002397 } else {
Daniel Veillard90493a92001-08-14 14:12:47 +00002398 if (format)
2399 xmlDocDumpFormatMemory(doc, &result, &len, 1);
2400 else
2401 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002402 }
2403 if (result == NULL) {
2404 fprintf(stderr, "Failed to save\n");
Daniel Veillard25048d82004-08-14 22:37:54 +00002405 progresult = XMLLINT_ERR_OUT;
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002406 } else {
2407 write(1, result, len);
2408 xmlFree(result);
2409 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +00002410 } else
2411#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002412 if (compress) {
2413 xmlSaveFile(output ? output : "-", doc);
2414 }
Daniel Veillardd536f702001-11-08 17:32:47 +00002415 else if (encoding != NULL) {
2416 if ( format ) {
Daniel Veillard3df01182003-12-10 10:17:51 +00002417 ret = xmlSaveFormatFileEnc(output ? output : "-", doc,
2418 encoding, 1);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002419 }
Daniel Veillardd536f702001-11-08 17:32:47 +00002420 else {
Daniel Veillard3df01182003-12-10 10:17:51 +00002421 ret = xmlSaveFileEnc(output ? output : "-", doc, encoding);
2422 }
2423 if (ret < 0) {
2424 fprintf(stderr, "failed save to %s\n",
2425 output ? output : "-");
William M. Brack8304d872004-06-08 13:29:32 +00002426 progresult = XMLLINT_ERR_OUT;
Daniel Veillardd536f702001-11-08 17:32:47 +00002427 }
2428 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002429 else if (format) {
Daniel Veillard3df01182003-12-10 10:17:51 +00002430 ret = xmlSaveFormatFile(output ? output : "-", doc, 1);
2431 if (ret < 0) {
2432 fprintf(stderr, "failed save to %s\n",
2433 output ? output : "-");
William M. Brack8304d872004-06-08 13:29:32 +00002434 progresult = XMLLINT_ERR_OUT;
Daniel Veillard3df01182003-12-10 10:17:51 +00002435 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002436 }
2437 else {
2438 FILE *out;
2439 if (output == NULL)
2440 out = stdout;
2441 else {
2442 out = fopen(output,"wb");
2443 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00002444 if (out != NULL) {
Daniel Veillard828ce832003-10-08 19:19:10 +00002445 if (xmlDocDump(out, doc) < 0)
William M. Brack8304d872004-06-08 13:29:32 +00002446 progresult = XMLLINT_ERR_OUT;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002447
Daniel Veillard05d987b2003-10-08 11:54:57 +00002448 if (output != NULL)
2449 fclose(out);
2450 } else {
2451 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00002452 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00002453 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002454 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002455 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002456 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002457 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002458#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002459 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002460 FILE *out;
2461 if (output == NULL)
2462 out = stdout;
2463 else {
2464 out = fopen(output,"wb");
2465 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00002466 if (out != NULL) {
2467 xmlDebugDumpDocument(out, doc);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002468
Daniel Veillard05d987b2003-10-08 11:54:57 +00002469 if (output != NULL)
2470 fclose(out);
2471 } else {
2472 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00002473 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00002474 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002475 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002476#endif
2477 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002478#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002479
Daniel Veillard4432df22003-09-28 18:58:27 +00002480#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002481 /*
2482 * A posteriori validation test
2483 */
Daniel Veillard66f68e72003-08-18 16:39:51 +00002484 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
Daniel Veillardcd429612000-10-11 15:57:05 +00002485 xmlDtdPtr dtd;
2486
Daniel Veillard48b2f892001-02-25 16:11:03 +00002487 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002488 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002489 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00002490 if (dtdvalid != NULL)
2491 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
2492 else
2493 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002494 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002495 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002496 }
Daniel Veillardcd429612000-10-11 15:57:05 +00002497 if (dtd == NULL) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00002498 if (dtdvalid != NULL)
2499 xmlGenericError(xmlGenericErrorContext,
2500 "Could not parse DTD %s\n", dtdvalid);
2501 else
2502 xmlGenericError(xmlGenericErrorContext,
2503 "Could not parse DTD %s\n", dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00002504 progresult = XMLLINT_ERR_DTD;
Daniel Veillardcd429612000-10-11 15:57:05 +00002505 } else {
Daniel Veillarda37aab82003-06-09 09:10:36 +00002506 xmlValidCtxtPtr cvp;
2507
2508 if ((cvp = xmlNewValidCtxt()) == NULL) {
2509 xmlGenericError(xmlGenericErrorContext,
2510 "Couldn't allocate validation context\n");
2511 exit(-1);
2512 }
2513 cvp->userData = (void *) stderr;
2514 cvp->error = (xmlValidityErrorFunc) fprintf;
2515 cvp->warning = (xmlValidityWarningFunc) fprintf;
2516
Daniel Veillard48b2f892001-02-25 16:11:03 +00002517 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002518 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002519 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002520 if (!xmlValidateDtd(cvp, doc, dtd)) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00002521 if (dtdvalid != NULL)
2522 xmlGenericError(xmlGenericErrorContext,
2523 "Document %s does not validate against %s\n",
2524 filename, dtdvalid);
2525 else
2526 xmlGenericError(xmlGenericErrorContext,
2527 "Document %s does not validate against %s\n",
2528 filename, dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00002529 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00002530 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002531 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002532 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002533 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002534 xmlFreeValidCtxt(cvp);
Daniel Veillardcd429612000-10-11 15:57:05 +00002535 xmlFreeDtd(dtd);
2536 }
2537 } else if (postvalid) {
Daniel Veillarda37aab82003-06-09 09:10:36 +00002538 xmlValidCtxtPtr cvp;
2539
2540 if ((cvp = xmlNewValidCtxt()) == NULL) {
2541 xmlGenericError(xmlGenericErrorContext,
2542 "Couldn't allocate validation context\n");
2543 exit(-1);
2544 }
2545
Daniel Veillard48b2f892001-02-25 16:11:03 +00002546 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002547 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002548 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002549 cvp->userData = (void *) stderr;
2550 cvp->error = (xmlValidityErrorFunc) fprintf;
2551 cvp->warning = (xmlValidityWarningFunc) fprintf;
2552 if (!xmlValidateDocument(cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00002553 xmlGenericError(xmlGenericErrorContext,
2554 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00002555 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00002556 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002557 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002558 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002559 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002560 xmlFreeValidCtxt(cvp);
Daniel Veillard4432df22003-09-28 18:58:27 +00002561 }
2562#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00002563#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00002564 if (relaxngschemas != NULL) {
Daniel Veillard71531f32003-02-05 13:19:53 +00002565 xmlRelaxNGValidCtxtPtr ctxt;
2566 int ret;
2567
Daniel Veillard42f12e92003-03-07 18:32:59 +00002568 if ((timing) && (!repeat)) {
2569 startTimer();
2570 }
2571
Daniel Veillard71531f32003-02-05 13:19:53 +00002572 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2573 xmlRelaxNGSetValidErrors(ctxt,
2574 (xmlRelaxNGValidityErrorFunc) fprintf,
2575 (xmlRelaxNGValidityWarningFunc) fprintf,
2576 stderr);
2577 ret = xmlRelaxNGValidateDoc(ctxt, doc);
2578 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002579 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard71531f32003-02-05 13:19:53 +00002580 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002581 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00002582 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00002583 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002584 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard71531f32003-02-05 13:19:53 +00002585 filename);
William M. Brack8304d872004-06-08 13:29:32 +00002586 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00002587 }
2588 xmlRelaxNGFreeValidCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002589 if ((timing) && (!repeat)) {
2590 endTimer("Validating");
2591 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002592 } else if (wxschemas != NULL) {
2593 xmlSchemaValidCtxtPtr ctxt;
2594 int ret;
2595
2596 if ((timing) && (!repeat)) {
2597 startTimer();
2598 }
2599
2600 ctxt = xmlSchemaNewValidCtxt(wxschemas);
2601 xmlSchemaSetValidErrors(ctxt,
2602 (xmlSchemaValidityErrorFunc) fprintf,
2603 (xmlSchemaValidityWarningFunc) fprintf,
2604 stderr);
2605 ret = xmlSchemaValidateDoc(ctxt, doc);
2606 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002607 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002608 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002609 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00002610 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002611 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002612 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002613 filename);
William M. Brack8304d872004-06-08 13:29:32 +00002614 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002615 }
2616 xmlSchemaFreeValidCtxt(ctxt);
2617 if ((timing) && (!repeat)) {
2618 endTimer("Validating");
2619 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002620 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002621#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002622
2623#ifdef LIBXML_DEBUG_ENABLED
2624 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00002625 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002626#endif
2627
2628 /*
2629 * free it.
2630 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00002631 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002632 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002633 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002634 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002635 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002636 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002637 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002638}
2639
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002640/************************************************************************
2641 * *
2642 * Usage and Main *
2643 * *
2644 ************************************************************************/
2645
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00002646static void showVersion(const char *name) {
2647 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
2648 fprintf(stderr, " compiled with: ");
Daniel Veillard4432df22003-09-28 18:58:27 +00002649#ifdef LIBXML_VALID_ENABLED
2650 fprintf(stderr, "DTDValid ");
2651#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00002652#ifdef LIBXML_FTP_ENABLED
2653 fprintf(stderr, "FTP ");
2654#endif
2655#ifdef LIBXML_HTTP_ENABLED
2656 fprintf(stderr, "HTTP ");
2657#endif
2658#ifdef LIBXML_HTML_ENABLED
2659 fprintf(stderr, "HTML ");
2660#endif
2661#ifdef LIBXML_C14N_ENABLED
2662 fprintf(stderr, "C14N ");
2663#endif
2664#ifdef LIBXML_CATALOG_ENABLED
2665 fprintf(stderr, "Catalog ");
2666#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00002667#ifdef LIBXML_XPATH_ENABLED
2668 fprintf(stderr, "XPath ");
2669#endif
2670#ifdef LIBXML_XPTR_ENABLED
2671 fprintf(stderr, "XPointer ");
2672#endif
2673#ifdef LIBXML_XINCLUDE_ENABLED
2674 fprintf(stderr, "XInclude ");
2675#endif
2676#ifdef LIBXML_ICONV_ENABLED
2677 fprintf(stderr, "Iconv ");
2678#endif
2679#ifdef DEBUG_MEMORY_LOCATION
2680 fprintf(stderr, "MemDebug ");
2681#endif
2682#ifdef LIBXML_UNICODE_ENABLED
2683 fprintf(stderr, "Unicode ");
2684#endif
2685#ifdef LIBXML_REGEXP_ENABLED
2686 fprintf(stderr, "Regexps ");
2687#endif
2688#ifdef LIBXML_AUTOMATA_ENABLED
2689 fprintf(stderr, "Automata ");
2690#endif
2691#ifdef LIBXML_SCHEMAS_ENABLED
2692 fprintf(stderr, "Schemas ");
2693#endif
Daniel Veillardce1648b2005-01-04 15:10:22 +00002694#ifdef LIBXML_MODULES_ENABLED
2695 fprintf(stderr, "Modules ");
2696#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00002697 fprintf(stderr, "\n");
2698}
2699
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002700static void usage(const char *name) {
2701 printf("Usage : %s [options] XMLfiles ...\n", name);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002702#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002703 printf("\tParse the XML files and output the result of the parsing\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002704#else
2705 printf("\tParse the XML files\n");
2706#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002707 printf("\t--version : display the version of the XML library used\n");
2708#ifdef LIBXML_DEBUG_ENABLED
2709 printf("\t--debug : dump a debug tree of the in-memory document\n");
2710 printf("\t--shell : run a navigating shell\n");
2711 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00002712#else
Daniel Veillard81273902003-09-30 00:43:48 +00002713#ifdef LIBXML_READER_ENABLED
Daniel Veillard8326e732003-01-07 00:19:07 +00002714 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard81273902003-09-30 00:43:48 +00002715#endif /* LIBXML_READER_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002716#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00002717#ifdef LIBXML_TREE_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002718 printf("\t--copy : used to test the internal copy implementation\n");
Daniel Veillard652327a2003-09-29 18:02:38 +00002719#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002720 printf("\t--recover : output what was parsable on broken XML documents\n");
2721 printf("\t--noent : substitute entity references by their value\n");
2722 printf("\t--noout : don't output the result tree\n");
Daniel Veillard0bff36d2004-08-31 09:37:03 +00002723 printf("\t--path 'paths': provide a set of paths for resources\n");
2724 printf("\t--load-trace : print trace of all external entites loaded\n");
Daniel Veillarde8b09e42003-05-13 22:14:13 +00002725 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002726 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00002727 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00002728#ifdef LIBXML_VALID_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002729 printf("\t--valid : validate the document in addition to std well-formed check\n");
2730 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
2731 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
Daniel Veillard66f68e72003-08-18 16:39:51 +00002732 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00002733#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002734 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002735 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002736 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
2737 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002738#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002739#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002740 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002741#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002742#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002743#ifdef LIBXML_HTML_ENABLED
2744 printf("\t--html : use the HTML parser\n");
Daniel Veillard42fd4122003-11-04 08:47:48 +00002745 printf("\t--xmlout : force to use the XML serializer when using --html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002746#endif
Daniel Veillard73b013f2003-09-30 12:36:01 +00002747#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002748 printf("\t--push : use the push mode of the parser\n");
Daniel Veillard73b013f2003-09-30 12:36:01 +00002749#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002750#ifdef HAVE_SYS_MMAN_H
2751 printf("\t--memory : parse from memory\n");
2752#endif
Daniel Veillard87076042004-05-03 22:54:49 +00002753 printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002754 printf("\t--nowarning : do not emit warnings from parser/validator\n");
2755 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillarddca8cc72003-09-26 13:53:14 +00002756 printf("\t--nocdata : replace cdata section with text nodes\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002757#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00002758 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002759 printf("\t--encode encoding : output in the given encoding\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002760 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
2761#endif /* LIBXML_OUTPUT_ENABLED */
Aleksey Sanin2650df12005-06-06 17:16:50 +00002762 printf("\t--c14n : save in W3C canonical format (with comments)\n");
2763 printf("\t--exc-c14n : save in W3C exclusive canonical format (with comments)\n");
Daniel Veillard25048d82004-08-14 22:37:54 +00002764#ifdef LIBXML_C14N_ENABLED
2765#endif /* LIBXML_C14N_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002766 printf("\t--nsclean : remove redundant namespace declarations\n");
2767 printf("\t--testIO : test user I/O support\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002768#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00002769 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
2770 printf("\t otherwise XML Catalogs starting from \n");
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002771 printf("\t %s are activated by default\n", XML_XML_DEFAULT_CATALOG);
Daniel Veillard05c13a22001-09-09 08:38:09 +00002772 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002773#endif
2774 printf("\t--auto : generate a small doc on the fly\n");
2775#ifdef LIBXML_XINCLUDE_ENABLED
2776 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00002777 printf("\t--noxincludenode : same but do not generate XInclude nodes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002778#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002779 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00002780 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard81273902003-09-30 00:43:48 +00002781#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00002782 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002783 printf("\t--walker : create a reader and walk though the resulting doc\n");
Daniel Veillard81273902003-09-30 00:43:48 +00002784#endif /* LIBXML_READER_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002785#ifdef LIBXML_PATTERN_ENABLED
2786 printf("\t--pattern pattern_value : test the pattern support\n");
2787#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002788 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillardef4d3bc2003-02-07 12:38:22 +00002789#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard71531f32003-02-05 13:19:53 +00002790 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002791 printf("\t--schema schema : do validation against the WXS schema\n");
Daniel Veillard71531f32003-02-05 13:19:53 +00002792#endif
Daniel Veillarda42f25f2002-01-25 14:15:40 +00002793 printf("\nLibxml project home page: http://xmlsoft.org/\n");
2794 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002795}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002796
2797static void registerNode(xmlNodePtr node)
2798{
2799 node->_private = malloc(sizeof(long));
2800 *(long*)node->_private = (long) 0x81726354;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002801 nbregister++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002802}
2803
2804static void deregisterNode(xmlNodePtr node)
2805{
2806 assert(node->_private != NULL);
2807 assert(*(long*)node->_private == (long) 0x81726354);
2808 free(node->_private);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002809 nbregister--;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002810}
2811
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002812int
2813main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00002814 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002815 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00002816 int version = 0;
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00002817 const char* indent;
2818
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002819 if (argc <= 1) {
2820 usage(argv[0]);
2821 return(1);
2822 }
Daniel Veillardbe803962000-06-28 23:40:59 +00002823 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002824 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002825 if (!strcmp(argv[i], "-"))
2826 break;
2827
2828 if (argv[i][0] != '-')
2829 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002830 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
2831 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00002832 else
2833#ifdef LIBXML_DEBUG_ENABLED
2834 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002835 (!strcmp(argv[i], "--shell"))) {
2836 shell++;
2837 noout = 1;
2838 } else
2839#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00002840#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002841 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
2842 copy++;
Daniel Veillard652327a2003-09-29 18:02:38 +00002843 else
2844#endif /* LIBXML_TREE_ENABLED */
2845 if ((!strcmp(argv[i], "-recover")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002846 (!strcmp(argv[i], "--recover"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002847 recovery++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002848 options |= XML_PARSE_RECOVER;
2849 } else if ((!strcmp(argv[i], "-noent")) ||
2850 (!strcmp(argv[i], "--noent"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002851 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002852 options |= XML_PARSE_NOENT;
Daniel Veillarddca8cc72003-09-26 13:53:14 +00002853 } else if ((!strcmp(argv[i], "-nsclean")) ||
2854 (!strcmp(argv[i], "--nsclean"))) {
2855 options |= XML_PARSE_NSCLEAN;
2856 } else if ((!strcmp(argv[i], "-nocdata")) ||
2857 (!strcmp(argv[i], "--nocdata"))) {
2858 options |= XML_PARSE_NOCDATA;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002859 } else if ((!strcmp(argv[i], "-nodict")) ||
2860 (!strcmp(argv[i], "--nodict"))) {
2861 options |= XML_PARSE_NODICT;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002862 } else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00002863 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00002864 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00002865 version = 1;
2866 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002867 (!strcmp(argv[i], "--noout")))
2868 noout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002869#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002870 else if ((!strcmp(argv[i], "-o")) ||
2871 (!strcmp(argv[i], "-output")) ||
2872 (!strcmp(argv[i], "--output"))) {
2873 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00002874 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002875 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002876#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002877 else if ((!strcmp(argv[i], "-htmlout")) ||
2878 (!strcmp(argv[i], "--htmlout")))
2879 htmlout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002880 else if ((!strcmp(argv[i], "-nowrap")) ||
2881 (!strcmp(argv[i], "--nowrap")))
2882 nowrap++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002883#ifdef LIBXML_HTML_ENABLED
2884 else if ((!strcmp(argv[i], "-html")) ||
2885 (!strcmp(argv[i], "--html"))) {
2886 html++;
2887 }
Daniel Veillard42fd4122003-11-04 08:47:48 +00002888 else if ((!strcmp(argv[i], "-xmlout")) ||
2889 (!strcmp(argv[i], "--xmlout"))) {
2890 xmlout++;
2891 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002892#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002893 else if ((!strcmp(argv[i], "-loaddtd")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002894 (!strcmp(argv[i], "--loaddtd"))) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002895 loaddtd++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002896 options |= XML_PARSE_DTDLOAD;
2897 } else if ((!strcmp(argv[i], "-dtdattr")) ||
Daniel Veillard48da9102001-08-07 01:10:10 +00002898 (!strcmp(argv[i], "--dtdattr"))) {
2899 loaddtd++;
2900 dtdattrs++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002901 options |= XML_PARSE_DTDATTR;
Daniel Veillard4432df22003-09-28 18:58:27 +00002902 }
2903#ifdef LIBXML_VALID_ENABLED
2904 else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002905 (!strcmp(argv[i], "--valid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002906 valid++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002907 options |= XML_PARSE_DTDVALID;
2908 } else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002909 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002910 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002911 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00002912 options |= XML_PARSE_DTDLOAD;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002913 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00002914 (!strcmp(argv[i], "--dtdvalid"))) {
2915 i++;
2916 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002917 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00002918 options |= XML_PARSE_DTDLOAD;
Daniel Veillard66f68e72003-08-18 16:39:51 +00002919 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
2920 (!strcmp(argv[i], "--dtdvalidfpi"))) {
2921 i++;
2922 dtdvalidfpi = argv[i];
2923 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00002924 options |= XML_PARSE_DTDLOAD;
Daniel Veillardcd429612000-10-11 15:57:05 +00002925 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002926#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard29e43992001-12-13 22:21:58 +00002927 else if ((!strcmp(argv[i], "-dropdtd")) ||
2928 (!strcmp(argv[i], "--dropdtd")))
2929 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002930 else if ((!strcmp(argv[i], "-insert")) ||
2931 (!strcmp(argv[i], "--insert")))
2932 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00002933 else if ((!strcmp(argv[i], "-timing")) ||
2934 (!strcmp(argv[i], "--timing")))
2935 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002936 else if ((!strcmp(argv[i], "-auto")) ||
2937 (!strcmp(argv[i], "--auto")))
2938 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002939 else if ((!strcmp(argv[i], "-repeat")) ||
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00002940 (!strcmp(argv[i], "--repeat"))) {
2941 if (repeat)
2942 repeat *= 10;
2943 else
2944 repeat = 100;
Daniel Veillard73b013f2003-09-30 12:36:01 +00002945 }
2946#ifdef LIBXML_PUSH_ENABLED
2947 else if ((!strcmp(argv[i], "-push")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002948 (!strcmp(argv[i], "--push")))
2949 push++;
Daniel Veillard73b013f2003-09-30 12:36:01 +00002950#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +00002951#ifdef HAVE_SYS_MMAN_H
2952 else if ((!strcmp(argv[i], "-memory")) ||
2953 (!strcmp(argv[i], "--memory")))
2954 memory++;
2955#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00002956 else if ((!strcmp(argv[i], "-testIO")) ||
2957 (!strcmp(argv[i], "--testIO")))
2958 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002959#ifdef LIBXML_XINCLUDE_ENABLED
2960 else if ((!strcmp(argv[i], "-xinclude")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002961 (!strcmp(argv[i], "--xinclude"))) {
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002962 xinclude++;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002963 options |= XML_PARSE_XINCLUDE;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002964 }
Daniel Veillardc14c3892004-08-16 12:34:50 +00002965 else if ((!strcmp(argv[i], "-noxincludenode")) ||
2966 (!strcmp(argv[i], "--noxincludenode"))) {
2967 xinclude++;
2968 options |= XML_PARSE_XINCLUDE;
2969 options |= XML_PARSE_NOXINCNODE;
2970 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002971#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002972#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002973#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002974 else if ((!strcmp(argv[i], "-compress")) ||
2975 (!strcmp(argv[i], "--compress"))) {
2976 compress++;
2977 xmlSetCompressMode(9);
2978 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002979#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002980#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002981 else if ((!strcmp(argv[i], "-nowarning")) ||
2982 (!strcmp(argv[i], "--nowarning"))) {
2983 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00002984 xmlPedanticParserDefault(0);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002985 options |= XML_PARSE_NOWARNING;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002986 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00002987 else if ((!strcmp(argv[i], "-pedantic")) ||
2988 (!strcmp(argv[i], "--pedantic"))) {
2989 xmlGetWarningsDefaultValue = 1;
2990 xmlPedanticParserDefault(1);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002991 options |= XML_PARSE_PEDANTIC;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00002992 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00002993#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00002994 else if ((!strcmp(argv[i], "-debugent")) ||
2995 (!strcmp(argv[i], "--debugent"))) {
2996 debugent++;
2997 xmlParserDebugEntities = 1;
2998 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00002999#endif
Daniel Veillard25048d82004-08-14 22:37:54 +00003000#ifdef LIBXML_C14N_ENABLED
3001 else if ((!strcmp(argv[i], "-c14n")) ||
3002 (!strcmp(argv[i], "--c14n"))) {
3003 canonical++;
3004 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
3005 }
Aleksey Sanin2650df12005-06-06 17:16:50 +00003006 else if ((!strcmp(argv[i], "-exc-c14n")) ||
3007 (!strcmp(argv[i], "--exc-c14n"))) {
3008 exc_canonical++;
3009 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
3010 }
Daniel Veillard25048d82004-08-14 22:37:54 +00003011#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00003012#ifdef LIBXML_CATALOG_ENABLED
3013 else if ((!strcmp(argv[i], "-catalogs")) ||
3014 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003015 catalogs++;
3016 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
3017 (!strcmp(argv[i], "--nocatalogs"))) {
3018 nocatalogs++;
Daniel Veillard81418e32001-05-22 15:08:55 +00003019 }
3020#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00003021 else if ((!strcmp(argv[i], "-encode")) ||
3022 (!strcmp(argv[i], "--encode"))) {
3023 i++;
3024 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003025 /*
3026 * OK it's for testing purposes
3027 */
3028 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00003029 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003030 else if ((!strcmp(argv[i], "-noblanks")) ||
3031 (!strcmp(argv[i], "--noblanks"))) {
3032 noblanks++;
3033 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00003034 }
Daniel Veillard87076042004-05-03 22:54:49 +00003035 else if ((!strcmp(argv[i], "-maxmem")) ||
3036 (!strcmp(argv[i], "--maxmem"))) {
3037 i++;
3038 if (sscanf(argv[i], "%d", &maxmem) == 1) {
3039 xmlMemSetup(myFreeFunc, myMallocFunc, myReallocFunc,
3040 myStrdupFunc);
3041 } else {
3042 maxmem = 0;
3043 }
3044 }
Daniel Veillard90493a92001-08-14 14:12:47 +00003045 else if ((!strcmp(argv[i], "-format")) ||
3046 (!strcmp(argv[i], "--format"))) {
3047 noblanks++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003048#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00003049 format++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003050#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard90493a92001-08-14 14:12:47 +00003051 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00003052 }
Daniel Veillard81273902003-09-30 00:43:48 +00003053#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00003054 else if ((!strcmp(argv[i], "-stream")) ||
3055 (!strcmp(argv[i], "--stream"))) {
3056 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003057 }
Daniel Veillard7899c5c2003-11-03 12:31:38 +00003058 else if ((!strcmp(argv[i], "-walker")) ||
3059 (!strcmp(argv[i], "--walker"))) {
3060 walker++;
3061 noout++;
3062 }
Daniel Veillard81273902003-09-30 00:43:48 +00003063#endif /* LIBXML_READER_ENABLED */
3064#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00003065 else if ((!strcmp(argv[i], "-sax1")) ||
3066 (!strcmp(argv[i], "--sax1"))) {
3067 sax1++;
3068 }
Daniel Veillard81273902003-09-30 00:43:48 +00003069#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003070 else if ((!strcmp(argv[i], "-sax")) ||
3071 (!strcmp(argv[i], "--sax"))) {
3072 sax++;
3073 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003074 else if ((!strcmp(argv[i], "-chkregister")) ||
3075 (!strcmp(argv[i], "--chkregister"))) {
3076 chkregister++;
Daniel Veillard71531f32003-02-05 13:19:53 +00003077#ifdef LIBXML_SCHEMAS_ENABLED
3078 } else if ((!strcmp(argv[i], "-relaxng")) ||
3079 (!strcmp(argv[i], "--relaxng"))) {
3080 i++;
3081 relaxng = argv[i];
3082 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003083 options |= XML_PARSE_NOENT;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003084 } else if ((!strcmp(argv[i], "-schema")) ||
3085 (!strcmp(argv[i], "--schema"))) {
3086 i++;
3087 schema = argv[i];
3088 noent++;
Daniel Veillard71531f32003-02-05 13:19:53 +00003089#endif
Daniel Veillarde8b09e42003-05-13 22:14:13 +00003090 } else if ((!strcmp(argv[i], "-nonet")) ||
3091 (!strcmp(argv[i], "--nonet"))) {
Daniel Veillard61b93382003-11-03 14:28:31 +00003092 options |= XML_PARSE_NONET;
Daniel Veillard0bff36d2004-08-31 09:37:03 +00003093 } else if ((!strcmp(argv[i], "-load-trace")) ||
3094 (!strcmp(argv[i], "--load-trace"))) {
3095 load_trace++;
3096 } else if ((!strcmp(argv[i], "-path")) ||
3097 (!strcmp(argv[i], "--path"))) {
3098 i++;
3099 parsePath(BAD_CAST argv[i]);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003100#ifdef LIBXML_PATTERN_ENABLED
3101 } else if ((!strcmp(argv[i], "-pattern")) ||
3102 (!strcmp(argv[i], "--pattern"))) {
3103 i++;
3104 pattern = argv[i];
3105#endif
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003106 } else {
3107 fprintf(stderr, "Unknown option %s\n", argv[i]);
3108 usage(argv[0]);
3109 return(1);
3110 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003111 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003112
3113#ifdef LIBXML_CATALOG_ENABLED
3114 if (nocatalogs == 0) {
3115 if (catalogs) {
3116 const char *catal;
3117
3118 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003119 if (catal != NULL) {
3120 xmlLoadCatalogs(catal);
3121 } else {
3122 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
3123 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003124 }
3125 }
3126#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003127
Daniel Veillard81273902003-09-30 00:43:48 +00003128#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00003129 if (sax1)
3130 xmlSAXDefaultVersion(1);
3131 else
3132 xmlSAXDefaultVersion(2);
Daniel Veillard81273902003-09-30 00:43:48 +00003133#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00003134
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003135 if (chkregister) {
3136 xmlRegisterNodeDefault(registerNode);
3137 xmlDeregisterNodeDefault(deregisterNode);
3138 }
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00003139
3140 indent = getenv("XMLLINT_INDENT");
3141 if(indent != NULL) {
3142 xmlTreeIndentString = indent;
3143 }
3144
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003145
Daniel Veillard0bff36d2004-08-31 09:37:03 +00003146 defaultEntityLoader = xmlGetExternalEntityLoader();
3147 xmlSetExternalEntityLoader(xmllintExternalEntityLoader);
3148
Daniel Veillardd9bad132001-07-23 19:39:43 +00003149 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00003150 if (loaddtd != 0)
3151 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
3152 if (dtdattrs)
3153 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003154 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillard4432df22003-09-28 18:58:27 +00003155#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003156 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard4432df22003-09-28 18:58:27 +00003157#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003158 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00003159 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003160 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00003161 xmlGenericError(xmlGenericErrorContext,
3162 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
3163 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003164 "<html><head><title>%s output</title></head>\n",
3165 argv[0]);
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00003166 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003167 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
3168 argv[0]);
3169 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003170
3171#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003172 if ((relaxng != NULL) && (sax == 0)
Daniel Veillard81273902003-09-30 00:43:48 +00003173#ifdef LIBXML_READER_ENABLED
3174 && (stream == 0)
3175#endif /* LIBXML_READER_ENABLED */
3176 ) {
Daniel Veillard71531f32003-02-05 13:19:53 +00003177 xmlRelaxNGParserCtxtPtr ctxt;
3178
Daniel Veillardce192eb2003-04-16 15:58:05 +00003179 /* forces loading the DTDs */
3180 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003181 options |= XML_PARSE_DTDLOAD;
Daniel Veillard42f12e92003-03-07 18:32:59 +00003182 if (timing) {
3183 startTimer();
3184 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003185 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
3186 xmlRelaxNGSetParserErrors(ctxt,
3187 (xmlRelaxNGValidityErrorFunc) fprintf,
3188 (xmlRelaxNGValidityWarningFunc) fprintf,
3189 stderr);
3190 relaxngschemas = xmlRelaxNGParse(ctxt);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003191 if (relaxngschemas == NULL) {
3192 xmlGenericError(xmlGenericErrorContext,
3193 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +00003194 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +00003195 relaxng = NULL;
3196 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003197 xmlRelaxNGFreeParserCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00003198 if (timing) {
3199 endTimer("Compiling the schemas");
3200 }
Daniel Veillardebe25d42004-03-25 09:35:49 +00003201 } else if ((schema != NULL)
3202#ifdef LIBXML_READER_ENABLED
3203 && (stream == 0)
3204#endif
3205 ) {
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003206 xmlSchemaParserCtxtPtr ctxt;
3207
3208 if (timing) {
3209 startTimer();
3210 }
3211 ctxt = xmlSchemaNewParserCtxt(schema);
3212 xmlSchemaSetParserErrors(ctxt,
3213 (xmlSchemaValidityErrorFunc) fprintf,
3214 (xmlSchemaValidityWarningFunc) fprintf,
3215 stderr);
3216 wxschemas = xmlSchemaParse(ctxt);
3217 if (wxschemas == NULL) {
3218 xmlGenericError(xmlGenericErrorContext,
3219 "WXS schema %s failed to compile\n", schema);
William M. Brack8304d872004-06-08 13:29:32 +00003220 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003221 schema = NULL;
3222 }
3223 xmlSchemaFreeParserCtxt(ctxt);
3224 if (timing) {
3225 endTimer("Compiling the schemas");
3226 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003227 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003228#endif /* LIBXML_SCHEMAS_ENABLED */
3229#ifdef LIBXML_PATTERN_ENABLED
Daniel Veillard39e5c892005-07-03 22:48:50 +00003230 if ((pattern != NULL)
Daniel Veillardc9352532005-07-04 14:25:34 +00003231#ifdef LIBXML_READER_ENABLED
Daniel Veillard39e5c892005-07-03 22:48:50 +00003232 && (walker == 0)
3233#endif
3234 ) {
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00003235 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003236 if (patternc == NULL) {
3237 xmlGenericError(xmlGenericErrorContext,
3238 "Pattern %s failed to compile\n", pattern);
William M. Brack8304d872004-06-08 13:29:32 +00003239 progresult = XMLLINT_ERR_SCHEMAPAT;
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003240 pattern = NULL;
3241 }
3242 }
3243#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003244 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00003245 if ((!strcmp(argv[i], "-encode")) ||
3246 (!strcmp(argv[i], "--encode"))) {
3247 i++;
3248 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003249 } else if ((!strcmp(argv[i], "-o")) ||
3250 (!strcmp(argv[i], "-output")) ||
3251 (!strcmp(argv[i], "--output"))) {
3252 i++;
3253 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00003254 }
Daniel Veillard4432df22003-09-28 18:58:27 +00003255#ifdef LIBXML_VALID_ENABLED
Daniel Veillardcd429612000-10-11 15:57:05 +00003256 if ((!strcmp(argv[i], "-dtdvalid")) ||
3257 (!strcmp(argv[i], "--dtdvalid"))) {
3258 i++;
3259 continue;
Daniel Veillard0bff36d2004-08-31 09:37:03 +00003260 }
3261 if ((!strcmp(argv[i], "-path")) ||
3262 (!strcmp(argv[i], "--path"))) {
3263 i++;
3264 continue;
Daniel Veillardcd429612000-10-11 15:57:05 +00003265 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00003266 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
3267 (!strcmp(argv[i], "--dtdvalidfpi"))) {
3268 i++;
3269 continue;
3270 }
Daniel Veillard4432df22003-09-28 18:58:27 +00003271#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00003272 if ((!strcmp(argv[i], "-relaxng")) ||
3273 (!strcmp(argv[i], "--relaxng"))) {
3274 i++;
3275 continue;
3276 }
Daniel Veillard87076042004-05-03 22:54:49 +00003277 if ((!strcmp(argv[i], "-maxmem")) ||
3278 (!strcmp(argv[i], "--maxmem"))) {
3279 i++;
3280 continue;
3281 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003282 if ((!strcmp(argv[i], "-schema")) ||
3283 (!strcmp(argv[i], "--schema"))) {
3284 i++;
3285 continue;
3286 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003287#ifdef LIBXML_PATTERN_ENABLED
3288 if ((!strcmp(argv[i], "-pattern")) ||
3289 (!strcmp(argv[i], "--pattern"))) {
3290 i++;
3291 continue;
3292 }
3293#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00003294 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00003295 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003296 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00003297 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003298 if (repeat) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003299 xmlParserCtxtPtr ctxt = NULL;
3300
3301 for (acount = 0;acount < repeat;acount++) {
Daniel Veillard81273902003-09-30 00:43:48 +00003302#ifdef LIBXML_READER_ENABLED
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003303 if (stream != 0) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00003304 streamFile(argv[i]);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003305 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00003306#endif /* LIBXML_READER_ENABLED */
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003307 if (sax) {
3308 testSAX(argv[i]);
3309 } else {
3310 if (ctxt == NULL)
3311 ctxt = xmlNewParserCtxt();
3312 parseAndPrintFile(argv[i], ctxt);
3313 }
Daniel Veillard81273902003-09-30 00:43:48 +00003314#ifdef LIBXML_READER_ENABLED
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003315 }
Daniel Veillard81273902003-09-30 00:43:48 +00003316#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003317 }
3318 if (ctxt != NULL)
3319 xmlFreeParserCtxt(ctxt);
Daniel Veillard7704fb12003-01-03 16:19:51 +00003320 } else {
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003321 nbregister = 0;
3322
Daniel Veillard81273902003-09-30 00:43:48 +00003323#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00003324 if (stream != 0)
3325 streamFile(argv[i]);
3326 else
Daniel Veillard81273902003-09-30 00:43:48 +00003327#endif /* LIBXML_READER_ENABLED */
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003328 if (sax) {
3329 testSAX(argv[i]);
3330 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003331 parseAndPrintFile(argv[i], NULL);
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003332 }
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003333
3334 if ((chkregister) && (nbregister != 0)) {
3335 fprintf(stderr, "Registration count off: %d\n", nbregister);
William M. Brack8304d872004-06-08 13:29:32 +00003336 progresult = XMLLINT_ERR_RDREGIS;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003337 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00003338 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003339 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00003340 if ((timing) && (repeat)) {
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00003341 endTimer("%d iterations", repeat);
Daniel Veillarda7866932001-12-04 13:14:44 +00003342 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00003343 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003344 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00003345 if (generate)
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003346 parseAndPrintFile(NULL, NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003347 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00003348 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003349 }
Daniel Veillard845cce42002-01-09 11:51:37 +00003350 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003351 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003352 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003353#ifdef LIBXML_SCHEMAS_ENABLED
3354 if (relaxngschemas != NULL)
3355 xmlRelaxNGFree(relaxngschemas);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003356 if (wxschemas != NULL)
3357 xmlSchemaFree(wxschemas);
Daniel Veillard71531f32003-02-05 13:19:53 +00003358 xmlRelaxNGCleanupTypes();
3359#endif
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003360#ifdef LIBXML_PATTERN_ENABLED
3361 if (patternc != NULL)
3362 xmlFreePattern(patternc);
3363#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003364 xmlCleanupParser();
3365 xmlMemoryDump();
3366
Daniel Veillardf7cd4812001-02-23 18:44:52 +00003367 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003368}
Daniel Veillard88a172f2000-08-04 18:23:10 +00003369