blob: 3edeccb19b5fc76561b1f18769ad11eebd67e6b7 [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 Veillard73b013f2003-09-30 12:36:01 +0000171#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +0000172#ifdef HAVE_SYS_MMAN_H
173static int memory = 0;
174#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +0000175static int testIO = 0;
Daniel Veillardbe803962000-06-28 23:40:59 +0000176static char *encoding = NULL;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000177#ifdef LIBXML_XINCLUDE_ENABLED
178static int xinclude = 0;
179#endif
Daniel Veillard48da9102001-08-07 01:10:10 +0000180static int dtdattrs = 0;
Daniel Veillard10ea86c2001-06-20 13:55:33 +0000181static int loaddtd = 0;
William M. Brack8304d872004-06-08 13:29:32 +0000182static xmllintReturnCode progresult = XMLLINT_RETURN_OK;
Daniel Veillard48b2f892001-02-25 16:11:03 +0000183static int timing = 0;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000184static int generate = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000185static int dropdtd = 0;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000186#ifdef LIBXML_CATALOG_ENABLED
187static int catalogs = 0;
188static int nocatalogs = 0;
189#endif
Daniel Veillard25048d82004-08-14 22:37:54 +0000190#ifdef LIBXML_C14N_ENABLED
191static int canonical = 0;
Aleksey Sanin83868242009-07-09 10:26:22 +0200192static int canonical_11 = 0;
Aleksey Sanin2650df12005-06-06 17:16:50 +0000193static int exc_canonical = 0;
Daniel Veillard25048d82004-08-14 22:37:54 +0000194#endif
Daniel Veillard81273902003-09-30 00:43:48 +0000195#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000196static int stream = 0;
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000197static int walker = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000198#endif /* LIBXML_READER_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000199static int chkregister = 0;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +0000200static int nbregister = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000201#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +0000202static int sax1 = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000203#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000204#ifdef LIBXML_PATTERN_ENABLED
205static const char *pattern = NULL;
206static xmlPatternPtr patternc = NULL;
Daniel Veillard2fc6df92005-01-30 18:42:55 +0000207static xmlStreamCtxtPtr patstream = NULL;
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000208#endif
Daniel Veillard1934b0c2009-10-07 10:25:06 +0200209#ifdef LIBXML_XPATH_ENABLED
210static const char *xpathquery = NULL;
211#endif
Daniel Veillard8874b942005-08-25 13:19:21 +0000212static int options = XML_PARSE_COMPACT;
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000213static int sax = 0;
Daniel Veillard7e5c3f42008-07-29 16:12:31 +0000214static int oldxml10 = 0;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000215
Daniel Veillard87076042004-05-03 22:54:49 +0000216/************************************************************************
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000217 * *
218 * Entity loading control and customization. *
219 * *
220 ************************************************************************/
221#define MAX_PATHS 64
Daniel Veillarded121382007-04-17 12:33:19 +0000222#ifdef _WIN32
223# define PATH_SEPARATOR ';'
224#else
225# define PATH_SEPARATOR ':'
226#endif
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000227static xmlChar *paths[MAX_PATHS + 1];
228static int nbpaths = 0;
229static int load_trace = 0;
230
231static
232void parsePath(const xmlChar *path) {
233 const xmlChar *cur;
234
235 if (path == NULL)
236 return;
237 while (*path != 0) {
238 if (nbpaths >= MAX_PATHS) {
239 fprintf(stderr, "MAX_PATHS reached: too many paths\n");
240 return;
241 }
242 cur = path;
Daniel Veillarded121382007-04-17 12:33:19 +0000243 while ((*cur == ' ') || (*cur == PATH_SEPARATOR))
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000244 cur++;
245 path = cur;
Daniel Veillarded121382007-04-17 12:33:19 +0000246 while ((*cur != 0) && (*cur != ' ') && (*cur != PATH_SEPARATOR))
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000247 cur++;
248 if (cur != path) {
249 paths[nbpaths] = xmlStrndup(path, cur - path);
250 if (paths[nbpaths] != NULL)
251 nbpaths++;
252 path = cur;
253 }
254 }
255}
256
Daniel Veillard24505b02005-07-28 23:49:35 +0000257static xmlExternalEntityLoader defaultEntityLoader = NULL;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000258
Daniel Veillardf1edb102009-08-10 14:43:18 +0200259static xmlParserInputPtr
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000260xmllintExternalEntityLoader(const char *URL, const char *ID,
261 xmlParserCtxtPtr ctxt) {
262 xmlParserInputPtr ret;
263 warningSAXFunc warning = NULL;
Daniel Veillardea71f5d2006-02-19 16:55:55 +0000264 errorSAXFunc err = NULL;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000265
266 int i;
267 const char *lastsegment = URL;
268 const char *iter = URL;
269
Daniel Veillard5608b172008-01-11 06:53:15 +0000270 if ((nbpaths > 0) && (iter != NULL)) {
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000271 while (*iter != 0) {
272 if (*iter == '/')
273 lastsegment = iter + 1;
274 iter++;
275 }
276 }
277
278 if ((ctxt != NULL) && (ctxt->sax != NULL)) {
279 warning = ctxt->sax->warning;
Daniel Veillardea71f5d2006-02-19 16:55:55 +0000280 err = ctxt->sax->error;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000281 ctxt->sax->warning = NULL;
Daniel Veillardea71f5d2006-02-19 16:55:55 +0000282 ctxt->sax->error = NULL;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000283 }
284
285 if (defaultEntityLoader != NULL) {
286 ret = defaultEntityLoader(URL, ID, ctxt);
287 if (ret != NULL) {
288 if (warning != NULL)
289 ctxt->sax->warning = warning;
Daniel Veillardea71f5d2006-02-19 16:55:55 +0000290 if (err != NULL)
291 ctxt->sax->error = err;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000292 if (load_trace) {
293 fprintf \
294 (stderr,
295 "Loaded URL=\"%s\" ID=\"%s\"\n",
296 URL ? URL : "(null)",
297 ID ? ID : "(null)");
298 }
299 return(ret);
300 }
301 }
302 for (i = 0;i < nbpaths;i++) {
303 xmlChar *newURL;
304
305 newURL = xmlStrdup((const xmlChar *) paths[i]);
306 newURL = xmlStrcat(newURL, (const xmlChar *) "/");
307 newURL = xmlStrcat(newURL, (const xmlChar *) lastsegment);
308 if (newURL != NULL) {
309 ret = defaultEntityLoader((const char *)newURL, ID, ctxt);
310 if (ret != NULL) {
311 if (warning != NULL)
312 ctxt->sax->warning = warning;
Daniel Veillardea71f5d2006-02-19 16:55:55 +0000313 if (err != NULL)
314 ctxt->sax->error = err;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000315 if (load_trace) {
316 fprintf \
Daniel Veillardf1edb102009-08-10 14:43:18 +0200317 (stderr,
318 "Loaded URL=\"%s\" ID=\"%s\"\n",
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000319 newURL,
Daniel Veillardf1edb102009-08-10 14:43:18 +0200320 ID ? ID : "(null)");
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000321 }
322 xmlFree(newURL);
323 return(ret);
324 }
325 xmlFree(newURL);
326 }
327 }
Daniel Veillardea71f5d2006-02-19 16:55:55 +0000328 if (err != NULL)
329 ctxt->sax->error = err;
Daniel Veillard0bff36d2004-08-31 09:37:03 +0000330 if (warning != NULL) {
331 ctxt->sax->warning = warning;
332 if (URL != NULL)
333 warning(ctxt, "failed to load external entity \"%s\"\n", URL);
334 else if (ID != NULL)
335 warning(ctxt, "failed to load external entity \"%s\"\n", ID);
336 }
337 return(NULL);
338}
339/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +0200340 * *
Daniel Veillard87076042004-05-03 22:54:49 +0000341 * Memory allocation consumption debugging *
Daniel Veillardf1edb102009-08-10 14:43:18 +0200342 * *
Daniel Veillard87076042004-05-03 22:54:49 +0000343 ************************************************************************/
344
Daniel Veillard3af3b592004-05-05 19:22:30 +0000345static void
346OOM(void)
347{
Daniel Veillard87076042004-05-03 22:54:49 +0000348 fprintf(stderr, "Ran out of memory needs > %d bytes\n", maxmem);
William M. Brack8304d872004-06-08 13:29:32 +0000349 progresult = XMLLINT_ERR_MEM;
Daniel Veillard87076042004-05-03 22:54:49 +0000350}
351
Daniel Veillard3af3b592004-05-05 19:22:30 +0000352static void
353myFreeFunc(void *mem)
354{
Daniel Veillard87076042004-05-03 22:54:49 +0000355 xmlMemFree(mem);
356}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000357static void *
358myMallocFunc(size_t size)
359{
Daniel Veillard87076042004-05-03 22:54:49 +0000360 void *ret;
361
362 ret = xmlMemMalloc(size);
363 if (ret != NULL) {
364 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000365 OOM();
366 xmlMemFree(ret);
367 return (NULL);
368 }
Daniel Veillard87076042004-05-03 22:54:49 +0000369 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000370 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000371}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000372static void *
373myReallocFunc(void *mem, size_t size)
374{
Daniel Veillard87076042004-05-03 22:54:49 +0000375 void *ret;
376
377 ret = xmlMemRealloc(mem, size);
378 if (ret != NULL) {
379 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000380 OOM();
381 xmlMemFree(ret);
382 return (NULL);
383 }
Daniel Veillard87076042004-05-03 22:54:49 +0000384 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000385 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000386}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000387static char *
388myStrdupFunc(const char *str)
389{
Daniel Veillard87076042004-05-03 22:54:49 +0000390 char *ret;
391
392 ret = xmlMemoryStrdup(str);
393 if (ret != NULL) {
394 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000395 OOM();
396 xmlFree(ret);
397 return (NULL);
398 }
Daniel Veillard87076042004-05-03 22:54:49 +0000399 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000400 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000401}
Daniel Veillard87076042004-05-03 22:54:49 +0000402/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +0200403 * *
Daniel Veillard87076042004-05-03 22:54:49 +0000404 * Internal timing routines to remove the necessity to have *
405 * unix-specific function calls. *
Daniel Veillardf1edb102009-08-10 14:43:18 +0200406 * *
Daniel Veillard87076042004-05-03 22:54:49 +0000407 ************************************************************************/
Daniel Veillard01db67c2001-12-18 07:09:59 +0000408
Daniel Veillardf1edb102009-08-10 14:43:18 +0200409#ifndef HAVE_GETTIMEOFDAY
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000410#ifdef HAVE_SYS_TIMEB_H
411#ifdef HAVE_SYS_TIME_H
412#ifdef HAVE_FTIME
413
Daniel Veillard01c13b52002-12-10 15:19:08 +0000414static int
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000415my_gettimeofday(struct timeval *tvp, void *tzp)
416{
417 struct timeb timebuffer;
418
419 ftime(&timebuffer);
420 if (tvp) {
421 tvp->tv_sec = timebuffer.time;
422 tvp->tv_usec = timebuffer.millitm * 1000L;
423 }
424 return (0);
425}
426#define HAVE_GETTIMEOFDAY 1
427#define gettimeofday my_gettimeofday
428
429#endif /* HAVE_FTIME */
430#endif /* HAVE_SYS_TIME_H */
431#endif /* HAVE_SYS_TIMEB_H */
432#endif /* !HAVE_GETTIMEOFDAY */
433
Daniel Veillard01db67c2001-12-18 07:09:59 +0000434#if defined(HAVE_GETTIMEOFDAY)
435static struct timeval begin, end;
436
437/*
438 * startTimer: call where you want to start timing
439 */
440static void
441startTimer(void)
442{
443 gettimeofday(&begin, NULL);
444}
445
446/*
447 * endTimer: call where you want to stop timing and to print out a
448 * message about the timing performed; format is a printf
449 * type argument
450 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000451static void XMLCDECL
Daniel Veillard118aed72002-09-24 14:13:13 +0000452endTimer(const char *fmt, ...)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000453{
454 long msec;
455 va_list ap;
456
457 gettimeofday(&end, NULL);
458 msec = end.tv_sec - begin.tv_sec;
459 msec *= 1000;
460 msec += (end.tv_usec - begin.tv_usec) / 1000;
461
462#ifndef HAVE_STDARG_H
463#error "endTimer required stdarg functions"
464#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000465 va_start(ap, fmt);
466 vfprintf(stderr, fmt, ap);
Daniel Veillard01db67c2001-12-18 07:09:59 +0000467 va_end(ap);
468
469 fprintf(stderr, " took %ld ms\n", msec);
470}
471#elif defined(HAVE_TIME_H)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000472/*
473 * No gettimeofday function, so we have to make do with calling clock.
474 * This is obviously less accurate, but there's little we can do about
475 * that.
476 */
Daniel Veillard90bc3712002-03-07 15:12:58 +0000477#ifndef CLOCKS_PER_SEC
478#define CLOCKS_PER_SEC 100
479#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +0000480
481static clock_t begin, end;
482static void
483startTimer(void)
484{
485 begin = clock();
486}
Daniel Veillardffa3c742005-07-21 13:24:09 +0000487static void XMLCDECL
Daniel Veillard01db67c2001-12-18 07:09:59 +0000488endTimer(const char *fmt, ...)
489{
490 long msec;
491 va_list ap;
492
493 end = clock();
494 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
495
496#ifndef HAVE_STDARG_H
497#error "endTimer required stdarg functions"
498#endif
499 va_start(ap, fmt);
500 vfprintf(stderr, fmt, ap);
501 va_end(ap);
502 fprintf(stderr, " took %ld ms\n", msec);
503}
504#else
505
506/*
507 * We don't have a gettimeofday or time.h, so we just don't do timing
508 */
509static void
510startTimer(void)
511{
512 /*
513 * Do nothing
514 */
515}
Daniel Veillardffa3c742005-07-21 13:24:09 +0000516static void XMLCDECL
Daniel Veillard01db67c2001-12-18 07:09:59 +0000517endTimer(char *format, ...)
518{
519 /*
520 * We cannot do anything because we don't have a timing function
521 */
522#ifdef HAVE_STDARG_H
Patrick R. Gansterer023206f2012-05-10 22:17:51 +0800523 va_list ap;
Daniel Veillard01db67c2001-12-18 07:09:59 +0000524 va_start(ap, format);
525 vfprintf(stderr, format, ap);
526 va_end(ap);
Patrick R. Gansterer023206f2012-05-10 22:17:51 +0800527 fprintf(stderr, " was not timed\n");
Daniel Veillard01db67c2001-12-18 07:09:59 +0000528#else
529 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
530 * this ?!
531 */
532#endif
533}
534#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000535/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +0200536 * *
537 * HTML ouput *
538 * *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000539 ************************************************************************/
Daniel Veillard24505b02005-07-28 23:49:35 +0000540static char buffer[50000];
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000541
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000542static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000543xmlHTMLEncodeSend(void) {
544 char *result;
545
546 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
547 if (result) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000548 xmlGenericError(xmlGenericErrorContext, "%s", result);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000549 xmlFree(result);
550 }
551 buffer[0] = 0;
552}
553
554/**
555 * xmlHTMLPrintFileInfo:
556 * @input: an xmlParserInputPtr input
Daniel Veillardf1edb102009-08-10 14:43:18 +0200557 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000558 * Displays the associated file and line informations for the current input
559 */
560
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000561static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000562xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000563 int len;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000564 xmlGenericError(xmlGenericErrorContext, "<p>");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000565
566 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000567 if (input != NULL) {
568 if (input->filename) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000569 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000570 input->line);
571 } else {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000572 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000573 }
574 }
575 xmlHTMLEncodeSend();
576}
577
578/**
579 * xmlHTMLPrintFileContext:
580 * @input: an xmlParserInputPtr input
Daniel Veillardf1edb102009-08-10 14:43:18 +0200581 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000582 * Displays current context within the input content for error tracking
583 */
584
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000585static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000586xmlHTMLPrintFileContext(xmlParserInputPtr input) {
587 const xmlChar *cur, *base;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000588 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000589 int n;
590
591 if (input == NULL) return;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000592 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000593 cur = input->cur;
594 base = input->base;
595 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
596 cur--;
597 }
598 n = 0;
599 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
600 cur--;
601 if ((*cur == '\n') || (*cur == '\r')) cur++;
602 base = cur;
603 n = 0;
604 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000605 len = strlen(buffer);
Daniel Veillardf1edb102009-08-10 14:43:18 +0200606 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000607 (unsigned char) *cur++);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000608 n++;
609 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000610 len = strlen(buffer);
611 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000612 cur = input->cur;
613 while ((*cur == '\n') || (*cur == '\r'))
614 cur--;
615 n = 0;
616 while ((cur != base) && (n++ < 80)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000617 len = strlen(buffer);
618 snprintf(&buffer[len], sizeof(buffer) - len, " ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000619 base++;
620 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000621 len = strlen(buffer);
622 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000623 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000624 xmlGenericError(xmlGenericErrorContext, "</pre>");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000625}
626
627/**
628 * xmlHTMLError:
629 * @ctx: an XML parser context
630 * @msg: the message to display/transmit
631 * @...: extra parameters for the message display
Daniel Veillardf1edb102009-08-10 14:43:18 +0200632 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000633 * Display and format an error messages, gives file, line, position and
634 * extra parameters.
635 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000636static void XMLCDECL
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000637xmlHTMLError(void *ctx, const char *msg, ...)
638{
639 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
640 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000641 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000642 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000643
644 buffer[0] = 0;
645 input = ctxt->input;
646 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000647 input = ctxt->inputTab[ctxt->inputNr - 2];
648 }
Daniel Veillardf1edb102009-08-10 14:43:18 +0200649
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000650 xmlHTMLPrintFileInfo(input);
651
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000652 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000653 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000654 len = strlen(buffer);
655 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000656 va_end(args);
657 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000658 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000659
660 xmlHTMLPrintFileContext(input);
661 xmlHTMLEncodeSend();
662}
663
664/**
665 * xmlHTMLWarning:
666 * @ctx: an XML parser context
667 * @msg: the message to display/transmit
668 * @...: extra parameters for the message display
Daniel Veillardf1edb102009-08-10 14:43:18 +0200669 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000670 * Display and format a warning messages, gives file, line, position and
671 * extra parameters.
672 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000673static void XMLCDECL
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000674xmlHTMLWarning(void *ctx, const char *msg, ...)
675{
676 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
677 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000678 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000679 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000680
681 buffer[0] = 0;
682 input = ctxt->input;
683 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000684 input = ctxt->inputTab[ctxt->inputNr - 2];
685 }
Daniel Veillardf1edb102009-08-10 14:43:18 +0200686
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000687
688 xmlHTMLPrintFileInfo(input);
Daniel Veillardf1edb102009-08-10 14:43:18 +0200689
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000690 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000691 va_start(args, msg);
Daniel Veillardf1edb102009-08-10 14:43:18 +0200692 len = strlen(buffer);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000693 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000694 va_end(args);
695 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000696 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000697
698 xmlHTMLPrintFileContext(input);
699 xmlHTMLEncodeSend();
700}
701
702/**
703 * xmlHTMLValidityError:
704 * @ctx: an XML parser context
705 * @msg: the message to display/transmit
706 * @...: extra parameters for the message display
Daniel Veillardf1edb102009-08-10 14:43:18 +0200707 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000708 * Display and format an validity error messages, gives file,
709 * line, position and extra parameters.
710 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000711static void XMLCDECL
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000712xmlHTMLValidityError(void *ctx, const char *msg, ...)
713{
714 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
715 xmlParserInputPtr input;
716 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000717 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000718
719 buffer[0] = 0;
720 input = ctxt->input;
721 if ((input->filename == NULL) && (ctxt->inputNr > 1))
722 input = ctxt->inputTab[ctxt->inputNr - 2];
Daniel Veillardf1edb102009-08-10 14:43:18 +0200723
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000724 xmlHTMLPrintFileInfo(input);
725
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000726 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000727 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000728 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000729 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000730 va_end(args);
731 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000732 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000733
734 xmlHTMLPrintFileContext(input);
735 xmlHTMLEncodeSend();
Daniel Veillard9fcb4912005-03-16 12:57:31 +0000736 progresult = XMLLINT_ERR_VALID;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000737}
738
739/**
740 * xmlHTMLValidityWarning:
741 * @ctx: an XML parser context
742 * @msg: the message to display/transmit
743 * @...: extra parameters for the message display
Daniel Veillardf1edb102009-08-10 14:43:18 +0200744 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000745 * Display and format a validity warning messages, gives file, line,
746 * position and extra parameters.
747 */
Daniel Veillardffa3c742005-07-21 13:24:09 +0000748static void XMLCDECL
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000749xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
750{
751 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
752 xmlParserInputPtr input;
753 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000754 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000755
756 buffer[0] = 0;
757 input = ctxt->input;
758 if ((input->filename == NULL) && (ctxt->inputNr > 1))
759 input = ctxt->inputTab[ctxt->inputNr - 2];
760
761 xmlHTMLPrintFileInfo(input);
Daniel Veillardf1edb102009-08-10 14:43:18 +0200762
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000763 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000764 va_start(args, msg);
Daniel Veillardf1edb102009-08-10 14:43:18 +0200765 len = strlen(buffer);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000766 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000767 va_end(args);
768 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000769 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000770
771 xmlHTMLPrintFileContext(input);
772 xmlHTMLEncodeSend();
773}
774
775/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +0200776 * *
777 * Shell Interface *
778 * *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000779 ************************************************************************/
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000780#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardd0cf7f62004-11-09 16:17:02 +0000781#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000782/**
783 * xmlShellReadline:
784 * @prompt: the prompt value
785 *
786 * Read a string
Daniel Veillardf1edb102009-08-10 14:43:18 +0200787 *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000788 * Returns a pointer to it or NULL on EOF the caller is expected to
789 * free the returned string.
790 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000791static char *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000792xmlShellReadline(char *prompt) {
793#ifdef HAVE_LIBREADLINE
794 char *line_read;
795
796 /* Get a line from the user. */
797 line_read = readline (prompt);
798
799 /* If the line has any text in it, save it on the history. */
800 if (line_read && *line_read)
801 add_history (line_read);
802
803 return (line_read);
804#else
805 char line_read[501];
Daniel Veillard29e43992001-12-13 22:21:58 +0000806 char *ret;
807 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000808
809 if (prompt != NULL)
810 fprintf(stdout, "%s", prompt);
811 if (!fgets(line_read, 500, stdin))
812 return(NULL);
813 line_read[500] = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000814 len = strlen(line_read);
815 ret = (char *) malloc(len + 1);
816 if (ret != NULL) {
817 memcpy (ret, line_read, len + 1);
818 }
819 return(ret);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000820#endif
821}
Daniel Veillardd0cf7f62004-11-09 16:17:02 +0000822#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000823#endif /* LIBXML_DEBUG_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000824
825/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +0200826 * *
827 * I/O Interfaces *
828 * *
Daniel Veillard5e873c42000-04-12 13:27:38 +0000829 ************************************************************************/
830
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000831static int myRead(FILE *f, char * buf, int len) {
832 return(fread(buf, 1, len, f));
Daniel Veillard5e873c42000-04-12 13:27:38 +0000833}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000834static void myClose(FILE *f) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000835 if (f != stdin) {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000836 fclose(f);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000837 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000838}
839
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000840/************************************************************************
841 * *
Daniel Veillardf1edb102009-08-10 14:43:18 +0200842 * SAX based tests *
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000843 * *
844 ************************************************************************/
845
846/*
847 * empty SAX block
848 */
Daniel Veillard24505b02005-07-28 23:49:35 +0000849static xmlSAXHandler emptySAXHandlerStruct = {
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000850 NULL, /* internalSubset */
851 NULL, /* isStandalone */
852 NULL, /* hasInternalSubset */
853 NULL, /* hasExternalSubset */
854 NULL, /* resolveEntity */
855 NULL, /* getEntity */
856 NULL, /* entityDecl */
857 NULL, /* notationDecl */
858 NULL, /* attributeDecl */
859 NULL, /* elementDecl */
860 NULL, /* unparsedEntityDecl */
861 NULL, /* setDocumentLocator */
862 NULL, /* startDocument */
863 NULL, /* endDocument */
864 NULL, /* startElement */
865 NULL, /* endElement */
866 NULL, /* reference */
867 NULL, /* characters */
868 NULL, /* ignorableWhitespace */
869 NULL, /* processingInstruction */
870 NULL, /* comment */
871 NULL, /* xmlParserWarning */
872 NULL, /* xmlParserError */
873 NULL, /* xmlParserError */
874 NULL, /* getParameterEntity */
875 NULL, /* cdataBlock; */
876 NULL, /* externalSubset; */
Daniel Veillard971771e2005-07-09 17:32:57 +0000877 XML_SAX2_MAGIC,
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000878 NULL,
879 NULL, /* startElementNs */
880 NULL, /* endElementNs */
881 NULL /* xmlStructuredErrorFunc */
882};
883
Daniel Veillard24505b02005-07-28 23:49:35 +0000884static xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
Daniel Veillardf0af8ec2005-07-08 17:27:33 +0000885extern xmlSAXHandlerPtr debugSAXHandler;
886static int callbacks;
887
888/**
889 * isStandaloneDebug:
890 * @ctxt: An XML parser context
891 *
892 * Is this document tagged standalone ?
893 *
894 * Returns 1 if true
895 */
896static int
897isStandaloneDebug(void *ctx ATTRIBUTE_UNUSED)
898{
899 callbacks++;
900 if (noout)
901 return(0);
902 fprintf(stdout, "SAX.isStandalone()\n");
903 return(0);
904}
905
906/**
907 * hasInternalSubsetDebug:
908 * @ctxt: An XML parser context
909 *
910 * Does this document has an internal subset
911 *
912 * Returns 1 if true
913 */
914static int
915hasInternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
916{
917 callbacks++;
918 if (noout)
919 return(0);
920 fprintf(stdout, "SAX.hasInternalSubset()\n");
921 return(0);
922}
923
924/**
925 * hasExternalSubsetDebug:
926 * @ctxt: An XML parser context
927 *
928 * Does this document has an external subset
929 *
930 * Returns 1 if true
931 */
932static int
933hasExternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
934{
935 callbacks++;
936 if (noout)
937 return(0);
938 fprintf(stdout, "SAX.hasExternalSubset()\n");
939 return(0);
940}
941
942/**
943 * internalSubsetDebug:
944 * @ctxt: An XML parser context
945 *
946 * Does this document has an internal subset
947 */
948static void
949internalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
950 const xmlChar *ExternalID, const xmlChar *SystemID)
951{
952 callbacks++;
953 if (noout)
954 return;
955 fprintf(stdout, "SAX.internalSubset(%s,", name);
956 if (ExternalID == NULL)
957 fprintf(stdout, " ,");
958 else
959 fprintf(stdout, " %s,", ExternalID);
960 if (SystemID == NULL)
961 fprintf(stdout, " )\n");
962 else
963 fprintf(stdout, " %s)\n", SystemID);
964}
965
966/**
967 * externalSubsetDebug:
968 * @ctxt: An XML parser context
969 *
970 * Does this document has an external subset
971 */
972static void
973externalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
974 const xmlChar *ExternalID, const xmlChar *SystemID)
975{
976 callbacks++;
977 if (noout)
978 return;
979 fprintf(stdout, "SAX.externalSubset(%s,", name);
980 if (ExternalID == NULL)
981 fprintf(stdout, " ,");
982 else
983 fprintf(stdout, " %s,", ExternalID);
984 if (SystemID == NULL)
985 fprintf(stdout, " )\n");
986 else
987 fprintf(stdout, " %s)\n", SystemID);
988}
989
990/**
991 * resolveEntityDebug:
992 * @ctxt: An XML parser context
993 * @publicId: The public ID of the entity
994 * @systemId: The system ID of the entity
995 *
996 * Special entity resolver, better left to the parser, it has
997 * more context than the application layer.
998 * The default behaviour is to NOT resolve the entities, in that case
999 * the ENTITY_REF nodes are built in the structure (and the parameter
1000 * values).
1001 *
1002 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
1003 */
1004static xmlParserInputPtr
1005resolveEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *publicId, const xmlChar *systemId)
1006{
1007 callbacks++;
1008 if (noout)
1009 return(NULL);
1010 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
1011
Daniel Veillardf1edb102009-08-10 14:43:18 +02001012
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001013 fprintf(stdout, "SAX.resolveEntity(");
1014 if (publicId != NULL)
1015 fprintf(stdout, "%s", (char *)publicId);
1016 else
1017 fprintf(stdout, " ");
1018 if (systemId != NULL)
1019 fprintf(stdout, ", %s)\n", (char *)systemId);
1020 else
1021 fprintf(stdout, ", )\n");
1022 return(NULL);
1023}
1024
1025/**
1026 * getEntityDebug:
1027 * @ctxt: An XML parser context
1028 * @name: The entity name
1029 *
1030 * Get an entity by name
1031 *
1032 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
1033 */
1034static xmlEntityPtr
1035getEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1036{
1037 callbacks++;
1038 if (noout)
1039 return(NULL);
1040 fprintf(stdout, "SAX.getEntity(%s)\n", name);
1041 return(NULL);
1042}
1043
1044/**
1045 * getParameterEntityDebug:
1046 * @ctxt: An XML parser context
1047 * @name: The entity name
1048 *
1049 * Get a parameter entity by name
1050 *
1051 * Returns the xmlParserInputPtr
1052 */
1053static xmlEntityPtr
1054getParameterEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1055{
1056 callbacks++;
1057 if (noout)
1058 return(NULL);
1059 fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
1060 return(NULL);
1061}
1062
1063
1064/**
1065 * entityDeclDebug:
1066 * @ctxt: An XML parser context
Daniel Veillardf1edb102009-08-10 14:43:18 +02001067 * @name: the entity name
1068 * @type: the entity type
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001069 * @publicId: The public ID of the entity
1070 * @systemId: The system ID of the entity
1071 * @content: the entity value (without processing).
1072 *
1073 * An entity definition has been parsed
1074 */
1075static void
1076entityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
1077 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
1078{
1079const xmlChar *nullstr = BAD_CAST "(null)";
1080 /* not all libraries handle printing null pointers nicely */
1081 if (publicId == NULL)
1082 publicId = nullstr;
1083 if (systemId == NULL)
1084 systemId = nullstr;
1085 if (content == NULL)
1086 content = (xmlChar *)nullstr;
1087 callbacks++;
1088 if (noout)
1089 return;
1090 fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
1091 name, type, publicId, systemId, content);
1092}
1093
1094/**
1095 * attributeDeclDebug:
1096 * @ctxt: An XML parser context
Daniel Veillardf1edb102009-08-10 14:43:18 +02001097 * @name: the attribute name
1098 * @type: the attribute type
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001099 *
1100 * An attribute definition has been parsed
1101 */
1102static void
1103attributeDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar * elem,
1104 const xmlChar * name, int type, int def,
1105 const xmlChar * defaultValue, xmlEnumerationPtr tree)
1106{
1107 callbacks++;
1108 if (noout)
1109 return;
1110 if (defaultValue == NULL)
1111 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, NULL, ...)\n",
1112 elem, name, type, def);
1113 else
1114 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
1115 elem, name, type, def, defaultValue);
1116 xmlFreeEnumeration(tree);
1117}
1118
1119/**
1120 * elementDeclDebug:
1121 * @ctxt: An XML parser context
Daniel Veillardf1edb102009-08-10 14:43:18 +02001122 * @name: the element name
1123 * @type: the element type
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001124 * @content: the element value (without processing).
1125 *
1126 * An element definition has been parsed
1127 */
1128static void
1129elementDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
1130 xmlElementContentPtr content ATTRIBUTE_UNUSED)
1131{
1132 callbacks++;
1133 if (noout)
1134 return;
1135 fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
1136 name, type);
1137}
1138
1139/**
1140 * notationDeclDebug:
1141 * @ctxt: An XML parser context
1142 * @name: The name of the notation
1143 * @publicId: The public ID of the entity
1144 * @systemId: The system ID of the entity
1145 *
1146 * What to do when a notation declaration has been parsed.
1147 */
1148static void
1149notationDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
1150 const xmlChar *publicId, const xmlChar *systemId)
1151{
1152 callbacks++;
1153 if (noout)
1154 return;
1155 fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
1156 (char *) name, (char *) publicId, (char *) systemId);
1157}
1158
1159/**
1160 * unparsedEntityDeclDebug:
1161 * @ctxt: An XML parser context
1162 * @name: The name of the entity
1163 * @publicId: The public ID of the entity
1164 * @systemId: The system ID of the entity
1165 * @notationName: the name of the notation
1166 *
1167 * What to do when an unparsed entity declaration is parsed
1168 */
1169static void
1170unparsedEntityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
1171 const xmlChar *publicId, const xmlChar *systemId,
1172 const xmlChar *notationName)
1173{
1174const xmlChar *nullstr = BAD_CAST "(null)";
1175
1176 if (publicId == NULL)
1177 publicId = nullstr;
1178 if (systemId == NULL)
1179 systemId = nullstr;
1180 if (notationName == NULL)
1181 notationName = nullstr;
1182 callbacks++;
1183 if (noout)
1184 return;
1185 fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
1186 (char *) name, (char *) publicId, (char *) systemId,
1187 (char *) notationName);
1188}
1189
1190/**
1191 * setDocumentLocatorDebug:
1192 * @ctxt: An XML parser context
1193 * @loc: A SAX Locator
1194 *
1195 * Receive the document locator at startup, actually xmlDefaultSAXLocator
1196 * Everything is available on the context, so this is useless in our case.
1197 */
1198static void
1199setDocumentLocatorDebug(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
1200{
1201 callbacks++;
1202 if (noout)
1203 return;
1204 fprintf(stdout, "SAX.setDocumentLocator()\n");
1205}
1206
1207/**
1208 * startDocumentDebug:
1209 * @ctxt: An XML parser context
1210 *
1211 * called when the document start being processed.
1212 */
1213static void
1214startDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
1215{
1216 callbacks++;
1217 if (noout)
1218 return;
1219 fprintf(stdout, "SAX.startDocument()\n");
1220}
1221
1222/**
1223 * endDocumentDebug:
1224 * @ctxt: An XML parser context
1225 *
1226 * called when the document end has been detected.
1227 */
1228static void
1229endDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
1230{
1231 callbacks++;
1232 if (noout)
1233 return;
1234 fprintf(stdout, "SAX.endDocument()\n");
1235}
1236
1237/**
1238 * startElementDebug:
1239 * @ctxt: An XML parser context
1240 * @name: The element name
1241 *
1242 * called when an opening tag has been processed.
1243 */
1244static void
1245startElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, const xmlChar **atts)
1246{
1247 int i;
1248
1249 callbacks++;
1250 if (noout)
1251 return;
1252 fprintf(stdout, "SAX.startElement(%s", (char *) name);
1253 if (atts != NULL) {
1254 for (i = 0;(atts[i] != NULL);i++) {
1255 fprintf(stdout, ", %s='", atts[i++]);
1256 if (atts[i] != NULL)
1257 fprintf(stdout, "%s'", atts[i]);
1258 }
1259 }
1260 fprintf(stdout, ")\n");
1261}
1262
1263/**
1264 * endElementDebug:
1265 * @ctxt: An XML parser context
1266 * @name: The element name
1267 *
1268 * called when the end of an element has been detected.
1269 */
1270static void
1271endElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1272{
1273 callbacks++;
1274 if (noout)
1275 return;
1276 fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
1277}
1278
1279/**
1280 * charactersDebug:
1281 * @ctxt: An XML parser context
1282 * @ch: a xmlChar string
1283 * @len: the number of xmlChar
1284 *
1285 * receiving some chars from the parser.
1286 * Question: how much at a time ???
1287 */
1288static void
1289charactersDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
1290{
1291 char out[40];
1292 int i;
1293
1294 callbacks++;
1295 if (noout)
1296 return;
1297 for (i = 0;(i<len) && (i < 30);i++)
1298 out[i] = ch[i];
1299 out[i] = 0;
1300
1301 fprintf(stdout, "SAX.characters(%s, %d)\n", out, len);
1302}
1303
1304/**
1305 * referenceDebug:
1306 * @ctxt: An XML parser context
1307 * @name: The entity name
1308 *
Daniel Veillardf1edb102009-08-10 14:43:18 +02001309 * called when an entity reference is detected.
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001310 */
1311static void
1312referenceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1313{
1314 callbacks++;
1315 if (noout)
1316 return;
1317 fprintf(stdout, "SAX.reference(%s)\n", name);
1318}
1319
1320/**
1321 * ignorableWhitespaceDebug:
1322 * @ctxt: An XML parser context
1323 * @ch: a xmlChar string
1324 * @start: the first char in the string
1325 * @len: the number of xmlChar
1326 *
1327 * receiving some ignorable whitespaces from the parser.
1328 * Question: how much at a time ???
1329 */
1330static void
1331ignorableWhitespaceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
1332{
1333 char out[40];
1334 int i;
1335
1336 callbacks++;
1337 if (noout)
1338 return;
1339 for (i = 0;(i<len) && (i < 30);i++)
1340 out[i] = ch[i];
1341 out[i] = 0;
1342 fprintf(stdout, "SAX.ignorableWhitespace(%s, %d)\n", out, len);
1343}
1344
1345/**
1346 * processingInstructionDebug:
1347 * @ctxt: An XML parser context
1348 * @target: the target name
1349 * @data: the PI data's
1350 * @len: the number of xmlChar
1351 *
1352 * A processing instruction has been parsed.
1353 */
1354static void
1355processingInstructionDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *target,
1356 const xmlChar *data)
1357{
1358 callbacks++;
1359 if (noout)
1360 return;
1361 if (data != NULL)
1362 fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
1363 (char *) target, (char *) data);
1364 else
1365 fprintf(stdout, "SAX.processingInstruction(%s, NULL)\n",
1366 (char *) target);
1367}
1368
1369/**
1370 * cdataBlockDebug:
1371 * @ctx: the user data (XML parser context)
1372 * @value: The pcdata content
1373 * @len: the block length
1374 *
1375 * called when a pcdata block has been parsed
1376 */
1377static void
1378cdataBlockDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value, int len)
1379{
1380 callbacks++;
1381 if (noout)
1382 return;
1383 fprintf(stdout, "SAX.pcdata(%.20s, %d)\n",
1384 (char *) value, len);
1385}
1386
1387/**
1388 * commentDebug:
1389 * @ctxt: An XML parser context
1390 * @value: the comment content
1391 *
1392 * A comment has been parsed.
1393 */
1394static void
1395commentDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value)
1396{
1397 callbacks++;
1398 if (noout)
1399 return;
1400 fprintf(stdout, "SAX.comment(%s)\n", value);
1401}
1402
1403/**
1404 * warningDebug:
1405 * @ctxt: An XML parser context
1406 * @msg: the message to display/transmit
1407 * @...: extra parameters for the message display
1408 *
1409 * Display and format a warning messages, gives file, line, position and
1410 * extra parameters.
1411 */
Daniel Veillardffa3c742005-07-21 13:24:09 +00001412static void XMLCDECL
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001413warningDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1414{
1415 va_list args;
1416
1417 callbacks++;
1418 if (noout)
1419 return;
1420 va_start(args, msg);
1421 fprintf(stdout, "SAX.warning: ");
1422 vfprintf(stdout, msg, args);
1423 va_end(args);
1424}
1425
1426/**
1427 * errorDebug:
1428 * @ctxt: An XML parser context
1429 * @msg: the message to display/transmit
1430 * @...: extra parameters for the message display
1431 *
1432 * Display and format a error messages, gives file, line, position and
1433 * extra parameters.
1434 */
Daniel Veillardffa3c742005-07-21 13:24:09 +00001435static void XMLCDECL
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001436errorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1437{
1438 va_list args;
1439
1440 callbacks++;
1441 if (noout)
1442 return;
1443 va_start(args, msg);
1444 fprintf(stdout, "SAX.error: ");
1445 vfprintf(stdout, msg, args);
1446 va_end(args);
1447}
1448
1449/**
1450 * fatalErrorDebug:
1451 * @ctxt: An XML parser context
1452 * @msg: the message to display/transmit
1453 * @...: extra parameters for the message display
1454 *
1455 * Display and format a fatalError messages, gives file, line, position and
1456 * extra parameters.
1457 */
Daniel Veillardffa3c742005-07-21 13:24:09 +00001458static void XMLCDECL
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001459fatalErrorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1460{
1461 va_list args;
1462
1463 callbacks++;
1464 if (noout)
1465 return;
1466 va_start(args, msg);
1467 fprintf(stdout, "SAX.fatalError: ");
1468 vfprintf(stdout, msg, args);
1469 va_end(args);
1470}
1471
Daniel Veillard24505b02005-07-28 23:49:35 +00001472static xmlSAXHandler debugSAXHandlerStruct = {
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001473 internalSubsetDebug,
1474 isStandaloneDebug,
1475 hasInternalSubsetDebug,
1476 hasExternalSubsetDebug,
1477 resolveEntityDebug,
1478 getEntityDebug,
1479 entityDeclDebug,
1480 notationDeclDebug,
1481 attributeDeclDebug,
1482 elementDeclDebug,
1483 unparsedEntityDeclDebug,
1484 setDocumentLocatorDebug,
1485 startDocumentDebug,
1486 endDocumentDebug,
1487 startElementDebug,
1488 endElementDebug,
1489 referenceDebug,
1490 charactersDebug,
1491 ignorableWhitespaceDebug,
1492 processingInstructionDebug,
1493 commentDebug,
1494 warningDebug,
1495 errorDebug,
1496 fatalErrorDebug,
1497 getParameterEntityDebug,
1498 cdataBlockDebug,
1499 externalSubsetDebug,
1500 1,
1501 NULL,
1502 NULL,
1503 NULL,
1504 NULL
1505};
1506
1507xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
1508
1509/*
1510 * SAX2 specific callbacks
1511 */
1512/**
1513 * startElementNsDebug:
1514 * @ctxt: An XML parser context
1515 * @name: The element name
1516 *
1517 * called when an opening tag has been processed.
1518 */
1519static void
1520startElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
1521 const xmlChar *localname,
1522 const xmlChar *prefix,
1523 const xmlChar *URI,
1524 int nb_namespaces,
1525 const xmlChar **namespaces,
1526 int nb_attributes,
1527 int nb_defaulted,
1528 const xmlChar **attributes)
1529{
1530 int i;
1531
1532 callbacks++;
1533 if (noout)
1534 return;
1535 fprintf(stdout, "SAX.startElementNs(%s", (char *) localname);
1536 if (prefix == NULL)
1537 fprintf(stdout, ", NULL");
1538 else
1539 fprintf(stdout, ", %s", (char *) prefix);
1540 if (URI == NULL)
1541 fprintf(stdout, ", NULL");
1542 else
1543 fprintf(stdout, ", '%s'", (char *) URI);
1544 fprintf(stdout, ", %d", nb_namespaces);
Daniel Veillardf1edb102009-08-10 14:43:18 +02001545
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001546 if (namespaces != NULL) {
1547 for (i = 0;i < nb_namespaces * 2;i++) {
1548 fprintf(stdout, ", xmlns");
1549 if (namespaces[i] != NULL)
1550 fprintf(stdout, ":%s", namespaces[i]);
1551 i++;
1552 fprintf(stdout, "='%s'", namespaces[i]);
1553 }
1554 }
1555 fprintf(stdout, ", %d, %d", nb_attributes, nb_defaulted);
1556 if (attributes != NULL) {
1557 for (i = 0;i < nb_attributes * 5;i += 5) {
1558 if (attributes[i + 1] != NULL)
1559 fprintf(stdout, ", %s:%s='", attributes[i + 1], attributes[i]);
1560 else
1561 fprintf(stdout, ", %s='", attributes[i]);
1562 fprintf(stdout, "%.4s...', %d", attributes[i + 3],
1563 (int)(attributes[i + 4] - attributes[i + 3]));
1564 }
1565 }
1566 fprintf(stdout, ")\n");
1567}
1568
1569/**
1570 * endElementDebug:
1571 * @ctxt: An XML parser context
1572 * @name: The element name
1573 *
1574 * called when the end of an element has been detected.
1575 */
1576static void
1577endElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
1578 const xmlChar *localname,
1579 const xmlChar *prefix,
1580 const xmlChar *URI)
1581{
1582 callbacks++;
1583 if (noout)
1584 return;
1585 fprintf(stdout, "SAX.endElementNs(%s", (char *) localname);
1586 if (prefix == NULL)
1587 fprintf(stdout, ", NULL");
1588 else
1589 fprintf(stdout, ", %s", (char *) prefix);
1590 if (URI == NULL)
1591 fprintf(stdout, ", NULL)\n");
1592 else
1593 fprintf(stdout, ", '%s')\n", (char *) URI);
1594}
1595
Daniel Veillard24505b02005-07-28 23:49:35 +00001596static xmlSAXHandler debugSAX2HandlerStruct = {
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001597 internalSubsetDebug,
1598 isStandaloneDebug,
1599 hasInternalSubsetDebug,
1600 hasExternalSubsetDebug,
1601 resolveEntityDebug,
1602 getEntityDebug,
1603 entityDeclDebug,
1604 notationDeclDebug,
1605 attributeDeclDebug,
1606 elementDeclDebug,
1607 unparsedEntityDeclDebug,
1608 setDocumentLocatorDebug,
1609 startDocumentDebug,
1610 endDocumentDebug,
1611 NULL,
1612 NULL,
1613 referenceDebug,
1614 charactersDebug,
1615 ignorableWhitespaceDebug,
1616 processingInstructionDebug,
1617 commentDebug,
1618 warningDebug,
1619 errorDebug,
1620 fatalErrorDebug,
1621 getParameterEntityDebug,
1622 cdataBlockDebug,
1623 externalSubsetDebug,
1624 XML_SAX2_MAGIC,
1625 NULL,
1626 startElementNsDebug,
1627 endElementNsDebug,
1628 NULL
1629};
1630
Daniel Veillard24505b02005-07-28 23:49:35 +00001631static xmlSAXHandlerPtr debugSAX2Handler = &debugSAX2HandlerStruct;
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001632
1633static void
1634testSAX(const char *filename) {
1635 xmlSAXHandlerPtr handler;
1636 const char *user_data = "user_data"; /* mostly for debugging */
1637 xmlParserInputBufferPtr buf = NULL;
1638 xmlParserInputPtr inputStream;
1639 xmlParserCtxtPtr ctxt = NULL;
1640 xmlSAXHandlerPtr old_sax = NULL;
1641
1642 callbacks = 0;
1643
1644 if (noout) {
1645 handler = emptySAXHandler;
Daniel Veillard78dfc9f2005-07-10 22:30:30 +00001646#ifdef LIBXML_SAX1_ENABLED
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001647 } else if (sax1) {
1648 handler = debugSAXHandler;
Daniel Veillard78dfc9f2005-07-10 22:30:30 +00001649#endif
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001650 } else {
1651 handler = debugSAX2Handler;
1652 }
1653
1654 /*
1655 * it's not the simplest code but the most generic in term of I/O
1656 */
1657 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
1658 if (buf == NULL) {
1659 goto error;
1660 }
1661
1662#ifdef LIBXML_SCHEMAS_ENABLED
1663 if (wxschemas != NULL) {
1664 int ret;
1665 xmlSchemaValidCtxtPtr vctxt;
1666
1667 vctxt = xmlSchemaNewValidCtxt(wxschemas);
1668 xmlSchemaSetValidErrors(vctxt,
1669 (xmlSchemaValidityErrorFunc) fprintf,
1670 (xmlSchemaValidityWarningFunc) fprintf,
1671 stderr);
1672
Daniel Veillard971771e2005-07-09 17:32:57 +00001673 ret = xmlSchemaValidateStream(vctxt, buf, 0, handler,
1674 (void *)user_data);
1675 if (repeat == 0) {
1676 if (ret == 0) {
1677 fprintf(stderr, "%s validates\n", filename);
1678 } else if (ret > 0) {
1679 fprintf(stderr, "%s fails to validate\n", filename);
1680 progresult = XMLLINT_ERR_VALID;
1681 } else {
1682 fprintf(stderr, "%s validation generated an internal error\n",
1683 filename);
1684 progresult = XMLLINT_ERR_VALID;
1685 }
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001686 }
1687 xmlSchemaFreeValidCtxt(vctxt);
1688 } else
1689#endif
1690 {
1691 /*
1692 * Create the parser context amd hook the input
1693 */
1694 ctxt = xmlNewParserCtxt();
1695 if (ctxt == NULL) {
1696 xmlFreeParserInputBuffer(buf);
1697 goto error;
1698 }
1699 old_sax = ctxt->sax;
1700 ctxt->sax = handler;
1701 ctxt->userData = (void *) user_data;
1702 inputStream = xmlNewIOInputStream(ctxt, buf, XML_CHAR_ENCODING_NONE);
1703 if (inputStream == NULL) {
1704 xmlFreeParserInputBuffer(buf);
1705 goto error;
1706 }
1707 inputPush(ctxt, inputStream);
Daniel Veillardf1edb102009-08-10 14:43:18 +02001708
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001709 /* do the parsing */
1710 xmlParseDocument(ctxt);
1711
1712 if (ctxt->myDoc != NULL) {
1713 fprintf(stderr, "SAX generated a doc !\n");
1714 xmlFreeDoc(ctxt->myDoc);
1715 ctxt->myDoc = NULL;
1716 }
1717 }
1718
1719error:
1720 if (ctxt != NULL) {
1721 ctxt->sax = old_sax;
1722 xmlFreeParserCtxt(ctxt);
1723 }
1724}
1725
Daniel Veillard5e873c42000-04-12 13:27:38 +00001726/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +02001727 * *
1728 * Stream Test processing *
1729 * *
Daniel Veillard7704fb12003-01-03 16:19:51 +00001730 ************************************************************************/
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00001731#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001732static void processNode(xmlTextReaderPtr reader) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001733 const xmlChar *name, *value;
Daniel Veillard16ef8002005-01-31 00:27:50 +00001734 int type, empty;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001735
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001736 type = xmlTextReaderNodeType(reader);
Daniel Veillard16ef8002005-01-31 00:27:50 +00001737 empty = xmlTextReaderIsEmptyElement(reader);
Daniel Veillard99737f52003-03-22 14:55:50 +00001738
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001739 if (debug) {
1740 name = xmlTextReaderConstName(reader);
1741 if (name == NULL)
1742 name = BAD_CAST "--";
Daniel Veillard7704fb12003-01-03 16:19:51 +00001743
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001744 value = xmlTextReaderConstValue(reader);
1745
Daniel Veillardf1edb102009-08-10 14:43:18 +02001746
1747 printf("%d %d %s %d %d",
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001748 xmlTextReaderDepth(reader),
1749 type,
1750 name,
Daniel Veillard16ef8002005-01-31 00:27:50 +00001751 empty,
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001752 xmlTextReaderHasValue(reader));
1753 if (value == NULL)
1754 printf("\n");
1755 else {
1756 printf(" %s\n", value);
1757 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00001758 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001759#ifdef LIBXML_PATTERN_ENABLED
1760 if (patternc) {
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001761 xmlChar *path = NULL;
1762 int match = -1;
Daniel Veillardf1edb102009-08-10 14:43:18 +02001763
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001764 if (type == XML_READER_TYPE_ELEMENT) {
1765 /* do the check only on element start */
1766 match = xmlPatternMatch(patternc, xmlTextReaderCurrentNode(reader));
1767
1768 if (match) {
Daniel Veillardf1edb102009-08-10 14:43:18 +02001769#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001770 path = xmlGetNodePath(xmlTextReaderCurrentNode(reader));
1771 printf("Node %s matches pattern %s\n", path, pattern);
Daniel Veillardf1edb102009-08-10 14:43:18 +02001772#else
1773 printf("Node %s matches pattern %s\n",
1774 xmlTextReaderConstName(reader), pattern);
1775#endif
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001776 }
1777 }
1778 if (patstream != NULL) {
1779 int ret;
1780
1781 if (type == XML_READER_TYPE_ELEMENT) {
1782 ret = xmlStreamPush(patstream,
1783 xmlTextReaderConstLocalName(reader),
1784 xmlTextReaderConstNamespaceUri(reader));
1785 if (ret < 0) {
1786 fprintf(stderr, "xmlStreamPush() failure\n");
1787 xmlFreeStreamCtxt(patstream);
1788 patstream = NULL;
1789 } else if (ret != match) {
Daniel Veillardf1edb102009-08-10 14:43:18 +02001790#if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001791 if (path == NULL) {
1792 path = xmlGetNodePath(
1793 xmlTextReaderCurrentNode(reader));
1794 }
Daniel Veillardf1edb102009-08-10 14:43:18 +02001795#endif
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001796 fprintf(stderr,
1797 "xmlPatternMatch and xmlStreamPush disagree\n");
Daniel Veillardf1edb102009-08-10 14:43:18 +02001798 if (path != NULL)
1799 fprintf(stderr, " pattern %s node %s\n",
1800 pattern, path);
1801 else
1802 fprintf(stderr, " pattern %s node %s\n",
1803 pattern, xmlTextReaderConstName(reader));
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001804 }
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001805
Daniel Veillardf1edb102009-08-10 14:43:18 +02001806 }
Daniel Veillard16ef8002005-01-31 00:27:50 +00001807 if ((type == XML_READER_TYPE_END_ELEMENT) ||
1808 ((type == XML_READER_TYPE_ELEMENT) && (empty))) {
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001809 ret = xmlStreamPop(patstream);
1810 if (ret < 0) {
1811 fprintf(stderr, "xmlStreamPop() failure\n");
1812 xmlFreeStreamCtxt(patstream);
1813 patstream = NULL;
1814 }
1815 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001816 }
Daniel Veillardf9d16912005-01-30 22:36:30 +00001817 if (path != NULL)
1818 xmlFree(path);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001819 }
1820#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001821}
1822
1823static void streamFile(char *filename) {
1824 xmlTextReaderPtr reader;
1825 int ret;
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001826#ifdef HAVE_SYS_MMAN_H
1827 int fd = -1;
1828 struct stat info;
1829 const char *base = NULL;
1830 xmlParserInputBufferPtr input = NULL;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001831
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001832 if (memory) {
Daniel Veillardf1edb102009-08-10 14:43:18 +02001833 if (stat(filename, &info) < 0)
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001834 return;
1835 if ((fd = open(filename, O_RDONLY)) < 0)
1836 return;
1837 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
1838 if (base == (void *) MAP_FAILED)
1839 return;
1840
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001841 reader = xmlReaderForMemory(base, info.st_size, filename,
1842 NULL, options);
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001843 } else
1844#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001845 reader = xmlReaderForFile(filename, NULL, options);
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001846#ifdef LIBXML_PATTERN_ENABLED
Daniel Veillardd4301ab2005-02-03 22:24:10 +00001847 if (pattern != NULL) {
1848 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
1849 if (patternc == NULL) {
1850 xmlGenericError(xmlGenericErrorContext,
1851 "Pattern %s failed to compile\n", pattern);
1852 progresult = XMLLINT_ERR_SCHEMAPAT;
1853 pattern = NULL;
1854 }
1855 }
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001856 if (patternc != NULL) {
1857 patstream = xmlPatternGetStreamCtxt(patternc);
1858 if (patstream != NULL) {
1859 ret = xmlStreamPush(patstream, NULL, NULL);
1860 if (ret < 0) {
1861 fprintf(stderr, "xmlStreamPush() failure\n");
1862 xmlFreeStreamCtxt(patstream);
1863 patstream = NULL;
1864 }
1865 }
1866 }
1867#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001868
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001869
Daniel Veillard7704fb12003-01-03 16:19:51 +00001870 if (reader != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +00001871#ifdef LIBXML_VALID_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001872 if (valid)
1873 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00001874 else
Daniel Veillard4432df22003-09-28 18:58:27 +00001875#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce192eb2003-04-16 15:58:05 +00001876 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001877#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +00001878 if (relaxng != NULL) {
Daniel Veillard81514ba2003-09-16 23:17:26 +00001879 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00001880 startTimer();
1881 }
1882 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
1883 if (ret < 0) {
1884 xmlGenericError(xmlGenericErrorContext,
1885 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +00001886 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +00001887 relaxng = NULL;
1888 }
Daniel Veillard81514ba2003-09-16 23:17:26 +00001889 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00001890 endTimer("Compiling the schemas");
1891 }
1892 }
Daniel Veillardf10ae122005-07-10 19:03:16 +00001893 if (schema != NULL) {
1894 if ((timing) && (!repeat)) {
1895 startTimer();
1896 }
1897 ret = xmlTextReaderSchemaValidate(reader, schema);
1898 if (ret < 0) {
1899 xmlGenericError(xmlGenericErrorContext,
1900 "XSD schema %s failed to compile\n", schema);
1901 progresult = XMLLINT_ERR_SCHEMACOMP;
1902 schema = NULL;
1903 }
1904 if ((timing) && (!repeat)) {
1905 endTimer("Compiling the schemas");
1906 }
1907 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001908#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001909
1910 /*
1911 * Process all nodes in sequence
1912 */
Daniel Veillard81514ba2003-09-16 23:17:26 +00001913 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00001914 startTimer();
1915 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00001916 ret = xmlTextReaderRead(reader);
1917 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001918 if ((debug)
1919#ifdef LIBXML_PATTERN_ENABLED
1920 || (patternc)
1921#endif
1922 )
Daniel Veillard7704fb12003-01-03 16:19:51 +00001923 processNode(reader);
1924 ret = xmlTextReaderRead(reader);
1925 }
Daniel Veillard81514ba2003-09-16 23:17:26 +00001926 if ((timing) && (!repeat)) {
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001927#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf54cd532004-02-25 11:52:31 +00001928 if (relaxng != NULL)
Daniel Veillard49138f12004-02-19 12:58:36 +00001929 endTimer("Parsing and validating");
1930 else
Daniel Veillardf54cd532004-02-25 11:52:31 +00001931#endif
Daniel Veillard4432df22003-09-28 18:58:27 +00001932#ifdef LIBXML_VALID_ENABLED
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001933 if (valid)
Daniel Veillardce192eb2003-04-16 15:58:05 +00001934 endTimer("Parsing and validating");
1935 else
Daniel Veillard4432df22003-09-28 18:58:27 +00001936#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +00001937 endTimer("Parsing");
Daniel Veillardce192eb2003-04-16 15:58:05 +00001938 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00001939
Daniel Veillard4432df22003-09-28 18:58:27 +00001940#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf6bad792003-04-11 19:38:54 +00001941 if (valid) {
1942 if (xmlTextReaderIsValid(reader) != 1) {
1943 xmlGenericError(xmlGenericErrorContext,
1944 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001945 progresult = XMLLINT_ERR_VALID;
Daniel Veillardf6bad792003-04-11 19:38:54 +00001946 }
1947 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001948#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001949#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf10ae122005-07-10 19:03:16 +00001950 if ((relaxng != NULL) || (schema != NULL)) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00001951 if (xmlTextReaderIsValid(reader) != 1) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001952 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001953 progresult = XMLLINT_ERR_VALID;
Daniel Veillardf4e55762003-04-15 23:32:22 +00001954 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001955 fprintf(stderr, "%s validates\n", filename);
Daniel Veillardf4e55762003-04-15 23:32:22 +00001956 }
1957 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +00001958#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001959 /*
1960 * Done, cleanup and status
1961 */
1962 xmlFreeTextReader(reader);
1963 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001964 fprintf(stderr, "%s : failed to parse\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001965 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001966 }
1967 } else {
1968 fprintf(stderr, "Unable to open %s\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001969 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7704fb12003-01-03 16:19:51 +00001970 }
Daniel Veillard2fc6df92005-01-30 18:42:55 +00001971#ifdef LIBXML_PATTERN_ENABLED
1972 if (patstream != NULL) {
1973 xmlFreeStreamCtxt(patstream);
1974 patstream = NULL;
1975 }
1976#endif
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001977#ifdef HAVE_SYS_MMAN_H
1978 if (memory) {
1979 xmlFreeParserInputBuffer(input);
1980 munmap((char *) base, info.st_size);
1981 close(fd);
1982 }
1983#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +00001984}
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001985
1986static void walkDoc(xmlDocPtr doc) {
1987 xmlTextReaderPtr reader;
1988 int ret;
1989
Daniel Veillardd4301ab2005-02-03 22:24:10 +00001990#ifdef LIBXML_PATTERN_ENABLED
1991 xmlNodePtr root;
1992 const xmlChar *namespaces[22];
1993 int i;
1994 xmlNsPtr ns;
1995
1996 root = xmlDocGetRootElement(doc);
1997 for (ns = root->nsDef, i = 0;ns != NULL && i < 20;ns=ns->next) {
1998 namespaces[i++] = ns->href;
1999 namespaces[i++] = ns->prefix;
2000 }
2001 namespaces[i++] = NULL;
Daniel Veillard13cee4e2009-09-05 14:52:55 +02002002 namespaces[i] = NULL;
Daniel Veillardd4301ab2005-02-03 22:24:10 +00002003
2004 if (pattern != NULL) {
2005 patternc = xmlPatterncompile((const xmlChar *) pattern, doc->dict,
2006 0, &namespaces[0]);
2007 if (patternc == NULL) {
2008 xmlGenericError(xmlGenericErrorContext,
2009 "Pattern %s failed to compile\n", pattern);
2010 progresult = XMLLINT_ERR_SCHEMAPAT;
2011 pattern = NULL;
2012 }
2013 }
Daniel Veillard2b2e02d2005-02-05 23:20:22 +00002014 if (patternc != NULL) {
2015 patstream = xmlPatternGetStreamCtxt(patternc);
2016 if (patstream != NULL) {
2017 ret = xmlStreamPush(patstream, NULL, NULL);
2018 if (ret < 0) {
2019 fprintf(stderr, "xmlStreamPush() failure\n");
2020 xmlFreeStreamCtxt(patstream);
2021 patstream = NULL;
2022 }
2023 }
2024 }
Daniel Veillardd4301ab2005-02-03 22:24:10 +00002025#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002026 reader = xmlReaderWalker(doc);
2027 if (reader != NULL) {
2028 if ((timing) && (!repeat)) {
2029 startTimer();
2030 }
2031 ret = xmlTextReaderRead(reader);
2032 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002033 if ((debug)
2034#ifdef LIBXML_PATTERN_ENABLED
2035 || (patternc)
2036#endif
2037 )
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002038 processNode(reader);
2039 ret = xmlTextReaderRead(reader);
2040 }
2041 if ((timing) && (!repeat)) {
2042 endTimer("walking through the doc");
2043 }
2044 xmlFreeTextReader(reader);
2045 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002046 fprintf(stderr, "failed to walk through the doc\n");
William M. Brack8304d872004-06-08 13:29:32 +00002047 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002048 }
2049 } else {
2050 fprintf(stderr, "Failed to crate a reader from the document\n");
William M. Brack8304d872004-06-08 13:29:32 +00002051 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002052 }
Daniel Veillard2b2e02d2005-02-05 23:20:22 +00002053#ifdef LIBXML_PATTERN_ENABLED
2054 if (patstream != NULL) {
2055 xmlFreeStreamCtxt(patstream);
2056 patstream = NULL;
2057 }
2058#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002059}
Daniel Veillard81273902003-09-30 00:43:48 +00002060#endif /* LIBXML_READER_ENABLED */
Daniel Veillard7704fb12003-01-03 16:19:51 +00002061
Daniel Veillard1934b0c2009-10-07 10:25:06 +02002062#ifdef LIBXML_XPATH_ENABLED
2063/************************************************************************
2064 * *
2065 * XPath Query *
2066 * *
2067 ************************************************************************/
2068
2069static void doXPathDump(xmlXPathObjectPtr cur) {
2070 switch(cur->type) {
2071 case XPATH_NODESET: {
2072 int i;
2073 xmlNodePtr node;
2074#ifdef LIBXML_OUTPUT_ENABLED
2075 xmlSaveCtxtPtr ctxt;
2076
Daniel Veillardbdc64d62012-03-27 14:41:37 +08002077 if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr <= 0)) {
Daniel Veillard1934b0c2009-10-07 10:25:06 +02002078 fprintf(stderr, "XPath set is empty\n");
2079 progresult = XMLLINT_ERR_XPATH;
2080 break;
2081 }
2082 ctxt = xmlSaveToFd(1, NULL, 0);
2083 if (ctxt == NULL) {
2084 fprintf(stderr, "Out of memory for XPath\n");
2085 progresult = XMLLINT_ERR_MEM;
2086 return;
2087 }
2088 for (i = 0;i < cur->nodesetval->nodeNr;i++) {
2089 node = cur->nodesetval->nodeTab[i];
2090 xmlSaveTree(ctxt, node);
2091 }
2092 xmlSaveClose(ctxt);
2093#else
2094 printf("xpath returned %d nodes\n", cur->nodesetval->nodeNr);
2095#endif
2096 break;
2097 }
2098 case XPATH_BOOLEAN:
2099 if (cur->boolval) printf("true");
2100 else printf("false");
2101 break;
2102 case XPATH_NUMBER:
2103 switch (xmlXPathIsInf(cur->floatval)) {
2104 case 1:
2105 printf("Infinity");
2106 break;
2107 case -1:
2108 printf("-Infinity");
2109 break;
2110 default:
2111 if (xmlXPathIsNaN(cur->floatval)) {
2112 printf("NaN");
2113 } else {
2114 printf("%0g", cur->floatval);
2115 }
2116 }
2117 break;
2118 case XPATH_STRING:
2119 printf("%s", (const char *) cur->stringval);
2120 break;
2121 case XPATH_UNDEFINED:
2122 fprintf(stderr, "XPath Object is uninitialized\n");
2123 progresult = XMLLINT_ERR_XPATH;
2124 break;
2125 default:
2126 fprintf(stderr, "XPath object of unexpected type\n");
2127 progresult = XMLLINT_ERR_XPATH;
2128 break;
2129 }
2130}
2131
2132static void doXPathQuery(xmlDocPtr doc, const char *query) {
2133 xmlXPathContextPtr ctxt;
2134 xmlXPathObjectPtr res;
2135
2136 ctxt = xmlXPathNewContext(doc);
2137 if (ctxt == NULL) {
2138 fprintf(stderr, "Out of memory for XPath\n");
2139 progresult = XMLLINT_ERR_MEM;
2140 return;
2141 }
Daniel Veillard2e1eaca2012-05-25 16:44:20 +08002142 ctxt->node = (xmlNodePtr) doc;
Daniel Veillard1934b0c2009-10-07 10:25:06 +02002143 res = xmlXPathEval(BAD_CAST query, ctxt);
2144 xmlXPathFreeContext(ctxt);
2145
2146 if (res == NULL) {
2147 fprintf(stderr, "XPath evaluation failure\n");
2148 progresult = XMLLINT_ERR_XPATH;
2149 return;
2150 }
2151 doXPathDump(res);
2152 xmlXPathFreeObject(res);
2153}
2154#endif /* LIBXML_XPATH_ENABLED */
2155
Daniel Veillard7704fb12003-01-03 16:19:51 +00002156/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +02002157 * *
2158 * Tree Test processing *
2159 * *
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002160 ************************************************************************/
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002161static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
Daniel Veillard652327a2003-09-29 18:02:38 +00002162 xmlDocPtr doc = NULL;
2163#ifdef LIBXML_TREE_ENABLED
2164 xmlDocPtr tmp;
2165#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002166
Daniel Veillard48b2f892001-02-25 16:11:03 +00002167 if ((timing) && (!repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00002168 startTimer();
Daniel Veillardf1edb102009-08-10 14:43:18 +02002169
Daniel Veillard48b2f892001-02-25 16:11:03 +00002170
Daniel Veillard652327a2003-09-29 18:02:38 +00002171#ifdef LIBXML_TREE_ENABLED
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002172 if (filename == NULL) {
2173 if (generate) {
2174 xmlNodePtr n;
2175
2176 doc = xmlNewDoc(BAD_CAST "1.0");
Daniel Veillard95ddcd32004-10-26 21:53:55 +00002177 n = xmlNewDocNode(doc, NULL, BAD_CAST "info", NULL);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002178 xmlNodeSetContent(n, BAD_CAST "abc");
2179 xmlDocSetRootElement(doc, n);
2180 }
2181 }
Daniel Veillard652327a2003-09-29 18:02:38 +00002182#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002183#ifdef LIBXML_HTML_ENABLED
Daniel Veillard73b013f2003-09-30 12:36:01 +00002184#ifdef LIBXML_PUSH_ENABLED
William M. Brack78637da2003-07-31 14:47:38 +00002185 else if ((html) && (push)) {
2186 FILE *f;
2187
William M. Brack3403add2004-06-27 02:07:51 +00002188#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2189 f = fopen(filename, "rb");
2190#else
2191 f = fopen(filename, "r");
2192#endif
William M. Brack78637da2003-07-31 14:47:38 +00002193 if (f != NULL) {
2194 int res, size = 3;
2195 char chars[4096];
2196 htmlParserCtxtPtr ctxt;
2197
2198 /* if (repeat) */
2199 size = 4096;
2200 res = fread(chars, 1, 4, f);
2201 if (res > 0) {
2202 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
William M. Brack1d75c8a2003-10-27 13:48:16 +00002203 chars, res, filename, XML_CHAR_ENCODING_NONE);
William M. Brack78637da2003-07-31 14:47:38 +00002204 while ((res = fread(chars, 1, size, f)) > 0) {
2205 htmlParseChunk(ctxt, chars, res, 0);
2206 }
2207 htmlParseChunk(ctxt, chars, 0, 1);
2208 doc = ctxt->myDoc;
2209 htmlFreeParserCtxt(ctxt);
2210 }
2211 fclose(f);
2212 }
2213 }
Daniel Veillard73b013f2003-09-30 12:36:01 +00002214#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillardf1a27c62006-10-13 22:33:03 +00002215#ifdef HAVE_SYS_MMAN_H
2216 else if ((html) && (memory)) {
2217 int fd;
2218 struct stat info;
2219 const char *base;
Daniel Veillardf1edb102009-08-10 14:43:18 +02002220 if (stat(filename, &info) < 0)
Daniel Veillardf1a27c62006-10-13 22:33:03 +00002221 return;
2222 if ((fd = open(filename, O_RDONLY)) < 0)
2223 return;
2224 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
2225 if (base == (void *) MAP_FAILED)
2226 return;
2227
2228 doc = htmlReadMemory((char *) base, info.st_size, filename,
2229 NULL, options);
Daniel Veillardf1edb102009-08-10 14:43:18 +02002230
Daniel Veillardf1a27c62006-10-13 22:33:03 +00002231 munmap((char *) base, info.st_size);
2232 close(fd);
2233 }
2234#endif
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002235 else if (html) {
Daniel Veillard9475a352003-09-26 12:47:50 +00002236 doc = htmlReadFile(filename, NULL, options);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002237 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002238#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002239 else {
Daniel Veillard73b013f2003-09-30 12:36:01 +00002240#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002241 /*
2242 * build an XML tree from a string;
2243 */
2244 if (push) {
2245 FILE *f;
2246
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002247 /* '-' Usually means stdin -<sven@zen.org> */
2248 if ((filename[0] == '-') && (filename[1] == 0)) {
2249 f = stdin;
2250 } else {
William M. Brack3403add2004-06-27 02:07:51 +00002251#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2252 f = fopen(filename, "rb");
2253#else
2254 f = fopen(filename, "r");
2255#endif
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002256 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002257 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +00002258 int ret;
Daniel Veillarda880b122003-04-21 21:36:41 +00002259 int res, size = 1024;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002260 char chars[1024];
2261 xmlParserCtxtPtr ctxt;
2262
Daniel Veillarda880b122003-04-21 21:36:41 +00002263 /* if (repeat) size = 1024; */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002264 res = fread(chars, 1, 4, f);
2265 if (res > 0) {
2266 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
2267 chars, res, filename);
Daniel Veillard500a1de2004-03-22 15:22:58 +00002268 xmlCtxtUseOptions(ctxt, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002269 while ((res = fread(chars, 1, size, f)) > 0) {
2270 xmlParseChunk(ctxt, chars, res, 0);
2271 }
2272 xmlParseChunk(ctxt, chars, 0, 1);
2273 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +00002274 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002275 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +00002276 if (!ret) {
2277 xmlFreeDoc(doc);
2278 doc = NULL;
2279 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002280 }
Daniel Veillard84bff682009-09-11 15:30:19 +02002281 if (f != stdin)
2282 fclose(f);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002283 }
Daniel Veillard73b013f2003-09-30 12:36:01 +00002284 } else
2285#endif /* LIBXML_PUSH_ENABLED */
2286 if (testIO) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002287 if ((filename[0] == '-') && (filename[1] == 0)) {
Daniel Veillard60942de2003-09-25 21:05:58 +00002288 doc = xmlReadFd(0, NULL, NULL, options);
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002289 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002290 FILE *f;
Daniel Veillard5e873c42000-04-12 13:27:38 +00002291
William M. Brack3403add2004-06-27 02:07:51 +00002292#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2293 f = fopen(filename, "rb");
2294#else
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002295 f = fopen(filename, "r");
William M. Brack3403add2004-06-27 02:07:51 +00002296#endif
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002297 if (f != NULL) {
2298 if (rectxt == NULL)
2299 doc = xmlReadIO((xmlInputReadCallback) myRead,
2300 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +00002301 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002302 else
2303 doc = xmlCtxtReadIO(rectxt,
2304 (xmlInputReadCallback) myRead,
2305 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +00002306 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002307 } else
Daniel Veillard5e873c42000-04-12 13:27:38 +00002308 doc = NULL;
Daniel Veillard5e873c42000-04-12 13:27:38 +00002309 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002310 } else if (htmlout) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002311 xmlParserCtxtPtr ctxt;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002312
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002313 if (rectxt == NULL)
2314 ctxt = xmlNewParserCtxt();
2315 else
2316 ctxt = rectxt;
Daniel Veillardf1edb102009-08-10 14:43:18 +02002317 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002318 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +00002319 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002320 ctxt->sax->error = xmlHTMLError;
2321 ctxt->sax->warning = xmlHTMLWarning;
2322 ctxt->vctxt.error = xmlHTMLValidityError;
2323 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002324
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002325 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002326
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002327 if (rectxt == NULL)
2328 xmlFreeParserCtxt(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002329 }
Daniel Veillard46e370e2000-07-21 20:32:03 +00002330#ifdef HAVE_SYS_MMAN_H
2331 } else if (memory) {
2332 int fd;
2333 struct stat info;
2334 const char *base;
Daniel Veillardf1edb102009-08-10 14:43:18 +02002335 if (stat(filename, &info) < 0)
Daniel Veillard46e370e2000-07-21 20:32:03 +00002336 return;
2337 if ((fd = open(filename, O_RDONLY)) < 0)
2338 return;
2339 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +00002340 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +00002341 return;
2342
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002343 if (rectxt == NULL)
Daniel Veillard60942de2003-09-25 21:05:58 +00002344 doc = xmlReadMemory((char *) base, info.st_size,
2345 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002346 else
Daniel Veillard60942de2003-09-25 21:05:58 +00002347 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
2348 filename, NULL, options);
Daniel Veillardf1edb102009-08-10 14:43:18 +02002349
Daniel Veillard46e370e2000-07-21 20:32:03 +00002350 munmap((char *) base, info.st_size);
Daniel Veillardf1a27c62006-10-13 22:33:03 +00002351 close(fd);
Daniel Veillard46e370e2000-07-21 20:32:03 +00002352#endif
Daniel Veillard4432df22003-09-28 18:58:27 +00002353#ifdef LIBXML_VALID_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00002354 } else if (valid) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002355 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00002356
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002357 if (rectxt == NULL)
2358 ctxt = xmlNewParserCtxt();
2359 else
2360 ctxt = rectxt;
Daniel Veillardf1edb102009-08-10 14:43:18 +02002361 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002362 doc = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00002363 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002364 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
2365
2366 if (ctxt->valid == 0)
William M. Brack8304d872004-06-08 13:29:32 +00002367 progresult = XMLLINT_ERR_RDFILE;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002368 if (rectxt == NULL)
2369 xmlFreeParserCtxt(ctxt);
Daniel Veillardea7751d2002-12-20 00:16:24 +00002370 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002371#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00002372 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002373 if (rectxt != NULL)
2374 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
Daniel Veillard81562d22005-06-15 13:27:56 +00002375 else {
2376#ifdef LIBXML_SAX1_ENABLED
2377 if (sax1)
2378 doc = xmlParseFile(filename);
2379 else
2380#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002381 doc = xmlReadFile(filename, NULL, options);
Daniel Veillard81562d22005-06-15 13:27:56 +00002382 }
Daniel Veillardea7751d2002-12-20 00:16:24 +00002383 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002384 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002385
Daniel Veillard88a172f2000-08-04 18:23:10 +00002386 /*
2387 * If we don't have a document we might as well give up. Do we
2388 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002389 if (doc == NULL) {
William M. Brack8304d872004-06-08 13:29:32 +00002390 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard88a172f2000-08-04 18:23:10 +00002391 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002392 }
2393
Daniel Veillard48b2f892001-02-25 16:11:03 +00002394 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002395 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002396 }
2397
Daniel Veillard29e43992001-12-13 22:21:58 +00002398 /*
2399 * Remove DOCTYPE nodes
2400 */
2401 if (dropdtd) {
2402 xmlDtdPtr dtd;
2403
2404 dtd = xmlGetIntSubset(doc);
2405 if (dtd != NULL) {
2406 xmlUnlinkNode((xmlNodePtr)dtd);
2407 xmlFreeDtd(dtd);
2408 }
2409 }
2410
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002411#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +00002412 if (xinclude) {
2413 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002414 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002415 }
William M. Brack4e1c2db2005-02-11 10:58:55 +00002416 if (xmlXIncludeProcessFlags(doc, options) < 0)
2417 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard48b2f892001-02-25 16:11:03 +00002418 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002419 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002420 }
2421 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00002422#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +00002423
Daniel Veillard1934b0c2009-10-07 10:25:06 +02002424#ifdef LIBXML_XPATH_ENABLED
2425 if (xpathquery != NULL) {
2426 doXPathQuery(doc, xpathquery);
2427 }
2428#endif
2429
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002430#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00002431#ifdef LIBXML_XPATH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002432 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002433 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002434 */
Daniel Veillard26a45c82006-10-20 12:55:34 +00002435 if (shell) {
2436 xmlXPathOrderDocElems(doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002437 xmlShell(doc, filename, xmlShellReadline, stdout);
Daniel Veillard26a45c82006-10-20 12:55:34 +00002438 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002439#endif
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00002440#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002441
Daniel Veillard652327a2003-09-29 18:02:38 +00002442#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002443 /*
2444 * test intermediate copy if needed.
2445 */
2446 if (copy) {
2447 tmp = doc;
Daniel Veillard4edd3ed2004-09-20 20:03:01 +00002448 if (timing) {
2449 startTimer();
2450 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002451 doc = xmlCopyDoc(doc, 1);
Daniel Veillard4edd3ed2004-09-20 20:03:01 +00002452 if (timing) {
2453 endTimer("Copying");
2454 }
2455 if (timing) {
2456 startTimer();
2457 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002458 xmlFreeDoc(tmp);
Daniel Veillard4edd3ed2004-09-20 20:03:01 +00002459 if (timing) {
2460 endTimer("Freeing original");
2461 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002462 }
Daniel Veillard652327a2003-09-29 18:02:38 +00002463#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002464
Daniel Veillard4432df22003-09-28 18:58:27 +00002465#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002466 if ((insert) && (!html)) {
2467 const xmlChar* list[256];
2468 int nb, i;
2469 xmlNodePtr node;
2470
2471 if (doc->children != NULL) {
2472 node = doc->children;
2473 while ((node != NULL) && (node->last == NULL)) node = node->next;
2474 if (node != NULL) {
2475 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
2476 if (nb < 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002477 fprintf(stderr, "could not get valid list of elements\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002478 } else if (nb == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002479 fprintf(stderr, "No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002480 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002481 fprintf(stderr, "%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002482 nb);
2483 for (i = 0;i < nb;i++) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002484 fprintf(stderr, "%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002485 }
2486 }
2487 }
Daniel Veillardf1edb102009-08-10 14:43:18 +02002488 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002489 }else
2490#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00002491#ifdef LIBXML_READER_ENABLED
2492 if (walker) {
2493 walkDoc(doc);
2494 }
2495#endif /* LIBXML_READER_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002496#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00002497 if (noout == 0) {
Daniel Veillard3df01182003-12-10 10:17:51 +00002498 int ret;
2499
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002500 /*
2501 * print it.
2502 */
2503#ifdef LIBXML_DEBUG_ENABLED
2504 if (!debug) {
2505#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00002506 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002507 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002508 }
Daniel Veillard656ce942004-04-30 23:11:45 +00002509#ifdef LIBXML_HTML_ENABLED
Daniel Veillard42fd4122003-11-04 08:47:48 +00002510 if ((html) && (!xmlout)) {
2511 if (compress) {
2512 htmlSaveFile(output ? output : "-", doc);
2513 }
2514 else if (encoding != NULL) {
Adam Spragg5f9d9ce2010-11-01 14:27:11 +01002515 if (format == 1) {
Daniel Veillard42fd4122003-11-04 08:47:48 +00002516 htmlSaveFileFormat(output ? output : "-", doc, encoding, 1);
2517 }
2518 else {
2519 htmlSaveFileFormat(output ? output : "-", doc, encoding, 0);
2520 }
2521 }
Adam Spragg5f9d9ce2010-11-01 14:27:11 +01002522 else if (format == 1) {
Daniel Veillard42fd4122003-11-04 08:47:48 +00002523 htmlSaveFileFormat(output ? output : "-", doc, NULL, 1);
2524 }
2525 else {
2526 FILE *out;
2527 if (output == NULL)
2528 out = stdout;
2529 else {
2530 out = fopen(output,"wb");
2531 }
2532 if (out != NULL) {
2533 if (htmlDocDump(out, doc) < 0)
William M. Brack8304d872004-06-08 13:29:32 +00002534 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00002535
2536 if (output != NULL)
2537 fclose(out);
2538 } else {
2539 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00002540 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00002541 }
2542 }
2543 if ((timing) && (!repeat)) {
2544 endTimer("Saving");
2545 }
2546 } else
2547#endif
Daniel Veillard25048d82004-08-14 22:37:54 +00002548#ifdef LIBXML_C14N_ENABLED
2549 if (canonical) {
2550 xmlChar *result = NULL;
2551 int size;
2552
Aleksey Sanin83868242009-07-09 10:26:22 +02002553 size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_1_0, NULL, 1, &result);
2554 if (size >= 0) {
Stefan Kostdff8d0f2011-05-09 12:14:59 +03002555 if (write(1, result, size) == -1) {
2556 fprintf(stderr, "Can't write data\n");
2557 }
Aleksey Sanin83868242009-07-09 10:26:22 +02002558 xmlFree(result);
2559 } else {
2560 fprintf(stderr, "Failed to canonicalize\n");
2561 progresult = XMLLINT_ERR_OUT;
2562 }
2563 } else if (canonical) {
2564 xmlChar *result = NULL;
2565 int size;
2566
2567 size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_1_1, NULL, 1, &result);
Daniel Veillard25048d82004-08-14 22:37:54 +00002568 if (size >= 0) {
Stefan Kostdff8d0f2011-05-09 12:14:59 +03002569 if (write(1, result, size) == -1) {
2570 fprintf(stderr, "Can't write data\n");
2571 }
Daniel Veillard25048d82004-08-14 22:37:54 +00002572 xmlFree(result);
2573 } else {
2574 fprintf(stderr, "Failed to canonicalize\n");
2575 progresult = XMLLINT_ERR_OUT;
2576 }
2577 } else
Aleksey Sanin2650df12005-06-06 17:16:50 +00002578 if (exc_canonical) {
2579 xmlChar *result = NULL;
2580 int size;
2581
Aleksey Sanin83868242009-07-09 10:26:22 +02002582 size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_EXCLUSIVE_1_0, NULL, 1, &result);
Aleksey Sanin2650df12005-06-06 17:16:50 +00002583 if (size >= 0) {
Stefan Kostdff8d0f2011-05-09 12:14:59 +03002584 if (write(1, result, size) == -1) {
2585 fprintf(stderr, "Can't write data\n");
2586 }
Aleksey Sanin2650df12005-06-06 17:16:50 +00002587 xmlFree(result);
2588 } else {
2589 fprintf(stderr, "Failed to canonicalize\n");
2590 progresult = XMLLINT_ERR_OUT;
2591 }
2592 } else
Daniel Veillard25048d82004-08-14 22:37:54 +00002593#endif
Daniel Veillard3b2c2612001-04-04 00:09:00 +00002594#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002595 if (memory) {
2596 xmlChar *result;
2597 int len;
2598
2599 if (encoding != NULL) {
Adam Spragg5f9d9ce2010-11-01 14:27:11 +01002600 if (format == 1) {
Daniel Veillardd536f702001-11-08 17:32:47 +00002601 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
Daniel Veillardf1edb102009-08-10 14:43:18 +02002602 } else {
Daniel Veillardd536f702001-11-08 17:32:47 +00002603 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
2604 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002605 } else {
Adam Spragg5f9d9ce2010-11-01 14:27:11 +01002606 if (format == 1)
Daniel Veillard90493a92001-08-14 14:12:47 +00002607 xmlDocDumpFormatMemory(doc, &result, &len, 1);
2608 else
2609 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002610 }
2611 if (result == NULL) {
2612 fprintf(stderr, "Failed to save\n");
Daniel Veillard25048d82004-08-14 22:37:54 +00002613 progresult = XMLLINT_ERR_OUT;
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002614 } else {
Stefan Kostdff8d0f2011-05-09 12:14:59 +03002615 if (write(1, result, len) == -1) {
2616 fprintf(stderr, "Can't write data\n");
2617 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002618 xmlFree(result);
2619 }
Daniel Veillarddab39b52006-10-16 23:22:10 +00002620
Daniel Veillard3b2c2612001-04-04 00:09:00 +00002621 } else
2622#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002623 if (compress) {
2624 xmlSaveFile(output ? output : "-", doc);
Daniel Veillarddab39b52006-10-16 23:22:10 +00002625 } else if (oldout) {
2626 if (encoding != NULL) {
Adam Spragg5f9d9ce2010-11-01 14:27:11 +01002627 if (format == 1) {
Daniel Veillarddab39b52006-10-16 23:22:10 +00002628 ret = xmlSaveFormatFileEnc(output ? output : "-", doc,
2629 encoding, 1);
2630 }
2631 else {
2632 ret = xmlSaveFileEnc(output ? output : "-", doc,
2633 encoding);
2634 }
2635 if (ret < 0) {
2636 fprintf(stderr, "failed save to %s\n",
2637 output ? output : "-");
2638 progresult = XMLLINT_ERR_OUT;
2639 }
Adam Spragg5f9d9ce2010-11-01 14:27:11 +01002640 } else if (format == 1) {
Daniel Veillarddab39b52006-10-16 23:22:10 +00002641 ret = xmlSaveFormatFile(output ? output : "-", doc, 1);
2642 if (ret < 0) {
2643 fprintf(stderr, "failed save to %s\n",
2644 output ? output : "-");
2645 progresult = XMLLINT_ERR_OUT;
2646 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00002647 } else {
Daniel Veillarddab39b52006-10-16 23:22:10 +00002648 FILE *out;
2649 if (output == NULL)
2650 out = stdout;
2651 else {
2652 out = fopen(output,"wb");
2653 }
2654 if (out != NULL) {
2655 if (xmlDocDump(out, doc) < 0)
2656 progresult = XMLLINT_ERR_OUT;
2657
2658 if (output != NULL)
2659 fclose(out);
2660 } else {
2661 fprintf(stderr, "failed to open %s\n", output);
2662 progresult = XMLLINT_ERR_OUT;
2663 }
2664 }
2665 } else {
2666 xmlSaveCtxtPtr ctxt;
2667 int saveOpts = 0;
2668
Adam Spragg5f9d9ce2010-11-01 14:27:11 +01002669 if (format == 1)
Daniel Veillarddab39b52006-10-16 23:22:10 +00002670 saveOpts |= XML_SAVE_FORMAT;
Adam Spraggd2e62312010-11-03 15:33:40 +01002671 else if (format == 2)
2672 saveOpts |= XML_SAVE_WSNONSIG;
Daniel Veillard9ccea572010-03-10 15:02:49 +01002673
2674#if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
Daniel Veillard9d962642009-08-23 15:31:18 +02002675 if (xmlout)
2676 saveOpts |= XML_SAVE_AS_XML;
Daniel Veillard9ccea572010-03-10 15:02:49 +01002677#endif
Daniel Veillarddab39b52006-10-16 23:22:10 +00002678
2679 if (output == NULL)
2680 ctxt = xmlSaveToFd(1, encoding, saveOpts);
2681 else
2682 ctxt = xmlSaveToFilename(output, encoding, saveOpts);
2683
2684 if (ctxt != NULL) {
2685 if (xmlSaveDoc(ctxt, doc) < 0) {
2686 fprintf(stderr, "failed save to %s\n",
2687 output ? output : "-");
2688 progresult = XMLLINT_ERR_OUT;
2689 }
2690 xmlSaveClose(ctxt);
2691 } else {
William M. Brack8304d872004-06-08 13:29:32 +00002692 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00002693 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002694 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002695 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002696 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002697 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002698#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002699 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002700 FILE *out;
2701 if (output == NULL)
2702 out = stdout;
2703 else {
2704 out = fopen(output,"wb");
2705 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00002706 if (out != NULL) {
2707 xmlDebugDumpDocument(out, doc);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002708
Daniel Veillard05d987b2003-10-08 11:54:57 +00002709 if (output != NULL)
2710 fclose(out);
2711 } else {
2712 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00002713 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00002714 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00002715 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002716#endif
2717 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002718#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002719
Daniel Veillard4432df22003-09-28 18:58:27 +00002720#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002721 /*
2722 * A posteriori validation test
2723 */
Daniel Veillard66f68e72003-08-18 16:39:51 +00002724 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
Daniel Veillardcd429612000-10-11 15:57:05 +00002725 xmlDtdPtr dtd;
2726
Daniel Veillard48b2f892001-02-25 16:11:03 +00002727 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002728 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002729 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00002730 if (dtdvalid != NULL)
Daniel Veillardf1edb102009-08-10 14:43:18 +02002731 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
Daniel Veillard66f68e72003-08-18 16:39:51 +00002732 else
Daniel Veillardf1edb102009-08-10 14:43:18 +02002733 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002734 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002735 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002736 }
Daniel Veillardcd429612000-10-11 15:57:05 +00002737 if (dtd == NULL) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00002738 if (dtdvalid != NULL)
2739 xmlGenericError(xmlGenericErrorContext,
2740 "Could not parse DTD %s\n", dtdvalid);
2741 else
2742 xmlGenericError(xmlGenericErrorContext,
2743 "Could not parse DTD %s\n", dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00002744 progresult = XMLLINT_ERR_DTD;
Daniel Veillardcd429612000-10-11 15:57:05 +00002745 } else {
Daniel Veillarda37aab82003-06-09 09:10:36 +00002746 xmlValidCtxtPtr cvp;
2747
2748 if ((cvp = xmlNewValidCtxt()) == NULL) {
2749 xmlGenericError(xmlGenericErrorContext,
2750 "Couldn't allocate validation context\n");
2751 exit(-1);
2752 }
2753 cvp->userData = (void *) stderr;
2754 cvp->error = (xmlValidityErrorFunc) fprintf;
2755 cvp->warning = (xmlValidityWarningFunc) fprintf;
2756
Daniel Veillard48b2f892001-02-25 16:11:03 +00002757 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002758 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002759 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002760 if (!xmlValidateDtd(cvp, doc, dtd)) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00002761 if (dtdvalid != NULL)
2762 xmlGenericError(xmlGenericErrorContext,
2763 "Document %s does not validate against %s\n",
2764 filename, dtdvalid);
2765 else
2766 xmlGenericError(xmlGenericErrorContext,
2767 "Document %s does not validate against %s\n",
2768 filename, dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00002769 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00002770 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002771 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002772 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002773 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002774 xmlFreeValidCtxt(cvp);
Daniel Veillardcd429612000-10-11 15:57:05 +00002775 xmlFreeDtd(dtd);
2776 }
2777 } else if (postvalid) {
Daniel Veillarda37aab82003-06-09 09:10:36 +00002778 xmlValidCtxtPtr cvp;
2779
2780 if ((cvp = xmlNewValidCtxt()) == NULL) {
2781 xmlGenericError(xmlGenericErrorContext,
2782 "Couldn't allocate validation context\n");
2783 exit(-1);
2784 }
2785
Daniel Veillard48b2f892001-02-25 16:11:03 +00002786 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002787 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002788 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002789 cvp->userData = (void *) stderr;
2790 cvp->error = (xmlValidityErrorFunc) fprintf;
2791 cvp->warning = (xmlValidityWarningFunc) fprintf;
2792 if (!xmlValidateDocument(cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00002793 xmlGenericError(xmlGenericErrorContext,
2794 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00002795 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00002796 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002797 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002798 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002799 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00002800 xmlFreeValidCtxt(cvp);
Daniel Veillard4432df22003-09-28 18:58:27 +00002801 }
2802#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardd4501d72005-07-24 14:27:16 +00002803#ifdef LIBXML_SCHEMATRON_ENABLED
2804 if (wxschematron != NULL) {
2805 xmlSchematronValidCtxtPtr ctxt;
2806 int ret;
Daniel Veillardc740a172005-07-31 12:17:24 +00002807 int flag;
Daniel Veillardd4501d72005-07-24 14:27:16 +00002808
2809 if ((timing) && (!repeat)) {
2810 startTimer();
2811 }
2812
2813 if (debug)
2814 flag = XML_SCHEMATRON_OUT_XML;
Daniel Veillardc740a172005-07-31 12:17:24 +00002815 else
2816 flag = XML_SCHEMATRON_OUT_TEXT;
2817 if (noout)
2818 flag |= XML_SCHEMATRON_OUT_QUIET;
Daniel Veillardd4501d72005-07-24 14:27:16 +00002819 ctxt = xmlSchematronNewValidCtxt(wxschematron, flag);
2820#if 0
2821 xmlSchematronSetValidErrors(ctxt,
2822 (xmlSchematronValidityErrorFunc) fprintf,
2823 (xmlSchematronValidityWarningFunc) fprintf,
2824 stderr);
2825#endif
2826 ret = xmlSchematronValidateDoc(ctxt, doc);
2827 if (ret == 0) {
2828 fprintf(stderr, "%s validates\n", filename);
2829 } else if (ret > 0) {
2830 fprintf(stderr, "%s fails to validate\n", filename);
2831 progresult = XMLLINT_ERR_VALID;
2832 } else {
2833 fprintf(stderr, "%s validation generated an internal error\n",
2834 filename);
2835 progresult = XMLLINT_ERR_VALID;
2836 }
2837 xmlSchematronFreeValidCtxt(ctxt);
2838 if ((timing) && (!repeat)) {
2839 endTimer("Validating");
2840 }
2841 }
2842#endif
Daniel Veillard71531f32003-02-05 13:19:53 +00002843#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00002844 if (relaxngschemas != NULL) {
Daniel Veillard71531f32003-02-05 13:19:53 +00002845 xmlRelaxNGValidCtxtPtr ctxt;
2846 int ret;
2847
Daniel Veillard42f12e92003-03-07 18:32:59 +00002848 if ((timing) && (!repeat)) {
2849 startTimer();
2850 }
2851
Daniel Veillard71531f32003-02-05 13:19:53 +00002852 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2853 xmlRelaxNGSetValidErrors(ctxt,
2854 (xmlRelaxNGValidityErrorFunc) fprintf,
2855 (xmlRelaxNGValidityWarningFunc) fprintf,
2856 stderr);
2857 ret = xmlRelaxNGValidateDoc(ctxt, doc);
2858 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002859 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard71531f32003-02-05 13:19:53 +00002860 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002861 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00002862 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00002863 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002864 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard71531f32003-02-05 13:19:53 +00002865 filename);
William M. Brack8304d872004-06-08 13:29:32 +00002866 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00002867 }
2868 xmlRelaxNGFreeValidCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002869 if ((timing) && (!repeat)) {
2870 endTimer("Validating");
2871 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002872 } else if (wxschemas != NULL) {
2873 xmlSchemaValidCtxtPtr ctxt;
2874 int ret;
2875
2876 if ((timing) && (!repeat)) {
2877 startTimer();
2878 }
2879
2880 ctxt = xmlSchemaNewValidCtxt(wxschemas);
2881 xmlSchemaSetValidErrors(ctxt,
2882 (xmlSchemaValidityErrorFunc) fprintf,
2883 (xmlSchemaValidityWarningFunc) fprintf,
2884 stderr);
2885 ret = xmlSchemaValidateDoc(ctxt, doc);
2886 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002887 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002888 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002889 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00002890 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002891 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00002892 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002893 filename);
William M. Brack8304d872004-06-08 13:29:32 +00002894 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002895 }
2896 xmlSchemaFreeValidCtxt(ctxt);
2897 if ((timing) && (!repeat)) {
2898 endTimer("Validating");
2899 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002900 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002901#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002902
2903#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard6b099012008-11-06 13:47:39 +00002904#if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002905 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00002906 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002907#endif
Daniel Veillard6b099012008-11-06 13:47:39 +00002908#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002909
2910 /*
2911 * free it.
2912 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00002913 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002914 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00002915 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002916 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002917 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00002918 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00002919 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002920}
2921
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002922/************************************************************************
Daniel Veillardf1edb102009-08-10 14:43:18 +02002923 * *
2924 * Usage and Main *
2925 * *
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002926 ************************************************************************/
2927
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00002928static void showVersion(const char *name) {
2929 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
2930 fprintf(stderr, " compiled with: ");
Daniel Veillard602434d2005-09-12 09:20:31 +00002931 if (xmlHasFeature(XML_WITH_THREAD)) fprintf(stderr, "Threads ");
2932 if (xmlHasFeature(XML_WITH_TREE)) fprintf(stderr, "Tree ");
2933 if (xmlHasFeature(XML_WITH_OUTPUT)) fprintf(stderr, "Output ");
2934 if (xmlHasFeature(XML_WITH_PUSH)) fprintf(stderr, "Push ");
2935 if (xmlHasFeature(XML_WITH_READER)) fprintf(stderr, "Reader ");
2936 if (xmlHasFeature(XML_WITH_PATTERN)) fprintf(stderr, "Patterns ");
2937 if (xmlHasFeature(XML_WITH_WRITER)) fprintf(stderr, "Writer ");
2938 if (xmlHasFeature(XML_WITH_SAX1)) fprintf(stderr, "SAXv1 ");
Daniel Veillardf1edb102009-08-10 14:43:18 +02002939 if (xmlHasFeature(XML_WITH_FTP)) fprintf(stderr, "FTP ");
2940 if (xmlHasFeature(XML_WITH_HTTP)) fprintf(stderr, "HTTP ");
Daniel Veillard602434d2005-09-12 09:20:31 +00002941 if (xmlHasFeature(XML_WITH_VALID)) fprintf(stderr, "DTDValid ");
Daniel Veillardf1edb102009-08-10 14:43:18 +02002942 if (xmlHasFeature(XML_WITH_HTML)) fprintf(stderr, "HTML ");
2943 if (xmlHasFeature(XML_WITH_LEGACY)) fprintf(stderr, "Legacy ");
2944 if (xmlHasFeature(XML_WITH_C14N)) fprintf(stderr, "C14N ");
2945 if (xmlHasFeature(XML_WITH_CATALOG)) fprintf(stderr, "Catalog ");
2946 if (xmlHasFeature(XML_WITH_XPATH)) fprintf(stderr, "XPath ");
2947 if (xmlHasFeature(XML_WITH_XPTR)) fprintf(stderr, "XPointer ");
2948 if (xmlHasFeature(XML_WITH_XINCLUDE)) fprintf(stderr, "XInclude ");
2949 if (xmlHasFeature(XML_WITH_ICONV)) fprintf(stderr, "Iconv ");
2950 if (xmlHasFeature(XML_WITH_ISO8859X)) fprintf(stderr, "ISO8859X ");
2951 if (xmlHasFeature(XML_WITH_UNICODE)) fprintf(stderr, "Unicode ");
2952 if (xmlHasFeature(XML_WITH_REGEXP)) fprintf(stderr, "Regexps ");
2953 if (xmlHasFeature(XML_WITH_AUTOMATA)) fprintf(stderr, "Automata ");
2954 if (xmlHasFeature(XML_WITH_EXPR)) fprintf(stderr, "Expr ");
2955 if (xmlHasFeature(XML_WITH_SCHEMAS)) fprintf(stderr, "Schemas ");
2956 if (xmlHasFeature(XML_WITH_SCHEMATRON)) fprintf(stderr, "Schematron ");
2957 if (xmlHasFeature(XML_WITH_MODULES)) fprintf(stderr, "Modules ");
2958 if (xmlHasFeature(XML_WITH_DEBUG)) fprintf(stderr, "Debug ");
2959 if (xmlHasFeature(XML_WITH_DEBUG_MEM)) fprintf(stderr, "MemDebug ");
2960 if (xmlHasFeature(XML_WITH_DEBUG_RUN)) fprintf(stderr, "RunDebug ");
Daniel Veillard75acfee2006-07-13 06:29:56 +00002961 if (xmlHasFeature(XML_WITH_ZLIB)) fprintf(stderr, "Zlib ");
Anders F Bjorklundeae52612011-09-18 16:59:13 +02002962 if (xmlHasFeature(XML_WITH_LZMA)) fprintf(stderr, "Lzma ");
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00002963 fprintf(stderr, "\n");
2964}
2965
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002966static void usage(const char *name) {
2967 printf("Usage : %s [options] XMLfiles ...\n", name);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002968#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002969 printf("\tParse the XML files and output the result of the parsing\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002970#else
2971 printf("\tParse the XML files\n");
2972#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002973 printf("\t--version : display the version of the XML library used\n");
2974#ifdef LIBXML_DEBUG_ENABLED
2975 printf("\t--debug : dump a debug tree of the in-memory document\n");
2976 printf("\t--shell : run a navigating shell\n");
2977 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00002978#else
Daniel Veillard81273902003-09-30 00:43:48 +00002979#ifdef LIBXML_READER_ENABLED
Daniel Veillard8326e732003-01-07 00:19:07 +00002980 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard81273902003-09-30 00:43:48 +00002981#endif /* LIBXML_READER_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002982#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00002983#ifdef LIBXML_TREE_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002984 printf("\t--copy : used to test the internal copy implementation\n");
Daniel Veillard652327a2003-09-29 18:02:38 +00002985#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002986 printf("\t--recover : output what was parsable on broken XML documents\n");
Daniel Veillard8915c152008-08-26 13:05:34 +00002987 printf("\t--huge : remove any internal arbitrary parser limits\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002988 printf("\t--noent : substitute entity references by their value\n");
Daniel Veillardc62efc82011-05-16 16:03:50 +08002989 printf("\t--noenc : ignore any encoding specified inside the document\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002990 printf("\t--noout : don't output the result tree\n");
Daniel Veillard0bff36d2004-08-31 09:37:03 +00002991 printf("\t--path 'paths': provide a set of paths for resources\n");
2992 printf("\t--load-trace : print trace of all external entites loaded\n");
Daniel Veillarde8b09e42003-05-13 22:14:13 +00002993 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
Daniel Veillard8874b942005-08-25 13:19:21 +00002994 printf("\t--nocompact : do not generate compact text nodes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002995 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00002996 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00002997#ifdef LIBXML_VALID_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002998 printf("\t--valid : validate the document in addition to std well-formed check\n");
2999 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
3000 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
Daniel Veillard66f68e72003-08-18 16:39:51 +00003001 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00003002#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003003 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003004 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003005 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
3006 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003007#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003008#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003009 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003010#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003011#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003012#ifdef LIBXML_HTML_ENABLED
3013 printf("\t--html : use the HTML parser\n");
Daniel Veillard42fd4122003-11-04 08:47:48 +00003014 printf("\t--xmlout : force to use the XML serializer when using --html\n");
Daniel Veillardf1121c42010-07-26 14:02:42 +02003015 printf("\t--nodefdtd : do not default HTML doctype\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003016#endif
Daniel Veillard73b013f2003-09-30 12:36:01 +00003017#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003018 printf("\t--push : use the push mode of the parser\n");
Daniel Veillard73b013f2003-09-30 12:36:01 +00003019#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003020#ifdef HAVE_SYS_MMAN_H
3021 printf("\t--memory : parse from memory\n");
3022#endif
Daniel Veillard87076042004-05-03 22:54:49 +00003023 printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003024 printf("\t--nowarning : do not emit warnings from parser/validator\n");
3025 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillarddca8cc72003-09-26 13:53:14 +00003026 printf("\t--nocdata : replace cdata section with text nodes\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003027#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00003028 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003029 printf("\t--encode encoding : output in the given encoding\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003030 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
Adam Spraggd2e62312010-11-03 15:33:40 +01003031 printf("\t--pretty STYLE : pretty-print in a particular style\n");
3032 printf("\t 0 Do not pretty print\n");
3033 printf("\t 1 Format the XML content, as --format\n");
3034 printf("\t 2 Add whitespace inside tags, preserving content\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003035#endif /* LIBXML_OUTPUT_ENABLED */
Aleksey Sanin83868242009-07-09 10:26:22 +02003036 printf("\t--c14n : save in W3C canonical format v1.0 (with comments)\n");
3037 printf("\t--c14n11 : save in W3C canonical format v1.1 (with comments)\n");
Aleksey Sanin2650df12005-06-06 17:16:50 +00003038 printf("\t--exc-c14n : save in W3C exclusive canonical format (with comments)\n");
Daniel Veillard25048d82004-08-14 22:37:54 +00003039#ifdef LIBXML_C14N_ENABLED
3040#endif /* LIBXML_C14N_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003041 printf("\t--nsclean : remove redundant namespace declarations\n");
3042 printf("\t--testIO : test user I/O support\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003043#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00003044 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
3045 printf("\t otherwise XML Catalogs starting from \n");
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003046 printf("\t %s are activated by default\n", XML_XML_DEFAULT_CATALOG);
Daniel Veillard05c13a22001-09-09 08:38:09 +00003047 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003048#endif
3049 printf("\t--auto : generate a small doc on the fly\n");
3050#ifdef LIBXML_XINCLUDE_ENABLED
3051 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00003052 printf("\t--noxincludenode : same but do not generate XInclude nodes\n");
Daniel Veillard54bd29b2008-08-26 07:26:55 +00003053 printf("\t--nofixup-base-uris : do not fixup xml:base uris\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003054#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003055 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00003056 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard81273902003-09-30 00:43:48 +00003057#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00003058 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard7899c5c2003-11-03 12:31:38 +00003059 printf("\t--walker : create a reader and walk though the resulting doc\n");
Daniel Veillard81273902003-09-30 00:43:48 +00003060#endif /* LIBXML_READER_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003061#ifdef LIBXML_PATTERN_ENABLED
3062 printf("\t--pattern pattern_value : test the pattern support\n");
3063#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003064 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillardef4d3bc2003-02-07 12:38:22 +00003065#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard71531f32003-02-05 13:19:53 +00003066 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003067 printf("\t--schema schema : do validation against the WXS schema\n");
Daniel Veillard71531f32003-02-05 13:19:53 +00003068#endif
Daniel Veillarde70375c2005-07-30 21:09:12 +00003069#ifdef LIBXML_SCHEMATRON_ENABLED
3070 printf("\t--schematron schema : do validation against a schematron\n");
3071#endif
Daniel Veillard971771e2005-07-09 17:32:57 +00003072#ifdef LIBXML_SAX1_ENABLED
3073 printf("\t--sax1: use the old SAX1 interfaces for processing\n");
3074#endif
3075 printf("\t--sax: do not build a tree but work just at the SAX level\n");
Daniel Veillard7e5c3f42008-07-29 16:12:31 +00003076 printf("\t--oldxml10: use XML-1.0 parsing rules before the 5th edition\n");
Daniel Veillard1934b0c2009-10-07 10:25:06 +02003077#ifdef LIBXML_XPATH_ENABLED
3078 printf("\t--xpath expr: evaluate the XPath expression, inply --noout\n");
3079#endif
Daniel Veillard971771e2005-07-09 17:32:57 +00003080
Daniel Veillarda42f25f2002-01-25 14:15:40 +00003081 printf("\nLibxml project home page: http://xmlsoft.org/\n");
3082 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003083}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003084
3085static void registerNode(xmlNodePtr node)
3086{
3087 node->_private = malloc(sizeof(long));
3088 *(long*)node->_private = (long) 0x81726354;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003089 nbregister++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003090}
3091
3092static void deregisterNode(xmlNodePtr node)
3093{
3094 assert(node->_private != NULL);
3095 assert(*(long*)node->_private == (long) 0x81726354);
3096 free(node->_private);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003097 nbregister--;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003098}
3099
Daniel Veillard4a6845d2001-01-03 13:32:39 +00003100int
3101main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00003102 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003103 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00003104 int version = 0;
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00003105 const char* indent;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003106
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003107 if (argc <= 1) {
3108 usage(argv[0]);
3109 return(1);
3110 }
Daniel Veillardbe803962000-06-28 23:40:59 +00003111 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003112 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003113 if (!strcmp(argv[i], "-"))
3114 break;
3115
3116 if (argv[i][0] != '-')
3117 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003118 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
3119 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00003120 else
3121#ifdef LIBXML_DEBUG_ENABLED
3122 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003123 (!strcmp(argv[i], "--shell"))) {
3124 shell++;
3125 noout = 1;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003126 } else
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003127#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00003128#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003129 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
3130 copy++;
Daniel Veillard652327a2003-09-29 18:02:38 +00003131 else
3132#endif /* LIBXML_TREE_ENABLED */
3133 if ((!strcmp(argv[i], "-recover")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003134 (!strcmp(argv[i], "--recover"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003135 recovery++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003136 options |= XML_PARSE_RECOVER;
Daniel Veillard8915c152008-08-26 13:05:34 +00003137 } else if ((!strcmp(argv[i], "-huge")) ||
3138 (!strcmp(argv[i], "--huge"))) {
3139 options |= XML_PARSE_HUGE;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003140 } else if ((!strcmp(argv[i], "-noent")) ||
3141 (!strcmp(argv[i], "--noent"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003142 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003143 options |= XML_PARSE_NOENT;
Daniel Veillardc62efc82011-05-16 16:03:50 +08003144 } else if ((!strcmp(argv[i], "-noenc")) ||
3145 (!strcmp(argv[i], "--noenc"))) {
3146 noenc++;
3147 options |= XML_PARSE_IGNORE_ENC;
Daniel Veillarddca8cc72003-09-26 13:53:14 +00003148 } else if ((!strcmp(argv[i], "-nsclean")) ||
3149 (!strcmp(argv[i], "--nsclean"))) {
3150 options |= XML_PARSE_NSCLEAN;
3151 } else if ((!strcmp(argv[i], "-nocdata")) ||
3152 (!strcmp(argv[i], "--nocdata"))) {
3153 options |= XML_PARSE_NOCDATA;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003154 } else if ((!strcmp(argv[i], "-nodict")) ||
3155 (!strcmp(argv[i], "--nodict"))) {
3156 options |= XML_PARSE_NODICT;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003157 } else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00003158 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00003159 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00003160 version = 1;
3161 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003162 (!strcmp(argv[i], "--noout")))
3163 noout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003164#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003165 else if ((!strcmp(argv[i], "-o")) ||
3166 (!strcmp(argv[i], "-output")) ||
3167 (!strcmp(argv[i], "--output"))) {
3168 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00003169 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003170 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003171#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003172 else if ((!strcmp(argv[i], "-htmlout")) ||
3173 (!strcmp(argv[i], "--htmlout")))
3174 htmlout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003175 else if ((!strcmp(argv[i], "-nowrap")) ||
3176 (!strcmp(argv[i], "--nowrap")))
3177 nowrap++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003178#ifdef LIBXML_HTML_ENABLED
3179 else if ((!strcmp(argv[i], "-html")) ||
3180 (!strcmp(argv[i], "--html"))) {
3181 html++;
3182 }
Daniel Veillard42fd4122003-11-04 08:47:48 +00003183 else if ((!strcmp(argv[i], "-xmlout")) ||
3184 (!strcmp(argv[i], "--xmlout"))) {
3185 xmlout++;
Daniel Veillardf1121c42010-07-26 14:02:42 +02003186 } else if ((!strcmp(argv[i], "-nodefdtd")) ||
3187 (!strcmp(argv[i], "--nodefdtd"))) {
3188 nodefdtd++;
3189 options |= HTML_PARSE_NODEFDTD;
Daniel Veillard42fd4122003-11-04 08:47:48 +00003190 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003191#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003192 else if ((!strcmp(argv[i], "-loaddtd")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003193 (!strcmp(argv[i], "--loaddtd"))) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003194 loaddtd++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003195 options |= XML_PARSE_DTDLOAD;
3196 } else if ((!strcmp(argv[i], "-dtdattr")) ||
Daniel Veillard48da9102001-08-07 01:10:10 +00003197 (!strcmp(argv[i], "--dtdattr"))) {
3198 loaddtd++;
3199 dtdattrs++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003200 options |= XML_PARSE_DTDATTR;
Daniel Veillard4432df22003-09-28 18:58:27 +00003201 }
3202#ifdef LIBXML_VALID_ENABLED
3203 else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003204 (!strcmp(argv[i], "--valid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003205 valid++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003206 options |= XML_PARSE_DTDVALID;
3207 } else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003208 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003209 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003210 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00003211 options |= XML_PARSE_DTDLOAD;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003212 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00003213 (!strcmp(argv[i], "--dtdvalid"))) {
3214 i++;
3215 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003216 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00003217 options |= XML_PARSE_DTDLOAD;
Daniel Veillard66f68e72003-08-18 16:39:51 +00003218 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
3219 (!strcmp(argv[i], "--dtdvalidfpi"))) {
3220 i++;
3221 dtdvalidfpi = argv[i];
3222 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00003223 options |= XML_PARSE_DTDLOAD;
Daniel Veillardcd429612000-10-11 15:57:05 +00003224 }
Daniel Veillard4432df22003-09-28 18:58:27 +00003225#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard29e43992001-12-13 22:21:58 +00003226 else if ((!strcmp(argv[i], "-dropdtd")) ||
3227 (!strcmp(argv[i], "--dropdtd")))
3228 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003229 else if ((!strcmp(argv[i], "-insert")) ||
3230 (!strcmp(argv[i], "--insert")))
3231 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003232 else if ((!strcmp(argv[i], "-timing")) ||
3233 (!strcmp(argv[i], "--timing")))
3234 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00003235 else if ((!strcmp(argv[i], "-auto")) ||
3236 (!strcmp(argv[i], "--auto")))
3237 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003238 else if ((!strcmp(argv[i], "-repeat")) ||
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00003239 (!strcmp(argv[i], "--repeat"))) {
3240 if (repeat)
3241 repeat *= 10;
3242 else
3243 repeat = 100;
Daniel Veillard73b013f2003-09-30 12:36:01 +00003244 }
3245#ifdef LIBXML_PUSH_ENABLED
3246 else if ((!strcmp(argv[i], "-push")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003247 (!strcmp(argv[i], "--push")))
3248 push++;
Daniel Veillard73b013f2003-09-30 12:36:01 +00003249#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +00003250#ifdef HAVE_SYS_MMAN_H
3251 else if ((!strcmp(argv[i], "-memory")) ||
3252 (!strcmp(argv[i], "--memory")))
3253 memory++;
3254#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00003255 else if ((!strcmp(argv[i], "-testIO")) ||
3256 (!strcmp(argv[i], "--testIO")))
3257 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00003258#ifdef LIBXML_XINCLUDE_ENABLED
3259 else if ((!strcmp(argv[i], "-xinclude")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003260 (!strcmp(argv[i], "--xinclude"))) {
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00003261 xinclude++;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00003262 options |= XML_PARSE_XINCLUDE;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003263 }
Daniel Veillardc14c3892004-08-16 12:34:50 +00003264 else if ((!strcmp(argv[i], "-noxincludenode")) ||
3265 (!strcmp(argv[i], "--noxincludenode"))) {
3266 xinclude++;
3267 options |= XML_PARSE_XINCLUDE;
3268 options |= XML_PARSE_NOXINCNODE;
3269 }
Daniel Veillard54bd29b2008-08-26 07:26:55 +00003270 else if ((!strcmp(argv[i], "-nofixup-base-uris")) ||
3271 (!strcmp(argv[i], "--nofixup-base-uris"))) {
3272 xinclude++;
3273 options |= XML_PARSE_XINCLUDE;
3274 options |= XML_PARSE_NOBASEFIX;
3275 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00003276#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003277#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003278#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003279 else if ((!strcmp(argv[i], "-compress")) ||
3280 (!strcmp(argv[i], "--compress"))) {
3281 compress++;
3282 xmlSetCompressMode(9);
3283 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003284#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003285#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003286 else if ((!strcmp(argv[i], "-nowarning")) ||
3287 (!strcmp(argv[i], "--nowarning"))) {
3288 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003289 xmlPedanticParserDefault(0);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003290 options |= XML_PARSE_NOWARNING;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003291 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003292 else if ((!strcmp(argv[i], "-pedantic")) ||
3293 (!strcmp(argv[i], "--pedantic"))) {
3294 xmlGetWarningsDefaultValue = 1;
3295 xmlPedanticParserDefault(1);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003296 options |= XML_PARSE_PEDANTIC;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003297 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00003298#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003299 else if ((!strcmp(argv[i], "-debugent")) ||
3300 (!strcmp(argv[i], "--debugent"))) {
3301 debugent++;
3302 xmlParserDebugEntities = 1;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003303 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00003304#endif
Daniel Veillard25048d82004-08-14 22:37:54 +00003305#ifdef LIBXML_C14N_ENABLED
3306 else if ((!strcmp(argv[i], "-c14n")) ||
3307 (!strcmp(argv[i], "--c14n"))) {
3308 canonical++;
3309 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003310 }
Aleksey Sanin83868242009-07-09 10:26:22 +02003311 else if ((!strcmp(argv[i], "-c14n11")) ||
3312 (!strcmp(argv[i], "--c14n11"))) {
3313 canonical_11++;
3314 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003315 }
Aleksey Sanin2650df12005-06-06 17:16:50 +00003316 else if ((!strcmp(argv[i], "-exc-c14n")) ||
3317 (!strcmp(argv[i], "--exc-c14n"))) {
3318 exc_canonical++;
3319 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003320 }
Daniel Veillard25048d82004-08-14 22:37:54 +00003321#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00003322#ifdef LIBXML_CATALOG_ENABLED
3323 else if ((!strcmp(argv[i], "-catalogs")) ||
3324 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003325 catalogs++;
3326 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
3327 (!strcmp(argv[i], "--nocatalogs"))) {
3328 nocatalogs++;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003329 }
Daniel Veillard81418e32001-05-22 15:08:55 +00003330#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00003331 else if ((!strcmp(argv[i], "-encode")) ||
3332 (!strcmp(argv[i], "--encode"))) {
3333 i++;
3334 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00003335 /*
3336 * OK it's for testing purposes
3337 */
3338 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00003339 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003340 else if ((!strcmp(argv[i], "-noblanks")) ||
3341 (!strcmp(argv[i], "--noblanks"))) {
3342 noblanks++;
3343 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00003344 }
Daniel Veillard87076042004-05-03 22:54:49 +00003345 else if ((!strcmp(argv[i], "-maxmem")) ||
3346 (!strcmp(argv[i], "--maxmem"))) {
3347 i++;
3348 if (sscanf(argv[i], "%d", &maxmem) == 1) {
3349 xmlMemSetup(myFreeFunc, myMallocFunc, myReallocFunc,
3350 myStrdupFunc);
3351 } else {
3352 maxmem = 0;
3353 }
3354 }
Daniel Veillard90493a92001-08-14 14:12:47 +00003355 else if ((!strcmp(argv[i], "-format")) ||
3356 (!strcmp(argv[i], "--format"))) {
3357 noblanks++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003358#ifdef LIBXML_OUTPUT_ENABLED
Adam Spragg5f9d9ce2010-11-01 14:27:11 +01003359 format = 1;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003360#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard90493a92001-08-14 14:12:47 +00003361 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00003362 }
Adam Spraggd2e62312010-11-03 15:33:40 +01003363 else if ((!strcmp(argv[i], "-pretty")) ||
3364 (!strcmp(argv[i], "--pretty"))) {
3365 i++;
3366#ifdef LIBXML_OUTPUT_ENABLED
3367 format = atoi(argv[i]);
Adam Spraggd2e62312010-11-03 15:33:40 +01003368 if (format == 1) {
3369 noblanks++;
3370 xmlKeepBlanksDefault(0);
3371 }
Brandon Slack0c7109c2012-05-11 10:50:59 +08003372#endif /* LIBXML_OUTPUT_ENABLED */
Adam Spraggd2e62312010-11-03 15:33:40 +01003373 }
Daniel Veillard81273902003-09-30 00:43:48 +00003374#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00003375 else if ((!strcmp(argv[i], "-stream")) ||
3376 (!strcmp(argv[i], "--stream"))) {
3377 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003378 }
Daniel Veillard7899c5c2003-11-03 12:31:38 +00003379 else if ((!strcmp(argv[i], "-walker")) ||
3380 (!strcmp(argv[i], "--walker"))) {
3381 walker++;
3382 noout++;
3383 }
Daniel Veillard81273902003-09-30 00:43:48 +00003384#endif /* LIBXML_READER_ENABLED */
3385#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00003386 else if ((!strcmp(argv[i], "-sax1")) ||
3387 (!strcmp(argv[i], "--sax1"))) {
Daniel Veillard023d0ba2009-07-29 11:34:50 +02003388 sax1++;
3389 options |= XML_PARSE_SAX1;
Daniel Veillard07cb8222003-09-10 10:51:05 +00003390 }
Daniel Veillard81273902003-09-30 00:43:48 +00003391#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003392 else if ((!strcmp(argv[i], "-sax")) ||
3393 (!strcmp(argv[i], "--sax"))) {
Daniel Veillard023d0ba2009-07-29 11:34:50 +02003394 sax++;
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003395 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003396 else if ((!strcmp(argv[i], "-chkregister")) ||
3397 (!strcmp(argv[i], "--chkregister"))) {
Daniel Veillard023d0ba2009-07-29 11:34:50 +02003398 chkregister++;
Daniel Veillard71531f32003-02-05 13:19:53 +00003399#ifdef LIBXML_SCHEMAS_ENABLED
3400 } else if ((!strcmp(argv[i], "-relaxng")) ||
3401 (!strcmp(argv[i], "--relaxng"))) {
3402 i++;
3403 relaxng = argv[i];
3404 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003405 options |= XML_PARSE_NOENT;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003406 } else if ((!strcmp(argv[i], "-schema")) ||
3407 (!strcmp(argv[i], "--schema"))) {
3408 i++;
3409 schema = argv[i];
3410 noent++;
Daniel Veillard71531f32003-02-05 13:19:53 +00003411#endif
Daniel Veillardd4501d72005-07-24 14:27:16 +00003412#ifdef LIBXML_SCHEMATRON_ENABLED
3413 } else if ((!strcmp(argv[i], "-schematron")) ||
3414 (!strcmp(argv[i], "--schematron"))) {
3415 i++;
3416 schematron = argv[i];
3417 noent++;
3418#endif
Daniel Veillarde8b09e42003-05-13 22:14:13 +00003419 } else if ((!strcmp(argv[i], "-nonet")) ||
3420 (!strcmp(argv[i], "--nonet"))) {
Daniel Veillard61b93382003-11-03 14:28:31 +00003421 options |= XML_PARSE_NONET;
Daniel Veillard968d6432006-04-25 16:17:53 +00003422 xmlSetExternalEntityLoader(xmlNoNetExternalEntityLoader);
Daniel Veillard8874b942005-08-25 13:19:21 +00003423 } else if ((!strcmp(argv[i], "-nocompact")) ||
3424 (!strcmp(argv[i], "--nocompact"))) {
3425 options &= ~XML_PARSE_COMPACT;
Daniel Veillard0bff36d2004-08-31 09:37:03 +00003426 } else if ((!strcmp(argv[i], "-load-trace")) ||
3427 (!strcmp(argv[i], "--load-trace"))) {
3428 load_trace++;
3429 } else if ((!strcmp(argv[i], "-path")) ||
3430 (!strcmp(argv[i], "--path"))) {
3431 i++;
3432 parsePath(BAD_CAST argv[i]);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003433#ifdef LIBXML_PATTERN_ENABLED
3434 } else if ((!strcmp(argv[i], "-pattern")) ||
3435 (!strcmp(argv[i], "--pattern"))) {
3436 i++;
3437 pattern = argv[i];
3438#endif
Daniel Veillard1934b0c2009-10-07 10:25:06 +02003439#ifdef LIBXML_XPATH_ENABLED
3440 } else if ((!strcmp(argv[i], "-xpath")) ||
3441 (!strcmp(argv[i], "--xpath"))) {
3442 i++;
3443 noout++;
3444 xpathquery = argv[i];
3445#endif
Daniel Veillard7e5c3f42008-07-29 16:12:31 +00003446 } else if ((!strcmp(argv[i], "-oldxml10")) ||
3447 (!strcmp(argv[i], "--oldxml10"))) {
3448 oldxml10++;
3449 options |= XML_PARSE_OLD10;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003450 } else {
3451 fprintf(stderr, "Unknown option %s\n", argv[i]);
3452 usage(argv[0]);
3453 return(1);
3454 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003455 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003456
3457#ifdef LIBXML_CATALOG_ENABLED
3458 if (nocatalogs == 0) {
3459 if (catalogs) {
3460 const char *catal;
3461
3462 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00003463 if (catal != NULL) {
3464 xmlLoadCatalogs(catal);
3465 } else {
3466 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
3467 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003468 }
3469 }
3470#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003471
Daniel Veillard81273902003-09-30 00:43:48 +00003472#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00003473 if (sax1)
3474 xmlSAXDefaultVersion(1);
3475 else
3476 xmlSAXDefaultVersion(2);
Daniel Veillard81273902003-09-30 00:43:48 +00003477#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00003478
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003479 if (chkregister) {
3480 xmlRegisterNodeDefault(registerNode);
3481 xmlDeregisterNodeDefault(deregisterNode);
3482 }
Daniel Veillardf1edb102009-08-10 14:43:18 +02003483
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00003484 indent = getenv("XMLLINT_INDENT");
3485 if(indent != NULL) {
3486 xmlTreeIndentString = indent;
3487 }
Daniel Veillardf1edb102009-08-10 14:43:18 +02003488
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003489
Daniel Veillard0bff36d2004-08-31 09:37:03 +00003490 defaultEntityLoader = xmlGetExternalEntityLoader();
3491 xmlSetExternalEntityLoader(xmllintExternalEntityLoader);
3492
Daniel Veillardd9bad132001-07-23 19:39:43 +00003493 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00003494 if (loaddtd != 0)
3495 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
3496 if (dtdattrs)
3497 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003498 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillard4432df22003-09-28 18:58:27 +00003499#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003500 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard4432df22003-09-28 18:58:27 +00003501#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003502 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00003503 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003504 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00003505 xmlGenericError(xmlGenericErrorContext,
3506 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
3507 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003508 "<html><head><title>%s output</title></head>\n",
3509 argv[0]);
Daniel Veillardf1edb102009-08-10 14:43:18 +02003510 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003511 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
3512 argv[0]);
3513 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003514
Daniel Veillardd4501d72005-07-24 14:27:16 +00003515#ifdef LIBXML_SCHEMATRON_ENABLED
3516 if ((schematron != NULL) && (sax == 0)
3517#ifdef LIBXML_READER_ENABLED
3518 && (stream == 0)
3519#endif /* LIBXML_READER_ENABLED */
3520 ) {
3521 xmlSchematronParserCtxtPtr ctxt;
3522
3523 /* forces loading the DTDs */
Daniel Veillardf1edb102009-08-10 14:43:18 +02003524 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillardd4501d72005-07-24 14:27:16 +00003525 options |= XML_PARSE_DTDLOAD;
3526 if (timing) {
3527 startTimer();
3528 }
3529 ctxt = xmlSchematronNewParserCtxt(schematron);
3530#if 0
3531 xmlSchematronSetParserErrors(ctxt,
3532 (xmlSchematronValidityErrorFunc) fprintf,
3533 (xmlSchematronValidityWarningFunc) fprintf,
3534 stderr);
3535#endif
3536 wxschematron = xmlSchematronParse(ctxt);
3537 if (wxschematron == NULL) {
3538 xmlGenericError(xmlGenericErrorContext,
3539 "Schematron schema %s failed to compile\n", schematron);
3540 progresult = XMLLINT_ERR_SCHEMACOMP;
3541 schematron = NULL;
3542 }
3543 xmlSchematronFreeParserCtxt(ctxt);
3544 if (timing) {
3545 endTimer("Compiling the schemas");
3546 }
3547 }
3548#endif
Daniel Veillard71531f32003-02-05 13:19:53 +00003549#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003550 if ((relaxng != NULL) && (sax == 0)
Daniel Veillard81273902003-09-30 00:43:48 +00003551#ifdef LIBXML_READER_ENABLED
3552 && (stream == 0)
3553#endif /* LIBXML_READER_ENABLED */
3554 ) {
Daniel Veillard71531f32003-02-05 13:19:53 +00003555 xmlRelaxNGParserCtxtPtr ctxt;
3556
Daniel Veillardce192eb2003-04-16 15:58:05 +00003557 /* forces loading the DTDs */
Daniel Veillardf1edb102009-08-10 14:43:18 +02003558 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00003559 options |= XML_PARSE_DTDLOAD;
Daniel Veillard42f12e92003-03-07 18:32:59 +00003560 if (timing) {
3561 startTimer();
3562 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003563 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
3564 xmlRelaxNGSetParserErrors(ctxt,
3565 (xmlRelaxNGValidityErrorFunc) fprintf,
3566 (xmlRelaxNGValidityWarningFunc) fprintf,
3567 stderr);
3568 relaxngschemas = xmlRelaxNGParse(ctxt);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003569 if (relaxngschemas == NULL) {
3570 xmlGenericError(xmlGenericErrorContext,
3571 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +00003572 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +00003573 relaxng = NULL;
3574 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003575 xmlRelaxNGFreeParserCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00003576 if (timing) {
3577 endTimer("Compiling the schemas");
3578 }
Daniel Veillardebe25d42004-03-25 09:35:49 +00003579 } else if ((schema != NULL)
3580#ifdef LIBXML_READER_ENABLED
Daniel Veillardf10ae122005-07-10 19:03:16 +00003581 && (stream == 0)
Daniel Veillardebe25d42004-03-25 09:35:49 +00003582#endif
3583 ) {
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003584 xmlSchemaParserCtxtPtr ctxt;
3585
3586 if (timing) {
3587 startTimer();
3588 }
3589 ctxt = xmlSchemaNewParserCtxt(schema);
3590 xmlSchemaSetParserErrors(ctxt,
3591 (xmlSchemaValidityErrorFunc) fprintf,
3592 (xmlSchemaValidityWarningFunc) fprintf,
3593 stderr);
3594 wxschemas = xmlSchemaParse(ctxt);
3595 if (wxschemas == NULL) {
3596 xmlGenericError(xmlGenericErrorContext,
3597 "WXS schema %s failed to compile\n", schema);
William M. Brack8304d872004-06-08 13:29:32 +00003598 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003599 schema = NULL;
3600 }
3601 xmlSchemaFreeParserCtxt(ctxt);
3602 if (timing) {
3603 endTimer("Compiling the schemas");
3604 }
Daniel Veillard71531f32003-02-05 13:19:53 +00003605 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003606#endif /* LIBXML_SCHEMAS_ENABLED */
3607#ifdef LIBXML_PATTERN_ENABLED
Daniel Veillard39e5c892005-07-03 22:48:50 +00003608 if ((pattern != NULL)
Daniel Veillardc9352532005-07-04 14:25:34 +00003609#ifdef LIBXML_READER_ENABLED
Daniel Veillard39e5c892005-07-03 22:48:50 +00003610 && (walker == 0)
3611#endif
3612 ) {
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00003613 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003614 if (patternc == NULL) {
3615 xmlGenericError(xmlGenericErrorContext,
3616 "Pattern %s failed to compile\n", pattern);
William M. Brack8304d872004-06-08 13:29:32 +00003617 progresult = XMLLINT_ERR_SCHEMAPAT;
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003618 pattern = NULL;
3619 }
3620 }
3621#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003622 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00003623 if ((!strcmp(argv[i], "-encode")) ||
3624 (!strcmp(argv[i], "--encode"))) {
3625 i++;
3626 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00003627 } else if ((!strcmp(argv[i], "-o")) ||
3628 (!strcmp(argv[i], "-output")) ||
3629 (!strcmp(argv[i], "--output"))) {
3630 i++;
3631 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00003632 }
Daniel Veillard4432df22003-09-28 18:58:27 +00003633#ifdef LIBXML_VALID_ENABLED
Daniel Veillardcd429612000-10-11 15:57:05 +00003634 if ((!strcmp(argv[i], "-dtdvalid")) ||
3635 (!strcmp(argv[i], "--dtdvalid"))) {
3636 i++;
3637 continue;
Daniel Veillardf1edb102009-08-10 14:43:18 +02003638 }
Daniel Veillard0bff36d2004-08-31 09:37:03 +00003639 if ((!strcmp(argv[i], "-path")) ||
3640 (!strcmp(argv[i], "--path"))) {
3641 i++;
3642 continue;
Daniel Veillardcd429612000-10-11 15:57:05 +00003643 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00003644 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
3645 (!strcmp(argv[i], "--dtdvalidfpi"))) {
3646 i++;
3647 continue;
3648 }
Daniel Veillard4432df22003-09-28 18:58:27 +00003649#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00003650 if ((!strcmp(argv[i], "-relaxng")) ||
3651 (!strcmp(argv[i], "--relaxng"))) {
3652 i++;
3653 continue;
3654 }
Daniel Veillard87076042004-05-03 22:54:49 +00003655 if ((!strcmp(argv[i], "-maxmem")) ||
3656 (!strcmp(argv[i], "--maxmem"))) {
3657 i++;
3658 continue;
3659 }
Adam Spraggd2e62312010-11-03 15:33:40 +01003660 if ((!strcmp(argv[i], "-pretty")) ||
3661 (!strcmp(argv[i], "--pretty"))) {
3662 i++;
3663 continue;
3664 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003665 if ((!strcmp(argv[i], "-schema")) ||
3666 (!strcmp(argv[i], "--schema"))) {
3667 i++;
3668 continue;
3669 }
Daniel Veillardd4501d72005-07-24 14:27:16 +00003670 if ((!strcmp(argv[i], "-schematron")) ||
3671 (!strcmp(argv[i], "--schematron"))) {
3672 i++;
3673 continue;
3674 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003675#ifdef LIBXML_PATTERN_ENABLED
3676 if ((!strcmp(argv[i], "-pattern")) ||
3677 (!strcmp(argv[i], "--pattern"))) {
3678 i++;
3679 continue;
3680 }
3681#endif
Daniel Veillard1934b0c2009-10-07 10:25:06 +02003682#ifdef LIBXML_XPATH_ENABLED
3683 if ((!strcmp(argv[i], "-xpath")) ||
3684 (!strcmp(argv[i], "--xpath"))) {
3685 i++;
3686 continue;
3687 }
3688#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00003689 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00003690 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003691 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00003692 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003693 if (repeat) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003694 xmlParserCtxtPtr ctxt = NULL;
3695
3696 for (acount = 0;acount < repeat;acount++) {
Daniel Veillard81273902003-09-30 00:43:48 +00003697#ifdef LIBXML_READER_ENABLED
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003698 if (stream != 0) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00003699 streamFile(argv[i]);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00003700 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00003701#endif /* LIBXML_READER_ENABLED */
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003702 if (sax) {
3703 testSAX(argv[i]);
3704 } else {
3705 if (ctxt == NULL)
3706 ctxt = xmlNewParserCtxt();
3707 parseAndPrintFile(argv[i], ctxt);
3708 }
Daniel Veillard81273902003-09-30 00:43:48 +00003709#ifdef LIBXML_READER_ENABLED
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003710 }
Daniel Veillard81273902003-09-30 00:43:48 +00003711#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003712 }
3713 if (ctxt != NULL)
3714 xmlFreeParserCtxt(ctxt);
Daniel Veillard7704fb12003-01-03 16:19:51 +00003715 } else {
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003716 nbregister = 0;
3717
Daniel Veillard81273902003-09-30 00:43:48 +00003718#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00003719 if (stream != 0)
3720 streamFile(argv[i]);
3721 else
Daniel Veillard81273902003-09-30 00:43:48 +00003722#endif /* LIBXML_READER_ENABLED */
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003723 if (sax) {
3724 testSAX(argv[i]);
3725 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003726 parseAndPrintFile(argv[i], NULL);
Daniel Veillardf0af8ec2005-07-08 17:27:33 +00003727 }
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003728
3729 if ((chkregister) && (nbregister != 0)) {
3730 fprintf(stderr, "Registration count off: %d\n", nbregister);
William M. Brack8304d872004-06-08 13:29:32 +00003731 progresult = XMLLINT_ERR_RDREGIS;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00003732 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00003733 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003734 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00003735 if ((timing) && (repeat)) {
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00003736 endTimer("%d iterations", repeat);
Daniel Veillarda7866932001-12-04 13:14:44 +00003737 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00003738 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003739 }
Daniel Veillardf1edb102009-08-10 14:43:18 +02003740 if (generate)
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003741 parseAndPrintFile(NULL, NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003742 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00003743 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003744 }
Daniel Veillard845cce42002-01-09 11:51:37 +00003745 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00003746 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003747 }
Daniel Veillardd4501d72005-07-24 14:27:16 +00003748#ifdef LIBXML_SCHEMATRON_ENABLED
3749 if (wxschematron != NULL)
3750 xmlSchematronFree(wxschematron);
3751#endif
Daniel Veillard71531f32003-02-05 13:19:53 +00003752#ifdef LIBXML_SCHEMAS_ENABLED
3753 if (relaxngschemas != NULL)
3754 xmlRelaxNGFree(relaxngschemas);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00003755 if (wxschemas != NULL)
3756 xmlSchemaFree(wxschemas);
Daniel Veillard71531f32003-02-05 13:19:53 +00003757 xmlRelaxNGCleanupTypes();
3758#endif
Daniel Veillardb3de70c2003-12-02 22:32:15 +00003759#ifdef LIBXML_PATTERN_ENABLED
3760 if (patternc != NULL)
3761 xmlFreePattern(patternc);
3762#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003763 xmlCleanupParser();
3764 xmlMemoryDump();
3765
Daniel Veillardf7cd4812001-02-23 18:44:52 +00003766 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00003767}
Daniel Veillard88a172f2000-08-04 18:23:10 +00003768