blob: e91707c6432f9e968b6360acee0319eacd28de20 [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 Veillard81273902003-09-30 00:43:48 +0000171#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000172static int stream = 0;
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000173static int walker = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000174#endif /* LIBXML_READER_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000175static int chkregister = 0;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +0000176static int nbregister = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000177#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +0000178static int sax1 = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000179#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000180#ifdef LIBXML_PATTERN_ENABLED
181static const char *pattern = NULL;
182static xmlPatternPtr patternc = NULL;
183#endif
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000184static int options = 0;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000185
Daniel Veillard87076042004-05-03 22:54:49 +0000186/************************************************************************
187 * *
188 * Memory allocation consumption debugging *
189 * *
190 ************************************************************************/
191
Daniel Veillard3af3b592004-05-05 19:22:30 +0000192static void
193OOM(void)
194{
Daniel Veillard87076042004-05-03 22:54:49 +0000195 fprintf(stderr, "Ran out of memory needs > %d bytes\n", maxmem);
William M. Brack8304d872004-06-08 13:29:32 +0000196 progresult = XMLLINT_ERR_MEM;
Daniel Veillard87076042004-05-03 22:54:49 +0000197}
198
Daniel Veillard3af3b592004-05-05 19:22:30 +0000199static void
200myFreeFunc(void *mem)
201{
Daniel Veillard87076042004-05-03 22:54:49 +0000202 xmlMemFree(mem);
203}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000204static void *
205myMallocFunc(size_t size)
206{
Daniel Veillard87076042004-05-03 22:54:49 +0000207 void *ret;
208
209 ret = xmlMemMalloc(size);
210 if (ret != NULL) {
211 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000212 OOM();
213 xmlMemFree(ret);
214 return (NULL);
215 }
Daniel Veillard87076042004-05-03 22:54:49 +0000216 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000217 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000218}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000219static void *
220myReallocFunc(void *mem, size_t size)
221{
Daniel Veillard87076042004-05-03 22:54:49 +0000222 void *ret;
223
224 ret = xmlMemRealloc(mem, size);
225 if (ret != NULL) {
226 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000227 OOM();
228 xmlMemFree(ret);
229 return (NULL);
230 }
Daniel Veillard87076042004-05-03 22:54:49 +0000231 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000232 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000233}
Daniel Veillard3af3b592004-05-05 19:22:30 +0000234static char *
235myStrdupFunc(const char *str)
236{
Daniel Veillard87076042004-05-03 22:54:49 +0000237 char *ret;
238
239 ret = xmlMemoryStrdup(str);
240 if (ret != NULL) {
241 if (xmlMemUsed() > maxmem) {
Daniel Veillard3af3b592004-05-05 19:22:30 +0000242 OOM();
243 xmlFree(ret);
244 return (NULL);
245 }
Daniel Veillard87076042004-05-03 22:54:49 +0000246 }
Daniel Veillard3af3b592004-05-05 19:22:30 +0000247 return (ret);
Daniel Veillard87076042004-05-03 22:54:49 +0000248}
Daniel Veillard87076042004-05-03 22:54:49 +0000249/************************************************************************
250 * *
251 * Internal timing routines to remove the necessity to have *
252 * unix-specific function calls. *
253 * *
254 ************************************************************************/
Daniel Veillard01db67c2001-12-18 07:09:59 +0000255
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000256#ifndef HAVE_GETTIMEOFDAY
257#ifdef HAVE_SYS_TIMEB_H
258#ifdef HAVE_SYS_TIME_H
259#ifdef HAVE_FTIME
260
Daniel Veillard01c13b52002-12-10 15:19:08 +0000261static int
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000262my_gettimeofday(struct timeval *tvp, void *tzp)
263{
264 struct timeb timebuffer;
265
266 ftime(&timebuffer);
267 if (tvp) {
268 tvp->tv_sec = timebuffer.time;
269 tvp->tv_usec = timebuffer.millitm * 1000L;
270 }
271 return (0);
272}
273#define HAVE_GETTIMEOFDAY 1
274#define gettimeofday my_gettimeofday
275
276#endif /* HAVE_FTIME */
277#endif /* HAVE_SYS_TIME_H */
278#endif /* HAVE_SYS_TIMEB_H */
279#endif /* !HAVE_GETTIMEOFDAY */
280
Daniel Veillard01db67c2001-12-18 07:09:59 +0000281#if defined(HAVE_GETTIMEOFDAY)
282static struct timeval begin, end;
283
284/*
285 * startTimer: call where you want to start timing
286 */
287static void
288startTimer(void)
289{
290 gettimeofday(&begin, NULL);
291}
292
293/*
294 * endTimer: call where you want to stop timing and to print out a
295 * message about the timing performed; format is a printf
296 * type argument
297 */
298static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000299endTimer(const char *fmt, ...)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000300{
301 long msec;
302 va_list ap;
303
304 gettimeofday(&end, NULL);
305 msec = end.tv_sec - begin.tv_sec;
306 msec *= 1000;
307 msec += (end.tv_usec - begin.tv_usec) / 1000;
308
309#ifndef HAVE_STDARG_H
310#error "endTimer required stdarg functions"
311#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000312 va_start(ap, fmt);
313 vfprintf(stderr, fmt, ap);
Daniel Veillard01db67c2001-12-18 07:09:59 +0000314 va_end(ap);
315
316 fprintf(stderr, " took %ld ms\n", msec);
317}
318#elif defined(HAVE_TIME_H)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000319/*
320 * No gettimeofday function, so we have to make do with calling clock.
321 * This is obviously less accurate, but there's little we can do about
322 * that.
323 */
Daniel Veillard90bc3712002-03-07 15:12:58 +0000324#ifndef CLOCKS_PER_SEC
325#define CLOCKS_PER_SEC 100
326#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +0000327
328static clock_t begin, end;
329static void
330startTimer(void)
331{
332 begin = clock();
333}
334static void
335endTimer(const char *fmt, ...)
336{
337 long msec;
338 va_list ap;
339
340 end = clock();
341 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
342
343#ifndef HAVE_STDARG_H
344#error "endTimer required stdarg functions"
345#endif
346 va_start(ap, fmt);
347 vfprintf(stderr, fmt, ap);
348 va_end(ap);
349 fprintf(stderr, " took %ld ms\n", msec);
350}
351#else
352
353/*
354 * We don't have a gettimeofday or time.h, so we just don't do timing
355 */
356static void
357startTimer(void)
358{
359 /*
360 * Do nothing
361 */
362}
363static void
364endTimer(char *format, ...)
365{
366 /*
367 * We cannot do anything because we don't have a timing function
368 */
369#ifdef HAVE_STDARG_H
370 va_start(ap, format);
371 vfprintf(stderr, format, ap);
372 va_end(ap);
373 fprintf(stderr, " was not timed\n", msec);
374#else
375 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
376 * this ?!
377 */
378#endif
379}
380#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000381/************************************************************************
382 * *
383 * HTML ouput *
384 * *
385 ************************************************************************/
386char buffer[50000];
387
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000388static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000389xmlHTMLEncodeSend(void) {
390 char *result;
391
392 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
393 if (result) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000394 xmlGenericError(xmlGenericErrorContext, "%s", result);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000395 xmlFree(result);
396 }
397 buffer[0] = 0;
398}
399
400/**
401 * xmlHTMLPrintFileInfo:
402 * @input: an xmlParserInputPtr input
403 *
404 * Displays the associated file and line informations for the current input
405 */
406
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000407static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000408xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000409 int len;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000410 xmlGenericError(xmlGenericErrorContext, "<p>");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000411
412 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000413 if (input != NULL) {
414 if (input->filename) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000415 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000416 input->line);
417 } else {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000418 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000419 }
420 }
421 xmlHTMLEncodeSend();
422}
423
424/**
425 * xmlHTMLPrintFileContext:
426 * @input: an xmlParserInputPtr input
427 *
428 * Displays current context within the input content for error tracking
429 */
430
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000431static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000432xmlHTMLPrintFileContext(xmlParserInputPtr input) {
433 const xmlChar *cur, *base;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000434 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000435 int n;
436
437 if (input == NULL) return;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000438 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000439 cur = input->cur;
440 base = input->base;
441 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
442 cur--;
443 }
444 n = 0;
445 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
446 cur--;
447 if ((*cur == '\n') || (*cur == '\r')) cur++;
448 base = cur;
449 n = 0;
450 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000451 len = strlen(buffer);
452 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
453 (unsigned char) *cur++);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000454 n++;
455 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000456 len = strlen(buffer);
457 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000458 cur = input->cur;
459 while ((*cur == '\n') || (*cur == '\r'))
460 cur--;
461 n = 0;
462 while ((cur != base) && (n++ < 80)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000463 len = strlen(buffer);
464 snprintf(&buffer[len], sizeof(buffer) - len, " ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000465 base++;
466 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000467 len = strlen(buffer);
468 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000469 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000470 xmlGenericError(xmlGenericErrorContext, "</pre>");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000471}
472
473/**
474 * xmlHTMLError:
475 * @ctx: an XML parser context
476 * @msg: the message to display/transmit
477 * @...: extra parameters for the message display
478 *
479 * Display and format an error messages, gives file, line, position and
480 * extra parameters.
481 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000482static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000483xmlHTMLError(void *ctx, const char *msg, ...)
484{
485 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
486 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000487 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000488 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000489
490 buffer[0] = 0;
491 input = ctxt->input;
492 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000493 input = ctxt->inputTab[ctxt->inputNr - 2];
494 }
495
496 xmlHTMLPrintFileInfo(input);
497
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000498 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000499 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000500 len = strlen(buffer);
501 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000502 va_end(args);
503 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000504 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000505
506 xmlHTMLPrintFileContext(input);
507 xmlHTMLEncodeSend();
508}
509
510/**
511 * xmlHTMLWarning:
512 * @ctx: an XML parser context
513 * @msg: the message to display/transmit
514 * @...: extra parameters for the message display
515 *
516 * Display and format a warning messages, gives file, line, position and
517 * extra parameters.
518 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000519static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000520xmlHTMLWarning(void *ctx, const char *msg, ...)
521{
522 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
523 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000524 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000525 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000526
527 buffer[0] = 0;
528 input = ctxt->input;
529 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000530 input = ctxt->inputTab[ctxt->inputNr - 2];
531 }
532
533
534 xmlHTMLPrintFileInfo(input);
535
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000536 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000537 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000538 len = strlen(buffer);
539 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000540 va_end(args);
541 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000542 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000543
544 xmlHTMLPrintFileContext(input);
545 xmlHTMLEncodeSend();
546}
547
548/**
549 * xmlHTMLValidityError:
550 * @ctx: an XML parser context
551 * @msg: the message to display/transmit
552 * @...: extra parameters for the message display
553 *
554 * Display and format an validity error messages, gives file,
555 * line, position and extra parameters.
556 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000557static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000558xmlHTMLValidityError(void *ctx, const char *msg, ...)
559{
560 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
561 xmlParserInputPtr input;
562 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000563 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000564
565 buffer[0] = 0;
566 input = ctxt->input;
567 if ((input->filename == NULL) && (ctxt->inputNr > 1))
568 input = ctxt->inputTab[ctxt->inputNr - 2];
569
570 xmlHTMLPrintFileInfo(input);
571
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000572 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000573 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000574 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000575 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000576 va_end(args);
577 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000578 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000579
580 xmlHTMLPrintFileContext(input);
581 xmlHTMLEncodeSend();
582}
583
584/**
585 * xmlHTMLValidityWarning:
586 * @ctx: an XML parser context
587 * @msg: the message to display/transmit
588 * @...: extra parameters for the message display
589 *
590 * Display and format a validity warning messages, gives file, line,
591 * position and extra parameters.
592 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000593static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000594xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
595{
596 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
597 xmlParserInputPtr input;
598 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000599 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000600
601 buffer[0] = 0;
602 input = ctxt->input;
603 if ((input->filename == NULL) && (ctxt->inputNr > 1))
604 input = ctxt->inputTab[ctxt->inputNr - 2];
605
606 xmlHTMLPrintFileInfo(input);
607
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000608 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000609 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000610 len = strlen(buffer);
611 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000612 va_end(args);
613 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000614 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000615
616 xmlHTMLPrintFileContext(input);
617 xmlHTMLEncodeSend();
618}
619
620/************************************************************************
621 * *
622 * Shell Interface *
623 * *
624 ************************************************************************/
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000625#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000626/**
627 * xmlShellReadline:
628 * @prompt: the prompt value
629 *
630 * Read a string
631 *
632 * Returns a pointer to it or NULL on EOF the caller is expected to
633 * free the returned string.
634 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000635static char *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000636xmlShellReadline(char *prompt) {
637#ifdef HAVE_LIBREADLINE
638 char *line_read;
639
640 /* Get a line from the user. */
641 line_read = readline (prompt);
642
643 /* If the line has any text in it, save it on the history. */
644 if (line_read && *line_read)
645 add_history (line_read);
646
647 return (line_read);
648#else
649 char line_read[501];
Daniel Veillard29e43992001-12-13 22:21:58 +0000650 char *ret;
651 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000652
653 if (prompt != NULL)
654 fprintf(stdout, "%s", prompt);
655 if (!fgets(line_read, 500, stdin))
656 return(NULL);
657 line_read[500] = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000658 len = strlen(line_read);
659 ret = (char *) malloc(len + 1);
660 if (ret != NULL) {
661 memcpy (ret, line_read, len + 1);
662 }
663 return(ret);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000664#endif
665}
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000666#endif /* LIBXML_DEBUG_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000667
668/************************************************************************
669 * *
Daniel Veillard5e873c42000-04-12 13:27:38 +0000670 * I/O Interfaces *
671 * *
672 ************************************************************************/
673
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000674static int myRead(FILE *f, char * buf, int len) {
675 return(fread(buf, 1, len, f));
Daniel Veillard5e873c42000-04-12 13:27:38 +0000676}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000677static void myClose(FILE *f) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000678 if (f != stdin) {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000679 fclose(f);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000680 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000681}
682
Daniel Veillard81273902003-09-30 00:43:48 +0000683#ifdef LIBXML_READER_ENABLED
Daniel Veillard5e873c42000-04-12 13:27:38 +0000684/************************************************************************
685 * *
Daniel Veillard7704fb12003-01-03 16:19:51 +0000686 * Stream Test processing *
687 * *
688 ************************************************************************/
Daniel Veillard7704fb12003-01-03 16:19:51 +0000689static void processNode(xmlTextReaderPtr reader) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000690 const xmlChar *name, *value;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000691
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000692 name = xmlTextReaderConstName(reader);
Daniel Veillarde59494f2003-01-04 16:35:29 +0000693 if (name == NULL)
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000694 name = BAD_CAST "--";
Daniel Veillard99737f52003-03-22 14:55:50 +0000695
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000696 value = xmlTextReaderConstValue(reader);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000697
Daniel Veillard99737f52003-03-22 14:55:50 +0000698 printf("%d %d %s %d %d",
Daniel Veillarde59494f2003-01-04 16:35:29 +0000699 xmlTextReaderDepth(reader),
700 xmlTextReaderNodeType(reader),
701 name,
Daniel Veillard99737f52003-03-22 14:55:50 +0000702 xmlTextReaderIsEmptyElement(reader),
703 xmlTextReaderHasValue(reader));
Daniel Veillarde59494f2003-01-04 16:35:29 +0000704 if (value == NULL)
705 printf("\n");
706 else {
707 printf(" %s\n", value);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000708 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000709#ifdef LIBXML_PATTERN_ENABLED
710 if (patternc) {
711 if (xmlPatternMatch(patternc, xmlTextReaderCurrentNode(reader)) == 1) {
712 printf("Node matches pattern %s\n", pattern);
713 }
714 }
715#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000716}
717
718static void streamFile(char *filename) {
719 xmlTextReaderPtr reader;
720 int ret;
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000721#ifdef HAVE_SYS_MMAN_H
722 int fd = -1;
723 struct stat info;
724 const char *base = NULL;
725 xmlParserInputBufferPtr input = NULL;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000726
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000727 if (memory) {
728 if (stat(filename, &info) < 0)
729 return;
730 if ((fd = open(filename, O_RDONLY)) < 0)
731 return;
732 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
733 if (base == (void *) MAP_FAILED)
734 return;
735
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000736 reader = xmlReaderForMemory(base, info.st_size, filename,
737 NULL, options);
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000738 } else
739#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000740 reader = xmlReaderForFile(filename, NULL, options);
741
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000742
Daniel Veillard7704fb12003-01-03 16:19:51 +0000743 if (reader != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +0000744#ifdef LIBXML_VALID_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000745 if (valid)
746 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +0000747 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000748#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce192eb2003-04-16 15:58:05 +0000749 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000750#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +0000751 if (relaxng != NULL) {
Daniel Veillard81514ba2003-09-16 23:17:26 +0000752 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000753 startTimer();
754 }
755 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
756 if (ret < 0) {
757 xmlGenericError(xmlGenericErrorContext,
758 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +0000759 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +0000760 relaxng = NULL;
761 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000762 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000763 endTimer("Compiling the schemas");
764 }
765 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000766#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000767
768 /*
769 * Process all nodes in sequence
770 */
Daniel Veillard81514ba2003-09-16 23:17:26 +0000771 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000772 startTimer();
773 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000774 ret = xmlTextReaderRead(reader);
775 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000776 if ((debug)
777#ifdef LIBXML_PATTERN_ENABLED
778 || (patternc)
779#endif
780 )
Daniel Veillard7704fb12003-01-03 16:19:51 +0000781 processNode(reader);
782 ret = xmlTextReaderRead(reader);
783 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000784 if ((timing) && (!repeat)) {
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000785#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf54cd532004-02-25 11:52:31 +0000786 if (relaxng != NULL)
Daniel Veillard49138f12004-02-19 12:58:36 +0000787 endTimer("Parsing and validating");
788 else
Daniel Veillardf54cd532004-02-25 11:52:31 +0000789#endif
Daniel Veillard4432df22003-09-28 18:58:27 +0000790#ifdef LIBXML_VALID_ENABLED
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000791 if (valid)
Daniel Veillardce192eb2003-04-16 15:58:05 +0000792 endTimer("Parsing and validating");
793 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000794#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +0000795 endTimer("Parsing");
Daniel Veillardce192eb2003-04-16 15:58:05 +0000796 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000797
Daniel Veillard4432df22003-09-28 18:58:27 +0000798#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf6bad792003-04-11 19:38:54 +0000799 if (valid) {
800 if (xmlTextReaderIsValid(reader) != 1) {
801 xmlGenericError(xmlGenericErrorContext,
802 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +0000803 progresult = XMLLINT_ERR_VALID;
Daniel Veillardf6bad792003-04-11 19:38:54 +0000804 }
805 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000806#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000807#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +0000808 if (relaxng != NULL) {
809 if (xmlTextReaderIsValid(reader) != 1) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +0000810 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +0000811 progresult = XMLLINT_ERR_VALID;
Daniel Veillardf4e55762003-04-15 23:32:22 +0000812 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +0000813 fprintf(stderr, "%s validates\n", filename);
Daniel Veillardf4e55762003-04-15 23:32:22 +0000814 }
815 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000816#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000817 /*
818 * Done, cleanup and status
819 */
820 xmlFreeTextReader(reader);
821 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +0000822 fprintf(stderr, "%s : failed to parse\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +0000823 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000824 }
825 } else {
826 fprintf(stderr, "Unable to open %s\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +0000827 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000828 }
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000829#ifdef HAVE_SYS_MMAN_H
830 if (memory) {
831 xmlFreeParserInputBuffer(input);
832 munmap((char *) base, info.st_size);
833 close(fd);
834 }
835#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000836}
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000837
838static void walkDoc(xmlDocPtr doc) {
839 xmlTextReaderPtr reader;
840 int ret;
841
842 reader = xmlReaderWalker(doc);
843 if (reader != NULL) {
844 if ((timing) && (!repeat)) {
845 startTimer();
846 }
847 ret = xmlTextReaderRead(reader);
848 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000849 if ((debug)
850#ifdef LIBXML_PATTERN_ENABLED
851 || (patternc)
852#endif
853 )
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000854 processNode(reader);
855 ret = xmlTextReaderRead(reader);
856 }
857 if ((timing) && (!repeat)) {
858 endTimer("walking through the doc");
859 }
860 xmlFreeTextReader(reader);
861 if (ret != 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +0000862 fprintf(stderr, "failed to walk through the doc\n");
William M. Brack8304d872004-06-08 13:29:32 +0000863 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000864 }
865 } else {
866 fprintf(stderr, "Failed to crate a reader from the document\n");
William M. Brack8304d872004-06-08 13:29:32 +0000867 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000868 }
869}
Daniel Veillard81273902003-09-30 00:43:48 +0000870#endif /* LIBXML_READER_ENABLED */
Daniel Veillard7704fb12003-01-03 16:19:51 +0000871
872/************************************************************************
873 * *
874 * Tree Test processing *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000875 * *
876 ************************************************************************/
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000877static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
Daniel Veillard652327a2003-09-29 18:02:38 +0000878 xmlDocPtr doc = NULL;
879#ifdef LIBXML_TREE_ENABLED
880 xmlDocPtr tmp;
881#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000882
Daniel Veillard48b2f892001-02-25 16:11:03 +0000883 if ((timing) && (!repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +0000884 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000885
886
Daniel Veillard652327a2003-09-29 18:02:38 +0000887#ifdef LIBXML_TREE_ENABLED
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000888 if (filename == NULL) {
889 if (generate) {
890 xmlNodePtr n;
891
892 doc = xmlNewDoc(BAD_CAST "1.0");
893 n = xmlNewNode(NULL, BAD_CAST "info");
894 xmlNodeSetContent(n, BAD_CAST "abc");
895 xmlDocSetRootElement(doc, n);
896 }
897 }
Daniel Veillard652327a2003-09-29 18:02:38 +0000898#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000899#ifdef LIBXML_HTML_ENABLED
Daniel Veillard73b013f2003-09-30 12:36:01 +0000900#ifdef LIBXML_PUSH_ENABLED
William M. Brack78637da2003-07-31 14:47:38 +0000901 else if ((html) && (push)) {
902 FILE *f;
903
William M. Brack3403add2004-06-27 02:07:51 +0000904#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
905 f = fopen(filename, "rb");
906#else
907 f = fopen(filename, "r");
908#endif
William M. Brack78637da2003-07-31 14:47:38 +0000909 if (f != NULL) {
910 int res, size = 3;
911 char chars[4096];
912 htmlParserCtxtPtr ctxt;
913
914 /* if (repeat) */
915 size = 4096;
916 res = fread(chars, 1, 4, f);
917 if (res > 0) {
918 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
William M. Brack1d75c8a2003-10-27 13:48:16 +0000919 chars, res, filename, XML_CHAR_ENCODING_NONE);
William M. Brack78637da2003-07-31 14:47:38 +0000920 while ((res = fread(chars, 1, size, f)) > 0) {
921 htmlParseChunk(ctxt, chars, res, 0);
922 }
923 htmlParseChunk(ctxt, chars, 0, 1);
924 doc = ctxt->myDoc;
925 htmlFreeParserCtxt(ctxt);
926 }
927 fclose(f);
928 }
929 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000930#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000931 else if (html) {
Daniel Veillard9475a352003-09-26 12:47:50 +0000932 doc = htmlReadFile(filename, NULL, options);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000933 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000934#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000935 else {
Daniel Veillard73b013f2003-09-30 12:36:01 +0000936#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000937 /*
938 * build an XML tree from a string;
939 */
940 if (push) {
941 FILE *f;
942
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000943 /* '-' Usually means stdin -<sven@zen.org> */
944 if ((filename[0] == '-') && (filename[1] == 0)) {
945 f = stdin;
946 } else {
William M. Brack3403add2004-06-27 02:07:51 +0000947#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
948 f = fopen(filename, "rb");
949#else
950 f = fopen(filename, "r");
951#endif
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000952 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000953 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +0000954 int ret;
Daniel Veillarda880b122003-04-21 21:36:41 +0000955 int res, size = 1024;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000956 char chars[1024];
957 xmlParserCtxtPtr ctxt;
958
Daniel Veillarda880b122003-04-21 21:36:41 +0000959 /* if (repeat) size = 1024; */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000960 res = fread(chars, 1, 4, f);
961 if (res > 0) {
962 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
963 chars, res, filename);
Daniel Veillard500a1de2004-03-22 15:22:58 +0000964 xmlCtxtUseOptions(ctxt, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000965 while ((res = fread(chars, 1, size, f)) > 0) {
966 xmlParseChunk(ctxt, chars, res, 0);
967 }
968 xmlParseChunk(ctxt, chars, 0, 1);
969 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +0000970 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000971 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +0000972 if (!ret) {
973 xmlFreeDoc(doc);
974 doc = NULL;
975 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000976 }
977 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000978 } else
979#endif /* LIBXML_PUSH_ENABLED */
980 if (testIO) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000981 if ((filename[0] == '-') && (filename[1] == 0)) {
Daniel Veillard60942de2003-09-25 21:05:58 +0000982 doc = xmlReadFd(0, NULL, NULL, options);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000983 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000984 FILE *f;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000985
William M. Brack3403add2004-06-27 02:07:51 +0000986#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
987 f = fopen(filename, "rb");
988#else
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000989 f = fopen(filename, "r");
William M. Brack3403add2004-06-27 02:07:51 +0000990#endif
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000991 if (f != NULL) {
992 if (rectxt == NULL)
993 doc = xmlReadIO((xmlInputReadCallback) myRead,
994 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +0000995 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000996 else
997 doc = xmlCtxtReadIO(rectxt,
998 (xmlInputReadCallback) myRead,
999 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +00001000 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001001 } else
Daniel Veillard5e873c42000-04-12 13:27:38 +00001002 doc = NULL;
Daniel Veillard5e873c42000-04-12 13:27:38 +00001003 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001004 } else if (htmlout) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001005 xmlParserCtxtPtr ctxt;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001006
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001007 if (rectxt == NULL)
1008 ctxt = xmlNewParserCtxt();
1009 else
1010 ctxt = rectxt;
Daniel Veillard88a172f2000-08-04 18:23:10 +00001011 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001012 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +00001013 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001014 ctxt->sax->error = xmlHTMLError;
1015 ctxt->sax->warning = xmlHTMLWarning;
1016 ctxt->vctxt.error = xmlHTMLValidityError;
1017 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001018
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001019 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001020
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001021 if (rectxt == NULL)
1022 xmlFreeParserCtxt(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001023 }
Daniel Veillard46e370e2000-07-21 20:32:03 +00001024#ifdef HAVE_SYS_MMAN_H
1025 } else if (memory) {
1026 int fd;
1027 struct stat info;
1028 const char *base;
1029 if (stat(filename, &info) < 0)
1030 return;
1031 if ((fd = open(filename, O_RDONLY)) < 0)
1032 return;
1033 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +00001034 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +00001035 return;
1036
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001037 if (rectxt == NULL)
Daniel Veillard60942de2003-09-25 21:05:58 +00001038 doc = xmlReadMemory((char *) base, info.st_size,
1039 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001040 else
Daniel Veillard60942de2003-09-25 21:05:58 +00001041 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
1042 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001043
Daniel Veillard46e370e2000-07-21 20:32:03 +00001044 munmap((char *) base, info.st_size);
1045#endif
Daniel Veillard4432df22003-09-28 18:58:27 +00001046#ifdef LIBXML_VALID_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00001047 } else if (valid) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001048 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001049
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001050 if (rectxt == NULL)
1051 ctxt = xmlNewParserCtxt();
1052 else
1053 ctxt = rectxt;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001054 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001055 doc = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001056 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001057 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
1058
1059 if (ctxt->valid == 0)
William M. Brack8304d872004-06-08 13:29:32 +00001060 progresult = XMLLINT_ERR_RDFILE;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001061 if (rectxt == NULL)
1062 xmlFreeParserCtxt(ctxt);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001063 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001064#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00001065 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001066 if (rectxt != NULL)
1067 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
1068 else
1069 doc = xmlReadFile(filename, NULL, options);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001070 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001071 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001072
Daniel Veillard88a172f2000-08-04 18:23:10 +00001073 /*
1074 * If we don't have a document we might as well give up. Do we
1075 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001076 if (doc == NULL) {
William M. Brack8304d872004-06-08 13:29:32 +00001077 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard88a172f2000-08-04 18:23:10 +00001078 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001079 }
1080
Daniel Veillard48b2f892001-02-25 16:11:03 +00001081 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001082 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001083 }
1084
Daniel Veillard29e43992001-12-13 22:21:58 +00001085 /*
1086 * Remove DOCTYPE nodes
1087 */
1088 if (dropdtd) {
1089 xmlDtdPtr dtd;
1090
1091 dtd = xmlGetIntSubset(doc);
1092 if (dtd != NULL) {
1093 xmlUnlinkNode((xmlNodePtr)dtd);
1094 xmlFreeDtd(dtd);
1095 }
1096 }
1097
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001098#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +00001099 if (xinclude) {
1100 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001101 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001102 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +00001103 xmlXIncludeProcessFlags(doc, options);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001104 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001105 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001106 }
1107 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001108#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +00001109
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001110#ifdef LIBXML_DEBUG_ENABLED
1111 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001112 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001113 */
1114 if (shell)
1115 xmlShell(doc, filename, xmlShellReadline, stdout);
1116#endif
1117
Daniel Veillard652327a2003-09-29 18:02:38 +00001118#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001119 /*
1120 * test intermediate copy if needed.
1121 */
1122 if (copy) {
1123 tmp = doc;
1124 doc = xmlCopyDoc(doc, 1);
1125 xmlFreeDoc(tmp);
1126 }
Daniel Veillard652327a2003-09-29 18:02:38 +00001127#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001128
Daniel Veillard4432df22003-09-28 18:58:27 +00001129#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001130 if ((insert) && (!html)) {
1131 const xmlChar* list[256];
1132 int nb, i;
1133 xmlNodePtr node;
1134
1135 if (doc->children != NULL) {
1136 node = doc->children;
1137 while ((node != NULL) && (node->last == NULL)) node = node->next;
1138 if (node != NULL) {
1139 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
1140 if (nb < 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001141 fprintf(stderr, "could not get valid list of elements\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001142 } else if (nb == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001143 fprintf(stderr, "No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001144 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001145 fprintf(stderr, "%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001146 nb);
1147 for (i = 0;i < nb;i++) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001148 fprintf(stderr, "%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001149 }
1150 }
1151 }
1152 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001153 }else
1154#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001155#ifdef LIBXML_READER_ENABLED
1156 if (walker) {
1157 walkDoc(doc);
1158 }
1159#endif /* LIBXML_READER_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001160#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001161 if (noout == 0) {
Daniel Veillard3df01182003-12-10 10:17:51 +00001162 int ret;
1163
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001164 /*
1165 * print it.
1166 */
1167#ifdef LIBXML_DEBUG_ENABLED
1168 if (!debug) {
1169#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00001170 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001171 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001172 }
Daniel Veillard656ce942004-04-30 23:11:45 +00001173#ifdef LIBXML_HTML_ENABLED
Daniel Veillard42fd4122003-11-04 08:47:48 +00001174 if ((html) && (!xmlout)) {
1175 if (compress) {
1176 htmlSaveFile(output ? output : "-", doc);
1177 }
1178 else if (encoding != NULL) {
1179 if ( format ) {
1180 htmlSaveFileFormat(output ? output : "-", doc, encoding, 1);
1181 }
1182 else {
1183 htmlSaveFileFormat(output ? output : "-", doc, encoding, 0);
1184 }
1185 }
1186 else if (format) {
1187 htmlSaveFileFormat(output ? output : "-", doc, NULL, 1);
1188 }
1189 else {
1190 FILE *out;
1191 if (output == NULL)
1192 out = stdout;
1193 else {
1194 out = fopen(output,"wb");
1195 }
1196 if (out != NULL) {
1197 if (htmlDocDump(out, doc) < 0)
William M. Brack8304d872004-06-08 13:29:32 +00001198 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00001199
1200 if (output != NULL)
1201 fclose(out);
1202 } else {
1203 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00001204 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00001205 }
1206 }
1207 if ((timing) && (!repeat)) {
1208 endTimer("Saving");
1209 }
1210 } else
1211#endif
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001212#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001213 if (memory) {
1214 xmlChar *result;
1215 int len;
1216
1217 if (encoding != NULL) {
Daniel Veillardd536f702001-11-08 17:32:47 +00001218 if ( format ) {
1219 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
1220 } else {
1221 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
1222 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001223 } else {
Daniel Veillard90493a92001-08-14 14:12:47 +00001224 if (format)
1225 xmlDocDumpFormatMemory(doc, &result, &len, 1);
1226 else
1227 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001228 }
1229 if (result == NULL) {
1230 fprintf(stderr, "Failed to save\n");
1231 } else {
1232 write(1, result, len);
1233 xmlFree(result);
1234 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001235 } else
1236#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001237 if (compress) {
1238 xmlSaveFile(output ? output : "-", doc);
1239 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001240 else if (encoding != NULL) {
1241 if ( format ) {
Daniel Veillard3df01182003-12-10 10:17:51 +00001242 ret = xmlSaveFormatFileEnc(output ? output : "-", doc,
1243 encoding, 1);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001244 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001245 else {
Daniel Veillard3df01182003-12-10 10:17:51 +00001246 ret = xmlSaveFileEnc(output ? output : "-", doc, encoding);
1247 }
1248 if (ret < 0) {
1249 fprintf(stderr, "failed save to %s\n",
1250 output ? output : "-");
William M. Brack8304d872004-06-08 13:29:32 +00001251 progresult = XMLLINT_ERR_OUT;
Daniel Veillardd536f702001-11-08 17:32:47 +00001252 }
1253 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001254 else if (format) {
Daniel Veillard3df01182003-12-10 10:17:51 +00001255 ret = xmlSaveFormatFile(output ? output : "-", doc, 1);
1256 if (ret < 0) {
1257 fprintf(stderr, "failed save to %s\n",
1258 output ? output : "-");
William M. Brack8304d872004-06-08 13:29:32 +00001259 progresult = XMLLINT_ERR_OUT;
Daniel Veillard3df01182003-12-10 10:17:51 +00001260 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001261 }
1262 else {
1263 FILE *out;
1264 if (output == NULL)
1265 out = stdout;
1266 else {
1267 out = fopen(output,"wb");
1268 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001269 if (out != NULL) {
Daniel Veillard828ce832003-10-08 19:19:10 +00001270 if (xmlDocDump(out, doc) < 0)
William M. Brack8304d872004-06-08 13:29:32 +00001271 progresult = XMLLINT_ERR_OUT;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001272
Daniel Veillard05d987b2003-10-08 11:54:57 +00001273 if (output != NULL)
1274 fclose(out);
1275 } else {
1276 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00001277 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00001278 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001279 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001280 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001281 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001282 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001283#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001284 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001285 FILE *out;
1286 if (output == NULL)
1287 out = stdout;
1288 else {
1289 out = fopen(output,"wb");
1290 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001291 if (out != NULL) {
1292 xmlDebugDumpDocument(out, doc);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001293
Daniel Veillard05d987b2003-10-08 11:54:57 +00001294 if (output != NULL)
1295 fclose(out);
1296 } else {
1297 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00001298 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00001299 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001300 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001301#endif
1302 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001303#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001304
Daniel Veillard4432df22003-09-28 18:58:27 +00001305#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001306 /*
1307 * A posteriori validation test
1308 */
Daniel Veillard66f68e72003-08-18 16:39:51 +00001309 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
Daniel Veillardcd429612000-10-11 15:57:05 +00001310 xmlDtdPtr dtd;
1311
Daniel Veillard48b2f892001-02-25 16:11:03 +00001312 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001313 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001314 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00001315 if (dtdvalid != NULL)
1316 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
1317 else
1318 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001319 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001320 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001321 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001322 if (dtd == NULL) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001323 if (dtdvalid != NULL)
1324 xmlGenericError(xmlGenericErrorContext,
1325 "Could not parse DTD %s\n", dtdvalid);
1326 else
1327 xmlGenericError(xmlGenericErrorContext,
1328 "Could not parse DTD %s\n", dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00001329 progresult = XMLLINT_ERR_DTD;
Daniel Veillardcd429612000-10-11 15:57:05 +00001330 } else {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001331 xmlValidCtxtPtr cvp;
1332
1333 if ((cvp = xmlNewValidCtxt()) == NULL) {
1334 xmlGenericError(xmlGenericErrorContext,
1335 "Couldn't allocate validation context\n");
1336 exit(-1);
1337 }
1338 cvp->userData = (void *) stderr;
1339 cvp->error = (xmlValidityErrorFunc) fprintf;
1340 cvp->warning = (xmlValidityWarningFunc) fprintf;
1341
Daniel Veillard48b2f892001-02-25 16:11:03 +00001342 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001343 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001344 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001345 if (!xmlValidateDtd(cvp, doc, dtd)) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001346 if (dtdvalid != NULL)
1347 xmlGenericError(xmlGenericErrorContext,
1348 "Document %s does not validate against %s\n",
1349 filename, dtdvalid);
1350 else
1351 xmlGenericError(xmlGenericErrorContext,
1352 "Document %s does not validate against %s\n",
1353 filename, dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00001354 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00001355 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001356 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001357 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001358 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001359 xmlFreeValidCtxt(cvp);
Daniel Veillardcd429612000-10-11 15:57:05 +00001360 xmlFreeDtd(dtd);
1361 }
1362 } else if (postvalid) {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001363 xmlValidCtxtPtr cvp;
1364
1365 if ((cvp = xmlNewValidCtxt()) == NULL) {
1366 xmlGenericError(xmlGenericErrorContext,
1367 "Couldn't allocate validation context\n");
1368 exit(-1);
1369 }
1370
Daniel Veillard48b2f892001-02-25 16:11:03 +00001371 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001372 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001373 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001374 cvp->userData = (void *) stderr;
1375 cvp->error = (xmlValidityErrorFunc) fprintf;
1376 cvp->warning = (xmlValidityWarningFunc) fprintf;
1377 if (!xmlValidateDocument(cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001378 xmlGenericError(xmlGenericErrorContext,
1379 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001380 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00001381 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001382 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001383 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001384 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001385 xmlFreeValidCtxt(cvp);
Daniel Veillard4432df22003-09-28 18:58:27 +00001386 }
1387#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00001388#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001389 if (relaxngschemas != NULL) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001390 xmlRelaxNGValidCtxtPtr ctxt;
1391 int ret;
1392
Daniel Veillard42f12e92003-03-07 18:32:59 +00001393 if ((timing) && (!repeat)) {
1394 startTimer();
1395 }
1396
Daniel Veillard71531f32003-02-05 13:19:53 +00001397 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
1398 xmlRelaxNGSetValidErrors(ctxt,
1399 (xmlRelaxNGValidityErrorFunc) fprintf,
1400 (xmlRelaxNGValidityWarningFunc) fprintf,
1401 stderr);
1402 ret = xmlRelaxNGValidateDoc(ctxt, doc);
1403 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001404 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard71531f32003-02-05 13:19:53 +00001405 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001406 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001407 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00001408 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001409 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard71531f32003-02-05 13:19:53 +00001410 filename);
William M. Brack8304d872004-06-08 13:29:32 +00001411 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00001412 }
1413 xmlRelaxNGFreeValidCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001414 if ((timing) && (!repeat)) {
1415 endTimer("Validating");
1416 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001417 } else if (wxschemas != NULL) {
1418 xmlSchemaValidCtxtPtr ctxt;
1419 int ret;
1420
1421 if ((timing) && (!repeat)) {
1422 startTimer();
1423 }
1424
1425 ctxt = xmlSchemaNewValidCtxt(wxschemas);
1426 xmlSchemaSetValidErrors(ctxt,
1427 (xmlSchemaValidityErrorFunc) fprintf,
1428 (xmlSchemaValidityWarningFunc) fprintf,
1429 stderr);
1430 ret = xmlSchemaValidateDoc(ctxt, doc);
1431 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001432 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001433 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001434 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001435 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001436 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001437 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001438 filename);
William M. Brack8304d872004-06-08 13:29:32 +00001439 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001440 }
1441 xmlSchemaFreeValidCtxt(ctxt);
1442 if ((timing) && (!repeat)) {
1443 endTimer("Validating");
1444 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001445 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001446#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001447
1448#ifdef LIBXML_DEBUG_ENABLED
1449 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001450 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001451#endif
1452
1453 /*
1454 * free it.
1455 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00001456 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001457 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001458 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001459 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001460 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001461 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001462 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001463}
1464
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001465/************************************************************************
1466 * *
1467 * Usage and Main *
1468 * *
1469 ************************************************************************/
1470
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001471static void showVersion(const char *name) {
1472 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
1473 fprintf(stderr, " compiled with: ");
Daniel Veillard4432df22003-09-28 18:58:27 +00001474#ifdef LIBXML_VALID_ENABLED
1475 fprintf(stderr, "DTDValid ");
1476#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001477#ifdef LIBXML_FTP_ENABLED
1478 fprintf(stderr, "FTP ");
1479#endif
1480#ifdef LIBXML_HTTP_ENABLED
1481 fprintf(stderr, "HTTP ");
1482#endif
1483#ifdef LIBXML_HTML_ENABLED
1484 fprintf(stderr, "HTML ");
1485#endif
1486#ifdef LIBXML_C14N_ENABLED
1487 fprintf(stderr, "C14N ");
1488#endif
1489#ifdef LIBXML_CATALOG_ENABLED
1490 fprintf(stderr, "Catalog ");
1491#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001492#ifdef LIBXML_XPATH_ENABLED
1493 fprintf(stderr, "XPath ");
1494#endif
1495#ifdef LIBXML_XPTR_ENABLED
1496 fprintf(stderr, "XPointer ");
1497#endif
1498#ifdef LIBXML_XINCLUDE_ENABLED
1499 fprintf(stderr, "XInclude ");
1500#endif
1501#ifdef LIBXML_ICONV_ENABLED
1502 fprintf(stderr, "Iconv ");
1503#endif
1504#ifdef DEBUG_MEMORY_LOCATION
1505 fprintf(stderr, "MemDebug ");
1506#endif
1507#ifdef LIBXML_UNICODE_ENABLED
1508 fprintf(stderr, "Unicode ");
1509#endif
1510#ifdef LIBXML_REGEXP_ENABLED
1511 fprintf(stderr, "Regexps ");
1512#endif
1513#ifdef LIBXML_AUTOMATA_ENABLED
1514 fprintf(stderr, "Automata ");
1515#endif
1516#ifdef LIBXML_SCHEMAS_ENABLED
1517 fprintf(stderr, "Schemas ");
1518#endif
1519 fprintf(stderr, "\n");
1520}
1521
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001522static void usage(const char *name) {
1523 printf("Usage : %s [options] XMLfiles ...\n", name);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001524#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001525 printf("\tParse the XML files and output the result of the parsing\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001526#else
1527 printf("\tParse the XML files\n");
1528#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001529 printf("\t--version : display the version of the XML library used\n");
1530#ifdef LIBXML_DEBUG_ENABLED
1531 printf("\t--debug : dump a debug tree of the in-memory document\n");
1532 printf("\t--shell : run a navigating shell\n");
1533 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00001534#else
Daniel Veillard81273902003-09-30 00:43:48 +00001535#ifdef LIBXML_READER_ENABLED
Daniel Veillard8326e732003-01-07 00:19:07 +00001536 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001537#endif /* LIBXML_READER_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001538#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001539#ifdef LIBXML_TREE_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001540 printf("\t--copy : used to test the internal copy implementation\n");
Daniel Veillard652327a2003-09-29 18:02:38 +00001541#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001542 printf("\t--recover : output what was parsable on broken XML documents\n");
1543 printf("\t--noent : substitute entity references by their value\n");
1544 printf("\t--noout : don't output the result tree\n");
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001545 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001546 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001547 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001548#ifdef LIBXML_VALID_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001549 printf("\t--valid : validate the document in addition to std well-formed check\n");
1550 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
1551 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
Daniel Veillard66f68e72003-08-18 16:39:51 +00001552 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001553#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001554 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001555 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001556 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
1557 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001558#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001559#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001560 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001561#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001562#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001563#ifdef LIBXML_HTML_ENABLED
1564 printf("\t--html : use the HTML parser\n");
Daniel Veillard42fd4122003-11-04 08:47:48 +00001565 printf("\t--xmlout : force to use the XML serializer when using --html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001566#endif
Daniel Veillard73b013f2003-09-30 12:36:01 +00001567#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001568 printf("\t--push : use the push mode of the parser\n");
Daniel Veillard73b013f2003-09-30 12:36:01 +00001569#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001570#ifdef HAVE_SYS_MMAN_H
1571 printf("\t--memory : parse from memory\n");
1572#endif
Daniel Veillard87076042004-05-03 22:54:49 +00001573 printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001574 printf("\t--nowarning : do not emit warnings from parser/validator\n");
1575 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001576 printf("\t--nocdata : replace cdata section with text nodes\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001577#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001578 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001579 printf("\t--encode encoding : output in the given encoding\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001580 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
1581#endif /* LIBXML_OUTPUT_ENABLED */
1582 printf("\t--nsclean : remove redundant namespace declarations\n");
1583 printf("\t--testIO : test user I/O support\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001584#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00001585 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
1586 printf("\t otherwise XML Catalogs starting from \n");
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001587 printf("\t %s are activated by default\n", XML_XML_DEFAULT_CATALOG);
Daniel Veillard05c13a22001-09-09 08:38:09 +00001588 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001589#endif
1590 printf("\t--auto : generate a small doc on the fly\n");
1591#ifdef LIBXML_XINCLUDE_ENABLED
1592 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001593#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001594 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00001595 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard81273902003-09-30 00:43:48 +00001596#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001597 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001598 printf("\t--walker : create a reader and walk though the resulting doc\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001599#endif /* LIBXML_READER_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001600#ifdef LIBXML_PATTERN_ENABLED
1601 printf("\t--pattern pattern_value : test the pattern support\n");
1602#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001603 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillardef4d3bc2003-02-07 12:38:22 +00001604#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard71531f32003-02-05 13:19:53 +00001605 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001606 printf("\t--schema schema : do validation against the WXS schema\n");
Daniel Veillard71531f32003-02-05 13:19:53 +00001607#endif
Daniel Veillarda42f25f2002-01-25 14:15:40 +00001608 printf("\nLibxml project home page: http://xmlsoft.org/\n");
1609 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001610}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001611
1612static void registerNode(xmlNodePtr node)
1613{
1614 node->_private = malloc(sizeof(long));
1615 *(long*)node->_private = (long) 0x81726354;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00001616 nbregister++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001617}
1618
1619static void deregisterNode(xmlNodePtr node)
1620{
1621 assert(node->_private != NULL);
1622 assert(*(long*)node->_private == (long) 0x81726354);
1623 free(node->_private);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00001624 nbregister--;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001625}
1626
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001627int
1628main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001629 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001630 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00001631 int version = 0;
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001632 const char* indent;
1633
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001634 if (argc <= 1) {
1635 usage(argv[0]);
1636 return(1);
1637 }
Daniel Veillardbe803962000-06-28 23:40:59 +00001638 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001639 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001640 if (!strcmp(argv[i], "-"))
1641 break;
1642
1643 if (argv[i][0] != '-')
1644 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001645 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
1646 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00001647 else
1648#ifdef LIBXML_DEBUG_ENABLED
1649 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001650 (!strcmp(argv[i], "--shell"))) {
1651 shell++;
1652 noout = 1;
1653 } else
1654#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001655#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001656 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
1657 copy++;
Daniel Veillard652327a2003-09-29 18:02:38 +00001658 else
1659#endif /* LIBXML_TREE_ENABLED */
1660 if ((!strcmp(argv[i], "-recover")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001661 (!strcmp(argv[i], "--recover"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001662 recovery++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001663 options |= XML_PARSE_RECOVER;
1664 } else if ((!strcmp(argv[i], "-noent")) ||
1665 (!strcmp(argv[i], "--noent"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001666 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001667 options |= XML_PARSE_NOENT;
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001668 } else if ((!strcmp(argv[i], "-nsclean")) ||
1669 (!strcmp(argv[i], "--nsclean"))) {
1670 options |= XML_PARSE_NSCLEAN;
1671 } else if ((!strcmp(argv[i], "-nocdata")) ||
1672 (!strcmp(argv[i], "--nocdata"))) {
1673 options |= XML_PARSE_NOCDATA;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001674 } else if ((!strcmp(argv[i], "-nodict")) ||
1675 (!strcmp(argv[i], "--nodict"))) {
1676 options |= XML_PARSE_NODICT;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001677 } else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00001678 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001679 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00001680 version = 1;
1681 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001682 (!strcmp(argv[i], "--noout")))
1683 noout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001684#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001685 else if ((!strcmp(argv[i], "-o")) ||
1686 (!strcmp(argv[i], "-output")) ||
1687 (!strcmp(argv[i], "--output"))) {
1688 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00001689 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001690 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001691#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001692 else if ((!strcmp(argv[i], "-htmlout")) ||
1693 (!strcmp(argv[i], "--htmlout")))
1694 htmlout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001695 else if ((!strcmp(argv[i], "-nowrap")) ||
1696 (!strcmp(argv[i], "--nowrap")))
1697 nowrap++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001698#ifdef LIBXML_HTML_ENABLED
1699 else if ((!strcmp(argv[i], "-html")) ||
1700 (!strcmp(argv[i], "--html"))) {
1701 html++;
1702 }
Daniel Veillard42fd4122003-11-04 08:47:48 +00001703 else if ((!strcmp(argv[i], "-xmlout")) ||
1704 (!strcmp(argv[i], "--xmlout"))) {
1705 xmlout++;
1706 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001707#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001708 else if ((!strcmp(argv[i], "-loaddtd")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001709 (!strcmp(argv[i], "--loaddtd"))) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001710 loaddtd++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001711 options |= XML_PARSE_DTDLOAD;
1712 } else if ((!strcmp(argv[i], "-dtdattr")) ||
Daniel Veillard48da9102001-08-07 01:10:10 +00001713 (!strcmp(argv[i], "--dtdattr"))) {
1714 loaddtd++;
1715 dtdattrs++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001716 options |= XML_PARSE_DTDATTR;
Daniel Veillard4432df22003-09-28 18:58:27 +00001717 }
1718#ifdef LIBXML_VALID_ENABLED
1719 else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001720 (!strcmp(argv[i], "--valid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001721 valid++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001722 options |= XML_PARSE_DTDVALID;
1723 } else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001724 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001725 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001726 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00001727 options |= XML_PARSE_DTDLOAD;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001728 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00001729 (!strcmp(argv[i], "--dtdvalid"))) {
1730 i++;
1731 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001732 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00001733 options |= XML_PARSE_DTDLOAD;
Daniel Veillard66f68e72003-08-18 16:39:51 +00001734 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
1735 (!strcmp(argv[i], "--dtdvalidfpi"))) {
1736 i++;
1737 dtdvalidfpi = argv[i];
1738 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00001739 options |= XML_PARSE_DTDLOAD;
Daniel Veillardcd429612000-10-11 15:57:05 +00001740 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001741#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard29e43992001-12-13 22:21:58 +00001742 else if ((!strcmp(argv[i], "-dropdtd")) ||
1743 (!strcmp(argv[i], "--dropdtd")))
1744 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001745 else if ((!strcmp(argv[i], "-insert")) ||
1746 (!strcmp(argv[i], "--insert")))
1747 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001748 else if ((!strcmp(argv[i], "-timing")) ||
1749 (!strcmp(argv[i], "--timing")))
1750 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001751 else if ((!strcmp(argv[i], "-auto")) ||
1752 (!strcmp(argv[i], "--auto")))
1753 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001754 else if ((!strcmp(argv[i], "-repeat")) ||
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001755 (!strcmp(argv[i], "--repeat"))) {
1756 if (repeat)
1757 repeat *= 10;
1758 else
1759 repeat = 100;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001760 }
1761#ifdef LIBXML_PUSH_ENABLED
1762 else if ((!strcmp(argv[i], "-push")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001763 (!strcmp(argv[i], "--push")))
1764 push++;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001765#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +00001766#ifdef HAVE_SYS_MMAN_H
1767 else if ((!strcmp(argv[i], "-memory")) ||
1768 (!strcmp(argv[i], "--memory")))
1769 memory++;
1770#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00001771 else if ((!strcmp(argv[i], "-testIO")) ||
1772 (!strcmp(argv[i], "--testIO")))
1773 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001774#ifdef LIBXML_XINCLUDE_ENABLED
1775 else if ((!strcmp(argv[i], "-xinclude")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001776 (!strcmp(argv[i], "--xinclude"))) {
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001777 xinclude++;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001778 options |= XML_PARSE_XINCLUDE;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001779 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001780#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001781#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001782#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001783 else if ((!strcmp(argv[i], "-compress")) ||
1784 (!strcmp(argv[i], "--compress"))) {
1785 compress++;
1786 xmlSetCompressMode(9);
1787 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001788#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001789#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001790 else if ((!strcmp(argv[i], "-nowarning")) ||
1791 (!strcmp(argv[i], "--nowarning"))) {
1792 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001793 xmlPedanticParserDefault(0);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001794 options |= XML_PARSE_NOWARNING;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001795 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001796 else if ((!strcmp(argv[i], "-pedantic")) ||
1797 (!strcmp(argv[i], "--pedantic"))) {
1798 xmlGetWarningsDefaultValue = 1;
1799 xmlPedanticParserDefault(1);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001800 options |= XML_PARSE_PEDANTIC;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001801 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001802#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001803 else if ((!strcmp(argv[i], "-debugent")) ||
1804 (!strcmp(argv[i], "--debugent"))) {
1805 debugent++;
1806 xmlParserDebugEntities = 1;
1807 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001808#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00001809#ifdef LIBXML_CATALOG_ENABLED
1810 else if ((!strcmp(argv[i], "-catalogs")) ||
1811 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001812 catalogs++;
1813 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
1814 (!strcmp(argv[i], "--nocatalogs"))) {
1815 nocatalogs++;
Daniel Veillard81418e32001-05-22 15:08:55 +00001816 }
1817#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00001818 else if ((!strcmp(argv[i], "-encode")) ||
1819 (!strcmp(argv[i], "--encode"))) {
1820 i++;
1821 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001822 /*
1823 * OK it's for testing purposes
1824 */
1825 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00001826 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001827 else if ((!strcmp(argv[i], "-noblanks")) ||
1828 (!strcmp(argv[i], "--noblanks"))) {
1829 noblanks++;
1830 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00001831 }
Daniel Veillard87076042004-05-03 22:54:49 +00001832 else if ((!strcmp(argv[i], "-maxmem")) ||
1833 (!strcmp(argv[i], "--maxmem"))) {
1834 i++;
1835 if (sscanf(argv[i], "%d", &maxmem) == 1) {
1836 xmlMemSetup(myFreeFunc, myMallocFunc, myReallocFunc,
1837 myStrdupFunc);
1838 } else {
1839 maxmem = 0;
1840 }
1841 }
Daniel Veillard90493a92001-08-14 14:12:47 +00001842 else if ((!strcmp(argv[i], "-format")) ||
1843 (!strcmp(argv[i], "--format"))) {
1844 noblanks++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001845#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001846 format++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001847#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard90493a92001-08-14 14:12:47 +00001848 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001849 }
Daniel Veillard81273902003-09-30 00:43:48 +00001850#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001851 else if ((!strcmp(argv[i], "-stream")) ||
1852 (!strcmp(argv[i], "--stream"))) {
1853 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001854 }
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001855 else if ((!strcmp(argv[i], "-walker")) ||
1856 (!strcmp(argv[i], "--walker"))) {
1857 walker++;
1858 noout++;
1859 }
Daniel Veillard81273902003-09-30 00:43:48 +00001860#endif /* LIBXML_READER_ENABLED */
1861#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001862 else if ((!strcmp(argv[i], "-sax1")) ||
1863 (!strcmp(argv[i], "--sax1"))) {
1864 sax1++;
1865 }
Daniel Veillard81273902003-09-30 00:43:48 +00001866#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001867 else if ((!strcmp(argv[i], "-chkregister")) ||
1868 (!strcmp(argv[i], "--chkregister"))) {
1869 chkregister++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001870#ifdef LIBXML_SCHEMAS_ENABLED
1871 } else if ((!strcmp(argv[i], "-relaxng")) ||
1872 (!strcmp(argv[i], "--relaxng"))) {
1873 i++;
1874 relaxng = argv[i];
1875 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001876 options |= XML_PARSE_NOENT;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001877 } else if ((!strcmp(argv[i], "-schema")) ||
1878 (!strcmp(argv[i], "--schema"))) {
1879 i++;
1880 schema = argv[i];
1881 noent++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001882#endif
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001883 } else if ((!strcmp(argv[i], "-nonet")) ||
1884 (!strcmp(argv[i], "--nonet"))) {
Daniel Veillard61b93382003-11-03 14:28:31 +00001885 options |= XML_PARSE_NONET;
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001886#ifdef LIBXML_PATTERN_ENABLED
1887 } else if ((!strcmp(argv[i], "-pattern")) ||
1888 (!strcmp(argv[i], "--pattern"))) {
1889 i++;
1890 pattern = argv[i];
1891#endif
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001892 } else {
1893 fprintf(stderr, "Unknown option %s\n", argv[i]);
1894 usage(argv[0]);
1895 return(1);
1896 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001897 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001898
1899#ifdef LIBXML_CATALOG_ENABLED
1900 if (nocatalogs == 0) {
1901 if (catalogs) {
1902 const char *catal;
1903
1904 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00001905 if (catal != NULL) {
1906 xmlLoadCatalogs(catal);
1907 } else {
1908 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
1909 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001910 }
1911 }
1912#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001913
Daniel Veillard81273902003-09-30 00:43:48 +00001914#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001915 if (sax1)
1916 xmlSAXDefaultVersion(1);
1917 else
1918 xmlSAXDefaultVersion(2);
Daniel Veillard81273902003-09-30 00:43:48 +00001919#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00001920
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001921 if (chkregister) {
1922 xmlRegisterNodeDefault(registerNode);
1923 xmlDeregisterNodeDefault(deregisterNode);
1924 }
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001925
1926 indent = getenv("XMLLINT_INDENT");
1927 if(indent != NULL) {
1928 xmlTreeIndentString = indent;
1929 }
1930
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001931
Daniel Veillardd9bad132001-07-23 19:39:43 +00001932 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00001933 if (loaddtd != 0)
1934 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
1935 if (dtdattrs)
1936 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001937 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillard4432df22003-09-28 18:58:27 +00001938#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001939 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard4432df22003-09-28 18:58:27 +00001940#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001941 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001942 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001943 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001944 xmlGenericError(xmlGenericErrorContext,
1945 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
1946 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001947 "<html><head><title>%s output</title></head>\n",
1948 argv[0]);
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001949 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001950 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
1951 argv[0]);
1952 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001953
1954#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard81273902003-09-30 00:43:48 +00001955 if ((relaxng != NULL)
1956#ifdef LIBXML_READER_ENABLED
1957 && (stream == 0)
1958#endif /* LIBXML_READER_ENABLED */
1959 ) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001960 xmlRelaxNGParserCtxtPtr ctxt;
1961
Daniel Veillardce192eb2003-04-16 15:58:05 +00001962 /* forces loading the DTDs */
1963 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001964 options |= XML_PARSE_DTDLOAD;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001965 if (timing) {
1966 startTimer();
1967 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001968 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
1969 xmlRelaxNGSetParserErrors(ctxt,
1970 (xmlRelaxNGValidityErrorFunc) fprintf,
1971 (xmlRelaxNGValidityWarningFunc) fprintf,
1972 stderr);
1973 relaxngschemas = xmlRelaxNGParse(ctxt);
Daniel Veillardce192eb2003-04-16 15:58:05 +00001974 if (relaxngschemas == NULL) {
1975 xmlGenericError(xmlGenericErrorContext,
1976 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +00001977 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +00001978 relaxng = NULL;
1979 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001980 xmlRelaxNGFreeParserCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001981 if (timing) {
1982 endTimer("Compiling the schemas");
1983 }
Daniel Veillardebe25d42004-03-25 09:35:49 +00001984 } else if ((schema != NULL)
1985#ifdef LIBXML_READER_ENABLED
1986 && (stream == 0)
1987#endif
1988 ) {
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001989 xmlSchemaParserCtxtPtr ctxt;
1990
1991 if (timing) {
1992 startTimer();
1993 }
1994 ctxt = xmlSchemaNewParserCtxt(schema);
1995 xmlSchemaSetParserErrors(ctxt,
1996 (xmlSchemaValidityErrorFunc) fprintf,
1997 (xmlSchemaValidityWarningFunc) fprintf,
1998 stderr);
1999 wxschemas = xmlSchemaParse(ctxt);
2000 if (wxschemas == NULL) {
2001 xmlGenericError(xmlGenericErrorContext,
2002 "WXS schema %s failed to compile\n", schema);
William M. Brack8304d872004-06-08 13:29:32 +00002003 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002004 schema = NULL;
2005 }
2006 xmlSchemaFreeParserCtxt(ctxt);
2007 if (timing) {
2008 endTimer("Compiling the schemas");
2009 }
Daniel Veillard71531f32003-02-05 13:19:53 +00002010 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002011#endif /* LIBXML_SCHEMAS_ENABLED */
2012#ifdef LIBXML_PATTERN_ENABLED
2013 if (pattern != NULL) {
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00002014 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002015 if (patternc == NULL) {
2016 xmlGenericError(xmlGenericErrorContext,
2017 "Pattern %s failed to compile\n", pattern);
William M. Brack8304d872004-06-08 13:29:32 +00002018 progresult = XMLLINT_ERR_SCHEMAPAT;
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002019 pattern = NULL;
2020 }
2021 }
2022#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002023 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00002024 if ((!strcmp(argv[i], "-encode")) ||
2025 (!strcmp(argv[i], "--encode"))) {
2026 i++;
2027 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002028 } else if ((!strcmp(argv[i], "-o")) ||
2029 (!strcmp(argv[i], "-output")) ||
2030 (!strcmp(argv[i], "--output"))) {
2031 i++;
2032 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00002033 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002034#ifdef LIBXML_VALID_ENABLED
Daniel Veillardcd429612000-10-11 15:57:05 +00002035 if ((!strcmp(argv[i], "-dtdvalid")) ||
2036 (!strcmp(argv[i], "--dtdvalid"))) {
2037 i++;
2038 continue;
2039 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00002040 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
2041 (!strcmp(argv[i], "--dtdvalidfpi"))) {
2042 i++;
2043 continue;
2044 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002045#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00002046 if ((!strcmp(argv[i], "-relaxng")) ||
2047 (!strcmp(argv[i], "--relaxng"))) {
2048 i++;
2049 continue;
2050 }
Daniel Veillard87076042004-05-03 22:54:49 +00002051 if ((!strcmp(argv[i], "-maxmem")) ||
2052 (!strcmp(argv[i], "--maxmem"))) {
2053 i++;
2054 continue;
2055 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002056 if ((!strcmp(argv[i], "-schema")) ||
2057 (!strcmp(argv[i], "--schema"))) {
2058 i++;
2059 continue;
2060 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002061#ifdef LIBXML_PATTERN_ENABLED
2062 if ((!strcmp(argv[i], "-pattern")) ||
2063 (!strcmp(argv[i], "--pattern"))) {
2064 i++;
2065 continue;
2066 }
2067#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00002068 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00002069 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002070 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002071 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002072 if (repeat) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002073 xmlParserCtxtPtr ctxt = NULL;
2074
2075 for (acount = 0;acount < repeat;acount++) {
Daniel Veillard81273902003-09-30 00:43:48 +00002076#ifdef LIBXML_READER_ENABLED
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002077 if (stream != 0) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00002078 streamFile(argv[i]);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002079 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00002080#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002081 if (ctxt == NULL)
2082 ctxt = xmlNewParserCtxt();
2083 parseAndPrintFile(argv[i], ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00002084#ifdef LIBXML_READER_ENABLED
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002085 }
Daniel Veillard81273902003-09-30 00:43:48 +00002086#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002087 }
2088 if (ctxt != NULL)
2089 xmlFreeParserCtxt(ctxt);
Daniel Veillard7704fb12003-01-03 16:19:51 +00002090 } else {
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002091 nbregister = 0;
2092
Daniel Veillard81273902003-09-30 00:43:48 +00002093#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00002094 if (stream != 0)
2095 streamFile(argv[i]);
2096 else
Daniel Veillard81273902003-09-30 00:43:48 +00002097#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002098 parseAndPrintFile(argv[i], NULL);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002099
2100 if ((chkregister) && (nbregister != 0)) {
2101 fprintf(stderr, "Registration count off: %d\n", nbregister);
William M. Brack8304d872004-06-08 13:29:32 +00002102 progresult = XMLLINT_ERR_RDREGIS;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002103 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00002104 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002105 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00002106 if ((timing) && (repeat)) {
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00002107 endTimer("%d iterations", repeat);
Daniel Veillarda7866932001-12-04 13:14:44 +00002108 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002109 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002110 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002111 if (generate)
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002112 parseAndPrintFile(NULL, NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002113 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00002114 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002115 }
Daniel Veillard845cce42002-01-09 11:51:37 +00002116 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002117 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002118 }
Daniel Veillard71531f32003-02-05 13:19:53 +00002119#ifdef LIBXML_SCHEMAS_ENABLED
2120 if (relaxngschemas != NULL)
2121 xmlRelaxNGFree(relaxngschemas);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002122 if (wxschemas != NULL)
2123 xmlSchemaFree(wxschemas);
Daniel Veillard71531f32003-02-05 13:19:53 +00002124 xmlRelaxNGCleanupTypes();
2125#endif
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002126#ifdef LIBXML_PATTERN_ENABLED
2127 if (patternc != NULL)
2128 xmlFreePattern(patternc);
2129#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002130 xmlCleanupParser();
2131 xmlMemoryDump();
2132
Daniel Veillardf7cd4812001-02-23 18:44:52 +00002133 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002134}
Daniel Veillard88a172f2000-08-04 18:23:10 +00002135