blob: 9b0b89d0989d3e49c5f1ed2296a17128fa6c9db0 [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 Veillardd4501d72005-07-24 14:27:16 +000088#ifdef LIBXML_SCHEMATRON_ENABLED
89#include <libxml/schematron.h>
90#endif
Daniel Veillard71531f32003-02-05 13:19:53 +000091#ifdef LIBXML_SCHEMAS_ENABLED
92#include <libxml/relaxng.h>
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000093#include <libxml/xmlschemas.h>
Daniel Veillard71531f32003-02-05 13:19:53 +000094#endif
Daniel Veillardb3de70c2003-12-02 22:32:15 +000095#ifdef LIBXML_PATTERN_ENABLED
96#include <libxml/pattern.h>
97#endif
Daniel Veillard6ebf3c42004-08-22 13:11:39 +000098#ifdef LIBXML_C14N_ENABLED
99#include <libxml/c14n.h>
100#endif
Daniel Veillarddab39b52006-10-16 23:22:10 +0000101#ifdef LIBXML_OUTPUT_ENABLED
102#include <libxml/xmlsave.h>
103#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000104
Daniel Veillard3be27512003-01-26 19:49:04 +0000105#ifndef XML_XML_DEFAULT_CATALOG
106#define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog"
107#endif
108
William M. Brack8304d872004-06-08 13:29:32 +0000109typedef enum {
110 XMLLINT_RETURN_OK = 0, /* No error */
111 XMLLINT_ERR_UNCLASS, /* Unclassified */
112 XMLLINT_ERR_DTD, /* Error in DTD */
113 XMLLINT_ERR_VALID, /* Validation error */
114 XMLLINT_ERR_RDFILE, /* CtxtReadFile error */
115 XMLLINT_ERR_SCHEMACOMP, /* Schema compilation */
116 XMLLINT_ERR_OUT, /* Error writing output */
117 XMLLINT_ERR_SCHEMAPAT, /* Error in schema pattern */
118 XMLLINT_ERR_RDREGIS, /* Error in Reader registration */
119 XMLLINT_ERR_MEM /* Out of memory error */
120} xmllintReturnCode;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000121#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000122static int shell = 0;
123static int debugent = 0;
124#endif
Daniel Veillard8326e732003-01-07 00:19:07 +0000125static int debug = 0;
Daniel Veillard87076042004-05-03 22:54:49 +0000126static int maxmem = 0;
Daniel Veillard652327a2003-09-29 18:02:38 +0000127#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000128static int copy = 0;
Daniel Veillard652327a2003-09-29 18:02:38 +0000129#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000130static int recovery = 0;
131static int noent = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000132static int noblanks = 0;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000133static int noout = 0;
134static int nowrap = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000135#ifdef LIBXML_OUTPUT_ENABLED
136static int format = 0;
137static const char *output = NULL;
138static int compress = 0;
Daniel Veillarddab39b52006-10-16 23:22:10 +0000139static int oldout = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000140#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4432df22003-09-28 18:58:27 +0000141#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000142static int valid = 0;
143static int postvalid = 0;
Daniel Veillardcd429612000-10-11 15:57:05 +0000144static char * dtdvalid = NULL;
Daniel Veillard66f68e72003-08-18 16:39:51 +0000145static char * dtdvalidfpi = NULL;
Daniel Veillard4432df22003-09-28 18:58:27 +0000146#endif
Daniel Veillard71531f32003-02-05 13:19:53 +0000147#ifdef LIBXML_SCHEMAS_ENABLED
148static char * relaxng = NULL;
149static xmlRelaxNGPtr relaxngschemas = NULL;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +0000150static char * schema = NULL;
151static xmlSchemaPtr wxschemas = NULL;
Daniel Veillard71531f32003-02-05 13:19:53 +0000152#endif
Daniel Veillard8c6e6532005-09-08 21:39:47 +0000153#ifdef LIBXML_SCHEMATRON_ENABLED
Daniel Veillardd4501d72005-07-24 14:27:16 +0000154static char * schematron = NULL;
155static xmlSchematronPtr wxschematron = NULL;
156#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000157static int repeat = 0;
158static int insert = 0;
Daniel Veillard656ce942004-04-30 23:11:45 +0000159#if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000160static int html = 0;
Daniel Veillard42fd4122003-11-04 08:47:48 +0000161static int xmlout = 0;
Daniel Veillard4432df22003-09-28 18:58:27 +0000162#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000163static int htmlout = 0;
Daniel Veillard73b013f2003-09-30 12:36:01 +0000164#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000165static int push = 0;
Daniel Veillard73b013f2003-09-30 12:36:01 +0000166#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +0000167#ifdef HAVE_SYS_MMAN_H
168static int memory = 0;
169#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +0000170static int testIO = 0;
Daniel Veillardbe803962000-06-28 23:40:59 +0000171static char *encoding = NULL;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000172#ifdef LIBXML_XINCLUDE_ENABLED
173static int xinclude = 0;
174#endif
Daniel Veillard48da9102001-08-07 01:10:10 +0000175static int dtdattrs = 0;
Daniel Veillard10ea86c2001-06-20 13:55:33 +0000176static int loaddtd = 0;
William M. Brack8304d872004-06-08 13:29:32 +0000177static xmllintReturnCode progresult = XMLLINT_RETURN_OK;
Daniel Veillard48b2f892001-02-25 16:11:03 +0000178static int timing = 0;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000179static int generate = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000180static int dropdtd = 0;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000181#ifdef LIBXML_CATALOG_ENABLED
182static int catalogs = 0;
183static int nocatalogs = 0;
184#endif
Daniel Veillard25048d82004-08-14 22:37:54 +0000185#ifdef LIBXML_C14N_ENABLED
186static int canonical = 0;
Aleksey Sanin83868242009-07-09 10:26:22 +0200187static int canonical_11 = 0;
Aleksey Sanin2650df12005-06-06 17:16:50 +0000188static int exc_canonical = 0;
Daniel Veillard25048d82004-08-14 22:37:54 +0000189#endif
Daniel Veillard81273902003-09-30 00:43:48 +0000190#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000191static int stream = 0;
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000192static int walker = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000193#endif /* LIBXML_READER_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000194static int chkregister = 0;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +0000195static int nbregister = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000196#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +0000197static int sax1 = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000198#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000199#ifdef LIBXML_PATTERN_ENABLED
200static const char *pattern = NULL;
201static xmlPatternPtr patternc = NULL;
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000202static xmlStreamCtxtPtr patstream = NULL;
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000203#endif
Daniel Veillard8874b942005-08-25 13:19:21 +0000204static int options = XML_PARSE_COMPACT;
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000205static int sax = 0;
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000206static int oldxml10 = 0;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000207
Daniel Veillard87076042004-05-03 22:54:49 +0000208/************************************************************************
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000209 * *
210 * Entity loading control and customization. *
211 * *
212 ************************************************************************/
213#define MAX_PATHS 64
Daniel Veillarded121382007-04-17 12:33:19 +0000214#ifdef _WIN32
215# define PATH_SEPARATOR ';'
216#else
217# define PATH_SEPARATOR ':'
218#endif
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000219static xmlChar *paths[MAX_PATHS + 1];
220static int nbpaths = 0;
221static int load_trace = 0;
222
223static
224void parsePath(const xmlChar *path) {
225 const xmlChar *cur;
226
227 if (path == NULL)
228 return;
229 while (*path != 0) {
230 if (nbpaths >= MAX_PATHS) {
231 fprintf(stderr, "MAX_PATHS reached: too many paths\n");
232 return;
233 }
234 cur = path;
Daniel Veillarded121382007-04-17 12:33:19 +0000235 while ((*cur == ' ') || (*cur == PATH_SEPARATOR))
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000236 cur++;
237 path = cur;
Daniel Veillarded121382007-04-17 12:33:19 +0000238 while ((*cur != 0) && (*cur != ' ') && (*cur != PATH_SEPARATOR))
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000239 cur++;
240 if (cur != path) {
241 paths[nbpaths] = xmlStrndup(path, cur - path);
242 if (paths[nbpaths] != NULL)
243 nbpaths++;
244 path = cur;
245 }
246 }
247}
248
Daniel Veillard24505b02005-07-28 23:49:35 +0000249static xmlExternalEntityLoader defaultEntityLoader = NULL;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000250
Daniel Veillardf1edb102009-08-10 14:43:18 +0200251static xmlParserInputPtr
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000252xmllintExternalEntityLoader(const char *URL, const char *ID,
253 xmlParserCtxtPtr ctxt) {
254 xmlParserInputPtr ret;
255 warningSAXFunc warning = NULL;
Daniel Veillardea71f5d2006-02-19 16:55:55 +0000256 errorSAXFunc err = NULL;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000257
258 int i;
259 const char *lastsegment = URL;
260 const char *iter = URL;
261
Daniel Veillard5608b172008-01-11 06:53:15 +0000262 if ((nbpaths > 0) && (iter != NULL)) {
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000263 while (*iter != 0) {
264 if (*iter == '/')
265 lastsegment = iter + 1;
266 iter++;
267 }
268 }
269
270 if ((ctxt != NULL) && (ctxt->sax != NULL)) {
271 warning = ctxt->sax->warning;
Daniel Veillardea71f5d2006-02-19 16:55:55 +0000272 err = ctxt->sax->error;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000273 ctxt->sax->warning = NULL;
Daniel Veillardea71f5d2006-02-19 16:55:55 +0000274 ctxt->sax->error = NULL;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000275 }
276
277 if (defaultEntityLoader != NULL) {
278 ret = defaultEntityLoader(URL, ID, ctxt);
279 if (ret != NULL) {
280 if (warning != NULL)
281 ctxt->sax->warning = warning;
Daniel Veillardea71f5d2006-02-19 16:55:55 +0000282 if (err != NULL)
283 ctxt->sax->error = err;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000284 if (load_trace) {
285 fprintf \
286 (stderr,
287 "Loaded URL=\"%s\" ID=\"%s\"\n",
288 URL ? URL : "(null)",
289 ID ? ID : "(null)");
290 }
291 return(ret);
292 }
293 }
294 for (i = 0;i < nbpaths;i++) {
295 xmlChar *newURL;
296
297 newURL = xmlStrdup((const xmlChar *) paths[i]);
298 newURL = xmlStrcat(newURL, (const xmlChar *) "/");
299 newURL = xmlStrcat(newURL, (const xmlChar *) lastsegment);
300 if (newURL != NULL) {
301 ret = defaultEntityLoader((const char *)newURL, ID, ctxt);
302 if (ret != NULL) {
303 if (warning != NULL)
304 ctxt->sax->warning = warning;
Daniel Veillardea71f5d2006-02-19 16:55:55 +0000305 if (err != NULL)
306 ctxt->sax->error = err;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000307 if (load_trace) {
308 fprintf \
Daniel Veillardf1edb102009-08-10 14:43:18 +0200309 (stderr,
310 "Loaded URL=\"%s\" ID=\"%s\"\n",
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000311 newURL,
Daniel Veillardf1edb102009-08-10 14:43:18 +0200312 ID ? ID : "(null)");
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000313 }
314 xmlFree(newURL);
315 return(ret);
316 }
317 xmlFree(newURL);
318 }
319 }
Daniel Veillardea71f5d2006-02-19 16:55:55 +0000320 if (err != NULL)
321 ctxt->sax->error = err;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000322 if (warning != NULL) {
323 ctxt->sax->warning = warning;
324 if (URL != NULL)
325 warning(ctxt, "failed to load external entity \"%s\"\n", URL);
326 else if (ID != NULL)
327 warning(ctxt, "failed to load external entity \"%s\"\n", ID);
328 }
329 return(NULL);
330}
331/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +0200332 * *
Daniel Veillard87076042004-05-03 22:54:49 +0000333 * Memory allocation consumption debugging *
Daniel Veillardf1edb102009-08-10 14:43:18 +0200334 * *
Daniel Veillard87076042004-05-03 22:54:49 +0000335 ************************************************************************/
336
Daniel Veillard3af3b592004-05-05 19:22:30 +0000337static void
338OOM(void)
339{
Daniel Veillard87076042004-05-03 22:54:49 +0000340 fprintf(stderr, "Ran out of memory needs > %d bytes\n", maxmem);
William M. Brack8304d872004-06-08 13:29:32 +0000341 progresult = XMLLINT_ERR_MEM;
Daniel Veillard87076042004-05-03 22:54:49 +0000342}
343
Daniel Veillard3af3b592004-05-05 19:22:30 +0000344static void
345myFreeFunc(void *mem)
346{
Daniel Veillard87076042004-05-03 22:54:49 +0000347 xmlMemFree(mem);
348}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000349static void *
350myMallocFunc(size_t size)
351{
Daniel Veillard87076042004-05-03 22:54:49 +0000352 void *ret;
353
354 ret = xmlMemMalloc(size);
355 if (ret != NULL) {
356 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000357 OOM();
358 xmlMemFree(ret);
359 return (NULL);
360 }
Daniel Veillard87076042004-05-03 22:54:49 +0000361 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000362 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000363}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000364static void *
365myReallocFunc(void *mem, size_t size)
366{
Daniel Veillard87076042004-05-03 22:54:49 +0000367 void *ret;
368
369 ret = xmlMemRealloc(mem, size);
370 if (ret != NULL) {
371 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000372 OOM();
373 xmlMemFree(ret);
374 return (NULL);
375 }
Daniel Veillard87076042004-05-03 22:54:49 +0000376 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000377 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000378}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000379static char *
380myStrdupFunc(const char *str)
381{
Daniel Veillard87076042004-05-03 22:54:49 +0000382 char *ret;
383
384 ret = xmlMemoryStrdup(str);
385 if (ret != NULL) {
386 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000387 OOM();
388 xmlFree(ret);
389 return (NULL);
390 }
Daniel Veillard87076042004-05-03 22:54:49 +0000391 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000392 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000393}
Daniel Veillard87076042004-05-03 22:54:49 +0000394/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +0200395 * *
Daniel Veillard87076042004-05-03 22:54:49 +0000396 * Internal timing routines to remove the necessity to have *
397 * unix-specific function calls. *
Daniel Veillardf1edb102009-08-10 14:43:18 +0200398 * *
Daniel Veillard87076042004-05-03 22:54:49 +0000399 ************************************************************************/
Daniel Veillard01db67c2001-12-18 07:09:59 +0000400
Daniel Veillardf1edb102009-08-10 14:43:18 +0200401#ifndef HAVE_GETTIMEOFDAY
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000402#ifdef HAVE_SYS_TIMEB_H
403#ifdef HAVE_SYS_TIME_H
404#ifdef HAVE_FTIME
405
Daniel Veillard01c13b52002-12-10 15:19:08 +0000406static int
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000407my_gettimeofday(struct timeval *tvp, void *tzp)
408{
409 struct timeb timebuffer;
410
411 ftime(&timebuffer);
412 if (tvp) {
413 tvp->tv_sec = timebuffer.time;
414 tvp->tv_usec = timebuffer.millitm * 1000L;
415 }
416 return (0);
417}
418#define HAVE_GETTIMEOFDAY 1
419#define gettimeofday my_gettimeofday
420
421#endif /* HAVE_FTIME */
422#endif /* HAVE_SYS_TIME_H */
423#endif /* HAVE_SYS_TIMEB_H */
424#endif /* !HAVE_GETTIMEOFDAY */
425
Daniel Veillard01db67c2001-12-18 07:09:59 +0000426#if defined(HAVE_GETTIMEOFDAY)
427static struct timeval begin, end;
428
429/*
430 * startTimer: call where you want to start timing
431 */
432static void
433startTimer(void)
434{
435 gettimeofday(&begin, NULL);
436}
437
438/*
439 * endTimer: call where you want to stop timing and to print out a
440 * message about the timing performed; format is a printf
441 * type argument
442 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000443static void XMLCDECL
Daniel Veillard118aed72002-09-24 14:13:13 +0000444endTimer(const char *fmt, ...)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000445{
446 long msec;
447 va_list ap;
448
449 gettimeofday(&end, NULL);
450 msec = end.tv_sec - begin.tv_sec;
451 msec *= 1000;
452 msec += (end.tv_usec - begin.tv_usec) / 1000;
453
454#ifndef HAVE_STDARG_H
455#error "endTimer required stdarg functions"
456#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000457 va_start(ap, fmt);
458 vfprintf(stderr, fmt, ap);
Daniel Veillard01db67c2001-12-18 07:09:59 +0000459 va_end(ap);
460
461 fprintf(stderr, " took %ld ms\n", msec);
462}
463#elif defined(HAVE_TIME_H)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000464/*
465 * No gettimeofday function, so we have to make do with calling clock.
466 * This is obviously less accurate, but there's little we can do about
467 * that.
468 */
Daniel Veillard90bc3712002-03-07 15:12:58 +0000469#ifndef CLOCKS_PER_SEC
470#define CLOCKS_PER_SEC 100
471#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +0000472
473static clock_t begin, end;
474static void
475startTimer(void)
476{
477 begin = clock();
478}
Daniel Veillardffa3c742005-07-21 13:24:09 +0000479static void XMLCDECL
Daniel Veillard01db67c2001-12-18 07:09:59 +0000480endTimer(const char *fmt, ...)
481{
482 long msec;
483 va_list ap;
484
485 end = clock();
486 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
487
488#ifndef HAVE_STDARG_H
489#error "endTimer required stdarg functions"
490#endif
491 va_start(ap, fmt);
492 vfprintf(stderr, fmt, ap);
493 va_end(ap);
494 fprintf(stderr, " took %ld ms\n", msec);
495}
496#else
497
498/*
499 * We don't have a gettimeofday or time.h, so we just don't do timing
500 */
501static void
502startTimer(void)
503{
504 /*
505 * Do nothing
506 */
507}
Daniel Veillardffa3c742005-07-21 13:24:09 +0000508static void XMLCDECL
Daniel Veillard01db67c2001-12-18 07:09:59 +0000509endTimer(char *format, ...)
510{
511 /*
512 * We cannot do anything because we don't have a timing function
513 */
514#ifdef HAVE_STDARG_H
515 va_start(ap, format);
516 vfprintf(stderr, format, ap);
517 va_end(ap);
518 fprintf(stderr, " was not timed\n", msec);
519#else
520 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
521 * this ?!
522 */
523#endif
524}
525#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000526/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +0200527 * *
528 * HTML ouput *
529 * *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000530 ************************************************************************/
Daniel Veillard24505b02005-07-28 23:49:35 +0000531static char buffer[50000];
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000532
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000533static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000534xmlHTMLEncodeSend(void) {
535 char *result;
536
537 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
538 if (result) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000539 xmlGenericError(xmlGenericErrorContext, "%s", result);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000540 xmlFree(result);
541 }
542 buffer[0] = 0;
543}
544
545/**
546 * xmlHTMLPrintFileInfo:
547 * @input: an xmlParserInputPtr input
Daniel Veillardf1edb102009-08-10 14:43:18 +0200548 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000549 * Displays the associated file and line informations for the current input
550 */
551
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000552static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000553xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000554 int len;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000555 xmlGenericError(xmlGenericErrorContext, "<p>");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000556
557 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000558 if (input != NULL) {
559 if (input->filename) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000560 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000561 input->line);
562 } else {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000563 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000564 }
565 }
566 xmlHTMLEncodeSend();
567}
568
569/**
570 * xmlHTMLPrintFileContext:
571 * @input: an xmlParserInputPtr input
Daniel Veillardf1edb102009-08-10 14:43:18 +0200572 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000573 * Displays current context within the input content for error tracking
574 */
575
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000576static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000577xmlHTMLPrintFileContext(xmlParserInputPtr input) {
578 const xmlChar *cur, *base;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000579 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000580 int n;
581
582 if (input == NULL) return;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000583 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000584 cur = input->cur;
585 base = input->base;
586 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
587 cur--;
588 }
589 n = 0;
590 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
591 cur--;
592 if ((*cur == '\n') || (*cur == '\r')) cur++;
593 base = cur;
594 n = 0;
595 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000596 len = strlen(buffer);
Daniel Veillardf1edb102009-08-10 14:43:18 +0200597 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000598 (unsigned char) *cur++);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000599 n++;
600 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000601 len = strlen(buffer);
602 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000603 cur = input->cur;
604 while ((*cur == '\n') || (*cur == '\r'))
605 cur--;
606 n = 0;
607 while ((cur != base) && (n++ < 80)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000608 len = strlen(buffer);
609 snprintf(&buffer[len], sizeof(buffer) - len, " ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000610 base++;
611 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000612 len = strlen(buffer);
613 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000614 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000615 xmlGenericError(xmlGenericErrorContext, "</pre>");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000616}
617
618/**
619 * xmlHTMLError:
620 * @ctx: an XML parser context
621 * @msg: the message to display/transmit
622 * @...: extra parameters for the message display
Daniel Veillardf1edb102009-08-10 14:43:18 +0200623 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000624 * Display and format an error messages, gives file, line, position and
625 * extra parameters.
626 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000627static void XMLCDECL
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000628xmlHTMLError(void *ctx, const char *msg, ...)
629{
630 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
631 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000632 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000633 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000634
635 buffer[0] = 0;
636 input = ctxt->input;
637 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000638 input = ctxt->inputTab[ctxt->inputNr - 2];
639 }
Daniel Veillardf1edb102009-08-10 14:43:18 +0200640
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000641 xmlHTMLPrintFileInfo(input);
642
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000643 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000644 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000645 len = strlen(buffer);
646 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000647 va_end(args);
648 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000649 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000650
651 xmlHTMLPrintFileContext(input);
652 xmlHTMLEncodeSend();
653}
654
655/**
656 * xmlHTMLWarning:
657 * @ctx: an XML parser context
658 * @msg: the message to display/transmit
659 * @...: extra parameters for the message display
Daniel Veillardf1edb102009-08-10 14:43:18 +0200660 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000661 * Display and format a warning messages, gives file, line, position and
662 * extra parameters.
663 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000664static void XMLCDECL
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000665xmlHTMLWarning(void *ctx, const char *msg, ...)
666{
667 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
668 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000669 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000670 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000671
672 buffer[0] = 0;
673 input = ctxt->input;
674 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000675 input = ctxt->inputTab[ctxt->inputNr - 2];
676 }
Daniel Veillardf1edb102009-08-10 14:43:18 +0200677
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000678
679 xmlHTMLPrintFileInfo(input);
Daniel Veillardf1edb102009-08-10 14:43:18 +0200680
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000681 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000682 va_start(args, msg);
Daniel Veillardf1edb102009-08-10 14:43:18 +0200683 len = strlen(buffer);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000684 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000685 va_end(args);
686 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000687 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000688
689 xmlHTMLPrintFileContext(input);
690 xmlHTMLEncodeSend();
691}
692
693/**
694 * xmlHTMLValidityError:
695 * @ctx: an XML parser context
696 * @msg: the message to display/transmit
697 * @...: extra parameters for the message display
Daniel Veillardf1edb102009-08-10 14:43:18 +0200698 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000699 * Display and format an validity error messages, gives file,
700 * line, position and extra parameters.
701 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000702static void XMLCDECL
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000703xmlHTMLValidityError(void *ctx, const char *msg, ...)
704{
705 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
706 xmlParserInputPtr input;
707 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000708 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000709
710 buffer[0] = 0;
711 input = ctxt->input;
712 if ((input->filename == NULL) && (ctxt->inputNr > 1))
713 input = ctxt->inputTab[ctxt->inputNr - 2];
Daniel Veillardf1edb102009-08-10 14:43:18 +0200714
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000715 xmlHTMLPrintFileInfo(input);
716
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000717 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000718 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000719 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000720 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000721 va_end(args);
722 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000723 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000724
725 xmlHTMLPrintFileContext(input);
726 xmlHTMLEncodeSend();
Daniel Veillard9fcb4912005-03-16 12:57:31 +0000727 progresult = XMLLINT_ERR_VALID;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000728}
729
730/**
731 * xmlHTMLValidityWarning:
732 * @ctx: an XML parser context
733 * @msg: the message to display/transmit
734 * @...: extra parameters for the message display
Daniel Veillardf1edb102009-08-10 14:43:18 +0200735 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000736 * Display and format a validity warning messages, gives file, line,
737 * position and extra parameters.
738 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000739static void XMLCDECL
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000740xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
741{
742 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
743 xmlParserInputPtr input;
744 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000745 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000746
747 buffer[0] = 0;
748 input = ctxt->input;
749 if ((input->filename == NULL) && (ctxt->inputNr > 1))
750 input = ctxt->inputTab[ctxt->inputNr - 2];
751
752 xmlHTMLPrintFileInfo(input);
Daniel Veillardf1edb102009-08-10 14:43:18 +0200753
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000754 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000755 va_start(args, msg);
Daniel Veillardf1edb102009-08-10 14:43:18 +0200756 len = strlen(buffer);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000757 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000758 va_end(args);
759 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000760 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000761
762 xmlHTMLPrintFileContext(input);
763 xmlHTMLEncodeSend();
764}
765
766/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +0200767 * *
768 * Shell Interface *
769 * *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000770 ************************************************************************/
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000771#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardd0cf7f62004-11-09 16:17:02 +0000772#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000773/**
774 * xmlShellReadline:
775 * @prompt: the prompt value
776 *
777 * Read a string
Daniel Veillardf1edb102009-08-10 14:43:18 +0200778 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000779 * Returns a pointer to it or NULL on EOF the caller is expected to
780 * free the returned string.
781 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000782static char *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000783xmlShellReadline(char *prompt) {
784#ifdef HAVE_LIBREADLINE
785 char *line_read;
786
787 /* Get a line from the user. */
788 line_read = readline (prompt);
789
790 /* If the line has any text in it, save it on the history. */
791 if (line_read && *line_read)
792 add_history (line_read);
793
794 return (line_read);
795#else
796 char line_read[501];
Daniel Veillard29e43992001-12-13 22:21:58 +0000797 char *ret;
798 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000799
800 if (prompt != NULL)
801 fprintf(stdout, "%s", prompt);
802 if (!fgets(line_read, 500, stdin))
803 return(NULL);
804 line_read[500] = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000805 len = strlen(line_read);
806 ret = (char *) malloc(len + 1);
807 if (ret != NULL) {
808 memcpy (ret, line_read, len + 1);
809 }
810 return(ret);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000811#endif
812}
Daniel Veillardd0cf7f62004-11-09 16:17:02 +0000813#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000814#endif /* LIBXML_DEBUG_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000815
816/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +0200817 * *
818 * I/O Interfaces *
819 * *
Daniel Veillard5e873c42000-04-12 13:27:38 +0000820 ************************************************************************/
821
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000822static int myRead(FILE *f, char * buf, int len) {
823 return(fread(buf, 1, len, f));
Daniel Veillard5e873c42000-04-12 13:27:38 +0000824}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000825static void myClose(FILE *f) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000826 if (f != stdin) {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000827 fclose(f);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000828 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000829}
830
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000831/************************************************************************
832 * *
Daniel Veillardf1edb102009-08-10 14:43:18 +0200833 * SAX based tests *
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000834 * *
835 ************************************************************************/
836
837/*
838 * empty SAX block
839 */
Daniel Veillard24505b02005-07-28 23:49:35 +0000840static xmlSAXHandler emptySAXHandlerStruct = {
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000841 NULL, /* internalSubset */
842 NULL, /* isStandalone */
843 NULL, /* hasInternalSubset */
844 NULL, /* hasExternalSubset */
845 NULL, /* resolveEntity */
846 NULL, /* getEntity */
847 NULL, /* entityDecl */
848 NULL, /* notationDecl */
849 NULL, /* attributeDecl */
850 NULL, /* elementDecl */
851 NULL, /* unparsedEntityDecl */
852 NULL, /* setDocumentLocator */
853 NULL, /* startDocument */
854 NULL, /* endDocument */
855 NULL, /* startElement */
856 NULL, /* endElement */
857 NULL, /* reference */
858 NULL, /* characters */
859 NULL, /* ignorableWhitespace */
860 NULL, /* processingInstruction */
861 NULL, /* comment */
862 NULL, /* xmlParserWarning */
863 NULL, /* xmlParserError */
864 NULL, /* xmlParserError */
865 NULL, /* getParameterEntity */
866 NULL, /* cdataBlock; */
867 NULL, /* externalSubset; */
Daniel Veillard971771e2005-07-09 17:32:57 +0000868 XML_SAX2_MAGIC,
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000869 NULL,
870 NULL, /* startElementNs */
871 NULL, /* endElementNs */
872 NULL /* xmlStructuredErrorFunc */
873};
874
Daniel Veillard24505b02005-07-28 23:49:35 +0000875static xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000876extern xmlSAXHandlerPtr debugSAXHandler;
877static int callbacks;
878
879/**
880 * isStandaloneDebug:
881 * @ctxt: An XML parser context
882 *
883 * Is this document tagged standalone ?
884 *
885 * Returns 1 if true
886 */
887static int
888isStandaloneDebug(void *ctx ATTRIBUTE_UNUSED)
889{
890 callbacks++;
891 if (noout)
892 return(0);
893 fprintf(stdout, "SAX.isStandalone()\n");
894 return(0);
895}
896
897/**
898 * hasInternalSubsetDebug:
899 * @ctxt: An XML parser context
900 *
901 * Does this document has an internal subset
902 *
903 * Returns 1 if true
904 */
905static int
906hasInternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
907{
908 callbacks++;
909 if (noout)
910 return(0);
911 fprintf(stdout, "SAX.hasInternalSubset()\n");
912 return(0);
913}
914
915/**
916 * hasExternalSubsetDebug:
917 * @ctxt: An XML parser context
918 *
919 * Does this document has an external subset
920 *
921 * Returns 1 if true
922 */
923static int
924hasExternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
925{
926 callbacks++;
927 if (noout)
928 return(0);
929 fprintf(stdout, "SAX.hasExternalSubset()\n");
930 return(0);
931}
932
933/**
934 * internalSubsetDebug:
935 * @ctxt: An XML parser context
936 *
937 * Does this document has an internal subset
938 */
939static void
940internalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
941 const xmlChar *ExternalID, const xmlChar *SystemID)
942{
943 callbacks++;
944 if (noout)
945 return;
946 fprintf(stdout, "SAX.internalSubset(%s,", name);
947 if (ExternalID == NULL)
948 fprintf(stdout, " ,");
949 else
950 fprintf(stdout, " %s,", ExternalID);
951 if (SystemID == NULL)
952 fprintf(stdout, " )\n");
953 else
954 fprintf(stdout, " %s)\n", SystemID);
955}
956
957/**
958 * externalSubsetDebug:
959 * @ctxt: An XML parser context
960 *
961 * Does this document has an external subset
962 */
963static void
964externalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
965 const xmlChar *ExternalID, const xmlChar *SystemID)
966{
967 callbacks++;
968 if (noout)
969 return;
970 fprintf(stdout, "SAX.externalSubset(%s,", name);
971 if (ExternalID == NULL)
972 fprintf(stdout, " ,");
973 else
974 fprintf(stdout, " %s,", ExternalID);
975 if (SystemID == NULL)
976 fprintf(stdout, " )\n");
977 else
978 fprintf(stdout, " %s)\n", SystemID);
979}
980
981/**
982 * resolveEntityDebug:
983 * @ctxt: An XML parser context
984 * @publicId: The public ID of the entity
985 * @systemId: The system ID of the entity
986 *
987 * Special entity resolver, better left to the parser, it has
988 * more context than the application layer.
989 * The default behaviour is to NOT resolve the entities, in that case
990 * the ENTITY_REF nodes are built in the structure (and the parameter
991 * values).
992 *
993 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
994 */
995static xmlParserInputPtr
996resolveEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *publicId, const xmlChar *systemId)
997{
998 callbacks++;
999 if (noout)
1000 return(NULL);
1001 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
1002
Daniel Veillardf1edb102009-08-10 14:43:18 +02001003
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001004 fprintf(stdout, "SAX.resolveEntity(");
1005 if (publicId != NULL)
1006 fprintf(stdout, "%s", (char *)publicId);
1007 else
1008 fprintf(stdout, " ");
1009 if (systemId != NULL)
1010 fprintf(stdout, ", %s)\n", (char *)systemId);
1011 else
1012 fprintf(stdout, ", )\n");
1013 return(NULL);
1014}
1015
1016/**
1017 * getEntityDebug:
1018 * @ctxt: An XML parser context
1019 * @name: The entity name
1020 *
1021 * Get an entity by name
1022 *
1023 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
1024 */
1025static xmlEntityPtr
1026getEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1027{
1028 callbacks++;
1029 if (noout)
1030 return(NULL);
1031 fprintf(stdout, "SAX.getEntity(%s)\n", name);
1032 return(NULL);
1033}
1034
1035/**
1036 * getParameterEntityDebug:
1037 * @ctxt: An XML parser context
1038 * @name: The entity name
1039 *
1040 * Get a parameter entity by name
1041 *
1042 * Returns the xmlParserInputPtr
1043 */
1044static xmlEntityPtr
1045getParameterEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1046{
1047 callbacks++;
1048 if (noout)
1049 return(NULL);
1050 fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
1051 return(NULL);
1052}
1053
1054
1055/**
1056 * entityDeclDebug:
1057 * @ctxt: An XML parser context
Daniel Veillardf1edb102009-08-10 14:43:18 +02001058 * @name: the entity name
1059 * @type: the entity type
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001060 * @publicId: The public ID of the entity
1061 * @systemId: The system ID of the entity
1062 * @content: the entity value (without processing).
1063 *
1064 * An entity definition has been parsed
1065 */
1066static void
1067entityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
1068 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
1069{
1070const xmlChar *nullstr = BAD_CAST "(null)";
1071 /* not all libraries handle printing null pointers nicely */
1072 if (publicId == NULL)
1073 publicId = nullstr;
1074 if (systemId == NULL)
1075 systemId = nullstr;
1076 if (content == NULL)
1077 content = (xmlChar *)nullstr;
1078 callbacks++;
1079 if (noout)
1080 return;
1081 fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
1082 name, type, publicId, systemId, content);
1083}
1084
1085/**
1086 * attributeDeclDebug:
1087 * @ctxt: An XML parser context
Daniel Veillardf1edb102009-08-10 14:43:18 +02001088 * @name: the attribute name
1089 * @type: the attribute type
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001090 *
1091 * An attribute definition has been parsed
1092 */
1093static void
1094attributeDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar * elem,
1095 const xmlChar * name, int type, int def,
1096 const xmlChar * defaultValue, xmlEnumerationPtr tree)
1097{
1098 callbacks++;
1099 if (noout)
1100 return;
1101 if (defaultValue == NULL)
1102 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, NULL, ...)\n",
1103 elem, name, type, def);
1104 else
1105 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
1106 elem, name, type, def, defaultValue);
1107 xmlFreeEnumeration(tree);
1108}
1109
1110/**
1111 * elementDeclDebug:
1112 * @ctxt: An XML parser context
Daniel Veillardf1edb102009-08-10 14:43:18 +02001113 * @name: the element name
1114 * @type: the element type
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001115 * @content: the element value (without processing).
1116 *
1117 * An element definition has been parsed
1118 */
1119static void
1120elementDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
1121 xmlElementContentPtr content ATTRIBUTE_UNUSED)
1122{
1123 callbacks++;
1124 if (noout)
1125 return;
1126 fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
1127 name, type);
1128}
1129
1130/**
1131 * notationDeclDebug:
1132 * @ctxt: An XML parser context
1133 * @name: The name of the notation
1134 * @publicId: The public ID of the entity
1135 * @systemId: The system ID of the entity
1136 *
1137 * What to do when a notation declaration has been parsed.
1138 */
1139static void
1140notationDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
1141 const xmlChar *publicId, const xmlChar *systemId)
1142{
1143 callbacks++;
1144 if (noout)
1145 return;
1146 fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
1147 (char *) name, (char *) publicId, (char *) systemId);
1148}
1149
1150/**
1151 * unparsedEntityDeclDebug:
1152 * @ctxt: An XML parser context
1153 * @name: The name of the entity
1154 * @publicId: The public ID of the entity
1155 * @systemId: The system ID of the entity
1156 * @notationName: the name of the notation
1157 *
1158 * What to do when an unparsed entity declaration is parsed
1159 */
1160static void
1161unparsedEntityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
1162 const xmlChar *publicId, const xmlChar *systemId,
1163 const xmlChar *notationName)
1164{
1165const xmlChar *nullstr = BAD_CAST "(null)";
1166
1167 if (publicId == NULL)
1168 publicId = nullstr;
1169 if (systemId == NULL)
1170 systemId = nullstr;
1171 if (notationName == NULL)
1172 notationName = nullstr;
1173 callbacks++;
1174 if (noout)
1175 return;
1176 fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
1177 (char *) name, (char *) publicId, (char *) systemId,
1178 (char *) notationName);
1179}
1180
1181/**
1182 * setDocumentLocatorDebug:
1183 * @ctxt: An XML parser context
1184 * @loc: A SAX Locator
1185 *
1186 * Receive the document locator at startup, actually xmlDefaultSAXLocator
1187 * Everything is available on the context, so this is useless in our case.
1188 */
1189static void
1190setDocumentLocatorDebug(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
1191{
1192 callbacks++;
1193 if (noout)
1194 return;
1195 fprintf(stdout, "SAX.setDocumentLocator()\n");
1196}
1197
1198/**
1199 * startDocumentDebug:
1200 * @ctxt: An XML parser context
1201 *
1202 * called when the document start being processed.
1203 */
1204static void
1205startDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
1206{
1207 callbacks++;
1208 if (noout)
1209 return;
1210 fprintf(stdout, "SAX.startDocument()\n");
1211}
1212
1213/**
1214 * endDocumentDebug:
1215 * @ctxt: An XML parser context
1216 *
1217 * called when the document end has been detected.
1218 */
1219static void
1220endDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
1221{
1222 callbacks++;
1223 if (noout)
1224 return;
1225 fprintf(stdout, "SAX.endDocument()\n");
1226}
1227
1228/**
1229 * startElementDebug:
1230 * @ctxt: An XML parser context
1231 * @name: The element name
1232 *
1233 * called when an opening tag has been processed.
1234 */
1235static void
1236startElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, const xmlChar **atts)
1237{
1238 int i;
1239
1240 callbacks++;
1241 if (noout)
1242 return;
1243 fprintf(stdout, "SAX.startElement(%s", (char *) name);
1244 if (atts != NULL) {
1245 for (i = 0;(atts[i] != NULL);i++) {
1246 fprintf(stdout, ", %s='", atts[i++]);
1247 if (atts[i] != NULL)
1248 fprintf(stdout, "%s'", atts[i]);
1249 }
1250 }
1251 fprintf(stdout, ")\n");
1252}
1253
1254/**
1255 * endElementDebug:
1256 * @ctxt: An XML parser context
1257 * @name: The element name
1258 *
1259 * called when the end of an element has been detected.
1260 */
1261static void
1262endElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1263{
1264 callbacks++;
1265 if (noout)
1266 return;
1267 fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
1268}
1269
1270/**
1271 * charactersDebug:
1272 * @ctxt: An XML parser context
1273 * @ch: a xmlChar string
1274 * @len: the number of xmlChar
1275 *
1276 * receiving some chars from the parser.
1277 * Question: how much at a time ???
1278 */
1279static void
1280charactersDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
1281{
1282 char out[40];
1283 int i;
1284
1285 callbacks++;
1286 if (noout)
1287 return;
1288 for (i = 0;(i<len) && (i < 30);i++)
1289 out[i] = ch[i];
1290 out[i] = 0;
1291
1292 fprintf(stdout, "SAX.characters(%s, %d)\n", out, len);
1293}
1294
1295/**
1296 * referenceDebug:
1297 * @ctxt: An XML parser context
1298 * @name: The entity name
1299 *
Daniel Veillardf1edb102009-08-10 14:43:18 +02001300 * called when an entity reference is detected.
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001301 */
1302static void
1303referenceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1304{
1305 callbacks++;
1306 if (noout)
1307 return;
1308 fprintf(stdout, "SAX.reference(%s)\n", name);
1309}
1310
1311/**
1312 * ignorableWhitespaceDebug:
1313 * @ctxt: An XML parser context
1314 * @ch: a xmlChar string
1315 * @start: the first char in the string
1316 * @len: the number of xmlChar
1317 *
1318 * receiving some ignorable whitespaces from the parser.
1319 * Question: how much at a time ???
1320 */
1321static void
1322ignorableWhitespaceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
1323{
1324 char out[40];
1325 int i;
1326
1327 callbacks++;
1328 if (noout)
1329 return;
1330 for (i = 0;(i<len) && (i < 30);i++)
1331 out[i] = ch[i];
1332 out[i] = 0;
1333 fprintf(stdout, "SAX.ignorableWhitespace(%s, %d)\n", out, len);
1334}
1335
1336/**
1337 * processingInstructionDebug:
1338 * @ctxt: An XML parser context
1339 * @target: the target name
1340 * @data: the PI data's
1341 * @len: the number of xmlChar
1342 *
1343 * A processing instruction has been parsed.
1344 */
1345static void
1346processingInstructionDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *target,
1347 const xmlChar *data)
1348{
1349 callbacks++;
1350 if (noout)
1351 return;
1352 if (data != NULL)
1353 fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
1354 (char *) target, (char *) data);
1355 else
1356 fprintf(stdout, "SAX.processingInstruction(%s, NULL)\n",
1357 (char *) target);
1358}
1359
1360/**
1361 * cdataBlockDebug:
1362 * @ctx: the user data (XML parser context)
1363 * @value: The pcdata content
1364 * @len: the block length
1365 *
1366 * called when a pcdata block has been parsed
1367 */
1368static void
1369cdataBlockDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value, int len)
1370{
1371 callbacks++;
1372 if (noout)
1373 return;
1374 fprintf(stdout, "SAX.pcdata(%.20s, %d)\n",
1375 (char *) value, len);
1376}
1377
1378/**
1379 * commentDebug:
1380 * @ctxt: An XML parser context
1381 * @value: the comment content
1382 *
1383 * A comment has been parsed.
1384 */
1385static void
1386commentDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value)
1387{
1388 callbacks++;
1389 if (noout)
1390 return;
1391 fprintf(stdout, "SAX.comment(%s)\n", value);
1392}
1393
1394/**
1395 * warningDebug:
1396 * @ctxt: An XML parser context
1397 * @msg: the message to display/transmit
1398 * @...: extra parameters for the message display
1399 *
1400 * Display and format a warning messages, gives file, line, position and
1401 * extra parameters.
1402 */
Daniel Veillardffa3c742005-07-21 13:24:09 +00001403static void XMLCDECL
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001404warningDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1405{
1406 va_list args;
1407
1408 callbacks++;
1409 if (noout)
1410 return;
1411 va_start(args, msg);
1412 fprintf(stdout, "SAX.warning: ");
1413 vfprintf(stdout, msg, args);
1414 va_end(args);
1415}
1416
1417/**
1418 * errorDebug:
1419 * @ctxt: An XML parser context
1420 * @msg: the message to display/transmit
1421 * @...: extra parameters for the message display
1422 *
1423 * Display and format a error messages, gives file, line, position and
1424 * extra parameters.
1425 */
Daniel Veillardffa3c742005-07-21 13:24:09 +00001426static void XMLCDECL
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001427errorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1428{
1429 va_list args;
1430
1431 callbacks++;
1432 if (noout)
1433 return;
1434 va_start(args, msg);
1435 fprintf(stdout, "SAX.error: ");
1436 vfprintf(stdout, msg, args);
1437 va_end(args);
1438}
1439
1440/**
1441 * fatalErrorDebug:
1442 * @ctxt: An XML parser context
1443 * @msg: the message to display/transmit
1444 * @...: extra parameters for the message display
1445 *
1446 * Display and format a fatalError messages, gives file, line, position and
1447 * extra parameters.
1448 */
Daniel Veillardffa3c742005-07-21 13:24:09 +00001449static void XMLCDECL
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001450fatalErrorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1451{
1452 va_list args;
1453
1454 callbacks++;
1455 if (noout)
1456 return;
1457 va_start(args, msg);
1458 fprintf(stdout, "SAX.fatalError: ");
1459 vfprintf(stdout, msg, args);
1460 va_end(args);
1461}
1462
Daniel Veillard24505b02005-07-28 23:49:35 +00001463static xmlSAXHandler debugSAXHandlerStruct = {
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001464 internalSubsetDebug,
1465 isStandaloneDebug,
1466 hasInternalSubsetDebug,
1467 hasExternalSubsetDebug,
1468 resolveEntityDebug,
1469 getEntityDebug,
1470 entityDeclDebug,
1471 notationDeclDebug,
1472 attributeDeclDebug,
1473 elementDeclDebug,
1474 unparsedEntityDeclDebug,
1475 setDocumentLocatorDebug,
1476 startDocumentDebug,
1477 endDocumentDebug,
1478 startElementDebug,
1479 endElementDebug,
1480 referenceDebug,
1481 charactersDebug,
1482 ignorableWhitespaceDebug,
1483 processingInstructionDebug,
1484 commentDebug,
1485 warningDebug,
1486 errorDebug,
1487 fatalErrorDebug,
1488 getParameterEntityDebug,
1489 cdataBlockDebug,
1490 externalSubsetDebug,
1491 1,
1492 NULL,
1493 NULL,
1494 NULL,
1495 NULL
1496};
1497
1498xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
1499
1500/*
1501 * SAX2 specific callbacks
1502 */
1503/**
1504 * startElementNsDebug:
1505 * @ctxt: An XML parser context
1506 * @name: The element name
1507 *
1508 * called when an opening tag has been processed.
1509 */
1510static void
1511startElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
1512 const xmlChar *localname,
1513 const xmlChar *prefix,
1514 const xmlChar *URI,
1515 int nb_namespaces,
1516 const xmlChar **namespaces,
1517 int nb_attributes,
1518 int nb_defaulted,
1519 const xmlChar **attributes)
1520{
1521 int i;
1522
1523 callbacks++;
1524 if (noout)
1525 return;
1526 fprintf(stdout, "SAX.startElementNs(%s", (char *) localname);
1527 if (prefix == NULL)
1528 fprintf(stdout, ", NULL");
1529 else
1530 fprintf(stdout, ", %s", (char *) prefix);
1531 if (URI == NULL)
1532 fprintf(stdout, ", NULL");
1533 else
1534 fprintf(stdout, ", '%s'", (char *) URI);
1535 fprintf(stdout, ", %d", nb_namespaces);
Daniel Veillardf1edb102009-08-10 14:43:18 +02001536
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001537 if (namespaces != NULL) {
1538 for (i = 0;i < nb_namespaces * 2;i++) {
1539 fprintf(stdout, ", xmlns");
1540 if (namespaces[i] != NULL)
1541 fprintf(stdout, ":%s", namespaces[i]);
1542 i++;
1543 fprintf(stdout, "='%s'", namespaces[i]);
1544 }
1545 }
1546 fprintf(stdout, ", %d, %d", nb_attributes, nb_defaulted);
1547 if (attributes != NULL) {
1548 for (i = 0;i < nb_attributes * 5;i += 5) {
1549 if (attributes[i + 1] != NULL)
1550 fprintf(stdout, ", %s:%s='", attributes[i + 1], attributes[i]);
1551 else
1552 fprintf(stdout, ", %s='", attributes[i]);
1553 fprintf(stdout, "%.4s...', %d", attributes[i + 3],
1554 (int)(attributes[i + 4] - attributes[i + 3]));
1555 }
1556 }
1557 fprintf(stdout, ")\n");
1558}
1559
1560/**
1561 * endElementDebug:
1562 * @ctxt: An XML parser context
1563 * @name: The element name
1564 *
1565 * called when the end of an element has been detected.
1566 */
1567static void
1568endElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
1569 const xmlChar *localname,
1570 const xmlChar *prefix,
1571 const xmlChar *URI)
1572{
1573 callbacks++;
1574 if (noout)
1575 return;
1576 fprintf(stdout, "SAX.endElementNs(%s", (char *) localname);
1577 if (prefix == NULL)
1578 fprintf(stdout, ", NULL");
1579 else
1580 fprintf(stdout, ", %s", (char *) prefix);
1581 if (URI == NULL)
1582 fprintf(stdout, ", NULL)\n");
1583 else
1584 fprintf(stdout, ", '%s')\n", (char *) URI);
1585}
1586
Daniel Veillard24505b02005-07-28 23:49:35 +00001587static xmlSAXHandler debugSAX2HandlerStruct = {
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001588 internalSubsetDebug,
1589 isStandaloneDebug,
1590 hasInternalSubsetDebug,
1591 hasExternalSubsetDebug,
1592 resolveEntityDebug,
1593 getEntityDebug,
1594 entityDeclDebug,
1595 notationDeclDebug,
1596 attributeDeclDebug,
1597 elementDeclDebug,
1598 unparsedEntityDeclDebug,
1599 setDocumentLocatorDebug,
1600 startDocumentDebug,
1601 endDocumentDebug,
1602 NULL,
1603 NULL,
1604 referenceDebug,
1605 charactersDebug,
1606 ignorableWhitespaceDebug,
1607 processingInstructionDebug,
1608 commentDebug,
1609 warningDebug,
1610 errorDebug,
1611 fatalErrorDebug,
1612 getParameterEntityDebug,
1613 cdataBlockDebug,
1614 externalSubsetDebug,
1615 XML_SAX2_MAGIC,
1616 NULL,
1617 startElementNsDebug,
1618 endElementNsDebug,
1619 NULL
1620};
1621
Daniel Veillard24505b02005-07-28 23:49:35 +00001622static xmlSAXHandlerPtr debugSAX2Handler = &debugSAX2HandlerStruct;
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001623
1624static void
1625testSAX(const char *filename) {
1626 xmlSAXHandlerPtr handler;
1627 const char *user_data = "user_data"; /* mostly for debugging */
1628 xmlParserInputBufferPtr buf = NULL;
1629 xmlParserInputPtr inputStream;
1630 xmlParserCtxtPtr ctxt = NULL;
1631 xmlSAXHandlerPtr old_sax = NULL;
1632
1633 callbacks = 0;
1634
1635 if (noout) {
1636 handler = emptySAXHandler;
Daniel Veillard78dfc9f2005-07-10 22:30:30 +00001637#ifdef LIBXML_SAX1_ENABLED
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001638 } else if (sax1) {
1639 handler = debugSAXHandler;
Daniel Veillard78dfc9f2005-07-10 22:30:30 +00001640#endif
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001641 } else {
1642 handler = debugSAX2Handler;
1643 }
1644
1645 /*
1646 * it's not the simplest code but the most generic in term of I/O
1647 */
1648 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
1649 if (buf == NULL) {
1650 goto error;
1651 }
1652
1653#ifdef LIBXML_SCHEMAS_ENABLED
1654 if (wxschemas != NULL) {
1655 int ret;
1656 xmlSchemaValidCtxtPtr vctxt;
1657
1658 vctxt = xmlSchemaNewValidCtxt(wxschemas);
1659 xmlSchemaSetValidErrors(vctxt,
1660 (xmlSchemaValidityErrorFunc) fprintf,
1661 (xmlSchemaValidityWarningFunc) fprintf,
1662 stderr);
1663
Daniel Veillard971771e2005-07-09 17:32:57 +00001664 ret = xmlSchemaValidateStream(vctxt, buf, 0, handler,
1665 (void *)user_data);
1666 if (repeat == 0) {
1667 if (ret == 0) {
1668 fprintf(stderr, "%s validates\n", filename);
1669 } else if (ret > 0) {
1670 fprintf(stderr, "%s fails to validate\n", filename);
1671 progresult = XMLLINT_ERR_VALID;
1672 } else {
1673 fprintf(stderr, "%s validation generated an internal error\n",
1674 filename);
1675 progresult = XMLLINT_ERR_VALID;
1676 }
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001677 }
1678 xmlSchemaFreeValidCtxt(vctxt);
1679 } else
1680#endif
1681 {
1682 /*
1683 * Create the parser context amd hook the input
1684 */
1685 ctxt = xmlNewParserCtxt();
1686 if (ctxt == NULL) {
1687 xmlFreeParserInputBuffer(buf);
1688 goto error;
1689 }
1690 old_sax = ctxt->sax;
1691 ctxt->sax = handler;
1692 ctxt->userData = (void *) user_data;
1693 inputStream = xmlNewIOInputStream(ctxt, buf, XML_CHAR_ENCODING_NONE);
1694 if (inputStream == NULL) {
1695 xmlFreeParserInputBuffer(buf);
1696 goto error;
1697 }
1698 inputPush(ctxt, inputStream);
Daniel Veillardf1edb102009-08-10 14:43:18 +02001699
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001700 /* do the parsing */
1701 xmlParseDocument(ctxt);
1702
1703 if (ctxt->myDoc != NULL) {
1704 fprintf(stderr, "SAX generated a doc !\n");
1705 xmlFreeDoc(ctxt->myDoc);
1706 ctxt->myDoc = NULL;
1707 }
1708 }
1709
1710error:
1711 if (ctxt != NULL) {
1712 ctxt->sax = old_sax;
1713 xmlFreeParserCtxt(ctxt);
1714 }
1715}
1716
Daniel Veillard5e873c42000-04-12 13:27:38 +00001717/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +02001718 * *
1719 * Stream Test processing *
1720 * *
Daniel Veillard7704fb12003-01-03 16:19:51 +00001721 ************************************************************************/
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001722#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001723static void processNode(xmlTextReaderPtr reader) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001724 const xmlChar *name, *value;
Daniel Veillard16ef8002005-01-31 00:27:50 +00001725 int type, empty;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001726
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001727 type = xmlTextReaderNodeType(reader);
Daniel Veillard16ef8002005-01-31 00:27:50 +00001728 empty = xmlTextReaderIsEmptyElement(reader);
Daniel Veillard99737f52003-03-22 14:55:50 +00001729
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001730 if (debug) {
1731 name = xmlTextReaderConstName(reader);
1732 if (name == NULL)
1733 name = BAD_CAST "--";
Daniel Veillard7704fb12003-01-03 16:19:51 +00001734
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001735 value = xmlTextReaderConstValue(reader);
1736
Daniel Veillardf1edb102009-08-10 14:43:18 +02001737
1738 printf("%d %d %s %d %d",
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001739 xmlTextReaderDepth(reader),
1740 type,
1741 name,
Daniel Veillard16ef8002005-01-31 00:27:50 +00001742 empty,
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001743 xmlTextReaderHasValue(reader));
1744 if (value == NULL)
1745 printf("\n");
1746 else {
1747 printf(" %s\n", value);
1748 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00001749 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001750#ifdef LIBXML_PATTERN_ENABLED
1751 if (patternc) {
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001752 xmlChar *path = NULL;
1753 int match = -1;
Daniel Veillardf1edb102009-08-10 14:43:18 +02001754
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001755 if (type == XML_READER_TYPE_ELEMENT) {
1756 /* do the check only on element start */
1757 match = xmlPatternMatch(patternc, xmlTextReaderCurrentNode(reader));
1758
1759 if (match) {
Daniel Veillardf1edb102009-08-10 14:43:18 +02001760#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001761 path = xmlGetNodePath(xmlTextReaderCurrentNode(reader));
1762 printf("Node %s matches pattern %s\n", path, pattern);
Daniel Veillardf1edb102009-08-10 14:43:18 +02001763#else
1764 printf("Node %s matches pattern %s\n",
1765 xmlTextReaderConstName(reader), pattern);
1766#endif
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001767 }
1768 }
1769 if (patstream != NULL) {
1770 int ret;
1771
1772 if (type == XML_READER_TYPE_ELEMENT) {
1773 ret = xmlStreamPush(patstream,
1774 xmlTextReaderConstLocalName(reader),
1775 xmlTextReaderConstNamespaceUri(reader));
1776 if (ret < 0) {
1777 fprintf(stderr, "xmlStreamPush() failure\n");
1778 xmlFreeStreamCtxt(patstream);
1779 patstream = NULL;
1780 } else if (ret != match) {
Daniel Veillardf1edb102009-08-10 14:43:18 +02001781#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001782 if (path == NULL) {
1783 path = xmlGetNodePath(
1784 xmlTextReaderCurrentNode(reader));
1785 }
Daniel Veillardf1edb102009-08-10 14:43:18 +02001786#endif
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001787 fprintf(stderr,
1788 "xmlPatternMatch and xmlStreamPush disagree\n");
Daniel Veillardf1edb102009-08-10 14:43:18 +02001789 if (path != NULL)
1790 fprintf(stderr, " pattern %s node %s\n",
1791 pattern, path);
1792 else
1793 fprintf(stderr, " pattern %s node %s\n",
1794 pattern, xmlTextReaderConstName(reader));
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001795 }
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001796
Daniel Veillardf1edb102009-08-10 14:43:18 +02001797 }
Daniel Veillard16ef8002005-01-31 00:27:50 +00001798 if ((type == XML_READER_TYPE_END_ELEMENT) ||
1799 ((type == XML_READER_TYPE_ELEMENT) && (empty))) {
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001800 ret = xmlStreamPop(patstream);
1801 if (ret < 0) {
1802 fprintf(stderr, "xmlStreamPop() failure\n");
1803 xmlFreeStreamCtxt(patstream);
1804 patstream = NULL;
1805 }
1806 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001807 }
Daniel Veillardf9d16912005-01-30 22:36:30 +00001808 if (path != NULL)
1809 xmlFree(path);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001810 }
1811#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001812}
1813
1814static void streamFile(char *filename) {
1815 xmlTextReaderPtr reader;
1816 int ret;
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001817#ifdef HAVE_SYS_MMAN_H
1818 int fd = -1;
1819 struct stat info;
1820 const char *base = NULL;
1821 xmlParserInputBufferPtr input = NULL;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001822
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001823 if (memory) {
Daniel Veillardf1edb102009-08-10 14:43:18 +02001824 if (stat(filename, &info) < 0)
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001825 return;
1826 if ((fd = open(filename, O_RDONLY)) < 0)
1827 return;
1828 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
1829 if (base == (void *) MAP_FAILED)
1830 return;
1831
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001832 reader = xmlReaderForMemory(base, info.st_size, filename,
1833 NULL, options);
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001834 } else
1835#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001836 reader = xmlReaderForFile(filename, NULL, options);
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001837#ifdef LIBXML_PATTERN_ENABLED
Daniel Veillardd4301ab2005-02-03 22:24:10 +00001838 if (pattern != NULL) {
1839 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
1840 if (patternc == NULL) {
1841 xmlGenericError(xmlGenericErrorContext,
1842 "Pattern %s failed to compile\n", pattern);
1843 progresult = XMLLINT_ERR_SCHEMAPAT;
1844 pattern = NULL;
1845 }
1846 }
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001847 if (patternc != NULL) {
1848 patstream = xmlPatternGetStreamCtxt(patternc);
1849 if (patstream != NULL) {
1850 ret = xmlStreamPush(patstream, NULL, NULL);
1851 if (ret < 0) {
1852 fprintf(stderr, "xmlStreamPush() failure\n");
1853 xmlFreeStreamCtxt(patstream);
1854 patstream = NULL;
1855 }
1856 }
1857 }
1858#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001859
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001860
Daniel Veillard7704fb12003-01-03 16:19:51 +00001861 if (reader != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001862#ifdef LIBXML_VALID_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001863 if (valid)
1864 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00001865 else
Daniel Veillard4432df22003-09-28 18:58:27 +00001866#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce192eb2003-04-16 15:58:05 +00001867 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001868#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +00001869 if (relaxng != NULL) {
Daniel Veillard81514ba2003-09-16 23:17:26 +00001870 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00001871 startTimer();
1872 }
1873 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
1874 if (ret < 0) {
1875 xmlGenericError(xmlGenericErrorContext,
1876 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +00001877 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +00001878 relaxng = NULL;
1879 }
Daniel Veillard81514ba2003-09-16 23:17:26 +00001880 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00001881 endTimer("Compiling the schemas");
1882 }
1883 }
Daniel Veillardf10ae122005-07-10 19:03:16 +00001884 if (schema != NULL) {
1885 if ((timing) && (!repeat)) {
1886 startTimer();
1887 }
1888 ret = xmlTextReaderSchemaValidate(reader, schema);
1889 if (ret < 0) {
1890 xmlGenericError(xmlGenericErrorContext,
1891 "XSD schema %s failed to compile\n", schema);
1892 progresult = XMLLINT_ERR_SCHEMACOMP;
1893 schema = NULL;
1894 }
1895 if ((timing) && (!repeat)) {
1896 endTimer("Compiling the schemas");
1897 }
1898 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001899#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001900
1901 /*
1902 * Process all nodes in sequence
1903 */
Daniel Veillard81514ba2003-09-16 23:17:26 +00001904 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00001905 startTimer();
1906 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00001907 ret = xmlTextReaderRead(reader);
1908 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001909 if ((debug)
1910#ifdef LIBXML_PATTERN_ENABLED
1911 || (patternc)
1912#endif
1913 )
Daniel Veillard7704fb12003-01-03 16:19:51 +00001914 processNode(reader);
1915 ret = xmlTextReaderRead(reader);
1916 }
Daniel Veillard81514ba2003-09-16 23:17:26 +00001917 if ((timing) && (!repeat)) {
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001918#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf54cd532004-02-25 11:52:31 +00001919 if (relaxng != NULL)
Daniel Veillard49138f12004-02-19 12:58:36 +00001920 endTimer("Parsing and validating");
1921 else
Daniel Veillardf54cd532004-02-25 11:52:31 +00001922#endif
Daniel Veillard4432df22003-09-28 18:58:27 +00001923#ifdef LIBXML_VALID_ENABLED
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001924 if (valid)
Daniel Veillardce192eb2003-04-16 15:58:05 +00001925 endTimer("Parsing and validating");
1926 else
Daniel Veillard4432df22003-09-28 18:58:27 +00001927#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +00001928 endTimer("Parsing");
Daniel Veillardce192eb2003-04-16 15:58:05 +00001929 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00001930
Daniel Veillard4432df22003-09-28 18:58:27 +00001931#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf6bad792003-04-11 19:38:54 +00001932 if (valid) {
1933 if (xmlTextReaderIsValid(reader) != 1) {
1934 xmlGenericError(xmlGenericErrorContext,
1935 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001936 progresult = XMLLINT_ERR_VALID;
Daniel Veillardf6bad792003-04-11 19:38:54 +00001937 }
1938 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001939#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001940#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf10ae122005-07-10 19:03:16 +00001941 if ((relaxng != NULL) || (schema != NULL)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00001942 if (xmlTextReaderIsValid(reader) != 1) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001943 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001944 progresult = XMLLINT_ERR_VALID;
Daniel Veillardf4e55762003-04-15 23:32:22 +00001945 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001946 fprintf(stderr, "%s validates\n", filename);
Daniel Veillardf4e55762003-04-15 23:32:22 +00001947 }
1948 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001949#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001950 /*
1951 * Done, cleanup and status
1952 */
1953 xmlFreeTextReader(reader);
1954 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001955 fprintf(stderr, "%s : failed to parse\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001956 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001957 }
1958 } else {
1959 fprintf(stderr, "Unable to open %s\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001960 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001961 }
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001962#ifdef LIBXML_PATTERN_ENABLED
1963 if (patstream != NULL) {
1964 xmlFreeStreamCtxt(patstream);
1965 patstream = NULL;
1966 }
1967#endif
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001968#ifdef HAVE_SYS_MMAN_H
1969 if (memory) {
1970 xmlFreeParserInputBuffer(input);
1971 munmap((char *) base, info.st_size);
1972 close(fd);
1973 }
1974#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001975}
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001976
1977static void walkDoc(xmlDocPtr doc) {
1978 xmlTextReaderPtr reader;
1979 int ret;
1980
Daniel Veillardd4301ab2005-02-03 22:24:10 +00001981#ifdef LIBXML_PATTERN_ENABLED
1982 xmlNodePtr root;
1983 const xmlChar *namespaces[22];
1984 int i;
1985 xmlNsPtr ns;
1986
1987 root = xmlDocGetRootElement(doc);
1988 for (ns = root->nsDef, i = 0;ns != NULL && i < 20;ns=ns->next) {
1989 namespaces[i++] = ns->href;
1990 namespaces[i++] = ns->prefix;
1991 }
1992 namespaces[i++] = NULL;
1993 namespaces[i++] = NULL;
1994
1995 if (pattern != NULL) {
1996 patternc = xmlPatterncompile((const xmlChar *) pattern, doc->dict,
1997 0, &namespaces[0]);
1998 if (patternc == NULL) {
1999 xmlGenericError(xmlGenericErrorContext,
2000 "Pattern %s failed to compile\n", pattern);
2001 progresult = XMLLINT_ERR_SCHEMAPAT;
2002 pattern = NULL;
2003 }
2004 }
Daniel Veillard2b2e02d2005-02-05 23:20:22 +00002005 if (patternc != NULL) {
2006 patstream = xmlPatternGetStreamCtxt(patternc);
2007 if (patstream != NULL) {
2008 ret = xmlStreamPush(patstream, NULL, NULL);
2009 if (ret < 0) {
2010 fprintf(stderr, "xmlStreamPush() failure\n");
2011 xmlFreeStreamCtxt(patstream);
2012 patstream = NULL;
2013 }
2014 }
2015 }
Daniel Veillardd4301ab2005-02-03 22:24:10 +00002016#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002017 reader = xmlReaderWalker(doc);
2018 if (reader != NULL) {
2019 if ((timing) && (!repeat)) {
2020 startTimer();
2021 }
2022 ret = xmlTextReaderRead(reader);
2023 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002024 if ((debug)
2025#ifdef LIBXML_PATTERN_ENABLED
2026 || (patternc)
2027#endif
2028 )
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002029 processNode(reader);
2030 ret = xmlTextReaderRead(reader);
2031 }
2032 if ((timing) && (!repeat)) {
2033 endTimer("walking through the doc");
2034 }
2035 xmlFreeTextReader(reader);
2036 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002037 fprintf(stderr, "failed to walk through the doc\n");
William M. Brack8304d872004-06-08 13:29:32 +00002038 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002039 }
2040 } else {
2041 fprintf(stderr, "Failed to crate a reader from the document\n");
William M. Brack8304d872004-06-08 13:29:32 +00002042 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002043 }
Daniel Veillard2b2e02d2005-02-05 23:20:22 +00002044#ifdef LIBXML_PATTERN_ENABLED
2045 if (patstream != NULL) {
2046 xmlFreeStreamCtxt(patstream);
2047 patstream = NULL;
2048 }
2049#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002050}
Daniel Veillard81273902003-09-30 00:43:48 +00002051#endif /* LIBXML_READER_ENABLED */
Daniel Veillard7704fb12003-01-03 16:19:51 +00002052
2053/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +02002054 * *
2055 * Tree Test processing *
2056 * *
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002057 ************************************************************************/
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002058static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
Daniel Veillard652327a2003-09-29 18:02:38 +00002059 xmlDocPtr doc = NULL;
2060#ifdef LIBXML_TREE_ENABLED
2061 xmlDocPtr tmp;
2062#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002063
Daniel Veillard48b2f892001-02-25 16:11:03 +00002064 if ((timing) && (!repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00002065 startTimer();
Daniel Veillardf1edb102009-08-10 14:43:18 +02002066
Daniel Veillard48b2f892001-02-25 16:11:03 +00002067
Daniel Veillard652327a2003-09-29 18:02:38 +00002068#ifdef LIBXML_TREE_ENABLED
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002069 if (filename == NULL) {
2070 if (generate) {
2071 xmlNodePtr n;
2072
2073 doc = xmlNewDoc(BAD_CAST "1.0");
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002074 n = xmlNewDocNode(doc, NULL, BAD_CAST "info", NULL);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002075 xmlNodeSetContent(n, BAD_CAST "abc");
2076 xmlDocSetRootElement(doc, n);
2077 }
2078 }
Daniel Veillard652327a2003-09-29 18:02:38 +00002079#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002080#ifdef LIBXML_HTML_ENABLED
Daniel Veillard73b013f2003-09-30 12:36:01 +00002081#ifdef LIBXML_PUSH_ENABLED
William M. Brack78637da2003-07-31 14:47:38 +00002082 else if ((html) && (push)) {
2083 FILE *f;
2084
William M. Brack3403add2004-06-27 02:07:51 +00002085#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2086 f = fopen(filename, "rb");
2087#else
2088 f = fopen(filename, "r");
2089#endif
William M. Brack78637da2003-07-31 14:47:38 +00002090 if (f != NULL) {
2091 int res, size = 3;
2092 char chars[4096];
2093 htmlParserCtxtPtr ctxt;
2094
2095 /* if (repeat) */
2096 size = 4096;
2097 res = fread(chars, 1, 4, f);
2098 if (res > 0) {
2099 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
William M. Brack1d75c8a2003-10-27 13:48:16 +00002100 chars, res, filename, XML_CHAR_ENCODING_NONE);
William M. Brack78637da2003-07-31 14:47:38 +00002101 while ((res = fread(chars, 1, size, f)) > 0) {
2102 htmlParseChunk(ctxt, chars, res, 0);
2103 }
2104 htmlParseChunk(ctxt, chars, 0, 1);
2105 doc = ctxt->myDoc;
2106 htmlFreeParserCtxt(ctxt);
2107 }
2108 fclose(f);
2109 }
2110 }
Daniel Veillard73b013f2003-09-30 12:36:01 +00002111#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillardf1a27c62006-10-13 22:33:03 +00002112#ifdef HAVE_SYS_MMAN_H
2113 else if ((html) && (memory)) {
2114 int fd;
2115 struct stat info;
2116 const char *base;
Daniel Veillardf1edb102009-08-10 14:43:18 +02002117 if (stat(filename, &info) < 0)
Daniel Veillardf1a27c62006-10-13 22:33:03 +00002118 return;
2119 if ((fd = open(filename, O_RDONLY)) < 0)
2120 return;
2121 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
2122 if (base == (void *) MAP_FAILED)
2123 return;
2124
2125 doc = htmlReadMemory((char *) base, info.st_size, filename,
2126 NULL, options);
Daniel Veillardf1edb102009-08-10 14:43:18 +02002127
Daniel Veillardf1a27c62006-10-13 22:33:03 +00002128 munmap((char *) base, info.st_size);
2129 close(fd);
2130 }
2131#endif
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002132 else if (html) {
Daniel Veillard9475a352003-09-26 12:47:50 +00002133 doc = htmlReadFile(filename, NULL, options);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002134 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002135#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002136 else {
Daniel Veillard73b013f2003-09-30 12:36:01 +00002137#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002138 /*
2139 * build an XML tree from a string;
2140 */
2141 if (push) {
2142 FILE *f;
2143
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002144 /* '-' Usually means stdin -<sven@zen.org> */
2145 if ((filename[0] == '-') && (filename[1] == 0)) {
2146 f = stdin;
2147 } else {
William M. Brack3403add2004-06-27 02:07:51 +00002148#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2149 f = fopen(filename, "rb");
2150#else
2151 f = fopen(filename, "r");
2152#endif
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002153 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002154 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +00002155 int ret;
Daniel Veillarda880b122003-04-21 21:36:41 +00002156 int res, size = 1024;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002157 char chars[1024];
2158 xmlParserCtxtPtr ctxt;
2159
Daniel Veillarda880b122003-04-21 21:36:41 +00002160 /* if (repeat) size = 1024; */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002161 res = fread(chars, 1, 4, f);
2162 if (res > 0) {
2163 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
2164 chars, res, filename);
Daniel Veillard500a1de2004-03-22 15:22:58 +00002165 xmlCtxtUseOptions(ctxt, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002166 while ((res = fread(chars, 1, size, f)) > 0) {
2167 xmlParseChunk(ctxt, chars, res, 0);
2168 }
2169 xmlParseChunk(ctxt, chars, 0, 1);
2170 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +00002171 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002172 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +00002173 if (!ret) {
2174 xmlFreeDoc(doc);
2175 doc = NULL;
2176 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002177 }
2178 }
Daniel Veillard73b013f2003-09-30 12:36:01 +00002179 } else
2180#endif /* LIBXML_PUSH_ENABLED */
2181 if (testIO) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002182 if ((filename[0] == '-') && (filename[1] == 0)) {
Daniel Veillard60942de2003-09-25 21:05:58 +00002183 doc = xmlReadFd(0, NULL, NULL, options);
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002184 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002185 FILE *f;
Daniel Veillard5e873c42000-04-12 13:27:38 +00002186
William M. Brack3403add2004-06-27 02:07:51 +00002187#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2188 f = fopen(filename, "rb");
2189#else
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002190 f = fopen(filename, "r");
William M. Brack3403add2004-06-27 02:07:51 +00002191#endif
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002192 if (f != NULL) {
2193 if (rectxt == NULL)
2194 doc = xmlReadIO((xmlInputReadCallback) myRead,
2195 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +00002196 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002197 else
2198 doc = xmlCtxtReadIO(rectxt,
2199 (xmlInputReadCallback) myRead,
2200 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +00002201 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002202 } else
Daniel Veillard5e873c42000-04-12 13:27:38 +00002203 doc = NULL;
Daniel Veillard5e873c42000-04-12 13:27:38 +00002204 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002205 } else if (htmlout) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002206 xmlParserCtxtPtr ctxt;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002207
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002208 if (rectxt == NULL)
2209 ctxt = xmlNewParserCtxt();
2210 else
2211 ctxt = rectxt;
Daniel Veillardf1edb102009-08-10 14:43:18 +02002212 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002213 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +00002214 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002215 ctxt->sax->error = xmlHTMLError;
2216 ctxt->sax->warning = xmlHTMLWarning;
2217 ctxt->vctxt.error = xmlHTMLValidityError;
2218 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002219
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002220 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002221
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002222 if (rectxt == NULL)
2223 xmlFreeParserCtxt(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002224 }
Daniel Veillard46e370e2000-07-21 20:32:03 +00002225#ifdef HAVE_SYS_MMAN_H
2226 } else if (memory) {
2227 int fd;
2228 struct stat info;
2229 const char *base;
Daniel Veillardf1edb102009-08-10 14:43:18 +02002230 if (stat(filename, &info) < 0)
Daniel Veillard46e370e2000-07-21 20:32:03 +00002231 return;
2232 if ((fd = open(filename, O_RDONLY)) < 0)
2233 return;
2234 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +00002235 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +00002236 return;
2237
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002238 if (rectxt == NULL)
Daniel Veillard60942de2003-09-25 21:05:58 +00002239 doc = xmlReadMemory((char *) base, info.st_size,
2240 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002241 else
Daniel Veillard60942de2003-09-25 21:05:58 +00002242 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
2243 filename, NULL, options);
Daniel Veillardf1edb102009-08-10 14:43:18 +02002244
Daniel Veillard46e370e2000-07-21 20:32:03 +00002245 munmap((char *) base, info.st_size);
Daniel Veillardf1a27c62006-10-13 22:33:03 +00002246 close(fd);
Daniel Veillard46e370e2000-07-21 20:32:03 +00002247#endif
Daniel Veillard4432df22003-09-28 18:58:27 +00002248#ifdef LIBXML_VALID_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00002249 } else if (valid) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002250 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00002251
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002252 if (rectxt == NULL)
2253 ctxt = xmlNewParserCtxt();
2254 else
2255 ctxt = rectxt;
Daniel Veillardf1edb102009-08-10 14:43:18 +02002256 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002257 doc = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00002258 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002259 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
2260
2261 if (ctxt->valid == 0)
William M. Brack8304d872004-06-08 13:29:32 +00002262 progresult = XMLLINT_ERR_RDFILE;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002263 if (rectxt == NULL)
2264 xmlFreeParserCtxt(ctxt);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002265 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002266#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00002267 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002268 if (rectxt != NULL)
2269 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
Daniel Veillard81562d22005-06-15 13:27:56 +00002270 else {
2271#ifdef LIBXML_SAX1_ENABLED
2272 if (sax1)
2273 doc = xmlParseFile(filename);
2274 else
2275#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002276 doc = xmlReadFile(filename, NULL, options);
Daniel Veillard81562d22005-06-15 13:27:56 +00002277 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00002278 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002279 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002280
Daniel Veillard88a172f2000-08-04 18:23:10 +00002281 /*
2282 * If we don't have a document we might as well give up. Do we
2283 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002284 if (doc == NULL) {
William M. Brack8304d872004-06-08 13:29:32 +00002285 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard88a172f2000-08-04 18:23:10 +00002286 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002287 }
2288
Daniel Veillard48b2f892001-02-25 16:11:03 +00002289 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002290 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002291 }
2292
Daniel Veillard29e43992001-12-13 22:21:58 +00002293 /*
2294 * Remove DOCTYPE nodes
2295 */
2296 if (dropdtd) {
2297 xmlDtdPtr dtd;
2298
2299 dtd = xmlGetIntSubset(doc);
2300 if (dtd != NULL) {
2301 xmlUnlinkNode((xmlNodePtr)dtd);
2302 xmlFreeDtd(dtd);
2303 }
2304 }
2305
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002306#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +00002307 if (xinclude) {
2308 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002309 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002310 }
William M. Brack4e1c2db2005-02-11 10:58:55 +00002311 if (xmlXIncludeProcessFlags(doc, options) < 0)
2312 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard48b2f892001-02-25 16:11:03 +00002313 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002314 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002315 }
2316 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002317#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +00002318
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002319#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00002320#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002321 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002322 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002323 */
Daniel Veillard26a45c82006-10-20 12:55:34 +00002324 if (shell) {
2325 xmlXPathOrderDocElems(doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002326 xmlShell(doc, filename, xmlShellReadline, stdout);
Daniel Veillard26a45c82006-10-20 12:55:34 +00002327 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002328#endif
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00002329#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002330
Daniel Veillard652327a2003-09-29 18:02:38 +00002331#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002332 /*
2333 * test intermediate copy if needed.
2334 */
2335 if (copy) {
2336 tmp = doc;
Daniel Veillard4edd3ed2004-09-20 20:03:01 +00002337 if (timing) {
2338 startTimer();
2339 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002340 doc = xmlCopyDoc(doc, 1);
Daniel Veillard4edd3ed2004-09-20 20:03:01 +00002341 if (timing) {
2342 endTimer("Copying");
2343 }
2344 if (timing) {
2345 startTimer();
2346 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002347 xmlFreeDoc(tmp);
Daniel Veillard4edd3ed2004-09-20 20:03:01 +00002348 if (timing) {
2349 endTimer("Freeing original");
2350 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002351 }
Daniel Veillard652327a2003-09-29 18:02:38 +00002352#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002353
Daniel Veillard4432df22003-09-28 18:58:27 +00002354#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002355 if ((insert) && (!html)) {
2356 const xmlChar* list[256];
2357 int nb, i;
2358 xmlNodePtr node;
2359
2360 if (doc->children != NULL) {
2361 node = doc->children;
2362 while ((node != NULL) && (node->last == NULL)) node = node->next;
2363 if (node != NULL) {
2364 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
2365 if (nb < 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002366 fprintf(stderr, "could not get valid list of elements\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002367 } else if (nb == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002368 fprintf(stderr, "No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002369 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002370 fprintf(stderr, "%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002371 nb);
2372 for (i = 0;i < nb;i++) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002373 fprintf(stderr, "%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002374 }
2375 }
2376 }
Daniel Veillardf1edb102009-08-10 14:43:18 +02002377 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002378 }else
2379#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002380#ifdef LIBXML_READER_ENABLED
2381 if (walker) {
2382 walkDoc(doc);
2383 }
2384#endif /* LIBXML_READER_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002385#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00002386 if (noout == 0) {
Daniel Veillard3df01182003-12-10 10:17:51 +00002387 int ret;
2388
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002389 /*
2390 * print it.
2391 */
2392#ifdef LIBXML_DEBUG_ENABLED
2393 if (!debug) {
2394#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00002395 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002396 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002397 }
Daniel Veillard656ce942004-04-30 23:11:45 +00002398#ifdef LIBXML_HTML_ENABLED
Daniel Veillard42fd4122003-11-04 08:47:48 +00002399 if ((html) && (!xmlout)) {
2400 if (compress) {
2401 htmlSaveFile(output ? output : "-", doc);
2402 }
2403 else if (encoding != NULL) {
2404 if ( format ) {
2405 htmlSaveFileFormat(output ? output : "-", doc, encoding, 1);
2406 }
2407 else {
2408 htmlSaveFileFormat(output ? output : "-", doc, encoding, 0);
2409 }
2410 }
2411 else if (format) {
2412 htmlSaveFileFormat(output ? output : "-", doc, NULL, 1);
2413 }
2414 else {
2415 FILE *out;
2416 if (output == NULL)
2417 out = stdout;
2418 else {
2419 out = fopen(output,"wb");
2420 }
2421 if (out != NULL) {
2422 if (htmlDocDump(out, doc) < 0)
William M. Brack8304d872004-06-08 13:29:32 +00002423 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00002424
2425 if (output != NULL)
2426 fclose(out);
2427 } else {
2428 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00002429 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00002430 }
2431 }
2432 if ((timing) && (!repeat)) {
2433 endTimer("Saving");
2434 }
2435 } else
2436#endif
Daniel Veillard25048d82004-08-14 22:37:54 +00002437#ifdef LIBXML_C14N_ENABLED
2438 if (canonical) {
2439 xmlChar *result = NULL;
2440 int size;
2441
Aleksey Sanin83868242009-07-09 10:26:22 +02002442 size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_1_0, NULL, 1, &result);
2443 if (size >= 0) {
2444 write(1, result, size);
2445 xmlFree(result);
2446 } else {
2447 fprintf(stderr, "Failed to canonicalize\n");
2448 progresult = XMLLINT_ERR_OUT;
2449 }
2450 } else if (canonical) {
2451 xmlChar *result = NULL;
2452 int size;
2453
2454 size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_1_1, NULL, 1, &result);
Daniel Veillard25048d82004-08-14 22:37:54 +00002455 if (size >= 0) {
2456 write(1, result, size);
2457 xmlFree(result);
2458 } else {
2459 fprintf(stderr, "Failed to canonicalize\n");
2460 progresult = XMLLINT_ERR_OUT;
2461 }
2462 } else
Aleksey Sanin2650df12005-06-06 17:16:50 +00002463 if (exc_canonical) {
2464 xmlChar *result = NULL;
2465 int size;
2466
Aleksey Sanin83868242009-07-09 10:26:22 +02002467 size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_EXCLUSIVE_1_0, NULL, 1, &result);
Aleksey Sanin2650df12005-06-06 17:16:50 +00002468 if (size >= 0) {
2469 write(1, result, size);
2470 xmlFree(result);
2471 } else {
2472 fprintf(stderr, "Failed to canonicalize\n");
2473 progresult = XMLLINT_ERR_OUT;
2474 }
2475 } else
Daniel Veillard25048d82004-08-14 22:37:54 +00002476#endif
Daniel Veillard3b2c2612001-04-04 00:09:00 +00002477#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002478 if (memory) {
2479 xmlChar *result;
2480 int len;
2481
2482 if (encoding != NULL) {
Daniel Veillardd536f702001-11-08 17:32:47 +00002483 if ( format ) {
2484 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
Daniel Veillardf1edb102009-08-10 14:43:18 +02002485 } else {
Daniel Veillardd536f702001-11-08 17:32:47 +00002486 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
2487 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002488 } else {
Daniel Veillard90493a92001-08-14 14:12:47 +00002489 if (format)
2490 xmlDocDumpFormatMemory(doc, &result, &len, 1);
2491 else
2492 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002493 }
2494 if (result == NULL) {
2495 fprintf(stderr, "Failed to save\n");
Daniel Veillard25048d82004-08-14 22:37:54 +00002496 progresult = XMLLINT_ERR_OUT;
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002497 } else {
2498 write(1, result, len);
2499 xmlFree(result);
2500 }
Daniel Veillarddab39b52006-10-16 23:22:10 +00002501
Daniel Veillard3b2c2612001-04-04 00:09:00 +00002502 } else
2503#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002504 if (compress) {
2505 xmlSaveFile(output ? output : "-", doc);
Daniel Veillarddab39b52006-10-16 23:22:10 +00002506 } else if (oldout) {
2507 if (encoding != NULL) {
2508 if ( format ) {
2509 ret = xmlSaveFormatFileEnc(output ? output : "-", doc,
2510 encoding, 1);
2511 }
2512 else {
2513 ret = xmlSaveFileEnc(output ? output : "-", doc,
2514 encoding);
2515 }
2516 if (ret < 0) {
2517 fprintf(stderr, "failed save to %s\n",
2518 output ? output : "-");
2519 progresult = XMLLINT_ERR_OUT;
2520 }
2521 } else if (format) {
2522 ret = xmlSaveFormatFile(output ? output : "-", doc, 1);
2523 if (ret < 0) {
2524 fprintf(stderr, "failed save to %s\n",
2525 output ? output : "-");
2526 progresult = XMLLINT_ERR_OUT;
2527 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00002528 } else {
Daniel Veillarddab39b52006-10-16 23:22:10 +00002529 FILE *out;
2530 if (output == NULL)
2531 out = stdout;
2532 else {
2533 out = fopen(output,"wb");
2534 }
2535 if (out != NULL) {
2536 if (xmlDocDump(out, doc) < 0)
2537 progresult = XMLLINT_ERR_OUT;
2538
2539 if (output != NULL)
2540 fclose(out);
2541 } else {
2542 fprintf(stderr, "failed to open %s\n", output);
2543 progresult = XMLLINT_ERR_OUT;
2544 }
2545 }
2546 } else {
2547 xmlSaveCtxtPtr ctxt;
2548 int saveOpts = 0;
2549
2550 if (format)
2551 saveOpts |= XML_SAVE_FORMAT;
Daniel Veillard9d962642009-08-23 15:31:18 +02002552 if (xmlout)
2553 saveOpts |= XML_SAVE_AS_XML;
Daniel Veillarddab39b52006-10-16 23:22:10 +00002554
2555 if (output == NULL)
2556 ctxt = xmlSaveToFd(1, encoding, saveOpts);
2557 else
2558 ctxt = xmlSaveToFilename(output, encoding, saveOpts);
2559
2560 if (ctxt != NULL) {
2561 if (xmlSaveDoc(ctxt, doc) < 0) {
2562 fprintf(stderr, "failed save to %s\n",
2563 output ? output : "-");
2564 progresult = XMLLINT_ERR_OUT;
2565 }
2566 xmlSaveClose(ctxt);
2567 } else {
William M. Brack8304d872004-06-08 13:29:32 +00002568 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00002569 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002570 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002571 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002572 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002573 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002574#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002575 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002576 FILE *out;
2577 if (output == NULL)
2578 out = stdout;
2579 else {
2580 out = fopen(output,"wb");
2581 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00002582 if (out != NULL) {
2583 xmlDebugDumpDocument(out, doc);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002584
Daniel Veillard05d987b2003-10-08 11:54:57 +00002585 if (output != NULL)
2586 fclose(out);
2587 } else {
2588 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00002589 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00002590 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002591 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002592#endif
2593 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002594#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002595
Daniel Veillard4432df22003-09-28 18:58:27 +00002596#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002597 /*
2598 * A posteriori validation test
2599 */
Daniel Veillard66f68e72003-08-18 16:39:51 +00002600 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
Daniel Veillardcd429612000-10-11 15:57:05 +00002601 xmlDtdPtr dtd;
2602
Daniel Veillard48b2f892001-02-25 16:11:03 +00002603 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002604 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002605 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00002606 if (dtdvalid != NULL)
Daniel Veillardf1edb102009-08-10 14:43:18 +02002607 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
Daniel Veillard66f68e72003-08-18 16:39:51 +00002608 else
Daniel Veillardf1edb102009-08-10 14:43:18 +02002609 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002610 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002611 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002612 }
Daniel Veillardcd429612000-10-11 15:57:05 +00002613 if (dtd == NULL) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00002614 if (dtdvalid != NULL)
2615 xmlGenericError(xmlGenericErrorContext,
2616 "Could not parse DTD %s\n", dtdvalid);
2617 else
2618 xmlGenericError(xmlGenericErrorContext,
2619 "Could not parse DTD %s\n", dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00002620 progresult = XMLLINT_ERR_DTD;
Daniel Veillardcd429612000-10-11 15:57:05 +00002621 } else {
Daniel Veillarda37aab82003-06-09 09:10:36 +00002622 xmlValidCtxtPtr cvp;
2623
2624 if ((cvp = xmlNewValidCtxt()) == NULL) {
2625 xmlGenericError(xmlGenericErrorContext,
2626 "Couldn't allocate validation context\n");
2627 exit(-1);
2628 }
2629 cvp->userData = (void *) stderr;
2630 cvp->error = (xmlValidityErrorFunc) fprintf;
2631 cvp->warning = (xmlValidityWarningFunc) fprintf;
2632
Daniel Veillard48b2f892001-02-25 16:11:03 +00002633 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002634 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002635 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002636 if (!xmlValidateDtd(cvp, doc, dtd)) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00002637 if (dtdvalid != NULL)
2638 xmlGenericError(xmlGenericErrorContext,
2639 "Document %s does not validate against %s\n",
2640 filename, dtdvalid);
2641 else
2642 xmlGenericError(xmlGenericErrorContext,
2643 "Document %s does not validate against %s\n",
2644 filename, dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00002645 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00002646 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002647 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002648 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002649 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002650 xmlFreeValidCtxt(cvp);
Daniel Veillardcd429612000-10-11 15:57:05 +00002651 xmlFreeDtd(dtd);
2652 }
2653 } else if (postvalid) {
Daniel Veillarda37aab82003-06-09 09:10:36 +00002654 xmlValidCtxtPtr cvp;
2655
2656 if ((cvp = xmlNewValidCtxt()) == NULL) {
2657 xmlGenericError(xmlGenericErrorContext,
2658 "Couldn't allocate validation context\n");
2659 exit(-1);
2660 }
2661
Daniel Veillard48b2f892001-02-25 16:11:03 +00002662 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002663 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002664 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002665 cvp->userData = (void *) stderr;
2666 cvp->error = (xmlValidityErrorFunc) fprintf;
2667 cvp->warning = (xmlValidityWarningFunc) fprintf;
2668 if (!xmlValidateDocument(cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00002669 xmlGenericError(xmlGenericErrorContext,
2670 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00002671 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00002672 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002673 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002674 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002675 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002676 xmlFreeValidCtxt(cvp);
Daniel Veillard4432df22003-09-28 18:58:27 +00002677 }
2678#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardd4501d72005-07-24 14:27:16 +00002679#ifdef LIBXML_SCHEMATRON_ENABLED
2680 if (wxschematron != NULL) {
2681 xmlSchematronValidCtxtPtr ctxt;
2682 int ret;
Daniel Veillardc740a172005-07-31 12:17:24 +00002683 int flag;
Daniel Veillardd4501d72005-07-24 14:27:16 +00002684
2685 if ((timing) && (!repeat)) {
2686 startTimer();
2687 }
2688
2689 if (debug)
2690 flag = XML_SCHEMATRON_OUT_XML;
Daniel Veillardc740a172005-07-31 12:17:24 +00002691 else
2692 flag = XML_SCHEMATRON_OUT_TEXT;
2693 if (noout)
2694 flag |= XML_SCHEMATRON_OUT_QUIET;
Daniel Veillardd4501d72005-07-24 14:27:16 +00002695 ctxt = xmlSchematronNewValidCtxt(wxschematron, flag);
2696#if 0
2697 xmlSchematronSetValidErrors(ctxt,
2698 (xmlSchematronValidityErrorFunc) fprintf,
2699 (xmlSchematronValidityWarningFunc) fprintf,
2700 stderr);
2701#endif
2702 ret = xmlSchematronValidateDoc(ctxt, doc);
2703 if (ret == 0) {
2704 fprintf(stderr, "%s validates\n", filename);
2705 } else if (ret > 0) {
2706 fprintf(stderr, "%s fails to validate\n", filename);
2707 progresult = XMLLINT_ERR_VALID;
2708 } else {
2709 fprintf(stderr, "%s validation generated an internal error\n",
2710 filename);
2711 progresult = XMLLINT_ERR_VALID;
2712 }
2713 xmlSchematronFreeValidCtxt(ctxt);
2714 if ((timing) && (!repeat)) {
2715 endTimer("Validating");
2716 }
2717 }
2718#endif
Daniel Veillard71531f32003-02-05 13:19:53 +00002719#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00002720 if (relaxngschemas != NULL) {
Daniel Veillard71531f32003-02-05 13:19:53 +00002721 xmlRelaxNGValidCtxtPtr ctxt;
2722 int ret;
2723
Daniel Veillard42f12e92003-03-07 18:32:59 +00002724 if ((timing) && (!repeat)) {
2725 startTimer();
2726 }
2727
Daniel Veillard71531f32003-02-05 13:19:53 +00002728 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2729 xmlRelaxNGSetValidErrors(ctxt,
2730 (xmlRelaxNGValidityErrorFunc) fprintf,
2731 (xmlRelaxNGValidityWarningFunc) fprintf,
2732 stderr);
2733 ret = xmlRelaxNGValidateDoc(ctxt, doc);
2734 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002735 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard71531f32003-02-05 13:19:53 +00002736 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002737 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00002738 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00002739 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002740 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard71531f32003-02-05 13:19:53 +00002741 filename);
William M. Brack8304d872004-06-08 13:29:32 +00002742 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00002743 }
2744 xmlRelaxNGFreeValidCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002745 if ((timing) && (!repeat)) {
2746 endTimer("Validating");
2747 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002748 } else if (wxschemas != NULL) {
2749 xmlSchemaValidCtxtPtr ctxt;
2750 int ret;
2751
2752 if ((timing) && (!repeat)) {
2753 startTimer();
2754 }
2755
2756 ctxt = xmlSchemaNewValidCtxt(wxschemas);
2757 xmlSchemaSetValidErrors(ctxt,
2758 (xmlSchemaValidityErrorFunc) fprintf,
2759 (xmlSchemaValidityWarningFunc) fprintf,
2760 stderr);
2761 ret = xmlSchemaValidateDoc(ctxt, doc);
2762 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002763 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002764 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002765 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00002766 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002767 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002768 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002769 filename);
William M. Brack8304d872004-06-08 13:29:32 +00002770 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002771 }
2772 xmlSchemaFreeValidCtxt(ctxt);
2773 if ((timing) && (!repeat)) {
2774 endTimer("Validating");
2775 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002776 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002777#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002778
2779#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard6b099012008-11-06 13:47:39 +00002780#if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002781 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00002782 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002783#endif
Daniel Veillard6b099012008-11-06 13:47:39 +00002784#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002785
2786 /*
2787 * free it.
2788 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00002789 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002790 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002791 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002792 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002793 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002794 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002795 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002796}
2797
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002798/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +02002799 * *
2800 * Usage and Main *
2801 * *
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002802 ************************************************************************/
2803
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00002804static void showVersion(const char *name) {
2805 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
2806 fprintf(stderr, " compiled with: ");
Daniel Veillard602434d2005-09-12 09:20:31 +00002807 if (xmlHasFeature(XML_WITH_THREAD)) fprintf(stderr, "Threads ");
2808 if (xmlHasFeature(XML_WITH_TREE)) fprintf(stderr, "Tree ");
2809 if (xmlHasFeature(XML_WITH_OUTPUT)) fprintf(stderr, "Output ");
2810 if (xmlHasFeature(XML_WITH_PUSH)) fprintf(stderr, "Push ");
2811 if (xmlHasFeature(XML_WITH_READER)) fprintf(stderr, "Reader ");
2812 if (xmlHasFeature(XML_WITH_PATTERN)) fprintf(stderr, "Patterns ");
2813 if (xmlHasFeature(XML_WITH_WRITER)) fprintf(stderr, "Writer ");
2814 if (xmlHasFeature(XML_WITH_SAX1)) fprintf(stderr, "SAXv1 ");
Daniel Veillardf1edb102009-08-10 14:43:18 +02002815 if (xmlHasFeature(XML_WITH_FTP)) fprintf(stderr, "FTP ");
2816 if (xmlHasFeature(XML_WITH_HTTP)) fprintf(stderr, "HTTP ");
Daniel Veillard602434d2005-09-12 09:20:31 +00002817 if (xmlHasFeature(XML_WITH_VALID)) fprintf(stderr, "DTDValid ");
Daniel Veillardf1edb102009-08-10 14:43:18 +02002818 if (xmlHasFeature(XML_WITH_HTML)) fprintf(stderr, "HTML ");
2819 if (xmlHasFeature(XML_WITH_LEGACY)) fprintf(stderr, "Legacy ");
2820 if (xmlHasFeature(XML_WITH_C14N)) fprintf(stderr, "C14N ");
2821 if (xmlHasFeature(XML_WITH_CATALOG)) fprintf(stderr, "Catalog ");
2822 if (xmlHasFeature(XML_WITH_XPATH)) fprintf(stderr, "XPath ");
2823 if (xmlHasFeature(XML_WITH_XPTR)) fprintf(stderr, "XPointer ");
2824 if (xmlHasFeature(XML_WITH_XINCLUDE)) fprintf(stderr, "XInclude ");
2825 if (xmlHasFeature(XML_WITH_ICONV)) fprintf(stderr, "Iconv ");
2826 if (xmlHasFeature(XML_WITH_ISO8859X)) fprintf(stderr, "ISO8859X ");
2827 if (xmlHasFeature(XML_WITH_UNICODE)) fprintf(stderr, "Unicode ");
2828 if (xmlHasFeature(XML_WITH_REGEXP)) fprintf(stderr, "Regexps ");
2829 if (xmlHasFeature(XML_WITH_AUTOMATA)) fprintf(stderr, "Automata ");
2830 if (xmlHasFeature(XML_WITH_EXPR)) fprintf(stderr, "Expr ");
2831 if (xmlHasFeature(XML_WITH_SCHEMAS)) fprintf(stderr, "Schemas ");
2832 if (xmlHasFeature(XML_WITH_SCHEMATRON)) fprintf(stderr, "Schematron ");
2833 if (xmlHasFeature(XML_WITH_MODULES)) fprintf(stderr, "Modules ");
2834 if (xmlHasFeature(XML_WITH_DEBUG)) fprintf(stderr, "Debug ");
2835 if (xmlHasFeature(XML_WITH_DEBUG_MEM)) fprintf(stderr, "MemDebug ");
2836 if (xmlHasFeature(XML_WITH_DEBUG_RUN)) fprintf(stderr, "RunDebug ");
Daniel Veillard75acfee2006-07-13 06:29:56 +00002837 if (xmlHasFeature(XML_WITH_ZLIB)) fprintf(stderr, "Zlib ");
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00002838 fprintf(stderr, "\n");
2839}
2840
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002841static void usage(const char *name) {
2842 printf("Usage : %s [options] XMLfiles ...\n", name);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002843#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002844 printf("\tParse the XML files and output the result of the parsing\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002845#else
2846 printf("\tParse the XML files\n");
2847#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002848 printf("\t--version : display the version of the XML library used\n");
2849#ifdef LIBXML_DEBUG_ENABLED
2850 printf("\t--debug : dump a debug tree of the in-memory document\n");
2851 printf("\t--shell : run a navigating shell\n");
2852 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00002853#else
Daniel Veillard81273902003-09-30 00:43:48 +00002854#ifdef LIBXML_READER_ENABLED
Daniel Veillard8326e732003-01-07 00:19:07 +00002855 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard81273902003-09-30 00:43:48 +00002856#endif /* LIBXML_READER_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002857#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00002858#ifdef LIBXML_TREE_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002859 printf("\t--copy : used to test the internal copy implementation\n");
Daniel Veillard652327a2003-09-29 18:02:38 +00002860#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002861 printf("\t--recover : output what was parsable on broken XML documents\n");
Daniel Veillard8915c152008-08-26 13:05:34 +00002862 printf("\t--huge : remove any internal arbitrary parser limits\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002863 printf("\t--noent : substitute entity references by their value\n");
2864 printf("\t--noout : don't output the result tree\n");
Daniel Veillard0bff36d2004-08-31 09:37:03 +00002865 printf("\t--path 'paths': provide a set of paths for resources\n");
2866 printf("\t--load-trace : print trace of all external entites loaded\n");
Daniel Veillarde8b09e42003-05-13 22:14:13 +00002867 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
Daniel Veillard8874b942005-08-25 13:19:21 +00002868 printf("\t--nocompact : do not generate compact text nodes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002869 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00002870 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00002871#ifdef LIBXML_VALID_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002872 printf("\t--valid : validate the document in addition to std well-formed check\n");
2873 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
2874 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
Daniel Veillard66f68e72003-08-18 16:39:51 +00002875 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00002876#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002877 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002878 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002879 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
2880 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002881#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002882#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002883 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002884#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002885#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002886#ifdef LIBXML_HTML_ENABLED
2887 printf("\t--html : use the HTML parser\n");
Daniel Veillard42fd4122003-11-04 08:47:48 +00002888 printf("\t--xmlout : force to use the XML serializer when using --html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002889#endif
Daniel Veillard73b013f2003-09-30 12:36:01 +00002890#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002891 printf("\t--push : use the push mode of the parser\n");
Daniel Veillard73b013f2003-09-30 12:36:01 +00002892#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002893#ifdef HAVE_SYS_MMAN_H
2894 printf("\t--memory : parse from memory\n");
2895#endif
Daniel Veillard87076042004-05-03 22:54:49 +00002896 printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002897 printf("\t--nowarning : do not emit warnings from parser/validator\n");
2898 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillarddca8cc72003-09-26 13:53:14 +00002899 printf("\t--nocdata : replace cdata section with text nodes\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002900#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00002901 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002902 printf("\t--encode encoding : output in the given encoding\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002903 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
2904#endif /* LIBXML_OUTPUT_ENABLED */
Aleksey Sanin83868242009-07-09 10:26:22 +02002905 printf("\t--c14n : save in W3C canonical format v1.0 (with comments)\n");
2906 printf("\t--c14n11 : save in W3C canonical format v1.1 (with comments)\n");
Aleksey Sanin2650df12005-06-06 17:16:50 +00002907 printf("\t--exc-c14n : save in W3C exclusive canonical format (with comments)\n");
Daniel Veillard25048d82004-08-14 22:37:54 +00002908#ifdef LIBXML_C14N_ENABLED
2909#endif /* LIBXML_C14N_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002910 printf("\t--nsclean : remove redundant namespace declarations\n");
2911 printf("\t--testIO : test user I/O support\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002912#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00002913 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
2914 printf("\t otherwise XML Catalogs starting from \n");
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002915 printf("\t %s are activated by default\n", XML_XML_DEFAULT_CATALOG);
Daniel Veillard05c13a22001-09-09 08:38:09 +00002916 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002917#endif
2918 printf("\t--auto : generate a small doc on the fly\n");
2919#ifdef LIBXML_XINCLUDE_ENABLED
2920 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00002921 printf("\t--noxincludenode : same but do not generate XInclude nodes\n");
Daniel Veillard54bd29b2008-08-26 07:26:55 +00002922 printf("\t--nofixup-base-uris : do not fixup xml:base uris\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002923#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002924 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00002925 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard81273902003-09-30 00:43:48 +00002926#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00002927 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002928 printf("\t--walker : create a reader and walk though the resulting doc\n");
Daniel Veillard81273902003-09-30 00:43:48 +00002929#endif /* LIBXML_READER_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002930#ifdef LIBXML_PATTERN_ENABLED
2931 printf("\t--pattern pattern_value : test the pattern support\n");
2932#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002933 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillardef4d3bc2003-02-07 12:38:22 +00002934#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard71531f32003-02-05 13:19:53 +00002935 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002936 printf("\t--schema schema : do validation against the WXS schema\n");
Daniel Veillard71531f32003-02-05 13:19:53 +00002937#endif
Daniel Veillarde70375c2005-07-30 21:09:12 +00002938#ifdef LIBXML_SCHEMATRON_ENABLED
2939 printf("\t--schematron schema : do validation against a schematron\n");
2940#endif
Daniel Veillard971771e2005-07-09 17:32:57 +00002941#ifdef LIBXML_SAX1_ENABLED
2942 printf("\t--sax1: use the old SAX1 interfaces for processing\n");
2943#endif
2944 printf("\t--sax: do not build a tree but work just at the SAX level\n");
Daniel Veillard7e5c3f42008-07-29 16:12:31 +00002945 printf("\t--oldxml10: use XML-1.0 parsing rules before the 5th edition\n");
Daniel Veillard971771e2005-07-09 17:32:57 +00002946
Daniel Veillarda42f25f2002-01-25 14:15:40 +00002947 printf("\nLibxml project home page: http://xmlsoft.org/\n");
2948 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002949}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002950
2951static void registerNode(xmlNodePtr node)
2952{
2953 node->_private = malloc(sizeof(long));
2954 *(long*)node->_private = (long) 0x81726354;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002955 nbregister++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002956}
2957
2958static void deregisterNode(xmlNodePtr node)
2959{
2960 assert(node->_private != NULL);
2961 assert(*(long*)node->_private == (long) 0x81726354);
2962 free(node->_private);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002963 nbregister--;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002964}
2965
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002966int
2967main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00002968 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002969 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00002970 int version = 0;
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00002971 const char* indent;
Daniel Veillardf1edb102009-08-10 14:43:18 +02002972
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002973 if (argc <= 1) {
2974 usage(argv[0]);
2975 return(1);
2976 }
Daniel Veillardbe803962000-06-28 23:40:59 +00002977 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002978 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002979 if (!strcmp(argv[i], "-"))
2980 break;
2981
2982 if (argv[i][0] != '-')
2983 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002984 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
2985 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00002986 else
2987#ifdef LIBXML_DEBUG_ENABLED
2988 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002989 (!strcmp(argv[i], "--shell"))) {
2990 shell++;
2991 noout = 1;
Daniel Veillardf1edb102009-08-10 14:43:18 +02002992 } else
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002993#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00002994#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002995 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
2996 copy++;
Daniel Veillard652327a2003-09-29 18:02:38 +00002997 else
2998#endif /* LIBXML_TREE_ENABLED */
2999 if ((!strcmp(argv[i], "-recover")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003000 (!strcmp(argv[i], "--recover"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003001 recovery++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003002 options |= XML_PARSE_RECOVER;
Daniel Veillard8915c152008-08-26 13:05:34 +00003003 } else if ((!strcmp(argv[i], "-huge")) ||
3004 (!strcmp(argv[i], "--huge"))) {
3005 options |= XML_PARSE_HUGE;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003006 } else if ((!strcmp(argv[i], "-noent")) ||
3007 (!strcmp(argv[i], "--noent"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003008 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003009 options |= XML_PARSE_NOENT;
Daniel Veillarddca8cc72003-09-26 13:53:14 +00003010 } else if ((!strcmp(argv[i], "-nsclean")) ||
3011 (!strcmp(argv[i], "--nsclean"))) {
3012 options |= XML_PARSE_NSCLEAN;
3013 } else if ((!strcmp(argv[i], "-nocdata")) ||
3014 (!strcmp(argv[i], "--nocdata"))) {
3015 options |= XML_PARSE_NOCDATA;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003016 } else if ((!strcmp(argv[i], "-nodict")) ||
3017 (!strcmp(argv[i], "--nodict"))) {
3018 options |= XML_PARSE_NODICT;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003019 } else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00003020 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00003021 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00003022 version = 1;
3023 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003024 (!strcmp(argv[i], "--noout")))
3025 noout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003026#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003027 else if ((!strcmp(argv[i], "-o")) ||
3028 (!strcmp(argv[i], "-output")) ||
3029 (!strcmp(argv[i], "--output"))) {
3030 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00003031 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003032 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003033#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003034 else if ((!strcmp(argv[i], "-htmlout")) ||
3035 (!strcmp(argv[i], "--htmlout")))
3036 htmlout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003037 else if ((!strcmp(argv[i], "-nowrap")) ||
3038 (!strcmp(argv[i], "--nowrap")))
3039 nowrap++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003040#ifdef LIBXML_HTML_ENABLED
3041 else if ((!strcmp(argv[i], "-html")) ||
3042 (!strcmp(argv[i], "--html"))) {
3043 html++;
3044 }
Daniel Veillard42fd4122003-11-04 08:47:48 +00003045 else if ((!strcmp(argv[i], "-xmlout")) ||
3046 (!strcmp(argv[i], "--xmlout"))) {
3047 xmlout++;
3048 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003049#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003050 else if ((!strcmp(argv[i], "-loaddtd")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003051 (!strcmp(argv[i], "--loaddtd"))) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003052 loaddtd++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003053 options |= XML_PARSE_DTDLOAD;
3054 } else if ((!strcmp(argv[i], "-dtdattr")) ||
Daniel Veillard48da9102001-08-07 01:10:10 +00003055 (!strcmp(argv[i], "--dtdattr"))) {
3056 loaddtd++;
3057 dtdattrs++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003058 options |= XML_PARSE_DTDATTR;
Daniel Veillard4432df22003-09-28 18:58:27 +00003059 }
3060#ifdef LIBXML_VALID_ENABLED
3061 else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003062 (!strcmp(argv[i], "--valid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003063 valid++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003064 options |= XML_PARSE_DTDVALID;
3065 } else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003066 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003067 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003068 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00003069 options |= XML_PARSE_DTDLOAD;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003070 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00003071 (!strcmp(argv[i], "--dtdvalid"))) {
3072 i++;
3073 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003074 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00003075 options |= XML_PARSE_DTDLOAD;
Daniel Veillard66f68e72003-08-18 16:39:51 +00003076 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
3077 (!strcmp(argv[i], "--dtdvalidfpi"))) {
3078 i++;
3079 dtdvalidfpi = argv[i];
3080 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00003081 options |= XML_PARSE_DTDLOAD;
Daniel Veillardcd429612000-10-11 15:57:05 +00003082 }
Daniel Veillard4432df22003-09-28 18:58:27 +00003083#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard29e43992001-12-13 22:21:58 +00003084 else if ((!strcmp(argv[i], "-dropdtd")) ||
3085 (!strcmp(argv[i], "--dropdtd")))
3086 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003087 else if ((!strcmp(argv[i], "-insert")) ||
3088 (!strcmp(argv[i], "--insert")))
3089 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003090 else if ((!strcmp(argv[i], "-timing")) ||
3091 (!strcmp(argv[i], "--timing")))
3092 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00003093 else if ((!strcmp(argv[i], "-auto")) ||
3094 (!strcmp(argv[i], "--auto")))
3095 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003096 else if ((!strcmp(argv[i], "-repeat")) ||
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00003097 (!strcmp(argv[i], "--repeat"))) {
3098 if (repeat)
3099 repeat *= 10;
3100 else
3101 repeat = 100;
Daniel Veillard73b013f2003-09-30 12:36:01 +00003102 }
3103#ifdef LIBXML_PUSH_ENABLED
3104 else if ((!strcmp(argv[i], "-push")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003105 (!strcmp(argv[i], "--push")))
3106 push++;
Daniel Veillard73b013f2003-09-30 12:36:01 +00003107#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +00003108#ifdef HAVE_SYS_MMAN_H
3109 else if ((!strcmp(argv[i], "-memory")) ||
3110 (!strcmp(argv[i], "--memory")))
3111 memory++;
3112#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00003113 else if ((!strcmp(argv[i], "-testIO")) ||
3114 (!strcmp(argv[i], "--testIO")))
3115 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00003116#ifdef LIBXML_XINCLUDE_ENABLED
3117 else if ((!strcmp(argv[i], "-xinclude")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003118 (!strcmp(argv[i], "--xinclude"))) {
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00003119 xinclude++;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00003120 options |= XML_PARSE_XINCLUDE;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003121 }
Daniel Veillardc14c3892004-08-16 12:34:50 +00003122 else if ((!strcmp(argv[i], "-noxincludenode")) ||
3123 (!strcmp(argv[i], "--noxincludenode"))) {
3124 xinclude++;
3125 options |= XML_PARSE_XINCLUDE;
3126 options |= XML_PARSE_NOXINCNODE;
3127 }
Daniel Veillard54bd29b2008-08-26 07:26:55 +00003128 else if ((!strcmp(argv[i], "-nofixup-base-uris")) ||
3129 (!strcmp(argv[i], "--nofixup-base-uris"))) {
3130 xinclude++;
3131 options |= XML_PARSE_XINCLUDE;
3132 options |= XML_PARSE_NOBASEFIX;
3133 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00003134#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003135#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003136#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003137 else if ((!strcmp(argv[i], "-compress")) ||
3138 (!strcmp(argv[i], "--compress"))) {
3139 compress++;
3140 xmlSetCompressMode(9);
3141 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003142#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003143#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003144 else if ((!strcmp(argv[i], "-nowarning")) ||
3145 (!strcmp(argv[i], "--nowarning"))) {
3146 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003147 xmlPedanticParserDefault(0);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003148 options |= XML_PARSE_NOWARNING;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003149 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003150 else if ((!strcmp(argv[i], "-pedantic")) ||
3151 (!strcmp(argv[i], "--pedantic"))) {
3152 xmlGetWarningsDefaultValue = 1;
3153 xmlPedanticParserDefault(1);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003154 options |= XML_PARSE_PEDANTIC;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003155 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00003156#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003157 else if ((!strcmp(argv[i], "-debugent")) ||
3158 (!strcmp(argv[i], "--debugent"))) {
3159 debugent++;
3160 xmlParserDebugEntities = 1;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003161 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00003162#endif
Daniel Veillard25048d82004-08-14 22:37:54 +00003163#ifdef LIBXML_C14N_ENABLED
3164 else if ((!strcmp(argv[i], "-c14n")) ||
3165 (!strcmp(argv[i], "--c14n"))) {
3166 canonical++;
3167 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003168 }
Aleksey Sanin83868242009-07-09 10:26:22 +02003169 else if ((!strcmp(argv[i], "-c14n11")) ||
3170 (!strcmp(argv[i], "--c14n11"))) {
3171 canonical_11++;
3172 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003173 }
Aleksey Sanin2650df12005-06-06 17:16:50 +00003174 else if ((!strcmp(argv[i], "-exc-c14n")) ||
3175 (!strcmp(argv[i], "--exc-c14n"))) {
3176 exc_canonical++;
3177 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003178 }
Daniel Veillard25048d82004-08-14 22:37:54 +00003179#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00003180#ifdef LIBXML_CATALOG_ENABLED
3181 else if ((!strcmp(argv[i], "-catalogs")) ||
3182 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003183 catalogs++;
3184 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
3185 (!strcmp(argv[i], "--nocatalogs"))) {
3186 nocatalogs++;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003187 }
Daniel Veillard81418e32001-05-22 15:08:55 +00003188#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00003189 else if ((!strcmp(argv[i], "-encode")) ||
3190 (!strcmp(argv[i], "--encode"))) {
3191 i++;
3192 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003193 /*
3194 * OK it's for testing purposes
3195 */
3196 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00003197 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003198 else if ((!strcmp(argv[i], "-noblanks")) ||
3199 (!strcmp(argv[i], "--noblanks"))) {
3200 noblanks++;
3201 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00003202 }
Daniel Veillard87076042004-05-03 22:54:49 +00003203 else if ((!strcmp(argv[i], "-maxmem")) ||
3204 (!strcmp(argv[i], "--maxmem"))) {
3205 i++;
3206 if (sscanf(argv[i], "%d", &maxmem) == 1) {
3207 xmlMemSetup(myFreeFunc, myMallocFunc, myReallocFunc,
3208 myStrdupFunc);
3209 } else {
3210 maxmem = 0;
3211 }
3212 }
Daniel Veillard90493a92001-08-14 14:12:47 +00003213 else if ((!strcmp(argv[i], "-format")) ||
3214 (!strcmp(argv[i], "--format"))) {
3215 noblanks++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003216#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00003217 format++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003218#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard90493a92001-08-14 14:12:47 +00003219 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00003220 }
Daniel Veillard81273902003-09-30 00:43:48 +00003221#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00003222 else if ((!strcmp(argv[i], "-stream")) ||
3223 (!strcmp(argv[i], "--stream"))) {
3224 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003225 }
Daniel Veillard7899c5c2003-11-03 12:31:38 +00003226 else if ((!strcmp(argv[i], "-walker")) ||
3227 (!strcmp(argv[i], "--walker"))) {
3228 walker++;
3229 noout++;
3230 }
Daniel Veillard81273902003-09-30 00:43:48 +00003231#endif /* LIBXML_READER_ENABLED */
3232#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00003233 else if ((!strcmp(argv[i], "-sax1")) ||
3234 (!strcmp(argv[i], "--sax1"))) {
Daniel Veillard023d0ba2009-07-29 11:34:50 +02003235 sax1++;
3236 options |= XML_PARSE_SAX1;
Daniel Veillard07cb8222003-09-10 10:51:05 +00003237 }
Daniel Veillard81273902003-09-30 00:43:48 +00003238#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003239 else if ((!strcmp(argv[i], "-sax")) ||
3240 (!strcmp(argv[i], "--sax"))) {
Daniel Veillard023d0ba2009-07-29 11:34:50 +02003241 sax++;
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003242 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003243 else if ((!strcmp(argv[i], "-chkregister")) ||
3244 (!strcmp(argv[i], "--chkregister"))) {
Daniel Veillard023d0ba2009-07-29 11:34:50 +02003245 chkregister++;
Daniel Veillard71531f32003-02-05 13:19:53 +00003246#ifdef LIBXML_SCHEMAS_ENABLED
3247 } else if ((!strcmp(argv[i], "-relaxng")) ||
3248 (!strcmp(argv[i], "--relaxng"))) {
3249 i++;
3250 relaxng = argv[i];
3251 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003252 options |= XML_PARSE_NOENT;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003253 } else if ((!strcmp(argv[i], "-schema")) ||
3254 (!strcmp(argv[i], "--schema"))) {
3255 i++;
3256 schema = argv[i];
3257 noent++;
Daniel Veillard71531f32003-02-05 13:19:53 +00003258#endif
Daniel Veillardd4501d72005-07-24 14:27:16 +00003259#ifdef LIBXML_SCHEMATRON_ENABLED
3260 } else if ((!strcmp(argv[i], "-schematron")) ||
3261 (!strcmp(argv[i], "--schematron"))) {
3262 i++;
3263 schematron = argv[i];
3264 noent++;
3265#endif
Daniel Veillarde8b09e42003-05-13 22:14:13 +00003266 } else if ((!strcmp(argv[i], "-nonet")) ||
3267 (!strcmp(argv[i], "--nonet"))) {
Daniel Veillard61b93382003-11-03 14:28:31 +00003268 options |= XML_PARSE_NONET;
Daniel Veillard968d6432006-04-25 16:17:53 +00003269 xmlSetExternalEntityLoader(xmlNoNetExternalEntityLoader);
Daniel Veillard8874b942005-08-25 13:19:21 +00003270 } else if ((!strcmp(argv[i], "-nocompact")) ||
3271 (!strcmp(argv[i], "--nocompact"))) {
3272 options &= ~XML_PARSE_COMPACT;
Daniel Veillard0bff36d2004-08-31 09:37:03 +00003273 } else if ((!strcmp(argv[i], "-load-trace")) ||
3274 (!strcmp(argv[i], "--load-trace"))) {
3275 load_trace++;
3276 } else if ((!strcmp(argv[i], "-path")) ||
3277 (!strcmp(argv[i], "--path"))) {
3278 i++;
3279 parsePath(BAD_CAST argv[i]);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003280#ifdef LIBXML_PATTERN_ENABLED
3281 } else if ((!strcmp(argv[i], "-pattern")) ||
3282 (!strcmp(argv[i], "--pattern"))) {
3283 i++;
3284 pattern = argv[i];
3285#endif
Daniel Veillard7e5c3f42008-07-29 16:12:31 +00003286 } else if ((!strcmp(argv[i], "-oldxml10")) ||
3287 (!strcmp(argv[i], "--oldxml10"))) {
3288 oldxml10++;
3289 options |= XML_PARSE_OLD10;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003290 } else {
3291 fprintf(stderr, "Unknown option %s\n", argv[i]);
3292 usage(argv[0]);
3293 return(1);
3294 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003295 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003296
3297#ifdef LIBXML_CATALOG_ENABLED
3298 if (nocatalogs == 0) {
3299 if (catalogs) {
3300 const char *catal;
3301
3302 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003303 if (catal != NULL) {
3304 xmlLoadCatalogs(catal);
3305 } else {
3306 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
3307 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003308 }
3309 }
3310#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003311
Daniel Veillard81273902003-09-30 00:43:48 +00003312#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00003313 if (sax1)
3314 xmlSAXDefaultVersion(1);
3315 else
3316 xmlSAXDefaultVersion(2);
Daniel Veillard81273902003-09-30 00:43:48 +00003317#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00003318
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003319 if (chkregister) {
3320 xmlRegisterNodeDefault(registerNode);
3321 xmlDeregisterNodeDefault(deregisterNode);
3322 }
Daniel Veillardf1edb102009-08-10 14:43:18 +02003323
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00003324 indent = getenv("XMLLINT_INDENT");
3325 if(indent != NULL) {
3326 xmlTreeIndentString = indent;
3327 }
Daniel Veillardf1edb102009-08-10 14:43:18 +02003328
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003329
Daniel Veillard0bff36d2004-08-31 09:37:03 +00003330 defaultEntityLoader = xmlGetExternalEntityLoader();
3331 xmlSetExternalEntityLoader(xmllintExternalEntityLoader);
3332
Daniel Veillardd9bad132001-07-23 19:39:43 +00003333 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00003334 if (loaddtd != 0)
3335 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
3336 if (dtdattrs)
3337 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003338 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillard4432df22003-09-28 18:58:27 +00003339#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003340 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard4432df22003-09-28 18:58:27 +00003341#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003342 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00003343 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003344 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00003345 xmlGenericError(xmlGenericErrorContext,
3346 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
3347 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003348 "<html><head><title>%s output</title></head>\n",
3349 argv[0]);
Daniel Veillardf1edb102009-08-10 14:43:18 +02003350 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003351 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
3352 argv[0]);
3353 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003354
Daniel Veillardd4501d72005-07-24 14:27:16 +00003355#ifdef LIBXML_SCHEMATRON_ENABLED
3356 if ((schematron != NULL) && (sax == 0)
3357#ifdef LIBXML_READER_ENABLED
3358 && (stream == 0)
3359#endif /* LIBXML_READER_ENABLED */
3360 ) {
3361 xmlSchematronParserCtxtPtr ctxt;
3362
3363 /* forces loading the DTDs */
Daniel Veillardf1edb102009-08-10 14:43:18 +02003364 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillardd4501d72005-07-24 14:27:16 +00003365 options |= XML_PARSE_DTDLOAD;
3366 if (timing) {
3367 startTimer();
3368 }
3369 ctxt = xmlSchematronNewParserCtxt(schematron);
3370#if 0
3371 xmlSchematronSetParserErrors(ctxt,
3372 (xmlSchematronValidityErrorFunc) fprintf,
3373 (xmlSchematronValidityWarningFunc) fprintf,
3374 stderr);
3375#endif
3376 wxschematron = xmlSchematronParse(ctxt);
3377 if (wxschematron == NULL) {
3378 xmlGenericError(xmlGenericErrorContext,
3379 "Schematron schema %s failed to compile\n", schematron);
3380 progresult = XMLLINT_ERR_SCHEMACOMP;
3381 schematron = NULL;
3382 }
3383 xmlSchematronFreeParserCtxt(ctxt);
3384 if (timing) {
3385 endTimer("Compiling the schemas");
3386 }
3387 }
3388#endif
Daniel Veillard71531f32003-02-05 13:19:53 +00003389#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003390 if ((relaxng != NULL) && (sax == 0)
Daniel Veillard81273902003-09-30 00:43:48 +00003391#ifdef LIBXML_READER_ENABLED
3392 && (stream == 0)
3393#endif /* LIBXML_READER_ENABLED */
3394 ) {
Daniel Veillard71531f32003-02-05 13:19:53 +00003395 xmlRelaxNGParserCtxtPtr ctxt;
3396
Daniel Veillardce192eb2003-04-16 15:58:05 +00003397 /* forces loading the DTDs */
Daniel Veillardf1edb102009-08-10 14:43:18 +02003398 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003399 options |= XML_PARSE_DTDLOAD;
Daniel Veillard42f12e92003-03-07 18:32:59 +00003400 if (timing) {
3401 startTimer();
3402 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003403 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
3404 xmlRelaxNGSetParserErrors(ctxt,
3405 (xmlRelaxNGValidityErrorFunc) fprintf,
3406 (xmlRelaxNGValidityWarningFunc) fprintf,
3407 stderr);
3408 relaxngschemas = xmlRelaxNGParse(ctxt);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003409 if (relaxngschemas == NULL) {
3410 xmlGenericError(xmlGenericErrorContext,
3411 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +00003412 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +00003413 relaxng = NULL;
3414 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003415 xmlRelaxNGFreeParserCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00003416 if (timing) {
3417 endTimer("Compiling the schemas");
3418 }
Daniel Veillardebe25d42004-03-25 09:35:49 +00003419 } else if ((schema != NULL)
3420#ifdef LIBXML_READER_ENABLED
Daniel Veillardf10ae122005-07-10 19:03:16 +00003421 && (stream == 0)
Daniel Veillardebe25d42004-03-25 09:35:49 +00003422#endif
3423 ) {
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003424 xmlSchemaParserCtxtPtr ctxt;
3425
3426 if (timing) {
3427 startTimer();
3428 }
3429 ctxt = xmlSchemaNewParserCtxt(schema);
3430 xmlSchemaSetParserErrors(ctxt,
3431 (xmlSchemaValidityErrorFunc) fprintf,
3432 (xmlSchemaValidityWarningFunc) fprintf,
3433 stderr);
3434 wxschemas = xmlSchemaParse(ctxt);
3435 if (wxschemas == NULL) {
3436 xmlGenericError(xmlGenericErrorContext,
3437 "WXS schema %s failed to compile\n", schema);
William M. Brack8304d872004-06-08 13:29:32 +00003438 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003439 schema = NULL;
3440 }
3441 xmlSchemaFreeParserCtxt(ctxt);
3442 if (timing) {
3443 endTimer("Compiling the schemas");
3444 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003445 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003446#endif /* LIBXML_SCHEMAS_ENABLED */
3447#ifdef LIBXML_PATTERN_ENABLED
Daniel Veillard39e5c892005-07-03 22:48:50 +00003448 if ((pattern != NULL)
Daniel Veillardc9352532005-07-04 14:25:34 +00003449#ifdef LIBXML_READER_ENABLED
Daniel Veillard39e5c892005-07-03 22:48:50 +00003450 && (walker == 0)
3451#endif
3452 ) {
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00003453 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003454 if (patternc == NULL) {
3455 xmlGenericError(xmlGenericErrorContext,
3456 "Pattern %s failed to compile\n", pattern);
William M. Brack8304d872004-06-08 13:29:32 +00003457 progresult = XMLLINT_ERR_SCHEMAPAT;
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003458 pattern = NULL;
3459 }
3460 }
3461#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003462 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00003463 if ((!strcmp(argv[i], "-encode")) ||
3464 (!strcmp(argv[i], "--encode"))) {
3465 i++;
3466 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003467 } else if ((!strcmp(argv[i], "-o")) ||
3468 (!strcmp(argv[i], "-output")) ||
3469 (!strcmp(argv[i], "--output"))) {
3470 i++;
3471 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00003472 }
Daniel Veillard4432df22003-09-28 18:58:27 +00003473#ifdef LIBXML_VALID_ENABLED
Daniel Veillardcd429612000-10-11 15:57:05 +00003474 if ((!strcmp(argv[i], "-dtdvalid")) ||
3475 (!strcmp(argv[i], "--dtdvalid"))) {
3476 i++;
3477 continue;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003478 }
Daniel Veillard0bff36d2004-08-31 09:37:03 +00003479 if ((!strcmp(argv[i], "-path")) ||
3480 (!strcmp(argv[i], "--path"))) {
3481 i++;
3482 continue;
Daniel Veillardcd429612000-10-11 15:57:05 +00003483 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00003484 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
3485 (!strcmp(argv[i], "--dtdvalidfpi"))) {
3486 i++;
3487 continue;
3488 }
Daniel Veillard4432df22003-09-28 18:58:27 +00003489#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00003490 if ((!strcmp(argv[i], "-relaxng")) ||
3491 (!strcmp(argv[i], "--relaxng"))) {
3492 i++;
3493 continue;
3494 }
Daniel Veillard87076042004-05-03 22:54:49 +00003495 if ((!strcmp(argv[i], "-maxmem")) ||
3496 (!strcmp(argv[i], "--maxmem"))) {
3497 i++;
3498 continue;
3499 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003500 if ((!strcmp(argv[i], "-schema")) ||
3501 (!strcmp(argv[i], "--schema"))) {
3502 i++;
3503 continue;
3504 }
Daniel Veillardd4501d72005-07-24 14:27:16 +00003505 if ((!strcmp(argv[i], "-schematron")) ||
3506 (!strcmp(argv[i], "--schematron"))) {
3507 i++;
3508 continue;
3509 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003510#ifdef LIBXML_PATTERN_ENABLED
3511 if ((!strcmp(argv[i], "-pattern")) ||
3512 (!strcmp(argv[i], "--pattern"))) {
3513 i++;
3514 continue;
3515 }
3516#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00003517 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00003518 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003519 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00003520 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003521 if (repeat) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003522 xmlParserCtxtPtr ctxt = NULL;
3523
3524 for (acount = 0;acount < repeat;acount++) {
Daniel Veillard81273902003-09-30 00:43:48 +00003525#ifdef LIBXML_READER_ENABLED
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003526 if (stream != 0) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00003527 streamFile(argv[i]);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003528 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00003529#endif /* LIBXML_READER_ENABLED */
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003530 if (sax) {
3531 testSAX(argv[i]);
3532 } else {
3533 if (ctxt == NULL)
3534 ctxt = xmlNewParserCtxt();
3535 parseAndPrintFile(argv[i], ctxt);
3536 }
Daniel Veillard81273902003-09-30 00:43:48 +00003537#ifdef LIBXML_READER_ENABLED
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003538 }
Daniel Veillard81273902003-09-30 00:43:48 +00003539#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003540 }
3541 if (ctxt != NULL)
3542 xmlFreeParserCtxt(ctxt);
Daniel Veillard7704fb12003-01-03 16:19:51 +00003543 } else {
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003544 nbregister = 0;
3545
Daniel Veillard81273902003-09-30 00:43:48 +00003546#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00003547 if (stream != 0)
3548 streamFile(argv[i]);
3549 else
Daniel Veillard81273902003-09-30 00:43:48 +00003550#endif /* LIBXML_READER_ENABLED */
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003551 if (sax) {
3552 testSAX(argv[i]);
3553 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003554 parseAndPrintFile(argv[i], NULL);
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003555 }
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003556
3557 if ((chkregister) && (nbregister != 0)) {
3558 fprintf(stderr, "Registration count off: %d\n", nbregister);
William M. Brack8304d872004-06-08 13:29:32 +00003559 progresult = XMLLINT_ERR_RDREGIS;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003560 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00003561 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003562 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00003563 if ((timing) && (repeat)) {
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00003564 endTimer("%d iterations", repeat);
Daniel Veillarda7866932001-12-04 13:14:44 +00003565 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00003566 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003567 }
Daniel Veillardf1edb102009-08-10 14:43:18 +02003568 if (generate)
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003569 parseAndPrintFile(NULL, NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003570 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00003571 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003572 }
Daniel Veillard845cce42002-01-09 11:51:37 +00003573 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003574 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003575 }
Daniel Veillardd4501d72005-07-24 14:27:16 +00003576#ifdef LIBXML_SCHEMATRON_ENABLED
3577 if (wxschematron != NULL)
3578 xmlSchematronFree(wxschematron);
3579#endif
Daniel Veillard71531f32003-02-05 13:19:53 +00003580#ifdef LIBXML_SCHEMAS_ENABLED
3581 if (relaxngschemas != NULL)
3582 xmlRelaxNGFree(relaxngschemas);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003583 if (wxschemas != NULL)
3584 xmlSchemaFree(wxschemas);
Daniel Veillard71531f32003-02-05 13:19:53 +00003585 xmlRelaxNGCleanupTypes();
3586#endif
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003587#ifdef LIBXML_PATTERN_ENABLED
3588 if (patternc != NULL)
3589 xmlFreePattern(patternc);
3590#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003591 xmlCleanupParser();
3592 xmlMemoryDump();
3593
Daniel Veillardf7cd4812001-02-23 18:44:52 +00003594 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003595}
Daniel Veillard88a172f2000-08-04 18:23:10 +00003596