blob: 6225df9d8022870ee9020938612878d30162e607 [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 Veillardce8b83b2000-04-05 18:38:42 +000095
Daniel Veillard3be27512003-01-26 19:49:04 +000096#ifndef XML_XML_DEFAULT_CATALOG
97#define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog"
98#endif
99
William M. Brack8304d872004-06-08 13:29:32 +0000100typedef enum {
101 XMLLINT_RETURN_OK = 0, /* No error */
102 XMLLINT_ERR_UNCLASS, /* Unclassified */
103 XMLLINT_ERR_DTD, /* Error in DTD */
104 XMLLINT_ERR_VALID, /* Validation error */
105 XMLLINT_ERR_RDFILE, /* CtxtReadFile error */
106 XMLLINT_ERR_SCHEMACOMP, /* Schema compilation */
107 XMLLINT_ERR_OUT, /* Error writing output */
108 XMLLINT_ERR_SCHEMAPAT, /* Error in schema pattern */
109 XMLLINT_ERR_RDREGIS, /* Error in Reader registration */
110 XMLLINT_ERR_MEM /* Out of memory error */
111} xmllintReturnCode;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000112#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000113static int shell = 0;
114static int debugent = 0;
115#endif
Daniel Veillard8326e732003-01-07 00:19:07 +0000116static int debug = 0;
Daniel Veillard87076042004-05-03 22:54:49 +0000117static int maxmem = 0;
Daniel Veillard652327a2003-09-29 18:02:38 +0000118#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000119static int copy = 0;
Daniel Veillard652327a2003-09-29 18:02:38 +0000120#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000121static int recovery = 0;
122static int noent = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000123static int noblanks = 0;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000124static int noout = 0;
125static int nowrap = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000126#ifdef LIBXML_OUTPUT_ENABLED
127static int format = 0;
128static const char *output = NULL;
129static int compress = 0;
130#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4432df22003-09-28 18:58:27 +0000131#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000132static int valid = 0;
133static int postvalid = 0;
Daniel Veillardcd429612000-10-11 15:57:05 +0000134static char * dtdvalid = NULL;
Daniel Veillard66f68e72003-08-18 16:39:51 +0000135static char * dtdvalidfpi = NULL;
Daniel Veillard4432df22003-09-28 18:58:27 +0000136#endif
Daniel Veillard71531f32003-02-05 13:19:53 +0000137#ifdef LIBXML_SCHEMAS_ENABLED
138static char * relaxng = NULL;
139static xmlRelaxNGPtr relaxngschemas = NULL;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +0000140static char * schema = NULL;
141static xmlSchemaPtr wxschemas = NULL;
Daniel Veillard71531f32003-02-05 13:19:53 +0000142#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000143static int repeat = 0;
144static int insert = 0;
Daniel Veillard656ce942004-04-30 23:11:45 +0000145#if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000146static int html = 0;
Daniel Veillard42fd4122003-11-04 08:47:48 +0000147static int xmlout = 0;
Daniel Veillard4432df22003-09-28 18:58:27 +0000148#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000149static int htmlout = 0;
Daniel Veillard73b013f2003-09-30 12:36:01 +0000150#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000151static int push = 0;
Daniel Veillard73b013f2003-09-30 12:36:01 +0000152#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +0000153#ifdef HAVE_SYS_MMAN_H
154static int memory = 0;
155#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +0000156static int testIO = 0;
Daniel Veillardbe803962000-06-28 23:40:59 +0000157static char *encoding = NULL;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000158#ifdef LIBXML_XINCLUDE_ENABLED
159static int xinclude = 0;
160#endif
Daniel Veillard48da9102001-08-07 01:10:10 +0000161static int dtdattrs = 0;
Daniel Veillard10ea86c2001-06-20 13:55:33 +0000162static int loaddtd = 0;
William M. Brack8304d872004-06-08 13:29:32 +0000163static xmllintReturnCode progresult = XMLLINT_RETURN_OK;
Daniel Veillard48b2f892001-02-25 16:11:03 +0000164static int timing = 0;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000165static int generate = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000166static int dropdtd = 0;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000167#ifdef LIBXML_CATALOG_ENABLED
168static int catalogs = 0;
169static int nocatalogs = 0;
170#endif
Daniel Veillard25048d82004-08-14 22:37:54 +0000171#ifdef LIBXML_C14N_ENABLED
172static int canonical = 0;
173#endif
Daniel Veillard81273902003-09-30 00:43:48 +0000174#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000175static int stream = 0;
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000176static int walker = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000177#endif /* LIBXML_READER_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000178static int chkregister = 0;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +0000179static int nbregister = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000180#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +0000181static int sax1 = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000182#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000183#ifdef LIBXML_PATTERN_ENABLED
184static const char *pattern = NULL;
185static xmlPatternPtr patternc = NULL;
186#endif
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000187static int options = 0;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000188
Daniel Veillard87076042004-05-03 22:54:49 +0000189/************************************************************************
190 * *
191 * Memory allocation consumption debugging *
192 * *
193 ************************************************************************/
194
Daniel Veillard3af3b592004-05-05 19:22:30 +0000195static void
196OOM(void)
197{
Daniel Veillard87076042004-05-03 22:54:49 +0000198 fprintf(stderr, "Ran out of memory needs > %d bytes\n", maxmem);
William M. Brack8304d872004-06-08 13:29:32 +0000199 progresult = XMLLINT_ERR_MEM;
Daniel Veillard87076042004-05-03 22:54:49 +0000200}
201
Daniel Veillard3af3b592004-05-05 19:22:30 +0000202static void
203myFreeFunc(void *mem)
204{
Daniel Veillard87076042004-05-03 22:54:49 +0000205 xmlMemFree(mem);
206}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000207static void *
208myMallocFunc(size_t size)
209{
Daniel Veillard87076042004-05-03 22:54:49 +0000210 void *ret;
211
212 ret = xmlMemMalloc(size);
213 if (ret != NULL) {
214 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000215 OOM();
216 xmlMemFree(ret);
217 return (NULL);
218 }
Daniel Veillard87076042004-05-03 22:54:49 +0000219 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000220 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000221}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000222static void *
223myReallocFunc(void *mem, size_t size)
224{
Daniel Veillard87076042004-05-03 22:54:49 +0000225 void *ret;
226
227 ret = xmlMemRealloc(mem, size);
228 if (ret != NULL) {
229 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000230 OOM();
231 xmlMemFree(ret);
232 return (NULL);
233 }
Daniel Veillard87076042004-05-03 22:54:49 +0000234 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000235 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000236}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000237static char *
238myStrdupFunc(const char *str)
239{
Daniel Veillard87076042004-05-03 22:54:49 +0000240 char *ret;
241
242 ret = xmlMemoryStrdup(str);
243 if (ret != NULL) {
244 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000245 OOM();
246 xmlFree(ret);
247 return (NULL);
248 }
Daniel Veillard87076042004-05-03 22:54:49 +0000249 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000250 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000251}
Daniel Veillard87076042004-05-03 22:54:49 +0000252/************************************************************************
253 * *
254 * Internal timing routines to remove the necessity to have *
255 * unix-specific function calls. *
256 * *
257 ************************************************************************/
Daniel Veillard01db67c2001-12-18 07:09:59 +0000258
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000259#ifndef HAVE_GETTIMEOFDAY
260#ifdef HAVE_SYS_TIMEB_H
261#ifdef HAVE_SYS_TIME_H
262#ifdef HAVE_FTIME
263
Daniel Veillard01c13b52002-12-10 15:19:08 +0000264static int
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000265my_gettimeofday(struct timeval *tvp, void *tzp)
266{
267 struct timeb timebuffer;
268
269 ftime(&timebuffer);
270 if (tvp) {
271 tvp->tv_sec = timebuffer.time;
272 tvp->tv_usec = timebuffer.millitm * 1000L;
273 }
274 return (0);
275}
276#define HAVE_GETTIMEOFDAY 1
277#define gettimeofday my_gettimeofday
278
279#endif /* HAVE_FTIME */
280#endif /* HAVE_SYS_TIME_H */
281#endif /* HAVE_SYS_TIMEB_H */
282#endif /* !HAVE_GETTIMEOFDAY */
283
Daniel Veillard01db67c2001-12-18 07:09:59 +0000284#if defined(HAVE_GETTIMEOFDAY)
285static struct timeval begin, end;
286
287/*
288 * startTimer: call where you want to start timing
289 */
290static void
291startTimer(void)
292{
293 gettimeofday(&begin, NULL);
294}
295
296/*
297 * endTimer: call where you want to stop timing and to print out a
298 * message about the timing performed; format is a printf
299 * type argument
300 */
301static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000302endTimer(const char *fmt, ...)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000303{
304 long msec;
305 va_list ap;
306
307 gettimeofday(&end, NULL);
308 msec = end.tv_sec - begin.tv_sec;
309 msec *= 1000;
310 msec += (end.tv_usec - begin.tv_usec) / 1000;
311
312#ifndef HAVE_STDARG_H
313#error "endTimer required stdarg functions"
314#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000315 va_start(ap, fmt);
316 vfprintf(stderr, fmt, ap);
Daniel Veillard01db67c2001-12-18 07:09:59 +0000317 va_end(ap);
318
319 fprintf(stderr, " took %ld ms\n", msec);
320}
321#elif defined(HAVE_TIME_H)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000322/*
323 * No gettimeofday function, so we have to make do with calling clock.
324 * This is obviously less accurate, but there's little we can do about
325 * that.
326 */
Daniel Veillard90bc3712002-03-07 15:12:58 +0000327#ifndef CLOCKS_PER_SEC
328#define CLOCKS_PER_SEC 100
329#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +0000330
331static clock_t begin, end;
332static void
333startTimer(void)
334{
335 begin = clock();
336}
337static void
338endTimer(const char *fmt, ...)
339{
340 long msec;
341 va_list ap;
342
343 end = clock();
344 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
345
346#ifndef HAVE_STDARG_H
347#error "endTimer required stdarg functions"
348#endif
349 va_start(ap, fmt);
350 vfprintf(stderr, fmt, ap);
351 va_end(ap);
352 fprintf(stderr, " took %ld ms\n", msec);
353}
354#else
355
356/*
357 * We don't have a gettimeofday or time.h, so we just don't do timing
358 */
359static void
360startTimer(void)
361{
362 /*
363 * Do nothing
364 */
365}
366static void
367endTimer(char *format, ...)
368{
369 /*
370 * We cannot do anything because we don't have a timing function
371 */
372#ifdef HAVE_STDARG_H
373 va_start(ap, format);
374 vfprintf(stderr, format, ap);
375 va_end(ap);
376 fprintf(stderr, " was not timed\n", msec);
377#else
378 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
379 * this ?!
380 */
381#endif
382}
383#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000384/************************************************************************
385 * *
386 * HTML ouput *
387 * *
388 ************************************************************************/
389char buffer[50000];
390
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000391static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000392xmlHTMLEncodeSend(void) {
393 char *result;
394
395 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
396 if (result) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000397 xmlGenericError(xmlGenericErrorContext, "%s", result);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000398 xmlFree(result);
399 }
400 buffer[0] = 0;
401}
402
403/**
404 * xmlHTMLPrintFileInfo:
405 * @input: an xmlParserInputPtr input
406 *
407 * Displays the associated file and line informations for the current input
408 */
409
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000410static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000411xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000412 int len;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000413 xmlGenericError(xmlGenericErrorContext, "<p>");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000414
415 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000416 if (input != NULL) {
417 if (input->filename) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000418 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000419 input->line);
420 } else {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000421 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000422 }
423 }
424 xmlHTMLEncodeSend();
425}
426
427/**
428 * xmlHTMLPrintFileContext:
429 * @input: an xmlParserInputPtr input
430 *
431 * Displays current context within the input content for error tracking
432 */
433
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000434static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000435xmlHTMLPrintFileContext(xmlParserInputPtr input) {
436 const xmlChar *cur, *base;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000437 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000438 int n;
439
440 if (input == NULL) return;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000441 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000442 cur = input->cur;
443 base = input->base;
444 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
445 cur--;
446 }
447 n = 0;
448 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
449 cur--;
450 if ((*cur == '\n') || (*cur == '\r')) cur++;
451 base = cur;
452 n = 0;
453 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000454 len = strlen(buffer);
455 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
456 (unsigned char) *cur++);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000457 n++;
458 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000459 len = strlen(buffer);
460 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000461 cur = input->cur;
462 while ((*cur == '\n') || (*cur == '\r'))
463 cur--;
464 n = 0;
465 while ((cur != base) && (n++ < 80)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000466 len = strlen(buffer);
467 snprintf(&buffer[len], sizeof(buffer) - len, " ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000468 base++;
469 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000470 len = strlen(buffer);
471 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000472 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000473 xmlGenericError(xmlGenericErrorContext, "</pre>");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000474}
475
476/**
477 * xmlHTMLError:
478 * @ctx: an XML parser context
479 * @msg: the message to display/transmit
480 * @...: extra parameters for the message display
481 *
482 * Display and format an error messages, gives file, line, position and
483 * extra parameters.
484 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000485static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000486xmlHTMLError(void *ctx, const char *msg, ...)
487{
488 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
489 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000490 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000491 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000492
493 buffer[0] = 0;
494 input = ctxt->input;
495 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000496 input = ctxt->inputTab[ctxt->inputNr - 2];
497 }
498
499 xmlHTMLPrintFileInfo(input);
500
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000501 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000502 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000503 len = strlen(buffer);
504 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000505 va_end(args);
506 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000507 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000508
509 xmlHTMLPrintFileContext(input);
510 xmlHTMLEncodeSend();
511}
512
513/**
514 * xmlHTMLWarning:
515 * @ctx: an XML parser context
516 * @msg: the message to display/transmit
517 * @...: extra parameters for the message display
518 *
519 * Display and format a warning messages, gives file, line, position and
520 * extra parameters.
521 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000522static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000523xmlHTMLWarning(void *ctx, const char *msg, ...)
524{
525 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
526 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000527 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000528 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000529
530 buffer[0] = 0;
531 input = ctxt->input;
532 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000533 input = ctxt->inputTab[ctxt->inputNr - 2];
534 }
535
536
537 xmlHTMLPrintFileInfo(input);
538
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000539 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000540 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000541 len = strlen(buffer);
542 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000543 va_end(args);
544 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000545 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000546
547 xmlHTMLPrintFileContext(input);
548 xmlHTMLEncodeSend();
549}
550
551/**
552 * xmlHTMLValidityError:
553 * @ctx: an XML parser context
554 * @msg: the message to display/transmit
555 * @...: extra parameters for the message display
556 *
557 * Display and format an validity error messages, gives file,
558 * line, position and extra parameters.
559 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000560static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000561xmlHTMLValidityError(void *ctx, const char *msg, ...)
562{
563 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
564 xmlParserInputPtr input;
565 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000566 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000567
568 buffer[0] = 0;
569 input = ctxt->input;
570 if ((input->filename == NULL) && (ctxt->inputNr > 1))
571 input = ctxt->inputTab[ctxt->inputNr - 2];
572
573 xmlHTMLPrintFileInfo(input);
574
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000575 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000576 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000577 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000578 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000579 va_end(args);
580 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000581 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000582
583 xmlHTMLPrintFileContext(input);
584 xmlHTMLEncodeSend();
585}
586
587/**
588 * xmlHTMLValidityWarning:
589 * @ctx: an XML parser context
590 * @msg: the message to display/transmit
591 * @...: extra parameters for the message display
592 *
593 * Display and format a validity warning messages, gives file, line,
594 * position and extra parameters.
595 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000596static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000597xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
598{
599 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
600 xmlParserInputPtr input;
601 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000602 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000603
604 buffer[0] = 0;
605 input = ctxt->input;
606 if ((input->filename == NULL) && (ctxt->inputNr > 1))
607 input = ctxt->inputTab[ctxt->inputNr - 2];
608
609 xmlHTMLPrintFileInfo(input);
610
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000611 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000612 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000613 len = strlen(buffer);
614 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000615 va_end(args);
616 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000617 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000618
619 xmlHTMLPrintFileContext(input);
620 xmlHTMLEncodeSend();
621}
622
623/************************************************************************
624 * *
625 * Shell Interface *
626 * *
627 ************************************************************************/
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000628#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000629/**
630 * xmlShellReadline:
631 * @prompt: the prompt value
632 *
633 * Read a string
634 *
635 * Returns a pointer to it or NULL on EOF the caller is expected to
636 * free the returned string.
637 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000638static char *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000639xmlShellReadline(char *prompt) {
640#ifdef HAVE_LIBREADLINE
641 char *line_read;
642
643 /* Get a line from the user. */
644 line_read = readline (prompt);
645
646 /* If the line has any text in it, save it on the history. */
647 if (line_read && *line_read)
648 add_history (line_read);
649
650 return (line_read);
651#else
652 char line_read[501];
Daniel Veillard29e43992001-12-13 22:21:58 +0000653 char *ret;
654 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000655
656 if (prompt != NULL)
657 fprintf(stdout, "%s", prompt);
658 if (!fgets(line_read, 500, stdin))
659 return(NULL);
660 line_read[500] = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000661 len = strlen(line_read);
662 ret = (char *) malloc(len + 1);
663 if (ret != NULL) {
664 memcpy (ret, line_read, len + 1);
665 }
666 return(ret);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000667#endif
668}
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000669#endif /* LIBXML_DEBUG_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000670
671/************************************************************************
672 * *
Daniel Veillard5e873c42000-04-12 13:27:38 +0000673 * I/O Interfaces *
674 * *
675 ************************************************************************/
676
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000677static int myRead(FILE *f, char * buf, int len) {
678 return(fread(buf, 1, len, f));
Daniel Veillard5e873c42000-04-12 13:27:38 +0000679}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000680static void myClose(FILE *f) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000681 if (f != stdin) {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000682 fclose(f);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000683 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000684}
685
Daniel Veillard81273902003-09-30 00:43:48 +0000686#ifdef LIBXML_READER_ENABLED
Daniel Veillard5e873c42000-04-12 13:27:38 +0000687/************************************************************************
688 * *
Daniel Veillard7704fb12003-01-03 16:19:51 +0000689 * Stream Test processing *
690 * *
691 ************************************************************************/
Daniel Veillard7704fb12003-01-03 16:19:51 +0000692static void processNode(xmlTextReaderPtr reader) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000693 const xmlChar *name, *value;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000694
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000695 name = xmlTextReaderConstName(reader);
Daniel Veillarde59494f2003-01-04 16:35:29 +0000696 if (name == NULL)
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000697 name = BAD_CAST "--";
Daniel Veillard99737f52003-03-22 14:55:50 +0000698
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000699 value = xmlTextReaderConstValue(reader);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000700
Daniel Veillard99737f52003-03-22 14:55:50 +0000701 printf("%d %d %s %d %d",
Daniel Veillarde59494f2003-01-04 16:35:29 +0000702 xmlTextReaderDepth(reader),
703 xmlTextReaderNodeType(reader),
704 name,
Daniel Veillard99737f52003-03-22 14:55:50 +0000705 xmlTextReaderIsEmptyElement(reader),
706 xmlTextReaderHasValue(reader));
Daniel Veillarde59494f2003-01-04 16:35:29 +0000707 if (value == NULL)
708 printf("\n");
709 else {
710 printf(" %s\n", value);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000711 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000712#ifdef LIBXML_PATTERN_ENABLED
713 if (patternc) {
714 if (xmlPatternMatch(patternc, xmlTextReaderCurrentNode(reader)) == 1) {
715 printf("Node matches pattern %s\n", pattern);
716 }
717 }
718#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000719}
720
721static void streamFile(char *filename) {
722 xmlTextReaderPtr reader;
723 int ret;
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000724#ifdef HAVE_SYS_MMAN_H
725 int fd = -1;
726 struct stat info;
727 const char *base = NULL;
728 xmlParserInputBufferPtr input = NULL;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000729
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000730 if (memory) {
731 if (stat(filename, &info) < 0)
732 return;
733 if ((fd = open(filename, O_RDONLY)) < 0)
734 return;
735 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
736 if (base == (void *) MAP_FAILED)
737 return;
738
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000739 reader = xmlReaderForMemory(base, info.st_size, filename,
740 NULL, options);
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000741 } else
742#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000743 reader = xmlReaderForFile(filename, NULL, options);
744
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000745
Daniel Veillard7704fb12003-01-03 16:19:51 +0000746 if (reader != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +0000747#ifdef LIBXML_VALID_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000748 if (valid)
749 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +0000750 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000751#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce192eb2003-04-16 15:58:05 +0000752 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000753#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +0000754 if (relaxng != NULL) {
Daniel Veillard81514ba2003-09-16 23:17:26 +0000755 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000756 startTimer();
757 }
758 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
759 if (ret < 0) {
760 xmlGenericError(xmlGenericErrorContext,
761 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +0000762 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +0000763 relaxng = NULL;
764 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000765 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000766 endTimer("Compiling the schemas");
767 }
768 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000769#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000770
771 /*
772 * Process all nodes in sequence
773 */
Daniel Veillard81514ba2003-09-16 23:17:26 +0000774 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000775 startTimer();
776 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000777 ret = xmlTextReaderRead(reader);
778 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000779 if ((debug)
780#ifdef LIBXML_PATTERN_ENABLED
781 || (patternc)
782#endif
783 )
Daniel Veillard7704fb12003-01-03 16:19:51 +0000784 processNode(reader);
785 ret = xmlTextReaderRead(reader);
786 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000787 if ((timing) && (!repeat)) {
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000788#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf54cd532004-02-25 11:52:31 +0000789 if (relaxng != NULL)
Daniel Veillard49138f12004-02-19 12:58:36 +0000790 endTimer("Parsing and validating");
791 else
Daniel Veillardf54cd532004-02-25 11:52:31 +0000792#endif
Daniel Veillard4432df22003-09-28 18:58:27 +0000793#ifdef LIBXML_VALID_ENABLED
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000794 if (valid)
Daniel Veillardce192eb2003-04-16 15:58:05 +0000795 endTimer("Parsing and validating");
796 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000797#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +0000798 endTimer("Parsing");
Daniel Veillardce192eb2003-04-16 15:58:05 +0000799 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000800
Daniel Veillard4432df22003-09-28 18:58:27 +0000801#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf6bad792003-04-11 19:38:54 +0000802 if (valid) {
803 if (xmlTextReaderIsValid(reader) != 1) {
804 xmlGenericError(xmlGenericErrorContext,
805 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +0000806 progresult = XMLLINT_ERR_VALID;
Daniel Veillardf6bad792003-04-11 19:38:54 +0000807 }
808 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000809#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000810#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +0000811 if (relaxng != NULL) {
812 if (xmlTextReaderIsValid(reader) != 1) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +0000813 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +0000814 progresult = XMLLINT_ERR_VALID;
Daniel Veillardf4e55762003-04-15 23:32:22 +0000815 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +0000816 fprintf(stderr, "%s validates\n", filename);
Daniel Veillardf4e55762003-04-15 23:32:22 +0000817 }
818 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000819#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000820 /*
821 * Done, cleanup and status
822 */
823 xmlFreeTextReader(reader);
824 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +0000825 fprintf(stderr, "%s : failed to parse\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +0000826 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000827 }
828 } else {
829 fprintf(stderr, "Unable to open %s\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +0000830 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000831 }
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000832#ifdef HAVE_SYS_MMAN_H
833 if (memory) {
834 xmlFreeParserInputBuffer(input);
835 munmap((char *) base, info.st_size);
836 close(fd);
837 }
838#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000839}
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000840
841static void walkDoc(xmlDocPtr doc) {
842 xmlTextReaderPtr reader;
843 int ret;
844
845 reader = xmlReaderWalker(doc);
846 if (reader != NULL) {
847 if ((timing) && (!repeat)) {
848 startTimer();
849 }
850 ret = xmlTextReaderRead(reader);
851 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000852 if ((debug)
853#ifdef LIBXML_PATTERN_ENABLED
854 || (patternc)
855#endif
856 )
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000857 processNode(reader);
858 ret = xmlTextReaderRead(reader);
859 }
860 if ((timing) && (!repeat)) {
861 endTimer("walking through the doc");
862 }
863 xmlFreeTextReader(reader);
864 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +0000865 fprintf(stderr, "failed to walk through the doc\n");
William M. Brack8304d872004-06-08 13:29:32 +0000866 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000867 }
868 } else {
869 fprintf(stderr, "Failed to crate a reader from the document\n");
William M. Brack8304d872004-06-08 13:29:32 +0000870 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000871 }
872}
Daniel Veillard81273902003-09-30 00:43:48 +0000873#endif /* LIBXML_READER_ENABLED */
Daniel Veillard7704fb12003-01-03 16:19:51 +0000874
875/************************************************************************
876 * *
877 * Tree Test processing *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000878 * *
879 ************************************************************************/
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000880static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
Daniel Veillard652327a2003-09-29 18:02:38 +0000881 xmlDocPtr doc = NULL;
882#ifdef LIBXML_TREE_ENABLED
883 xmlDocPtr tmp;
884#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000885
Daniel Veillard48b2f892001-02-25 16:11:03 +0000886 if ((timing) && (!repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +0000887 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000888
889
Daniel Veillard652327a2003-09-29 18:02:38 +0000890#ifdef LIBXML_TREE_ENABLED
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000891 if (filename == NULL) {
892 if (generate) {
893 xmlNodePtr n;
894
895 doc = xmlNewDoc(BAD_CAST "1.0");
896 n = xmlNewNode(NULL, BAD_CAST "info");
897 xmlNodeSetContent(n, BAD_CAST "abc");
898 xmlDocSetRootElement(doc, n);
899 }
900 }
Daniel Veillard652327a2003-09-29 18:02:38 +0000901#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000902#ifdef LIBXML_HTML_ENABLED
Daniel Veillard73b013f2003-09-30 12:36:01 +0000903#ifdef LIBXML_PUSH_ENABLED
William M. Brack78637da2003-07-31 14:47:38 +0000904 else if ((html) && (push)) {
905 FILE *f;
906
William M. Brack3403add2004-06-27 02:07:51 +0000907#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
908 f = fopen(filename, "rb");
909#else
910 f = fopen(filename, "r");
911#endif
William M. Brack78637da2003-07-31 14:47:38 +0000912 if (f != NULL) {
913 int res, size = 3;
914 char chars[4096];
915 htmlParserCtxtPtr ctxt;
916
917 /* if (repeat) */
918 size = 4096;
919 res = fread(chars, 1, 4, f);
920 if (res > 0) {
921 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
William M. Brack1d75c8a2003-10-27 13:48:16 +0000922 chars, res, filename, XML_CHAR_ENCODING_NONE);
William M. Brack78637da2003-07-31 14:47:38 +0000923 while ((res = fread(chars, 1, size, f)) > 0) {
924 htmlParseChunk(ctxt, chars, res, 0);
925 }
926 htmlParseChunk(ctxt, chars, 0, 1);
927 doc = ctxt->myDoc;
928 htmlFreeParserCtxt(ctxt);
929 }
930 fclose(f);
931 }
932 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000933#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000934 else if (html) {
Daniel Veillard9475a352003-09-26 12:47:50 +0000935 doc = htmlReadFile(filename, NULL, options);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000936 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000937#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000938 else {
Daniel Veillard73b013f2003-09-30 12:36:01 +0000939#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000940 /*
941 * build an XML tree from a string;
942 */
943 if (push) {
944 FILE *f;
945
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000946 /* '-' Usually means stdin -<sven@zen.org> */
947 if ((filename[0] == '-') && (filename[1] == 0)) {
948 f = stdin;
949 } else {
William M. Brack3403add2004-06-27 02:07:51 +0000950#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
951 f = fopen(filename, "rb");
952#else
953 f = fopen(filename, "r");
954#endif
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000955 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000956 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +0000957 int ret;
Daniel Veillarda880b122003-04-21 21:36:41 +0000958 int res, size = 1024;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000959 char chars[1024];
960 xmlParserCtxtPtr ctxt;
961
Daniel Veillarda880b122003-04-21 21:36:41 +0000962 /* if (repeat) size = 1024; */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000963 res = fread(chars, 1, 4, f);
964 if (res > 0) {
965 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
966 chars, res, filename);
Daniel Veillard500a1de2004-03-22 15:22:58 +0000967 xmlCtxtUseOptions(ctxt, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000968 while ((res = fread(chars, 1, size, f)) > 0) {
969 xmlParseChunk(ctxt, chars, res, 0);
970 }
971 xmlParseChunk(ctxt, chars, 0, 1);
972 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +0000973 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000974 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +0000975 if (!ret) {
976 xmlFreeDoc(doc);
977 doc = NULL;
978 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000979 }
980 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000981 } else
982#endif /* LIBXML_PUSH_ENABLED */
983 if (testIO) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000984 if ((filename[0] == '-') && (filename[1] == 0)) {
Daniel Veillard60942de2003-09-25 21:05:58 +0000985 doc = xmlReadFd(0, NULL, NULL, options);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000986 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000987 FILE *f;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000988
William M. Brack3403add2004-06-27 02:07:51 +0000989#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
990 f = fopen(filename, "rb");
991#else
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000992 f = fopen(filename, "r");
William M. Brack3403add2004-06-27 02:07:51 +0000993#endif
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000994 if (f != NULL) {
995 if (rectxt == NULL)
996 doc = xmlReadIO((xmlInputReadCallback) myRead,
997 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +0000998 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000999 else
1000 doc = xmlCtxtReadIO(rectxt,
1001 (xmlInputReadCallback) myRead,
1002 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +00001003 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001004 } else
Daniel Veillard5e873c42000-04-12 13:27:38 +00001005 doc = NULL;
Daniel Veillard5e873c42000-04-12 13:27:38 +00001006 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001007 } else if (htmlout) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001008 xmlParserCtxtPtr ctxt;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001009
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001010 if (rectxt == NULL)
1011 ctxt = xmlNewParserCtxt();
1012 else
1013 ctxt = rectxt;
Daniel Veillard88a172f2000-08-04 18:23:10 +00001014 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001015 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +00001016 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001017 ctxt->sax->error = xmlHTMLError;
1018 ctxt->sax->warning = xmlHTMLWarning;
1019 ctxt->vctxt.error = xmlHTMLValidityError;
1020 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001021
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001022 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001023
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001024 if (rectxt == NULL)
1025 xmlFreeParserCtxt(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001026 }
Daniel Veillard46e370e2000-07-21 20:32:03 +00001027#ifdef HAVE_SYS_MMAN_H
1028 } else if (memory) {
1029 int fd;
1030 struct stat info;
1031 const char *base;
1032 if (stat(filename, &info) < 0)
1033 return;
1034 if ((fd = open(filename, O_RDONLY)) < 0)
1035 return;
1036 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +00001037 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +00001038 return;
1039
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001040 if (rectxt == NULL)
Daniel Veillard60942de2003-09-25 21:05:58 +00001041 doc = xmlReadMemory((char *) base, info.st_size,
1042 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001043 else
Daniel Veillard60942de2003-09-25 21:05:58 +00001044 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
1045 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001046
Daniel Veillard46e370e2000-07-21 20:32:03 +00001047 munmap((char *) base, info.st_size);
1048#endif
Daniel Veillard4432df22003-09-28 18:58:27 +00001049#ifdef LIBXML_VALID_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00001050 } else if (valid) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001051 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001052
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001053 if (rectxt == NULL)
1054 ctxt = xmlNewParserCtxt();
1055 else
1056 ctxt = rectxt;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001057 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001058 doc = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001059 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001060 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
1061
1062 if (ctxt->valid == 0)
William M. Brack8304d872004-06-08 13:29:32 +00001063 progresult = XMLLINT_ERR_RDFILE;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001064 if (rectxt == NULL)
1065 xmlFreeParserCtxt(ctxt);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001066 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001067#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00001068 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001069 if (rectxt != NULL)
1070 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
1071 else
1072 doc = xmlReadFile(filename, NULL, options);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001073 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001074 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001075
Daniel Veillard88a172f2000-08-04 18:23:10 +00001076 /*
1077 * If we don't have a document we might as well give up. Do we
1078 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001079 if (doc == NULL) {
William M. Brack8304d872004-06-08 13:29:32 +00001080 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard88a172f2000-08-04 18:23:10 +00001081 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001082 }
1083
Daniel Veillard48b2f892001-02-25 16:11:03 +00001084 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001085 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001086 }
1087
Daniel Veillard29e43992001-12-13 22:21:58 +00001088 /*
1089 * Remove DOCTYPE nodes
1090 */
1091 if (dropdtd) {
1092 xmlDtdPtr dtd;
1093
1094 dtd = xmlGetIntSubset(doc);
1095 if (dtd != NULL) {
1096 xmlUnlinkNode((xmlNodePtr)dtd);
1097 xmlFreeDtd(dtd);
1098 }
1099 }
1100
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001101#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +00001102 if (xinclude) {
1103 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001104 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001105 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +00001106 xmlXIncludeProcessFlags(doc, options);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001107 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001108 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001109 }
1110 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001111#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +00001112
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001113#ifdef LIBXML_DEBUG_ENABLED
1114 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001115 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001116 */
1117 if (shell)
1118 xmlShell(doc, filename, xmlShellReadline, stdout);
1119#endif
1120
Daniel Veillard652327a2003-09-29 18:02:38 +00001121#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001122 /*
1123 * test intermediate copy if needed.
1124 */
1125 if (copy) {
1126 tmp = doc;
1127 doc = xmlCopyDoc(doc, 1);
1128 xmlFreeDoc(tmp);
1129 }
Daniel Veillard652327a2003-09-29 18:02:38 +00001130#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001131
Daniel Veillard4432df22003-09-28 18:58:27 +00001132#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001133 if ((insert) && (!html)) {
1134 const xmlChar* list[256];
1135 int nb, i;
1136 xmlNodePtr node;
1137
1138 if (doc->children != NULL) {
1139 node = doc->children;
1140 while ((node != NULL) && (node->last == NULL)) node = node->next;
1141 if (node != NULL) {
1142 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
1143 if (nb < 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001144 fprintf(stderr, "could not get valid list of elements\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001145 } else if (nb == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001146 fprintf(stderr, "No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001147 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001148 fprintf(stderr, "%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001149 nb);
1150 for (i = 0;i < nb;i++) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001151 fprintf(stderr, "%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001152 }
1153 }
1154 }
1155 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001156 }else
1157#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001158#ifdef LIBXML_READER_ENABLED
1159 if (walker) {
1160 walkDoc(doc);
1161 }
1162#endif /* LIBXML_READER_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001163#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001164 if (noout == 0) {
Daniel Veillard3df01182003-12-10 10:17:51 +00001165 int ret;
1166
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001167 /*
1168 * print it.
1169 */
1170#ifdef LIBXML_DEBUG_ENABLED
1171 if (!debug) {
1172#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00001173 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001174 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001175 }
Daniel Veillard656ce942004-04-30 23:11:45 +00001176#ifdef LIBXML_HTML_ENABLED
Daniel Veillard42fd4122003-11-04 08:47:48 +00001177 if ((html) && (!xmlout)) {
1178 if (compress) {
1179 htmlSaveFile(output ? output : "-", doc);
1180 }
1181 else if (encoding != NULL) {
1182 if ( format ) {
1183 htmlSaveFileFormat(output ? output : "-", doc, encoding, 1);
1184 }
1185 else {
1186 htmlSaveFileFormat(output ? output : "-", doc, encoding, 0);
1187 }
1188 }
1189 else if (format) {
1190 htmlSaveFileFormat(output ? output : "-", doc, NULL, 1);
1191 }
1192 else {
1193 FILE *out;
1194 if (output == NULL)
1195 out = stdout;
1196 else {
1197 out = fopen(output,"wb");
1198 }
1199 if (out != NULL) {
1200 if (htmlDocDump(out, doc) < 0)
William M. Brack8304d872004-06-08 13:29:32 +00001201 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00001202
1203 if (output != NULL)
1204 fclose(out);
1205 } else {
1206 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00001207 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00001208 }
1209 }
1210 if ((timing) && (!repeat)) {
1211 endTimer("Saving");
1212 }
1213 } else
1214#endif
Daniel Veillard25048d82004-08-14 22:37:54 +00001215#ifdef LIBXML_C14N_ENABLED
1216 if (canonical) {
1217 xmlChar *result = NULL;
1218 int size;
1219
1220 size = xmlC14NDocDumpMemory(doc, NULL, 0, NULL, 1, &result);
1221 if (size >= 0) {
1222 write(1, result, size);
1223 xmlFree(result);
1224 } else {
1225 fprintf(stderr, "Failed to canonicalize\n");
1226 progresult = XMLLINT_ERR_OUT;
1227 }
1228 } else
1229#endif
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001230#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001231 if (memory) {
1232 xmlChar *result;
1233 int len;
1234
1235 if (encoding != NULL) {
Daniel Veillardd536f702001-11-08 17:32:47 +00001236 if ( format ) {
1237 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
1238 } else {
1239 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
1240 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001241 } else {
Daniel Veillard90493a92001-08-14 14:12:47 +00001242 if (format)
1243 xmlDocDumpFormatMemory(doc, &result, &len, 1);
1244 else
1245 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001246 }
1247 if (result == NULL) {
1248 fprintf(stderr, "Failed to save\n");
Daniel Veillard25048d82004-08-14 22:37:54 +00001249 progresult = XMLLINT_ERR_OUT;
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001250 } else {
1251 write(1, result, len);
1252 xmlFree(result);
1253 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001254 } else
1255#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001256 if (compress) {
1257 xmlSaveFile(output ? output : "-", doc);
1258 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001259 else if (encoding != NULL) {
1260 if ( format ) {
Daniel Veillard3df01182003-12-10 10:17:51 +00001261 ret = xmlSaveFormatFileEnc(output ? output : "-", doc,
1262 encoding, 1);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001263 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001264 else {
Daniel Veillard3df01182003-12-10 10:17:51 +00001265 ret = xmlSaveFileEnc(output ? output : "-", doc, encoding);
1266 }
1267 if (ret < 0) {
1268 fprintf(stderr, "failed save to %s\n",
1269 output ? output : "-");
William M. Brack8304d872004-06-08 13:29:32 +00001270 progresult = XMLLINT_ERR_OUT;
Daniel Veillardd536f702001-11-08 17:32:47 +00001271 }
1272 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001273 else if (format) {
Daniel Veillard3df01182003-12-10 10:17:51 +00001274 ret = xmlSaveFormatFile(output ? output : "-", doc, 1);
1275 if (ret < 0) {
1276 fprintf(stderr, "failed save to %s\n",
1277 output ? output : "-");
William M. Brack8304d872004-06-08 13:29:32 +00001278 progresult = XMLLINT_ERR_OUT;
Daniel Veillard3df01182003-12-10 10:17:51 +00001279 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001280 }
1281 else {
1282 FILE *out;
1283 if (output == NULL)
1284 out = stdout;
1285 else {
1286 out = fopen(output,"wb");
1287 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001288 if (out != NULL) {
Daniel Veillard828ce832003-10-08 19:19:10 +00001289 if (xmlDocDump(out, doc) < 0)
William M. Brack8304d872004-06-08 13:29:32 +00001290 progresult = XMLLINT_ERR_OUT;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001291
Daniel Veillard05d987b2003-10-08 11:54:57 +00001292 if (output != NULL)
1293 fclose(out);
1294 } else {
1295 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00001296 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00001297 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001298 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001299 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001300 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001301 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001302#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001303 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001304 FILE *out;
1305 if (output == NULL)
1306 out = stdout;
1307 else {
1308 out = fopen(output,"wb");
1309 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001310 if (out != NULL) {
1311 xmlDebugDumpDocument(out, doc);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001312
Daniel Veillard05d987b2003-10-08 11:54:57 +00001313 if (output != NULL)
1314 fclose(out);
1315 } else {
1316 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00001317 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00001318 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001319 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001320#endif
1321 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001322#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001323
Daniel Veillard4432df22003-09-28 18:58:27 +00001324#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001325 /*
1326 * A posteriori validation test
1327 */
Daniel Veillard66f68e72003-08-18 16:39:51 +00001328 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
Daniel Veillardcd429612000-10-11 15:57:05 +00001329 xmlDtdPtr dtd;
1330
Daniel Veillard48b2f892001-02-25 16:11:03 +00001331 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001332 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001333 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00001334 if (dtdvalid != NULL)
1335 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
1336 else
1337 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001338 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001339 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001340 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001341 if (dtd == NULL) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001342 if (dtdvalid != NULL)
1343 xmlGenericError(xmlGenericErrorContext,
1344 "Could not parse DTD %s\n", dtdvalid);
1345 else
1346 xmlGenericError(xmlGenericErrorContext,
1347 "Could not parse DTD %s\n", dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00001348 progresult = XMLLINT_ERR_DTD;
Daniel Veillardcd429612000-10-11 15:57:05 +00001349 } else {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001350 xmlValidCtxtPtr cvp;
1351
1352 if ((cvp = xmlNewValidCtxt()) == NULL) {
1353 xmlGenericError(xmlGenericErrorContext,
1354 "Couldn't allocate validation context\n");
1355 exit(-1);
1356 }
1357 cvp->userData = (void *) stderr;
1358 cvp->error = (xmlValidityErrorFunc) fprintf;
1359 cvp->warning = (xmlValidityWarningFunc) fprintf;
1360
Daniel Veillard48b2f892001-02-25 16:11:03 +00001361 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001362 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001363 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001364 if (!xmlValidateDtd(cvp, doc, dtd)) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001365 if (dtdvalid != NULL)
1366 xmlGenericError(xmlGenericErrorContext,
1367 "Document %s does not validate against %s\n",
1368 filename, dtdvalid);
1369 else
1370 xmlGenericError(xmlGenericErrorContext,
1371 "Document %s does not validate against %s\n",
1372 filename, dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00001373 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00001374 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001375 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001376 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001377 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001378 xmlFreeValidCtxt(cvp);
Daniel Veillardcd429612000-10-11 15:57:05 +00001379 xmlFreeDtd(dtd);
1380 }
1381 } else if (postvalid) {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001382 xmlValidCtxtPtr cvp;
1383
1384 if ((cvp = xmlNewValidCtxt()) == NULL) {
1385 xmlGenericError(xmlGenericErrorContext,
1386 "Couldn't allocate validation context\n");
1387 exit(-1);
1388 }
1389
Daniel Veillard48b2f892001-02-25 16:11:03 +00001390 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001391 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001392 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001393 cvp->userData = (void *) stderr;
1394 cvp->error = (xmlValidityErrorFunc) fprintf;
1395 cvp->warning = (xmlValidityWarningFunc) fprintf;
1396 if (!xmlValidateDocument(cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001397 xmlGenericError(xmlGenericErrorContext,
1398 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001399 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00001400 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001401 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001402 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001403 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001404 xmlFreeValidCtxt(cvp);
Daniel Veillard4432df22003-09-28 18:58:27 +00001405 }
1406#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00001407#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001408 if (relaxngschemas != NULL) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001409 xmlRelaxNGValidCtxtPtr ctxt;
1410 int ret;
1411
Daniel Veillard42f12e92003-03-07 18:32:59 +00001412 if ((timing) && (!repeat)) {
1413 startTimer();
1414 }
1415
Daniel Veillard71531f32003-02-05 13:19:53 +00001416 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
1417 xmlRelaxNGSetValidErrors(ctxt,
1418 (xmlRelaxNGValidityErrorFunc) fprintf,
1419 (xmlRelaxNGValidityWarningFunc) fprintf,
1420 stderr);
1421 ret = xmlRelaxNGValidateDoc(ctxt, doc);
1422 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001423 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard71531f32003-02-05 13:19:53 +00001424 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001425 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001426 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00001427 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001428 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard71531f32003-02-05 13:19:53 +00001429 filename);
William M. Brack8304d872004-06-08 13:29:32 +00001430 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00001431 }
1432 xmlRelaxNGFreeValidCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001433 if ((timing) && (!repeat)) {
1434 endTimer("Validating");
1435 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001436 } else if (wxschemas != NULL) {
1437 xmlSchemaValidCtxtPtr ctxt;
1438 int ret;
1439
1440 if ((timing) && (!repeat)) {
1441 startTimer();
1442 }
1443
1444 ctxt = xmlSchemaNewValidCtxt(wxschemas);
1445 xmlSchemaSetValidErrors(ctxt,
1446 (xmlSchemaValidityErrorFunc) fprintf,
1447 (xmlSchemaValidityWarningFunc) fprintf,
1448 stderr);
1449 ret = xmlSchemaValidateDoc(ctxt, doc);
1450 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001451 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001452 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001453 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001454 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001455 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001456 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001457 filename);
William M. Brack8304d872004-06-08 13:29:32 +00001458 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001459 }
1460 xmlSchemaFreeValidCtxt(ctxt);
1461 if ((timing) && (!repeat)) {
1462 endTimer("Validating");
1463 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001464 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001465#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001466
1467#ifdef LIBXML_DEBUG_ENABLED
1468 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001469 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001470#endif
1471
1472 /*
1473 * free it.
1474 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00001475 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001476 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001477 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001478 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001479 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001480 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001481 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001482}
1483
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001484/************************************************************************
1485 * *
1486 * Usage and Main *
1487 * *
1488 ************************************************************************/
1489
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001490static void showVersion(const char *name) {
1491 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
1492 fprintf(stderr, " compiled with: ");
Daniel Veillard4432df22003-09-28 18:58:27 +00001493#ifdef LIBXML_VALID_ENABLED
1494 fprintf(stderr, "DTDValid ");
1495#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001496#ifdef LIBXML_FTP_ENABLED
1497 fprintf(stderr, "FTP ");
1498#endif
1499#ifdef LIBXML_HTTP_ENABLED
1500 fprintf(stderr, "HTTP ");
1501#endif
1502#ifdef LIBXML_HTML_ENABLED
1503 fprintf(stderr, "HTML ");
1504#endif
1505#ifdef LIBXML_C14N_ENABLED
1506 fprintf(stderr, "C14N ");
1507#endif
1508#ifdef LIBXML_CATALOG_ENABLED
1509 fprintf(stderr, "Catalog ");
1510#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001511#ifdef LIBXML_XPATH_ENABLED
1512 fprintf(stderr, "XPath ");
1513#endif
1514#ifdef LIBXML_XPTR_ENABLED
1515 fprintf(stderr, "XPointer ");
1516#endif
1517#ifdef LIBXML_XINCLUDE_ENABLED
1518 fprintf(stderr, "XInclude ");
1519#endif
1520#ifdef LIBXML_ICONV_ENABLED
1521 fprintf(stderr, "Iconv ");
1522#endif
1523#ifdef DEBUG_MEMORY_LOCATION
1524 fprintf(stderr, "MemDebug ");
1525#endif
1526#ifdef LIBXML_UNICODE_ENABLED
1527 fprintf(stderr, "Unicode ");
1528#endif
1529#ifdef LIBXML_REGEXP_ENABLED
1530 fprintf(stderr, "Regexps ");
1531#endif
1532#ifdef LIBXML_AUTOMATA_ENABLED
1533 fprintf(stderr, "Automata ");
1534#endif
1535#ifdef LIBXML_SCHEMAS_ENABLED
1536 fprintf(stderr, "Schemas ");
1537#endif
1538 fprintf(stderr, "\n");
1539}
1540
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001541static void usage(const char *name) {
1542 printf("Usage : %s [options] XMLfiles ...\n", name);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001543#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001544 printf("\tParse the XML files and output the result of the parsing\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001545#else
1546 printf("\tParse the XML files\n");
1547#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001548 printf("\t--version : display the version of the XML library used\n");
1549#ifdef LIBXML_DEBUG_ENABLED
1550 printf("\t--debug : dump a debug tree of the in-memory document\n");
1551 printf("\t--shell : run a navigating shell\n");
1552 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00001553#else
Daniel Veillard81273902003-09-30 00:43:48 +00001554#ifdef LIBXML_READER_ENABLED
Daniel Veillard8326e732003-01-07 00:19:07 +00001555 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001556#endif /* LIBXML_READER_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001557#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001558#ifdef LIBXML_TREE_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001559 printf("\t--copy : used to test the internal copy implementation\n");
Daniel Veillard652327a2003-09-29 18:02:38 +00001560#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001561 printf("\t--recover : output what was parsable on broken XML documents\n");
1562 printf("\t--noent : substitute entity references by their value\n");
1563 printf("\t--noout : don't output the result tree\n");
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001564 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001565 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001566 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001567#ifdef LIBXML_VALID_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001568 printf("\t--valid : validate the document in addition to std well-formed check\n");
1569 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
1570 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
Daniel Veillard66f68e72003-08-18 16:39:51 +00001571 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001572#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001573 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001574 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001575 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
1576 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001577#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001578#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001579 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001580#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001581#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001582#ifdef LIBXML_HTML_ENABLED
1583 printf("\t--html : use the HTML parser\n");
Daniel Veillard42fd4122003-11-04 08:47:48 +00001584 printf("\t--xmlout : force to use the XML serializer when using --html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001585#endif
Daniel Veillard73b013f2003-09-30 12:36:01 +00001586#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001587 printf("\t--push : use the push mode of the parser\n");
Daniel Veillard73b013f2003-09-30 12:36:01 +00001588#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001589#ifdef HAVE_SYS_MMAN_H
1590 printf("\t--memory : parse from memory\n");
1591#endif
Daniel Veillard87076042004-05-03 22:54:49 +00001592 printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001593 printf("\t--nowarning : do not emit warnings from parser/validator\n");
1594 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001595 printf("\t--nocdata : replace cdata section with text nodes\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001596#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001597 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001598 printf("\t--encode encoding : output in the given encoding\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001599 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
1600#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard25048d82004-08-14 22:37:54 +00001601 printf("\t--c14n: save in W3C canonical format (with comments)\n");
1602#ifdef LIBXML_C14N_ENABLED
1603#endif /* LIBXML_C14N_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001604 printf("\t--nsclean : remove redundant namespace declarations\n");
1605 printf("\t--testIO : test user I/O support\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001606#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00001607 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
1608 printf("\t otherwise XML Catalogs starting from \n");
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001609 printf("\t %s are activated by default\n", XML_XML_DEFAULT_CATALOG);
Daniel Veillard05c13a22001-09-09 08:38:09 +00001610 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001611#endif
1612 printf("\t--auto : generate a small doc on the fly\n");
1613#ifdef LIBXML_XINCLUDE_ENABLED
1614 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillardc14c3892004-08-16 12:34:50 +00001615 printf("\t--noxincludenode : same but do not generate XInclude nodes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001616#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001617 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00001618 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard81273902003-09-30 00:43:48 +00001619#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001620 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001621 printf("\t--walker : create a reader and walk though the resulting doc\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001622#endif /* LIBXML_READER_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001623#ifdef LIBXML_PATTERN_ENABLED
1624 printf("\t--pattern pattern_value : test the pattern support\n");
1625#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001626 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillardef4d3bc2003-02-07 12:38:22 +00001627#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard71531f32003-02-05 13:19:53 +00001628 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001629 printf("\t--schema schema : do validation against the WXS schema\n");
Daniel Veillard71531f32003-02-05 13:19:53 +00001630#endif
Daniel Veillarda42f25f2002-01-25 14:15:40 +00001631 printf("\nLibxml project home page: http://xmlsoft.org/\n");
1632 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001633}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001634
1635static void registerNode(xmlNodePtr node)
1636{
1637 node->_private = malloc(sizeof(long));
1638 *(long*)node->_private = (long) 0x81726354;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00001639 nbregister++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001640}
1641
1642static void deregisterNode(xmlNodePtr node)
1643{
1644 assert(node->_private != NULL);
1645 assert(*(long*)node->_private == (long) 0x81726354);
1646 free(node->_private);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00001647 nbregister--;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001648}
1649
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001650int
1651main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001652 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001653 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00001654 int version = 0;
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001655 const char* indent;
1656
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001657 if (argc <= 1) {
1658 usage(argv[0]);
1659 return(1);
1660 }
Daniel Veillardbe803962000-06-28 23:40:59 +00001661 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001662 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001663 if (!strcmp(argv[i], "-"))
1664 break;
1665
1666 if (argv[i][0] != '-')
1667 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001668 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
1669 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00001670 else
1671#ifdef LIBXML_DEBUG_ENABLED
1672 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001673 (!strcmp(argv[i], "--shell"))) {
1674 shell++;
1675 noout = 1;
1676 } else
1677#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001678#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001679 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
1680 copy++;
Daniel Veillard652327a2003-09-29 18:02:38 +00001681 else
1682#endif /* LIBXML_TREE_ENABLED */
1683 if ((!strcmp(argv[i], "-recover")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001684 (!strcmp(argv[i], "--recover"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001685 recovery++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001686 options |= XML_PARSE_RECOVER;
1687 } else if ((!strcmp(argv[i], "-noent")) ||
1688 (!strcmp(argv[i], "--noent"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001689 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001690 options |= XML_PARSE_NOENT;
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001691 } else if ((!strcmp(argv[i], "-nsclean")) ||
1692 (!strcmp(argv[i], "--nsclean"))) {
1693 options |= XML_PARSE_NSCLEAN;
1694 } else if ((!strcmp(argv[i], "-nocdata")) ||
1695 (!strcmp(argv[i], "--nocdata"))) {
1696 options |= XML_PARSE_NOCDATA;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001697 } else if ((!strcmp(argv[i], "-nodict")) ||
1698 (!strcmp(argv[i], "--nodict"))) {
1699 options |= XML_PARSE_NODICT;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001700 } else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00001701 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001702 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00001703 version = 1;
1704 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001705 (!strcmp(argv[i], "--noout")))
1706 noout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001707#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001708 else if ((!strcmp(argv[i], "-o")) ||
1709 (!strcmp(argv[i], "-output")) ||
1710 (!strcmp(argv[i], "--output"))) {
1711 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00001712 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001713 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001714#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001715 else if ((!strcmp(argv[i], "-htmlout")) ||
1716 (!strcmp(argv[i], "--htmlout")))
1717 htmlout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001718 else if ((!strcmp(argv[i], "-nowrap")) ||
1719 (!strcmp(argv[i], "--nowrap")))
1720 nowrap++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001721#ifdef LIBXML_HTML_ENABLED
1722 else if ((!strcmp(argv[i], "-html")) ||
1723 (!strcmp(argv[i], "--html"))) {
1724 html++;
1725 }
Daniel Veillard42fd4122003-11-04 08:47:48 +00001726 else if ((!strcmp(argv[i], "-xmlout")) ||
1727 (!strcmp(argv[i], "--xmlout"))) {
1728 xmlout++;
1729 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001730#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001731 else if ((!strcmp(argv[i], "-loaddtd")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001732 (!strcmp(argv[i], "--loaddtd"))) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001733 loaddtd++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001734 options |= XML_PARSE_DTDLOAD;
1735 } else if ((!strcmp(argv[i], "-dtdattr")) ||
Daniel Veillard48da9102001-08-07 01:10:10 +00001736 (!strcmp(argv[i], "--dtdattr"))) {
1737 loaddtd++;
1738 dtdattrs++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001739 options |= XML_PARSE_DTDATTR;
Daniel Veillard4432df22003-09-28 18:58:27 +00001740 }
1741#ifdef LIBXML_VALID_ENABLED
1742 else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001743 (!strcmp(argv[i], "--valid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001744 valid++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001745 options |= XML_PARSE_DTDVALID;
1746 } else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001747 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001748 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001749 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00001750 options |= XML_PARSE_DTDLOAD;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001751 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00001752 (!strcmp(argv[i], "--dtdvalid"))) {
1753 i++;
1754 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001755 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00001756 options |= XML_PARSE_DTDLOAD;
Daniel Veillard66f68e72003-08-18 16:39:51 +00001757 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
1758 (!strcmp(argv[i], "--dtdvalidfpi"))) {
1759 i++;
1760 dtdvalidfpi = argv[i];
1761 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00001762 options |= XML_PARSE_DTDLOAD;
Daniel Veillardcd429612000-10-11 15:57:05 +00001763 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001764#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard29e43992001-12-13 22:21:58 +00001765 else if ((!strcmp(argv[i], "-dropdtd")) ||
1766 (!strcmp(argv[i], "--dropdtd")))
1767 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001768 else if ((!strcmp(argv[i], "-insert")) ||
1769 (!strcmp(argv[i], "--insert")))
1770 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001771 else if ((!strcmp(argv[i], "-timing")) ||
1772 (!strcmp(argv[i], "--timing")))
1773 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001774 else if ((!strcmp(argv[i], "-auto")) ||
1775 (!strcmp(argv[i], "--auto")))
1776 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001777 else if ((!strcmp(argv[i], "-repeat")) ||
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001778 (!strcmp(argv[i], "--repeat"))) {
1779 if (repeat)
1780 repeat *= 10;
1781 else
1782 repeat = 100;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001783 }
1784#ifdef LIBXML_PUSH_ENABLED
1785 else if ((!strcmp(argv[i], "-push")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001786 (!strcmp(argv[i], "--push")))
1787 push++;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001788#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +00001789#ifdef HAVE_SYS_MMAN_H
1790 else if ((!strcmp(argv[i], "-memory")) ||
1791 (!strcmp(argv[i], "--memory")))
1792 memory++;
1793#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00001794 else if ((!strcmp(argv[i], "-testIO")) ||
1795 (!strcmp(argv[i], "--testIO")))
1796 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001797#ifdef LIBXML_XINCLUDE_ENABLED
1798 else if ((!strcmp(argv[i], "-xinclude")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001799 (!strcmp(argv[i], "--xinclude"))) {
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001800 xinclude++;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001801 options |= XML_PARSE_XINCLUDE;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001802 }
Daniel Veillardc14c3892004-08-16 12:34:50 +00001803 else if ((!strcmp(argv[i], "-noxincludenode")) ||
1804 (!strcmp(argv[i], "--noxincludenode"))) {
1805 xinclude++;
1806 options |= XML_PARSE_XINCLUDE;
1807 options |= XML_PARSE_NOXINCNODE;
1808 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001809#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001810#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001811#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001812 else if ((!strcmp(argv[i], "-compress")) ||
1813 (!strcmp(argv[i], "--compress"))) {
1814 compress++;
1815 xmlSetCompressMode(9);
1816 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001817#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001818#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001819 else if ((!strcmp(argv[i], "-nowarning")) ||
1820 (!strcmp(argv[i], "--nowarning"))) {
1821 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001822 xmlPedanticParserDefault(0);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001823 options |= XML_PARSE_NOWARNING;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001824 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001825 else if ((!strcmp(argv[i], "-pedantic")) ||
1826 (!strcmp(argv[i], "--pedantic"))) {
1827 xmlGetWarningsDefaultValue = 1;
1828 xmlPedanticParserDefault(1);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001829 options |= XML_PARSE_PEDANTIC;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001830 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001831#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001832 else if ((!strcmp(argv[i], "-debugent")) ||
1833 (!strcmp(argv[i], "--debugent"))) {
1834 debugent++;
1835 xmlParserDebugEntities = 1;
1836 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001837#endif
Daniel Veillard25048d82004-08-14 22:37:54 +00001838#ifdef LIBXML_C14N_ENABLED
1839 else if ((!strcmp(argv[i], "-c14n")) ||
1840 (!strcmp(argv[i], "--c14n"))) {
1841 canonical++;
1842 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
1843 }
1844#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00001845#ifdef LIBXML_CATALOG_ENABLED
1846 else if ((!strcmp(argv[i], "-catalogs")) ||
1847 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001848 catalogs++;
1849 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
1850 (!strcmp(argv[i], "--nocatalogs"))) {
1851 nocatalogs++;
Daniel Veillard81418e32001-05-22 15:08:55 +00001852 }
1853#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00001854 else if ((!strcmp(argv[i], "-encode")) ||
1855 (!strcmp(argv[i], "--encode"))) {
1856 i++;
1857 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001858 /*
1859 * OK it's for testing purposes
1860 */
1861 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00001862 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001863 else if ((!strcmp(argv[i], "-noblanks")) ||
1864 (!strcmp(argv[i], "--noblanks"))) {
1865 noblanks++;
1866 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00001867 }
Daniel Veillard87076042004-05-03 22:54:49 +00001868 else if ((!strcmp(argv[i], "-maxmem")) ||
1869 (!strcmp(argv[i], "--maxmem"))) {
1870 i++;
1871 if (sscanf(argv[i], "%d", &maxmem) == 1) {
1872 xmlMemSetup(myFreeFunc, myMallocFunc, myReallocFunc,
1873 myStrdupFunc);
1874 } else {
1875 maxmem = 0;
1876 }
1877 }
Daniel Veillard90493a92001-08-14 14:12:47 +00001878 else if ((!strcmp(argv[i], "-format")) ||
1879 (!strcmp(argv[i], "--format"))) {
1880 noblanks++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001881#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001882 format++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001883#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard90493a92001-08-14 14:12:47 +00001884 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001885 }
Daniel Veillard81273902003-09-30 00:43:48 +00001886#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001887 else if ((!strcmp(argv[i], "-stream")) ||
1888 (!strcmp(argv[i], "--stream"))) {
1889 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001890 }
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001891 else if ((!strcmp(argv[i], "-walker")) ||
1892 (!strcmp(argv[i], "--walker"))) {
1893 walker++;
1894 noout++;
1895 }
Daniel Veillard81273902003-09-30 00:43:48 +00001896#endif /* LIBXML_READER_ENABLED */
1897#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001898 else if ((!strcmp(argv[i], "-sax1")) ||
1899 (!strcmp(argv[i], "--sax1"))) {
1900 sax1++;
1901 }
Daniel Veillard81273902003-09-30 00:43:48 +00001902#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001903 else if ((!strcmp(argv[i], "-chkregister")) ||
1904 (!strcmp(argv[i], "--chkregister"))) {
1905 chkregister++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001906#ifdef LIBXML_SCHEMAS_ENABLED
1907 } else if ((!strcmp(argv[i], "-relaxng")) ||
1908 (!strcmp(argv[i], "--relaxng"))) {
1909 i++;
1910 relaxng = argv[i];
1911 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001912 options |= XML_PARSE_NOENT;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001913 } else if ((!strcmp(argv[i], "-schema")) ||
1914 (!strcmp(argv[i], "--schema"))) {
1915 i++;
1916 schema = argv[i];
1917 noent++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001918#endif
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001919 } else if ((!strcmp(argv[i], "-nonet")) ||
1920 (!strcmp(argv[i], "--nonet"))) {
Daniel Veillard61b93382003-11-03 14:28:31 +00001921 options |= XML_PARSE_NONET;
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001922#ifdef LIBXML_PATTERN_ENABLED
1923 } else if ((!strcmp(argv[i], "-pattern")) ||
1924 (!strcmp(argv[i], "--pattern"))) {
1925 i++;
1926 pattern = argv[i];
1927#endif
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001928 } else {
1929 fprintf(stderr, "Unknown option %s\n", argv[i]);
1930 usage(argv[0]);
1931 return(1);
1932 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001933 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001934
1935#ifdef LIBXML_CATALOG_ENABLED
1936 if (nocatalogs == 0) {
1937 if (catalogs) {
1938 const char *catal;
1939
1940 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00001941 if (catal != NULL) {
1942 xmlLoadCatalogs(catal);
1943 } else {
1944 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
1945 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001946 }
1947 }
1948#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001949
Daniel Veillard81273902003-09-30 00:43:48 +00001950#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001951 if (sax1)
1952 xmlSAXDefaultVersion(1);
1953 else
1954 xmlSAXDefaultVersion(2);
Daniel Veillard81273902003-09-30 00:43:48 +00001955#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00001956
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001957 if (chkregister) {
1958 xmlRegisterNodeDefault(registerNode);
1959 xmlDeregisterNodeDefault(deregisterNode);
1960 }
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001961
1962 indent = getenv("XMLLINT_INDENT");
1963 if(indent != NULL) {
1964 xmlTreeIndentString = indent;
1965 }
1966
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001967
Daniel Veillardd9bad132001-07-23 19:39:43 +00001968 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00001969 if (loaddtd != 0)
1970 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
1971 if (dtdattrs)
1972 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001973 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillard4432df22003-09-28 18:58:27 +00001974#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001975 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard4432df22003-09-28 18:58:27 +00001976#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001977 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001978 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001979 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001980 xmlGenericError(xmlGenericErrorContext,
1981 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
1982 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001983 "<html><head><title>%s output</title></head>\n",
1984 argv[0]);
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001985 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001986 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
1987 argv[0]);
1988 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001989
1990#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard81273902003-09-30 00:43:48 +00001991 if ((relaxng != NULL)
1992#ifdef LIBXML_READER_ENABLED
1993 && (stream == 0)
1994#endif /* LIBXML_READER_ENABLED */
1995 ) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001996 xmlRelaxNGParserCtxtPtr ctxt;
1997
Daniel Veillardce192eb2003-04-16 15:58:05 +00001998 /* forces loading the DTDs */
1999 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00002000 options |= XML_PARSE_DTDLOAD;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002001 if (timing) {
2002 startTimer();
2003 }
Daniel Veillard71531f32003-02-05 13:19:53 +00002004 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
2005 xmlRelaxNGSetParserErrors(ctxt,
2006 (xmlRelaxNGValidityErrorFunc) fprintf,
2007 (xmlRelaxNGValidityWarningFunc) fprintf,
2008 stderr);
2009 relaxngschemas = xmlRelaxNGParse(ctxt);
Daniel Veillardce192eb2003-04-16 15:58:05 +00002010 if (relaxngschemas == NULL) {
2011 xmlGenericError(xmlGenericErrorContext,
2012 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +00002013 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +00002014 relaxng = NULL;
2015 }
Daniel Veillard71531f32003-02-05 13:19:53 +00002016 xmlRelaxNGFreeParserCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002017 if (timing) {
2018 endTimer("Compiling the schemas");
2019 }
Daniel Veillardebe25d42004-03-25 09:35:49 +00002020 } else if ((schema != NULL)
2021#ifdef LIBXML_READER_ENABLED
2022 && (stream == 0)
2023#endif
2024 ) {
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002025 xmlSchemaParserCtxtPtr ctxt;
2026
2027 if (timing) {
2028 startTimer();
2029 }
2030 ctxt = xmlSchemaNewParserCtxt(schema);
2031 xmlSchemaSetParserErrors(ctxt,
2032 (xmlSchemaValidityErrorFunc) fprintf,
2033 (xmlSchemaValidityWarningFunc) fprintf,
2034 stderr);
2035 wxschemas = xmlSchemaParse(ctxt);
2036 if (wxschemas == NULL) {
2037 xmlGenericError(xmlGenericErrorContext,
2038 "WXS schema %s failed to compile\n", schema);
William M. Brack8304d872004-06-08 13:29:32 +00002039 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002040 schema = NULL;
2041 }
2042 xmlSchemaFreeParserCtxt(ctxt);
2043 if (timing) {
2044 endTimer("Compiling the schemas");
2045 }
Daniel Veillard71531f32003-02-05 13:19:53 +00002046 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002047#endif /* LIBXML_SCHEMAS_ENABLED */
2048#ifdef LIBXML_PATTERN_ENABLED
2049 if (pattern != NULL) {
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00002050 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002051 if (patternc == NULL) {
2052 xmlGenericError(xmlGenericErrorContext,
2053 "Pattern %s failed to compile\n", pattern);
William M. Brack8304d872004-06-08 13:29:32 +00002054 progresult = XMLLINT_ERR_SCHEMAPAT;
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002055 pattern = NULL;
2056 }
2057 }
2058#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002059 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00002060 if ((!strcmp(argv[i], "-encode")) ||
2061 (!strcmp(argv[i], "--encode"))) {
2062 i++;
2063 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002064 } else if ((!strcmp(argv[i], "-o")) ||
2065 (!strcmp(argv[i], "-output")) ||
2066 (!strcmp(argv[i], "--output"))) {
2067 i++;
2068 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00002069 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002070#ifdef LIBXML_VALID_ENABLED
Daniel Veillardcd429612000-10-11 15:57:05 +00002071 if ((!strcmp(argv[i], "-dtdvalid")) ||
2072 (!strcmp(argv[i], "--dtdvalid"))) {
2073 i++;
2074 continue;
2075 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00002076 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
2077 (!strcmp(argv[i], "--dtdvalidfpi"))) {
2078 i++;
2079 continue;
2080 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002081#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00002082 if ((!strcmp(argv[i], "-relaxng")) ||
2083 (!strcmp(argv[i], "--relaxng"))) {
2084 i++;
2085 continue;
2086 }
Daniel Veillard87076042004-05-03 22:54:49 +00002087 if ((!strcmp(argv[i], "-maxmem")) ||
2088 (!strcmp(argv[i], "--maxmem"))) {
2089 i++;
2090 continue;
2091 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002092 if ((!strcmp(argv[i], "-schema")) ||
2093 (!strcmp(argv[i], "--schema"))) {
2094 i++;
2095 continue;
2096 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002097#ifdef LIBXML_PATTERN_ENABLED
2098 if ((!strcmp(argv[i], "-pattern")) ||
2099 (!strcmp(argv[i], "--pattern"))) {
2100 i++;
2101 continue;
2102 }
2103#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00002104 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00002105 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002106 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002107 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002108 if (repeat) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002109 xmlParserCtxtPtr ctxt = NULL;
2110
2111 for (acount = 0;acount < repeat;acount++) {
Daniel Veillard81273902003-09-30 00:43:48 +00002112#ifdef LIBXML_READER_ENABLED
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002113 if (stream != 0) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00002114 streamFile(argv[i]);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002115 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00002116#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002117 if (ctxt == NULL)
2118 ctxt = xmlNewParserCtxt();
2119 parseAndPrintFile(argv[i], ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00002120#ifdef LIBXML_READER_ENABLED
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002121 }
Daniel Veillard81273902003-09-30 00:43:48 +00002122#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002123 }
2124 if (ctxt != NULL)
2125 xmlFreeParserCtxt(ctxt);
Daniel Veillard7704fb12003-01-03 16:19:51 +00002126 } else {
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002127 nbregister = 0;
2128
Daniel Veillard81273902003-09-30 00:43:48 +00002129#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00002130 if (stream != 0)
2131 streamFile(argv[i]);
2132 else
Daniel Veillard81273902003-09-30 00:43:48 +00002133#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002134 parseAndPrintFile(argv[i], NULL);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002135
2136 if ((chkregister) && (nbregister != 0)) {
2137 fprintf(stderr, "Registration count off: %d\n", nbregister);
William M. Brack8304d872004-06-08 13:29:32 +00002138 progresult = XMLLINT_ERR_RDREGIS;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002139 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00002140 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002141 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00002142 if ((timing) && (repeat)) {
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00002143 endTimer("%d iterations", repeat);
Daniel Veillarda7866932001-12-04 13:14:44 +00002144 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002145 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002146 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002147 if (generate)
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002148 parseAndPrintFile(NULL, NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002149 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00002150 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002151 }
Daniel Veillard845cce42002-01-09 11:51:37 +00002152 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002153 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002154 }
Daniel Veillard71531f32003-02-05 13:19:53 +00002155#ifdef LIBXML_SCHEMAS_ENABLED
2156 if (relaxngschemas != NULL)
2157 xmlRelaxNGFree(relaxngschemas);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002158 if (wxschemas != NULL)
2159 xmlSchemaFree(wxschemas);
Daniel Veillard71531f32003-02-05 13:19:53 +00002160 xmlRelaxNGCleanupTypes();
2161#endif
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002162#ifdef LIBXML_PATTERN_ENABLED
2163 if (patternc != NULL)
2164 xmlFreePattern(patternc);
2165#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002166 xmlCleanupParser();
2167 xmlMemoryDump();
2168
Daniel Veillardf7cd4812001-02-23 18:44:52 +00002169 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002170}
Daniel Veillard88a172f2000-08-04 18:23:10 +00002171