blob: d69722c4ec7e807f81d27b5fd94a95603d12a35e [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 */
Daniel Veillard1934b0c2009-10-07 10:25:06 +0200111 XMLLINT_ERR_UNCLASS = 1, /* Unclassified */
112 XMLLINT_ERR_DTD = 2, /* Error in DTD */
113 XMLLINT_ERR_VALID = 3, /* Validation error */
114 XMLLINT_ERR_RDFILE = 4, /* CtxtReadFile error */
115 XMLLINT_ERR_SCHEMACOMP = 5, /* Schema compilation */
116 XMLLINT_ERR_OUT = 6, /* Error writing output */
117 XMLLINT_ERR_SCHEMAPAT = 7, /* Error in schema pattern */
118 XMLLINT_ERR_RDREGIS = 8, /* Error in Reader registration */
119 XMLLINT_ERR_MEM = 9, /* Out of memory error */
120 XMLLINT_ERR_XPATH = 10 /* XPath evaluation error */
William M. Brack8304d872004-06-08 13:29:32 +0000121} xmllintReturnCode;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000122#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000123static int shell = 0;
124static int debugent = 0;
125#endif
Daniel Veillard8326e732003-01-07 00:19:07 +0000126static int debug = 0;
Daniel Veillard87076042004-05-03 22:54:49 +0000127static int maxmem = 0;
Daniel Veillard652327a2003-09-29 18:02:38 +0000128#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000129static int copy = 0;
Daniel Veillard652327a2003-09-29 18:02:38 +0000130#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000131static int recovery = 0;
132static int noent = 0;
Daniel Veillardc62efc82011-05-16 16:03:50 +0800133static int noenc = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000134static int noblanks = 0;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000135static int noout = 0;
136static int nowrap = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000137#ifdef LIBXML_OUTPUT_ENABLED
138static int format = 0;
139static const char *output = NULL;
140static int compress = 0;
Daniel Veillarddab39b52006-10-16 23:22:10 +0000141static int oldout = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000142#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4432df22003-09-28 18:58:27 +0000143#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000144static int valid = 0;
145static int postvalid = 0;
Daniel Veillardcd429612000-10-11 15:57:05 +0000146static char * dtdvalid = NULL;
Daniel Veillard66f68e72003-08-18 16:39:51 +0000147static char * dtdvalidfpi = NULL;
Daniel Veillard4432df22003-09-28 18:58:27 +0000148#endif
Daniel Veillard71531f32003-02-05 13:19:53 +0000149#ifdef LIBXML_SCHEMAS_ENABLED
150static char * relaxng = NULL;
151static xmlRelaxNGPtr relaxngschemas = NULL;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +0000152static char * schema = NULL;
153static xmlSchemaPtr wxschemas = NULL;
Daniel Veillard71531f32003-02-05 13:19:53 +0000154#endif
Daniel Veillard8c6e6532005-09-08 21:39:47 +0000155#ifdef LIBXML_SCHEMATRON_ENABLED
Daniel Veillardd4501d72005-07-24 14:27:16 +0000156static char * schematron = NULL;
157static xmlSchematronPtr wxschematron = NULL;
158#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000159static int repeat = 0;
160static int insert = 0;
Daniel Veillard656ce942004-04-30 23:11:45 +0000161#if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000162static int html = 0;
Daniel Veillard42fd4122003-11-04 08:47:48 +0000163static int xmlout = 0;
Daniel Veillard4432df22003-09-28 18:58:27 +0000164#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000165static int htmlout = 0;
Daniel Veillardf1121c42010-07-26 14:02:42 +0200166#if defined(LIBXML_HTML_ENABLED)
167static int nodefdtd = 0;
168#endif
Daniel Veillard73b013f2003-09-30 12:36:01 +0000169#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000170static int push = 0;
Daniel Veillard1abd2212012-10-25 15:37:50 +0800171static int pushsize = 4096;
Daniel Veillard73b013f2003-09-30 12:36:01 +0000172#endif /* LIBXML_PUSH_ENABLED */
Daniel Richard G5706b6d2012-08-06 11:32:54 +0800173#ifdef HAVE_MMAP
Daniel Veillard46e370e2000-07-21 20:32:03 +0000174static int memory = 0;
175#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +0000176static int testIO = 0;
Daniel Veillardbe803962000-06-28 23:40:59 +0000177static char *encoding = NULL;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000178#ifdef LIBXML_XINCLUDE_ENABLED
179static int xinclude = 0;
180#endif
Daniel Veillard48da9102001-08-07 01:10:10 +0000181static int dtdattrs = 0;
Daniel Veillard10ea86c2001-06-20 13:55:33 +0000182static int loaddtd = 0;
William M. Brack8304d872004-06-08 13:29:32 +0000183static xmllintReturnCode progresult = XMLLINT_RETURN_OK;
Daniel Veillard48b2f892001-02-25 16:11:03 +0000184static int timing = 0;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000185static int generate = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000186static int dropdtd = 0;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000187#ifdef LIBXML_CATALOG_ENABLED
188static int catalogs = 0;
189static int nocatalogs = 0;
190#endif
Daniel Veillard25048d82004-08-14 22:37:54 +0000191#ifdef LIBXML_C14N_ENABLED
192static int canonical = 0;
Aleksey Sanin83868242009-07-09 10:26:22 +0200193static int canonical_11 = 0;
Aleksey Sanin2650df12005-06-06 17:16:50 +0000194static int exc_canonical = 0;
Daniel Veillard25048d82004-08-14 22:37:54 +0000195#endif
Daniel Veillard81273902003-09-30 00:43:48 +0000196#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000197static int stream = 0;
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000198static int walker = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000199#endif /* LIBXML_READER_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000200static int chkregister = 0;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +0000201static int nbregister = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000202#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +0000203static int sax1 = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000204#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000205#ifdef LIBXML_PATTERN_ENABLED
206static const char *pattern = NULL;
207static xmlPatternPtr patternc = NULL;
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000208static xmlStreamCtxtPtr patstream = NULL;
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000209#endif
Daniel Veillard1934b0c2009-10-07 10:25:06 +0200210#ifdef LIBXML_XPATH_ENABLED
211static const char *xpathquery = NULL;
212#endif
Daniel Veillard968a03a2012-08-13 12:41:33 +0800213static int options = XML_PARSE_COMPACT | XML_PARSE_BIG_LINES;
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000214static int sax = 0;
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000215static int oldxml10 = 0;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000216
Daniel Veillard87076042004-05-03 22:54:49 +0000217/************************************************************************
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000218 * *
219 * Entity loading control and customization. *
220 * *
221 ************************************************************************/
222#define MAX_PATHS 64
Daniel Veillarded121382007-04-17 12:33:19 +0000223#ifdef _WIN32
224# define PATH_SEPARATOR ';'
225#else
226# define PATH_SEPARATOR ':'
227#endif
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000228static xmlChar *paths[MAX_PATHS + 1];
229static int nbpaths = 0;
230static int load_trace = 0;
231
232static
233void parsePath(const xmlChar *path) {
234 const xmlChar *cur;
235
236 if (path == NULL)
237 return;
238 while (*path != 0) {
239 if (nbpaths >= MAX_PATHS) {
240 fprintf(stderr, "MAX_PATHS reached: too many paths\n");
241 return;
242 }
243 cur = path;
Daniel Veillarded121382007-04-17 12:33:19 +0000244 while ((*cur == ' ') || (*cur == PATH_SEPARATOR))
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000245 cur++;
246 path = cur;
Daniel Veillarded121382007-04-17 12:33:19 +0000247 while ((*cur != 0) && (*cur != ' ') && (*cur != PATH_SEPARATOR))
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000248 cur++;
249 if (cur != path) {
250 paths[nbpaths] = xmlStrndup(path, cur - path);
251 if (paths[nbpaths] != NULL)
252 nbpaths++;
253 path = cur;
254 }
255 }
256}
257
Daniel Veillard24505b02005-07-28 23:49:35 +0000258static xmlExternalEntityLoader defaultEntityLoader = NULL;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000259
Daniel Veillardf1edb102009-08-10 14:43:18 +0200260static xmlParserInputPtr
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000261xmllintExternalEntityLoader(const char *URL, const char *ID,
262 xmlParserCtxtPtr ctxt) {
263 xmlParserInputPtr ret;
264 warningSAXFunc warning = NULL;
Daniel Veillardea71f5d2006-02-19 16:55:55 +0000265 errorSAXFunc err = NULL;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000266
267 int i;
268 const char *lastsegment = URL;
269 const char *iter = URL;
270
Daniel Veillard5608b172008-01-11 06:53:15 +0000271 if ((nbpaths > 0) && (iter != NULL)) {
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000272 while (*iter != 0) {
273 if (*iter == '/')
274 lastsegment = iter + 1;
275 iter++;
276 }
277 }
278
279 if ((ctxt != NULL) && (ctxt->sax != NULL)) {
280 warning = ctxt->sax->warning;
Daniel Veillardea71f5d2006-02-19 16:55:55 +0000281 err = ctxt->sax->error;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000282 ctxt->sax->warning = NULL;
Daniel Veillardea71f5d2006-02-19 16:55:55 +0000283 ctxt->sax->error = NULL;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000284 }
285
286 if (defaultEntityLoader != NULL) {
287 ret = defaultEntityLoader(URL, ID, ctxt);
288 if (ret != NULL) {
289 if (warning != NULL)
290 ctxt->sax->warning = warning;
Daniel Veillardea71f5d2006-02-19 16:55:55 +0000291 if (err != NULL)
292 ctxt->sax->error = err;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000293 if (load_trace) {
294 fprintf \
295 (stderr,
296 "Loaded URL=\"%s\" ID=\"%s\"\n",
297 URL ? URL : "(null)",
298 ID ? ID : "(null)");
299 }
300 return(ret);
301 }
302 }
303 for (i = 0;i < nbpaths;i++) {
304 xmlChar *newURL;
305
306 newURL = xmlStrdup((const xmlChar *) paths[i]);
307 newURL = xmlStrcat(newURL, (const xmlChar *) "/");
308 newURL = xmlStrcat(newURL, (const xmlChar *) lastsegment);
309 if (newURL != NULL) {
310 ret = defaultEntityLoader((const char *)newURL, ID, ctxt);
311 if (ret != NULL) {
312 if (warning != NULL)
313 ctxt->sax->warning = warning;
Daniel Veillardea71f5d2006-02-19 16:55:55 +0000314 if (err != NULL)
315 ctxt->sax->error = err;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000316 if (load_trace) {
317 fprintf \
Daniel Veillardf1edb102009-08-10 14:43:18 +0200318 (stderr,
319 "Loaded URL=\"%s\" ID=\"%s\"\n",
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000320 newURL,
Daniel Veillardf1edb102009-08-10 14:43:18 +0200321 ID ? ID : "(null)");
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000322 }
323 xmlFree(newURL);
324 return(ret);
325 }
326 xmlFree(newURL);
327 }
328 }
Daniel Veillardea71f5d2006-02-19 16:55:55 +0000329 if (err != NULL)
330 ctxt->sax->error = err;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000331 if (warning != NULL) {
332 ctxt->sax->warning = warning;
333 if (URL != NULL)
334 warning(ctxt, "failed to load external entity \"%s\"\n", URL);
335 else if (ID != NULL)
336 warning(ctxt, "failed to load external entity \"%s\"\n", ID);
337 }
338 return(NULL);
339}
340/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +0200341 * *
Daniel Veillard87076042004-05-03 22:54:49 +0000342 * Memory allocation consumption debugging *
Daniel Veillardf1edb102009-08-10 14:43:18 +0200343 * *
Daniel Veillard87076042004-05-03 22:54:49 +0000344 ************************************************************************/
345
Daniel Veillard3af3b592004-05-05 19:22:30 +0000346static void
347OOM(void)
348{
Daniel Veillard87076042004-05-03 22:54:49 +0000349 fprintf(stderr, "Ran out of memory needs > %d bytes\n", maxmem);
William M. Brack8304d872004-06-08 13:29:32 +0000350 progresult = XMLLINT_ERR_MEM;
Daniel Veillard87076042004-05-03 22:54:49 +0000351}
352
Daniel Veillard3af3b592004-05-05 19:22:30 +0000353static void
354myFreeFunc(void *mem)
355{
Daniel Veillard87076042004-05-03 22:54:49 +0000356 xmlMemFree(mem);
357}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000358static void *
359myMallocFunc(size_t size)
360{
Daniel Veillard87076042004-05-03 22:54:49 +0000361 void *ret;
362
363 ret = xmlMemMalloc(size);
364 if (ret != NULL) {
365 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000366 OOM();
367 xmlMemFree(ret);
368 return (NULL);
369 }
Daniel Veillard87076042004-05-03 22:54:49 +0000370 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000371 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000372}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000373static void *
374myReallocFunc(void *mem, size_t size)
375{
Daniel Veillard87076042004-05-03 22:54:49 +0000376 void *ret;
377
378 ret = xmlMemRealloc(mem, size);
379 if (ret != NULL) {
380 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000381 OOM();
382 xmlMemFree(ret);
383 return (NULL);
384 }
Daniel Veillard87076042004-05-03 22:54:49 +0000385 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000386 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000387}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000388static char *
389myStrdupFunc(const char *str)
390{
Daniel Veillard87076042004-05-03 22:54:49 +0000391 char *ret;
392
393 ret = xmlMemoryStrdup(str);
394 if (ret != NULL) {
395 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000396 OOM();
397 xmlFree(ret);
398 return (NULL);
399 }
Daniel Veillard87076042004-05-03 22:54:49 +0000400 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000401 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000402}
Daniel Veillard87076042004-05-03 22:54:49 +0000403/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +0200404 * *
Daniel Veillard87076042004-05-03 22:54:49 +0000405 * Internal timing routines to remove the necessity to have *
406 * unix-specific function calls. *
Daniel Veillardf1edb102009-08-10 14:43:18 +0200407 * *
Daniel Veillard87076042004-05-03 22:54:49 +0000408 ************************************************************************/
Daniel Veillard01db67c2001-12-18 07:09:59 +0000409
Daniel Veillardf1edb102009-08-10 14:43:18 +0200410#ifndef HAVE_GETTIMEOFDAY
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000411#ifdef HAVE_SYS_TIMEB_H
412#ifdef HAVE_SYS_TIME_H
413#ifdef HAVE_FTIME
414
Daniel Veillard01c13b52002-12-10 15:19:08 +0000415static int
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000416my_gettimeofday(struct timeval *tvp, void *tzp)
417{
418 struct timeb timebuffer;
419
420 ftime(&timebuffer);
421 if (tvp) {
422 tvp->tv_sec = timebuffer.time;
423 tvp->tv_usec = timebuffer.millitm * 1000L;
424 }
425 return (0);
426}
427#define HAVE_GETTIMEOFDAY 1
428#define gettimeofday my_gettimeofday
429
430#endif /* HAVE_FTIME */
431#endif /* HAVE_SYS_TIME_H */
432#endif /* HAVE_SYS_TIMEB_H */
433#endif /* !HAVE_GETTIMEOFDAY */
434
Daniel Veillard01db67c2001-12-18 07:09:59 +0000435#if defined(HAVE_GETTIMEOFDAY)
436static struct timeval begin, end;
437
438/*
439 * startTimer: call where you want to start timing
440 */
441static void
442startTimer(void)
443{
444 gettimeofday(&begin, NULL);
445}
446
447/*
448 * endTimer: call where you want to stop timing and to print out a
449 * message about the timing performed; format is a printf
450 * type argument
451 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000452static void XMLCDECL
Daniel Veillard118aed72002-09-24 14:13:13 +0000453endTimer(const char *fmt, ...)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000454{
455 long msec;
456 va_list ap;
457
458 gettimeofday(&end, NULL);
459 msec = end.tv_sec - begin.tv_sec;
460 msec *= 1000;
461 msec += (end.tv_usec - begin.tv_usec) / 1000;
462
463#ifndef HAVE_STDARG_H
464#error "endTimer required stdarg functions"
465#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000466 va_start(ap, fmt);
467 vfprintf(stderr, fmt, ap);
Daniel Veillard01db67c2001-12-18 07:09:59 +0000468 va_end(ap);
469
470 fprintf(stderr, " took %ld ms\n", msec);
471}
472#elif defined(HAVE_TIME_H)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000473/*
474 * No gettimeofday function, so we have to make do with calling clock.
475 * This is obviously less accurate, but there's little we can do about
476 * that.
477 */
Daniel Veillard90bc3712002-03-07 15:12:58 +0000478#ifndef CLOCKS_PER_SEC
479#define CLOCKS_PER_SEC 100
480#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +0000481
482static clock_t begin, end;
483static void
484startTimer(void)
485{
486 begin = clock();
487}
Daniel Veillardffa3c742005-07-21 13:24:09 +0000488static void XMLCDECL
Daniel Veillard01db67c2001-12-18 07:09:59 +0000489endTimer(const char *fmt, ...)
490{
491 long msec;
492 va_list ap;
493
494 end = clock();
495 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
496
497#ifndef HAVE_STDARG_H
498#error "endTimer required stdarg functions"
499#endif
500 va_start(ap, fmt);
501 vfprintf(stderr, fmt, ap);
502 va_end(ap);
503 fprintf(stderr, " took %ld ms\n", msec);
504}
505#else
506
507/*
508 * We don't have a gettimeofday or time.h, so we just don't do timing
509 */
510static void
511startTimer(void)
512{
513 /*
514 * Do nothing
515 */
516}
Daniel Veillardffa3c742005-07-21 13:24:09 +0000517static void XMLCDECL
Daniel Veillard01db67c2001-12-18 07:09:59 +0000518endTimer(char *format, ...)
519{
520 /*
521 * We cannot do anything because we don't have a timing function
522 */
523#ifdef HAVE_STDARG_H
Patrick R. Gansterer023206f2012-05-10 22:17:51 +0800524 va_list ap;
Daniel Veillard01db67c2001-12-18 07:09:59 +0000525 va_start(ap, format);
526 vfprintf(stderr, format, ap);
527 va_end(ap);
Patrick R. Gansterer023206f2012-05-10 22:17:51 +0800528 fprintf(stderr, " was not timed\n");
Daniel Veillard01db67c2001-12-18 07:09:59 +0000529#else
530 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
531 * this ?!
532 */
533#endif
534}
535#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000536/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +0200537 * *
538 * HTML ouput *
539 * *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000540 ************************************************************************/
Daniel Veillard24505b02005-07-28 23:49:35 +0000541static char buffer[50000];
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000542
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000543static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000544xmlHTMLEncodeSend(void) {
545 char *result;
546
547 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
548 if (result) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000549 xmlGenericError(xmlGenericErrorContext, "%s", result);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000550 xmlFree(result);
551 }
552 buffer[0] = 0;
553}
554
555/**
556 * xmlHTMLPrintFileInfo:
557 * @input: an xmlParserInputPtr input
Daniel Veillardf1edb102009-08-10 14:43:18 +0200558 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000559 * Displays the associated file and line informations for the current input
560 */
561
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000562static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000563xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000564 int len;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000565 xmlGenericError(xmlGenericErrorContext, "<p>");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000566
567 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000568 if (input != NULL) {
569 if (input->filename) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000570 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000571 input->line);
572 } else {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000573 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000574 }
575 }
576 xmlHTMLEncodeSend();
577}
578
579/**
580 * xmlHTMLPrintFileContext:
581 * @input: an xmlParserInputPtr input
Daniel Veillardf1edb102009-08-10 14:43:18 +0200582 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000583 * Displays current context within the input content for error tracking
584 */
585
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000586static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000587xmlHTMLPrintFileContext(xmlParserInputPtr input) {
588 const xmlChar *cur, *base;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000589 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000590 int n;
591
592 if (input == NULL) return;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000593 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000594 cur = input->cur;
595 base = input->base;
596 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
597 cur--;
598 }
599 n = 0;
600 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
601 cur--;
602 if ((*cur == '\n') || (*cur == '\r')) cur++;
603 base = cur;
604 n = 0;
605 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000606 len = strlen(buffer);
Daniel Veillardf1edb102009-08-10 14:43:18 +0200607 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000608 (unsigned char) *cur++);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000609 n++;
610 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000611 len = strlen(buffer);
612 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000613 cur = input->cur;
614 while ((*cur == '\n') || (*cur == '\r'))
615 cur--;
616 n = 0;
617 while ((cur != base) && (n++ < 80)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000618 len = strlen(buffer);
619 snprintf(&buffer[len], sizeof(buffer) - len, " ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000620 base++;
621 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000622 len = strlen(buffer);
623 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000624 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000625 xmlGenericError(xmlGenericErrorContext, "</pre>");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000626}
627
628/**
629 * xmlHTMLError:
630 * @ctx: an XML parser context
631 * @msg: the message to display/transmit
632 * @...: extra parameters for the message display
Daniel Veillardf1edb102009-08-10 14:43:18 +0200633 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000634 * Display and format an error messages, gives file, line, position and
635 * extra parameters.
636 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000637static void XMLCDECL
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000638xmlHTMLError(void *ctx, const char *msg, ...)
639{
640 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
641 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000642 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000643 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000644
645 buffer[0] = 0;
646 input = ctxt->input;
647 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000648 input = ctxt->inputTab[ctxt->inputNr - 2];
649 }
Daniel Veillardf1edb102009-08-10 14:43:18 +0200650
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000651 xmlHTMLPrintFileInfo(input);
652
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000653 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000654 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000655 len = strlen(buffer);
656 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000657 va_end(args);
658 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000659 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000660
661 xmlHTMLPrintFileContext(input);
662 xmlHTMLEncodeSend();
663}
664
665/**
666 * xmlHTMLWarning:
667 * @ctx: an XML parser context
668 * @msg: the message to display/transmit
669 * @...: extra parameters for the message display
Daniel Veillardf1edb102009-08-10 14:43:18 +0200670 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000671 * Display and format a warning messages, gives file, line, position and
672 * extra parameters.
673 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000674static void XMLCDECL
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000675xmlHTMLWarning(void *ctx, const char *msg, ...)
676{
677 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
678 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000679 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000680 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000681
682 buffer[0] = 0;
683 input = ctxt->input;
684 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000685 input = ctxt->inputTab[ctxt->inputNr - 2];
686 }
Daniel Veillardf1edb102009-08-10 14:43:18 +0200687
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000688
689 xmlHTMLPrintFileInfo(input);
Daniel Veillardf1edb102009-08-10 14:43:18 +0200690
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000691 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000692 va_start(args, msg);
Daniel Veillardf1edb102009-08-10 14:43:18 +0200693 len = strlen(buffer);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000694 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000695 va_end(args);
696 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000697 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000698
699 xmlHTMLPrintFileContext(input);
700 xmlHTMLEncodeSend();
701}
702
703/**
704 * xmlHTMLValidityError:
705 * @ctx: an XML parser context
706 * @msg: the message to display/transmit
707 * @...: extra parameters for the message display
Daniel Veillardf1edb102009-08-10 14:43:18 +0200708 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000709 * Display and format an validity error messages, gives file,
710 * line, position and extra parameters.
711 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000712static void XMLCDECL
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000713xmlHTMLValidityError(void *ctx, const char *msg, ...)
714{
715 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
716 xmlParserInputPtr input;
717 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000718 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000719
720 buffer[0] = 0;
721 input = ctxt->input;
722 if ((input->filename == NULL) && (ctxt->inputNr > 1))
723 input = ctxt->inputTab[ctxt->inputNr - 2];
Daniel Veillardf1edb102009-08-10 14:43:18 +0200724
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000725 xmlHTMLPrintFileInfo(input);
726
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000727 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000728 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000729 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000730 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000731 va_end(args);
732 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000733 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000734
735 xmlHTMLPrintFileContext(input);
736 xmlHTMLEncodeSend();
Daniel Veillard9fcb4912005-03-16 12:57:31 +0000737 progresult = XMLLINT_ERR_VALID;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000738}
739
740/**
741 * xmlHTMLValidityWarning:
742 * @ctx: an XML parser context
743 * @msg: the message to display/transmit
744 * @...: extra parameters for the message display
Daniel Veillardf1edb102009-08-10 14:43:18 +0200745 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000746 * Display and format a validity warning messages, gives file, line,
747 * position and extra parameters.
748 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000749static void XMLCDECL
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000750xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
751{
752 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
753 xmlParserInputPtr input;
754 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000755 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000756
757 buffer[0] = 0;
758 input = ctxt->input;
759 if ((input->filename == NULL) && (ctxt->inputNr > 1))
760 input = ctxt->inputTab[ctxt->inputNr - 2];
761
762 xmlHTMLPrintFileInfo(input);
Daniel Veillardf1edb102009-08-10 14:43:18 +0200763
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000764 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000765 va_start(args, msg);
Daniel Veillardf1edb102009-08-10 14:43:18 +0200766 len = strlen(buffer);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000767 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000768 va_end(args);
769 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000770 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000771
772 xmlHTMLPrintFileContext(input);
773 xmlHTMLEncodeSend();
774}
775
776/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +0200777 * *
778 * Shell Interface *
779 * *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000780 ************************************************************************/
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000781#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardd0cf7f62004-11-09 16:17:02 +0000782#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000783/**
784 * xmlShellReadline:
785 * @prompt: the prompt value
786 *
787 * Read a string
Daniel Veillardf1edb102009-08-10 14:43:18 +0200788 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000789 * Returns a pointer to it or NULL on EOF the caller is expected to
790 * free the returned string.
791 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000792static char *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000793xmlShellReadline(char *prompt) {
794#ifdef HAVE_LIBREADLINE
795 char *line_read;
796
797 /* Get a line from the user. */
798 line_read = readline (prompt);
799
800 /* If the line has any text in it, save it on the history. */
801 if (line_read && *line_read)
802 add_history (line_read);
803
804 return (line_read);
805#else
806 char line_read[501];
Daniel Veillard29e43992001-12-13 22:21:58 +0000807 char *ret;
808 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000809
810 if (prompt != NULL)
811 fprintf(stdout, "%s", prompt);
812 if (!fgets(line_read, 500, stdin))
813 return(NULL);
814 line_read[500] = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000815 len = strlen(line_read);
816 ret = (char *) malloc(len + 1);
817 if (ret != NULL) {
818 memcpy (ret, line_read, len + 1);
819 }
820 return(ret);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000821#endif
822}
Daniel Veillardd0cf7f62004-11-09 16:17:02 +0000823#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000824#endif /* LIBXML_DEBUG_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000825
826/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +0200827 * *
828 * I/O Interfaces *
829 * *
Daniel Veillard5e873c42000-04-12 13:27:38 +0000830 ************************************************************************/
831
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000832static int myRead(FILE *f, char * buf, int len) {
833 return(fread(buf, 1, len, f));
Daniel Veillard5e873c42000-04-12 13:27:38 +0000834}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000835static void myClose(FILE *f) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000836 if (f != stdin) {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000837 fclose(f);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000838 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000839}
840
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000841/************************************************************************
842 * *
Daniel Veillardf1edb102009-08-10 14:43:18 +0200843 * SAX based tests *
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000844 * *
845 ************************************************************************/
846
847/*
848 * empty SAX block
849 */
Daniel Veillard24505b02005-07-28 23:49:35 +0000850static xmlSAXHandler emptySAXHandlerStruct = {
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000851 NULL, /* internalSubset */
852 NULL, /* isStandalone */
853 NULL, /* hasInternalSubset */
854 NULL, /* hasExternalSubset */
855 NULL, /* resolveEntity */
856 NULL, /* getEntity */
857 NULL, /* entityDecl */
858 NULL, /* notationDecl */
859 NULL, /* attributeDecl */
860 NULL, /* elementDecl */
861 NULL, /* unparsedEntityDecl */
862 NULL, /* setDocumentLocator */
863 NULL, /* startDocument */
864 NULL, /* endDocument */
865 NULL, /* startElement */
866 NULL, /* endElement */
867 NULL, /* reference */
868 NULL, /* characters */
869 NULL, /* ignorableWhitespace */
870 NULL, /* processingInstruction */
871 NULL, /* comment */
872 NULL, /* xmlParserWarning */
873 NULL, /* xmlParserError */
874 NULL, /* xmlParserError */
875 NULL, /* getParameterEntity */
876 NULL, /* cdataBlock; */
877 NULL, /* externalSubset; */
Daniel Veillard971771e2005-07-09 17:32:57 +0000878 XML_SAX2_MAGIC,
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000879 NULL,
880 NULL, /* startElementNs */
881 NULL, /* endElementNs */
882 NULL /* xmlStructuredErrorFunc */
883};
884
Daniel Veillard24505b02005-07-28 23:49:35 +0000885static xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000886extern xmlSAXHandlerPtr debugSAXHandler;
887static int callbacks;
888
889/**
890 * isStandaloneDebug:
891 * @ctxt: An XML parser context
892 *
893 * Is this document tagged standalone ?
894 *
895 * Returns 1 if true
896 */
897static int
898isStandaloneDebug(void *ctx ATTRIBUTE_UNUSED)
899{
900 callbacks++;
901 if (noout)
902 return(0);
903 fprintf(stdout, "SAX.isStandalone()\n");
904 return(0);
905}
906
907/**
908 * hasInternalSubsetDebug:
909 * @ctxt: An XML parser context
910 *
911 * Does this document has an internal subset
912 *
913 * Returns 1 if true
914 */
915static int
916hasInternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
917{
918 callbacks++;
919 if (noout)
920 return(0);
921 fprintf(stdout, "SAX.hasInternalSubset()\n");
922 return(0);
923}
924
925/**
926 * hasExternalSubsetDebug:
927 * @ctxt: An XML parser context
928 *
929 * Does this document has an external subset
930 *
931 * Returns 1 if true
932 */
933static int
934hasExternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
935{
936 callbacks++;
937 if (noout)
938 return(0);
939 fprintf(stdout, "SAX.hasExternalSubset()\n");
940 return(0);
941}
942
943/**
944 * internalSubsetDebug:
945 * @ctxt: An XML parser context
946 *
947 * Does this document has an internal subset
948 */
949static void
950internalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
951 const xmlChar *ExternalID, const xmlChar *SystemID)
952{
953 callbacks++;
954 if (noout)
955 return;
956 fprintf(stdout, "SAX.internalSubset(%s,", name);
957 if (ExternalID == NULL)
958 fprintf(stdout, " ,");
959 else
960 fprintf(stdout, " %s,", ExternalID);
961 if (SystemID == NULL)
962 fprintf(stdout, " )\n");
963 else
964 fprintf(stdout, " %s)\n", SystemID);
965}
966
967/**
968 * externalSubsetDebug:
969 * @ctxt: An XML parser context
970 *
971 * Does this document has an external subset
972 */
973static void
974externalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
975 const xmlChar *ExternalID, const xmlChar *SystemID)
976{
977 callbacks++;
978 if (noout)
979 return;
980 fprintf(stdout, "SAX.externalSubset(%s,", name);
981 if (ExternalID == NULL)
982 fprintf(stdout, " ,");
983 else
984 fprintf(stdout, " %s,", ExternalID);
985 if (SystemID == NULL)
986 fprintf(stdout, " )\n");
987 else
988 fprintf(stdout, " %s)\n", SystemID);
989}
990
991/**
992 * resolveEntityDebug:
993 * @ctxt: An XML parser context
994 * @publicId: The public ID of the entity
995 * @systemId: The system ID of the entity
996 *
997 * Special entity resolver, better left to the parser, it has
998 * more context than the application layer.
999 * The default behaviour is to NOT resolve the entities, in that case
1000 * the ENTITY_REF nodes are built in the structure (and the parameter
1001 * values).
1002 *
1003 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
1004 */
1005static xmlParserInputPtr
1006resolveEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *publicId, const xmlChar *systemId)
1007{
1008 callbacks++;
1009 if (noout)
1010 return(NULL);
1011 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
1012
Daniel Veillardf1edb102009-08-10 14:43:18 +02001013
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001014 fprintf(stdout, "SAX.resolveEntity(");
1015 if (publicId != NULL)
1016 fprintf(stdout, "%s", (char *)publicId);
1017 else
1018 fprintf(stdout, " ");
1019 if (systemId != NULL)
1020 fprintf(stdout, ", %s)\n", (char *)systemId);
1021 else
1022 fprintf(stdout, ", )\n");
1023 return(NULL);
1024}
1025
1026/**
1027 * getEntityDebug:
1028 * @ctxt: An XML parser context
1029 * @name: The entity name
1030 *
1031 * Get an entity by name
1032 *
1033 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
1034 */
1035static xmlEntityPtr
1036getEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1037{
1038 callbacks++;
1039 if (noout)
1040 return(NULL);
1041 fprintf(stdout, "SAX.getEntity(%s)\n", name);
1042 return(NULL);
1043}
1044
1045/**
1046 * getParameterEntityDebug:
1047 * @ctxt: An XML parser context
1048 * @name: The entity name
1049 *
1050 * Get a parameter entity by name
1051 *
1052 * Returns the xmlParserInputPtr
1053 */
1054static xmlEntityPtr
1055getParameterEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1056{
1057 callbacks++;
1058 if (noout)
1059 return(NULL);
1060 fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
1061 return(NULL);
1062}
1063
1064
1065/**
1066 * entityDeclDebug:
1067 * @ctxt: An XML parser context
Daniel Veillardf1edb102009-08-10 14:43:18 +02001068 * @name: the entity name
1069 * @type: the entity type
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001070 * @publicId: The public ID of the entity
1071 * @systemId: The system ID of the entity
1072 * @content: the entity value (without processing).
1073 *
1074 * An entity definition has been parsed
1075 */
1076static void
1077entityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
1078 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
1079{
1080const xmlChar *nullstr = BAD_CAST "(null)";
1081 /* not all libraries handle printing null pointers nicely */
1082 if (publicId == NULL)
1083 publicId = nullstr;
1084 if (systemId == NULL)
1085 systemId = nullstr;
1086 if (content == NULL)
1087 content = (xmlChar *)nullstr;
1088 callbacks++;
1089 if (noout)
1090 return;
1091 fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
1092 name, type, publicId, systemId, content);
1093}
1094
1095/**
1096 * attributeDeclDebug:
1097 * @ctxt: An XML parser context
Daniel Veillardf1edb102009-08-10 14:43:18 +02001098 * @name: the attribute name
1099 * @type: the attribute type
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001100 *
1101 * An attribute definition has been parsed
1102 */
1103static void
1104attributeDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar * elem,
1105 const xmlChar * name, int type, int def,
1106 const xmlChar * defaultValue, xmlEnumerationPtr tree)
1107{
1108 callbacks++;
1109 if (noout)
1110 return;
1111 if (defaultValue == NULL)
1112 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, NULL, ...)\n",
1113 elem, name, type, def);
1114 else
1115 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
1116 elem, name, type, def, defaultValue);
1117 xmlFreeEnumeration(tree);
1118}
1119
1120/**
1121 * elementDeclDebug:
1122 * @ctxt: An XML parser context
Daniel Veillardf1edb102009-08-10 14:43:18 +02001123 * @name: the element name
1124 * @type: the element type
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001125 * @content: the element value (without processing).
1126 *
1127 * An element definition has been parsed
1128 */
1129static void
1130elementDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
1131 xmlElementContentPtr content ATTRIBUTE_UNUSED)
1132{
1133 callbacks++;
1134 if (noout)
1135 return;
1136 fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
1137 name, type);
1138}
1139
1140/**
1141 * notationDeclDebug:
1142 * @ctxt: An XML parser context
1143 * @name: The name of the notation
1144 * @publicId: The public ID of the entity
1145 * @systemId: The system ID of the entity
1146 *
1147 * What to do when a notation declaration has been parsed.
1148 */
1149static void
1150notationDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
1151 const xmlChar *publicId, const xmlChar *systemId)
1152{
1153 callbacks++;
1154 if (noout)
1155 return;
1156 fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
1157 (char *) name, (char *) publicId, (char *) systemId);
1158}
1159
1160/**
1161 * unparsedEntityDeclDebug:
1162 * @ctxt: An XML parser context
1163 * @name: The name of the entity
1164 * @publicId: The public ID of the entity
1165 * @systemId: The system ID of the entity
1166 * @notationName: the name of the notation
1167 *
1168 * What to do when an unparsed entity declaration is parsed
1169 */
1170static void
1171unparsedEntityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
1172 const xmlChar *publicId, const xmlChar *systemId,
1173 const xmlChar *notationName)
1174{
1175const xmlChar *nullstr = BAD_CAST "(null)";
1176
1177 if (publicId == NULL)
1178 publicId = nullstr;
1179 if (systemId == NULL)
1180 systemId = nullstr;
1181 if (notationName == NULL)
1182 notationName = nullstr;
1183 callbacks++;
1184 if (noout)
1185 return;
1186 fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
1187 (char *) name, (char *) publicId, (char *) systemId,
1188 (char *) notationName);
1189}
1190
1191/**
1192 * setDocumentLocatorDebug:
1193 * @ctxt: An XML parser context
1194 * @loc: A SAX Locator
1195 *
1196 * Receive the document locator at startup, actually xmlDefaultSAXLocator
1197 * Everything is available on the context, so this is useless in our case.
1198 */
1199static void
1200setDocumentLocatorDebug(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
1201{
1202 callbacks++;
1203 if (noout)
1204 return;
1205 fprintf(stdout, "SAX.setDocumentLocator()\n");
1206}
1207
1208/**
1209 * startDocumentDebug:
1210 * @ctxt: An XML parser context
1211 *
1212 * called when the document start being processed.
1213 */
1214static void
1215startDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
1216{
1217 callbacks++;
1218 if (noout)
1219 return;
1220 fprintf(stdout, "SAX.startDocument()\n");
1221}
1222
1223/**
1224 * endDocumentDebug:
1225 * @ctxt: An XML parser context
1226 *
1227 * called when the document end has been detected.
1228 */
1229static void
1230endDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
1231{
1232 callbacks++;
1233 if (noout)
1234 return;
1235 fprintf(stdout, "SAX.endDocument()\n");
1236}
1237
1238/**
1239 * startElementDebug:
1240 * @ctxt: An XML parser context
1241 * @name: The element name
1242 *
1243 * called when an opening tag has been processed.
1244 */
1245static void
1246startElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, const xmlChar **atts)
1247{
1248 int i;
1249
1250 callbacks++;
1251 if (noout)
1252 return;
1253 fprintf(stdout, "SAX.startElement(%s", (char *) name);
1254 if (atts != NULL) {
1255 for (i = 0;(atts[i] != NULL);i++) {
1256 fprintf(stdout, ", %s='", atts[i++]);
1257 if (atts[i] != NULL)
1258 fprintf(stdout, "%s'", atts[i]);
1259 }
1260 }
1261 fprintf(stdout, ")\n");
1262}
1263
1264/**
1265 * endElementDebug:
1266 * @ctxt: An XML parser context
1267 * @name: The element name
1268 *
1269 * called when the end of an element has been detected.
1270 */
1271static void
1272endElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1273{
1274 callbacks++;
1275 if (noout)
1276 return;
1277 fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
1278}
1279
1280/**
1281 * charactersDebug:
1282 * @ctxt: An XML parser context
1283 * @ch: a xmlChar string
1284 * @len: the number of xmlChar
1285 *
1286 * receiving some chars from the parser.
1287 * Question: how much at a time ???
1288 */
1289static void
1290charactersDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
1291{
1292 char out[40];
1293 int i;
1294
1295 callbacks++;
1296 if (noout)
1297 return;
1298 for (i = 0;(i<len) && (i < 30);i++)
1299 out[i] = ch[i];
1300 out[i] = 0;
1301
1302 fprintf(stdout, "SAX.characters(%s, %d)\n", out, len);
1303}
1304
1305/**
1306 * referenceDebug:
1307 * @ctxt: An XML parser context
1308 * @name: The entity name
1309 *
Daniel Veillardf1edb102009-08-10 14:43:18 +02001310 * called when an entity reference is detected.
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001311 */
1312static void
1313referenceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1314{
1315 callbacks++;
1316 if (noout)
1317 return;
1318 fprintf(stdout, "SAX.reference(%s)\n", name);
1319}
1320
1321/**
1322 * ignorableWhitespaceDebug:
1323 * @ctxt: An XML parser context
1324 * @ch: a xmlChar string
1325 * @start: the first char in the string
1326 * @len: the number of xmlChar
1327 *
1328 * receiving some ignorable whitespaces from the parser.
1329 * Question: how much at a time ???
1330 */
1331static void
1332ignorableWhitespaceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
1333{
1334 char out[40];
1335 int i;
1336
1337 callbacks++;
1338 if (noout)
1339 return;
1340 for (i = 0;(i<len) && (i < 30);i++)
1341 out[i] = ch[i];
1342 out[i] = 0;
1343 fprintf(stdout, "SAX.ignorableWhitespace(%s, %d)\n", out, len);
1344}
1345
1346/**
1347 * processingInstructionDebug:
1348 * @ctxt: An XML parser context
1349 * @target: the target name
1350 * @data: the PI data's
1351 * @len: the number of xmlChar
1352 *
1353 * A processing instruction has been parsed.
1354 */
1355static void
1356processingInstructionDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *target,
1357 const xmlChar *data)
1358{
1359 callbacks++;
1360 if (noout)
1361 return;
1362 if (data != NULL)
1363 fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
1364 (char *) target, (char *) data);
1365 else
1366 fprintf(stdout, "SAX.processingInstruction(%s, NULL)\n",
1367 (char *) target);
1368}
1369
1370/**
1371 * cdataBlockDebug:
1372 * @ctx: the user data (XML parser context)
1373 * @value: The pcdata content
1374 * @len: the block length
1375 *
1376 * called when a pcdata block has been parsed
1377 */
1378static void
1379cdataBlockDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value, int len)
1380{
1381 callbacks++;
1382 if (noout)
1383 return;
1384 fprintf(stdout, "SAX.pcdata(%.20s, %d)\n",
1385 (char *) value, len);
1386}
1387
1388/**
1389 * commentDebug:
1390 * @ctxt: An XML parser context
1391 * @value: the comment content
1392 *
1393 * A comment has been parsed.
1394 */
1395static void
1396commentDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value)
1397{
1398 callbacks++;
1399 if (noout)
1400 return;
1401 fprintf(stdout, "SAX.comment(%s)\n", value);
1402}
1403
1404/**
1405 * warningDebug:
1406 * @ctxt: An XML parser context
1407 * @msg: the message to display/transmit
1408 * @...: extra parameters for the message display
1409 *
1410 * Display and format a warning messages, gives file, line, position and
1411 * extra parameters.
1412 */
Daniel Veillardffa3c742005-07-21 13:24:09 +00001413static void XMLCDECL
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001414warningDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1415{
1416 va_list args;
1417
1418 callbacks++;
1419 if (noout)
1420 return;
1421 va_start(args, msg);
1422 fprintf(stdout, "SAX.warning: ");
1423 vfprintf(stdout, msg, args);
1424 va_end(args);
1425}
1426
1427/**
1428 * errorDebug:
1429 * @ctxt: An XML parser context
1430 * @msg: the message to display/transmit
1431 * @...: extra parameters for the message display
1432 *
1433 * Display and format a error messages, gives file, line, position and
1434 * extra parameters.
1435 */
Daniel Veillardffa3c742005-07-21 13:24:09 +00001436static void XMLCDECL
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001437errorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1438{
1439 va_list args;
1440
1441 callbacks++;
1442 if (noout)
1443 return;
1444 va_start(args, msg);
1445 fprintf(stdout, "SAX.error: ");
1446 vfprintf(stdout, msg, args);
1447 va_end(args);
1448}
1449
1450/**
1451 * fatalErrorDebug:
1452 * @ctxt: An XML parser context
1453 * @msg: the message to display/transmit
1454 * @...: extra parameters for the message display
1455 *
1456 * Display and format a fatalError messages, gives file, line, position and
1457 * extra parameters.
1458 */
Daniel Veillardffa3c742005-07-21 13:24:09 +00001459static void XMLCDECL
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001460fatalErrorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1461{
1462 va_list args;
1463
1464 callbacks++;
1465 if (noout)
1466 return;
1467 va_start(args, msg);
1468 fprintf(stdout, "SAX.fatalError: ");
1469 vfprintf(stdout, msg, args);
1470 va_end(args);
1471}
1472
Daniel Veillard24505b02005-07-28 23:49:35 +00001473static xmlSAXHandler debugSAXHandlerStruct = {
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001474 internalSubsetDebug,
1475 isStandaloneDebug,
1476 hasInternalSubsetDebug,
1477 hasExternalSubsetDebug,
1478 resolveEntityDebug,
1479 getEntityDebug,
1480 entityDeclDebug,
1481 notationDeclDebug,
1482 attributeDeclDebug,
1483 elementDeclDebug,
1484 unparsedEntityDeclDebug,
1485 setDocumentLocatorDebug,
1486 startDocumentDebug,
1487 endDocumentDebug,
1488 startElementDebug,
1489 endElementDebug,
1490 referenceDebug,
1491 charactersDebug,
1492 ignorableWhitespaceDebug,
1493 processingInstructionDebug,
1494 commentDebug,
1495 warningDebug,
1496 errorDebug,
1497 fatalErrorDebug,
1498 getParameterEntityDebug,
1499 cdataBlockDebug,
1500 externalSubsetDebug,
1501 1,
1502 NULL,
1503 NULL,
1504 NULL,
1505 NULL
1506};
1507
1508xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
1509
1510/*
1511 * SAX2 specific callbacks
1512 */
1513/**
1514 * startElementNsDebug:
1515 * @ctxt: An XML parser context
1516 * @name: The element name
1517 *
1518 * called when an opening tag has been processed.
1519 */
1520static void
1521startElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
1522 const xmlChar *localname,
1523 const xmlChar *prefix,
1524 const xmlChar *URI,
1525 int nb_namespaces,
1526 const xmlChar **namespaces,
1527 int nb_attributes,
1528 int nb_defaulted,
1529 const xmlChar **attributes)
1530{
1531 int i;
1532
1533 callbacks++;
1534 if (noout)
1535 return;
1536 fprintf(stdout, "SAX.startElementNs(%s", (char *) localname);
1537 if (prefix == NULL)
1538 fprintf(stdout, ", NULL");
1539 else
1540 fprintf(stdout, ", %s", (char *) prefix);
1541 if (URI == NULL)
1542 fprintf(stdout, ", NULL");
1543 else
1544 fprintf(stdout, ", '%s'", (char *) URI);
1545 fprintf(stdout, ", %d", nb_namespaces);
Daniel Veillardf1edb102009-08-10 14:43:18 +02001546
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001547 if (namespaces != NULL) {
1548 for (i = 0;i < nb_namespaces * 2;i++) {
1549 fprintf(stdout, ", xmlns");
1550 if (namespaces[i] != NULL)
1551 fprintf(stdout, ":%s", namespaces[i]);
1552 i++;
1553 fprintf(stdout, "='%s'", namespaces[i]);
1554 }
1555 }
1556 fprintf(stdout, ", %d, %d", nb_attributes, nb_defaulted);
1557 if (attributes != NULL) {
1558 for (i = 0;i < nb_attributes * 5;i += 5) {
1559 if (attributes[i + 1] != NULL)
1560 fprintf(stdout, ", %s:%s='", attributes[i + 1], attributes[i]);
1561 else
1562 fprintf(stdout, ", %s='", attributes[i]);
1563 fprintf(stdout, "%.4s...', %d", attributes[i + 3],
1564 (int)(attributes[i + 4] - attributes[i + 3]));
1565 }
1566 }
1567 fprintf(stdout, ")\n");
1568}
1569
1570/**
1571 * endElementDebug:
1572 * @ctxt: An XML parser context
1573 * @name: The element name
1574 *
1575 * called when the end of an element has been detected.
1576 */
1577static void
1578endElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
1579 const xmlChar *localname,
1580 const xmlChar *prefix,
1581 const xmlChar *URI)
1582{
1583 callbacks++;
1584 if (noout)
1585 return;
1586 fprintf(stdout, "SAX.endElementNs(%s", (char *) localname);
1587 if (prefix == NULL)
1588 fprintf(stdout, ", NULL");
1589 else
1590 fprintf(stdout, ", %s", (char *) prefix);
1591 if (URI == NULL)
1592 fprintf(stdout, ", NULL)\n");
1593 else
1594 fprintf(stdout, ", '%s')\n", (char *) URI);
1595}
1596
Daniel Veillard24505b02005-07-28 23:49:35 +00001597static xmlSAXHandler debugSAX2HandlerStruct = {
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001598 internalSubsetDebug,
1599 isStandaloneDebug,
1600 hasInternalSubsetDebug,
1601 hasExternalSubsetDebug,
1602 resolveEntityDebug,
1603 getEntityDebug,
1604 entityDeclDebug,
1605 notationDeclDebug,
1606 attributeDeclDebug,
1607 elementDeclDebug,
1608 unparsedEntityDeclDebug,
1609 setDocumentLocatorDebug,
1610 startDocumentDebug,
1611 endDocumentDebug,
1612 NULL,
1613 NULL,
1614 referenceDebug,
1615 charactersDebug,
1616 ignorableWhitespaceDebug,
1617 processingInstructionDebug,
1618 commentDebug,
1619 warningDebug,
1620 errorDebug,
1621 fatalErrorDebug,
1622 getParameterEntityDebug,
1623 cdataBlockDebug,
1624 externalSubsetDebug,
1625 XML_SAX2_MAGIC,
1626 NULL,
1627 startElementNsDebug,
1628 endElementNsDebug,
1629 NULL
1630};
1631
Daniel Veillard24505b02005-07-28 23:49:35 +00001632static xmlSAXHandlerPtr debugSAX2Handler = &debugSAX2HandlerStruct;
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001633
1634static void
1635testSAX(const char *filename) {
1636 xmlSAXHandlerPtr handler;
1637 const char *user_data = "user_data"; /* mostly for debugging */
1638 xmlParserInputBufferPtr buf = NULL;
1639 xmlParserInputPtr inputStream;
1640 xmlParserCtxtPtr ctxt = NULL;
1641 xmlSAXHandlerPtr old_sax = NULL;
1642
1643 callbacks = 0;
1644
1645 if (noout) {
1646 handler = emptySAXHandler;
Daniel Veillard78dfc9f2005-07-10 22:30:30 +00001647#ifdef LIBXML_SAX1_ENABLED
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001648 } else if (sax1) {
1649 handler = debugSAXHandler;
Daniel Veillard78dfc9f2005-07-10 22:30:30 +00001650#endif
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001651 } else {
1652 handler = debugSAX2Handler;
1653 }
1654
1655 /*
1656 * it's not the simplest code but the most generic in term of I/O
1657 */
1658 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
1659 if (buf == NULL) {
1660 goto error;
1661 }
1662
1663#ifdef LIBXML_SCHEMAS_ENABLED
1664 if (wxschemas != NULL) {
1665 int ret;
1666 xmlSchemaValidCtxtPtr vctxt;
1667
1668 vctxt = xmlSchemaNewValidCtxt(wxschemas);
1669 xmlSchemaSetValidErrors(vctxt,
1670 (xmlSchemaValidityErrorFunc) fprintf,
1671 (xmlSchemaValidityWarningFunc) fprintf,
1672 stderr);
Daniel Veillard97fa5b32012-08-14 11:01:07 +08001673 xmlSchemaValidateSetFilename(vctxt, filename);
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001674
Daniel Veillard971771e2005-07-09 17:32:57 +00001675 ret = xmlSchemaValidateStream(vctxt, buf, 0, handler,
1676 (void *)user_data);
1677 if (repeat == 0) {
1678 if (ret == 0) {
1679 fprintf(stderr, "%s validates\n", filename);
1680 } else if (ret > 0) {
1681 fprintf(stderr, "%s fails to validate\n", filename);
1682 progresult = XMLLINT_ERR_VALID;
1683 } else {
1684 fprintf(stderr, "%s validation generated an internal error\n",
1685 filename);
1686 progresult = XMLLINT_ERR_VALID;
1687 }
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001688 }
1689 xmlSchemaFreeValidCtxt(vctxt);
1690 } else
1691#endif
1692 {
1693 /*
1694 * Create the parser context amd hook the input
1695 */
1696 ctxt = xmlNewParserCtxt();
1697 if (ctxt == NULL) {
1698 xmlFreeParserInputBuffer(buf);
1699 goto error;
1700 }
1701 old_sax = ctxt->sax;
1702 ctxt->sax = handler;
1703 ctxt->userData = (void *) user_data;
1704 inputStream = xmlNewIOInputStream(ctxt, buf, XML_CHAR_ENCODING_NONE);
1705 if (inputStream == NULL) {
1706 xmlFreeParserInputBuffer(buf);
1707 goto error;
1708 }
1709 inputPush(ctxt, inputStream);
Daniel Veillardf1edb102009-08-10 14:43:18 +02001710
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001711 /* do the parsing */
1712 xmlParseDocument(ctxt);
1713
1714 if (ctxt->myDoc != NULL) {
1715 fprintf(stderr, "SAX generated a doc !\n");
1716 xmlFreeDoc(ctxt->myDoc);
1717 ctxt->myDoc = NULL;
1718 }
1719 }
1720
1721error:
1722 if (ctxt != NULL) {
1723 ctxt->sax = old_sax;
1724 xmlFreeParserCtxt(ctxt);
1725 }
1726}
1727
Daniel Veillard5e873c42000-04-12 13:27:38 +00001728/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +02001729 * *
1730 * Stream Test processing *
1731 * *
Daniel Veillard7704fb12003-01-03 16:19:51 +00001732 ************************************************************************/
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001733#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001734static void processNode(xmlTextReaderPtr reader) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001735 const xmlChar *name, *value;
Daniel Veillard16ef8002005-01-31 00:27:50 +00001736 int type, empty;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001737
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001738 type = xmlTextReaderNodeType(reader);
Daniel Veillard16ef8002005-01-31 00:27:50 +00001739 empty = xmlTextReaderIsEmptyElement(reader);
Daniel Veillard99737f52003-03-22 14:55:50 +00001740
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001741 if (debug) {
1742 name = xmlTextReaderConstName(reader);
1743 if (name == NULL)
1744 name = BAD_CAST "--";
Daniel Veillard7704fb12003-01-03 16:19:51 +00001745
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001746 value = xmlTextReaderConstValue(reader);
1747
Daniel Veillardf1edb102009-08-10 14:43:18 +02001748
1749 printf("%d %d %s %d %d",
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001750 xmlTextReaderDepth(reader),
1751 type,
1752 name,
Daniel Veillard16ef8002005-01-31 00:27:50 +00001753 empty,
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001754 xmlTextReaderHasValue(reader));
1755 if (value == NULL)
1756 printf("\n");
1757 else {
1758 printf(" %s\n", value);
1759 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00001760 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001761#ifdef LIBXML_PATTERN_ENABLED
1762 if (patternc) {
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001763 xmlChar *path = NULL;
1764 int match = -1;
Daniel Veillardf1edb102009-08-10 14:43:18 +02001765
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001766 if (type == XML_READER_TYPE_ELEMENT) {
1767 /* do the check only on element start */
1768 match = xmlPatternMatch(patternc, xmlTextReaderCurrentNode(reader));
1769
1770 if (match) {
Daniel Veillardf1edb102009-08-10 14:43:18 +02001771#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001772 path = xmlGetNodePath(xmlTextReaderCurrentNode(reader));
1773 printf("Node %s matches pattern %s\n", path, pattern);
Daniel Veillardf1edb102009-08-10 14:43:18 +02001774#else
1775 printf("Node %s matches pattern %s\n",
1776 xmlTextReaderConstName(reader), pattern);
1777#endif
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001778 }
1779 }
1780 if (patstream != NULL) {
1781 int ret;
1782
1783 if (type == XML_READER_TYPE_ELEMENT) {
1784 ret = xmlStreamPush(patstream,
1785 xmlTextReaderConstLocalName(reader),
1786 xmlTextReaderConstNamespaceUri(reader));
1787 if (ret < 0) {
1788 fprintf(stderr, "xmlStreamPush() failure\n");
1789 xmlFreeStreamCtxt(patstream);
1790 patstream = NULL;
1791 } else if (ret != match) {
Daniel Veillardf1edb102009-08-10 14:43:18 +02001792#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001793 if (path == NULL) {
1794 path = xmlGetNodePath(
1795 xmlTextReaderCurrentNode(reader));
1796 }
Daniel Veillardf1edb102009-08-10 14:43:18 +02001797#endif
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001798 fprintf(stderr,
1799 "xmlPatternMatch and xmlStreamPush disagree\n");
Daniel Veillardf1edb102009-08-10 14:43:18 +02001800 if (path != NULL)
1801 fprintf(stderr, " pattern %s node %s\n",
1802 pattern, path);
1803 else
1804 fprintf(stderr, " pattern %s node %s\n",
1805 pattern, xmlTextReaderConstName(reader));
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001806 }
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001807
Daniel Veillardf1edb102009-08-10 14:43:18 +02001808 }
Daniel Veillard16ef8002005-01-31 00:27:50 +00001809 if ((type == XML_READER_TYPE_END_ELEMENT) ||
1810 ((type == XML_READER_TYPE_ELEMENT) && (empty))) {
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001811 ret = xmlStreamPop(patstream);
1812 if (ret < 0) {
1813 fprintf(stderr, "xmlStreamPop() failure\n");
1814 xmlFreeStreamCtxt(patstream);
1815 patstream = NULL;
1816 }
1817 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001818 }
Daniel Veillardf9d16912005-01-30 22:36:30 +00001819 if (path != NULL)
1820 xmlFree(path);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001821 }
1822#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001823}
1824
1825static void streamFile(char *filename) {
1826 xmlTextReaderPtr reader;
1827 int ret;
Daniel Richard G5706b6d2012-08-06 11:32:54 +08001828#ifdef HAVE_MMAP
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001829 int fd = -1;
1830 struct stat info;
1831 const char *base = NULL;
1832 xmlParserInputBufferPtr input = NULL;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001833
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001834 if (memory) {
Daniel Veillardf1edb102009-08-10 14:43:18 +02001835 if (stat(filename, &info) < 0)
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001836 return;
1837 if ((fd = open(filename, O_RDONLY)) < 0)
1838 return;
1839 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillardb98c6a02013-07-12 12:08:40 +08001840 if (base == (void *) MAP_FAILED) {
1841 close(fd);
1842 fprintf(stderr, "mmap failure for file %s\n", filename);
1843 progresult = XMLLINT_ERR_RDFILE;
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001844 return;
Daniel Veillardb98c6a02013-07-12 12:08:40 +08001845 }
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001846
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001847 reader = xmlReaderForMemory(base, info.st_size, filename,
1848 NULL, options);
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001849 } else
1850#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001851 reader = xmlReaderForFile(filename, NULL, options);
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001852#ifdef LIBXML_PATTERN_ENABLED
Daniel Veillardd4301ab2005-02-03 22:24:10 +00001853 if (pattern != NULL) {
1854 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
1855 if (patternc == NULL) {
1856 xmlGenericError(xmlGenericErrorContext,
1857 "Pattern %s failed to compile\n", pattern);
1858 progresult = XMLLINT_ERR_SCHEMAPAT;
1859 pattern = NULL;
1860 }
1861 }
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001862 if (patternc != NULL) {
1863 patstream = xmlPatternGetStreamCtxt(patternc);
1864 if (patstream != NULL) {
1865 ret = xmlStreamPush(patstream, NULL, NULL);
1866 if (ret < 0) {
1867 fprintf(stderr, "xmlStreamPush() failure\n");
1868 xmlFreeStreamCtxt(patstream);
1869 patstream = NULL;
1870 }
1871 }
1872 }
1873#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001874
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001875
Daniel Veillard7704fb12003-01-03 16:19:51 +00001876 if (reader != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001877#ifdef LIBXML_VALID_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001878 if (valid)
1879 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00001880 else
Daniel Veillard4432df22003-09-28 18:58:27 +00001881#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarde4d16d72012-12-21 10:58:14 +08001882 if (loaddtd)
1883 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001884#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +00001885 if (relaxng != NULL) {
Daniel Veillard81514ba2003-09-16 23:17:26 +00001886 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00001887 startTimer();
1888 }
1889 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
1890 if (ret < 0) {
1891 xmlGenericError(xmlGenericErrorContext,
1892 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +00001893 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +00001894 relaxng = NULL;
1895 }
Daniel Veillard81514ba2003-09-16 23:17:26 +00001896 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00001897 endTimer("Compiling the schemas");
1898 }
1899 }
Daniel Veillardf10ae122005-07-10 19:03:16 +00001900 if (schema != NULL) {
1901 if ((timing) && (!repeat)) {
1902 startTimer();
1903 }
1904 ret = xmlTextReaderSchemaValidate(reader, schema);
1905 if (ret < 0) {
1906 xmlGenericError(xmlGenericErrorContext,
1907 "XSD schema %s failed to compile\n", schema);
1908 progresult = XMLLINT_ERR_SCHEMACOMP;
1909 schema = NULL;
1910 }
1911 if ((timing) && (!repeat)) {
1912 endTimer("Compiling the schemas");
1913 }
1914 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001915#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001916
1917 /*
1918 * Process all nodes in sequence
1919 */
Daniel Veillard81514ba2003-09-16 23:17:26 +00001920 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00001921 startTimer();
1922 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00001923 ret = xmlTextReaderRead(reader);
1924 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001925 if ((debug)
1926#ifdef LIBXML_PATTERN_ENABLED
1927 || (patternc)
1928#endif
1929 )
Daniel Veillard7704fb12003-01-03 16:19:51 +00001930 processNode(reader);
1931 ret = xmlTextReaderRead(reader);
1932 }
Daniel Veillard81514ba2003-09-16 23:17:26 +00001933 if ((timing) && (!repeat)) {
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001934#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf54cd532004-02-25 11:52:31 +00001935 if (relaxng != NULL)
Daniel Veillard49138f12004-02-19 12:58:36 +00001936 endTimer("Parsing and validating");
1937 else
Daniel Veillardf54cd532004-02-25 11:52:31 +00001938#endif
Daniel Veillard4432df22003-09-28 18:58:27 +00001939#ifdef LIBXML_VALID_ENABLED
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001940 if (valid)
Daniel Veillardce192eb2003-04-16 15:58:05 +00001941 endTimer("Parsing and validating");
1942 else
Daniel Veillard4432df22003-09-28 18:58:27 +00001943#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +00001944 endTimer("Parsing");
Daniel Veillardce192eb2003-04-16 15:58:05 +00001945 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00001946
Daniel Veillard4432df22003-09-28 18:58:27 +00001947#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf6bad792003-04-11 19:38:54 +00001948 if (valid) {
1949 if (xmlTextReaderIsValid(reader) != 1) {
1950 xmlGenericError(xmlGenericErrorContext,
1951 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001952 progresult = XMLLINT_ERR_VALID;
Daniel Veillardf6bad792003-04-11 19:38:54 +00001953 }
1954 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001955#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001956#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf10ae122005-07-10 19:03:16 +00001957 if ((relaxng != NULL) || (schema != NULL)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00001958 if (xmlTextReaderIsValid(reader) != 1) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001959 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001960 progresult = XMLLINT_ERR_VALID;
Daniel Veillardf4e55762003-04-15 23:32:22 +00001961 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001962 fprintf(stderr, "%s validates\n", filename);
Daniel Veillardf4e55762003-04-15 23:32:22 +00001963 }
1964 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001965#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001966 /*
1967 * Done, cleanup and status
1968 */
1969 xmlFreeTextReader(reader);
1970 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001971 fprintf(stderr, "%s : failed to parse\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001972 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001973 }
1974 } else {
1975 fprintf(stderr, "Unable to open %s\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001976 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001977 }
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001978#ifdef LIBXML_PATTERN_ENABLED
1979 if (patstream != NULL) {
1980 xmlFreeStreamCtxt(patstream);
1981 patstream = NULL;
1982 }
1983#endif
Daniel Richard G5706b6d2012-08-06 11:32:54 +08001984#ifdef HAVE_MMAP
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001985 if (memory) {
1986 xmlFreeParserInputBuffer(input);
1987 munmap((char *) base, info.st_size);
1988 close(fd);
1989 }
1990#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001991}
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001992
1993static void walkDoc(xmlDocPtr doc) {
1994 xmlTextReaderPtr reader;
1995 int ret;
1996
Daniel Veillardd4301ab2005-02-03 22:24:10 +00001997#ifdef LIBXML_PATTERN_ENABLED
1998 xmlNodePtr root;
1999 const xmlChar *namespaces[22];
2000 int i;
2001 xmlNsPtr ns;
2002
2003 root = xmlDocGetRootElement(doc);
2004 for (ns = root->nsDef, i = 0;ns != NULL && i < 20;ns=ns->next) {
2005 namespaces[i++] = ns->href;
2006 namespaces[i++] = ns->prefix;
2007 }
2008 namespaces[i++] = NULL;
Daniel Veillard13cee4e2009-09-05 14:52:55 +02002009 namespaces[i] = NULL;
Daniel Veillardd4301ab2005-02-03 22:24:10 +00002010
2011 if (pattern != NULL) {
2012 patternc = xmlPatterncompile((const xmlChar *) pattern, doc->dict,
2013 0, &namespaces[0]);
2014 if (patternc == NULL) {
2015 xmlGenericError(xmlGenericErrorContext,
2016 "Pattern %s failed to compile\n", pattern);
2017 progresult = XMLLINT_ERR_SCHEMAPAT;
2018 pattern = NULL;
2019 }
2020 }
Daniel Veillard2b2e02d2005-02-05 23:20:22 +00002021 if (patternc != NULL) {
2022 patstream = xmlPatternGetStreamCtxt(patternc);
2023 if (patstream != NULL) {
2024 ret = xmlStreamPush(patstream, NULL, NULL);
2025 if (ret < 0) {
2026 fprintf(stderr, "xmlStreamPush() failure\n");
2027 xmlFreeStreamCtxt(patstream);
2028 patstream = NULL;
2029 }
2030 }
2031 }
Daniel Veillardd4301ab2005-02-03 22:24:10 +00002032#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002033 reader = xmlReaderWalker(doc);
2034 if (reader != NULL) {
2035 if ((timing) && (!repeat)) {
2036 startTimer();
2037 }
2038 ret = xmlTextReaderRead(reader);
2039 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002040 if ((debug)
2041#ifdef LIBXML_PATTERN_ENABLED
2042 || (patternc)
2043#endif
2044 )
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002045 processNode(reader);
2046 ret = xmlTextReaderRead(reader);
2047 }
2048 if ((timing) && (!repeat)) {
2049 endTimer("walking through the doc");
2050 }
2051 xmlFreeTextReader(reader);
2052 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002053 fprintf(stderr, "failed to walk through the doc\n");
William M. Brack8304d872004-06-08 13:29:32 +00002054 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002055 }
2056 } else {
2057 fprintf(stderr, "Failed to crate a reader from the document\n");
William M. Brack8304d872004-06-08 13:29:32 +00002058 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002059 }
Daniel Veillard2b2e02d2005-02-05 23:20:22 +00002060#ifdef LIBXML_PATTERN_ENABLED
2061 if (patstream != NULL) {
2062 xmlFreeStreamCtxt(patstream);
2063 patstream = NULL;
2064 }
2065#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002066}
Daniel Veillard81273902003-09-30 00:43:48 +00002067#endif /* LIBXML_READER_ENABLED */
Daniel Veillard7704fb12003-01-03 16:19:51 +00002068
Daniel Veillard1934b0c2009-10-07 10:25:06 +02002069#ifdef LIBXML_XPATH_ENABLED
2070/************************************************************************
2071 * *
2072 * XPath Query *
2073 * *
2074 ************************************************************************/
2075
2076static void doXPathDump(xmlXPathObjectPtr cur) {
2077 switch(cur->type) {
2078 case XPATH_NODESET: {
2079 int i;
2080 xmlNodePtr node;
2081#ifdef LIBXML_OUTPUT_ENABLED
2082 xmlSaveCtxtPtr ctxt;
2083
Daniel Veillardbdc64d62012-03-27 14:41:37 +08002084 if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr <= 0)) {
Daniel Veillard1934b0c2009-10-07 10:25:06 +02002085 fprintf(stderr, "XPath set is empty\n");
2086 progresult = XMLLINT_ERR_XPATH;
2087 break;
2088 }
2089 ctxt = xmlSaveToFd(1, NULL, 0);
2090 if (ctxt == NULL) {
2091 fprintf(stderr, "Out of memory for XPath\n");
2092 progresult = XMLLINT_ERR_MEM;
2093 return;
2094 }
2095 for (i = 0;i < cur->nodesetval->nodeNr;i++) {
2096 node = cur->nodesetval->nodeTab[i];
2097 xmlSaveTree(ctxt, node);
2098 }
2099 xmlSaveClose(ctxt);
2100#else
2101 printf("xpath returned %d nodes\n", cur->nodesetval->nodeNr);
2102#endif
2103 break;
2104 }
2105 case XPATH_BOOLEAN:
2106 if (cur->boolval) printf("true");
2107 else printf("false");
2108 break;
2109 case XPATH_NUMBER:
2110 switch (xmlXPathIsInf(cur->floatval)) {
2111 case 1:
2112 printf("Infinity");
2113 break;
2114 case -1:
2115 printf("-Infinity");
2116 break;
2117 default:
2118 if (xmlXPathIsNaN(cur->floatval)) {
2119 printf("NaN");
2120 } else {
2121 printf("%0g", cur->floatval);
2122 }
2123 }
2124 break;
2125 case XPATH_STRING:
2126 printf("%s", (const char *) cur->stringval);
2127 break;
2128 case XPATH_UNDEFINED:
2129 fprintf(stderr, "XPath Object is uninitialized\n");
2130 progresult = XMLLINT_ERR_XPATH;
2131 break;
2132 default:
2133 fprintf(stderr, "XPath object of unexpected type\n");
2134 progresult = XMLLINT_ERR_XPATH;
2135 break;
2136 }
2137}
2138
2139static void doXPathQuery(xmlDocPtr doc, const char *query) {
2140 xmlXPathContextPtr ctxt;
2141 xmlXPathObjectPtr res;
2142
2143 ctxt = xmlXPathNewContext(doc);
2144 if (ctxt == NULL) {
2145 fprintf(stderr, "Out of memory for XPath\n");
2146 progresult = XMLLINT_ERR_MEM;
2147 return;
2148 }
Daniel Veillard2e1eaca2012-05-25 16:44:20 +08002149 ctxt->node = (xmlNodePtr) doc;
Daniel Veillard1934b0c2009-10-07 10:25:06 +02002150 res = xmlXPathEval(BAD_CAST query, ctxt);
2151 xmlXPathFreeContext(ctxt);
2152
2153 if (res == NULL) {
2154 fprintf(stderr, "XPath evaluation failure\n");
2155 progresult = XMLLINT_ERR_XPATH;
2156 return;
2157 }
2158 doXPathDump(res);
2159 xmlXPathFreeObject(res);
2160}
2161#endif /* LIBXML_XPATH_ENABLED */
2162
Daniel Veillard7704fb12003-01-03 16:19:51 +00002163/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +02002164 * *
2165 * Tree Test processing *
2166 * *
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002167 ************************************************************************/
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002168static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
Daniel Veillard652327a2003-09-29 18:02:38 +00002169 xmlDocPtr doc = NULL;
2170#ifdef LIBXML_TREE_ENABLED
2171 xmlDocPtr tmp;
2172#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002173
Daniel Veillard48b2f892001-02-25 16:11:03 +00002174 if ((timing) && (!repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00002175 startTimer();
Daniel Veillardf1edb102009-08-10 14:43:18 +02002176
Daniel Veillard48b2f892001-02-25 16:11:03 +00002177
Daniel Veillard652327a2003-09-29 18:02:38 +00002178#ifdef LIBXML_TREE_ENABLED
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002179 if (filename == NULL) {
2180 if (generate) {
2181 xmlNodePtr n;
2182
2183 doc = xmlNewDoc(BAD_CAST "1.0");
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002184 n = xmlNewDocNode(doc, NULL, BAD_CAST "info", NULL);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002185 xmlNodeSetContent(n, BAD_CAST "abc");
2186 xmlDocSetRootElement(doc, n);
2187 }
2188 }
Daniel Veillard652327a2003-09-29 18:02:38 +00002189#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002190#ifdef LIBXML_HTML_ENABLED
Daniel Veillard73b013f2003-09-30 12:36:01 +00002191#ifdef LIBXML_PUSH_ENABLED
William M. Brack78637da2003-07-31 14:47:38 +00002192 else if ((html) && (push)) {
2193 FILE *f;
2194
William M. Brack3403add2004-06-27 02:07:51 +00002195#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2196 f = fopen(filename, "rb");
2197#else
2198 f = fopen(filename, "r");
2199#endif
William M. Brack78637da2003-07-31 14:47:38 +00002200 if (f != NULL) {
Daniel Veillard1abd2212012-10-25 15:37:50 +08002201 int res;
William M. Brack78637da2003-07-31 14:47:38 +00002202 char chars[4096];
2203 htmlParserCtxtPtr ctxt;
2204
William M. Brack78637da2003-07-31 14:47:38 +00002205 res = fread(chars, 1, 4, f);
2206 if (res > 0) {
2207 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
William M. Brack1d75c8a2003-10-27 13:48:16 +00002208 chars, res, filename, XML_CHAR_ENCODING_NONE);
Daniel Veillard1abd2212012-10-25 15:37:50 +08002209 while ((res = fread(chars, 1, pushsize, f)) > 0) {
William M. Brack78637da2003-07-31 14:47:38 +00002210 htmlParseChunk(ctxt, chars, res, 0);
2211 }
2212 htmlParseChunk(ctxt, chars, 0, 1);
2213 doc = ctxt->myDoc;
2214 htmlFreeParserCtxt(ctxt);
2215 }
2216 fclose(f);
2217 }
2218 }
Daniel Veillard73b013f2003-09-30 12:36:01 +00002219#endif /* LIBXML_PUSH_ENABLED */
Daniel Richard G5706b6d2012-08-06 11:32:54 +08002220#ifdef HAVE_MMAP
Daniel Veillardf1a27c62006-10-13 22:33:03 +00002221 else if ((html) && (memory)) {
2222 int fd;
2223 struct stat info;
2224 const char *base;
Daniel Veillardf1edb102009-08-10 14:43:18 +02002225 if (stat(filename, &info) < 0)
Daniel Veillardf1a27c62006-10-13 22:33:03 +00002226 return;
2227 if ((fd = open(filename, O_RDONLY)) < 0)
2228 return;
2229 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillardb98c6a02013-07-12 12:08:40 +08002230 if (base == (void *) MAP_FAILED) {
2231 close(fd);
2232 fprintf(stderr, "mmap failure for file %s\n", filename);
2233 progresult = XMLLINT_ERR_RDFILE;
Daniel Veillardf1a27c62006-10-13 22:33:03 +00002234 return;
Daniel Veillardb98c6a02013-07-12 12:08:40 +08002235 }
Daniel Veillardf1a27c62006-10-13 22:33:03 +00002236
2237 doc = htmlReadMemory((char *) base, info.st_size, filename,
2238 NULL, options);
Daniel Veillardf1edb102009-08-10 14:43:18 +02002239
Daniel Veillardf1a27c62006-10-13 22:33:03 +00002240 munmap((char *) base, info.st_size);
2241 close(fd);
2242 }
2243#endif
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002244 else if (html) {
Daniel Veillard9475a352003-09-26 12:47:50 +00002245 doc = htmlReadFile(filename, NULL, options);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002246 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002247#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002248 else {
Daniel Veillard73b013f2003-09-30 12:36:01 +00002249#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002250 /*
2251 * build an XML tree from a string;
2252 */
2253 if (push) {
2254 FILE *f;
2255
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002256 /* '-' Usually means stdin -<sven@zen.org> */
2257 if ((filename[0] == '-') && (filename[1] == 0)) {
2258 f = stdin;
2259 } else {
William M. Brack3403add2004-06-27 02:07:51 +00002260#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2261 f = fopen(filename, "rb");
2262#else
2263 f = fopen(filename, "r");
2264#endif
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002265 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002266 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +00002267 int ret;
Daniel Veillarda880b122003-04-21 21:36:41 +00002268 int res, size = 1024;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002269 char chars[1024];
2270 xmlParserCtxtPtr ctxt;
2271
Daniel Veillarda880b122003-04-21 21:36:41 +00002272 /* if (repeat) size = 1024; */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002273 res = fread(chars, 1, 4, f);
2274 if (res > 0) {
2275 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
2276 chars, res, filename);
Daniel Veillard500a1de2004-03-22 15:22:58 +00002277 xmlCtxtUseOptions(ctxt, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002278 while ((res = fread(chars, 1, size, f)) > 0) {
2279 xmlParseChunk(ctxt, chars, res, 0);
2280 }
2281 xmlParseChunk(ctxt, chars, 0, 1);
2282 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +00002283 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002284 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +00002285 if (!ret) {
2286 xmlFreeDoc(doc);
2287 doc = NULL;
2288 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002289 }
Daniel Veillard84bff682009-09-11 15:30:19 +02002290 if (f != stdin)
2291 fclose(f);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002292 }
Daniel Veillard73b013f2003-09-30 12:36:01 +00002293 } else
2294#endif /* LIBXML_PUSH_ENABLED */
2295 if (testIO) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002296 if ((filename[0] == '-') && (filename[1] == 0)) {
Daniel Veillard60942de2003-09-25 21:05:58 +00002297 doc = xmlReadFd(0, NULL, NULL, options);
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002298 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002299 FILE *f;
Daniel Veillard5e873c42000-04-12 13:27:38 +00002300
William M. Brack3403add2004-06-27 02:07:51 +00002301#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2302 f = fopen(filename, "rb");
2303#else
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002304 f = fopen(filename, "r");
William M. Brack3403add2004-06-27 02:07:51 +00002305#endif
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002306 if (f != NULL) {
2307 if (rectxt == NULL)
2308 doc = xmlReadIO((xmlInputReadCallback) myRead,
2309 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +00002310 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002311 else
2312 doc = xmlCtxtReadIO(rectxt,
2313 (xmlInputReadCallback) myRead,
2314 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +00002315 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002316 } else
Daniel Veillard5e873c42000-04-12 13:27:38 +00002317 doc = NULL;
Daniel Veillard5e873c42000-04-12 13:27:38 +00002318 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002319 } else if (htmlout) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002320 xmlParserCtxtPtr ctxt;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002321
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002322 if (rectxt == NULL)
2323 ctxt = xmlNewParserCtxt();
2324 else
2325 ctxt = rectxt;
Daniel Veillardf1edb102009-08-10 14:43:18 +02002326 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002327 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +00002328 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002329 ctxt->sax->error = xmlHTMLError;
2330 ctxt->sax->warning = xmlHTMLWarning;
2331 ctxt->vctxt.error = xmlHTMLValidityError;
2332 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002333
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002334 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002335
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002336 if (rectxt == NULL)
2337 xmlFreeParserCtxt(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002338 }
Daniel Richard G5706b6d2012-08-06 11:32:54 +08002339#ifdef HAVE_MMAP
Daniel Veillard46e370e2000-07-21 20:32:03 +00002340 } else if (memory) {
2341 int fd;
2342 struct stat info;
2343 const char *base;
Daniel Veillardf1edb102009-08-10 14:43:18 +02002344 if (stat(filename, &info) < 0)
Daniel Veillard46e370e2000-07-21 20:32:03 +00002345 return;
2346 if ((fd = open(filename, O_RDONLY)) < 0)
2347 return;
2348 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillarda75a0092013-05-08 13:45:48 +08002349 if (base == (void *) MAP_FAILED) {
Daniel Veillardb98c6a02013-07-12 12:08:40 +08002350 close(fd);
Daniel Veillarda75a0092013-05-08 13:45:48 +08002351 fprintf(stderr, "mmap failure for file %s\n", filename);
2352 progresult = XMLLINT_ERR_RDFILE;
Daniel Veillard46e370e2000-07-21 20:32:03 +00002353 return;
Daniel Veillarda75a0092013-05-08 13:45:48 +08002354 }
Daniel Veillard46e370e2000-07-21 20:32:03 +00002355
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002356 if (rectxt == NULL)
Daniel Veillard60942de2003-09-25 21:05:58 +00002357 doc = xmlReadMemory((char *) base, info.st_size,
2358 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002359 else
Daniel Veillard60942de2003-09-25 21:05:58 +00002360 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
2361 filename, NULL, options);
Daniel Veillardf1edb102009-08-10 14:43:18 +02002362
Daniel Veillard46e370e2000-07-21 20:32:03 +00002363 munmap((char *) base, info.st_size);
Daniel Veillardf1a27c62006-10-13 22:33:03 +00002364 close(fd);
Daniel Veillard46e370e2000-07-21 20:32:03 +00002365#endif
Daniel Veillard4432df22003-09-28 18:58:27 +00002366#ifdef LIBXML_VALID_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00002367 } else if (valid) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002368 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00002369
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002370 if (rectxt == NULL)
2371 ctxt = xmlNewParserCtxt();
2372 else
2373 ctxt = rectxt;
Daniel Veillardf1edb102009-08-10 14:43:18 +02002374 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002375 doc = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00002376 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002377 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
2378
2379 if (ctxt->valid == 0)
William M. Brack8304d872004-06-08 13:29:32 +00002380 progresult = XMLLINT_ERR_RDFILE;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002381 if (rectxt == NULL)
2382 xmlFreeParserCtxt(ctxt);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002383 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002384#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00002385 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002386 if (rectxt != NULL)
2387 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
Daniel Veillard81562d22005-06-15 13:27:56 +00002388 else {
2389#ifdef LIBXML_SAX1_ENABLED
2390 if (sax1)
2391 doc = xmlParseFile(filename);
2392 else
2393#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002394 doc = xmlReadFile(filename, NULL, options);
Daniel Veillard81562d22005-06-15 13:27:56 +00002395 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00002396 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002397 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002398
Daniel Veillard88a172f2000-08-04 18:23:10 +00002399 /*
2400 * If we don't have a document we might as well give up. Do we
2401 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002402 if (doc == NULL) {
William M. Brack8304d872004-06-08 13:29:32 +00002403 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard88a172f2000-08-04 18:23:10 +00002404 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002405 }
2406
Daniel Veillard48b2f892001-02-25 16:11:03 +00002407 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002408 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002409 }
2410
Daniel Veillard29e43992001-12-13 22:21:58 +00002411 /*
2412 * Remove DOCTYPE nodes
2413 */
2414 if (dropdtd) {
2415 xmlDtdPtr dtd;
2416
2417 dtd = xmlGetIntSubset(doc);
2418 if (dtd != NULL) {
2419 xmlUnlinkNode((xmlNodePtr)dtd);
2420 xmlFreeDtd(dtd);
2421 }
2422 }
2423
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002424#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +00002425 if (xinclude) {
2426 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002427 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002428 }
William M. Brack4e1c2db2005-02-11 10:58:55 +00002429 if (xmlXIncludeProcessFlags(doc, options) < 0)
2430 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard48b2f892001-02-25 16:11:03 +00002431 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002432 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002433 }
2434 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002435#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +00002436
Daniel Veillard1934b0c2009-10-07 10:25:06 +02002437#ifdef LIBXML_XPATH_ENABLED
2438 if (xpathquery != NULL) {
2439 doXPathQuery(doc, xpathquery);
2440 }
2441#endif
2442
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002443#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00002444#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002445 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002446 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002447 */
Daniel Veillard26a45c82006-10-20 12:55:34 +00002448 if (shell) {
2449 xmlXPathOrderDocElems(doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002450 xmlShell(doc, filename, xmlShellReadline, stdout);
Daniel Veillard26a45c82006-10-20 12:55:34 +00002451 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002452#endif
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00002453#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002454
Daniel Veillard652327a2003-09-29 18:02:38 +00002455#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002456 /*
2457 * test intermediate copy if needed.
2458 */
2459 if (copy) {
2460 tmp = doc;
Daniel Veillard4edd3ed2004-09-20 20:03:01 +00002461 if (timing) {
2462 startTimer();
2463 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002464 doc = xmlCopyDoc(doc, 1);
Daniel Veillard4edd3ed2004-09-20 20:03:01 +00002465 if (timing) {
2466 endTimer("Copying");
2467 }
2468 if (timing) {
2469 startTimer();
2470 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002471 xmlFreeDoc(tmp);
Daniel Veillard4edd3ed2004-09-20 20:03:01 +00002472 if (timing) {
2473 endTimer("Freeing original");
2474 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002475 }
Daniel Veillard652327a2003-09-29 18:02:38 +00002476#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002477
Daniel Veillard4432df22003-09-28 18:58:27 +00002478#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002479 if ((insert) && (!html)) {
2480 const xmlChar* list[256];
2481 int nb, i;
2482 xmlNodePtr node;
2483
2484 if (doc->children != NULL) {
2485 node = doc->children;
2486 while ((node != NULL) && (node->last == NULL)) node = node->next;
2487 if (node != NULL) {
2488 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
2489 if (nb < 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002490 fprintf(stderr, "could not get valid list of elements\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002491 } else if (nb == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002492 fprintf(stderr, "No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002493 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002494 fprintf(stderr, "%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002495 nb);
2496 for (i = 0;i < nb;i++) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002497 fprintf(stderr, "%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002498 }
2499 }
2500 }
Daniel Veillardf1edb102009-08-10 14:43:18 +02002501 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002502 }else
2503#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002504#ifdef LIBXML_READER_ENABLED
2505 if (walker) {
2506 walkDoc(doc);
2507 }
2508#endif /* LIBXML_READER_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002509#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00002510 if (noout == 0) {
Daniel Veillard3df01182003-12-10 10:17:51 +00002511 int ret;
2512
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002513 /*
2514 * print it.
2515 */
2516#ifdef LIBXML_DEBUG_ENABLED
2517 if (!debug) {
2518#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00002519 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002520 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002521 }
Daniel Veillard656ce942004-04-30 23:11:45 +00002522#ifdef LIBXML_HTML_ENABLED
Daniel Veillard42fd4122003-11-04 08:47:48 +00002523 if ((html) && (!xmlout)) {
2524 if (compress) {
2525 htmlSaveFile(output ? output : "-", doc);
2526 }
2527 else if (encoding != NULL) {
Adam Spragg5f9d9ce2010-11-01 14:27:11 +01002528 if (format == 1) {
Daniel Veillard42fd4122003-11-04 08:47:48 +00002529 htmlSaveFileFormat(output ? output : "-", doc, encoding, 1);
2530 }
2531 else {
2532 htmlSaveFileFormat(output ? output : "-", doc, encoding, 0);
2533 }
2534 }
Adam Spragg5f9d9ce2010-11-01 14:27:11 +01002535 else if (format == 1) {
Daniel Veillard42fd4122003-11-04 08:47:48 +00002536 htmlSaveFileFormat(output ? output : "-", doc, NULL, 1);
2537 }
2538 else {
2539 FILE *out;
2540 if (output == NULL)
2541 out = stdout;
2542 else {
2543 out = fopen(output,"wb");
2544 }
2545 if (out != NULL) {
2546 if (htmlDocDump(out, doc) < 0)
William M. Brack8304d872004-06-08 13:29:32 +00002547 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00002548
2549 if (output != NULL)
2550 fclose(out);
2551 } else {
2552 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00002553 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00002554 }
2555 }
2556 if ((timing) && (!repeat)) {
2557 endTimer("Saving");
2558 }
2559 } else
2560#endif
Daniel Veillard25048d82004-08-14 22:37:54 +00002561#ifdef LIBXML_C14N_ENABLED
2562 if (canonical) {
2563 xmlChar *result = NULL;
2564 int size;
2565
Aleksey Sanin83868242009-07-09 10:26:22 +02002566 size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_1_0, NULL, 1, &result);
2567 if (size >= 0) {
Stefan Kostdff8d0f2011-05-09 12:14:59 +03002568 if (write(1, result, size) == -1) {
2569 fprintf(stderr, "Can't write data\n");
2570 }
Aleksey Sanin83868242009-07-09 10:26:22 +02002571 xmlFree(result);
2572 } else {
2573 fprintf(stderr, "Failed to canonicalize\n");
2574 progresult = XMLLINT_ERR_OUT;
2575 }
2576 } else if (canonical) {
2577 xmlChar *result = NULL;
2578 int size;
2579
2580 size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_1_1, NULL, 1, &result);
Daniel Veillard25048d82004-08-14 22:37:54 +00002581 if (size >= 0) {
Stefan Kostdff8d0f2011-05-09 12:14:59 +03002582 if (write(1, result, size) == -1) {
2583 fprintf(stderr, "Can't write data\n");
2584 }
Daniel Veillard25048d82004-08-14 22:37:54 +00002585 xmlFree(result);
2586 } else {
2587 fprintf(stderr, "Failed to canonicalize\n");
2588 progresult = XMLLINT_ERR_OUT;
2589 }
2590 } else
Aleksey Sanin2650df12005-06-06 17:16:50 +00002591 if (exc_canonical) {
2592 xmlChar *result = NULL;
2593 int size;
2594
Aleksey Sanin83868242009-07-09 10:26:22 +02002595 size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_EXCLUSIVE_1_0, NULL, 1, &result);
Aleksey Sanin2650df12005-06-06 17:16:50 +00002596 if (size >= 0) {
Stefan Kostdff8d0f2011-05-09 12:14:59 +03002597 if (write(1, result, size) == -1) {
2598 fprintf(stderr, "Can't write data\n");
2599 }
Aleksey Sanin2650df12005-06-06 17:16:50 +00002600 xmlFree(result);
2601 } else {
2602 fprintf(stderr, "Failed to canonicalize\n");
2603 progresult = XMLLINT_ERR_OUT;
2604 }
2605 } else
Daniel Veillard25048d82004-08-14 22:37:54 +00002606#endif
Daniel Richard G5706b6d2012-08-06 11:32:54 +08002607#ifdef HAVE_MMAP
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002608 if (memory) {
2609 xmlChar *result;
2610 int len;
2611
2612 if (encoding != NULL) {
Adam Spragg5f9d9ce2010-11-01 14:27:11 +01002613 if (format == 1) {
Daniel Veillardd536f702001-11-08 17:32:47 +00002614 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
Daniel Veillardf1edb102009-08-10 14:43:18 +02002615 } else {
Daniel Veillardd536f702001-11-08 17:32:47 +00002616 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
2617 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002618 } else {
Adam Spragg5f9d9ce2010-11-01 14:27:11 +01002619 if (format == 1)
Daniel Veillard90493a92001-08-14 14:12:47 +00002620 xmlDocDumpFormatMemory(doc, &result, &len, 1);
2621 else
2622 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002623 }
2624 if (result == NULL) {
2625 fprintf(stderr, "Failed to save\n");
Daniel Veillard25048d82004-08-14 22:37:54 +00002626 progresult = XMLLINT_ERR_OUT;
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002627 } else {
Stefan Kostdff8d0f2011-05-09 12:14:59 +03002628 if (write(1, result, len) == -1) {
2629 fprintf(stderr, "Can't write data\n");
2630 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002631 xmlFree(result);
2632 }
Daniel Veillarddab39b52006-10-16 23:22:10 +00002633
Daniel Veillard3b2c2612001-04-04 00:09:00 +00002634 } else
Daniel Richard G5706b6d2012-08-06 11:32:54 +08002635#endif /* HAVE_MMAP */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002636 if (compress) {
2637 xmlSaveFile(output ? output : "-", doc);
Daniel Veillarddab39b52006-10-16 23:22:10 +00002638 } else if (oldout) {
2639 if (encoding != NULL) {
Adam Spragg5f9d9ce2010-11-01 14:27:11 +01002640 if (format == 1) {
Daniel Veillarddab39b52006-10-16 23:22:10 +00002641 ret = xmlSaveFormatFileEnc(output ? output : "-", doc,
2642 encoding, 1);
2643 }
2644 else {
2645 ret = xmlSaveFileEnc(output ? output : "-", doc,
2646 encoding);
2647 }
2648 if (ret < 0) {
2649 fprintf(stderr, "failed save to %s\n",
2650 output ? output : "-");
2651 progresult = XMLLINT_ERR_OUT;
2652 }
Adam Spragg5f9d9ce2010-11-01 14:27:11 +01002653 } else if (format == 1) {
Daniel Veillarddab39b52006-10-16 23:22:10 +00002654 ret = xmlSaveFormatFile(output ? output : "-", doc, 1);
2655 if (ret < 0) {
2656 fprintf(stderr, "failed save to %s\n",
2657 output ? output : "-");
2658 progresult = XMLLINT_ERR_OUT;
2659 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00002660 } else {
Daniel Veillarddab39b52006-10-16 23:22:10 +00002661 FILE *out;
2662 if (output == NULL)
2663 out = stdout;
2664 else {
2665 out = fopen(output,"wb");
2666 }
2667 if (out != NULL) {
2668 if (xmlDocDump(out, doc) < 0)
2669 progresult = XMLLINT_ERR_OUT;
2670
2671 if (output != NULL)
2672 fclose(out);
2673 } else {
2674 fprintf(stderr, "failed to open %s\n", output);
2675 progresult = XMLLINT_ERR_OUT;
2676 }
2677 }
2678 } else {
2679 xmlSaveCtxtPtr ctxt;
2680 int saveOpts = 0;
2681
Adam Spragg5f9d9ce2010-11-01 14:27:11 +01002682 if (format == 1)
Daniel Veillarddab39b52006-10-16 23:22:10 +00002683 saveOpts |= XML_SAVE_FORMAT;
Adam Spraggd2e62312010-11-03 15:33:40 +01002684 else if (format == 2)
2685 saveOpts |= XML_SAVE_WSNONSIG;
Daniel Veillard9ccea572010-03-10 15:02:49 +01002686
2687#if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
Daniel Veillard9d962642009-08-23 15:31:18 +02002688 if (xmlout)
2689 saveOpts |= XML_SAVE_AS_XML;
Daniel Veillard9ccea572010-03-10 15:02:49 +01002690#endif
Daniel Veillarddab39b52006-10-16 23:22:10 +00002691
2692 if (output == NULL)
2693 ctxt = xmlSaveToFd(1, encoding, saveOpts);
2694 else
2695 ctxt = xmlSaveToFilename(output, encoding, saveOpts);
2696
2697 if (ctxt != NULL) {
2698 if (xmlSaveDoc(ctxt, doc) < 0) {
2699 fprintf(stderr, "failed save to %s\n",
2700 output ? output : "-");
2701 progresult = XMLLINT_ERR_OUT;
2702 }
2703 xmlSaveClose(ctxt);
2704 } else {
William M. Brack8304d872004-06-08 13:29:32 +00002705 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00002706 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002707 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002708 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002709 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002710 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002711#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002712 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002713 FILE *out;
2714 if (output == NULL)
2715 out = stdout;
2716 else {
2717 out = fopen(output,"wb");
2718 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00002719 if (out != NULL) {
2720 xmlDebugDumpDocument(out, doc);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002721
Daniel Veillard05d987b2003-10-08 11:54:57 +00002722 if (output != NULL)
2723 fclose(out);
2724 } else {
2725 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00002726 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00002727 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002728 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002729#endif
2730 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002731#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002732
Daniel Veillard4432df22003-09-28 18:58:27 +00002733#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002734 /*
2735 * A posteriori validation test
2736 */
Daniel Veillard66f68e72003-08-18 16:39:51 +00002737 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
Daniel Veillardcd429612000-10-11 15:57:05 +00002738 xmlDtdPtr dtd;
2739
Daniel Veillard48b2f892001-02-25 16:11:03 +00002740 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002741 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002742 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00002743 if (dtdvalid != NULL)
Daniel Veillardf1edb102009-08-10 14:43:18 +02002744 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
Daniel Veillard66f68e72003-08-18 16:39:51 +00002745 else
Daniel Veillardf1edb102009-08-10 14:43:18 +02002746 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002747 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002748 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002749 }
Daniel Veillardcd429612000-10-11 15:57:05 +00002750 if (dtd == NULL) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00002751 if (dtdvalid != NULL)
2752 xmlGenericError(xmlGenericErrorContext,
2753 "Could not parse DTD %s\n", dtdvalid);
2754 else
2755 xmlGenericError(xmlGenericErrorContext,
2756 "Could not parse DTD %s\n", dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00002757 progresult = XMLLINT_ERR_DTD;
Daniel Veillardcd429612000-10-11 15:57:05 +00002758 } else {
Daniel Veillarda37aab82003-06-09 09:10:36 +00002759 xmlValidCtxtPtr cvp;
2760
2761 if ((cvp = xmlNewValidCtxt()) == NULL) {
2762 xmlGenericError(xmlGenericErrorContext,
2763 "Couldn't allocate validation context\n");
2764 exit(-1);
2765 }
2766 cvp->userData = (void *) stderr;
2767 cvp->error = (xmlValidityErrorFunc) fprintf;
2768 cvp->warning = (xmlValidityWarningFunc) fprintf;
2769
Daniel Veillard48b2f892001-02-25 16:11:03 +00002770 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002771 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002772 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002773 if (!xmlValidateDtd(cvp, doc, dtd)) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00002774 if (dtdvalid != NULL)
2775 xmlGenericError(xmlGenericErrorContext,
2776 "Document %s does not validate against %s\n",
2777 filename, dtdvalid);
2778 else
2779 xmlGenericError(xmlGenericErrorContext,
2780 "Document %s does not validate against %s\n",
2781 filename, dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00002782 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00002783 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002784 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002785 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002786 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002787 xmlFreeValidCtxt(cvp);
Daniel Veillardcd429612000-10-11 15:57:05 +00002788 xmlFreeDtd(dtd);
2789 }
2790 } else if (postvalid) {
Daniel Veillarda37aab82003-06-09 09:10:36 +00002791 xmlValidCtxtPtr cvp;
2792
2793 if ((cvp = xmlNewValidCtxt()) == NULL) {
2794 xmlGenericError(xmlGenericErrorContext,
2795 "Couldn't allocate validation context\n");
2796 exit(-1);
2797 }
2798
Daniel Veillard48b2f892001-02-25 16:11:03 +00002799 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002800 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002801 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002802 cvp->userData = (void *) stderr;
2803 cvp->error = (xmlValidityErrorFunc) fprintf;
2804 cvp->warning = (xmlValidityWarningFunc) fprintf;
2805 if (!xmlValidateDocument(cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00002806 xmlGenericError(xmlGenericErrorContext,
2807 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00002808 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00002809 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002810 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002811 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002812 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002813 xmlFreeValidCtxt(cvp);
Daniel Veillard4432df22003-09-28 18:58:27 +00002814 }
2815#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardd4501d72005-07-24 14:27:16 +00002816#ifdef LIBXML_SCHEMATRON_ENABLED
2817 if (wxschematron != NULL) {
2818 xmlSchematronValidCtxtPtr ctxt;
2819 int ret;
Daniel Veillardc740a172005-07-31 12:17:24 +00002820 int flag;
Daniel Veillardd4501d72005-07-24 14:27:16 +00002821
2822 if ((timing) && (!repeat)) {
2823 startTimer();
2824 }
2825
2826 if (debug)
2827 flag = XML_SCHEMATRON_OUT_XML;
Daniel Veillardc740a172005-07-31 12:17:24 +00002828 else
2829 flag = XML_SCHEMATRON_OUT_TEXT;
2830 if (noout)
2831 flag |= XML_SCHEMATRON_OUT_QUIET;
Daniel Veillardd4501d72005-07-24 14:27:16 +00002832 ctxt = xmlSchematronNewValidCtxt(wxschematron, flag);
2833#if 0
2834 xmlSchematronSetValidErrors(ctxt,
2835 (xmlSchematronValidityErrorFunc) fprintf,
2836 (xmlSchematronValidityWarningFunc) fprintf,
2837 stderr);
2838#endif
2839 ret = xmlSchematronValidateDoc(ctxt, doc);
2840 if (ret == 0) {
2841 fprintf(stderr, "%s validates\n", filename);
2842 } else if (ret > 0) {
2843 fprintf(stderr, "%s fails to validate\n", filename);
2844 progresult = XMLLINT_ERR_VALID;
2845 } else {
2846 fprintf(stderr, "%s validation generated an internal error\n",
2847 filename);
2848 progresult = XMLLINT_ERR_VALID;
2849 }
2850 xmlSchematronFreeValidCtxt(ctxt);
2851 if ((timing) && (!repeat)) {
2852 endTimer("Validating");
2853 }
2854 }
2855#endif
Daniel Veillard71531f32003-02-05 13:19:53 +00002856#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00002857 if (relaxngschemas != NULL) {
Daniel Veillard71531f32003-02-05 13:19:53 +00002858 xmlRelaxNGValidCtxtPtr ctxt;
2859 int ret;
2860
Daniel Veillard42f12e92003-03-07 18:32:59 +00002861 if ((timing) && (!repeat)) {
2862 startTimer();
2863 }
2864
Daniel Veillard71531f32003-02-05 13:19:53 +00002865 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2866 xmlRelaxNGSetValidErrors(ctxt,
2867 (xmlRelaxNGValidityErrorFunc) fprintf,
2868 (xmlRelaxNGValidityWarningFunc) fprintf,
2869 stderr);
2870 ret = xmlRelaxNGValidateDoc(ctxt, doc);
2871 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002872 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard71531f32003-02-05 13:19:53 +00002873 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002874 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00002875 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00002876 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002877 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard71531f32003-02-05 13:19:53 +00002878 filename);
William M. Brack8304d872004-06-08 13:29:32 +00002879 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00002880 }
2881 xmlRelaxNGFreeValidCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002882 if ((timing) && (!repeat)) {
2883 endTimer("Validating");
2884 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002885 } else if (wxschemas != NULL) {
2886 xmlSchemaValidCtxtPtr ctxt;
2887 int ret;
2888
2889 if ((timing) && (!repeat)) {
2890 startTimer();
2891 }
2892
2893 ctxt = xmlSchemaNewValidCtxt(wxschemas);
2894 xmlSchemaSetValidErrors(ctxt,
2895 (xmlSchemaValidityErrorFunc) fprintf,
2896 (xmlSchemaValidityWarningFunc) fprintf,
2897 stderr);
2898 ret = xmlSchemaValidateDoc(ctxt, doc);
2899 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002900 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002901 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002902 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00002903 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002904 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002905 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002906 filename);
William M. Brack8304d872004-06-08 13:29:32 +00002907 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002908 }
2909 xmlSchemaFreeValidCtxt(ctxt);
2910 if ((timing) && (!repeat)) {
2911 endTimer("Validating");
2912 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002913 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002914#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002915
2916#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard6b099012008-11-06 13:47:39 +00002917#if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002918 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00002919 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002920#endif
Daniel Veillard6b099012008-11-06 13:47:39 +00002921#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002922
2923 /*
2924 * free it.
2925 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00002926 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002927 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002928 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002929 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002930 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002931 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002932 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002933}
2934
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002935/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +02002936 * *
2937 * Usage and Main *
2938 * *
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002939 ************************************************************************/
2940
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00002941static void showVersion(const char *name) {
2942 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
2943 fprintf(stderr, " compiled with: ");
Daniel Veillard602434d2005-09-12 09:20:31 +00002944 if (xmlHasFeature(XML_WITH_THREAD)) fprintf(stderr, "Threads ");
2945 if (xmlHasFeature(XML_WITH_TREE)) fprintf(stderr, "Tree ");
2946 if (xmlHasFeature(XML_WITH_OUTPUT)) fprintf(stderr, "Output ");
2947 if (xmlHasFeature(XML_WITH_PUSH)) fprintf(stderr, "Push ");
2948 if (xmlHasFeature(XML_WITH_READER)) fprintf(stderr, "Reader ");
2949 if (xmlHasFeature(XML_WITH_PATTERN)) fprintf(stderr, "Patterns ");
2950 if (xmlHasFeature(XML_WITH_WRITER)) fprintf(stderr, "Writer ");
2951 if (xmlHasFeature(XML_WITH_SAX1)) fprintf(stderr, "SAXv1 ");
Daniel Veillardf1edb102009-08-10 14:43:18 +02002952 if (xmlHasFeature(XML_WITH_FTP)) fprintf(stderr, "FTP ");
2953 if (xmlHasFeature(XML_WITH_HTTP)) fprintf(stderr, "HTTP ");
Daniel Veillard602434d2005-09-12 09:20:31 +00002954 if (xmlHasFeature(XML_WITH_VALID)) fprintf(stderr, "DTDValid ");
Daniel Veillardf1edb102009-08-10 14:43:18 +02002955 if (xmlHasFeature(XML_WITH_HTML)) fprintf(stderr, "HTML ");
2956 if (xmlHasFeature(XML_WITH_LEGACY)) fprintf(stderr, "Legacy ");
2957 if (xmlHasFeature(XML_WITH_C14N)) fprintf(stderr, "C14N ");
2958 if (xmlHasFeature(XML_WITH_CATALOG)) fprintf(stderr, "Catalog ");
2959 if (xmlHasFeature(XML_WITH_XPATH)) fprintf(stderr, "XPath ");
2960 if (xmlHasFeature(XML_WITH_XPTR)) fprintf(stderr, "XPointer ");
2961 if (xmlHasFeature(XML_WITH_XINCLUDE)) fprintf(stderr, "XInclude ");
2962 if (xmlHasFeature(XML_WITH_ICONV)) fprintf(stderr, "Iconv ");
2963 if (xmlHasFeature(XML_WITH_ISO8859X)) fprintf(stderr, "ISO8859X ");
2964 if (xmlHasFeature(XML_WITH_UNICODE)) fprintf(stderr, "Unicode ");
2965 if (xmlHasFeature(XML_WITH_REGEXP)) fprintf(stderr, "Regexps ");
2966 if (xmlHasFeature(XML_WITH_AUTOMATA)) fprintf(stderr, "Automata ");
2967 if (xmlHasFeature(XML_WITH_EXPR)) fprintf(stderr, "Expr ");
2968 if (xmlHasFeature(XML_WITH_SCHEMAS)) fprintf(stderr, "Schemas ");
2969 if (xmlHasFeature(XML_WITH_SCHEMATRON)) fprintf(stderr, "Schematron ");
2970 if (xmlHasFeature(XML_WITH_MODULES)) fprintf(stderr, "Modules ");
2971 if (xmlHasFeature(XML_WITH_DEBUG)) fprintf(stderr, "Debug ");
2972 if (xmlHasFeature(XML_WITH_DEBUG_MEM)) fprintf(stderr, "MemDebug ");
2973 if (xmlHasFeature(XML_WITH_DEBUG_RUN)) fprintf(stderr, "RunDebug ");
Daniel Veillard75acfee2006-07-13 06:29:56 +00002974 if (xmlHasFeature(XML_WITH_ZLIB)) fprintf(stderr, "Zlib ");
Anders F Bjorklundeae52612011-09-18 16:59:13 +02002975 if (xmlHasFeature(XML_WITH_LZMA)) fprintf(stderr, "Lzma ");
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00002976 fprintf(stderr, "\n");
2977}
2978
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002979static void usage(const char *name) {
2980 printf("Usage : %s [options] XMLfiles ...\n", name);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002981#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002982 printf("\tParse the XML files and output the result of the parsing\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002983#else
2984 printf("\tParse the XML files\n");
2985#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002986 printf("\t--version : display the version of the XML library used\n");
2987#ifdef LIBXML_DEBUG_ENABLED
2988 printf("\t--debug : dump a debug tree of the in-memory document\n");
2989 printf("\t--shell : run a navigating shell\n");
2990 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00002991#else
Daniel Veillard81273902003-09-30 00:43:48 +00002992#ifdef LIBXML_READER_ENABLED
Daniel Veillard8326e732003-01-07 00:19:07 +00002993 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard81273902003-09-30 00:43:48 +00002994#endif /* LIBXML_READER_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002995#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00002996#ifdef LIBXML_TREE_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002997 printf("\t--copy : used to test the internal copy implementation\n");
Daniel Veillard652327a2003-09-29 18:02:38 +00002998#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002999 printf("\t--recover : output what was parsable on broken XML documents\n");
Daniel Veillard8915c152008-08-26 13:05:34 +00003000 printf("\t--huge : remove any internal arbitrary parser limits\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003001 printf("\t--noent : substitute entity references by their value\n");
Daniel Veillardc62efc82011-05-16 16:03:50 +08003002 printf("\t--noenc : ignore any encoding specified inside the document\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003003 printf("\t--noout : don't output the result tree\n");
Daniel Veillard0bff36d2004-08-31 09:37:03 +00003004 printf("\t--path 'paths': provide a set of paths for resources\n");
3005 printf("\t--load-trace : print trace of all external entites loaded\n");
Daniel Veillarde8b09e42003-05-13 22:14:13 +00003006 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
Daniel Veillard8874b942005-08-25 13:19:21 +00003007 printf("\t--nocompact : do not generate compact text nodes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003008 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00003009 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00003010#ifdef LIBXML_VALID_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003011 printf("\t--valid : validate the document in addition to std well-formed check\n");
3012 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
3013 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
Daniel Veillard66f68e72003-08-18 16:39:51 +00003014 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00003015#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003016 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003017 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003018 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
3019 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003020#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003021#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003022 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003023#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003024#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003025#ifdef LIBXML_HTML_ENABLED
3026 printf("\t--html : use the HTML parser\n");
Daniel Veillard42fd4122003-11-04 08:47:48 +00003027 printf("\t--xmlout : force to use the XML serializer when using --html\n");
Daniel Veillardf1121c42010-07-26 14:02:42 +02003028 printf("\t--nodefdtd : do not default HTML doctype\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003029#endif
Daniel Veillard73b013f2003-09-30 12:36:01 +00003030#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003031 printf("\t--push : use the push mode of the parser\n");
Daniel Veillard1abd2212012-10-25 15:37:50 +08003032 printf("\t--pushsmall : use the push mode of the parser using tiny increments\n");
Daniel Veillard73b013f2003-09-30 12:36:01 +00003033#endif /* LIBXML_PUSH_ENABLED */
Daniel Richard G5706b6d2012-08-06 11:32:54 +08003034#ifdef HAVE_MMAP
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003035 printf("\t--memory : parse from memory\n");
3036#endif
Daniel Veillard87076042004-05-03 22:54:49 +00003037 printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003038 printf("\t--nowarning : do not emit warnings from parser/validator\n");
3039 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillarddca8cc72003-09-26 13:53:14 +00003040 printf("\t--nocdata : replace cdata section with text nodes\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003041#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00003042 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003043 printf("\t--encode encoding : output in the given encoding\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003044 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
Adam Spraggd2e62312010-11-03 15:33:40 +01003045 printf("\t--pretty STYLE : pretty-print in a particular style\n");
3046 printf("\t 0 Do not pretty print\n");
3047 printf("\t 1 Format the XML content, as --format\n");
3048 printf("\t 2 Add whitespace inside tags, preserving content\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003049#endif /* LIBXML_OUTPUT_ENABLED */
Aleksey Sanin83868242009-07-09 10:26:22 +02003050 printf("\t--c14n : save in W3C canonical format v1.0 (with comments)\n");
3051 printf("\t--c14n11 : save in W3C canonical format v1.1 (with comments)\n");
Aleksey Sanin2650df12005-06-06 17:16:50 +00003052 printf("\t--exc-c14n : save in W3C exclusive canonical format (with comments)\n");
Daniel Veillard25048d82004-08-14 22:37:54 +00003053#ifdef LIBXML_C14N_ENABLED
3054#endif /* LIBXML_C14N_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003055 printf("\t--nsclean : remove redundant namespace declarations\n");
3056 printf("\t--testIO : test user I/O support\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003057#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00003058 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
3059 printf("\t otherwise XML Catalogs starting from \n");
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003060 printf("\t %s are activated by default\n", XML_XML_DEFAULT_CATALOG);
Daniel Veillard05c13a22001-09-09 08:38:09 +00003061 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003062#endif
3063 printf("\t--auto : generate a small doc on the fly\n");
3064#ifdef LIBXML_XINCLUDE_ENABLED
3065 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00003066 printf("\t--noxincludenode : same but do not generate XInclude nodes\n");
Daniel Veillard54bd29b2008-08-26 07:26:55 +00003067 printf("\t--nofixup-base-uris : do not fixup xml:base uris\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003068#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003069 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00003070 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard81273902003-09-30 00:43:48 +00003071#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00003072 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard7899c5c2003-11-03 12:31:38 +00003073 printf("\t--walker : create a reader and walk though the resulting doc\n");
Daniel Veillard81273902003-09-30 00:43:48 +00003074#endif /* LIBXML_READER_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003075#ifdef LIBXML_PATTERN_ENABLED
3076 printf("\t--pattern pattern_value : test the pattern support\n");
3077#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003078 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillardef4d3bc2003-02-07 12:38:22 +00003079#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard71531f32003-02-05 13:19:53 +00003080 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003081 printf("\t--schema schema : do validation against the WXS schema\n");
Daniel Veillard71531f32003-02-05 13:19:53 +00003082#endif
Daniel Veillarde70375c2005-07-30 21:09:12 +00003083#ifdef LIBXML_SCHEMATRON_ENABLED
3084 printf("\t--schematron schema : do validation against a schematron\n");
3085#endif
Daniel Veillard971771e2005-07-09 17:32:57 +00003086#ifdef LIBXML_SAX1_ENABLED
3087 printf("\t--sax1: use the old SAX1 interfaces for processing\n");
3088#endif
3089 printf("\t--sax: do not build a tree but work just at the SAX level\n");
Daniel Veillard7e5c3f42008-07-29 16:12:31 +00003090 printf("\t--oldxml10: use XML-1.0 parsing rules before the 5th edition\n");
Daniel Veillard1934b0c2009-10-07 10:25:06 +02003091#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard113384f2013-03-27 11:43:41 +08003092 printf("\t--xpath expr: evaluate the XPath expression, imply --noout\n");
Daniel Veillard1934b0c2009-10-07 10:25:06 +02003093#endif
Daniel Veillard971771e2005-07-09 17:32:57 +00003094
Daniel Veillarda42f25f2002-01-25 14:15:40 +00003095 printf("\nLibxml project home page: http://xmlsoft.org/\n");
3096 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003097}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003098
3099static void registerNode(xmlNodePtr node)
3100{
3101 node->_private = malloc(sizeof(long));
Daniel Veillarde71dce12013-07-11 15:41:22 +08003102 if (node->_private == NULL) {
3103 fprintf(stderr, "Out of memory in xmllint:registerNode()\n");
3104 exit(XMLLINT_ERR_MEM);
3105 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003106 *(long*)node->_private = (long) 0x81726354;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003107 nbregister++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003108}
3109
3110static void deregisterNode(xmlNodePtr node)
3111{
3112 assert(node->_private != NULL);
3113 assert(*(long*)node->_private == (long) 0x81726354);
3114 free(node->_private);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003115 nbregister--;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003116}
3117
Daniel Veillard4a6845d2001-01-03 13:32:39 +00003118int
3119main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00003120 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003121 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00003122 int version = 0;
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00003123 const char* indent;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003124
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003125 if (argc <= 1) {
3126 usage(argv[0]);
3127 return(1);
3128 }
Daniel Veillardbe803962000-06-28 23:40:59 +00003129 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003130 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003131 if (!strcmp(argv[i], "-"))
3132 break;
3133
3134 if (argv[i][0] != '-')
3135 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003136 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
3137 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00003138 else
3139#ifdef LIBXML_DEBUG_ENABLED
3140 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003141 (!strcmp(argv[i], "--shell"))) {
3142 shell++;
3143 noout = 1;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003144 } else
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003145#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00003146#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003147 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
3148 copy++;
Daniel Veillard652327a2003-09-29 18:02:38 +00003149 else
3150#endif /* LIBXML_TREE_ENABLED */
3151 if ((!strcmp(argv[i], "-recover")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003152 (!strcmp(argv[i], "--recover"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003153 recovery++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003154 options |= XML_PARSE_RECOVER;
Daniel Veillard8915c152008-08-26 13:05:34 +00003155 } else if ((!strcmp(argv[i], "-huge")) ||
3156 (!strcmp(argv[i], "--huge"))) {
3157 options |= XML_PARSE_HUGE;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003158 } else if ((!strcmp(argv[i], "-noent")) ||
3159 (!strcmp(argv[i], "--noent"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003160 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003161 options |= XML_PARSE_NOENT;
Daniel Veillardc62efc82011-05-16 16:03:50 +08003162 } else if ((!strcmp(argv[i], "-noenc")) ||
3163 (!strcmp(argv[i], "--noenc"))) {
3164 noenc++;
3165 options |= XML_PARSE_IGNORE_ENC;
Daniel Veillarddca8cc72003-09-26 13:53:14 +00003166 } else if ((!strcmp(argv[i], "-nsclean")) ||
3167 (!strcmp(argv[i], "--nsclean"))) {
3168 options |= XML_PARSE_NSCLEAN;
3169 } else if ((!strcmp(argv[i], "-nocdata")) ||
3170 (!strcmp(argv[i], "--nocdata"))) {
3171 options |= XML_PARSE_NOCDATA;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003172 } else if ((!strcmp(argv[i], "-nodict")) ||
3173 (!strcmp(argv[i], "--nodict"))) {
3174 options |= XML_PARSE_NODICT;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003175 } else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00003176 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00003177 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00003178 version = 1;
3179 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003180 (!strcmp(argv[i], "--noout")))
3181 noout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003182#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003183 else if ((!strcmp(argv[i], "-o")) ||
3184 (!strcmp(argv[i], "-output")) ||
3185 (!strcmp(argv[i], "--output"))) {
3186 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00003187 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003188 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003189#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003190 else if ((!strcmp(argv[i], "-htmlout")) ||
3191 (!strcmp(argv[i], "--htmlout")))
3192 htmlout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003193 else if ((!strcmp(argv[i], "-nowrap")) ||
3194 (!strcmp(argv[i], "--nowrap")))
3195 nowrap++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003196#ifdef LIBXML_HTML_ENABLED
3197 else if ((!strcmp(argv[i], "-html")) ||
3198 (!strcmp(argv[i], "--html"))) {
3199 html++;
3200 }
Daniel Veillard42fd4122003-11-04 08:47:48 +00003201 else if ((!strcmp(argv[i], "-xmlout")) ||
3202 (!strcmp(argv[i], "--xmlout"))) {
3203 xmlout++;
Daniel Veillardf1121c42010-07-26 14:02:42 +02003204 } else if ((!strcmp(argv[i], "-nodefdtd")) ||
3205 (!strcmp(argv[i], "--nodefdtd"))) {
3206 nodefdtd++;
3207 options |= HTML_PARSE_NODEFDTD;
Daniel Veillard42fd4122003-11-04 08:47:48 +00003208 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003209#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003210 else if ((!strcmp(argv[i], "-loaddtd")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003211 (!strcmp(argv[i], "--loaddtd"))) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003212 loaddtd++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003213 options |= XML_PARSE_DTDLOAD;
3214 } else if ((!strcmp(argv[i], "-dtdattr")) ||
Daniel Veillard48da9102001-08-07 01:10:10 +00003215 (!strcmp(argv[i], "--dtdattr"))) {
3216 loaddtd++;
3217 dtdattrs++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003218 options |= XML_PARSE_DTDATTR;
Daniel Veillard4432df22003-09-28 18:58:27 +00003219 }
3220#ifdef LIBXML_VALID_ENABLED
3221 else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003222 (!strcmp(argv[i], "--valid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003223 valid++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003224 options |= XML_PARSE_DTDVALID;
3225 } else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003226 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003227 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003228 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00003229 options |= XML_PARSE_DTDLOAD;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003230 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00003231 (!strcmp(argv[i], "--dtdvalid"))) {
3232 i++;
3233 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003234 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00003235 options |= XML_PARSE_DTDLOAD;
Daniel Veillard66f68e72003-08-18 16:39:51 +00003236 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
3237 (!strcmp(argv[i], "--dtdvalidfpi"))) {
3238 i++;
3239 dtdvalidfpi = argv[i];
3240 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00003241 options |= XML_PARSE_DTDLOAD;
Daniel Veillardcd429612000-10-11 15:57:05 +00003242 }
Daniel Veillard4432df22003-09-28 18:58:27 +00003243#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard29e43992001-12-13 22:21:58 +00003244 else if ((!strcmp(argv[i], "-dropdtd")) ||
3245 (!strcmp(argv[i], "--dropdtd")))
3246 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003247 else if ((!strcmp(argv[i], "-insert")) ||
3248 (!strcmp(argv[i], "--insert")))
3249 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003250 else if ((!strcmp(argv[i], "-timing")) ||
3251 (!strcmp(argv[i], "--timing")))
3252 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00003253 else if ((!strcmp(argv[i], "-auto")) ||
3254 (!strcmp(argv[i], "--auto")))
3255 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003256 else if ((!strcmp(argv[i], "-repeat")) ||
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00003257 (!strcmp(argv[i], "--repeat"))) {
3258 if (repeat)
3259 repeat *= 10;
3260 else
3261 repeat = 100;
Daniel Veillard73b013f2003-09-30 12:36:01 +00003262 }
3263#ifdef LIBXML_PUSH_ENABLED
3264 else if ((!strcmp(argv[i], "-push")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003265 (!strcmp(argv[i], "--push")))
3266 push++;
Daniel Veillard1abd2212012-10-25 15:37:50 +08003267 else if ((!strcmp(argv[i], "-pushsmall")) ||
3268 (!strcmp(argv[i], "--pushsmall"))) {
3269 push++;
3270 pushsize = 10;
3271 }
Daniel Veillard73b013f2003-09-30 12:36:01 +00003272#endif /* LIBXML_PUSH_ENABLED */
Daniel Richard G5706b6d2012-08-06 11:32:54 +08003273#ifdef HAVE_MMAP
Daniel Veillard46e370e2000-07-21 20:32:03 +00003274 else if ((!strcmp(argv[i], "-memory")) ||
3275 (!strcmp(argv[i], "--memory")))
3276 memory++;
3277#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00003278 else if ((!strcmp(argv[i], "-testIO")) ||
3279 (!strcmp(argv[i], "--testIO")))
3280 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00003281#ifdef LIBXML_XINCLUDE_ENABLED
3282 else if ((!strcmp(argv[i], "-xinclude")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003283 (!strcmp(argv[i], "--xinclude"))) {
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00003284 xinclude++;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00003285 options |= XML_PARSE_XINCLUDE;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003286 }
Daniel Veillardc14c3892004-08-16 12:34:50 +00003287 else if ((!strcmp(argv[i], "-noxincludenode")) ||
3288 (!strcmp(argv[i], "--noxincludenode"))) {
3289 xinclude++;
3290 options |= XML_PARSE_XINCLUDE;
3291 options |= XML_PARSE_NOXINCNODE;
3292 }
Daniel Veillard54bd29b2008-08-26 07:26:55 +00003293 else if ((!strcmp(argv[i], "-nofixup-base-uris")) ||
3294 (!strcmp(argv[i], "--nofixup-base-uris"))) {
3295 xinclude++;
3296 options |= XML_PARSE_XINCLUDE;
3297 options |= XML_PARSE_NOBASEFIX;
3298 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00003299#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003300#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003301#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003302 else if ((!strcmp(argv[i], "-compress")) ||
3303 (!strcmp(argv[i], "--compress"))) {
3304 compress++;
3305 xmlSetCompressMode(9);
3306 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003307#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003308#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003309 else if ((!strcmp(argv[i], "-nowarning")) ||
3310 (!strcmp(argv[i], "--nowarning"))) {
3311 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003312 xmlPedanticParserDefault(0);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003313 options |= XML_PARSE_NOWARNING;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003314 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003315 else if ((!strcmp(argv[i], "-pedantic")) ||
3316 (!strcmp(argv[i], "--pedantic"))) {
3317 xmlGetWarningsDefaultValue = 1;
3318 xmlPedanticParserDefault(1);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003319 options |= XML_PARSE_PEDANTIC;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003320 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00003321#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003322 else if ((!strcmp(argv[i], "-debugent")) ||
3323 (!strcmp(argv[i], "--debugent"))) {
3324 debugent++;
3325 xmlParserDebugEntities = 1;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003326 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00003327#endif
Daniel Veillard25048d82004-08-14 22:37:54 +00003328#ifdef LIBXML_C14N_ENABLED
3329 else if ((!strcmp(argv[i], "-c14n")) ||
3330 (!strcmp(argv[i], "--c14n"))) {
3331 canonical++;
3332 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003333 }
Aleksey Sanin83868242009-07-09 10:26:22 +02003334 else if ((!strcmp(argv[i], "-c14n11")) ||
3335 (!strcmp(argv[i], "--c14n11"))) {
3336 canonical_11++;
3337 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003338 }
Aleksey Sanin2650df12005-06-06 17:16:50 +00003339 else if ((!strcmp(argv[i], "-exc-c14n")) ||
3340 (!strcmp(argv[i], "--exc-c14n"))) {
3341 exc_canonical++;
3342 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003343 }
Daniel Veillard25048d82004-08-14 22:37:54 +00003344#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00003345#ifdef LIBXML_CATALOG_ENABLED
3346 else if ((!strcmp(argv[i], "-catalogs")) ||
3347 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003348 catalogs++;
3349 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
3350 (!strcmp(argv[i], "--nocatalogs"))) {
3351 nocatalogs++;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003352 }
Daniel Veillard81418e32001-05-22 15:08:55 +00003353#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00003354 else if ((!strcmp(argv[i], "-encode")) ||
3355 (!strcmp(argv[i], "--encode"))) {
3356 i++;
3357 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003358 /*
3359 * OK it's for testing purposes
3360 */
3361 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00003362 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003363 else if ((!strcmp(argv[i], "-noblanks")) ||
3364 (!strcmp(argv[i], "--noblanks"))) {
Daniel Veillardf933c892012-09-07 19:32:12 +08003365 noblanks++;
3366 xmlKeepBlanksDefault(0);
3367 options |= XML_PARSE_NOBLANKS;
Daniel Veillard90493a92001-08-14 14:12:47 +00003368 }
Daniel Veillard87076042004-05-03 22:54:49 +00003369 else if ((!strcmp(argv[i], "-maxmem")) ||
3370 (!strcmp(argv[i], "--maxmem"))) {
3371 i++;
3372 if (sscanf(argv[i], "%d", &maxmem) == 1) {
3373 xmlMemSetup(myFreeFunc, myMallocFunc, myReallocFunc,
3374 myStrdupFunc);
3375 } else {
3376 maxmem = 0;
3377 }
3378 }
Daniel Veillard90493a92001-08-14 14:12:47 +00003379 else if ((!strcmp(argv[i], "-format")) ||
3380 (!strcmp(argv[i], "--format"))) {
3381 noblanks++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003382#ifdef LIBXML_OUTPUT_ENABLED
Adam Spragg5f9d9ce2010-11-01 14:27:11 +01003383 format = 1;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003384#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard90493a92001-08-14 14:12:47 +00003385 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00003386 }
Adam Spraggd2e62312010-11-03 15:33:40 +01003387 else if ((!strcmp(argv[i], "-pretty")) ||
3388 (!strcmp(argv[i], "--pretty"))) {
3389 i++;
3390#ifdef LIBXML_OUTPUT_ENABLED
Tim Galeckas2205ff42013-08-29 16:44:33 +08003391 if (argv[i] != NULL) {
3392 format = atoi(argv[i]);
3393 if (format == 1) {
3394 noblanks++;
3395 xmlKeepBlanksDefault(0);
3396 }
3397 }
Brandon Slack0c7109c2012-05-11 10:50:59 +08003398#endif /* LIBXML_OUTPUT_ENABLED */
Adam Spraggd2e62312010-11-03 15:33:40 +01003399 }
Daniel Veillard81273902003-09-30 00:43:48 +00003400#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00003401 else if ((!strcmp(argv[i], "-stream")) ||
3402 (!strcmp(argv[i], "--stream"))) {
3403 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003404 }
Daniel Veillard7899c5c2003-11-03 12:31:38 +00003405 else if ((!strcmp(argv[i], "-walker")) ||
3406 (!strcmp(argv[i], "--walker"))) {
3407 walker++;
3408 noout++;
3409 }
Daniel Veillard81273902003-09-30 00:43:48 +00003410#endif /* LIBXML_READER_ENABLED */
3411#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00003412 else if ((!strcmp(argv[i], "-sax1")) ||
3413 (!strcmp(argv[i], "--sax1"))) {
Daniel Veillard023d0ba2009-07-29 11:34:50 +02003414 sax1++;
3415 options |= XML_PARSE_SAX1;
Daniel Veillard07cb8222003-09-10 10:51:05 +00003416 }
Daniel Veillard81273902003-09-30 00:43:48 +00003417#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003418 else if ((!strcmp(argv[i], "-sax")) ||
3419 (!strcmp(argv[i], "--sax"))) {
Daniel Veillard023d0ba2009-07-29 11:34:50 +02003420 sax++;
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003421 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003422 else if ((!strcmp(argv[i], "-chkregister")) ||
3423 (!strcmp(argv[i], "--chkregister"))) {
Daniel Veillard023d0ba2009-07-29 11:34:50 +02003424 chkregister++;
Daniel Veillard71531f32003-02-05 13:19:53 +00003425#ifdef LIBXML_SCHEMAS_ENABLED
3426 } else if ((!strcmp(argv[i], "-relaxng")) ||
3427 (!strcmp(argv[i], "--relaxng"))) {
3428 i++;
3429 relaxng = argv[i];
3430 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003431 options |= XML_PARSE_NOENT;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003432 } else if ((!strcmp(argv[i], "-schema")) ||
3433 (!strcmp(argv[i], "--schema"))) {
3434 i++;
3435 schema = argv[i];
3436 noent++;
Daniel Veillard71531f32003-02-05 13:19:53 +00003437#endif
Daniel Veillardd4501d72005-07-24 14:27:16 +00003438#ifdef LIBXML_SCHEMATRON_ENABLED
3439 } else if ((!strcmp(argv[i], "-schematron")) ||
3440 (!strcmp(argv[i], "--schematron"))) {
3441 i++;
3442 schematron = argv[i];
3443 noent++;
3444#endif
Daniel Veillarde8b09e42003-05-13 22:14:13 +00003445 } else if ((!strcmp(argv[i], "-nonet")) ||
3446 (!strcmp(argv[i], "--nonet"))) {
Daniel Veillard61b93382003-11-03 14:28:31 +00003447 options |= XML_PARSE_NONET;
Daniel Veillard968d6432006-04-25 16:17:53 +00003448 xmlSetExternalEntityLoader(xmlNoNetExternalEntityLoader);
Daniel Veillard8874b942005-08-25 13:19:21 +00003449 } else if ((!strcmp(argv[i], "-nocompact")) ||
3450 (!strcmp(argv[i], "--nocompact"))) {
3451 options &= ~XML_PARSE_COMPACT;
Daniel Veillard0bff36d2004-08-31 09:37:03 +00003452 } else if ((!strcmp(argv[i], "-load-trace")) ||
3453 (!strcmp(argv[i], "--load-trace"))) {
3454 load_trace++;
3455 } else if ((!strcmp(argv[i], "-path")) ||
3456 (!strcmp(argv[i], "--path"))) {
3457 i++;
3458 parsePath(BAD_CAST argv[i]);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003459#ifdef LIBXML_PATTERN_ENABLED
3460 } else if ((!strcmp(argv[i], "-pattern")) ||
3461 (!strcmp(argv[i], "--pattern"))) {
3462 i++;
3463 pattern = argv[i];
3464#endif
Daniel Veillard1934b0c2009-10-07 10:25:06 +02003465#ifdef LIBXML_XPATH_ENABLED
3466 } else if ((!strcmp(argv[i], "-xpath")) ||
3467 (!strcmp(argv[i], "--xpath"))) {
3468 i++;
3469 noout++;
3470 xpathquery = argv[i];
3471#endif
Daniel Veillard7e5c3f42008-07-29 16:12:31 +00003472 } else if ((!strcmp(argv[i], "-oldxml10")) ||
3473 (!strcmp(argv[i], "--oldxml10"))) {
3474 oldxml10++;
3475 options |= XML_PARSE_OLD10;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003476 } else {
3477 fprintf(stderr, "Unknown option %s\n", argv[i]);
3478 usage(argv[0]);
3479 return(1);
3480 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003481 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003482
3483#ifdef LIBXML_CATALOG_ENABLED
3484 if (nocatalogs == 0) {
3485 if (catalogs) {
3486 const char *catal;
3487
3488 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003489 if (catal != NULL) {
3490 xmlLoadCatalogs(catal);
3491 } else {
3492 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
3493 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003494 }
3495 }
3496#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003497
Daniel Veillard81273902003-09-30 00:43:48 +00003498#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00003499 if (sax1)
3500 xmlSAXDefaultVersion(1);
3501 else
3502 xmlSAXDefaultVersion(2);
Daniel Veillard81273902003-09-30 00:43:48 +00003503#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00003504
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003505 if (chkregister) {
3506 xmlRegisterNodeDefault(registerNode);
3507 xmlDeregisterNodeDefault(deregisterNode);
3508 }
Daniel Veillardf1edb102009-08-10 14:43:18 +02003509
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00003510 indent = getenv("XMLLINT_INDENT");
3511 if(indent != NULL) {
3512 xmlTreeIndentString = indent;
3513 }
Daniel Veillardf1edb102009-08-10 14:43:18 +02003514
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003515
Daniel Veillard0bff36d2004-08-31 09:37:03 +00003516 defaultEntityLoader = xmlGetExternalEntityLoader();
3517 xmlSetExternalEntityLoader(xmllintExternalEntityLoader);
3518
Daniel Veillardd9bad132001-07-23 19:39:43 +00003519 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00003520 if (loaddtd != 0)
3521 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
3522 if (dtdattrs)
3523 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003524 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillard4432df22003-09-28 18:58:27 +00003525#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003526 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard4432df22003-09-28 18:58:27 +00003527#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003528 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00003529 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003530 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00003531 xmlGenericError(xmlGenericErrorContext,
3532 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
3533 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003534 "<html><head><title>%s output</title></head>\n",
3535 argv[0]);
Daniel Veillardf1edb102009-08-10 14:43:18 +02003536 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003537 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
3538 argv[0]);
3539 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003540
Daniel Veillardd4501d72005-07-24 14:27:16 +00003541#ifdef LIBXML_SCHEMATRON_ENABLED
3542 if ((schematron != NULL) && (sax == 0)
3543#ifdef LIBXML_READER_ENABLED
3544 && (stream == 0)
3545#endif /* LIBXML_READER_ENABLED */
3546 ) {
3547 xmlSchematronParserCtxtPtr ctxt;
3548
3549 /* forces loading the DTDs */
Daniel Veillardf1edb102009-08-10 14:43:18 +02003550 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillardd4501d72005-07-24 14:27:16 +00003551 options |= XML_PARSE_DTDLOAD;
3552 if (timing) {
3553 startTimer();
3554 }
3555 ctxt = xmlSchematronNewParserCtxt(schematron);
3556#if 0
3557 xmlSchematronSetParserErrors(ctxt,
3558 (xmlSchematronValidityErrorFunc) fprintf,
3559 (xmlSchematronValidityWarningFunc) fprintf,
3560 stderr);
3561#endif
3562 wxschematron = xmlSchematronParse(ctxt);
3563 if (wxschematron == NULL) {
3564 xmlGenericError(xmlGenericErrorContext,
3565 "Schematron schema %s failed to compile\n", schematron);
3566 progresult = XMLLINT_ERR_SCHEMACOMP;
3567 schematron = NULL;
3568 }
3569 xmlSchematronFreeParserCtxt(ctxt);
3570 if (timing) {
3571 endTimer("Compiling the schemas");
3572 }
3573 }
3574#endif
Daniel Veillard71531f32003-02-05 13:19:53 +00003575#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003576 if ((relaxng != NULL) && (sax == 0)
Daniel Veillard81273902003-09-30 00:43:48 +00003577#ifdef LIBXML_READER_ENABLED
3578 && (stream == 0)
3579#endif /* LIBXML_READER_ENABLED */
3580 ) {
Daniel Veillard71531f32003-02-05 13:19:53 +00003581 xmlRelaxNGParserCtxtPtr ctxt;
3582
Daniel Veillardce192eb2003-04-16 15:58:05 +00003583 /* forces loading the DTDs */
Daniel Veillardf1edb102009-08-10 14:43:18 +02003584 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003585 options |= XML_PARSE_DTDLOAD;
Daniel Veillard42f12e92003-03-07 18:32:59 +00003586 if (timing) {
3587 startTimer();
3588 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003589 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
3590 xmlRelaxNGSetParserErrors(ctxt,
3591 (xmlRelaxNGValidityErrorFunc) fprintf,
3592 (xmlRelaxNGValidityWarningFunc) fprintf,
3593 stderr);
3594 relaxngschemas = xmlRelaxNGParse(ctxt);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003595 if (relaxngschemas == NULL) {
3596 xmlGenericError(xmlGenericErrorContext,
3597 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +00003598 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +00003599 relaxng = NULL;
3600 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003601 xmlRelaxNGFreeParserCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00003602 if (timing) {
3603 endTimer("Compiling the schemas");
3604 }
Daniel Veillardebe25d42004-03-25 09:35:49 +00003605 } else if ((schema != NULL)
3606#ifdef LIBXML_READER_ENABLED
Daniel Veillardf10ae122005-07-10 19:03:16 +00003607 && (stream == 0)
Daniel Veillardebe25d42004-03-25 09:35:49 +00003608#endif
3609 ) {
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003610 xmlSchemaParserCtxtPtr ctxt;
3611
3612 if (timing) {
3613 startTimer();
3614 }
3615 ctxt = xmlSchemaNewParserCtxt(schema);
3616 xmlSchemaSetParserErrors(ctxt,
3617 (xmlSchemaValidityErrorFunc) fprintf,
3618 (xmlSchemaValidityWarningFunc) fprintf,
3619 stderr);
3620 wxschemas = xmlSchemaParse(ctxt);
3621 if (wxschemas == NULL) {
3622 xmlGenericError(xmlGenericErrorContext,
3623 "WXS schema %s failed to compile\n", schema);
William M. Brack8304d872004-06-08 13:29:32 +00003624 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003625 schema = NULL;
3626 }
3627 xmlSchemaFreeParserCtxt(ctxt);
3628 if (timing) {
3629 endTimer("Compiling the schemas");
3630 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003631 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003632#endif /* LIBXML_SCHEMAS_ENABLED */
3633#ifdef LIBXML_PATTERN_ENABLED
Daniel Veillard39e5c892005-07-03 22:48:50 +00003634 if ((pattern != NULL)
Daniel Veillardc9352532005-07-04 14:25:34 +00003635#ifdef LIBXML_READER_ENABLED
Daniel Veillard39e5c892005-07-03 22:48:50 +00003636 && (walker == 0)
3637#endif
3638 ) {
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00003639 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003640 if (patternc == NULL) {
3641 xmlGenericError(xmlGenericErrorContext,
3642 "Pattern %s failed to compile\n", pattern);
William M. Brack8304d872004-06-08 13:29:32 +00003643 progresult = XMLLINT_ERR_SCHEMAPAT;
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003644 pattern = NULL;
3645 }
3646 }
3647#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003648 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00003649 if ((!strcmp(argv[i], "-encode")) ||
3650 (!strcmp(argv[i], "--encode"))) {
3651 i++;
3652 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003653 } else if ((!strcmp(argv[i], "-o")) ||
3654 (!strcmp(argv[i], "-output")) ||
3655 (!strcmp(argv[i], "--output"))) {
3656 i++;
3657 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00003658 }
Daniel Veillard4432df22003-09-28 18:58:27 +00003659#ifdef LIBXML_VALID_ENABLED
Daniel Veillardcd429612000-10-11 15:57:05 +00003660 if ((!strcmp(argv[i], "-dtdvalid")) ||
3661 (!strcmp(argv[i], "--dtdvalid"))) {
3662 i++;
3663 continue;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003664 }
Daniel Veillard0bff36d2004-08-31 09:37:03 +00003665 if ((!strcmp(argv[i], "-path")) ||
3666 (!strcmp(argv[i], "--path"))) {
3667 i++;
3668 continue;
Daniel Veillardcd429612000-10-11 15:57:05 +00003669 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00003670 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
3671 (!strcmp(argv[i], "--dtdvalidfpi"))) {
3672 i++;
3673 continue;
3674 }
Daniel Veillard4432df22003-09-28 18:58:27 +00003675#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00003676 if ((!strcmp(argv[i], "-relaxng")) ||
3677 (!strcmp(argv[i], "--relaxng"))) {
3678 i++;
3679 continue;
3680 }
Daniel Veillard87076042004-05-03 22:54:49 +00003681 if ((!strcmp(argv[i], "-maxmem")) ||
3682 (!strcmp(argv[i], "--maxmem"))) {
3683 i++;
3684 continue;
3685 }
Adam Spraggd2e62312010-11-03 15:33:40 +01003686 if ((!strcmp(argv[i], "-pretty")) ||
3687 (!strcmp(argv[i], "--pretty"))) {
3688 i++;
3689 continue;
3690 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003691 if ((!strcmp(argv[i], "-schema")) ||
3692 (!strcmp(argv[i], "--schema"))) {
3693 i++;
3694 continue;
3695 }
Daniel Veillardd4501d72005-07-24 14:27:16 +00003696 if ((!strcmp(argv[i], "-schematron")) ||
3697 (!strcmp(argv[i], "--schematron"))) {
3698 i++;
3699 continue;
3700 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003701#ifdef LIBXML_PATTERN_ENABLED
3702 if ((!strcmp(argv[i], "-pattern")) ||
3703 (!strcmp(argv[i], "--pattern"))) {
3704 i++;
3705 continue;
3706 }
3707#endif
Daniel Veillard1934b0c2009-10-07 10:25:06 +02003708#ifdef LIBXML_XPATH_ENABLED
3709 if ((!strcmp(argv[i], "-xpath")) ||
3710 (!strcmp(argv[i], "--xpath"))) {
3711 i++;
3712 continue;
3713 }
3714#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00003715 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00003716 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003717 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00003718 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003719 if (repeat) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003720 xmlParserCtxtPtr ctxt = NULL;
3721
3722 for (acount = 0;acount < repeat;acount++) {
Daniel Veillard81273902003-09-30 00:43:48 +00003723#ifdef LIBXML_READER_ENABLED
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003724 if (stream != 0) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00003725 streamFile(argv[i]);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003726 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00003727#endif /* LIBXML_READER_ENABLED */
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003728 if (sax) {
3729 testSAX(argv[i]);
3730 } else {
3731 if (ctxt == NULL)
3732 ctxt = xmlNewParserCtxt();
3733 parseAndPrintFile(argv[i], ctxt);
3734 }
Daniel Veillard81273902003-09-30 00:43:48 +00003735#ifdef LIBXML_READER_ENABLED
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003736 }
Daniel Veillard81273902003-09-30 00:43:48 +00003737#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003738 }
3739 if (ctxt != NULL)
3740 xmlFreeParserCtxt(ctxt);
Daniel Veillard7704fb12003-01-03 16:19:51 +00003741 } else {
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003742 nbregister = 0;
3743
Daniel Veillard81273902003-09-30 00:43:48 +00003744#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00003745 if (stream != 0)
3746 streamFile(argv[i]);
3747 else
Daniel Veillard81273902003-09-30 00:43:48 +00003748#endif /* LIBXML_READER_ENABLED */
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003749 if (sax) {
3750 testSAX(argv[i]);
3751 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003752 parseAndPrintFile(argv[i], NULL);
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003753 }
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003754
3755 if ((chkregister) && (nbregister != 0)) {
3756 fprintf(stderr, "Registration count off: %d\n", nbregister);
William M. Brack8304d872004-06-08 13:29:32 +00003757 progresult = XMLLINT_ERR_RDREGIS;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003758 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00003759 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003760 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00003761 if ((timing) && (repeat)) {
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00003762 endTimer("%d iterations", repeat);
Daniel Veillarda7866932001-12-04 13:14:44 +00003763 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00003764 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003765 }
Daniel Veillardf1edb102009-08-10 14:43:18 +02003766 if (generate)
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003767 parseAndPrintFile(NULL, NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003768 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00003769 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003770 }
Daniel Veillard845cce42002-01-09 11:51:37 +00003771 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003772 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003773 }
Daniel Veillardd4501d72005-07-24 14:27:16 +00003774#ifdef LIBXML_SCHEMATRON_ENABLED
3775 if (wxschematron != NULL)
3776 xmlSchematronFree(wxschematron);
3777#endif
Daniel Veillard71531f32003-02-05 13:19:53 +00003778#ifdef LIBXML_SCHEMAS_ENABLED
3779 if (relaxngschemas != NULL)
3780 xmlRelaxNGFree(relaxngschemas);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003781 if (wxschemas != NULL)
3782 xmlSchemaFree(wxschemas);
Daniel Veillard71531f32003-02-05 13:19:53 +00003783 xmlRelaxNGCleanupTypes();
3784#endif
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003785#ifdef LIBXML_PATTERN_ENABLED
3786 if (patternc != NULL)
3787 xmlFreePattern(patternc);
3788#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003789 xmlCleanupParser();
3790 xmlMemoryDump();
3791
Daniel Veillardf7cd4812001-02-23 18:44:52 +00003792 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003793}
Daniel Veillard88a172f2000-08-04 18:23:10 +00003794