blob: bd502d37167f1cfc338656fa5e135014b828487c [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
904 f = fopen(filename, "r");
905 if (f != NULL) {
906 int res, size = 3;
907 char chars[4096];
908 htmlParserCtxtPtr ctxt;
909
910 /* if (repeat) */
911 size = 4096;
912 res = fread(chars, 1, 4, f);
913 if (res > 0) {
914 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
William M. Brack1d75c8a2003-10-27 13:48:16 +0000915 chars, res, filename, XML_CHAR_ENCODING_NONE);
William M. Brack78637da2003-07-31 14:47:38 +0000916 while ((res = fread(chars, 1, size, f)) > 0) {
917 htmlParseChunk(ctxt, chars, res, 0);
918 }
919 htmlParseChunk(ctxt, chars, 0, 1);
920 doc = ctxt->myDoc;
921 htmlFreeParserCtxt(ctxt);
922 }
923 fclose(f);
924 }
925 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000926#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000927 else if (html) {
Daniel Veillard9475a352003-09-26 12:47:50 +0000928 doc = htmlReadFile(filename, NULL, options);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000929 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000930#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000931 else {
Daniel Veillard73b013f2003-09-30 12:36:01 +0000932#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000933 /*
934 * build an XML tree from a string;
935 */
936 if (push) {
937 FILE *f;
938
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000939 /* '-' Usually means stdin -<sven@zen.org> */
940 if ((filename[0] == '-') && (filename[1] == 0)) {
941 f = stdin;
942 } else {
943 f = fopen(filename, "r");
944 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000945 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +0000946 int ret;
Daniel Veillarda880b122003-04-21 21:36:41 +0000947 int res, size = 1024;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000948 char chars[1024];
949 xmlParserCtxtPtr ctxt;
950
Daniel Veillarda880b122003-04-21 21:36:41 +0000951 /* if (repeat) size = 1024; */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000952 res = fread(chars, 1, 4, f);
953 if (res > 0) {
954 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
955 chars, res, filename);
Daniel Veillard500a1de2004-03-22 15:22:58 +0000956 xmlCtxtUseOptions(ctxt, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000957 while ((res = fread(chars, 1, size, f)) > 0) {
958 xmlParseChunk(ctxt, chars, res, 0);
959 }
960 xmlParseChunk(ctxt, chars, 0, 1);
961 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +0000962 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000963 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +0000964 if (!ret) {
965 xmlFreeDoc(doc);
966 doc = NULL;
967 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000968 }
969 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000970 } else
971#endif /* LIBXML_PUSH_ENABLED */
972 if (testIO) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000973 if ((filename[0] == '-') && (filename[1] == 0)) {
Daniel Veillard60942de2003-09-25 21:05:58 +0000974 doc = xmlReadFd(0, NULL, NULL, options);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000975 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000976 FILE *f;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000977
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000978 f = fopen(filename, "r");
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000979 if (f != NULL) {
980 if (rectxt == NULL)
981 doc = xmlReadIO((xmlInputReadCallback) myRead,
982 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +0000983 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000984 else
985 doc = xmlCtxtReadIO(rectxt,
986 (xmlInputReadCallback) myRead,
987 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +0000988 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000989 } else
Daniel Veillard5e873c42000-04-12 13:27:38 +0000990 doc = NULL;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000991 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000992 } else if (htmlout) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000993 xmlParserCtxtPtr ctxt;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000994
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000995 if (rectxt == NULL)
996 ctxt = xmlNewParserCtxt();
997 else
998 ctxt = rectxt;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000999 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001000 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +00001001 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001002 ctxt->sax->error = xmlHTMLError;
1003 ctxt->sax->warning = xmlHTMLWarning;
1004 ctxt->vctxt.error = xmlHTMLValidityError;
1005 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001006
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001007 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001008
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001009 if (rectxt == NULL)
1010 xmlFreeParserCtxt(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001011 }
Daniel Veillard46e370e2000-07-21 20:32:03 +00001012#ifdef HAVE_SYS_MMAN_H
1013 } else if (memory) {
1014 int fd;
1015 struct stat info;
1016 const char *base;
1017 if (stat(filename, &info) < 0)
1018 return;
1019 if ((fd = open(filename, O_RDONLY)) < 0)
1020 return;
1021 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +00001022 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +00001023 return;
1024
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001025 if (rectxt == NULL)
Daniel Veillard60942de2003-09-25 21:05:58 +00001026 doc = xmlReadMemory((char *) base, info.st_size,
1027 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001028 else
Daniel Veillard60942de2003-09-25 21:05:58 +00001029 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
1030 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001031
Daniel Veillard46e370e2000-07-21 20:32:03 +00001032 munmap((char *) base, info.st_size);
1033#endif
Daniel Veillard4432df22003-09-28 18:58:27 +00001034#ifdef LIBXML_VALID_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +00001035 } else if (valid) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001036 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001037
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001038 if (rectxt == NULL)
1039 ctxt = xmlNewParserCtxt();
1040 else
1041 ctxt = rectxt;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001042 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001043 doc = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +00001044 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001045 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
1046
1047 if (ctxt->valid == 0)
William M. Brack8304d872004-06-08 13:29:32 +00001048 progresult = XMLLINT_ERR_RDFILE;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001049 if (rectxt == NULL)
1050 xmlFreeParserCtxt(ctxt);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001051 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001052#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +00001053 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001054 if (rectxt != NULL)
1055 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
1056 else
1057 doc = xmlReadFile(filename, NULL, options);
Daniel Veillardea7751d2002-12-20 00:16:24 +00001058 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001059 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001060
Daniel Veillard88a172f2000-08-04 18:23:10 +00001061 /*
1062 * If we don't have a document we might as well give up. Do we
1063 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001064 if (doc == NULL) {
William M. Brack8304d872004-06-08 13:29:32 +00001065 progresult = XMLLINT_ERR_UNCLASS;
Daniel Veillard88a172f2000-08-04 18:23:10 +00001066 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001067 }
1068
Daniel Veillard48b2f892001-02-25 16:11:03 +00001069 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001070 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001071 }
1072
Daniel Veillard29e43992001-12-13 22:21:58 +00001073 /*
1074 * Remove DOCTYPE nodes
1075 */
1076 if (dropdtd) {
1077 xmlDtdPtr dtd;
1078
1079 dtd = xmlGetIntSubset(doc);
1080 if (dtd != NULL) {
1081 xmlUnlinkNode((xmlNodePtr)dtd);
1082 xmlFreeDtd(dtd);
1083 }
1084 }
1085
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001086#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +00001087 if (xinclude) {
1088 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001089 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001090 }
Daniel Veillarde74d2e12003-12-09 11:35:37 +00001091 xmlXIncludeProcessFlags(doc, options);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001092 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001093 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001094 }
1095 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001096#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +00001097
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001098#ifdef LIBXML_DEBUG_ENABLED
1099 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001100 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001101 */
1102 if (shell)
1103 xmlShell(doc, filename, xmlShellReadline, stdout);
1104#endif
1105
Daniel Veillard652327a2003-09-29 18:02:38 +00001106#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001107 /*
1108 * test intermediate copy if needed.
1109 */
1110 if (copy) {
1111 tmp = doc;
1112 doc = xmlCopyDoc(doc, 1);
1113 xmlFreeDoc(tmp);
1114 }
Daniel Veillard652327a2003-09-29 18:02:38 +00001115#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001116
Daniel Veillard4432df22003-09-28 18:58:27 +00001117#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001118 if ((insert) && (!html)) {
1119 const xmlChar* list[256];
1120 int nb, i;
1121 xmlNodePtr node;
1122
1123 if (doc->children != NULL) {
1124 node = doc->children;
1125 while ((node != NULL) && (node->last == NULL)) node = node->next;
1126 if (node != NULL) {
1127 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
1128 if (nb < 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001129 fprintf(stderr, "could not get valid list of elements\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001130 } else if (nb == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001131 fprintf(stderr, "No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001132 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001133 fprintf(stderr, "%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001134 nb);
1135 for (i = 0;i < nb;i++) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001136 fprintf(stderr, "%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001137 }
1138 }
1139 }
1140 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001141 }else
1142#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001143#ifdef LIBXML_READER_ENABLED
1144 if (walker) {
1145 walkDoc(doc);
1146 }
1147#endif /* LIBXML_READER_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001148#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001149 if (noout == 0) {
Daniel Veillard3df01182003-12-10 10:17:51 +00001150 int ret;
1151
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001152 /*
1153 * print it.
1154 */
1155#ifdef LIBXML_DEBUG_ENABLED
1156 if (!debug) {
1157#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00001158 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001159 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001160 }
Daniel Veillard656ce942004-04-30 23:11:45 +00001161#ifdef LIBXML_HTML_ENABLED
Daniel Veillard42fd4122003-11-04 08:47:48 +00001162 if ((html) && (!xmlout)) {
1163 if (compress) {
1164 htmlSaveFile(output ? output : "-", doc);
1165 }
1166 else if (encoding != NULL) {
1167 if ( format ) {
1168 htmlSaveFileFormat(output ? output : "-", doc, encoding, 1);
1169 }
1170 else {
1171 htmlSaveFileFormat(output ? output : "-", doc, encoding, 0);
1172 }
1173 }
1174 else if (format) {
1175 htmlSaveFileFormat(output ? output : "-", doc, NULL, 1);
1176 }
1177 else {
1178 FILE *out;
1179 if (output == NULL)
1180 out = stdout;
1181 else {
1182 out = fopen(output,"wb");
1183 }
1184 if (out != NULL) {
1185 if (htmlDocDump(out, doc) < 0)
William M. Brack8304d872004-06-08 13:29:32 +00001186 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00001187
1188 if (output != NULL)
1189 fclose(out);
1190 } else {
1191 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00001192 progresult = XMLLINT_ERR_OUT;
Daniel Veillard42fd4122003-11-04 08:47:48 +00001193 }
1194 }
1195 if ((timing) && (!repeat)) {
1196 endTimer("Saving");
1197 }
1198 } else
1199#endif
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001200#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001201 if (memory) {
1202 xmlChar *result;
1203 int len;
1204
1205 if (encoding != NULL) {
Daniel Veillardd536f702001-11-08 17:32:47 +00001206 if ( format ) {
1207 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
1208 } else {
1209 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
1210 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001211 } else {
Daniel Veillard90493a92001-08-14 14:12:47 +00001212 if (format)
1213 xmlDocDumpFormatMemory(doc, &result, &len, 1);
1214 else
1215 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001216 }
1217 if (result == NULL) {
1218 fprintf(stderr, "Failed to save\n");
1219 } else {
1220 write(1, result, len);
1221 xmlFree(result);
1222 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001223 } else
1224#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001225 if (compress) {
1226 xmlSaveFile(output ? output : "-", doc);
1227 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001228 else if (encoding != NULL) {
1229 if ( format ) {
Daniel Veillard3df01182003-12-10 10:17:51 +00001230 ret = xmlSaveFormatFileEnc(output ? output : "-", doc,
1231 encoding, 1);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001232 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001233 else {
Daniel Veillard3df01182003-12-10 10:17:51 +00001234 ret = xmlSaveFileEnc(output ? output : "-", doc, encoding);
1235 }
1236 if (ret < 0) {
1237 fprintf(stderr, "failed save to %s\n",
1238 output ? output : "-");
William M. Brack8304d872004-06-08 13:29:32 +00001239 progresult = XMLLINT_ERR_OUT;
Daniel Veillardd536f702001-11-08 17:32:47 +00001240 }
1241 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001242 else if (format) {
Daniel Veillard3df01182003-12-10 10:17:51 +00001243 ret = xmlSaveFormatFile(output ? output : "-", doc, 1);
1244 if (ret < 0) {
1245 fprintf(stderr, "failed save to %s\n",
1246 output ? output : "-");
William M. Brack8304d872004-06-08 13:29:32 +00001247 progresult = XMLLINT_ERR_OUT;
Daniel Veillard3df01182003-12-10 10:17:51 +00001248 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001249 }
1250 else {
1251 FILE *out;
1252 if (output == NULL)
1253 out = stdout;
1254 else {
1255 out = fopen(output,"wb");
1256 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001257 if (out != NULL) {
Daniel Veillard828ce832003-10-08 19:19:10 +00001258 if (xmlDocDump(out, doc) < 0)
William M. Brack8304d872004-06-08 13:29:32 +00001259 progresult = XMLLINT_ERR_OUT;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001260
Daniel Veillard05d987b2003-10-08 11:54:57 +00001261 if (output != NULL)
1262 fclose(out);
1263 } else {
1264 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00001265 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00001266 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001267 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001268 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001269 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001270 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001271#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001272 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001273 FILE *out;
1274 if (output == NULL)
1275 out = stdout;
1276 else {
1277 out = fopen(output,"wb");
1278 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001279 if (out != NULL) {
1280 xmlDebugDumpDocument(out, doc);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001281
Daniel Veillard05d987b2003-10-08 11:54:57 +00001282 if (output != NULL)
1283 fclose(out);
1284 } else {
1285 fprintf(stderr, "failed to open %s\n", output);
William M. Brack8304d872004-06-08 13:29:32 +00001286 progresult = XMLLINT_ERR_OUT;
Daniel Veillard05d987b2003-10-08 11:54:57 +00001287 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001288 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001289#endif
1290 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001291#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001292
Daniel Veillard4432df22003-09-28 18:58:27 +00001293#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001294 /*
1295 * A posteriori validation test
1296 */
Daniel Veillard66f68e72003-08-18 16:39:51 +00001297 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
Daniel Veillardcd429612000-10-11 15:57:05 +00001298 xmlDtdPtr dtd;
1299
Daniel Veillard48b2f892001-02-25 16:11:03 +00001300 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001301 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001302 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00001303 if (dtdvalid != NULL)
1304 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
1305 else
1306 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001307 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001308 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001309 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001310 if (dtd == NULL) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001311 if (dtdvalid != NULL)
1312 xmlGenericError(xmlGenericErrorContext,
1313 "Could not parse DTD %s\n", dtdvalid);
1314 else
1315 xmlGenericError(xmlGenericErrorContext,
1316 "Could not parse DTD %s\n", dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00001317 progresult = XMLLINT_ERR_DTD;
Daniel Veillardcd429612000-10-11 15:57:05 +00001318 } else {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001319 xmlValidCtxtPtr cvp;
1320
1321 if ((cvp = xmlNewValidCtxt()) == NULL) {
1322 xmlGenericError(xmlGenericErrorContext,
1323 "Couldn't allocate validation context\n");
1324 exit(-1);
1325 }
1326 cvp->userData = (void *) stderr;
1327 cvp->error = (xmlValidityErrorFunc) fprintf;
1328 cvp->warning = (xmlValidityWarningFunc) fprintf;
1329
Daniel Veillard48b2f892001-02-25 16:11:03 +00001330 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001331 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001332 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001333 if (!xmlValidateDtd(cvp, doc, dtd)) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001334 if (dtdvalid != NULL)
1335 xmlGenericError(xmlGenericErrorContext,
1336 "Document %s does not validate against %s\n",
1337 filename, dtdvalid);
1338 else
1339 xmlGenericError(xmlGenericErrorContext,
1340 "Document %s does not validate against %s\n",
1341 filename, dtdvalidfpi);
William M. Brack8304d872004-06-08 13:29:32 +00001342 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00001343 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001344 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001345 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001346 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001347 xmlFreeValidCtxt(cvp);
Daniel Veillardcd429612000-10-11 15:57:05 +00001348 xmlFreeDtd(dtd);
1349 }
1350 } else if (postvalid) {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001351 xmlValidCtxtPtr cvp;
1352
1353 if ((cvp = xmlNewValidCtxt()) == NULL) {
1354 xmlGenericError(xmlGenericErrorContext,
1355 "Couldn't allocate validation context\n");
1356 exit(-1);
1357 }
1358
Daniel Veillard48b2f892001-02-25 16:11:03 +00001359 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001360 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001361 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001362 cvp->userData = (void *) stderr;
1363 cvp->error = (xmlValidityErrorFunc) fprintf;
1364 cvp->warning = (xmlValidityWarningFunc) fprintf;
1365 if (!xmlValidateDocument(cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001366 xmlGenericError(xmlGenericErrorContext,
1367 "Document %s does not validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001368 progresult = XMLLINT_ERR_VALID;
Daniel Veillardcd429612000-10-11 15:57:05 +00001369 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001370 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001371 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001372 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001373 xmlFreeValidCtxt(cvp);
Daniel Veillard4432df22003-09-28 18:58:27 +00001374 }
1375#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00001376#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001377 if (relaxngschemas != NULL) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001378 xmlRelaxNGValidCtxtPtr ctxt;
1379 int ret;
1380
Daniel Veillard42f12e92003-03-07 18:32:59 +00001381 if ((timing) && (!repeat)) {
1382 startTimer();
1383 }
1384
Daniel Veillard71531f32003-02-05 13:19:53 +00001385 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
1386 xmlRelaxNGSetValidErrors(ctxt,
1387 (xmlRelaxNGValidityErrorFunc) fprintf,
1388 (xmlRelaxNGValidityWarningFunc) fprintf,
1389 stderr);
1390 ret = xmlRelaxNGValidateDoc(ctxt, doc);
1391 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001392 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard71531f32003-02-05 13:19:53 +00001393 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001394 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001395 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00001396 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001397 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard71531f32003-02-05 13:19:53 +00001398 filename);
William M. Brack8304d872004-06-08 13:29:32 +00001399 progresult = XMLLINT_ERR_VALID;
Daniel Veillard71531f32003-02-05 13:19:53 +00001400 }
1401 xmlRelaxNGFreeValidCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001402 if ((timing) && (!repeat)) {
1403 endTimer("Validating");
1404 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001405 } else if (wxschemas != NULL) {
1406 xmlSchemaValidCtxtPtr ctxt;
1407 int ret;
1408
1409 if ((timing) && (!repeat)) {
1410 startTimer();
1411 }
1412
1413 ctxt = xmlSchemaNewValidCtxt(wxschemas);
1414 xmlSchemaSetValidErrors(ctxt,
1415 (xmlSchemaValidityErrorFunc) fprintf,
1416 (xmlSchemaValidityWarningFunc) fprintf,
1417 stderr);
1418 ret = xmlSchemaValidateDoc(ctxt, doc);
1419 if (ret == 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001420 fprintf(stderr, "%s validates\n", filename);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001421 } else if (ret > 0) {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001422 fprintf(stderr, "%s fails to validate\n", filename);
William M. Brack8304d872004-06-08 13:29:32 +00001423 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001424 } else {
Daniel Veillardd21f61b2003-12-29 10:31:21 +00001425 fprintf(stderr, "%s validation generated an internal error\n",
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001426 filename);
William M. Brack8304d872004-06-08 13:29:32 +00001427 progresult = XMLLINT_ERR_VALID;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001428 }
1429 xmlSchemaFreeValidCtxt(ctxt);
1430 if ((timing) && (!repeat)) {
1431 endTimer("Validating");
1432 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001433 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001434#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001435
1436#ifdef LIBXML_DEBUG_ENABLED
1437 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001438 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001439#endif
1440
1441 /*
1442 * free it.
1443 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00001444 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001445 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001446 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001447 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001448 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001449 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001450 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001451}
1452
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001453/************************************************************************
1454 * *
1455 * Usage and Main *
1456 * *
1457 ************************************************************************/
1458
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001459static void showVersion(const char *name) {
1460 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
1461 fprintf(stderr, " compiled with: ");
Daniel Veillard4432df22003-09-28 18:58:27 +00001462#ifdef LIBXML_VALID_ENABLED
1463 fprintf(stderr, "DTDValid ");
1464#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001465#ifdef LIBXML_FTP_ENABLED
1466 fprintf(stderr, "FTP ");
1467#endif
1468#ifdef LIBXML_HTTP_ENABLED
1469 fprintf(stderr, "HTTP ");
1470#endif
1471#ifdef LIBXML_HTML_ENABLED
1472 fprintf(stderr, "HTML ");
1473#endif
1474#ifdef LIBXML_C14N_ENABLED
1475 fprintf(stderr, "C14N ");
1476#endif
1477#ifdef LIBXML_CATALOG_ENABLED
1478 fprintf(stderr, "Catalog ");
1479#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001480#ifdef LIBXML_XPATH_ENABLED
1481 fprintf(stderr, "XPath ");
1482#endif
1483#ifdef LIBXML_XPTR_ENABLED
1484 fprintf(stderr, "XPointer ");
1485#endif
1486#ifdef LIBXML_XINCLUDE_ENABLED
1487 fprintf(stderr, "XInclude ");
1488#endif
1489#ifdef LIBXML_ICONV_ENABLED
1490 fprintf(stderr, "Iconv ");
1491#endif
1492#ifdef DEBUG_MEMORY_LOCATION
1493 fprintf(stderr, "MemDebug ");
1494#endif
1495#ifdef LIBXML_UNICODE_ENABLED
1496 fprintf(stderr, "Unicode ");
1497#endif
1498#ifdef LIBXML_REGEXP_ENABLED
1499 fprintf(stderr, "Regexps ");
1500#endif
1501#ifdef LIBXML_AUTOMATA_ENABLED
1502 fprintf(stderr, "Automata ");
1503#endif
1504#ifdef LIBXML_SCHEMAS_ENABLED
1505 fprintf(stderr, "Schemas ");
1506#endif
1507 fprintf(stderr, "\n");
1508}
1509
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001510static void usage(const char *name) {
1511 printf("Usage : %s [options] XMLfiles ...\n", name);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001512#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001513 printf("\tParse the XML files and output the result of the parsing\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001514#else
1515 printf("\tParse the XML files\n");
1516#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001517 printf("\t--version : display the version of the XML library used\n");
1518#ifdef LIBXML_DEBUG_ENABLED
1519 printf("\t--debug : dump a debug tree of the in-memory document\n");
1520 printf("\t--shell : run a navigating shell\n");
1521 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00001522#else
Daniel Veillard81273902003-09-30 00:43:48 +00001523#ifdef LIBXML_READER_ENABLED
Daniel Veillard8326e732003-01-07 00:19:07 +00001524 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001525#endif /* LIBXML_READER_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001526#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001527#ifdef LIBXML_TREE_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001528 printf("\t--copy : used to test the internal copy implementation\n");
Daniel Veillard652327a2003-09-29 18:02:38 +00001529#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001530 printf("\t--recover : output what was parsable on broken XML documents\n");
1531 printf("\t--noent : substitute entity references by their value\n");
1532 printf("\t--noout : don't output the result tree\n");
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001533 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001534 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001535 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001536#ifdef LIBXML_VALID_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001537 printf("\t--valid : validate the document in addition to std well-formed check\n");
1538 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
1539 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
Daniel Veillard66f68e72003-08-18 16:39:51 +00001540 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001541#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001542 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001543 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001544 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
1545 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001546#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001547#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001548 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001549#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001550#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001551#ifdef LIBXML_HTML_ENABLED
1552 printf("\t--html : use the HTML parser\n");
Daniel Veillard42fd4122003-11-04 08:47:48 +00001553 printf("\t--xmlout : force to use the XML serializer when using --html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001554#endif
Daniel Veillard73b013f2003-09-30 12:36:01 +00001555#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001556 printf("\t--push : use the push mode of the parser\n");
Daniel Veillard73b013f2003-09-30 12:36:01 +00001557#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001558#ifdef HAVE_SYS_MMAN_H
1559 printf("\t--memory : parse from memory\n");
1560#endif
Daniel Veillard87076042004-05-03 22:54:49 +00001561 printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001562 printf("\t--nowarning : do not emit warnings from parser/validator\n");
1563 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001564 printf("\t--nocdata : replace cdata section with text nodes\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001565#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001566 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001567 printf("\t--encode encoding : output in the given encoding\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001568 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
1569#endif /* LIBXML_OUTPUT_ENABLED */
1570 printf("\t--nsclean : remove redundant namespace declarations\n");
1571 printf("\t--testIO : test user I/O support\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001572#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00001573 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
1574 printf("\t otherwise XML Catalogs starting from \n");
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001575 printf("\t %s are activated by default\n", XML_XML_DEFAULT_CATALOG);
Daniel Veillard05c13a22001-09-09 08:38:09 +00001576 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001577#endif
1578 printf("\t--auto : generate a small doc on the fly\n");
1579#ifdef LIBXML_XINCLUDE_ENABLED
1580 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001581#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001582 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00001583 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard81273902003-09-30 00:43:48 +00001584#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001585 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001586 printf("\t--walker : create a reader and walk though the resulting doc\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001587#endif /* LIBXML_READER_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001588#ifdef LIBXML_PATTERN_ENABLED
1589 printf("\t--pattern pattern_value : test the pattern support\n");
1590#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001591 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillardef4d3bc2003-02-07 12:38:22 +00001592#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard71531f32003-02-05 13:19:53 +00001593 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001594 printf("\t--schema schema : do validation against the WXS schema\n");
Daniel Veillard71531f32003-02-05 13:19:53 +00001595#endif
Daniel Veillarda42f25f2002-01-25 14:15:40 +00001596 printf("\nLibxml project home page: http://xmlsoft.org/\n");
1597 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001598}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001599
1600static void registerNode(xmlNodePtr node)
1601{
1602 node->_private = malloc(sizeof(long));
1603 *(long*)node->_private = (long) 0x81726354;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00001604 nbregister++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001605}
1606
1607static void deregisterNode(xmlNodePtr node)
1608{
1609 assert(node->_private != NULL);
1610 assert(*(long*)node->_private == (long) 0x81726354);
1611 free(node->_private);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00001612 nbregister--;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001613}
1614
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001615int
1616main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001617 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001618 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00001619 int version = 0;
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001620 const char* indent;
1621
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001622 if (argc <= 1) {
1623 usage(argv[0]);
1624 return(1);
1625 }
Daniel Veillardbe803962000-06-28 23:40:59 +00001626 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001627 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001628 if (!strcmp(argv[i], "-"))
1629 break;
1630
1631 if (argv[i][0] != '-')
1632 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001633 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
1634 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00001635 else
1636#ifdef LIBXML_DEBUG_ENABLED
1637 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001638 (!strcmp(argv[i], "--shell"))) {
1639 shell++;
1640 noout = 1;
1641 } else
1642#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001643#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001644 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
1645 copy++;
Daniel Veillard652327a2003-09-29 18:02:38 +00001646 else
1647#endif /* LIBXML_TREE_ENABLED */
1648 if ((!strcmp(argv[i], "-recover")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001649 (!strcmp(argv[i], "--recover"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001650 recovery++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001651 options |= XML_PARSE_RECOVER;
1652 } else if ((!strcmp(argv[i], "-noent")) ||
1653 (!strcmp(argv[i], "--noent"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001654 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001655 options |= XML_PARSE_NOENT;
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001656 } else if ((!strcmp(argv[i], "-nsclean")) ||
1657 (!strcmp(argv[i], "--nsclean"))) {
1658 options |= XML_PARSE_NSCLEAN;
1659 } else if ((!strcmp(argv[i], "-nocdata")) ||
1660 (!strcmp(argv[i], "--nocdata"))) {
1661 options |= XML_PARSE_NOCDATA;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001662 } else if ((!strcmp(argv[i], "-nodict")) ||
1663 (!strcmp(argv[i], "--nodict"))) {
1664 options |= XML_PARSE_NODICT;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001665 } else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00001666 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001667 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00001668 version = 1;
1669 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001670 (!strcmp(argv[i], "--noout")))
1671 noout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001672#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001673 else if ((!strcmp(argv[i], "-o")) ||
1674 (!strcmp(argv[i], "-output")) ||
1675 (!strcmp(argv[i], "--output"))) {
1676 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00001677 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001678 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001679#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001680 else if ((!strcmp(argv[i], "-htmlout")) ||
1681 (!strcmp(argv[i], "--htmlout")))
1682 htmlout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001683 else if ((!strcmp(argv[i], "-nowrap")) ||
1684 (!strcmp(argv[i], "--nowrap")))
1685 nowrap++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001686#ifdef LIBXML_HTML_ENABLED
1687 else if ((!strcmp(argv[i], "-html")) ||
1688 (!strcmp(argv[i], "--html"))) {
1689 html++;
1690 }
Daniel Veillard42fd4122003-11-04 08:47:48 +00001691 else if ((!strcmp(argv[i], "-xmlout")) ||
1692 (!strcmp(argv[i], "--xmlout"))) {
1693 xmlout++;
1694 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001695#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001696 else if ((!strcmp(argv[i], "-loaddtd")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001697 (!strcmp(argv[i], "--loaddtd"))) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001698 loaddtd++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001699 options |= XML_PARSE_DTDLOAD;
1700 } else if ((!strcmp(argv[i], "-dtdattr")) ||
Daniel Veillard48da9102001-08-07 01:10:10 +00001701 (!strcmp(argv[i], "--dtdattr"))) {
1702 loaddtd++;
1703 dtdattrs++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001704 options |= XML_PARSE_DTDATTR;
Daniel Veillard4432df22003-09-28 18:58:27 +00001705 }
1706#ifdef LIBXML_VALID_ENABLED
1707 else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001708 (!strcmp(argv[i], "--valid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001709 valid++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001710 options |= XML_PARSE_DTDVALID;
1711 } else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001712 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001713 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001714 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00001715 options |= XML_PARSE_DTDLOAD;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001716 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00001717 (!strcmp(argv[i], "--dtdvalid"))) {
1718 i++;
1719 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001720 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00001721 options |= XML_PARSE_DTDLOAD;
Daniel Veillard66f68e72003-08-18 16:39:51 +00001722 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
1723 (!strcmp(argv[i], "--dtdvalidfpi"))) {
1724 i++;
1725 dtdvalidfpi = argv[i];
1726 loaddtd++;
Daniel Veillard5a30b2d2003-12-09 13:54:39 +00001727 options |= XML_PARSE_DTDLOAD;
Daniel Veillardcd429612000-10-11 15:57:05 +00001728 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001729#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard29e43992001-12-13 22:21:58 +00001730 else if ((!strcmp(argv[i], "-dropdtd")) ||
1731 (!strcmp(argv[i], "--dropdtd")))
1732 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001733 else if ((!strcmp(argv[i], "-insert")) ||
1734 (!strcmp(argv[i], "--insert")))
1735 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001736 else if ((!strcmp(argv[i], "-timing")) ||
1737 (!strcmp(argv[i], "--timing")))
1738 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001739 else if ((!strcmp(argv[i], "-auto")) ||
1740 (!strcmp(argv[i], "--auto")))
1741 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001742 else if ((!strcmp(argv[i], "-repeat")) ||
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001743 (!strcmp(argv[i], "--repeat"))) {
1744 if (repeat)
1745 repeat *= 10;
1746 else
1747 repeat = 100;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001748 }
1749#ifdef LIBXML_PUSH_ENABLED
1750 else if ((!strcmp(argv[i], "-push")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001751 (!strcmp(argv[i], "--push")))
1752 push++;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001753#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +00001754#ifdef HAVE_SYS_MMAN_H
1755 else if ((!strcmp(argv[i], "-memory")) ||
1756 (!strcmp(argv[i], "--memory")))
1757 memory++;
1758#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00001759 else if ((!strcmp(argv[i], "-testIO")) ||
1760 (!strcmp(argv[i], "--testIO")))
1761 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001762#ifdef LIBXML_XINCLUDE_ENABLED
1763 else if ((!strcmp(argv[i], "-xinclude")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001764 (!strcmp(argv[i], "--xinclude"))) {
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001765 xinclude++;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001766 options |= XML_PARSE_XINCLUDE;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001767 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001768#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001769#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001770#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001771 else if ((!strcmp(argv[i], "-compress")) ||
1772 (!strcmp(argv[i], "--compress"))) {
1773 compress++;
1774 xmlSetCompressMode(9);
1775 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001776#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001777#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001778 else if ((!strcmp(argv[i], "-nowarning")) ||
1779 (!strcmp(argv[i], "--nowarning"))) {
1780 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001781 xmlPedanticParserDefault(0);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001782 options |= XML_PARSE_NOWARNING;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001783 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001784 else if ((!strcmp(argv[i], "-pedantic")) ||
1785 (!strcmp(argv[i], "--pedantic"))) {
1786 xmlGetWarningsDefaultValue = 1;
1787 xmlPedanticParserDefault(1);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001788 options |= XML_PARSE_PEDANTIC;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001789 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001790#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001791 else if ((!strcmp(argv[i], "-debugent")) ||
1792 (!strcmp(argv[i], "--debugent"))) {
1793 debugent++;
1794 xmlParserDebugEntities = 1;
1795 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001796#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00001797#ifdef LIBXML_CATALOG_ENABLED
1798 else if ((!strcmp(argv[i], "-catalogs")) ||
1799 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001800 catalogs++;
1801 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
1802 (!strcmp(argv[i], "--nocatalogs"))) {
1803 nocatalogs++;
Daniel Veillard81418e32001-05-22 15:08:55 +00001804 }
1805#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00001806 else if ((!strcmp(argv[i], "-encode")) ||
1807 (!strcmp(argv[i], "--encode"))) {
1808 i++;
1809 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001810 /*
1811 * OK it's for testing purposes
1812 */
1813 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00001814 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001815 else if ((!strcmp(argv[i], "-noblanks")) ||
1816 (!strcmp(argv[i], "--noblanks"))) {
1817 noblanks++;
1818 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00001819 }
Daniel Veillard87076042004-05-03 22:54:49 +00001820 else if ((!strcmp(argv[i], "-maxmem")) ||
1821 (!strcmp(argv[i], "--maxmem"))) {
1822 i++;
1823 if (sscanf(argv[i], "%d", &maxmem) == 1) {
1824 xmlMemSetup(myFreeFunc, myMallocFunc, myReallocFunc,
1825 myStrdupFunc);
1826 } else {
1827 maxmem = 0;
1828 }
1829 }
Daniel Veillard90493a92001-08-14 14:12:47 +00001830 else if ((!strcmp(argv[i], "-format")) ||
1831 (!strcmp(argv[i], "--format"))) {
1832 noblanks++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001833#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001834 format++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001835#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard90493a92001-08-14 14:12:47 +00001836 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001837 }
Daniel Veillard81273902003-09-30 00:43:48 +00001838#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001839 else if ((!strcmp(argv[i], "-stream")) ||
1840 (!strcmp(argv[i], "--stream"))) {
1841 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001842 }
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001843 else if ((!strcmp(argv[i], "-walker")) ||
1844 (!strcmp(argv[i], "--walker"))) {
1845 walker++;
1846 noout++;
1847 }
Daniel Veillard81273902003-09-30 00:43:48 +00001848#endif /* LIBXML_READER_ENABLED */
1849#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001850 else if ((!strcmp(argv[i], "-sax1")) ||
1851 (!strcmp(argv[i], "--sax1"))) {
1852 sax1++;
1853 }
Daniel Veillard81273902003-09-30 00:43:48 +00001854#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001855 else if ((!strcmp(argv[i], "-chkregister")) ||
1856 (!strcmp(argv[i], "--chkregister"))) {
1857 chkregister++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001858#ifdef LIBXML_SCHEMAS_ENABLED
1859 } else if ((!strcmp(argv[i], "-relaxng")) ||
1860 (!strcmp(argv[i], "--relaxng"))) {
1861 i++;
1862 relaxng = argv[i];
1863 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001864 options |= XML_PARSE_NOENT;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001865 } else if ((!strcmp(argv[i], "-schema")) ||
1866 (!strcmp(argv[i], "--schema"))) {
1867 i++;
1868 schema = argv[i];
1869 noent++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001870#endif
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001871 } else if ((!strcmp(argv[i], "-nonet")) ||
1872 (!strcmp(argv[i], "--nonet"))) {
Daniel Veillard61b93382003-11-03 14:28:31 +00001873 options |= XML_PARSE_NONET;
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001874#ifdef LIBXML_PATTERN_ENABLED
1875 } else if ((!strcmp(argv[i], "-pattern")) ||
1876 (!strcmp(argv[i], "--pattern"))) {
1877 i++;
1878 pattern = argv[i];
1879#endif
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001880 } else {
1881 fprintf(stderr, "Unknown option %s\n", argv[i]);
1882 usage(argv[0]);
1883 return(1);
1884 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001885 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001886
1887#ifdef LIBXML_CATALOG_ENABLED
1888 if (nocatalogs == 0) {
1889 if (catalogs) {
1890 const char *catal;
1891
1892 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00001893 if (catal != NULL) {
1894 xmlLoadCatalogs(catal);
1895 } else {
1896 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
1897 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001898 }
1899 }
1900#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001901
Daniel Veillard81273902003-09-30 00:43:48 +00001902#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001903 if (sax1)
1904 xmlSAXDefaultVersion(1);
1905 else
1906 xmlSAXDefaultVersion(2);
Daniel Veillard81273902003-09-30 00:43:48 +00001907#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00001908
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001909 if (chkregister) {
1910 xmlRegisterNodeDefault(registerNode);
1911 xmlDeregisterNodeDefault(deregisterNode);
1912 }
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001913
1914 indent = getenv("XMLLINT_INDENT");
1915 if(indent != NULL) {
1916 xmlTreeIndentString = indent;
1917 }
1918
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001919
Daniel Veillardd9bad132001-07-23 19:39:43 +00001920 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00001921 if (loaddtd != 0)
1922 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
1923 if (dtdattrs)
1924 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001925 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillard4432df22003-09-28 18:58:27 +00001926#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001927 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard4432df22003-09-28 18:58:27 +00001928#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001929 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001930 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001931 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001932 xmlGenericError(xmlGenericErrorContext,
1933 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
1934 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001935 "<html><head><title>%s output</title></head>\n",
1936 argv[0]);
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001937 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001938 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
1939 argv[0]);
1940 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001941
1942#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard81273902003-09-30 00:43:48 +00001943 if ((relaxng != NULL)
1944#ifdef LIBXML_READER_ENABLED
1945 && (stream == 0)
1946#endif /* LIBXML_READER_ENABLED */
1947 ) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001948 xmlRelaxNGParserCtxtPtr ctxt;
1949
Daniel Veillardce192eb2003-04-16 15:58:05 +00001950 /* forces loading the DTDs */
1951 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001952 options |= XML_PARSE_DTDLOAD;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001953 if (timing) {
1954 startTimer();
1955 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001956 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
1957 xmlRelaxNGSetParserErrors(ctxt,
1958 (xmlRelaxNGValidityErrorFunc) fprintf,
1959 (xmlRelaxNGValidityWarningFunc) fprintf,
1960 stderr);
1961 relaxngschemas = xmlRelaxNGParse(ctxt);
Daniel Veillardce192eb2003-04-16 15:58:05 +00001962 if (relaxngschemas == NULL) {
1963 xmlGenericError(xmlGenericErrorContext,
1964 "Relax-NG schema %s failed to compile\n", relaxng);
William M. Brack8304d872004-06-08 13:29:32 +00001965 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillardce192eb2003-04-16 15:58:05 +00001966 relaxng = NULL;
1967 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001968 xmlRelaxNGFreeParserCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001969 if (timing) {
1970 endTimer("Compiling the schemas");
1971 }
Daniel Veillardebe25d42004-03-25 09:35:49 +00001972 } else if ((schema != NULL)
1973#ifdef LIBXML_READER_ENABLED
1974 && (stream == 0)
1975#endif
1976 ) {
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001977 xmlSchemaParserCtxtPtr ctxt;
1978
1979 if (timing) {
1980 startTimer();
1981 }
1982 ctxt = xmlSchemaNewParserCtxt(schema);
1983 xmlSchemaSetParserErrors(ctxt,
1984 (xmlSchemaValidityErrorFunc) fprintf,
1985 (xmlSchemaValidityWarningFunc) fprintf,
1986 stderr);
1987 wxschemas = xmlSchemaParse(ctxt);
1988 if (wxschemas == NULL) {
1989 xmlGenericError(xmlGenericErrorContext,
1990 "WXS schema %s failed to compile\n", schema);
William M. Brack8304d872004-06-08 13:29:32 +00001991 progresult = XMLLINT_ERR_SCHEMACOMP;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001992 schema = NULL;
1993 }
1994 xmlSchemaFreeParserCtxt(ctxt);
1995 if (timing) {
1996 endTimer("Compiling the schemas");
1997 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001998 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001999#endif /* LIBXML_SCHEMAS_ENABLED */
2000#ifdef LIBXML_PATTERN_ENABLED
2001 if (pattern != NULL) {
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00002002 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002003 if (patternc == NULL) {
2004 xmlGenericError(xmlGenericErrorContext,
2005 "Pattern %s failed to compile\n", pattern);
William M. Brack8304d872004-06-08 13:29:32 +00002006 progresult = XMLLINT_ERR_SCHEMAPAT;
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002007 pattern = NULL;
2008 }
2009 }
2010#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002011 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00002012 if ((!strcmp(argv[i], "-encode")) ||
2013 (!strcmp(argv[i], "--encode"))) {
2014 i++;
2015 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00002016 } else if ((!strcmp(argv[i], "-o")) ||
2017 (!strcmp(argv[i], "-output")) ||
2018 (!strcmp(argv[i], "--output"))) {
2019 i++;
2020 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00002021 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002022#ifdef LIBXML_VALID_ENABLED
Daniel Veillardcd429612000-10-11 15:57:05 +00002023 if ((!strcmp(argv[i], "-dtdvalid")) ||
2024 (!strcmp(argv[i], "--dtdvalid"))) {
2025 i++;
2026 continue;
2027 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00002028 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
2029 (!strcmp(argv[i], "--dtdvalidfpi"))) {
2030 i++;
2031 continue;
2032 }
Daniel Veillard4432df22003-09-28 18:58:27 +00002033#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00002034 if ((!strcmp(argv[i], "-relaxng")) ||
2035 (!strcmp(argv[i], "--relaxng"))) {
2036 i++;
2037 continue;
2038 }
Daniel Veillard87076042004-05-03 22:54:49 +00002039 if ((!strcmp(argv[i], "-maxmem")) ||
2040 (!strcmp(argv[i], "--maxmem"))) {
2041 i++;
2042 continue;
2043 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002044 if ((!strcmp(argv[i], "-schema")) ||
2045 (!strcmp(argv[i], "--schema"))) {
2046 i++;
2047 continue;
2048 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002049#ifdef LIBXML_PATTERN_ENABLED
2050 if ((!strcmp(argv[i], "-pattern")) ||
2051 (!strcmp(argv[i], "--pattern"))) {
2052 i++;
2053 continue;
2054 }
2055#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00002056 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00002057 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002058 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00002059 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002060 if (repeat) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002061 xmlParserCtxtPtr ctxt = NULL;
2062
2063 for (acount = 0;acount < repeat;acount++) {
Daniel Veillard81273902003-09-30 00:43:48 +00002064#ifdef LIBXML_READER_ENABLED
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002065 if (stream != 0) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00002066 streamFile(argv[i]);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00002067 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00002068#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002069 if (ctxt == NULL)
2070 ctxt = xmlNewParserCtxt();
2071 parseAndPrintFile(argv[i], ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00002072#ifdef LIBXML_READER_ENABLED
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002073 }
Daniel Veillard81273902003-09-30 00:43:48 +00002074#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002075 }
2076 if (ctxt != NULL)
2077 xmlFreeParserCtxt(ctxt);
Daniel Veillard7704fb12003-01-03 16:19:51 +00002078 } else {
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002079 nbregister = 0;
2080
Daniel Veillard81273902003-09-30 00:43:48 +00002081#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00002082 if (stream != 0)
2083 streamFile(argv[i]);
2084 else
Daniel Veillard81273902003-09-30 00:43:48 +00002085#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002086 parseAndPrintFile(argv[i], NULL);
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002087
2088 if ((chkregister) && (nbregister != 0)) {
2089 fprintf(stderr, "Registration count off: %d\n", nbregister);
William M. Brack8304d872004-06-08 13:29:32 +00002090 progresult = XMLLINT_ERR_RDREGIS;
Daniel Veillarda2d51fc2004-04-30 22:25:59 +00002091 }
Daniel Veillard7704fb12003-01-03 16:19:51 +00002092 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002093 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00002094 if ((timing) && (repeat)) {
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00002095 endTimer("%d iterations", repeat);
Daniel Veillarda7866932001-12-04 13:14:44 +00002096 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00002097 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002098 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00002099 if (generate)
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002100 parseAndPrintFile(NULL, NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002101 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00002102 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002103 }
Daniel Veillard845cce42002-01-09 11:51:37 +00002104 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00002105 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002106 }
Daniel Veillard71531f32003-02-05 13:19:53 +00002107#ifdef LIBXML_SCHEMAS_ENABLED
2108 if (relaxngschemas != NULL)
2109 xmlRelaxNGFree(relaxngschemas);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00002110 if (wxschemas != NULL)
2111 xmlSchemaFree(wxschemas);
Daniel Veillard71531f32003-02-05 13:19:53 +00002112 xmlRelaxNGCleanupTypes();
2113#endif
Daniel Veillardb3de70c2003-12-02 22:32:15 +00002114#ifdef LIBXML_PATTERN_ENABLED
2115 if (patternc != NULL)
2116 xmlFreePattern(patternc);
2117#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002118 xmlCleanupParser();
2119 xmlMemoryDump();
2120
Daniel Veillardf7cd4812001-02-23 18:44:52 +00002121 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00002122}
Daniel Veillard88a172f2000-08-04 18:23:10 +00002123