blob: 0cbc3d1974401d8d0c2b01c264547f1103441e60 [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; */
Daniel Veillard971771e2005-07-09 17:32:57 +0000841 XML_SAX2_MAGIC,
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000842 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;
Daniel Veillard78dfc9f2005-07-10 22:30:30 +00001610#ifdef LIBXML_SAX1_ENABLED
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001611 } else if (sax1) {
1612 handler = debugSAXHandler;
Daniel Veillard78dfc9f2005-07-10 22:30:30 +00001613#endif
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001614 } else {
1615 handler = debugSAX2Handler;
1616 }
1617
1618 /*
1619 * it's not the simplest code but the most generic in term of I/O
1620 */
1621 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
1622 if (buf == NULL) {
1623 goto error;
1624 }
1625
1626#ifdef LIBXML_SCHEMAS_ENABLED
1627 if (wxschemas != NULL) {
1628 int ret;
1629 xmlSchemaValidCtxtPtr vctxt;
1630
1631 vctxt = xmlSchemaNewValidCtxt(wxschemas);
1632 xmlSchemaSetValidErrors(vctxt,
1633 (xmlSchemaValidityErrorFunc) fprintf,
1634 (xmlSchemaValidityWarningFunc) fprintf,
1635 stderr);
1636
Daniel Veillard971771e2005-07-09 17:32:57 +00001637 ret = xmlSchemaValidateStream(vctxt, buf, 0, handler,
1638 (void *)user_data);
1639 if (repeat == 0) {
1640 if (ret == 0) {
1641 fprintf(stderr, "%s validates\n", filename);
1642 } else if (ret > 0) {
1643 fprintf(stderr, "%s fails to validate\n", filename);
1644 progresult = XMLLINT_ERR_VALID;
1645 } else {
1646 fprintf(stderr, "%s validation generated an internal error\n",
1647 filename);
1648 progresult = XMLLINT_ERR_VALID;
1649 }
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001650 }
1651 xmlSchemaFreeValidCtxt(vctxt);
1652 } else
1653#endif
1654 {
1655 /*
1656 * Create the parser context amd hook the input
1657 */
1658 ctxt = xmlNewParserCtxt();
1659 if (ctxt == NULL) {
1660 xmlFreeParserInputBuffer(buf);
1661 goto error;
1662 }
1663 old_sax = ctxt->sax;
1664 ctxt->sax = handler;
1665 ctxt->userData = (void *) user_data;
1666 inputStream = xmlNewIOInputStream(ctxt, buf, XML_CHAR_ENCODING_NONE);
1667 if (inputStream == NULL) {
1668 xmlFreeParserInputBuffer(buf);
1669 goto error;
1670 }
1671 inputPush(ctxt, inputStream);
1672
1673 /* do the parsing */
1674 xmlParseDocument(ctxt);
1675
1676 if (ctxt->myDoc != NULL) {
1677 fprintf(stderr, "SAX generated a doc !\n");
1678 xmlFreeDoc(ctxt->myDoc);
1679 ctxt->myDoc = NULL;
1680 }
1681 }
1682
1683error:
1684 if (ctxt != NULL) {
1685 ctxt->sax = old_sax;
1686 xmlFreeParserCtxt(ctxt);
1687 }
1688}
1689
Daniel Veillard5e873c42000-04-12 13:27:38 +00001690/************************************************************************
1691 * *
Daniel Veillard7704fb12003-01-03 16:19:51 +00001692 * Stream Test processing *
1693 * *
1694 ************************************************************************/
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001695#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001696static void processNode(xmlTextReaderPtr reader) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001697 const xmlChar *name, *value;
Daniel Veillard16ef8002005-01-31 00:27:50 +00001698 int type, empty;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001699
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001700 type = xmlTextReaderNodeType(reader);
Daniel Veillard16ef8002005-01-31 00:27:50 +00001701 empty = xmlTextReaderIsEmptyElement(reader);
Daniel Veillard99737f52003-03-22 14:55:50 +00001702
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001703 if (debug) {
1704 name = xmlTextReaderConstName(reader);
1705 if (name == NULL)
1706 name = BAD_CAST "--";
Daniel Veillard7704fb12003-01-03 16:19:51 +00001707
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001708 value = xmlTextReaderConstValue(reader);
1709
1710
1711 printf("%d %d %s %d %d",
1712 xmlTextReaderDepth(reader),
1713 type,
1714 name,
Daniel Veillard16ef8002005-01-31 00:27:50 +00001715 empty,
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001716 xmlTextReaderHasValue(reader));
1717 if (value == NULL)
1718 printf("\n");
1719 else {
1720 printf(" %s\n", value);
1721 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00001722 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001723#ifdef LIBXML_PATTERN_ENABLED
1724 if (patternc) {
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001725 xmlChar *path = NULL;
1726 int match = -1;
1727
1728 if (type == XML_READER_TYPE_ELEMENT) {
1729 /* do the check only on element start */
1730 match = xmlPatternMatch(patternc, xmlTextReaderCurrentNode(reader));
1731
1732 if (match) {
1733 path = xmlGetNodePath(xmlTextReaderCurrentNode(reader));
1734 printf("Node %s matches pattern %s\n", path, pattern);
1735 }
1736 }
1737 if (patstream != NULL) {
1738 int ret;
1739
1740 if (type == XML_READER_TYPE_ELEMENT) {
1741 ret = xmlStreamPush(patstream,
1742 xmlTextReaderConstLocalName(reader),
1743 xmlTextReaderConstNamespaceUri(reader));
1744 if (ret < 0) {
1745 fprintf(stderr, "xmlStreamPush() failure\n");
1746 xmlFreeStreamCtxt(patstream);
1747 patstream = NULL;
1748 } else if (ret != match) {
1749 if (path == NULL) {
1750 path = xmlGetNodePath(
1751 xmlTextReaderCurrentNode(reader));
1752 }
1753 fprintf(stderr,
1754 "xmlPatternMatch and xmlStreamPush disagree\n");
1755 fprintf(stderr,
1756 " pattern %s node %s\n",
1757 pattern, path);
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001758 }
1759
1760
Daniel Veillard16ef8002005-01-31 00:27:50 +00001761 }
1762 if ((type == XML_READER_TYPE_END_ELEMENT) ||
1763 ((type == XML_READER_TYPE_ELEMENT) && (empty))) {
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001764 ret = xmlStreamPop(patstream);
1765 if (ret < 0) {
1766 fprintf(stderr, "xmlStreamPop() failure\n");
1767 xmlFreeStreamCtxt(patstream);
1768 patstream = NULL;
1769 }
1770 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001771 }
Daniel Veillardf9d16912005-01-30 22:36:30 +00001772 if (path != NULL)
1773 xmlFree(path);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001774 }
1775#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001776}
1777
1778static void streamFile(char *filename) {
1779 xmlTextReaderPtr reader;
1780 int ret;
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001781#ifdef HAVE_SYS_MMAN_H
1782 int fd = -1;
1783 struct stat info;
1784 const char *base = NULL;
1785 xmlParserInputBufferPtr input = NULL;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001786
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001787 if (memory) {
1788 if (stat(filename, &info) < 0)
1789 return;
1790 if ((fd = open(filename, O_RDONLY)) < 0)
1791 return;
1792 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
1793 if (base == (void *) MAP_FAILED)
1794 return;
1795
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001796 reader = xmlReaderForMemory(base, info.st_size, filename,
1797 NULL, options);
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001798 } else
1799#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001800 reader = xmlReaderForFile(filename, NULL, options);
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001801#ifdef LIBXML_PATTERN_ENABLED
Daniel Veillardd4301ab2005-02-03 22:24:10 +00001802 if (pattern != NULL) {
1803 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
1804 if (patternc == NULL) {
1805 xmlGenericError(xmlGenericErrorContext,
1806 "Pattern %s failed to compile\n", pattern);
1807 progresult = XMLLINT_ERR_SCHEMAPAT;
1808 pattern = NULL;
1809 }
1810 }
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001811 if (patternc != NULL) {
1812 patstream = xmlPatternGetStreamCtxt(patternc);
1813 if (patstream != NULL) {
1814 ret = xmlStreamPush(patstream, NULL, NULL);
1815 if (ret < 0) {
1816 fprintf(stderr, "xmlStreamPush() failure\n");
1817 xmlFreeStreamCtxt(patstream);
1818 patstream = NULL;
1819 }
1820 }
1821 }
1822#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001823
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001824
Daniel Veillard7704fb12003-01-03 16:19:51 +00001825 if (reader != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001826#ifdef LIBXML_VALID_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001827 if (valid)
1828 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00001829 else
Daniel Veillard4432df22003-09-28 18:58:27 +00001830#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce192eb2003-04-16 15:58:05 +00001831 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001832#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +00001833 if (relaxng != NULL) {
Daniel Veillard81514ba2003-09-16 23:17:26 +00001834 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00001835 startTimer();
1836 }
1837 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
1838 if (ret < 0) {
1839 xmlGenericError(xmlGenericErrorContext,
1840 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +00001841 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +00001842 relaxng = NULL;
1843 }
Daniel Veillard81514ba2003-09-16 23:17:26 +00001844 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00001845 endTimer("Compiling the schemas");
1846 }
1847 }
Daniel Veillardf10ae122005-07-10 19:03:16 +00001848 if (schema != NULL) {
1849 if ((timing) && (!repeat)) {
1850 startTimer();
1851 }
1852 ret = xmlTextReaderSchemaValidate(reader, schema);
1853 if (ret < 0) {
1854 xmlGenericError(xmlGenericErrorContext,
1855 "XSD schema %s failed to compile\n", schema);
1856 progresult = XMLLINT_ERR_SCHEMACOMP;
1857 schema = NULL;
1858 }
1859 if ((timing) && (!repeat)) {
1860 endTimer("Compiling the schemas");
1861 }
1862 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001863#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001864
1865 /*
1866 * Process all nodes in sequence
1867 */
Daniel Veillard81514ba2003-09-16 23:17:26 +00001868 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00001869 startTimer();
1870 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00001871 ret = xmlTextReaderRead(reader);
1872 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001873 if ((debug)
1874#ifdef LIBXML_PATTERN_ENABLED
1875 || (patternc)
1876#endif
1877 )
Daniel Veillard7704fb12003-01-03 16:19:51 +00001878 processNode(reader);
1879 ret = xmlTextReaderRead(reader);
1880 }
Daniel Veillard81514ba2003-09-16 23:17:26 +00001881 if ((timing) && (!repeat)) {
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001882#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf54cd532004-02-25 11:52:31 +00001883 if (relaxng != NULL)
Daniel Veillard49138f12004-02-19 12:58:36 +00001884 endTimer("Parsing and validating");
1885 else
Daniel Veillardf54cd532004-02-25 11:52:31 +00001886#endif
Daniel Veillard4432df22003-09-28 18:58:27 +00001887#ifdef LIBXML_VALID_ENABLED
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001888 if (valid)
Daniel Veillardce192eb2003-04-16 15:58:05 +00001889 endTimer("Parsing and validating");
1890 else
Daniel Veillard4432df22003-09-28 18:58:27 +00001891#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +00001892 endTimer("Parsing");
Daniel Veillardce192eb2003-04-16 15:58:05 +00001893 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00001894
Daniel Veillard4432df22003-09-28 18:58:27 +00001895#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf6bad792003-04-11 19:38:54 +00001896 if (valid) {
1897 if (xmlTextReaderIsValid(reader) != 1) {
1898 xmlGenericError(xmlGenericErrorContext,
1899 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001900 progresult = XMLLINT_ERR_VALID;
Daniel Veillardf6bad792003-04-11 19:38:54 +00001901 }
1902 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001903#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001904#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf10ae122005-07-10 19:03:16 +00001905 if ((relaxng != NULL) || (schema != NULL)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00001906 if (xmlTextReaderIsValid(reader) != 1) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001907 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001908 progresult = XMLLINT_ERR_VALID;
Daniel Veillardf4e55762003-04-15 23:32:22 +00001909 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001910 fprintf(stderr, "%s validates\n", filename);
Daniel Veillardf4e55762003-04-15 23:32:22 +00001911 }
1912 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001913#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001914 /*
1915 * Done, cleanup and status
1916 */
1917 xmlFreeTextReader(reader);
1918 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001919 fprintf(stderr, "%s : failed to parse\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001920 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001921 }
1922 } else {
1923 fprintf(stderr, "Unable to open %s\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001924 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001925 }
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001926#ifdef LIBXML_PATTERN_ENABLED
1927 if (patstream != NULL) {
1928 xmlFreeStreamCtxt(patstream);
1929 patstream = NULL;
1930 }
1931#endif
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001932#ifdef HAVE_SYS_MMAN_H
1933 if (memory) {
1934 xmlFreeParserInputBuffer(input);
1935 munmap((char *) base, info.st_size);
1936 close(fd);
1937 }
1938#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001939}
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001940
1941static void walkDoc(xmlDocPtr doc) {
1942 xmlTextReaderPtr reader;
1943 int ret;
1944
Daniel Veillardd4301ab2005-02-03 22:24:10 +00001945#ifdef LIBXML_PATTERN_ENABLED
1946 xmlNodePtr root;
1947 const xmlChar *namespaces[22];
1948 int i;
1949 xmlNsPtr ns;
1950
1951 root = xmlDocGetRootElement(doc);
1952 for (ns = root->nsDef, i = 0;ns != NULL && i < 20;ns=ns->next) {
1953 namespaces[i++] = ns->href;
1954 namespaces[i++] = ns->prefix;
1955 }
1956 namespaces[i++] = NULL;
1957 namespaces[i++] = NULL;
1958
1959 if (pattern != NULL) {
1960 patternc = xmlPatterncompile((const xmlChar *) pattern, doc->dict,
1961 0, &namespaces[0]);
1962 if (patternc == NULL) {
1963 xmlGenericError(xmlGenericErrorContext,
1964 "Pattern %s failed to compile\n", pattern);
1965 progresult = XMLLINT_ERR_SCHEMAPAT;
1966 pattern = NULL;
1967 }
1968 }
Daniel Veillard2b2e02d2005-02-05 23:20:22 +00001969 if (patternc != NULL) {
1970 patstream = xmlPatternGetStreamCtxt(patternc);
1971 if (patstream != NULL) {
1972 ret = xmlStreamPush(patstream, NULL, NULL);
1973 if (ret < 0) {
1974 fprintf(stderr, "xmlStreamPush() failure\n");
1975 xmlFreeStreamCtxt(patstream);
1976 patstream = NULL;
1977 }
1978 }
1979 }
Daniel Veillardd4301ab2005-02-03 22:24:10 +00001980#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001981 reader = xmlReaderWalker(doc);
1982 if (reader != NULL) {
1983 if ((timing) && (!repeat)) {
1984 startTimer();
1985 }
1986 ret = xmlTextReaderRead(reader);
1987 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001988 if ((debug)
1989#ifdef LIBXML_PATTERN_ENABLED
1990 || (patternc)
1991#endif
1992 )
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001993 processNode(reader);
1994 ret = xmlTextReaderRead(reader);
1995 }
1996 if ((timing) && (!repeat)) {
1997 endTimer("walking through the doc");
1998 }
1999 xmlFreeTextReader(reader);
2000 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002001 fprintf(stderr, "failed to walk through the doc\n");
William M. Brack8304d872004-06-08 13:29:32 +00002002 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002003 }
2004 } else {
2005 fprintf(stderr, "Failed to crate a reader from the document\n");
William M. Brack8304d872004-06-08 13:29:32 +00002006 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002007 }
Daniel Veillard2b2e02d2005-02-05 23:20:22 +00002008#ifdef LIBXML_PATTERN_ENABLED
2009 if (patstream != NULL) {
2010 xmlFreeStreamCtxt(patstream);
2011 patstream = NULL;
2012 }
2013#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002014}
Daniel Veillard81273902003-09-30 00:43:48 +00002015#endif /* LIBXML_READER_ENABLED */
Daniel Veillard7704fb12003-01-03 16:19:51 +00002016
2017/************************************************************************
2018 * *
2019 * Tree Test processing *
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002020 * *
2021 ************************************************************************/
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002022static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
Daniel Veillard652327a2003-09-29 18:02:38 +00002023 xmlDocPtr doc = NULL;
2024#ifdef LIBXML_TREE_ENABLED
2025 xmlDocPtr tmp;
2026#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002027
Daniel Veillard48b2f892001-02-25 16:11:03 +00002028 if ((timing) && (!repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00002029 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002030
2031
Daniel Veillard652327a2003-09-29 18:02:38 +00002032#ifdef LIBXML_TREE_ENABLED
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002033 if (filename == NULL) {
2034 if (generate) {
2035 xmlNodePtr n;
2036
2037 doc = xmlNewDoc(BAD_CAST "1.0");
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002038 n = xmlNewDocNode(doc, NULL, BAD_CAST "info", NULL);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002039 xmlNodeSetContent(n, BAD_CAST "abc");
2040 xmlDocSetRootElement(doc, n);
2041 }
2042 }
Daniel Veillard652327a2003-09-29 18:02:38 +00002043#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002044#ifdef LIBXML_HTML_ENABLED
Daniel Veillard73b013f2003-09-30 12:36:01 +00002045#ifdef LIBXML_PUSH_ENABLED
William M. Brack78637da2003-07-31 14:47:38 +00002046 else if ((html) && (push)) {
2047 FILE *f;
2048
William M. Brack3403add2004-06-27 02:07:51 +00002049#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2050 f = fopen(filename, "rb");
2051#else
2052 f = fopen(filename, "r");
2053#endif
William M. Brack78637da2003-07-31 14:47:38 +00002054 if (f != NULL) {
2055 int res, size = 3;
2056 char chars[4096];
2057 htmlParserCtxtPtr ctxt;
2058
2059 /* if (repeat) */
2060 size = 4096;
2061 res = fread(chars, 1, 4, f);
2062 if (res > 0) {
2063 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
William M. Brack1d75c8a2003-10-27 13:48:16 +00002064 chars, res, filename, XML_CHAR_ENCODING_NONE);
William M. Brack78637da2003-07-31 14:47:38 +00002065 while ((res = fread(chars, 1, size, f)) > 0) {
2066 htmlParseChunk(ctxt, chars, res, 0);
2067 }
2068 htmlParseChunk(ctxt, chars, 0, 1);
2069 doc = ctxt->myDoc;
2070 htmlFreeParserCtxt(ctxt);
2071 }
2072 fclose(f);
2073 }
2074 }
Daniel Veillard73b013f2003-09-30 12:36:01 +00002075#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002076 else if (html) {
Daniel Veillard9475a352003-09-26 12:47:50 +00002077 doc = htmlReadFile(filename, NULL, options);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002078 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002079#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002080 else {
Daniel Veillard73b013f2003-09-30 12:36:01 +00002081#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002082 /*
2083 * build an XML tree from a string;
2084 */
2085 if (push) {
2086 FILE *f;
2087
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002088 /* '-' Usually means stdin -<sven@zen.org> */
2089 if ((filename[0] == '-') && (filename[1] == 0)) {
2090 f = stdin;
2091 } else {
William M. Brack3403add2004-06-27 02:07:51 +00002092#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2093 f = fopen(filename, "rb");
2094#else
2095 f = fopen(filename, "r");
2096#endif
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002097 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002098 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +00002099 int ret;
Daniel Veillarda880b122003-04-21 21:36:41 +00002100 int res, size = 1024;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002101 char chars[1024];
2102 xmlParserCtxtPtr ctxt;
2103
Daniel Veillarda880b122003-04-21 21:36:41 +00002104 /* if (repeat) size = 1024; */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002105 res = fread(chars, 1, 4, f);
2106 if (res > 0) {
2107 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
2108 chars, res, filename);
Daniel Veillard500a1de2004-03-22 15:22:58 +00002109 xmlCtxtUseOptions(ctxt, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002110 while ((res = fread(chars, 1, size, f)) > 0) {
2111 xmlParseChunk(ctxt, chars, res, 0);
2112 }
2113 xmlParseChunk(ctxt, chars, 0, 1);
2114 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +00002115 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002116 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +00002117 if (!ret) {
2118 xmlFreeDoc(doc);
2119 doc = NULL;
2120 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002121 }
2122 }
Daniel Veillard73b013f2003-09-30 12:36:01 +00002123 } else
2124#endif /* LIBXML_PUSH_ENABLED */
2125 if (testIO) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002126 if ((filename[0] == '-') && (filename[1] == 0)) {
Daniel Veillard60942de2003-09-25 21:05:58 +00002127 doc = xmlReadFd(0, NULL, NULL, options);
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002128 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002129 FILE *f;
Daniel Veillard5e873c42000-04-12 13:27:38 +00002130
William M. Brack3403add2004-06-27 02:07:51 +00002131#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2132 f = fopen(filename, "rb");
2133#else
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002134 f = fopen(filename, "r");
William M. Brack3403add2004-06-27 02:07:51 +00002135#endif
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002136 if (f != NULL) {
2137 if (rectxt == NULL)
2138 doc = xmlReadIO((xmlInputReadCallback) myRead,
2139 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +00002140 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002141 else
2142 doc = xmlCtxtReadIO(rectxt,
2143 (xmlInputReadCallback) myRead,
2144 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +00002145 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002146 } else
Daniel Veillard5e873c42000-04-12 13:27:38 +00002147 doc = NULL;
Daniel Veillard5e873c42000-04-12 13:27:38 +00002148 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002149 } else if (htmlout) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002150 xmlParserCtxtPtr ctxt;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002151
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002152 if (rectxt == NULL)
2153 ctxt = xmlNewParserCtxt();
2154 else
2155 ctxt = rectxt;
Daniel Veillard88a172f2000-08-04 18:23:10 +00002156 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002157 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +00002158 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002159 ctxt->sax->error = xmlHTMLError;
2160 ctxt->sax->warning = xmlHTMLWarning;
2161 ctxt->vctxt.error = xmlHTMLValidityError;
2162 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002163
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002164 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002165
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002166 if (rectxt == NULL)
2167 xmlFreeParserCtxt(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002168 }
Daniel Veillard46e370e2000-07-21 20:32:03 +00002169#ifdef HAVE_SYS_MMAN_H
2170 } else if (memory) {
2171 int fd;
2172 struct stat info;
2173 const char *base;
2174 if (stat(filename, &info) < 0)
2175 return;
2176 if ((fd = open(filename, O_RDONLY)) < 0)
2177 return;
2178 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +00002179 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +00002180 return;
2181
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002182 if (rectxt == NULL)
Daniel Veillard60942de2003-09-25 21:05:58 +00002183 doc = xmlReadMemory((char *) base, info.st_size,
2184 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002185 else
Daniel Veillard60942de2003-09-25 21:05:58 +00002186 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
2187 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002188
Daniel Veillard46e370e2000-07-21 20:32:03 +00002189 munmap((char *) base, info.st_size);
2190#endif
Daniel Veillard4432df22003-09-28 18:58:27 +00002191#ifdef LIBXML_VALID_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00002192 } else if (valid) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002193 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00002194
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002195 if (rectxt == NULL)
2196 ctxt = xmlNewParserCtxt();
2197 else
2198 ctxt = rectxt;
Daniel Veillardea7751d2002-12-20 00:16:24 +00002199 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002200 doc = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00002201 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002202 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
2203
2204 if (ctxt->valid == 0)
William M. Brack8304d872004-06-08 13:29:32 +00002205 progresult = XMLLINT_ERR_RDFILE;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002206 if (rectxt == NULL)
2207 xmlFreeParserCtxt(ctxt);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002208 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002209#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00002210 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002211 if (rectxt != NULL)
2212 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
Daniel Veillard81562d22005-06-15 13:27:56 +00002213 else {
2214#ifdef LIBXML_SAX1_ENABLED
2215 if (sax1)
2216 doc = xmlParseFile(filename);
2217 else
2218#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002219 doc = xmlReadFile(filename, NULL, options);
Daniel Veillard81562d22005-06-15 13:27:56 +00002220 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00002221 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002222 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002223
Daniel Veillard88a172f2000-08-04 18:23:10 +00002224 /*
2225 * If we don't have a document we might as well give up. Do we
2226 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002227 if (doc == NULL) {
William M. Brack8304d872004-06-08 13:29:32 +00002228 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard88a172f2000-08-04 18:23:10 +00002229 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002230 }
2231
Daniel Veillard48b2f892001-02-25 16:11:03 +00002232 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002233 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002234 }
2235
Daniel Veillard29e43992001-12-13 22:21:58 +00002236 /*
2237 * Remove DOCTYPE nodes
2238 */
2239 if (dropdtd) {
2240 xmlDtdPtr dtd;
2241
2242 dtd = xmlGetIntSubset(doc);
2243 if (dtd != NULL) {
2244 xmlUnlinkNode((xmlNodePtr)dtd);
2245 xmlFreeDtd(dtd);
2246 }
2247 }
2248
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002249#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +00002250 if (xinclude) {
2251 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002252 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002253 }
William M. Brack4e1c2db2005-02-11 10:58:55 +00002254 if (xmlXIncludeProcessFlags(doc, options) < 0)
2255 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard48b2f892001-02-25 16:11:03 +00002256 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002257 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002258 }
2259 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002260#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +00002261
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002262#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00002263#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002264 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002265 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002266 */
2267 if (shell)
2268 xmlShell(doc, filename, xmlShellReadline, stdout);
2269#endif
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00002270#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002271
Daniel Veillard652327a2003-09-29 18:02:38 +00002272#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002273 /*
2274 * test intermediate copy if needed.
2275 */
2276 if (copy) {
2277 tmp = doc;
Daniel Veillard4edd3ed2004-09-20 20:03:01 +00002278 if (timing) {
2279 startTimer();
2280 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002281 doc = xmlCopyDoc(doc, 1);
Daniel Veillard4edd3ed2004-09-20 20:03:01 +00002282 if (timing) {
2283 endTimer("Copying");
2284 }
2285 if (timing) {
2286 startTimer();
2287 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002288 xmlFreeDoc(tmp);
Daniel Veillard4edd3ed2004-09-20 20:03:01 +00002289 if (timing) {
2290 endTimer("Freeing original");
2291 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002292 }
Daniel Veillard652327a2003-09-29 18:02:38 +00002293#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002294
Daniel Veillard4432df22003-09-28 18:58:27 +00002295#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002296 if ((insert) && (!html)) {
2297 const xmlChar* list[256];
2298 int nb, i;
2299 xmlNodePtr node;
2300
2301 if (doc->children != NULL) {
2302 node = doc->children;
2303 while ((node != NULL) && (node->last == NULL)) node = node->next;
2304 if (node != NULL) {
2305 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
2306 if (nb < 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002307 fprintf(stderr, "could not get valid list of elements\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002308 } else if (nb == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002309 fprintf(stderr, "No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002310 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002311 fprintf(stderr, "%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002312 nb);
2313 for (i = 0;i < nb;i++) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002314 fprintf(stderr, "%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002315 }
2316 }
2317 }
2318 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002319 }else
2320#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002321#ifdef LIBXML_READER_ENABLED
2322 if (walker) {
2323 walkDoc(doc);
2324 }
2325#endif /* LIBXML_READER_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002326#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00002327 if (noout == 0) {
Daniel Veillard3df01182003-12-10 10:17:51 +00002328 int ret;
2329
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002330 /*
2331 * print it.
2332 */
2333#ifdef LIBXML_DEBUG_ENABLED
2334 if (!debug) {
2335#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00002336 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002337 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002338 }
Daniel Veillard656ce942004-04-30 23:11:45 +00002339#ifdef LIBXML_HTML_ENABLED
Daniel Veillard42fd4122003-11-04 08:47:48 +00002340 if ((html) && (!xmlout)) {
2341 if (compress) {
2342 htmlSaveFile(output ? output : "-", doc);
2343 }
2344 else if (encoding != NULL) {
2345 if ( format ) {
2346 htmlSaveFileFormat(output ? output : "-", doc, encoding, 1);
2347 }
2348 else {
2349 htmlSaveFileFormat(output ? output : "-", doc, encoding, 0);
2350 }
2351 }
2352 else if (format) {
2353 htmlSaveFileFormat(output ? output : "-", doc, NULL, 1);
2354 }
2355 else {
2356 FILE *out;
2357 if (output == NULL)
2358 out = stdout;
2359 else {
2360 out = fopen(output,"wb");
2361 }
2362 if (out != NULL) {
2363 if (htmlDocDump(out, doc) < 0)
William M. Brack8304d872004-06-08 13:29:32 +00002364 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00002365
2366 if (output != NULL)
2367 fclose(out);
2368 } else {
2369 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00002370 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00002371 }
2372 }
2373 if ((timing) && (!repeat)) {
2374 endTimer("Saving");
2375 }
2376 } else
2377#endif
Daniel Veillard25048d82004-08-14 22:37:54 +00002378#ifdef LIBXML_C14N_ENABLED
2379 if (canonical) {
2380 xmlChar *result = NULL;
2381 int size;
2382
2383 size = xmlC14NDocDumpMemory(doc, NULL, 0, NULL, 1, &result);
2384 if (size >= 0) {
2385 write(1, result, size);
2386 xmlFree(result);
2387 } else {
2388 fprintf(stderr, "Failed to canonicalize\n");
2389 progresult = XMLLINT_ERR_OUT;
2390 }
2391 } else
Aleksey Sanin2650df12005-06-06 17:16:50 +00002392 if (exc_canonical) {
2393 xmlChar *result = NULL;
2394 int size;
2395
2396 size = xmlC14NDocDumpMemory(doc, NULL, 1, NULL, 1, &result);
2397 if (size >= 0) {
2398 write(1, result, size);
2399 xmlFree(result);
2400 } else {
2401 fprintf(stderr, "Failed to canonicalize\n");
2402 progresult = XMLLINT_ERR_OUT;
2403 }
2404 } else
Daniel Veillard25048d82004-08-14 22:37:54 +00002405#endif
Daniel Veillard3b2c2612001-04-04 00:09:00 +00002406#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002407 if (memory) {
2408 xmlChar *result;
2409 int len;
2410
2411 if (encoding != NULL) {
Daniel Veillardd536f702001-11-08 17:32:47 +00002412 if ( format ) {
2413 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
2414 } else {
2415 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
2416 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002417 } else {
Daniel Veillard90493a92001-08-14 14:12:47 +00002418 if (format)
2419 xmlDocDumpFormatMemory(doc, &result, &len, 1);
2420 else
2421 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002422 }
2423 if (result == NULL) {
2424 fprintf(stderr, "Failed to save\n");
Daniel Veillard25048d82004-08-14 22:37:54 +00002425 progresult = XMLLINT_ERR_OUT;
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002426 } else {
2427 write(1, result, len);
2428 xmlFree(result);
2429 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +00002430 } else
2431#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002432 if (compress) {
2433 xmlSaveFile(output ? output : "-", doc);
2434 }
Daniel Veillardd536f702001-11-08 17:32:47 +00002435 else if (encoding != NULL) {
2436 if ( format ) {
Daniel Veillard3df01182003-12-10 10:17:51 +00002437 ret = xmlSaveFormatFileEnc(output ? output : "-", doc,
2438 encoding, 1);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002439 }
Daniel Veillardd536f702001-11-08 17:32:47 +00002440 else {
Daniel Veillard3df01182003-12-10 10:17:51 +00002441 ret = xmlSaveFileEnc(output ? output : "-", doc, encoding);
2442 }
2443 if (ret < 0) {
2444 fprintf(stderr, "failed save to %s\n",
2445 output ? output : "-");
William M. Brack8304d872004-06-08 13:29:32 +00002446 progresult = XMLLINT_ERR_OUT;
Daniel Veillardd536f702001-11-08 17:32:47 +00002447 }
2448 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002449 else if (format) {
Daniel Veillard3df01182003-12-10 10:17:51 +00002450 ret = xmlSaveFormatFile(output ? output : "-", doc, 1);
2451 if (ret < 0) {
2452 fprintf(stderr, "failed save to %s\n",
2453 output ? output : "-");
William M. Brack8304d872004-06-08 13:29:32 +00002454 progresult = XMLLINT_ERR_OUT;
Daniel Veillard3df01182003-12-10 10:17:51 +00002455 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002456 }
2457 else {
2458 FILE *out;
2459 if (output == NULL)
2460 out = stdout;
2461 else {
2462 out = fopen(output,"wb");
2463 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00002464 if (out != NULL) {
Daniel Veillard828ce832003-10-08 19:19:10 +00002465 if (xmlDocDump(out, doc) < 0)
William M. Brack8304d872004-06-08 13:29:32 +00002466 progresult = XMLLINT_ERR_OUT;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002467
Daniel Veillard05d987b2003-10-08 11:54:57 +00002468 if (output != NULL)
2469 fclose(out);
2470 } else {
2471 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00002472 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00002473 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002474 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002475 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002476 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002477 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002478#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002479 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002480 FILE *out;
2481 if (output == NULL)
2482 out = stdout;
2483 else {
2484 out = fopen(output,"wb");
2485 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00002486 if (out != NULL) {
2487 xmlDebugDumpDocument(out, doc);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002488
Daniel Veillard05d987b2003-10-08 11:54:57 +00002489 if (output != NULL)
2490 fclose(out);
2491 } else {
2492 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00002493 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00002494 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002495 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002496#endif
2497 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002498#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002499
Daniel Veillard4432df22003-09-28 18:58:27 +00002500#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002501 /*
2502 * A posteriori validation test
2503 */
Daniel Veillard66f68e72003-08-18 16:39:51 +00002504 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
Daniel Veillardcd429612000-10-11 15:57:05 +00002505 xmlDtdPtr dtd;
2506
Daniel Veillard48b2f892001-02-25 16:11:03 +00002507 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002508 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002509 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00002510 if (dtdvalid != NULL)
2511 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
2512 else
2513 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002514 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002515 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002516 }
Daniel Veillardcd429612000-10-11 15:57:05 +00002517 if (dtd == NULL) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00002518 if (dtdvalid != NULL)
2519 xmlGenericError(xmlGenericErrorContext,
2520 "Could not parse DTD %s\n", dtdvalid);
2521 else
2522 xmlGenericError(xmlGenericErrorContext,
2523 "Could not parse DTD %s\n", dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00002524 progresult = XMLLINT_ERR_DTD;
Daniel Veillardcd429612000-10-11 15:57:05 +00002525 } else {
Daniel Veillarda37aab82003-06-09 09:10:36 +00002526 xmlValidCtxtPtr cvp;
2527
2528 if ((cvp = xmlNewValidCtxt()) == NULL) {
2529 xmlGenericError(xmlGenericErrorContext,
2530 "Couldn't allocate validation context\n");
2531 exit(-1);
2532 }
2533 cvp->userData = (void *) stderr;
2534 cvp->error = (xmlValidityErrorFunc) fprintf;
2535 cvp->warning = (xmlValidityWarningFunc) fprintf;
2536
Daniel Veillard48b2f892001-02-25 16:11:03 +00002537 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002538 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002539 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002540 if (!xmlValidateDtd(cvp, doc, dtd)) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00002541 if (dtdvalid != NULL)
2542 xmlGenericError(xmlGenericErrorContext,
2543 "Document %s does not validate against %s\n",
2544 filename, dtdvalid);
2545 else
2546 xmlGenericError(xmlGenericErrorContext,
2547 "Document %s does not validate against %s\n",
2548 filename, dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00002549 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00002550 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002551 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002552 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002553 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002554 xmlFreeValidCtxt(cvp);
Daniel Veillardcd429612000-10-11 15:57:05 +00002555 xmlFreeDtd(dtd);
2556 }
2557 } else if (postvalid) {
Daniel Veillarda37aab82003-06-09 09:10:36 +00002558 xmlValidCtxtPtr cvp;
2559
2560 if ((cvp = xmlNewValidCtxt()) == NULL) {
2561 xmlGenericError(xmlGenericErrorContext,
2562 "Couldn't allocate validation context\n");
2563 exit(-1);
2564 }
2565
Daniel Veillard48b2f892001-02-25 16:11:03 +00002566 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002567 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002568 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002569 cvp->userData = (void *) stderr;
2570 cvp->error = (xmlValidityErrorFunc) fprintf;
2571 cvp->warning = (xmlValidityWarningFunc) fprintf;
2572 if (!xmlValidateDocument(cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00002573 xmlGenericError(xmlGenericErrorContext,
2574 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00002575 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00002576 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002577 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002578 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002579 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002580 xmlFreeValidCtxt(cvp);
Daniel Veillard4432df22003-09-28 18:58:27 +00002581 }
2582#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00002583#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00002584 if (relaxngschemas != NULL) {
Daniel Veillard71531f32003-02-05 13:19:53 +00002585 xmlRelaxNGValidCtxtPtr ctxt;
2586 int ret;
2587
Daniel Veillard42f12e92003-03-07 18:32:59 +00002588 if ((timing) && (!repeat)) {
2589 startTimer();
2590 }
2591
Daniel Veillard71531f32003-02-05 13:19:53 +00002592 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2593 xmlRelaxNGSetValidErrors(ctxt,
2594 (xmlRelaxNGValidityErrorFunc) fprintf,
2595 (xmlRelaxNGValidityWarningFunc) fprintf,
2596 stderr);
2597 ret = xmlRelaxNGValidateDoc(ctxt, doc);
2598 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002599 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard71531f32003-02-05 13:19:53 +00002600 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002601 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00002602 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00002603 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002604 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard71531f32003-02-05 13:19:53 +00002605 filename);
William M. Brack8304d872004-06-08 13:29:32 +00002606 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00002607 }
2608 xmlRelaxNGFreeValidCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002609 if ((timing) && (!repeat)) {
2610 endTimer("Validating");
2611 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002612 } else if (wxschemas != NULL) {
2613 xmlSchemaValidCtxtPtr ctxt;
2614 int ret;
2615
2616 if ((timing) && (!repeat)) {
2617 startTimer();
2618 }
2619
2620 ctxt = xmlSchemaNewValidCtxt(wxschemas);
2621 xmlSchemaSetValidErrors(ctxt,
2622 (xmlSchemaValidityErrorFunc) fprintf,
2623 (xmlSchemaValidityWarningFunc) fprintf,
2624 stderr);
2625 ret = xmlSchemaValidateDoc(ctxt, doc);
2626 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002627 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002628 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002629 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00002630 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002631 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002632 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002633 filename);
William M. Brack8304d872004-06-08 13:29:32 +00002634 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002635 }
2636 xmlSchemaFreeValidCtxt(ctxt);
2637 if ((timing) && (!repeat)) {
2638 endTimer("Validating");
2639 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002640 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002641#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002642
2643#ifdef LIBXML_DEBUG_ENABLED
2644 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00002645 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002646#endif
2647
2648 /*
2649 * free it.
2650 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00002651 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002652 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002653 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002654 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002655 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002656 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002657 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002658}
2659
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002660/************************************************************************
2661 * *
2662 * Usage and Main *
2663 * *
2664 ************************************************************************/
2665
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00002666static void showVersion(const char *name) {
2667 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
2668 fprintf(stderr, " compiled with: ");
Daniel Veillard4432df22003-09-28 18:58:27 +00002669#ifdef LIBXML_VALID_ENABLED
2670 fprintf(stderr, "DTDValid ");
2671#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00002672#ifdef LIBXML_FTP_ENABLED
2673 fprintf(stderr, "FTP ");
2674#endif
2675#ifdef LIBXML_HTTP_ENABLED
2676 fprintf(stderr, "HTTP ");
2677#endif
2678#ifdef LIBXML_HTML_ENABLED
2679 fprintf(stderr, "HTML ");
2680#endif
2681#ifdef LIBXML_C14N_ENABLED
2682 fprintf(stderr, "C14N ");
2683#endif
2684#ifdef LIBXML_CATALOG_ENABLED
2685 fprintf(stderr, "Catalog ");
2686#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00002687#ifdef LIBXML_XPATH_ENABLED
2688 fprintf(stderr, "XPath ");
2689#endif
2690#ifdef LIBXML_XPTR_ENABLED
2691 fprintf(stderr, "XPointer ");
2692#endif
2693#ifdef LIBXML_XINCLUDE_ENABLED
2694 fprintf(stderr, "XInclude ");
2695#endif
2696#ifdef LIBXML_ICONV_ENABLED
2697 fprintf(stderr, "Iconv ");
2698#endif
2699#ifdef DEBUG_MEMORY_LOCATION
2700 fprintf(stderr, "MemDebug ");
2701#endif
2702#ifdef LIBXML_UNICODE_ENABLED
2703 fprintf(stderr, "Unicode ");
2704#endif
2705#ifdef LIBXML_REGEXP_ENABLED
2706 fprintf(stderr, "Regexps ");
2707#endif
2708#ifdef LIBXML_AUTOMATA_ENABLED
2709 fprintf(stderr, "Automata ");
2710#endif
2711#ifdef LIBXML_SCHEMAS_ENABLED
2712 fprintf(stderr, "Schemas ");
2713#endif
Daniel Veillardce1648b2005-01-04 15:10:22 +00002714#ifdef LIBXML_MODULES_ENABLED
2715 fprintf(stderr, "Modules ");
2716#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00002717 fprintf(stderr, "\n");
2718}
2719
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002720static void usage(const char *name) {
2721 printf("Usage : %s [options] XMLfiles ...\n", name);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002722#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002723 printf("\tParse the XML files and output the result of the parsing\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002724#else
2725 printf("\tParse the XML files\n");
2726#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002727 printf("\t--version : display the version of the XML library used\n");
2728#ifdef LIBXML_DEBUG_ENABLED
2729 printf("\t--debug : dump a debug tree of the in-memory document\n");
2730 printf("\t--shell : run a navigating shell\n");
2731 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00002732#else
Daniel Veillard81273902003-09-30 00:43:48 +00002733#ifdef LIBXML_READER_ENABLED
Daniel Veillard8326e732003-01-07 00:19:07 +00002734 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard81273902003-09-30 00:43:48 +00002735#endif /* LIBXML_READER_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002736#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00002737#ifdef LIBXML_TREE_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002738 printf("\t--copy : used to test the internal copy implementation\n");
Daniel Veillard652327a2003-09-29 18:02:38 +00002739#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002740 printf("\t--recover : output what was parsable on broken XML documents\n");
2741 printf("\t--noent : substitute entity references by their value\n");
2742 printf("\t--noout : don't output the result tree\n");
Daniel Veillard0bff36d2004-08-31 09:37:03 +00002743 printf("\t--path 'paths': provide a set of paths for resources\n");
2744 printf("\t--load-trace : print trace of all external entites loaded\n");
Daniel Veillarde8b09e42003-05-13 22:14:13 +00002745 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002746 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00002747 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00002748#ifdef LIBXML_VALID_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002749 printf("\t--valid : validate the document in addition to std well-formed check\n");
2750 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
2751 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
Daniel Veillard66f68e72003-08-18 16:39:51 +00002752 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00002753#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002754 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002755 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002756 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
2757 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002758#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002759#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002760 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002761#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002762#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002763#ifdef LIBXML_HTML_ENABLED
2764 printf("\t--html : use the HTML parser\n");
Daniel Veillard42fd4122003-11-04 08:47:48 +00002765 printf("\t--xmlout : force to use the XML serializer when using --html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002766#endif
Daniel Veillard73b013f2003-09-30 12:36:01 +00002767#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002768 printf("\t--push : use the push mode of the parser\n");
Daniel Veillard73b013f2003-09-30 12:36:01 +00002769#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002770#ifdef HAVE_SYS_MMAN_H
2771 printf("\t--memory : parse from memory\n");
2772#endif
Daniel Veillard87076042004-05-03 22:54:49 +00002773 printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002774 printf("\t--nowarning : do not emit warnings from parser/validator\n");
2775 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillarddca8cc72003-09-26 13:53:14 +00002776 printf("\t--nocdata : replace cdata section with text nodes\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002777#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00002778 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002779 printf("\t--encode encoding : output in the given encoding\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002780 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
2781#endif /* LIBXML_OUTPUT_ENABLED */
Aleksey Sanin2650df12005-06-06 17:16:50 +00002782 printf("\t--c14n : save in W3C canonical format (with comments)\n");
2783 printf("\t--exc-c14n : save in W3C exclusive canonical format (with comments)\n");
Daniel Veillard25048d82004-08-14 22:37:54 +00002784#ifdef LIBXML_C14N_ENABLED
2785#endif /* LIBXML_C14N_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002786 printf("\t--nsclean : remove redundant namespace declarations\n");
2787 printf("\t--testIO : test user I/O support\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002788#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00002789 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
2790 printf("\t otherwise XML Catalogs starting from \n");
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002791 printf("\t %s are activated by default\n", XML_XML_DEFAULT_CATALOG);
Daniel Veillard05c13a22001-09-09 08:38:09 +00002792 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002793#endif
2794 printf("\t--auto : generate a small doc on the fly\n");
2795#ifdef LIBXML_XINCLUDE_ENABLED
2796 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00002797 printf("\t--noxincludenode : same but do not generate XInclude nodes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002798#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002799 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00002800 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard81273902003-09-30 00:43:48 +00002801#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00002802 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002803 printf("\t--walker : create a reader and walk though the resulting doc\n");
Daniel Veillard81273902003-09-30 00:43:48 +00002804#endif /* LIBXML_READER_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002805#ifdef LIBXML_PATTERN_ENABLED
2806 printf("\t--pattern pattern_value : test the pattern support\n");
2807#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002808 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillardef4d3bc2003-02-07 12:38:22 +00002809#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard71531f32003-02-05 13:19:53 +00002810 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002811 printf("\t--schema schema : do validation against the WXS schema\n");
Daniel Veillard71531f32003-02-05 13:19:53 +00002812#endif
Daniel Veillard971771e2005-07-09 17:32:57 +00002813#ifdef LIBXML_SAX1_ENABLED
2814 printf("\t--sax1: use the old SAX1 interfaces for processing\n");
2815#endif
2816 printf("\t--sax: do not build a tree but work just at the SAX level\n");
2817
Daniel Veillarda42f25f2002-01-25 14:15:40 +00002818 printf("\nLibxml project home page: http://xmlsoft.org/\n");
2819 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002820}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002821
2822static void registerNode(xmlNodePtr node)
2823{
2824 node->_private = malloc(sizeof(long));
2825 *(long*)node->_private = (long) 0x81726354;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002826 nbregister++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002827}
2828
2829static void deregisterNode(xmlNodePtr node)
2830{
2831 assert(node->_private != NULL);
2832 assert(*(long*)node->_private == (long) 0x81726354);
2833 free(node->_private);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002834 nbregister--;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002835}
2836
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002837int
2838main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00002839 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002840 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00002841 int version = 0;
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00002842 const char* indent;
2843
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002844 if (argc <= 1) {
2845 usage(argv[0]);
2846 return(1);
2847 }
Daniel Veillardbe803962000-06-28 23:40:59 +00002848 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002849 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002850 if (!strcmp(argv[i], "-"))
2851 break;
2852
2853 if (argv[i][0] != '-')
2854 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002855 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
2856 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00002857 else
2858#ifdef LIBXML_DEBUG_ENABLED
2859 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002860 (!strcmp(argv[i], "--shell"))) {
2861 shell++;
2862 noout = 1;
2863 } else
2864#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00002865#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002866 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
2867 copy++;
Daniel Veillard652327a2003-09-29 18:02:38 +00002868 else
2869#endif /* LIBXML_TREE_ENABLED */
2870 if ((!strcmp(argv[i], "-recover")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002871 (!strcmp(argv[i], "--recover"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002872 recovery++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002873 options |= XML_PARSE_RECOVER;
2874 } else if ((!strcmp(argv[i], "-noent")) ||
2875 (!strcmp(argv[i], "--noent"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002876 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002877 options |= XML_PARSE_NOENT;
Daniel Veillarddca8cc72003-09-26 13:53:14 +00002878 } else if ((!strcmp(argv[i], "-nsclean")) ||
2879 (!strcmp(argv[i], "--nsclean"))) {
2880 options |= XML_PARSE_NSCLEAN;
2881 } else if ((!strcmp(argv[i], "-nocdata")) ||
2882 (!strcmp(argv[i], "--nocdata"))) {
2883 options |= XML_PARSE_NOCDATA;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002884 } else if ((!strcmp(argv[i], "-nodict")) ||
2885 (!strcmp(argv[i], "--nodict"))) {
2886 options |= XML_PARSE_NODICT;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002887 } else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00002888 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00002889 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00002890 version = 1;
2891 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002892 (!strcmp(argv[i], "--noout")))
2893 noout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002894#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002895 else if ((!strcmp(argv[i], "-o")) ||
2896 (!strcmp(argv[i], "-output")) ||
2897 (!strcmp(argv[i], "--output"))) {
2898 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00002899 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002900 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002901#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002902 else if ((!strcmp(argv[i], "-htmlout")) ||
2903 (!strcmp(argv[i], "--htmlout")))
2904 htmlout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002905 else if ((!strcmp(argv[i], "-nowrap")) ||
2906 (!strcmp(argv[i], "--nowrap")))
2907 nowrap++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002908#ifdef LIBXML_HTML_ENABLED
2909 else if ((!strcmp(argv[i], "-html")) ||
2910 (!strcmp(argv[i], "--html"))) {
2911 html++;
2912 }
Daniel Veillard42fd4122003-11-04 08:47:48 +00002913 else if ((!strcmp(argv[i], "-xmlout")) ||
2914 (!strcmp(argv[i], "--xmlout"))) {
2915 xmlout++;
2916 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002917#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002918 else if ((!strcmp(argv[i], "-loaddtd")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002919 (!strcmp(argv[i], "--loaddtd"))) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002920 loaddtd++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002921 options |= XML_PARSE_DTDLOAD;
2922 } else if ((!strcmp(argv[i], "-dtdattr")) ||
Daniel Veillard48da9102001-08-07 01:10:10 +00002923 (!strcmp(argv[i], "--dtdattr"))) {
2924 loaddtd++;
2925 dtdattrs++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002926 options |= XML_PARSE_DTDATTR;
Daniel Veillard4432df22003-09-28 18:58:27 +00002927 }
2928#ifdef LIBXML_VALID_ENABLED
2929 else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002930 (!strcmp(argv[i], "--valid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002931 valid++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002932 options |= XML_PARSE_DTDVALID;
2933 } else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002934 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002935 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002936 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00002937 options |= XML_PARSE_DTDLOAD;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002938 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00002939 (!strcmp(argv[i], "--dtdvalid"))) {
2940 i++;
2941 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002942 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00002943 options |= XML_PARSE_DTDLOAD;
Daniel Veillard66f68e72003-08-18 16:39:51 +00002944 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
2945 (!strcmp(argv[i], "--dtdvalidfpi"))) {
2946 i++;
2947 dtdvalidfpi = argv[i];
2948 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00002949 options |= XML_PARSE_DTDLOAD;
Daniel Veillardcd429612000-10-11 15:57:05 +00002950 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002951#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard29e43992001-12-13 22:21:58 +00002952 else if ((!strcmp(argv[i], "-dropdtd")) ||
2953 (!strcmp(argv[i], "--dropdtd")))
2954 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002955 else if ((!strcmp(argv[i], "-insert")) ||
2956 (!strcmp(argv[i], "--insert")))
2957 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00002958 else if ((!strcmp(argv[i], "-timing")) ||
2959 (!strcmp(argv[i], "--timing")))
2960 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002961 else if ((!strcmp(argv[i], "-auto")) ||
2962 (!strcmp(argv[i], "--auto")))
2963 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002964 else if ((!strcmp(argv[i], "-repeat")) ||
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00002965 (!strcmp(argv[i], "--repeat"))) {
2966 if (repeat)
2967 repeat *= 10;
2968 else
2969 repeat = 100;
Daniel Veillard73b013f2003-09-30 12:36:01 +00002970 }
2971#ifdef LIBXML_PUSH_ENABLED
2972 else if ((!strcmp(argv[i], "-push")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002973 (!strcmp(argv[i], "--push")))
2974 push++;
Daniel Veillard73b013f2003-09-30 12:36:01 +00002975#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +00002976#ifdef HAVE_SYS_MMAN_H
2977 else if ((!strcmp(argv[i], "-memory")) ||
2978 (!strcmp(argv[i], "--memory")))
2979 memory++;
2980#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00002981 else if ((!strcmp(argv[i], "-testIO")) ||
2982 (!strcmp(argv[i], "--testIO")))
2983 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002984#ifdef LIBXML_XINCLUDE_ENABLED
2985 else if ((!strcmp(argv[i], "-xinclude")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002986 (!strcmp(argv[i], "--xinclude"))) {
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002987 xinclude++;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002988 options |= XML_PARSE_XINCLUDE;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002989 }
Daniel Veillardc14c3892004-08-16 12:34:50 +00002990 else if ((!strcmp(argv[i], "-noxincludenode")) ||
2991 (!strcmp(argv[i], "--noxincludenode"))) {
2992 xinclude++;
2993 options |= XML_PARSE_XINCLUDE;
2994 options |= XML_PARSE_NOXINCNODE;
2995 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002996#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002997#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002998#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002999 else if ((!strcmp(argv[i], "-compress")) ||
3000 (!strcmp(argv[i], "--compress"))) {
3001 compress++;
3002 xmlSetCompressMode(9);
3003 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003004#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003005#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003006 else if ((!strcmp(argv[i], "-nowarning")) ||
3007 (!strcmp(argv[i], "--nowarning"))) {
3008 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003009 xmlPedanticParserDefault(0);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003010 options |= XML_PARSE_NOWARNING;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003011 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003012 else if ((!strcmp(argv[i], "-pedantic")) ||
3013 (!strcmp(argv[i], "--pedantic"))) {
3014 xmlGetWarningsDefaultValue = 1;
3015 xmlPedanticParserDefault(1);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003016 options |= XML_PARSE_PEDANTIC;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003017 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00003018#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003019 else if ((!strcmp(argv[i], "-debugent")) ||
3020 (!strcmp(argv[i], "--debugent"))) {
3021 debugent++;
3022 xmlParserDebugEntities = 1;
3023 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00003024#endif
Daniel Veillard25048d82004-08-14 22:37:54 +00003025#ifdef LIBXML_C14N_ENABLED
3026 else if ((!strcmp(argv[i], "-c14n")) ||
3027 (!strcmp(argv[i], "--c14n"))) {
3028 canonical++;
3029 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
3030 }
Aleksey Sanin2650df12005-06-06 17:16:50 +00003031 else if ((!strcmp(argv[i], "-exc-c14n")) ||
3032 (!strcmp(argv[i], "--exc-c14n"))) {
3033 exc_canonical++;
3034 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
3035 }
Daniel Veillard25048d82004-08-14 22:37:54 +00003036#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00003037#ifdef LIBXML_CATALOG_ENABLED
3038 else if ((!strcmp(argv[i], "-catalogs")) ||
3039 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003040 catalogs++;
3041 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
3042 (!strcmp(argv[i], "--nocatalogs"))) {
3043 nocatalogs++;
Daniel Veillard81418e32001-05-22 15:08:55 +00003044 }
3045#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00003046 else if ((!strcmp(argv[i], "-encode")) ||
3047 (!strcmp(argv[i], "--encode"))) {
3048 i++;
3049 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003050 /*
3051 * OK it's for testing purposes
3052 */
3053 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00003054 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003055 else if ((!strcmp(argv[i], "-noblanks")) ||
3056 (!strcmp(argv[i], "--noblanks"))) {
3057 noblanks++;
3058 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00003059 }
Daniel Veillard87076042004-05-03 22:54:49 +00003060 else if ((!strcmp(argv[i], "-maxmem")) ||
3061 (!strcmp(argv[i], "--maxmem"))) {
3062 i++;
3063 if (sscanf(argv[i], "%d", &maxmem) == 1) {
3064 xmlMemSetup(myFreeFunc, myMallocFunc, myReallocFunc,
3065 myStrdupFunc);
3066 } else {
3067 maxmem = 0;
3068 }
3069 }
Daniel Veillard90493a92001-08-14 14:12:47 +00003070 else if ((!strcmp(argv[i], "-format")) ||
3071 (!strcmp(argv[i], "--format"))) {
3072 noblanks++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003073#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00003074 format++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003075#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard90493a92001-08-14 14:12:47 +00003076 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00003077 }
Daniel Veillard81273902003-09-30 00:43:48 +00003078#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00003079 else if ((!strcmp(argv[i], "-stream")) ||
3080 (!strcmp(argv[i], "--stream"))) {
3081 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003082 }
Daniel Veillard7899c5c2003-11-03 12:31:38 +00003083 else if ((!strcmp(argv[i], "-walker")) ||
3084 (!strcmp(argv[i], "--walker"))) {
3085 walker++;
3086 noout++;
3087 }
Daniel Veillard81273902003-09-30 00:43:48 +00003088#endif /* LIBXML_READER_ENABLED */
3089#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00003090 else if ((!strcmp(argv[i], "-sax1")) ||
3091 (!strcmp(argv[i], "--sax1"))) {
3092 sax1++;
3093 }
Daniel Veillard81273902003-09-30 00:43:48 +00003094#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003095 else if ((!strcmp(argv[i], "-sax")) ||
3096 (!strcmp(argv[i], "--sax"))) {
3097 sax++;
3098 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003099 else if ((!strcmp(argv[i], "-chkregister")) ||
3100 (!strcmp(argv[i], "--chkregister"))) {
3101 chkregister++;
Daniel Veillard71531f32003-02-05 13:19:53 +00003102#ifdef LIBXML_SCHEMAS_ENABLED
3103 } else if ((!strcmp(argv[i], "-relaxng")) ||
3104 (!strcmp(argv[i], "--relaxng"))) {
3105 i++;
3106 relaxng = argv[i];
3107 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003108 options |= XML_PARSE_NOENT;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003109 } else if ((!strcmp(argv[i], "-schema")) ||
3110 (!strcmp(argv[i], "--schema"))) {
3111 i++;
3112 schema = argv[i];
3113 noent++;
Daniel Veillard71531f32003-02-05 13:19:53 +00003114#endif
Daniel Veillarde8b09e42003-05-13 22:14:13 +00003115 } else if ((!strcmp(argv[i], "-nonet")) ||
3116 (!strcmp(argv[i], "--nonet"))) {
Daniel Veillard61b93382003-11-03 14:28:31 +00003117 options |= XML_PARSE_NONET;
Daniel Veillard0bff36d2004-08-31 09:37:03 +00003118 } else if ((!strcmp(argv[i], "-load-trace")) ||
3119 (!strcmp(argv[i], "--load-trace"))) {
3120 load_trace++;
3121 } else if ((!strcmp(argv[i], "-path")) ||
3122 (!strcmp(argv[i], "--path"))) {
3123 i++;
3124 parsePath(BAD_CAST argv[i]);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003125#ifdef LIBXML_PATTERN_ENABLED
3126 } else if ((!strcmp(argv[i], "-pattern")) ||
3127 (!strcmp(argv[i], "--pattern"))) {
3128 i++;
3129 pattern = argv[i];
3130#endif
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003131 } else {
3132 fprintf(stderr, "Unknown option %s\n", argv[i]);
3133 usage(argv[0]);
3134 return(1);
3135 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003136 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003137
3138#ifdef LIBXML_CATALOG_ENABLED
3139 if (nocatalogs == 0) {
3140 if (catalogs) {
3141 const char *catal;
3142
3143 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003144 if (catal != NULL) {
3145 xmlLoadCatalogs(catal);
3146 } else {
3147 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
3148 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003149 }
3150 }
3151#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003152
Daniel Veillard81273902003-09-30 00:43:48 +00003153#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00003154 if (sax1)
3155 xmlSAXDefaultVersion(1);
3156 else
3157 xmlSAXDefaultVersion(2);
Daniel Veillard81273902003-09-30 00:43:48 +00003158#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00003159
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003160 if (chkregister) {
3161 xmlRegisterNodeDefault(registerNode);
3162 xmlDeregisterNodeDefault(deregisterNode);
3163 }
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00003164
3165 indent = getenv("XMLLINT_INDENT");
3166 if(indent != NULL) {
3167 xmlTreeIndentString = indent;
3168 }
3169
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003170
Daniel Veillard0bff36d2004-08-31 09:37:03 +00003171 defaultEntityLoader = xmlGetExternalEntityLoader();
3172 xmlSetExternalEntityLoader(xmllintExternalEntityLoader);
3173
Daniel Veillardd9bad132001-07-23 19:39:43 +00003174 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00003175 if (loaddtd != 0)
3176 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
3177 if (dtdattrs)
3178 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003179 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillard4432df22003-09-28 18:58:27 +00003180#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003181 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard4432df22003-09-28 18:58:27 +00003182#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003183 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00003184 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003185 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00003186 xmlGenericError(xmlGenericErrorContext,
3187 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
3188 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003189 "<html><head><title>%s output</title></head>\n",
3190 argv[0]);
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00003191 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003192 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
3193 argv[0]);
3194 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003195
3196#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003197 if ((relaxng != NULL) && (sax == 0)
Daniel Veillard81273902003-09-30 00:43:48 +00003198#ifdef LIBXML_READER_ENABLED
3199 && (stream == 0)
3200#endif /* LIBXML_READER_ENABLED */
3201 ) {
Daniel Veillard71531f32003-02-05 13:19:53 +00003202 xmlRelaxNGParserCtxtPtr ctxt;
3203
Daniel Veillardce192eb2003-04-16 15:58:05 +00003204 /* forces loading the DTDs */
3205 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003206 options |= XML_PARSE_DTDLOAD;
Daniel Veillard42f12e92003-03-07 18:32:59 +00003207 if (timing) {
3208 startTimer();
3209 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003210 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
3211 xmlRelaxNGSetParserErrors(ctxt,
3212 (xmlRelaxNGValidityErrorFunc) fprintf,
3213 (xmlRelaxNGValidityWarningFunc) fprintf,
3214 stderr);
3215 relaxngschemas = xmlRelaxNGParse(ctxt);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003216 if (relaxngschemas == NULL) {
3217 xmlGenericError(xmlGenericErrorContext,
3218 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +00003219 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +00003220 relaxng = NULL;
3221 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003222 xmlRelaxNGFreeParserCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00003223 if (timing) {
3224 endTimer("Compiling the schemas");
3225 }
Daniel Veillardebe25d42004-03-25 09:35:49 +00003226 } else if ((schema != NULL)
3227#ifdef LIBXML_READER_ENABLED
Daniel Veillardf10ae122005-07-10 19:03:16 +00003228 && (stream == 0)
Daniel Veillardebe25d42004-03-25 09:35:49 +00003229#endif
3230 ) {
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003231 xmlSchemaParserCtxtPtr ctxt;
3232
3233 if (timing) {
3234 startTimer();
3235 }
3236 ctxt = xmlSchemaNewParserCtxt(schema);
3237 xmlSchemaSetParserErrors(ctxt,
3238 (xmlSchemaValidityErrorFunc) fprintf,
3239 (xmlSchemaValidityWarningFunc) fprintf,
3240 stderr);
3241 wxschemas = xmlSchemaParse(ctxt);
3242 if (wxschemas == NULL) {
3243 xmlGenericError(xmlGenericErrorContext,
3244 "WXS schema %s failed to compile\n", schema);
William M. Brack8304d872004-06-08 13:29:32 +00003245 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003246 schema = NULL;
3247 }
3248 xmlSchemaFreeParserCtxt(ctxt);
3249 if (timing) {
3250 endTimer("Compiling the schemas");
3251 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003252 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003253#endif /* LIBXML_SCHEMAS_ENABLED */
3254#ifdef LIBXML_PATTERN_ENABLED
Daniel Veillard39e5c892005-07-03 22:48:50 +00003255 if ((pattern != NULL)
Daniel Veillardc9352532005-07-04 14:25:34 +00003256#ifdef LIBXML_READER_ENABLED
Daniel Veillard39e5c892005-07-03 22:48:50 +00003257 && (walker == 0)
3258#endif
3259 ) {
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00003260 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003261 if (patternc == NULL) {
3262 xmlGenericError(xmlGenericErrorContext,
3263 "Pattern %s failed to compile\n", pattern);
William M. Brack8304d872004-06-08 13:29:32 +00003264 progresult = XMLLINT_ERR_SCHEMAPAT;
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003265 pattern = NULL;
3266 }
3267 }
3268#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003269 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00003270 if ((!strcmp(argv[i], "-encode")) ||
3271 (!strcmp(argv[i], "--encode"))) {
3272 i++;
3273 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003274 } else if ((!strcmp(argv[i], "-o")) ||
3275 (!strcmp(argv[i], "-output")) ||
3276 (!strcmp(argv[i], "--output"))) {
3277 i++;
3278 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00003279 }
Daniel Veillard4432df22003-09-28 18:58:27 +00003280#ifdef LIBXML_VALID_ENABLED
Daniel Veillardcd429612000-10-11 15:57:05 +00003281 if ((!strcmp(argv[i], "-dtdvalid")) ||
3282 (!strcmp(argv[i], "--dtdvalid"))) {
3283 i++;
3284 continue;
Daniel Veillard0bff36d2004-08-31 09:37:03 +00003285 }
3286 if ((!strcmp(argv[i], "-path")) ||
3287 (!strcmp(argv[i], "--path"))) {
3288 i++;
3289 continue;
Daniel Veillardcd429612000-10-11 15:57:05 +00003290 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00003291 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
3292 (!strcmp(argv[i], "--dtdvalidfpi"))) {
3293 i++;
3294 continue;
3295 }
Daniel Veillard4432df22003-09-28 18:58:27 +00003296#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00003297 if ((!strcmp(argv[i], "-relaxng")) ||
3298 (!strcmp(argv[i], "--relaxng"))) {
3299 i++;
3300 continue;
3301 }
Daniel Veillard87076042004-05-03 22:54:49 +00003302 if ((!strcmp(argv[i], "-maxmem")) ||
3303 (!strcmp(argv[i], "--maxmem"))) {
3304 i++;
3305 continue;
3306 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003307 if ((!strcmp(argv[i], "-schema")) ||
3308 (!strcmp(argv[i], "--schema"))) {
3309 i++;
3310 continue;
3311 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003312#ifdef LIBXML_PATTERN_ENABLED
3313 if ((!strcmp(argv[i], "-pattern")) ||
3314 (!strcmp(argv[i], "--pattern"))) {
3315 i++;
3316 continue;
3317 }
3318#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00003319 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00003320 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003321 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00003322 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003323 if (repeat) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003324 xmlParserCtxtPtr ctxt = NULL;
3325
3326 for (acount = 0;acount < repeat;acount++) {
Daniel Veillard81273902003-09-30 00:43:48 +00003327#ifdef LIBXML_READER_ENABLED
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003328 if (stream != 0) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00003329 streamFile(argv[i]);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003330 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00003331#endif /* LIBXML_READER_ENABLED */
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003332 if (sax) {
3333 testSAX(argv[i]);
3334 } else {
3335 if (ctxt == NULL)
3336 ctxt = xmlNewParserCtxt();
3337 parseAndPrintFile(argv[i], ctxt);
3338 }
Daniel Veillard81273902003-09-30 00:43:48 +00003339#ifdef LIBXML_READER_ENABLED
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003340 }
Daniel Veillard81273902003-09-30 00:43:48 +00003341#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003342 }
3343 if (ctxt != NULL)
3344 xmlFreeParserCtxt(ctxt);
Daniel Veillard7704fb12003-01-03 16:19:51 +00003345 } else {
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003346 nbregister = 0;
3347
Daniel Veillard81273902003-09-30 00:43:48 +00003348#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00003349 if (stream != 0)
3350 streamFile(argv[i]);
3351 else
Daniel Veillard81273902003-09-30 00:43:48 +00003352#endif /* LIBXML_READER_ENABLED */
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003353 if (sax) {
3354 testSAX(argv[i]);
3355 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003356 parseAndPrintFile(argv[i], NULL);
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003357 }
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003358
3359 if ((chkregister) && (nbregister != 0)) {
3360 fprintf(stderr, "Registration count off: %d\n", nbregister);
William M. Brack8304d872004-06-08 13:29:32 +00003361 progresult = XMLLINT_ERR_RDREGIS;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003362 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00003363 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003364 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00003365 if ((timing) && (repeat)) {
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00003366 endTimer("%d iterations", repeat);
Daniel Veillarda7866932001-12-04 13:14:44 +00003367 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00003368 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003369 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00003370 if (generate)
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003371 parseAndPrintFile(NULL, NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003372 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00003373 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003374 }
Daniel Veillard845cce42002-01-09 11:51:37 +00003375 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003376 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003377 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003378#ifdef LIBXML_SCHEMAS_ENABLED
3379 if (relaxngschemas != NULL)
3380 xmlRelaxNGFree(relaxngschemas);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003381 if (wxschemas != NULL)
3382 xmlSchemaFree(wxschemas);
Daniel Veillard71531f32003-02-05 13:19:53 +00003383 xmlRelaxNGCleanupTypes();
3384#endif
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003385#ifdef LIBXML_PATTERN_ENABLED
3386 if (patternc != NULL)
3387 xmlFreePattern(patternc);
3388#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003389 xmlCleanupParser();
3390 xmlMemoryDump();
3391
Daniel Veillardf7cd4812001-02-23 18:44:52 +00003392 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003393}
Daniel Veillard88a172f2000-08-04 18:23:10 +00003394