blob: b0dada35c960fa9d33f839c688a2a53e8aefe38e [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>
34#undef SOCKLEN_T
35#define SOCKLEN_T unsigned int
36#endif
37
Daniel Veillard90bc3712002-03-07 15:12:58 +000038#ifdef HAVE_SYS_TIMEB_H
39#include <sys/timeb.h>
40#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +000041
42#ifdef HAVE_SYS_TYPES_H
43#include <sys/types.h>
44#endif
45#ifdef HAVE_SYS_STAT_H
46#include <sys/stat.h>
47#endif
48#ifdef HAVE_FCNTL_H
49#include <fcntl.h>
50#endif
51#ifdef HAVE_UNISTD_H
52#include <unistd.h>
53#endif
Daniel Veillard46e370e2000-07-21 20:32:03 +000054#ifdef HAVE_SYS_MMAN_H
55#include <sys/mman.h>
Daniel Veillard87b95392000-08-12 21:12:04 +000056/* seems needed for Solaris */
57#ifndef MAP_FAILED
58#define MAP_FAILED ((void *) -1)
59#endif
Daniel Veillard46e370e2000-07-21 20:32:03 +000060#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +000061#ifdef HAVE_STDLIB_H
62#include <stdlib.h>
63#endif
64#ifdef HAVE_LIBREADLINE
65#include <readline/readline.h>
66#ifdef HAVE_LIBHISTORY
67#include <readline/history.h>
68#endif
69#endif
70
71#include <libxml/xmlmemory.h>
72#include <libxml/parser.h>
73#include <libxml/parserInternals.h>
74#include <libxml/HTMLparser.h>
75#include <libxml/HTMLtree.h>
76#include <libxml/tree.h>
77#include <libxml/xpath.h>
78#include <libxml/debugXML.h>
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +000079#include <libxml/xmlerror.h>
Daniel Veillard9e8bfae2000-11-06 16:43:11 +000080#ifdef LIBXML_XINCLUDE_ENABLED
81#include <libxml/xinclude.h>
82#endif
Daniel Veillard81418e32001-05-22 15:08:55 +000083#ifdef LIBXML_CATALOG_ENABLED
84#include <libxml/catalog.h>
85#endif
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000086#include <libxml/globals.h>
Daniel Veillard7704fb12003-01-03 16:19:51 +000087#include <libxml/xmlreader.h>
Daniel Veillard71531f32003-02-05 13:19:53 +000088#ifdef LIBXML_SCHEMAS_ENABLED
89#include <libxml/relaxng.h>
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000090#include <libxml/xmlschemas.h>
Daniel Veillard71531f32003-02-05 13:19:53 +000091#endif
Daniel Veillardb3de70c2003-12-02 22:32:15 +000092#ifdef LIBXML_PATTERN_ENABLED
93#include <libxml/pattern.h>
94#endif
Daniel Veillard6ebf3c42004-08-22 13:11:39 +000095#ifdef LIBXML_C14N_ENABLED
96#include <libxml/c14n.h>
97#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +000098
Daniel Veillard3be27512003-01-26 19:49:04 +000099#ifndef XML_XML_DEFAULT_CATALOG
100#define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog"
101#endif
102
William M. Brack8304d872004-06-08 13:29:32 +0000103typedef enum {
104 XMLLINT_RETURN_OK = 0, /* No error */
105 XMLLINT_ERR_UNCLASS, /* Unclassified */
106 XMLLINT_ERR_DTD, /* Error in DTD */
107 XMLLINT_ERR_VALID, /* Validation error */
108 XMLLINT_ERR_RDFILE, /* CtxtReadFile error */
109 XMLLINT_ERR_SCHEMACOMP, /* Schema compilation */
110 XMLLINT_ERR_OUT, /* Error writing output */
111 XMLLINT_ERR_SCHEMAPAT, /* Error in schema pattern */
112 XMLLINT_ERR_RDREGIS, /* Error in Reader registration */
113 XMLLINT_ERR_MEM /* Out of memory error */
114} xmllintReturnCode;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000115#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000116static int shell = 0;
117static int debugent = 0;
118#endif
Daniel Veillard8326e732003-01-07 00:19:07 +0000119static int debug = 0;
Daniel Veillard87076042004-05-03 22:54:49 +0000120static int maxmem = 0;
Daniel Veillard652327a2003-09-29 18:02:38 +0000121#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000122static int copy = 0;
Daniel Veillard652327a2003-09-29 18:02:38 +0000123#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000124static int recovery = 0;
125static int noent = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000126static int noblanks = 0;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000127static int noout = 0;
128static int nowrap = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000129#ifdef LIBXML_OUTPUT_ENABLED
130static int format = 0;
131static const char *output = NULL;
132static int compress = 0;
133#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4432df22003-09-28 18:58:27 +0000134#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000135static int valid = 0;
136static int postvalid = 0;
Daniel Veillardcd429612000-10-11 15:57:05 +0000137static char * dtdvalid = NULL;
Daniel Veillard66f68e72003-08-18 16:39:51 +0000138static char * dtdvalidfpi = NULL;
Daniel Veillard4432df22003-09-28 18:58:27 +0000139#endif
Daniel Veillard71531f32003-02-05 13:19:53 +0000140#ifdef LIBXML_SCHEMAS_ENABLED
141static char * relaxng = NULL;
142static xmlRelaxNGPtr relaxngschemas = NULL;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +0000143static char * schema = NULL;
144static xmlSchemaPtr wxschemas = NULL;
Daniel Veillard71531f32003-02-05 13:19:53 +0000145#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000146static int repeat = 0;
147static int insert = 0;
Daniel Veillard656ce942004-04-30 23:11:45 +0000148#if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000149static int html = 0;
Daniel Veillard42fd4122003-11-04 08:47:48 +0000150static int xmlout = 0;
Daniel Veillard4432df22003-09-28 18:58:27 +0000151#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000152static int htmlout = 0;
Daniel Veillard73b013f2003-09-30 12:36:01 +0000153#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000154static int push = 0;
Daniel Veillard73b013f2003-09-30 12:36:01 +0000155#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +0000156#ifdef HAVE_SYS_MMAN_H
157static int memory = 0;
158#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +0000159static int testIO = 0;
Daniel Veillardbe803962000-06-28 23:40:59 +0000160static char *encoding = NULL;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000161#ifdef LIBXML_XINCLUDE_ENABLED
162static int xinclude = 0;
163#endif
Daniel Veillard48da9102001-08-07 01:10:10 +0000164static int dtdattrs = 0;
Daniel Veillard10ea86c2001-06-20 13:55:33 +0000165static int loaddtd = 0;
William M. Brack8304d872004-06-08 13:29:32 +0000166static xmllintReturnCode progresult = XMLLINT_RETURN_OK;
Daniel Veillard48b2f892001-02-25 16:11:03 +0000167static int timing = 0;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000168static int generate = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000169static int dropdtd = 0;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000170#ifdef LIBXML_CATALOG_ENABLED
171static int catalogs = 0;
172static int nocatalogs = 0;
173#endif
Daniel Veillard25048d82004-08-14 22:37:54 +0000174#ifdef LIBXML_C14N_ENABLED
175static int canonical = 0;
176#endif
Daniel Veillard81273902003-09-30 00:43:48 +0000177#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000178static int stream = 0;
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000179static int walker = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000180#endif /* LIBXML_READER_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000181static int chkregister = 0;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +0000182static int nbregister = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000183#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +0000184static int sax1 = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000185#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000186#ifdef LIBXML_PATTERN_ENABLED
187static const char *pattern = NULL;
188static xmlPatternPtr patternc = NULL;
189#endif
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000190static int options = 0;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000191
Daniel Veillard87076042004-05-03 22:54:49 +0000192/************************************************************************
193 * *
194 * Memory allocation consumption debugging *
195 * *
196 ************************************************************************/
197
Daniel Veillard3af3b592004-05-05 19:22:30 +0000198static void
199OOM(void)
200{
Daniel Veillard87076042004-05-03 22:54:49 +0000201 fprintf(stderr, "Ran out of memory needs > %d bytes\n", maxmem);
William M. Brack8304d872004-06-08 13:29:32 +0000202 progresult = XMLLINT_ERR_MEM;
Daniel Veillard87076042004-05-03 22:54:49 +0000203}
204
Daniel Veillard3af3b592004-05-05 19:22:30 +0000205static void
206myFreeFunc(void *mem)
207{
Daniel Veillard87076042004-05-03 22:54:49 +0000208 xmlMemFree(mem);
209}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000210static void *
211myMallocFunc(size_t size)
212{
Daniel Veillard87076042004-05-03 22:54:49 +0000213 void *ret;
214
215 ret = xmlMemMalloc(size);
216 if (ret != NULL) {
217 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000218 OOM();
219 xmlMemFree(ret);
220 return (NULL);
221 }
Daniel Veillard87076042004-05-03 22:54:49 +0000222 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000223 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000224}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000225static void *
226myReallocFunc(void *mem, size_t size)
227{
Daniel Veillard87076042004-05-03 22:54:49 +0000228 void *ret;
229
230 ret = xmlMemRealloc(mem, size);
231 if (ret != NULL) {
232 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000233 OOM();
234 xmlMemFree(ret);
235 return (NULL);
236 }
Daniel Veillard87076042004-05-03 22:54:49 +0000237 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000238 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000239}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000240static char *
241myStrdupFunc(const char *str)
242{
Daniel Veillard87076042004-05-03 22:54:49 +0000243 char *ret;
244
245 ret = xmlMemoryStrdup(str);
246 if (ret != NULL) {
247 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000248 OOM();
249 xmlFree(ret);
250 return (NULL);
251 }
Daniel Veillard87076042004-05-03 22:54:49 +0000252 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000253 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000254}
Daniel Veillard87076042004-05-03 22:54:49 +0000255/************************************************************************
256 * *
257 * Internal timing routines to remove the necessity to have *
258 * unix-specific function calls. *
259 * *
260 ************************************************************************/
Daniel Veillard01db67c2001-12-18 07:09:59 +0000261
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000262#ifndef HAVE_GETTIMEOFDAY
263#ifdef HAVE_SYS_TIMEB_H
264#ifdef HAVE_SYS_TIME_H
265#ifdef HAVE_FTIME
266
Daniel Veillard01c13b52002-12-10 15:19:08 +0000267static int
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000268my_gettimeofday(struct timeval *tvp, void *tzp)
269{
270 struct timeb timebuffer;
271
272 ftime(&timebuffer);
273 if (tvp) {
274 tvp->tv_sec = timebuffer.time;
275 tvp->tv_usec = timebuffer.millitm * 1000L;
276 }
277 return (0);
278}
279#define HAVE_GETTIMEOFDAY 1
280#define gettimeofday my_gettimeofday
281
282#endif /* HAVE_FTIME */
283#endif /* HAVE_SYS_TIME_H */
284#endif /* HAVE_SYS_TIMEB_H */
285#endif /* !HAVE_GETTIMEOFDAY */
286
Daniel Veillard01db67c2001-12-18 07:09:59 +0000287#if defined(HAVE_GETTIMEOFDAY)
288static struct timeval begin, end;
289
290/*
291 * startTimer: call where you want to start timing
292 */
293static void
294startTimer(void)
295{
296 gettimeofday(&begin, NULL);
297}
298
299/*
300 * endTimer: call where you want to stop timing and to print out a
301 * message about the timing performed; format is a printf
302 * type argument
303 */
304static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000305endTimer(const char *fmt, ...)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000306{
307 long msec;
308 va_list ap;
309
310 gettimeofday(&end, NULL);
311 msec = end.tv_sec - begin.tv_sec;
312 msec *= 1000;
313 msec += (end.tv_usec - begin.tv_usec) / 1000;
314
315#ifndef HAVE_STDARG_H
316#error "endTimer required stdarg functions"
317#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000318 va_start(ap, fmt);
319 vfprintf(stderr, fmt, ap);
Daniel Veillard01db67c2001-12-18 07:09:59 +0000320 va_end(ap);
321
322 fprintf(stderr, " took %ld ms\n", msec);
323}
324#elif defined(HAVE_TIME_H)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000325/*
326 * No gettimeofday function, so we have to make do with calling clock.
327 * This is obviously less accurate, but there's little we can do about
328 * that.
329 */
Daniel Veillard90bc3712002-03-07 15:12:58 +0000330#ifndef CLOCKS_PER_SEC
331#define CLOCKS_PER_SEC 100
332#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +0000333
334static clock_t begin, end;
335static void
336startTimer(void)
337{
338 begin = clock();
339}
340static void
341endTimer(const char *fmt, ...)
342{
343 long msec;
344 va_list ap;
345
346 end = clock();
347 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
348
349#ifndef HAVE_STDARG_H
350#error "endTimer required stdarg functions"
351#endif
352 va_start(ap, fmt);
353 vfprintf(stderr, fmt, ap);
354 va_end(ap);
355 fprintf(stderr, " took %ld ms\n", msec);
356}
357#else
358
359/*
360 * We don't have a gettimeofday or time.h, so we just don't do timing
361 */
362static void
363startTimer(void)
364{
365 /*
366 * Do nothing
367 */
368}
369static void
370endTimer(char *format, ...)
371{
372 /*
373 * We cannot do anything because we don't have a timing function
374 */
375#ifdef HAVE_STDARG_H
376 va_start(ap, format);
377 vfprintf(stderr, format, ap);
378 va_end(ap);
379 fprintf(stderr, " was not timed\n", msec);
380#else
381 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
382 * this ?!
383 */
384#endif
385}
386#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000387/************************************************************************
388 * *
389 * HTML ouput *
390 * *
391 ************************************************************************/
392char buffer[50000];
393
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000394static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000395xmlHTMLEncodeSend(void) {
396 char *result;
397
398 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
399 if (result) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000400 xmlGenericError(xmlGenericErrorContext, "%s", result);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000401 xmlFree(result);
402 }
403 buffer[0] = 0;
404}
405
406/**
407 * xmlHTMLPrintFileInfo:
408 * @input: an xmlParserInputPtr input
409 *
410 * Displays the associated file and line informations for the current input
411 */
412
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000413static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000414xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000415 int len;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000416 xmlGenericError(xmlGenericErrorContext, "<p>");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000417
418 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000419 if (input != NULL) {
420 if (input->filename) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000421 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000422 input->line);
423 } else {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000424 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000425 }
426 }
427 xmlHTMLEncodeSend();
428}
429
430/**
431 * xmlHTMLPrintFileContext:
432 * @input: an xmlParserInputPtr input
433 *
434 * Displays current context within the input content for error tracking
435 */
436
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000437static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000438xmlHTMLPrintFileContext(xmlParserInputPtr input) {
439 const xmlChar *cur, *base;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000440 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000441 int n;
442
443 if (input == NULL) return;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000444 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000445 cur = input->cur;
446 base = input->base;
447 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
448 cur--;
449 }
450 n = 0;
451 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
452 cur--;
453 if ((*cur == '\n') || (*cur == '\r')) cur++;
454 base = cur;
455 n = 0;
456 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000457 len = strlen(buffer);
458 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
459 (unsigned char) *cur++);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000460 n++;
461 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000462 len = strlen(buffer);
463 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000464 cur = input->cur;
465 while ((*cur == '\n') || (*cur == '\r'))
466 cur--;
467 n = 0;
468 while ((cur != base) && (n++ < 80)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000469 len = strlen(buffer);
470 snprintf(&buffer[len], sizeof(buffer) - len, " ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000471 base++;
472 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000473 len = strlen(buffer);
474 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000475 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000476 xmlGenericError(xmlGenericErrorContext, "</pre>");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000477}
478
479/**
480 * xmlHTMLError:
481 * @ctx: an XML parser context
482 * @msg: the message to display/transmit
483 * @...: extra parameters for the message display
484 *
485 * Display and format an error messages, gives file, line, position and
486 * extra parameters.
487 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000488static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000489xmlHTMLError(void *ctx, const char *msg, ...)
490{
491 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
492 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000493 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000494 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000495
496 buffer[0] = 0;
497 input = ctxt->input;
498 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000499 input = ctxt->inputTab[ctxt->inputNr - 2];
500 }
501
502 xmlHTMLPrintFileInfo(input);
503
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000504 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000505 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000506 len = strlen(buffer);
507 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000508 va_end(args);
509 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000510 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000511
512 xmlHTMLPrintFileContext(input);
513 xmlHTMLEncodeSend();
514}
515
516/**
517 * xmlHTMLWarning:
518 * @ctx: an XML parser context
519 * @msg: the message to display/transmit
520 * @...: extra parameters for the message display
521 *
522 * Display and format a warning messages, gives file, line, position and
523 * extra parameters.
524 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000525static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000526xmlHTMLWarning(void *ctx, const char *msg, ...)
527{
528 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
529 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000530 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000531 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000532
533 buffer[0] = 0;
534 input = ctxt->input;
535 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000536 input = ctxt->inputTab[ctxt->inputNr - 2];
537 }
538
539
540 xmlHTMLPrintFileInfo(input);
541
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000542 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000543 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000544 len = strlen(buffer);
545 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000546 va_end(args);
547 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000548 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000549
550 xmlHTMLPrintFileContext(input);
551 xmlHTMLEncodeSend();
552}
553
554/**
555 * xmlHTMLValidityError:
556 * @ctx: an XML parser context
557 * @msg: the message to display/transmit
558 * @...: extra parameters for the message display
559 *
560 * Display and format an validity error messages, gives file,
561 * line, position and extra parameters.
562 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000563static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000564xmlHTMLValidityError(void *ctx, const char *msg, ...)
565{
566 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
567 xmlParserInputPtr input;
568 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000569 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000570
571 buffer[0] = 0;
572 input = ctxt->input;
573 if ((input->filename == NULL) && (ctxt->inputNr > 1))
574 input = ctxt->inputTab[ctxt->inputNr - 2];
575
576 xmlHTMLPrintFileInfo(input);
577
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000578 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000579 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000580 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000581 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000582 va_end(args);
583 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000584 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000585
586 xmlHTMLPrintFileContext(input);
587 xmlHTMLEncodeSend();
588}
589
590/**
591 * xmlHTMLValidityWarning:
592 * @ctx: an XML parser context
593 * @msg: the message to display/transmit
594 * @...: extra parameters for the message display
595 *
596 * Display and format a validity warning messages, gives file, line,
597 * position and extra parameters.
598 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000599static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000600xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
601{
602 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
603 xmlParserInputPtr input;
604 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000605 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000606
607 buffer[0] = 0;
608 input = ctxt->input;
609 if ((input->filename == NULL) && (ctxt->inputNr > 1))
610 input = ctxt->inputTab[ctxt->inputNr - 2];
611
612 xmlHTMLPrintFileInfo(input);
613
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000614 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000615 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000616 len = strlen(buffer);
617 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000618 va_end(args);
619 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000620 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000621
622 xmlHTMLPrintFileContext(input);
623 xmlHTMLEncodeSend();
624}
625
626/************************************************************************
627 * *
628 * Shell Interface *
629 * *
630 ************************************************************************/
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000631#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000632/**
633 * xmlShellReadline:
634 * @prompt: the prompt value
635 *
636 * Read a string
637 *
638 * Returns a pointer to it or NULL on EOF the caller is expected to
639 * free the returned string.
640 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000641static char *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000642xmlShellReadline(char *prompt) {
643#ifdef HAVE_LIBREADLINE
644 char *line_read;
645
646 /* Get a line from the user. */
647 line_read = readline (prompt);
648
649 /* If the line has any text in it, save it on the history. */
650 if (line_read && *line_read)
651 add_history (line_read);
652
653 return (line_read);
654#else
655 char line_read[501];
Daniel Veillard29e43992001-12-13 22:21:58 +0000656 char *ret;
657 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000658
659 if (prompt != NULL)
660 fprintf(stdout, "%s", prompt);
661 if (!fgets(line_read, 500, stdin))
662 return(NULL);
663 line_read[500] = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000664 len = strlen(line_read);
665 ret = (char *) malloc(len + 1);
666 if (ret != NULL) {
667 memcpy (ret, line_read, len + 1);
668 }
669 return(ret);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000670#endif
671}
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000672#endif /* LIBXML_DEBUG_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000673
674/************************************************************************
675 * *
Daniel Veillard5e873c42000-04-12 13:27:38 +0000676 * I/O Interfaces *
677 * *
678 ************************************************************************/
679
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000680static int myRead(FILE *f, char * buf, int len) {
681 return(fread(buf, 1, len, f));
Daniel Veillard5e873c42000-04-12 13:27:38 +0000682}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000683static void myClose(FILE *f) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000684 if (f != stdin) {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000685 fclose(f);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000686 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000687}
688
Daniel Veillard81273902003-09-30 00:43:48 +0000689#ifdef LIBXML_READER_ENABLED
Daniel Veillard5e873c42000-04-12 13:27:38 +0000690/************************************************************************
691 * *
Daniel Veillard7704fb12003-01-03 16:19:51 +0000692 * Stream Test processing *
693 * *
694 ************************************************************************/
Daniel Veillard7704fb12003-01-03 16:19:51 +0000695static void processNode(xmlTextReaderPtr reader) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000696 const xmlChar *name, *value;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000697
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000698 name = xmlTextReaderConstName(reader);
Daniel Veillarde59494f2003-01-04 16:35:29 +0000699 if (name == NULL)
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000700 name = BAD_CAST "--";
Daniel Veillard99737f52003-03-22 14:55:50 +0000701
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000702 value = xmlTextReaderConstValue(reader);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000703
Daniel Veillard99737f52003-03-22 14:55:50 +0000704 printf("%d %d %s %d %d",
Daniel Veillarde59494f2003-01-04 16:35:29 +0000705 xmlTextReaderDepth(reader),
706 xmlTextReaderNodeType(reader),
707 name,
Daniel Veillard99737f52003-03-22 14:55:50 +0000708 xmlTextReaderIsEmptyElement(reader),
709 xmlTextReaderHasValue(reader));
Daniel Veillarde59494f2003-01-04 16:35:29 +0000710 if (value == NULL)
711 printf("\n");
712 else {
713 printf(" %s\n", value);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000714 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000715#ifdef LIBXML_PATTERN_ENABLED
716 if (patternc) {
717 if (xmlPatternMatch(patternc, xmlTextReaderCurrentNode(reader)) == 1) {
718 printf("Node matches pattern %s\n", pattern);
719 }
720 }
721#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000722}
723
724static void streamFile(char *filename) {
725 xmlTextReaderPtr reader;
726 int ret;
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000727#ifdef HAVE_SYS_MMAN_H
728 int fd = -1;
729 struct stat info;
730 const char *base = NULL;
731 xmlParserInputBufferPtr input = NULL;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000732
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000733 if (memory) {
734 if (stat(filename, &info) < 0)
735 return;
736 if ((fd = open(filename, O_RDONLY)) < 0)
737 return;
738 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
739 if (base == (void *) MAP_FAILED)
740 return;
741
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000742 reader = xmlReaderForMemory(base, info.st_size, filename,
743 NULL, options);
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000744 } else
745#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000746 reader = xmlReaderForFile(filename, NULL, options);
747
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000748
Daniel Veillard7704fb12003-01-03 16:19:51 +0000749 if (reader != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +0000750#ifdef LIBXML_VALID_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000751 if (valid)
752 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +0000753 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000754#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce192eb2003-04-16 15:58:05 +0000755 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000756#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +0000757 if (relaxng != NULL) {
Daniel Veillard81514ba2003-09-16 23:17:26 +0000758 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000759 startTimer();
760 }
761 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
762 if (ret < 0) {
763 xmlGenericError(xmlGenericErrorContext,
764 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +0000765 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +0000766 relaxng = NULL;
767 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000768 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000769 endTimer("Compiling the schemas");
770 }
771 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000772#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000773
774 /*
775 * Process all nodes in sequence
776 */
Daniel Veillard81514ba2003-09-16 23:17:26 +0000777 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000778 startTimer();
779 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000780 ret = xmlTextReaderRead(reader);
781 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000782 if ((debug)
783#ifdef LIBXML_PATTERN_ENABLED
784 || (patternc)
785#endif
786 )
Daniel Veillard7704fb12003-01-03 16:19:51 +0000787 processNode(reader);
788 ret = xmlTextReaderRead(reader);
789 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000790 if ((timing) && (!repeat)) {
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000791#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf54cd532004-02-25 11:52:31 +0000792 if (relaxng != NULL)
Daniel Veillard49138f12004-02-19 12:58:36 +0000793 endTimer("Parsing and validating");
794 else
Daniel Veillardf54cd532004-02-25 11:52:31 +0000795#endif
Daniel Veillard4432df22003-09-28 18:58:27 +0000796#ifdef LIBXML_VALID_ENABLED
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000797 if (valid)
Daniel Veillardce192eb2003-04-16 15:58:05 +0000798 endTimer("Parsing and validating");
799 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000800#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +0000801 endTimer("Parsing");
Daniel Veillardce192eb2003-04-16 15:58:05 +0000802 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000803
Daniel Veillard4432df22003-09-28 18:58:27 +0000804#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf6bad792003-04-11 19:38:54 +0000805 if (valid) {
806 if (xmlTextReaderIsValid(reader) != 1) {
807 xmlGenericError(xmlGenericErrorContext,
808 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +0000809 progresult = XMLLINT_ERR_VALID;
Daniel Veillardf6bad792003-04-11 19:38:54 +0000810 }
811 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000812#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000813#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +0000814 if (relaxng != NULL) {
815 if (xmlTextReaderIsValid(reader) != 1) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +0000816 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +0000817 progresult = XMLLINT_ERR_VALID;
Daniel Veillardf4e55762003-04-15 23:32:22 +0000818 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +0000819 fprintf(stderr, "%s validates\n", filename);
Daniel Veillardf4e55762003-04-15 23:32:22 +0000820 }
821 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000822#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000823 /*
824 * Done, cleanup and status
825 */
826 xmlFreeTextReader(reader);
827 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +0000828 fprintf(stderr, "%s : failed to parse\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +0000829 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000830 }
831 } else {
832 fprintf(stderr, "Unable to open %s\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +0000833 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000834 }
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000835#ifdef HAVE_SYS_MMAN_H
836 if (memory) {
837 xmlFreeParserInputBuffer(input);
838 munmap((char *) base, info.st_size);
839 close(fd);
840 }
841#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000842}
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000843
844static void walkDoc(xmlDocPtr doc) {
845 xmlTextReaderPtr reader;
846 int ret;
847
848 reader = xmlReaderWalker(doc);
849 if (reader != NULL) {
850 if ((timing) && (!repeat)) {
851 startTimer();
852 }
853 ret = xmlTextReaderRead(reader);
854 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000855 if ((debug)
856#ifdef LIBXML_PATTERN_ENABLED
857 || (patternc)
858#endif
859 )
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000860 processNode(reader);
861 ret = xmlTextReaderRead(reader);
862 }
863 if ((timing) && (!repeat)) {
864 endTimer("walking through the doc");
865 }
866 xmlFreeTextReader(reader);
867 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +0000868 fprintf(stderr, "failed to walk through the doc\n");
William M. Brack8304d872004-06-08 13:29:32 +0000869 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000870 }
871 } else {
872 fprintf(stderr, "Failed to crate a reader from the document\n");
William M. Brack8304d872004-06-08 13:29:32 +0000873 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000874 }
875}
Daniel Veillard81273902003-09-30 00:43:48 +0000876#endif /* LIBXML_READER_ENABLED */
Daniel Veillard7704fb12003-01-03 16:19:51 +0000877
878/************************************************************************
879 * *
880 * Tree Test processing *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000881 * *
882 ************************************************************************/
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000883static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
Daniel Veillard652327a2003-09-29 18:02:38 +0000884 xmlDocPtr doc = NULL;
885#ifdef LIBXML_TREE_ENABLED
886 xmlDocPtr tmp;
887#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000888
Daniel Veillard48b2f892001-02-25 16:11:03 +0000889 if ((timing) && (!repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +0000890 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000891
892
Daniel Veillard652327a2003-09-29 18:02:38 +0000893#ifdef LIBXML_TREE_ENABLED
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000894 if (filename == NULL) {
895 if (generate) {
896 xmlNodePtr n;
897
898 doc = xmlNewDoc(BAD_CAST "1.0");
899 n = xmlNewNode(NULL, BAD_CAST "info");
900 xmlNodeSetContent(n, BAD_CAST "abc");
901 xmlDocSetRootElement(doc, n);
902 }
903 }
Daniel Veillard652327a2003-09-29 18:02:38 +0000904#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000905#ifdef LIBXML_HTML_ENABLED
Daniel Veillard73b013f2003-09-30 12:36:01 +0000906#ifdef LIBXML_PUSH_ENABLED
William M. Brack78637da2003-07-31 14:47:38 +0000907 else if ((html) && (push)) {
908 FILE *f;
909
William M. Brack3403add2004-06-27 02:07:51 +0000910#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
911 f = fopen(filename, "rb");
912#else
913 f = fopen(filename, "r");
914#endif
William M. Brack78637da2003-07-31 14:47:38 +0000915 if (f != NULL) {
916 int res, size = 3;
917 char chars[4096];
918 htmlParserCtxtPtr ctxt;
919
920 /* if (repeat) */
921 size = 4096;
922 res = fread(chars, 1, 4, f);
923 if (res > 0) {
924 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
William M. Brack1d75c8a2003-10-27 13:48:16 +0000925 chars, res, filename, XML_CHAR_ENCODING_NONE);
William M. Brack78637da2003-07-31 14:47:38 +0000926 while ((res = fread(chars, 1, size, f)) > 0) {
927 htmlParseChunk(ctxt, chars, res, 0);
928 }
929 htmlParseChunk(ctxt, chars, 0, 1);
930 doc = ctxt->myDoc;
931 htmlFreeParserCtxt(ctxt);
932 }
933 fclose(f);
934 }
935 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000936#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000937 else if (html) {
Daniel Veillard9475a352003-09-26 12:47:50 +0000938 doc = htmlReadFile(filename, NULL, options);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000939 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000940#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000941 else {
Daniel Veillard73b013f2003-09-30 12:36:01 +0000942#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000943 /*
944 * build an XML tree from a string;
945 */
946 if (push) {
947 FILE *f;
948
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000949 /* '-' Usually means stdin -<sven@zen.org> */
950 if ((filename[0] == '-') && (filename[1] == 0)) {
951 f = stdin;
952 } else {
William M. Brack3403add2004-06-27 02:07:51 +0000953#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
954 f = fopen(filename, "rb");
955#else
956 f = fopen(filename, "r");
957#endif
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000958 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000959 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +0000960 int ret;
Daniel Veillarda880b122003-04-21 21:36:41 +0000961 int res, size = 1024;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000962 char chars[1024];
963 xmlParserCtxtPtr ctxt;
964
Daniel Veillarda880b122003-04-21 21:36:41 +0000965 /* if (repeat) size = 1024; */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000966 res = fread(chars, 1, 4, f);
967 if (res > 0) {
968 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
969 chars, res, filename);
Daniel Veillard500a1de2004-03-22 15:22:58 +0000970 xmlCtxtUseOptions(ctxt, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000971 while ((res = fread(chars, 1, size, f)) > 0) {
972 xmlParseChunk(ctxt, chars, res, 0);
973 }
974 xmlParseChunk(ctxt, chars, 0, 1);
975 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +0000976 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000977 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +0000978 if (!ret) {
979 xmlFreeDoc(doc);
980 doc = NULL;
981 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000982 }
983 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000984 } else
985#endif /* LIBXML_PUSH_ENABLED */
986 if (testIO) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000987 if ((filename[0] == '-') && (filename[1] == 0)) {
Daniel Veillard60942de2003-09-25 21:05:58 +0000988 doc = xmlReadFd(0, NULL, NULL, options);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000989 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000990 FILE *f;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000991
William M. Brack3403add2004-06-27 02:07:51 +0000992#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
993 f = fopen(filename, "rb");
994#else
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000995 f = fopen(filename, "r");
William M. Brack3403add2004-06-27 02:07:51 +0000996#endif
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000997 if (f != NULL) {
998 if (rectxt == NULL)
999 doc = xmlReadIO((xmlInputReadCallback) myRead,
1000 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +00001001 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001002 else
1003 doc = xmlCtxtReadIO(rectxt,
1004 (xmlInputReadCallback) myRead,
1005 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +00001006 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001007 } else
Daniel Veillard5e873c42000-04-12 13:27:38 +00001008 doc = NULL;
Daniel Veillard5e873c42000-04-12 13:27:38 +00001009 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001010 } else if (htmlout) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001011 xmlParserCtxtPtr ctxt;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001012
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001013 if (rectxt == NULL)
1014 ctxt = xmlNewParserCtxt();
1015 else
1016 ctxt = rectxt;
Daniel Veillard88a172f2000-08-04 18:23:10 +00001017 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001018 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +00001019 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001020 ctxt->sax->error = xmlHTMLError;
1021 ctxt->sax->warning = xmlHTMLWarning;
1022 ctxt->vctxt.error = xmlHTMLValidityError;
1023 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001024
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001025 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001026
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001027 if (rectxt == NULL)
1028 xmlFreeParserCtxt(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001029 }
Daniel Veillard46e370e2000-07-21 20:32:03 +00001030#ifdef HAVE_SYS_MMAN_H
1031 } else if (memory) {
1032 int fd;
1033 struct stat info;
1034 const char *base;
1035 if (stat(filename, &info) < 0)
1036 return;
1037 if ((fd = open(filename, O_RDONLY)) < 0)
1038 return;
1039 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +00001040 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +00001041 return;
1042
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001043 if (rectxt == NULL)
Daniel Veillard60942de2003-09-25 21:05:58 +00001044 doc = xmlReadMemory((char *) base, info.st_size,
1045 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001046 else
Daniel Veillard60942de2003-09-25 21:05:58 +00001047 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
1048 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001049
Daniel Veillard46e370e2000-07-21 20:32:03 +00001050 munmap((char *) base, info.st_size);
1051#endif
Daniel Veillard4432df22003-09-28 18:58:27 +00001052#ifdef LIBXML_VALID_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00001053 } else if (valid) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001054 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001055
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001056 if (rectxt == NULL)
1057 ctxt = xmlNewParserCtxt();
1058 else
1059 ctxt = rectxt;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001060 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001061 doc = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001062 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001063 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
1064
1065 if (ctxt->valid == 0)
William M. Brack8304d872004-06-08 13:29:32 +00001066 progresult = XMLLINT_ERR_RDFILE;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001067 if (rectxt == NULL)
1068 xmlFreeParserCtxt(ctxt);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001069 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001070#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00001071 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001072 if (rectxt != NULL)
1073 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
1074 else
1075 doc = xmlReadFile(filename, NULL, options);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001076 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001077 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001078
Daniel Veillard88a172f2000-08-04 18:23:10 +00001079 /*
1080 * If we don't have a document we might as well give up. Do we
1081 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001082 if (doc == NULL) {
William M. Brack8304d872004-06-08 13:29:32 +00001083 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard88a172f2000-08-04 18:23:10 +00001084 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001085 }
1086
Daniel Veillard48b2f892001-02-25 16:11:03 +00001087 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001088 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001089 }
1090
Daniel Veillard29e43992001-12-13 22:21:58 +00001091 /*
1092 * Remove DOCTYPE nodes
1093 */
1094 if (dropdtd) {
1095 xmlDtdPtr dtd;
1096
1097 dtd = xmlGetIntSubset(doc);
1098 if (dtd != NULL) {
1099 xmlUnlinkNode((xmlNodePtr)dtd);
1100 xmlFreeDtd(dtd);
1101 }
1102 }
1103
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001104#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +00001105 if (xinclude) {
1106 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001107 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001108 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +00001109 xmlXIncludeProcessFlags(doc, options);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001110 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001111 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001112 }
1113 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001114#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +00001115
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001116#ifdef LIBXML_DEBUG_ENABLED
1117 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001118 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001119 */
1120 if (shell)
1121 xmlShell(doc, filename, xmlShellReadline, stdout);
1122#endif
1123
Daniel Veillard652327a2003-09-29 18:02:38 +00001124#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001125 /*
1126 * test intermediate copy if needed.
1127 */
1128 if (copy) {
1129 tmp = doc;
1130 doc = xmlCopyDoc(doc, 1);
1131 xmlFreeDoc(tmp);
1132 }
Daniel Veillard652327a2003-09-29 18:02:38 +00001133#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001134
Daniel Veillard4432df22003-09-28 18:58:27 +00001135#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001136 if ((insert) && (!html)) {
1137 const xmlChar* list[256];
1138 int nb, i;
1139 xmlNodePtr node;
1140
1141 if (doc->children != NULL) {
1142 node = doc->children;
1143 while ((node != NULL) && (node->last == NULL)) node = node->next;
1144 if (node != NULL) {
1145 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
1146 if (nb < 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001147 fprintf(stderr, "could not get valid list of elements\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001148 } else if (nb == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001149 fprintf(stderr, "No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001150 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001151 fprintf(stderr, "%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001152 nb);
1153 for (i = 0;i < nb;i++) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001154 fprintf(stderr, "%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001155 }
1156 }
1157 }
1158 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001159 }else
1160#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001161#ifdef LIBXML_READER_ENABLED
1162 if (walker) {
1163 walkDoc(doc);
1164 }
1165#endif /* LIBXML_READER_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001166#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001167 if (noout == 0) {
Daniel Veillard3df01182003-12-10 10:17:51 +00001168 int ret;
1169
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001170 /*
1171 * print it.
1172 */
1173#ifdef LIBXML_DEBUG_ENABLED
1174 if (!debug) {
1175#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00001176 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001177 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001178 }
Daniel Veillard656ce942004-04-30 23:11:45 +00001179#ifdef LIBXML_HTML_ENABLED
Daniel Veillard42fd4122003-11-04 08:47:48 +00001180 if ((html) && (!xmlout)) {
1181 if (compress) {
1182 htmlSaveFile(output ? output : "-", doc);
1183 }
1184 else if (encoding != NULL) {
1185 if ( format ) {
1186 htmlSaveFileFormat(output ? output : "-", doc, encoding, 1);
1187 }
1188 else {
1189 htmlSaveFileFormat(output ? output : "-", doc, encoding, 0);
1190 }
1191 }
1192 else if (format) {
1193 htmlSaveFileFormat(output ? output : "-", doc, NULL, 1);
1194 }
1195 else {
1196 FILE *out;
1197 if (output == NULL)
1198 out = stdout;
1199 else {
1200 out = fopen(output,"wb");
1201 }
1202 if (out != NULL) {
1203 if (htmlDocDump(out, doc) < 0)
William M. Brack8304d872004-06-08 13:29:32 +00001204 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00001205
1206 if (output != NULL)
1207 fclose(out);
1208 } else {
1209 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00001210 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00001211 }
1212 }
1213 if ((timing) && (!repeat)) {
1214 endTimer("Saving");
1215 }
1216 } else
1217#endif
Daniel Veillard25048d82004-08-14 22:37:54 +00001218#ifdef LIBXML_C14N_ENABLED
1219 if (canonical) {
1220 xmlChar *result = NULL;
1221 int size;
1222
1223 size = xmlC14NDocDumpMemory(doc, NULL, 0, NULL, 1, &result);
1224 if (size >= 0) {
1225 write(1, result, size);
1226 xmlFree(result);
1227 } else {
1228 fprintf(stderr, "Failed to canonicalize\n");
1229 progresult = XMLLINT_ERR_OUT;
1230 }
1231 } else
1232#endif
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001233#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001234 if (memory) {
1235 xmlChar *result;
1236 int len;
1237
1238 if (encoding != NULL) {
Daniel Veillardd536f702001-11-08 17:32:47 +00001239 if ( format ) {
1240 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
1241 } else {
1242 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
1243 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001244 } else {
Daniel Veillard90493a92001-08-14 14:12:47 +00001245 if (format)
1246 xmlDocDumpFormatMemory(doc, &result, &len, 1);
1247 else
1248 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001249 }
1250 if (result == NULL) {
1251 fprintf(stderr, "Failed to save\n");
Daniel Veillard25048d82004-08-14 22:37:54 +00001252 progresult = XMLLINT_ERR_OUT;
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001253 } else {
1254 write(1, result, len);
1255 xmlFree(result);
1256 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001257 } else
1258#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001259 if (compress) {
1260 xmlSaveFile(output ? output : "-", doc);
1261 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001262 else if (encoding != NULL) {
1263 if ( format ) {
Daniel Veillard3df01182003-12-10 10:17:51 +00001264 ret = xmlSaveFormatFileEnc(output ? output : "-", doc,
1265 encoding, 1);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001266 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001267 else {
Daniel Veillard3df01182003-12-10 10:17:51 +00001268 ret = xmlSaveFileEnc(output ? output : "-", doc, encoding);
1269 }
1270 if (ret < 0) {
1271 fprintf(stderr, "failed save to %s\n",
1272 output ? output : "-");
William M. Brack8304d872004-06-08 13:29:32 +00001273 progresult = XMLLINT_ERR_OUT;
Daniel Veillardd536f702001-11-08 17:32:47 +00001274 }
1275 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001276 else if (format) {
Daniel Veillard3df01182003-12-10 10:17:51 +00001277 ret = xmlSaveFormatFile(output ? output : "-", doc, 1);
1278 if (ret < 0) {
1279 fprintf(stderr, "failed save to %s\n",
1280 output ? output : "-");
William M. Brack8304d872004-06-08 13:29:32 +00001281 progresult = XMLLINT_ERR_OUT;
Daniel Veillard3df01182003-12-10 10:17:51 +00001282 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001283 }
1284 else {
1285 FILE *out;
1286 if (output == NULL)
1287 out = stdout;
1288 else {
1289 out = fopen(output,"wb");
1290 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001291 if (out != NULL) {
Daniel Veillard828ce832003-10-08 19:19:10 +00001292 if (xmlDocDump(out, doc) < 0)
William M. Brack8304d872004-06-08 13:29:32 +00001293 progresult = XMLLINT_ERR_OUT;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001294
Daniel Veillard05d987b2003-10-08 11:54:57 +00001295 if (output != NULL)
1296 fclose(out);
1297 } else {
1298 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00001299 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00001300 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001301 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001302 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001303 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001304 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001305#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001306 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001307 FILE *out;
1308 if (output == NULL)
1309 out = stdout;
1310 else {
1311 out = fopen(output,"wb");
1312 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001313 if (out != NULL) {
1314 xmlDebugDumpDocument(out, doc);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001315
Daniel Veillard05d987b2003-10-08 11:54:57 +00001316 if (output != NULL)
1317 fclose(out);
1318 } else {
1319 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00001320 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00001321 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001322 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001323#endif
1324 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001325#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001326
Daniel Veillard4432df22003-09-28 18:58:27 +00001327#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001328 /*
1329 * A posteriori validation test
1330 */
Daniel Veillard66f68e72003-08-18 16:39:51 +00001331 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
Daniel Veillardcd429612000-10-11 15:57:05 +00001332 xmlDtdPtr dtd;
1333
Daniel Veillard48b2f892001-02-25 16:11:03 +00001334 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001335 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001336 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00001337 if (dtdvalid != NULL)
1338 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
1339 else
1340 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001341 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001342 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001343 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001344 if (dtd == NULL) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001345 if (dtdvalid != NULL)
1346 xmlGenericError(xmlGenericErrorContext,
1347 "Could not parse DTD %s\n", dtdvalid);
1348 else
1349 xmlGenericError(xmlGenericErrorContext,
1350 "Could not parse DTD %s\n", dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00001351 progresult = XMLLINT_ERR_DTD;
Daniel Veillardcd429612000-10-11 15:57:05 +00001352 } else {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001353 xmlValidCtxtPtr cvp;
1354
1355 if ((cvp = xmlNewValidCtxt()) == NULL) {
1356 xmlGenericError(xmlGenericErrorContext,
1357 "Couldn't allocate validation context\n");
1358 exit(-1);
1359 }
1360 cvp->userData = (void *) stderr;
1361 cvp->error = (xmlValidityErrorFunc) fprintf;
1362 cvp->warning = (xmlValidityWarningFunc) fprintf;
1363
Daniel Veillard48b2f892001-02-25 16:11:03 +00001364 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001365 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001366 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001367 if (!xmlValidateDtd(cvp, doc, dtd)) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001368 if (dtdvalid != NULL)
1369 xmlGenericError(xmlGenericErrorContext,
1370 "Document %s does not validate against %s\n",
1371 filename, dtdvalid);
1372 else
1373 xmlGenericError(xmlGenericErrorContext,
1374 "Document %s does not validate against %s\n",
1375 filename, dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00001376 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00001377 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001378 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001379 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001380 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001381 xmlFreeValidCtxt(cvp);
Daniel Veillardcd429612000-10-11 15:57:05 +00001382 xmlFreeDtd(dtd);
1383 }
1384 } else if (postvalid) {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001385 xmlValidCtxtPtr cvp;
1386
1387 if ((cvp = xmlNewValidCtxt()) == NULL) {
1388 xmlGenericError(xmlGenericErrorContext,
1389 "Couldn't allocate validation context\n");
1390 exit(-1);
1391 }
1392
Daniel Veillard48b2f892001-02-25 16:11:03 +00001393 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001394 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001395 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001396 cvp->userData = (void *) stderr;
1397 cvp->error = (xmlValidityErrorFunc) fprintf;
1398 cvp->warning = (xmlValidityWarningFunc) fprintf;
1399 if (!xmlValidateDocument(cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001400 xmlGenericError(xmlGenericErrorContext,
1401 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001402 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00001403 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001404 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001405 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001406 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001407 xmlFreeValidCtxt(cvp);
Daniel Veillard4432df22003-09-28 18:58:27 +00001408 }
1409#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00001410#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001411 if (relaxngschemas != NULL) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001412 xmlRelaxNGValidCtxtPtr ctxt;
1413 int ret;
1414
Daniel Veillard42f12e92003-03-07 18:32:59 +00001415 if ((timing) && (!repeat)) {
1416 startTimer();
1417 }
1418
Daniel Veillard71531f32003-02-05 13:19:53 +00001419 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
1420 xmlRelaxNGSetValidErrors(ctxt,
1421 (xmlRelaxNGValidityErrorFunc) fprintf,
1422 (xmlRelaxNGValidityWarningFunc) fprintf,
1423 stderr);
1424 ret = xmlRelaxNGValidateDoc(ctxt, doc);
1425 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001426 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard71531f32003-02-05 13:19:53 +00001427 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001428 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001429 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00001430 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001431 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard71531f32003-02-05 13:19:53 +00001432 filename);
William M. Brack8304d872004-06-08 13:29:32 +00001433 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00001434 }
1435 xmlRelaxNGFreeValidCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001436 if ((timing) && (!repeat)) {
1437 endTimer("Validating");
1438 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001439 } else if (wxschemas != NULL) {
1440 xmlSchemaValidCtxtPtr ctxt;
1441 int ret;
1442
1443 if ((timing) && (!repeat)) {
1444 startTimer();
1445 }
1446
1447 ctxt = xmlSchemaNewValidCtxt(wxschemas);
1448 xmlSchemaSetValidErrors(ctxt,
1449 (xmlSchemaValidityErrorFunc) fprintf,
1450 (xmlSchemaValidityWarningFunc) fprintf,
1451 stderr);
1452 ret = xmlSchemaValidateDoc(ctxt, doc);
1453 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001454 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001455 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001456 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001457 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001458 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001459 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001460 filename);
William M. Brack8304d872004-06-08 13:29:32 +00001461 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001462 }
1463 xmlSchemaFreeValidCtxt(ctxt);
1464 if ((timing) && (!repeat)) {
1465 endTimer("Validating");
1466 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001467 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001468#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001469
1470#ifdef LIBXML_DEBUG_ENABLED
1471 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001472 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001473#endif
1474
1475 /*
1476 * free it.
1477 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00001478 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001479 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001480 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001481 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001482 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001483 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001484 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001485}
1486
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001487/************************************************************************
1488 * *
1489 * Usage and Main *
1490 * *
1491 ************************************************************************/
1492
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001493static void showVersion(const char *name) {
1494 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
1495 fprintf(stderr, " compiled with: ");
Daniel Veillard4432df22003-09-28 18:58:27 +00001496#ifdef LIBXML_VALID_ENABLED
1497 fprintf(stderr, "DTDValid ");
1498#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001499#ifdef LIBXML_FTP_ENABLED
1500 fprintf(stderr, "FTP ");
1501#endif
1502#ifdef LIBXML_HTTP_ENABLED
1503 fprintf(stderr, "HTTP ");
1504#endif
1505#ifdef LIBXML_HTML_ENABLED
1506 fprintf(stderr, "HTML ");
1507#endif
1508#ifdef LIBXML_C14N_ENABLED
1509 fprintf(stderr, "C14N ");
1510#endif
1511#ifdef LIBXML_CATALOG_ENABLED
1512 fprintf(stderr, "Catalog ");
1513#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001514#ifdef LIBXML_XPATH_ENABLED
1515 fprintf(stderr, "XPath ");
1516#endif
1517#ifdef LIBXML_XPTR_ENABLED
1518 fprintf(stderr, "XPointer ");
1519#endif
1520#ifdef LIBXML_XINCLUDE_ENABLED
1521 fprintf(stderr, "XInclude ");
1522#endif
1523#ifdef LIBXML_ICONV_ENABLED
1524 fprintf(stderr, "Iconv ");
1525#endif
1526#ifdef DEBUG_MEMORY_LOCATION
1527 fprintf(stderr, "MemDebug ");
1528#endif
1529#ifdef LIBXML_UNICODE_ENABLED
1530 fprintf(stderr, "Unicode ");
1531#endif
1532#ifdef LIBXML_REGEXP_ENABLED
1533 fprintf(stderr, "Regexps ");
1534#endif
1535#ifdef LIBXML_AUTOMATA_ENABLED
1536 fprintf(stderr, "Automata ");
1537#endif
1538#ifdef LIBXML_SCHEMAS_ENABLED
1539 fprintf(stderr, "Schemas ");
1540#endif
1541 fprintf(stderr, "\n");
1542}
1543
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001544static void usage(const char *name) {
1545 printf("Usage : %s [options] XMLfiles ...\n", name);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001546#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001547 printf("\tParse the XML files and output the result of the parsing\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001548#else
1549 printf("\tParse the XML files\n");
1550#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001551 printf("\t--version : display the version of the XML library used\n");
1552#ifdef LIBXML_DEBUG_ENABLED
1553 printf("\t--debug : dump a debug tree of the in-memory document\n");
1554 printf("\t--shell : run a navigating shell\n");
1555 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00001556#else
Daniel Veillard81273902003-09-30 00:43:48 +00001557#ifdef LIBXML_READER_ENABLED
Daniel Veillard8326e732003-01-07 00:19:07 +00001558 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001559#endif /* LIBXML_READER_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001560#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001561#ifdef LIBXML_TREE_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001562 printf("\t--copy : used to test the internal copy implementation\n");
Daniel Veillard652327a2003-09-29 18:02:38 +00001563#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001564 printf("\t--recover : output what was parsable on broken XML documents\n");
1565 printf("\t--noent : substitute entity references by their value\n");
1566 printf("\t--noout : don't output the result tree\n");
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001567 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001568 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001569 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001570#ifdef LIBXML_VALID_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001571 printf("\t--valid : validate the document in addition to std well-formed check\n");
1572 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
1573 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
Daniel Veillard66f68e72003-08-18 16:39:51 +00001574 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001575#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001576 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001577 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001578 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
1579 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001580#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001581#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001582 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001583#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001584#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001585#ifdef LIBXML_HTML_ENABLED
1586 printf("\t--html : use the HTML parser\n");
Daniel Veillard42fd4122003-11-04 08:47:48 +00001587 printf("\t--xmlout : force to use the XML serializer when using --html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001588#endif
Daniel Veillard73b013f2003-09-30 12:36:01 +00001589#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001590 printf("\t--push : use the push mode of the parser\n");
Daniel Veillard73b013f2003-09-30 12:36:01 +00001591#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001592#ifdef HAVE_SYS_MMAN_H
1593 printf("\t--memory : parse from memory\n");
1594#endif
Daniel Veillard87076042004-05-03 22:54:49 +00001595 printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001596 printf("\t--nowarning : do not emit warnings from parser/validator\n");
1597 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001598 printf("\t--nocdata : replace cdata section with text nodes\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001599#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001600 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001601 printf("\t--encode encoding : output in the given encoding\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001602 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
1603#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard25048d82004-08-14 22:37:54 +00001604 printf("\t--c14n: save in W3C canonical format (with comments)\n");
1605#ifdef LIBXML_C14N_ENABLED
1606#endif /* LIBXML_C14N_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001607 printf("\t--nsclean : remove redundant namespace declarations\n");
1608 printf("\t--testIO : test user I/O support\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001609#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00001610 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
1611 printf("\t otherwise XML Catalogs starting from \n");
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001612 printf("\t %s are activated by default\n", XML_XML_DEFAULT_CATALOG);
Daniel Veillard05c13a22001-09-09 08:38:09 +00001613 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001614#endif
1615 printf("\t--auto : generate a small doc on the fly\n");
1616#ifdef LIBXML_XINCLUDE_ENABLED
1617 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00001618 printf("\t--noxincludenode : same but do not generate XInclude nodes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001619#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001620 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00001621 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard81273902003-09-30 00:43:48 +00001622#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001623 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001624 printf("\t--walker : create a reader and walk though the resulting doc\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001625#endif /* LIBXML_READER_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001626#ifdef LIBXML_PATTERN_ENABLED
1627 printf("\t--pattern pattern_value : test the pattern support\n");
1628#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001629 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillardef4d3bc2003-02-07 12:38:22 +00001630#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard71531f32003-02-05 13:19:53 +00001631 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001632 printf("\t--schema schema : do validation against the WXS schema\n");
Daniel Veillard71531f32003-02-05 13:19:53 +00001633#endif
Daniel Veillarda42f25f2002-01-25 14:15:40 +00001634 printf("\nLibxml project home page: http://xmlsoft.org/\n");
1635 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001636}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001637
1638static void registerNode(xmlNodePtr node)
1639{
1640 node->_private = malloc(sizeof(long));
1641 *(long*)node->_private = (long) 0x81726354;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00001642 nbregister++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001643}
1644
1645static void deregisterNode(xmlNodePtr node)
1646{
1647 assert(node->_private != NULL);
1648 assert(*(long*)node->_private == (long) 0x81726354);
1649 free(node->_private);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00001650 nbregister--;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001651}
1652
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001653int
1654main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001655 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001656 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00001657 int version = 0;
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001658 const char* indent;
1659
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001660 if (argc <= 1) {
1661 usage(argv[0]);
1662 return(1);
1663 }
Daniel Veillardbe803962000-06-28 23:40:59 +00001664 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001665 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001666 if (!strcmp(argv[i], "-"))
1667 break;
1668
1669 if (argv[i][0] != '-')
1670 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001671 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
1672 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00001673 else
1674#ifdef LIBXML_DEBUG_ENABLED
1675 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001676 (!strcmp(argv[i], "--shell"))) {
1677 shell++;
1678 noout = 1;
1679 } else
1680#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001681#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001682 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
1683 copy++;
Daniel Veillard652327a2003-09-29 18:02:38 +00001684 else
1685#endif /* LIBXML_TREE_ENABLED */
1686 if ((!strcmp(argv[i], "-recover")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001687 (!strcmp(argv[i], "--recover"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001688 recovery++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001689 options |= XML_PARSE_RECOVER;
1690 } else if ((!strcmp(argv[i], "-noent")) ||
1691 (!strcmp(argv[i], "--noent"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001692 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001693 options |= XML_PARSE_NOENT;
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001694 } else if ((!strcmp(argv[i], "-nsclean")) ||
1695 (!strcmp(argv[i], "--nsclean"))) {
1696 options |= XML_PARSE_NSCLEAN;
1697 } else if ((!strcmp(argv[i], "-nocdata")) ||
1698 (!strcmp(argv[i], "--nocdata"))) {
1699 options |= XML_PARSE_NOCDATA;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001700 } else if ((!strcmp(argv[i], "-nodict")) ||
1701 (!strcmp(argv[i], "--nodict"))) {
1702 options |= XML_PARSE_NODICT;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001703 } else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00001704 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001705 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00001706 version = 1;
1707 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001708 (!strcmp(argv[i], "--noout")))
1709 noout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001710#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001711 else if ((!strcmp(argv[i], "-o")) ||
1712 (!strcmp(argv[i], "-output")) ||
1713 (!strcmp(argv[i], "--output"))) {
1714 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00001715 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001716 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001717#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001718 else if ((!strcmp(argv[i], "-htmlout")) ||
1719 (!strcmp(argv[i], "--htmlout")))
1720 htmlout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001721 else if ((!strcmp(argv[i], "-nowrap")) ||
1722 (!strcmp(argv[i], "--nowrap")))
1723 nowrap++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001724#ifdef LIBXML_HTML_ENABLED
1725 else if ((!strcmp(argv[i], "-html")) ||
1726 (!strcmp(argv[i], "--html"))) {
1727 html++;
1728 }
Daniel Veillard42fd4122003-11-04 08:47:48 +00001729 else if ((!strcmp(argv[i], "-xmlout")) ||
1730 (!strcmp(argv[i], "--xmlout"))) {
1731 xmlout++;
1732 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001733#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001734 else if ((!strcmp(argv[i], "-loaddtd")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001735 (!strcmp(argv[i], "--loaddtd"))) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001736 loaddtd++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001737 options |= XML_PARSE_DTDLOAD;
1738 } else if ((!strcmp(argv[i], "-dtdattr")) ||
Daniel Veillard48da9102001-08-07 01:10:10 +00001739 (!strcmp(argv[i], "--dtdattr"))) {
1740 loaddtd++;
1741 dtdattrs++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001742 options |= XML_PARSE_DTDATTR;
Daniel Veillard4432df22003-09-28 18:58:27 +00001743 }
1744#ifdef LIBXML_VALID_ENABLED
1745 else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001746 (!strcmp(argv[i], "--valid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001747 valid++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001748 options |= XML_PARSE_DTDVALID;
1749 } else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001750 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001751 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001752 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00001753 options |= XML_PARSE_DTDLOAD;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001754 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00001755 (!strcmp(argv[i], "--dtdvalid"))) {
1756 i++;
1757 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001758 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00001759 options |= XML_PARSE_DTDLOAD;
Daniel Veillard66f68e72003-08-18 16:39:51 +00001760 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
1761 (!strcmp(argv[i], "--dtdvalidfpi"))) {
1762 i++;
1763 dtdvalidfpi = argv[i];
1764 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00001765 options |= XML_PARSE_DTDLOAD;
Daniel Veillardcd429612000-10-11 15:57:05 +00001766 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001767#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard29e43992001-12-13 22:21:58 +00001768 else if ((!strcmp(argv[i], "-dropdtd")) ||
1769 (!strcmp(argv[i], "--dropdtd")))
1770 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001771 else if ((!strcmp(argv[i], "-insert")) ||
1772 (!strcmp(argv[i], "--insert")))
1773 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001774 else if ((!strcmp(argv[i], "-timing")) ||
1775 (!strcmp(argv[i], "--timing")))
1776 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001777 else if ((!strcmp(argv[i], "-auto")) ||
1778 (!strcmp(argv[i], "--auto")))
1779 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001780 else if ((!strcmp(argv[i], "-repeat")) ||
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001781 (!strcmp(argv[i], "--repeat"))) {
1782 if (repeat)
1783 repeat *= 10;
1784 else
1785 repeat = 100;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001786 }
1787#ifdef LIBXML_PUSH_ENABLED
1788 else if ((!strcmp(argv[i], "-push")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001789 (!strcmp(argv[i], "--push")))
1790 push++;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001791#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +00001792#ifdef HAVE_SYS_MMAN_H
1793 else if ((!strcmp(argv[i], "-memory")) ||
1794 (!strcmp(argv[i], "--memory")))
1795 memory++;
1796#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00001797 else if ((!strcmp(argv[i], "-testIO")) ||
1798 (!strcmp(argv[i], "--testIO")))
1799 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001800#ifdef LIBXML_XINCLUDE_ENABLED
1801 else if ((!strcmp(argv[i], "-xinclude")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001802 (!strcmp(argv[i], "--xinclude"))) {
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001803 xinclude++;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001804 options |= XML_PARSE_XINCLUDE;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001805 }
Daniel Veillardc14c3892004-08-16 12:34:50 +00001806 else if ((!strcmp(argv[i], "-noxincludenode")) ||
1807 (!strcmp(argv[i], "--noxincludenode"))) {
1808 xinclude++;
1809 options |= XML_PARSE_XINCLUDE;
1810 options |= XML_PARSE_NOXINCNODE;
1811 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001812#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001813#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001814#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001815 else if ((!strcmp(argv[i], "-compress")) ||
1816 (!strcmp(argv[i], "--compress"))) {
1817 compress++;
1818 xmlSetCompressMode(9);
1819 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001820#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001821#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001822 else if ((!strcmp(argv[i], "-nowarning")) ||
1823 (!strcmp(argv[i], "--nowarning"))) {
1824 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001825 xmlPedanticParserDefault(0);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001826 options |= XML_PARSE_NOWARNING;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001827 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001828 else if ((!strcmp(argv[i], "-pedantic")) ||
1829 (!strcmp(argv[i], "--pedantic"))) {
1830 xmlGetWarningsDefaultValue = 1;
1831 xmlPedanticParserDefault(1);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001832 options |= XML_PARSE_PEDANTIC;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001833 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001834#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001835 else if ((!strcmp(argv[i], "-debugent")) ||
1836 (!strcmp(argv[i], "--debugent"))) {
1837 debugent++;
1838 xmlParserDebugEntities = 1;
1839 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001840#endif
Daniel Veillard25048d82004-08-14 22:37:54 +00001841#ifdef LIBXML_C14N_ENABLED
1842 else if ((!strcmp(argv[i], "-c14n")) ||
1843 (!strcmp(argv[i], "--c14n"))) {
1844 canonical++;
1845 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
1846 }
1847#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00001848#ifdef LIBXML_CATALOG_ENABLED
1849 else if ((!strcmp(argv[i], "-catalogs")) ||
1850 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001851 catalogs++;
1852 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
1853 (!strcmp(argv[i], "--nocatalogs"))) {
1854 nocatalogs++;
Daniel Veillard81418e32001-05-22 15:08:55 +00001855 }
1856#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00001857 else if ((!strcmp(argv[i], "-encode")) ||
1858 (!strcmp(argv[i], "--encode"))) {
1859 i++;
1860 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001861 /*
1862 * OK it's for testing purposes
1863 */
1864 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00001865 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001866 else if ((!strcmp(argv[i], "-noblanks")) ||
1867 (!strcmp(argv[i], "--noblanks"))) {
1868 noblanks++;
1869 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00001870 }
Daniel Veillard87076042004-05-03 22:54:49 +00001871 else if ((!strcmp(argv[i], "-maxmem")) ||
1872 (!strcmp(argv[i], "--maxmem"))) {
1873 i++;
1874 if (sscanf(argv[i], "%d", &maxmem) == 1) {
1875 xmlMemSetup(myFreeFunc, myMallocFunc, myReallocFunc,
1876 myStrdupFunc);
1877 } else {
1878 maxmem = 0;
1879 }
1880 }
Daniel Veillard90493a92001-08-14 14:12:47 +00001881 else if ((!strcmp(argv[i], "-format")) ||
1882 (!strcmp(argv[i], "--format"))) {
1883 noblanks++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001884#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001885 format++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001886#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard90493a92001-08-14 14:12:47 +00001887 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001888 }
Daniel Veillard81273902003-09-30 00:43:48 +00001889#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001890 else if ((!strcmp(argv[i], "-stream")) ||
1891 (!strcmp(argv[i], "--stream"))) {
1892 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001893 }
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001894 else if ((!strcmp(argv[i], "-walker")) ||
1895 (!strcmp(argv[i], "--walker"))) {
1896 walker++;
1897 noout++;
1898 }
Daniel Veillard81273902003-09-30 00:43:48 +00001899#endif /* LIBXML_READER_ENABLED */
1900#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001901 else if ((!strcmp(argv[i], "-sax1")) ||
1902 (!strcmp(argv[i], "--sax1"))) {
1903 sax1++;
1904 }
Daniel Veillard81273902003-09-30 00:43:48 +00001905#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001906 else if ((!strcmp(argv[i], "-chkregister")) ||
1907 (!strcmp(argv[i], "--chkregister"))) {
1908 chkregister++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001909#ifdef LIBXML_SCHEMAS_ENABLED
1910 } else if ((!strcmp(argv[i], "-relaxng")) ||
1911 (!strcmp(argv[i], "--relaxng"))) {
1912 i++;
1913 relaxng = argv[i];
1914 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001915 options |= XML_PARSE_NOENT;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001916 } else if ((!strcmp(argv[i], "-schema")) ||
1917 (!strcmp(argv[i], "--schema"))) {
1918 i++;
1919 schema = argv[i];
1920 noent++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001921#endif
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001922 } else if ((!strcmp(argv[i], "-nonet")) ||
1923 (!strcmp(argv[i], "--nonet"))) {
Daniel Veillard61b93382003-11-03 14:28:31 +00001924 options |= XML_PARSE_NONET;
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001925#ifdef LIBXML_PATTERN_ENABLED
1926 } else if ((!strcmp(argv[i], "-pattern")) ||
1927 (!strcmp(argv[i], "--pattern"))) {
1928 i++;
1929 pattern = argv[i];
1930#endif
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001931 } else {
1932 fprintf(stderr, "Unknown option %s\n", argv[i]);
1933 usage(argv[0]);
1934 return(1);
1935 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001936 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001937
1938#ifdef LIBXML_CATALOG_ENABLED
1939 if (nocatalogs == 0) {
1940 if (catalogs) {
1941 const char *catal;
1942
1943 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00001944 if (catal != NULL) {
1945 xmlLoadCatalogs(catal);
1946 } else {
1947 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
1948 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001949 }
1950 }
1951#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001952
Daniel Veillard81273902003-09-30 00:43:48 +00001953#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001954 if (sax1)
1955 xmlSAXDefaultVersion(1);
1956 else
1957 xmlSAXDefaultVersion(2);
Daniel Veillard81273902003-09-30 00:43:48 +00001958#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00001959
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001960 if (chkregister) {
1961 xmlRegisterNodeDefault(registerNode);
1962 xmlDeregisterNodeDefault(deregisterNode);
1963 }
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001964
1965 indent = getenv("XMLLINT_INDENT");
1966 if(indent != NULL) {
1967 xmlTreeIndentString = indent;
1968 }
1969
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001970
Daniel Veillardd9bad132001-07-23 19:39:43 +00001971 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00001972 if (loaddtd != 0)
1973 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
1974 if (dtdattrs)
1975 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001976 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillard4432df22003-09-28 18:58:27 +00001977#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001978 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard4432df22003-09-28 18:58:27 +00001979#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001980 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001981 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001982 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001983 xmlGenericError(xmlGenericErrorContext,
1984 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
1985 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001986 "<html><head><title>%s output</title></head>\n",
1987 argv[0]);
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001988 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001989 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
1990 argv[0]);
1991 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001992
1993#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard81273902003-09-30 00:43:48 +00001994 if ((relaxng != NULL)
1995#ifdef LIBXML_READER_ENABLED
1996 && (stream == 0)
1997#endif /* LIBXML_READER_ENABLED */
1998 ) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001999 xmlRelaxNGParserCtxtPtr ctxt;
2000
Daniel Veillardce192eb2003-04-16 15:58:05 +00002001 /* forces loading the DTDs */
2002 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002003 options |= XML_PARSE_DTDLOAD;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002004 if (timing) {
2005 startTimer();
2006 }
Daniel Veillard71531f32003-02-05 13:19:53 +00002007 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
2008 xmlRelaxNGSetParserErrors(ctxt,
2009 (xmlRelaxNGValidityErrorFunc) fprintf,
2010 (xmlRelaxNGValidityWarningFunc) fprintf,
2011 stderr);
2012 relaxngschemas = xmlRelaxNGParse(ctxt);
Daniel Veillardce192eb2003-04-16 15:58:05 +00002013 if (relaxngschemas == NULL) {
2014 xmlGenericError(xmlGenericErrorContext,
2015 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +00002016 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +00002017 relaxng = NULL;
2018 }
Daniel Veillard71531f32003-02-05 13:19:53 +00002019 xmlRelaxNGFreeParserCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002020 if (timing) {
2021 endTimer("Compiling the schemas");
2022 }
Daniel Veillardebe25d42004-03-25 09:35:49 +00002023 } else if ((schema != NULL)
2024#ifdef LIBXML_READER_ENABLED
2025 && (stream == 0)
2026#endif
2027 ) {
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002028 xmlSchemaParserCtxtPtr ctxt;
2029
2030 if (timing) {
2031 startTimer();
2032 }
2033 ctxt = xmlSchemaNewParserCtxt(schema);
2034 xmlSchemaSetParserErrors(ctxt,
2035 (xmlSchemaValidityErrorFunc) fprintf,
2036 (xmlSchemaValidityWarningFunc) fprintf,
2037 stderr);
2038 wxschemas = xmlSchemaParse(ctxt);
2039 if (wxschemas == NULL) {
2040 xmlGenericError(xmlGenericErrorContext,
2041 "WXS schema %s failed to compile\n", schema);
William M. Brack8304d872004-06-08 13:29:32 +00002042 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002043 schema = NULL;
2044 }
2045 xmlSchemaFreeParserCtxt(ctxt);
2046 if (timing) {
2047 endTimer("Compiling the schemas");
2048 }
Daniel Veillard71531f32003-02-05 13:19:53 +00002049 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002050#endif /* LIBXML_SCHEMAS_ENABLED */
2051#ifdef LIBXML_PATTERN_ENABLED
2052 if (pattern != NULL) {
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00002053 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002054 if (patternc == NULL) {
2055 xmlGenericError(xmlGenericErrorContext,
2056 "Pattern %s failed to compile\n", pattern);
William M. Brack8304d872004-06-08 13:29:32 +00002057 progresult = XMLLINT_ERR_SCHEMAPAT;
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002058 pattern = NULL;
2059 }
2060 }
2061#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002062 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00002063 if ((!strcmp(argv[i], "-encode")) ||
2064 (!strcmp(argv[i], "--encode"))) {
2065 i++;
2066 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002067 } else if ((!strcmp(argv[i], "-o")) ||
2068 (!strcmp(argv[i], "-output")) ||
2069 (!strcmp(argv[i], "--output"))) {
2070 i++;
2071 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00002072 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002073#ifdef LIBXML_VALID_ENABLED
Daniel Veillardcd429612000-10-11 15:57:05 +00002074 if ((!strcmp(argv[i], "-dtdvalid")) ||
2075 (!strcmp(argv[i], "--dtdvalid"))) {
2076 i++;
2077 continue;
2078 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00002079 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
2080 (!strcmp(argv[i], "--dtdvalidfpi"))) {
2081 i++;
2082 continue;
2083 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002084#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00002085 if ((!strcmp(argv[i], "-relaxng")) ||
2086 (!strcmp(argv[i], "--relaxng"))) {
2087 i++;
2088 continue;
2089 }
Daniel Veillard87076042004-05-03 22:54:49 +00002090 if ((!strcmp(argv[i], "-maxmem")) ||
2091 (!strcmp(argv[i], "--maxmem"))) {
2092 i++;
2093 continue;
2094 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002095 if ((!strcmp(argv[i], "-schema")) ||
2096 (!strcmp(argv[i], "--schema"))) {
2097 i++;
2098 continue;
2099 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002100#ifdef LIBXML_PATTERN_ENABLED
2101 if ((!strcmp(argv[i], "-pattern")) ||
2102 (!strcmp(argv[i], "--pattern"))) {
2103 i++;
2104 continue;
2105 }
2106#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00002107 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00002108 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002109 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002110 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002111 if (repeat) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002112 xmlParserCtxtPtr ctxt = NULL;
2113
2114 for (acount = 0;acount < repeat;acount++) {
Daniel Veillard81273902003-09-30 00:43:48 +00002115#ifdef LIBXML_READER_ENABLED
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002116 if (stream != 0) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00002117 streamFile(argv[i]);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002118 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00002119#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002120 if (ctxt == NULL)
2121 ctxt = xmlNewParserCtxt();
2122 parseAndPrintFile(argv[i], ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00002123#ifdef LIBXML_READER_ENABLED
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002124 }
Daniel Veillard81273902003-09-30 00:43:48 +00002125#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002126 }
2127 if (ctxt != NULL)
2128 xmlFreeParserCtxt(ctxt);
Daniel Veillard7704fb12003-01-03 16:19:51 +00002129 } else {
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002130 nbregister = 0;
2131
Daniel Veillard81273902003-09-30 00:43:48 +00002132#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00002133 if (stream != 0)
2134 streamFile(argv[i]);
2135 else
Daniel Veillard81273902003-09-30 00:43:48 +00002136#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002137 parseAndPrintFile(argv[i], NULL);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002138
2139 if ((chkregister) && (nbregister != 0)) {
2140 fprintf(stderr, "Registration count off: %d\n", nbregister);
William M. Brack8304d872004-06-08 13:29:32 +00002141 progresult = XMLLINT_ERR_RDREGIS;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002142 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00002143 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002144 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00002145 if ((timing) && (repeat)) {
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00002146 endTimer("%d iterations", repeat);
Daniel Veillarda7866932001-12-04 13:14:44 +00002147 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002148 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002149 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002150 if (generate)
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002151 parseAndPrintFile(NULL, NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002152 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00002153 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002154 }
Daniel Veillard845cce42002-01-09 11:51:37 +00002155 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002156 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002157 }
Daniel Veillard71531f32003-02-05 13:19:53 +00002158#ifdef LIBXML_SCHEMAS_ENABLED
2159 if (relaxngschemas != NULL)
2160 xmlRelaxNGFree(relaxngschemas);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002161 if (wxschemas != NULL)
2162 xmlSchemaFree(wxschemas);
Daniel Veillard71531f32003-02-05 13:19:53 +00002163 xmlRelaxNGCleanupTypes();
2164#endif
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002165#ifdef LIBXML_PATTERN_ENABLED
2166 if (patternc != NULL)
2167 xmlFreePattern(patternc);
2168#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002169 xmlCleanupParser();
2170 xmlMemoryDump();
2171
Daniel Veillardf7cd4812001-02-23 18:44:52 +00002172 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002173}
Daniel Veillard88a172f2000-08-04 18:23:10 +00002174