blob: 37dc72e7295c1c2ba08136d2864193249b4ea266 [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
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000100#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000101static int shell = 0;
102static int debugent = 0;
103#endif
Daniel Veillard8326e732003-01-07 00:19:07 +0000104static int debug = 0;
Daniel Veillard652327a2003-09-29 18:02:38 +0000105#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000106static int copy = 0;
Daniel Veillard652327a2003-09-29 18:02:38 +0000107#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000108static int recovery = 0;
109static int noent = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000110static int noblanks = 0;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000111static int noout = 0;
112static int nowrap = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000113#ifdef LIBXML_OUTPUT_ENABLED
114static int format = 0;
115static const char *output = NULL;
116static int compress = 0;
117#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4432df22003-09-28 18:58:27 +0000118#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000119static int valid = 0;
120static int postvalid = 0;
Daniel Veillardcd429612000-10-11 15:57:05 +0000121static char * dtdvalid = NULL;
Daniel Veillard66f68e72003-08-18 16:39:51 +0000122static char * dtdvalidfpi = NULL;
Daniel Veillard4432df22003-09-28 18:58:27 +0000123#endif
Daniel Veillard71531f32003-02-05 13:19:53 +0000124#ifdef LIBXML_SCHEMAS_ENABLED
125static char * relaxng = NULL;
126static xmlRelaxNGPtr relaxngschemas = NULL;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +0000127static char * schema = NULL;
128static xmlSchemaPtr wxschemas = NULL;
Daniel Veillard71531f32003-02-05 13:19:53 +0000129#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000130static int repeat = 0;
131static int insert = 0;
Daniel Veillard4432df22003-09-28 18:58:27 +0000132#ifdef LIBXML_HTML_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000133static int html = 0;
Daniel Veillard42fd4122003-11-04 08:47:48 +0000134static int xmlout = 0;
Daniel Veillard4432df22003-09-28 18:58:27 +0000135#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000136static int htmlout = 0;
Daniel Veillard73b013f2003-09-30 12:36:01 +0000137#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000138static int push = 0;
Daniel Veillard73b013f2003-09-30 12:36:01 +0000139#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +0000140#ifdef HAVE_SYS_MMAN_H
141static int memory = 0;
142#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +0000143static int testIO = 0;
Daniel Veillardbe803962000-06-28 23:40:59 +0000144static char *encoding = NULL;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000145#ifdef LIBXML_XINCLUDE_ENABLED
146static int xinclude = 0;
147#endif
Daniel Veillard48da9102001-08-07 01:10:10 +0000148static int dtdattrs = 0;
Daniel Veillard10ea86c2001-06-20 13:55:33 +0000149static int loaddtd = 0;
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000150static int progresult = 0;
Daniel Veillard48b2f892001-02-25 16:11:03 +0000151static int timing = 0;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000152static int generate = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000153static int dropdtd = 0;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000154#ifdef LIBXML_CATALOG_ENABLED
155static int catalogs = 0;
156static int nocatalogs = 0;
157#endif
Daniel Veillard81273902003-09-30 00:43:48 +0000158#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000159static int stream = 0;
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000160static int walker = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000161#endif /* LIBXML_READER_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000162static int chkregister = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000163#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +0000164static int sax1 = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000165#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000166#ifdef LIBXML_PATTERN_ENABLED
167static const char *pattern = NULL;
168static xmlPatternPtr patternc = NULL;
169#endif
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000170static int options = 0;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000171
Daniel Veillard01db67c2001-12-18 07:09:59 +0000172/*
173 * Internal timing routines to remove the necessity to have unix-specific
174 * function calls
175 */
176
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000177#ifndef HAVE_GETTIMEOFDAY
178#ifdef HAVE_SYS_TIMEB_H
179#ifdef HAVE_SYS_TIME_H
180#ifdef HAVE_FTIME
181
Daniel Veillard01c13b52002-12-10 15:19:08 +0000182static int
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000183my_gettimeofday(struct timeval *tvp, void *tzp)
184{
185 struct timeb timebuffer;
186
187 ftime(&timebuffer);
188 if (tvp) {
189 tvp->tv_sec = timebuffer.time;
190 tvp->tv_usec = timebuffer.millitm * 1000L;
191 }
192 return (0);
193}
194#define HAVE_GETTIMEOFDAY 1
195#define gettimeofday my_gettimeofday
196
197#endif /* HAVE_FTIME */
198#endif /* HAVE_SYS_TIME_H */
199#endif /* HAVE_SYS_TIMEB_H */
200#endif /* !HAVE_GETTIMEOFDAY */
201
Daniel Veillard01db67c2001-12-18 07:09:59 +0000202#if defined(HAVE_GETTIMEOFDAY)
203static struct timeval begin, end;
204
205/*
206 * startTimer: call where you want to start timing
207 */
208static void
209startTimer(void)
210{
211 gettimeofday(&begin, NULL);
212}
213
214/*
215 * endTimer: call where you want to stop timing and to print out a
216 * message about the timing performed; format is a printf
217 * type argument
218 */
219static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000220endTimer(const char *fmt, ...)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000221{
222 long msec;
223 va_list ap;
224
225 gettimeofday(&end, NULL);
226 msec = end.tv_sec - begin.tv_sec;
227 msec *= 1000;
228 msec += (end.tv_usec - begin.tv_usec) / 1000;
229
230#ifndef HAVE_STDARG_H
231#error "endTimer required stdarg functions"
232#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000233 va_start(ap, fmt);
234 vfprintf(stderr, fmt, ap);
Daniel Veillard01db67c2001-12-18 07:09:59 +0000235 va_end(ap);
236
237 fprintf(stderr, " took %ld ms\n", msec);
238}
239#elif defined(HAVE_TIME_H)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000240/*
241 * No gettimeofday function, so we have to make do with calling clock.
242 * This is obviously less accurate, but there's little we can do about
243 * that.
244 */
Daniel Veillard90bc3712002-03-07 15:12:58 +0000245#ifndef CLOCKS_PER_SEC
246#define CLOCKS_PER_SEC 100
247#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +0000248
249static clock_t begin, end;
250static void
251startTimer(void)
252{
253 begin = clock();
254}
255static void
256endTimer(const char *fmt, ...)
257{
258 long msec;
259 va_list ap;
260
261 end = clock();
262 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
263
264#ifndef HAVE_STDARG_H
265#error "endTimer required stdarg functions"
266#endif
267 va_start(ap, fmt);
268 vfprintf(stderr, fmt, ap);
269 va_end(ap);
270 fprintf(stderr, " took %ld ms\n", msec);
271}
272#else
273
274/*
275 * We don't have a gettimeofday or time.h, so we just don't do timing
276 */
277static void
278startTimer(void)
279{
280 /*
281 * Do nothing
282 */
283}
284static void
285endTimer(char *format, ...)
286{
287 /*
288 * We cannot do anything because we don't have a timing function
289 */
290#ifdef HAVE_STDARG_H
291 va_start(ap, format);
292 vfprintf(stderr, format, ap);
293 va_end(ap);
294 fprintf(stderr, " was not timed\n", msec);
295#else
296 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
297 * this ?!
298 */
299#endif
300}
301#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000302/************************************************************************
303 * *
304 * HTML ouput *
305 * *
306 ************************************************************************/
307char buffer[50000];
308
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000309static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000310xmlHTMLEncodeSend(void) {
311 char *result;
312
313 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
314 if (result) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000315 xmlGenericError(xmlGenericErrorContext, "%s", result);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000316 xmlFree(result);
317 }
318 buffer[0] = 0;
319}
320
321/**
322 * xmlHTMLPrintFileInfo:
323 * @input: an xmlParserInputPtr input
324 *
325 * Displays the associated file and line informations for the current input
326 */
327
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000328static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000329xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000330 int len;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000331 xmlGenericError(xmlGenericErrorContext, "<p>");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000332
333 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000334 if (input != NULL) {
335 if (input->filename) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000336 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000337 input->line);
338 } else {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000339 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000340 }
341 }
342 xmlHTMLEncodeSend();
343}
344
345/**
346 * xmlHTMLPrintFileContext:
347 * @input: an xmlParserInputPtr input
348 *
349 * Displays current context within the input content for error tracking
350 */
351
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000352static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000353xmlHTMLPrintFileContext(xmlParserInputPtr input) {
354 const xmlChar *cur, *base;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000355 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000356 int n;
357
358 if (input == NULL) return;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000359 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000360 cur = input->cur;
361 base = input->base;
362 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
363 cur--;
364 }
365 n = 0;
366 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
367 cur--;
368 if ((*cur == '\n') || (*cur == '\r')) cur++;
369 base = cur;
370 n = 0;
371 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000372 len = strlen(buffer);
373 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
374 (unsigned char) *cur++);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000375 n++;
376 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000377 len = strlen(buffer);
378 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000379 cur = input->cur;
380 while ((*cur == '\n') || (*cur == '\r'))
381 cur--;
382 n = 0;
383 while ((cur != base) && (n++ < 80)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000384 len = strlen(buffer);
385 snprintf(&buffer[len], sizeof(buffer) - len, " ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000386 base++;
387 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000388 len = strlen(buffer);
389 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000390 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000391 xmlGenericError(xmlGenericErrorContext, "</pre>");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000392}
393
394/**
395 * xmlHTMLError:
396 * @ctx: an XML parser context
397 * @msg: the message to display/transmit
398 * @...: extra parameters for the message display
399 *
400 * Display and format an error messages, gives file, line, position and
401 * extra parameters.
402 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000403static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000404xmlHTMLError(void *ctx, const char *msg, ...)
405{
406 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
407 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000408 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000409 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000410
411 buffer[0] = 0;
412 input = ctxt->input;
413 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000414 input = ctxt->inputTab[ctxt->inputNr - 2];
415 }
416
417 xmlHTMLPrintFileInfo(input);
418
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000419 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000420 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000421 len = strlen(buffer);
422 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000423 va_end(args);
424 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000425 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000426
427 xmlHTMLPrintFileContext(input);
428 xmlHTMLEncodeSend();
429}
430
431/**
432 * xmlHTMLWarning:
433 * @ctx: an XML parser context
434 * @msg: the message to display/transmit
435 * @...: extra parameters for the message display
436 *
437 * Display and format a warning messages, gives file, line, position and
438 * extra parameters.
439 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000440static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000441xmlHTMLWarning(void *ctx, const char *msg, ...)
442{
443 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
444 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000445 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000446 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000447
448 buffer[0] = 0;
449 input = ctxt->input;
450 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000451 input = ctxt->inputTab[ctxt->inputNr - 2];
452 }
453
454
455 xmlHTMLPrintFileInfo(input);
456
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000457 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000458 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000459 len = strlen(buffer);
460 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000461 va_end(args);
462 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000463 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000464
465 xmlHTMLPrintFileContext(input);
466 xmlHTMLEncodeSend();
467}
468
469/**
470 * xmlHTMLValidityError:
471 * @ctx: an XML parser context
472 * @msg: the message to display/transmit
473 * @...: extra parameters for the message display
474 *
475 * Display and format an validity error messages, gives file,
476 * line, position and extra parameters.
477 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000478static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000479xmlHTMLValidityError(void *ctx, const char *msg, ...)
480{
481 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
482 xmlParserInputPtr input;
483 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000484 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000485
486 buffer[0] = 0;
487 input = ctxt->input;
488 if ((input->filename == NULL) && (ctxt->inputNr > 1))
489 input = ctxt->inputTab[ctxt->inputNr - 2];
490
491 xmlHTMLPrintFileInfo(input);
492
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000493 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000494 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000495 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000496 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000497 va_end(args);
498 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000499 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000500
501 xmlHTMLPrintFileContext(input);
502 xmlHTMLEncodeSend();
503}
504
505/**
506 * xmlHTMLValidityWarning:
507 * @ctx: an XML parser context
508 * @msg: the message to display/transmit
509 * @...: extra parameters for the message display
510 *
511 * Display and format a validity warning messages, gives file, line,
512 * position and extra parameters.
513 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000514static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000515xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
516{
517 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
518 xmlParserInputPtr input;
519 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000520 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000521
522 buffer[0] = 0;
523 input = ctxt->input;
524 if ((input->filename == NULL) && (ctxt->inputNr > 1))
525 input = ctxt->inputTab[ctxt->inputNr - 2];
526
527 xmlHTMLPrintFileInfo(input);
528
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000529 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000530 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000531 len = strlen(buffer);
532 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000533 va_end(args);
534 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000535 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000536
537 xmlHTMLPrintFileContext(input);
538 xmlHTMLEncodeSend();
539}
540
541/************************************************************************
542 * *
543 * Shell Interface *
544 * *
545 ************************************************************************/
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000546#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000547/**
548 * xmlShellReadline:
549 * @prompt: the prompt value
550 *
551 * Read a string
552 *
553 * Returns a pointer to it or NULL on EOF the caller is expected to
554 * free the returned string.
555 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000556static char *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000557xmlShellReadline(char *prompt) {
558#ifdef HAVE_LIBREADLINE
559 char *line_read;
560
561 /* Get a line from the user. */
562 line_read = readline (prompt);
563
564 /* If the line has any text in it, save it on the history. */
565 if (line_read && *line_read)
566 add_history (line_read);
567
568 return (line_read);
569#else
570 char line_read[501];
Daniel Veillard29e43992001-12-13 22:21:58 +0000571 char *ret;
572 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000573
574 if (prompt != NULL)
575 fprintf(stdout, "%s", prompt);
576 if (!fgets(line_read, 500, stdin))
577 return(NULL);
578 line_read[500] = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000579 len = strlen(line_read);
580 ret = (char *) malloc(len + 1);
581 if (ret != NULL) {
582 memcpy (ret, line_read, len + 1);
583 }
584 return(ret);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000585#endif
586}
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000587#endif /* LIBXML_DEBUG_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000588
589/************************************************************************
590 * *
Daniel Veillard5e873c42000-04-12 13:27:38 +0000591 * I/O Interfaces *
592 * *
593 ************************************************************************/
594
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000595static int myRead(FILE *f, char * buf, int len) {
596 return(fread(buf, 1, len, f));
Daniel Veillard5e873c42000-04-12 13:27:38 +0000597}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000598static void myClose(FILE *f) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000599 if (f != stdin) {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000600 fclose(f);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000601 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000602}
603
Daniel Veillard81273902003-09-30 00:43:48 +0000604#ifdef LIBXML_READER_ENABLED
Daniel Veillard5e873c42000-04-12 13:27:38 +0000605/************************************************************************
606 * *
Daniel Veillard7704fb12003-01-03 16:19:51 +0000607 * Stream Test processing *
608 * *
609 ************************************************************************/
Daniel Veillard7704fb12003-01-03 16:19:51 +0000610static void processNode(xmlTextReaderPtr reader) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000611 const xmlChar *name, *value;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000612
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000613 name = xmlTextReaderConstName(reader);
Daniel Veillarde59494f2003-01-04 16:35:29 +0000614 if (name == NULL)
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000615 name = BAD_CAST "--";
Daniel Veillard99737f52003-03-22 14:55:50 +0000616
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000617 value = xmlTextReaderConstValue(reader);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000618
Daniel Veillard99737f52003-03-22 14:55:50 +0000619 printf("%d %d %s %d %d",
Daniel Veillarde59494f2003-01-04 16:35:29 +0000620 xmlTextReaderDepth(reader),
621 xmlTextReaderNodeType(reader),
622 name,
Daniel Veillard99737f52003-03-22 14:55:50 +0000623 xmlTextReaderIsEmptyElement(reader),
624 xmlTextReaderHasValue(reader));
Daniel Veillarde59494f2003-01-04 16:35:29 +0000625 if (value == NULL)
626 printf("\n");
627 else {
628 printf(" %s\n", value);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000629 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000630#ifdef LIBXML_PATTERN_ENABLED
631 if (patternc) {
632 if (xmlPatternMatch(patternc, xmlTextReaderCurrentNode(reader)) == 1) {
633 printf("Node matches pattern %s\n", pattern);
634 }
635 }
636#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000637}
638
639static void streamFile(char *filename) {
640 xmlTextReaderPtr reader;
641 int ret;
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000642#ifdef HAVE_SYS_MMAN_H
643 int fd = -1;
644 struct stat info;
645 const char *base = NULL;
646 xmlParserInputBufferPtr input = NULL;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000647
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000648 if (memory) {
649 if (stat(filename, &info) < 0)
650 return;
651 if ((fd = open(filename, O_RDONLY)) < 0)
652 return;
653 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
654 if (base == (void *) MAP_FAILED)
655 return;
656
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000657 reader = xmlReaderForMemory(base, info.st_size, filename,
658 NULL, options);
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000659 } else
660#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000661 reader = xmlReaderForFile(filename, NULL, options);
662
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000663
Daniel Veillard7704fb12003-01-03 16:19:51 +0000664 if (reader != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +0000665#ifdef LIBXML_VALID_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000666 if (valid)
667 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +0000668 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000669#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce192eb2003-04-16 15:58:05 +0000670 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000671#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +0000672 if (relaxng != NULL) {
Daniel Veillard81514ba2003-09-16 23:17:26 +0000673 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000674 startTimer();
675 }
676 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
677 if (ret < 0) {
678 xmlGenericError(xmlGenericErrorContext,
679 "Relax-NG schema %s failed to compile\n", relaxng);
Daniel Veillard05d987b2003-10-08 11:54:57 +0000680 progresult = 5;
Daniel Veillardce192eb2003-04-16 15:58:05 +0000681 relaxng = NULL;
682 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000683 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000684 endTimer("Compiling the schemas");
685 }
686 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000687#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000688
689 /*
690 * Process all nodes in sequence
691 */
Daniel Veillard81514ba2003-09-16 23:17:26 +0000692 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000693 startTimer();
694 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000695 ret = xmlTextReaderRead(reader);
696 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000697 if ((debug)
698#ifdef LIBXML_PATTERN_ENABLED
699 || (patternc)
700#endif
701 )
Daniel Veillard7704fb12003-01-03 16:19:51 +0000702 processNode(reader);
703 ret = xmlTextReaderRead(reader);
704 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000705 if ((timing) && (!repeat)) {
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000706#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +0000707 if ((valid) || (relaxng != NULL))
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000708#else
Daniel Veillard4432df22003-09-28 18:58:27 +0000709#ifdef LIBXML_VALID_ENABLED
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000710 if (valid)
Daniel Veillardce192eb2003-04-16 15:58:05 +0000711 endTimer("Parsing and validating");
712 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000713#endif /* LIBXML_VALID_ENABLED */
714#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +0000715 endTimer("Parsing");
716 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000717
Daniel Veillard4432df22003-09-28 18:58:27 +0000718#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf6bad792003-04-11 19:38:54 +0000719 if (valid) {
720 if (xmlTextReaderIsValid(reader) != 1) {
721 xmlGenericError(xmlGenericErrorContext,
722 "Document %s does not validate\n", filename);
723 progresult = 3;
724 }
725 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000726#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000727#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +0000728 if (relaxng != NULL) {
729 if (xmlTextReaderIsValid(reader) != 1) {
730 printf("%s fails to validate\n", filename);
731 progresult = 3;
732 } else {
733 printf("%s validates\n", filename);
734 }
735 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000736#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000737 /*
738 * Done, cleanup and status
739 */
740 xmlFreeTextReader(reader);
741 if (ret != 0) {
742 printf("%s : failed to parse\n", filename);
Daniel Veillardf6bad792003-04-11 19:38:54 +0000743 progresult = 1;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000744 }
745 } else {
746 fprintf(stderr, "Unable to open %s\n", filename);
Daniel Veillardf6bad792003-04-11 19:38:54 +0000747 progresult = 1;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000748 }
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000749#ifdef HAVE_SYS_MMAN_H
750 if (memory) {
751 xmlFreeParserInputBuffer(input);
752 munmap((char *) base, info.st_size);
753 close(fd);
754 }
755#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000756}
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000757
758static void walkDoc(xmlDocPtr doc) {
759 xmlTextReaderPtr reader;
760 int ret;
761
762 reader = xmlReaderWalker(doc);
763 if (reader != NULL) {
764 if ((timing) && (!repeat)) {
765 startTimer();
766 }
767 ret = xmlTextReaderRead(reader);
768 while (ret == 1) {
Daniel Veillardb3de70c2003-12-02 22:32:15 +0000769 if ((debug)
770#ifdef LIBXML_PATTERN_ENABLED
771 || (patternc)
772#endif
773 )
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000774 processNode(reader);
775 ret = xmlTextReaderRead(reader);
776 }
777 if ((timing) && (!repeat)) {
778 endTimer("walking through the doc");
779 }
780 xmlFreeTextReader(reader);
781 if (ret != 0) {
782 printf("failed to walk through the doc\n");
783 progresult = 1;
784 }
785 } else {
786 fprintf(stderr, "Failed to crate a reader from the document\n");
787 progresult = 1;
788 }
789}
Daniel Veillard81273902003-09-30 00:43:48 +0000790#endif /* LIBXML_READER_ENABLED */
Daniel Veillard7704fb12003-01-03 16:19:51 +0000791
792/************************************************************************
793 * *
794 * Tree Test processing *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000795 * *
796 ************************************************************************/
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000797static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
Daniel Veillard652327a2003-09-29 18:02:38 +0000798 xmlDocPtr doc = NULL;
799#ifdef LIBXML_TREE_ENABLED
800 xmlDocPtr tmp;
801#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000802
Daniel Veillard48b2f892001-02-25 16:11:03 +0000803 if ((timing) && (!repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +0000804 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000805
806
Daniel Veillard652327a2003-09-29 18:02:38 +0000807#ifdef LIBXML_TREE_ENABLED
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000808 if (filename == NULL) {
809 if (generate) {
810 xmlNodePtr n;
811
812 doc = xmlNewDoc(BAD_CAST "1.0");
813 n = xmlNewNode(NULL, BAD_CAST "info");
814 xmlNodeSetContent(n, BAD_CAST "abc");
815 xmlDocSetRootElement(doc, n);
816 }
817 }
Daniel Veillard652327a2003-09-29 18:02:38 +0000818#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000819#ifdef LIBXML_HTML_ENABLED
Daniel Veillard73b013f2003-09-30 12:36:01 +0000820#ifdef LIBXML_PUSH_ENABLED
William M. Brack78637da2003-07-31 14:47:38 +0000821 else if ((html) && (push)) {
822 FILE *f;
823
824 f = fopen(filename, "r");
825 if (f != NULL) {
826 int res, size = 3;
827 char chars[4096];
828 htmlParserCtxtPtr ctxt;
829
830 /* if (repeat) */
831 size = 4096;
832 res = fread(chars, 1, 4, f);
833 if (res > 0) {
834 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
William M. Brack1d75c8a2003-10-27 13:48:16 +0000835 chars, res, filename, XML_CHAR_ENCODING_NONE);
William M. Brack78637da2003-07-31 14:47:38 +0000836 while ((res = fread(chars, 1, size, f)) > 0) {
837 htmlParseChunk(ctxt, chars, res, 0);
838 }
839 htmlParseChunk(ctxt, chars, 0, 1);
840 doc = ctxt->myDoc;
841 htmlFreeParserCtxt(ctxt);
842 }
843 fclose(f);
844 }
845 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000846#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000847 else if (html) {
Daniel Veillard9475a352003-09-26 12:47:50 +0000848 doc = htmlReadFile(filename, NULL, options);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000849 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000850#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000851 else {
Daniel Veillard73b013f2003-09-30 12:36:01 +0000852#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000853 /*
854 * build an XML tree from a string;
855 */
856 if (push) {
857 FILE *f;
858
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000859 /* '-' Usually means stdin -<sven@zen.org> */
860 if ((filename[0] == '-') && (filename[1] == 0)) {
861 f = stdin;
862 } else {
863 f = fopen(filename, "r");
864 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000865 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +0000866 int ret;
Daniel Veillarda880b122003-04-21 21:36:41 +0000867 int res, size = 1024;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000868 char chars[1024];
869 xmlParserCtxtPtr ctxt;
870
Daniel Veillarda880b122003-04-21 21:36:41 +0000871 /* if (repeat) size = 1024; */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000872 res = fread(chars, 1, 4, f);
873 if (res > 0) {
874 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
875 chars, res, filename);
876 while ((res = fread(chars, 1, size, f)) > 0) {
877 xmlParseChunk(ctxt, chars, res, 0);
878 }
879 xmlParseChunk(ctxt, chars, 0, 1);
880 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +0000881 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000882 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +0000883 if (!ret) {
884 xmlFreeDoc(doc);
885 doc = NULL;
886 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000887 }
888 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000889 } else
890#endif /* LIBXML_PUSH_ENABLED */
891 if (testIO) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000892 if ((filename[0] == '-') && (filename[1] == 0)) {
Daniel Veillard60942de2003-09-25 21:05:58 +0000893 doc = xmlReadFd(0, NULL, NULL, options);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000894 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000895 FILE *f;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000896
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000897 f = fopen(filename, "r");
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000898 if (f != NULL) {
899 if (rectxt == NULL)
900 doc = xmlReadIO((xmlInputReadCallback) myRead,
901 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +0000902 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000903 else
904 doc = xmlCtxtReadIO(rectxt,
905 (xmlInputReadCallback) myRead,
906 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +0000907 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000908 } else
Daniel Veillard5e873c42000-04-12 13:27:38 +0000909 doc = NULL;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000910 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000911 } else if (htmlout) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000912 xmlParserCtxtPtr ctxt;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000913
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000914 if (rectxt == NULL)
915 ctxt = xmlNewParserCtxt();
916 else
917 ctxt = rectxt;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000918 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000919 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000920 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000921 ctxt->sax->error = xmlHTMLError;
922 ctxt->sax->warning = xmlHTMLWarning;
923 ctxt->vctxt.error = xmlHTMLValidityError;
924 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000925
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000926 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000927
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000928 if (rectxt == NULL)
929 xmlFreeParserCtxt(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000930 }
Daniel Veillard46e370e2000-07-21 20:32:03 +0000931#ifdef HAVE_SYS_MMAN_H
932 } else if (memory) {
933 int fd;
934 struct stat info;
935 const char *base;
936 if (stat(filename, &info) < 0)
937 return;
938 if ((fd = open(filename, O_RDONLY)) < 0)
939 return;
940 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +0000941 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +0000942 return;
943
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000944 if (rectxt == NULL)
Daniel Veillard60942de2003-09-25 21:05:58 +0000945 doc = xmlReadMemory((char *) base, info.st_size,
946 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000947 else
Daniel Veillard60942de2003-09-25 21:05:58 +0000948 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
949 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000950
Daniel Veillard46e370e2000-07-21 20:32:03 +0000951 munmap((char *) base, info.st_size);
952#endif
Daniel Veillard4432df22003-09-28 18:58:27 +0000953#ifdef LIBXML_VALID_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +0000954 } else if (valid) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000955 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000956
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000957 if (rectxt == NULL)
958 ctxt = xmlNewParserCtxt();
959 else
960 ctxt = rectxt;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000961 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000962 doc = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000963 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000964 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
965
966 if (ctxt->valid == 0)
967 progresult = 4;
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000968 if (rectxt == NULL)
969 xmlFreeParserCtxt(ctxt);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000970 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000971#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +0000972 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000973 if (rectxt != NULL)
974 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
975 else
976 doc = xmlReadFile(filename, NULL, options);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000977 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000978 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000979
Daniel Veillard88a172f2000-08-04 18:23:10 +0000980 /*
981 * If we don't have a document we might as well give up. Do we
982 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000983 if (doc == NULL) {
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000984 progresult = 1;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000985 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000986 }
987
Daniel Veillard48b2f892001-02-25 16:11:03 +0000988 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000989 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000990 }
991
Daniel Veillard29e43992001-12-13 22:21:58 +0000992 /*
993 * Remove DOCTYPE nodes
994 */
995 if (dropdtd) {
996 xmlDtdPtr dtd;
997
998 dtd = xmlGetIntSubset(doc);
999 if (dtd != NULL) {
1000 xmlUnlinkNode((xmlNodePtr)dtd);
1001 xmlFreeDtd(dtd);
1002 }
1003 }
1004
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001005#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +00001006 if (xinclude) {
1007 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001008 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001009 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001010 xmlXIncludeProcess(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001011 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001012 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001013 }
1014 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001015#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +00001016
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001017#ifdef LIBXML_DEBUG_ENABLED
1018 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001019 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001020 */
1021 if (shell)
1022 xmlShell(doc, filename, xmlShellReadline, stdout);
1023#endif
1024
Daniel Veillard652327a2003-09-29 18:02:38 +00001025#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001026 /*
1027 * test intermediate copy if needed.
1028 */
1029 if (copy) {
1030 tmp = doc;
1031 doc = xmlCopyDoc(doc, 1);
1032 xmlFreeDoc(tmp);
1033 }
Daniel Veillard652327a2003-09-29 18:02:38 +00001034#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001035
Daniel Veillard4432df22003-09-28 18:58:27 +00001036#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001037 if ((insert) && (!html)) {
1038 const xmlChar* list[256];
1039 int nb, i;
1040 xmlNodePtr node;
1041
1042 if (doc->children != NULL) {
1043 node = doc->children;
1044 while ((node != NULL) && (node->last == NULL)) node = node->next;
1045 if (node != NULL) {
1046 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
1047 if (nb < 0) {
1048 printf("could not get valid list of elements\n");
1049 } else if (nb == 0) {
MDT 2001 John Fleckbbb9e432001-09-24 03:08:43 +00001050 printf("No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001051 } else {
MDT 2001 John Fleckbbb9e432001-09-24 03:08:43 +00001052 printf("%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001053 nb);
1054 for (i = 0;i < nb;i++) {
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001055 printf("%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001056 }
1057 }
1058 }
1059 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001060 }else
1061#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001062#ifdef LIBXML_READER_ENABLED
1063 if (walker) {
1064 walkDoc(doc);
1065 }
1066#endif /* LIBXML_READER_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001067#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001068 if (noout == 0) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001069 /*
1070 * print it.
1071 */
1072#ifdef LIBXML_DEBUG_ENABLED
1073 if (!debug) {
1074#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00001075 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001076 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001077 }
Daniel Veillard42fd4122003-11-04 08:47:48 +00001078#ifdef LIBXML_VALID_ENABLED
1079 if ((html) && (!xmlout)) {
1080 if (compress) {
1081 htmlSaveFile(output ? output : "-", doc);
1082 }
1083 else if (encoding != NULL) {
1084 if ( format ) {
1085 htmlSaveFileFormat(output ? output : "-", doc, encoding, 1);
1086 }
1087 else {
1088 htmlSaveFileFormat(output ? output : "-", doc, encoding, 0);
1089 }
1090 }
1091 else if (format) {
1092 htmlSaveFileFormat(output ? output : "-", doc, NULL, 1);
1093 }
1094 else {
1095 FILE *out;
1096 if (output == NULL)
1097 out = stdout;
1098 else {
1099 out = fopen(output,"wb");
1100 }
1101 if (out != NULL) {
1102 if (htmlDocDump(out, doc) < 0)
1103 progresult = 6;
1104
1105 if (output != NULL)
1106 fclose(out);
1107 } else {
1108 fprintf(stderr, "failed to open %s\n", output);
1109 progresult = 6;
1110 }
1111 }
1112 if ((timing) && (!repeat)) {
1113 endTimer("Saving");
1114 }
1115 } else
1116#endif
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001117#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001118 if (memory) {
1119 xmlChar *result;
1120 int len;
1121
1122 if (encoding != NULL) {
Daniel Veillardd536f702001-11-08 17:32:47 +00001123 if ( format ) {
1124 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
1125 } else {
1126 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
1127 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001128 } else {
Daniel Veillard90493a92001-08-14 14:12:47 +00001129 if (format)
1130 xmlDocDumpFormatMemory(doc, &result, &len, 1);
1131 else
1132 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001133 }
1134 if (result == NULL) {
1135 fprintf(stderr, "Failed to save\n");
1136 } else {
1137 write(1, result, len);
1138 xmlFree(result);
1139 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001140 } else
1141#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001142 if (compress) {
1143 xmlSaveFile(output ? output : "-", doc);
1144 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001145 else if (encoding != NULL) {
1146 if ( format ) {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001147 xmlSaveFormatFileEnc(output ? output : "-", doc, encoding, 1);
1148 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001149 else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001150 xmlSaveFileEnc(output ? output : "-", doc, encoding);
Daniel Veillardd536f702001-11-08 17:32:47 +00001151 }
1152 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001153 else if (format) {
1154 xmlSaveFormatFile(output ? output : "-", doc, 1);
1155 }
1156 else {
1157 FILE *out;
1158 if (output == NULL)
1159 out = stdout;
1160 else {
1161 out = fopen(output,"wb");
1162 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001163 if (out != NULL) {
Daniel Veillard828ce832003-10-08 19:19:10 +00001164 if (xmlDocDump(out, doc) < 0)
1165 progresult = 6;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001166
Daniel Veillard05d987b2003-10-08 11:54:57 +00001167 if (output != NULL)
1168 fclose(out);
1169 } else {
1170 fprintf(stderr, "failed to open %s\n", output);
1171 progresult = 6;
1172 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001173 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001174 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001175 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001176 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001177#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001178 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001179 FILE *out;
1180 if (output == NULL)
1181 out = stdout;
1182 else {
1183 out = fopen(output,"wb");
1184 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001185 if (out != NULL) {
1186 xmlDebugDumpDocument(out, doc);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001187
Daniel Veillard05d987b2003-10-08 11:54:57 +00001188 if (output != NULL)
1189 fclose(out);
1190 } else {
1191 fprintf(stderr, "failed to open %s\n", output);
1192 progresult = 6;
1193 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001194 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001195#endif
1196 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001197#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001198
Daniel Veillard4432df22003-09-28 18:58:27 +00001199#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001200 /*
1201 * A posteriori validation test
1202 */
Daniel Veillard66f68e72003-08-18 16:39:51 +00001203 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
Daniel Veillardcd429612000-10-11 15:57:05 +00001204 xmlDtdPtr dtd;
1205
Daniel Veillard48b2f892001-02-25 16:11:03 +00001206 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001207 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001208 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00001209 if (dtdvalid != NULL)
1210 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
1211 else
1212 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001213 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001214 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001215 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001216 if (dtd == NULL) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001217 if (dtdvalid != NULL)
1218 xmlGenericError(xmlGenericErrorContext,
1219 "Could not parse DTD %s\n", dtdvalid);
1220 else
1221 xmlGenericError(xmlGenericErrorContext,
1222 "Could not parse DTD %s\n", dtdvalidfpi);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001223 progresult = 2;
Daniel Veillardcd429612000-10-11 15:57:05 +00001224 } else {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001225 xmlValidCtxtPtr cvp;
1226
1227 if ((cvp = xmlNewValidCtxt()) == NULL) {
1228 xmlGenericError(xmlGenericErrorContext,
1229 "Couldn't allocate validation context\n");
1230 exit(-1);
1231 }
1232 cvp->userData = (void *) stderr;
1233 cvp->error = (xmlValidityErrorFunc) fprintf;
1234 cvp->warning = (xmlValidityWarningFunc) fprintf;
1235
Daniel Veillard48b2f892001-02-25 16:11:03 +00001236 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001237 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001238 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001239 if (!xmlValidateDtd(cvp, doc, dtd)) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001240 if (dtdvalid != NULL)
1241 xmlGenericError(xmlGenericErrorContext,
1242 "Document %s does not validate against %s\n",
1243 filename, dtdvalid);
1244 else
1245 xmlGenericError(xmlGenericErrorContext,
1246 "Document %s does not validate against %s\n",
1247 filename, dtdvalidfpi);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001248 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +00001249 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001250 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001251 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001252 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001253 xmlFreeValidCtxt(cvp);
Daniel Veillardcd429612000-10-11 15:57:05 +00001254 xmlFreeDtd(dtd);
1255 }
1256 } else if (postvalid) {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001257 xmlValidCtxtPtr cvp;
1258
1259 if ((cvp = xmlNewValidCtxt()) == NULL) {
1260 xmlGenericError(xmlGenericErrorContext,
1261 "Couldn't allocate validation context\n");
1262 exit(-1);
1263 }
1264
Daniel Veillard48b2f892001-02-25 16:11:03 +00001265 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001266 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001267 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001268 cvp->userData = (void *) stderr;
1269 cvp->error = (xmlValidityErrorFunc) fprintf;
1270 cvp->warning = (xmlValidityWarningFunc) fprintf;
1271 if (!xmlValidateDocument(cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001272 xmlGenericError(xmlGenericErrorContext,
1273 "Document %s does not validate\n", filename);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001274 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +00001275 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001276 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001277 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001278 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001279 xmlFreeValidCtxt(cvp);
Daniel Veillard4432df22003-09-28 18:58:27 +00001280 }
1281#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00001282#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001283 if (relaxngschemas != NULL) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001284 xmlRelaxNGValidCtxtPtr ctxt;
1285 int ret;
1286
Daniel Veillard42f12e92003-03-07 18:32:59 +00001287 if ((timing) && (!repeat)) {
1288 startTimer();
1289 }
1290
Daniel Veillard71531f32003-02-05 13:19:53 +00001291 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
1292 xmlRelaxNGSetValidErrors(ctxt,
1293 (xmlRelaxNGValidityErrorFunc) fprintf,
1294 (xmlRelaxNGValidityWarningFunc) fprintf,
1295 stderr);
1296 ret = xmlRelaxNGValidateDoc(ctxt, doc);
1297 if (ret == 0) {
1298 printf("%s validates\n", filename);
1299 } else if (ret > 0) {
1300 printf("%s fails to validate\n", filename);
1301 } else {
1302 printf("%s validation generated an internal error\n",
1303 filename);
1304 }
1305 xmlRelaxNGFreeValidCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001306 if ((timing) && (!repeat)) {
1307 endTimer("Validating");
1308 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001309 } else if (wxschemas != NULL) {
1310 xmlSchemaValidCtxtPtr ctxt;
1311 int ret;
1312
1313 if ((timing) && (!repeat)) {
1314 startTimer();
1315 }
1316
1317 ctxt = xmlSchemaNewValidCtxt(wxschemas);
1318 xmlSchemaSetValidErrors(ctxt,
1319 (xmlSchemaValidityErrorFunc) fprintf,
1320 (xmlSchemaValidityWarningFunc) fprintf,
1321 stderr);
1322 ret = xmlSchemaValidateDoc(ctxt, doc);
1323 if (ret == 0) {
1324 printf("%s validates\n", filename);
1325 } else if (ret > 0) {
1326 printf("%s fails to validate\n", filename);
1327 } else {
1328 printf("%s validation generated an internal error\n",
1329 filename);
1330 }
1331 xmlSchemaFreeValidCtxt(ctxt);
1332 if ((timing) && (!repeat)) {
1333 endTimer("Validating");
1334 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001335 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001336#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001337
1338#ifdef LIBXML_DEBUG_ENABLED
1339 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001340 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001341#endif
1342
1343 /*
1344 * free it.
1345 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00001346 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001347 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001348 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001349 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001350 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001351 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001352 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001353}
1354
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001355/************************************************************************
1356 * *
1357 * Usage and Main *
1358 * *
1359 ************************************************************************/
1360
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001361static void showVersion(const char *name) {
1362 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
1363 fprintf(stderr, " compiled with: ");
Daniel Veillard4432df22003-09-28 18:58:27 +00001364#ifdef LIBXML_VALID_ENABLED
1365 fprintf(stderr, "DTDValid ");
1366#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001367#ifdef LIBXML_FTP_ENABLED
1368 fprintf(stderr, "FTP ");
1369#endif
1370#ifdef LIBXML_HTTP_ENABLED
1371 fprintf(stderr, "HTTP ");
1372#endif
1373#ifdef LIBXML_HTML_ENABLED
1374 fprintf(stderr, "HTML ");
1375#endif
1376#ifdef LIBXML_C14N_ENABLED
1377 fprintf(stderr, "C14N ");
1378#endif
1379#ifdef LIBXML_CATALOG_ENABLED
1380 fprintf(stderr, "Catalog ");
1381#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001382#ifdef LIBXML_XPATH_ENABLED
1383 fprintf(stderr, "XPath ");
1384#endif
1385#ifdef LIBXML_XPTR_ENABLED
1386 fprintf(stderr, "XPointer ");
1387#endif
1388#ifdef LIBXML_XINCLUDE_ENABLED
1389 fprintf(stderr, "XInclude ");
1390#endif
1391#ifdef LIBXML_ICONV_ENABLED
1392 fprintf(stderr, "Iconv ");
1393#endif
1394#ifdef DEBUG_MEMORY_LOCATION
1395 fprintf(stderr, "MemDebug ");
1396#endif
1397#ifdef LIBXML_UNICODE_ENABLED
1398 fprintf(stderr, "Unicode ");
1399#endif
1400#ifdef LIBXML_REGEXP_ENABLED
1401 fprintf(stderr, "Regexps ");
1402#endif
1403#ifdef LIBXML_AUTOMATA_ENABLED
1404 fprintf(stderr, "Automata ");
1405#endif
1406#ifdef LIBXML_SCHEMAS_ENABLED
1407 fprintf(stderr, "Schemas ");
1408#endif
1409 fprintf(stderr, "\n");
1410}
1411
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001412static void usage(const char *name) {
1413 printf("Usage : %s [options] XMLfiles ...\n", name);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001414#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001415 printf("\tParse the XML files and output the result of the parsing\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001416#else
1417 printf("\tParse the XML files\n");
1418#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001419 printf("\t--version : display the version of the XML library used\n");
1420#ifdef LIBXML_DEBUG_ENABLED
1421 printf("\t--debug : dump a debug tree of the in-memory document\n");
1422 printf("\t--shell : run a navigating shell\n");
1423 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00001424#else
Daniel Veillard81273902003-09-30 00:43:48 +00001425#ifdef LIBXML_READER_ENABLED
Daniel Veillard8326e732003-01-07 00:19:07 +00001426 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001427#endif /* LIBXML_READER_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001428#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001429#ifdef LIBXML_TREE_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001430 printf("\t--copy : used to test the internal copy implementation\n");
Daniel Veillard652327a2003-09-29 18:02:38 +00001431#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001432 printf("\t--recover : output what was parsable on broken XML documents\n");
1433 printf("\t--noent : substitute entity references by their value\n");
1434 printf("\t--noout : don't output the result tree\n");
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001435 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001436 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001437 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001438#ifdef LIBXML_VALID_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001439 printf("\t--valid : validate the document in addition to std well-formed check\n");
1440 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
1441 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
Daniel Veillard66f68e72003-08-18 16:39:51 +00001442 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001443#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001444 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001445 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001446 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
1447 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001448#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001449#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001450 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001451#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001452#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001453#ifdef LIBXML_HTML_ENABLED
1454 printf("\t--html : use the HTML parser\n");
Daniel Veillard42fd4122003-11-04 08:47:48 +00001455 printf("\t--xmlout : force to use the XML serializer when using --html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001456#endif
Daniel Veillard73b013f2003-09-30 12:36:01 +00001457#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001458 printf("\t--push : use the push mode of the parser\n");
Daniel Veillard73b013f2003-09-30 12:36:01 +00001459#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001460#ifdef HAVE_SYS_MMAN_H
1461 printf("\t--memory : parse from memory\n");
1462#endif
1463 printf("\t--nowarning : do not emit warnings from parser/validator\n");
1464 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001465 printf("\t--nocdata : replace cdata section with text nodes\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001466#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001467 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001468 printf("\t--encode encoding : output in the given encoding\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001469 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
1470#endif /* LIBXML_OUTPUT_ENABLED */
1471 printf("\t--nsclean : remove redundant namespace declarations\n");
1472 printf("\t--testIO : test user I/O support\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001473#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00001474 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
1475 printf("\t otherwise XML Catalogs starting from \n");
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001476 printf("\t %s are activated by default\n", XML_XML_DEFAULT_CATALOG);
Daniel Veillard05c13a22001-09-09 08:38:09 +00001477 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001478#endif
1479 printf("\t--auto : generate a small doc on the fly\n");
1480#ifdef LIBXML_XINCLUDE_ENABLED
1481 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001482#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001483 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00001484 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard81273902003-09-30 00:43:48 +00001485#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001486 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001487 printf("\t--walker : create a reader and walk though the resulting doc\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001488#endif /* LIBXML_READER_ENABLED */
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001489#ifdef LIBXML_PATTERN_ENABLED
1490 printf("\t--pattern pattern_value : test the pattern support\n");
1491#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001492 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillardef4d3bc2003-02-07 12:38:22 +00001493#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard71531f32003-02-05 13:19:53 +00001494 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001495 printf("\t--schema schema : do validation against the WXS schema\n");
Daniel Veillard71531f32003-02-05 13:19:53 +00001496#endif
Daniel Veillarda42f25f2002-01-25 14:15:40 +00001497 printf("\nLibxml project home page: http://xmlsoft.org/\n");
1498 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001499}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001500
1501static void registerNode(xmlNodePtr node)
1502{
1503 node->_private = malloc(sizeof(long));
1504 *(long*)node->_private = (long) 0x81726354;
1505}
1506
1507static void deregisterNode(xmlNodePtr node)
1508{
1509 assert(node->_private != NULL);
1510 assert(*(long*)node->_private == (long) 0x81726354);
1511 free(node->_private);
1512}
1513
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001514int
1515main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001516 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001517 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00001518 int version = 0;
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001519 const char* indent;
1520
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001521 if (argc <= 1) {
1522 usage(argv[0]);
1523 return(1);
1524 }
Daniel Veillardbe803962000-06-28 23:40:59 +00001525 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001526 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001527 if (!strcmp(argv[i], "-"))
1528 break;
1529
1530 if (argv[i][0] != '-')
1531 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001532 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
1533 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00001534 else
1535#ifdef LIBXML_DEBUG_ENABLED
1536 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001537 (!strcmp(argv[i], "--shell"))) {
1538 shell++;
1539 noout = 1;
1540 } else
1541#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001542#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001543 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
1544 copy++;
Daniel Veillard652327a2003-09-29 18:02:38 +00001545 else
1546#endif /* LIBXML_TREE_ENABLED */
1547 if ((!strcmp(argv[i], "-recover")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001548 (!strcmp(argv[i], "--recover"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001549 recovery++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001550 options |= XML_PARSE_RECOVER;
1551 } else if ((!strcmp(argv[i], "-noent")) ||
1552 (!strcmp(argv[i], "--noent"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001553 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001554 options |= XML_PARSE_NOENT;
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001555 } else if ((!strcmp(argv[i], "-nsclean")) ||
1556 (!strcmp(argv[i], "--nsclean"))) {
1557 options |= XML_PARSE_NSCLEAN;
1558 } else if ((!strcmp(argv[i], "-nocdata")) ||
1559 (!strcmp(argv[i], "--nocdata"))) {
1560 options |= XML_PARSE_NOCDATA;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001561 } else if ((!strcmp(argv[i], "-nodict")) ||
1562 (!strcmp(argv[i], "--nodict"))) {
1563 options |= XML_PARSE_NODICT;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001564 } else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00001565 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001566 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00001567 version = 1;
1568 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001569 (!strcmp(argv[i], "--noout")))
1570 noout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001571#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001572 else if ((!strcmp(argv[i], "-o")) ||
1573 (!strcmp(argv[i], "-output")) ||
1574 (!strcmp(argv[i], "--output"))) {
1575 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00001576 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001577 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001578#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001579 else if ((!strcmp(argv[i], "-htmlout")) ||
1580 (!strcmp(argv[i], "--htmlout")))
1581 htmlout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001582 else if ((!strcmp(argv[i], "-nowrap")) ||
1583 (!strcmp(argv[i], "--nowrap")))
1584 nowrap++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001585#ifdef LIBXML_HTML_ENABLED
1586 else if ((!strcmp(argv[i], "-html")) ||
1587 (!strcmp(argv[i], "--html"))) {
1588 html++;
1589 }
Daniel Veillard42fd4122003-11-04 08:47:48 +00001590 else if ((!strcmp(argv[i], "-xmlout")) ||
1591 (!strcmp(argv[i], "--xmlout"))) {
1592 xmlout++;
1593 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001594#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001595 else if ((!strcmp(argv[i], "-loaddtd")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001596 (!strcmp(argv[i], "--loaddtd"))) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001597 loaddtd++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001598 options |= XML_PARSE_DTDLOAD;
1599 } else if ((!strcmp(argv[i], "-dtdattr")) ||
Daniel Veillard48da9102001-08-07 01:10:10 +00001600 (!strcmp(argv[i], "--dtdattr"))) {
1601 loaddtd++;
1602 dtdattrs++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001603 options |= XML_PARSE_DTDATTR;
Daniel Veillard4432df22003-09-28 18:58:27 +00001604 }
1605#ifdef LIBXML_VALID_ENABLED
1606 else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001607 (!strcmp(argv[i], "--valid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001608 valid++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001609 options |= XML_PARSE_DTDVALID;
1610 } else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001611 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001612 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001613 loaddtd++;
1614 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00001615 (!strcmp(argv[i], "--dtdvalid"))) {
1616 i++;
1617 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001618 loaddtd++;
Daniel Veillard66f68e72003-08-18 16:39:51 +00001619 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
1620 (!strcmp(argv[i], "--dtdvalidfpi"))) {
1621 i++;
1622 dtdvalidfpi = argv[i];
1623 loaddtd++;
Daniel Veillardcd429612000-10-11 15:57:05 +00001624 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001625#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard29e43992001-12-13 22:21:58 +00001626 else if ((!strcmp(argv[i], "-dropdtd")) ||
1627 (!strcmp(argv[i], "--dropdtd")))
1628 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001629 else if ((!strcmp(argv[i], "-insert")) ||
1630 (!strcmp(argv[i], "--insert")))
1631 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001632 else if ((!strcmp(argv[i], "-timing")) ||
1633 (!strcmp(argv[i], "--timing")))
1634 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001635 else if ((!strcmp(argv[i], "-auto")) ||
1636 (!strcmp(argv[i], "--auto")))
1637 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001638 else if ((!strcmp(argv[i], "-repeat")) ||
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001639 (!strcmp(argv[i], "--repeat"))) {
1640 if (repeat)
1641 repeat *= 10;
1642 else
1643 repeat = 100;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001644 }
1645#ifdef LIBXML_PUSH_ENABLED
1646 else if ((!strcmp(argv[i], "-push")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001647 (!strcmp(argv[i], "--push")))
1648 push++;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001649#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +00001650#ifdef HAVE_SYS_MMAN_H
1651 else if ((!strcmp(argv[i], "-memory")) ||
1652 (!strcmp(argv[i], "--memory")))
1653 memory++;
1654#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00001655 else if ((!strcmp(argv[i], "-testIO")) ||
1656 (!strcmp(argv[i], "--testIO")))
1657 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001658#ifdef LIBXML_XINCLUDE_ENABLED
1659 else if ((!strcmp(argv[i], "-xinclude")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001660 (!strcmp(argv[i], "--xinclude"))) {
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001661 xinclude++;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001662 options |= XML_PARSE_XINCLUDE;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001663 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001664#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001665#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001666#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001667 else if ((!strcmp(argv[i], "-compress")) ||
1668 (!strcmp(argv[i], "--compress"))) {
1669 compress++;
1670 xmlSetCompressMode(9);
1671 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001672#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001673#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001674 else if ((!strcmp(argv[i], "-nowarning")) ||
1675 (!strcmp(argv[i], "--nowarning"))) {
1676 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001677 xmlPedanticParserDefault(0);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001678 options |= XML_PARSE_NOWARNING;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001679 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001680 else if ((!strcmp(argv[i], "-pedantic")) ||
1681 (!strcmp(argv[i], "--pedantic"))) {
1682 xmlGetWarningsDefaultValue = 1;
1683 xmlPedanticParserDefault(1);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001684 options |= XML_PARSE_PEDANTIC;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001685 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001686#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001687 else if ((!strcmp(argv[i], "-debugent")) ||
1688 (!strcmp(argv[i], "--debugent"))) {
1689 debugent++;
1690 xmlParserDebugEntities = 1;
1691 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001692#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00001693#ifdef LIBXML_CATALOG_ENABLED
1694 else if ((!strcmp(argv[i], "-catalogs")) ||
1695 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001696 catalogs++;
1697 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
1698 (!strcmp(argv[i], "--nocatalogs"))) {
1699 nocatalogs++;
Daniel Veillard81418e32001-05-22 15:08:55 +00001700 }
1701#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00001702 else if ((!strcmp(argv[i], "-encode")) ||
1703 (!strcmp(argv[i], "--encode"))) {
1704 i++;
1705 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001706 /*
1707 * OK it's for testing purposes
1708 */
1709 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00001710 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001711 else if ((!strcmp(argv[i], "-noblanks")) ||
1712 (!strcmp(argv[i], "--noblanks"))) {
1713 noblanks++;
1714 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00001715 }
1716 else if ((!strcmp(argv[i], "-format")) ||
1717 (!strcmp(argv[i], "--format"))) {
1718 noblanks++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001719#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001720 format++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001721#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard90493a92001-08-14 14:12:47 +00001722 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001723 }
Daniel Veillard81273902003-09-30 00:43:48 +00001724#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001725 else if ((!strcmp(argv[i], "-stream")) ||
1726 (!strcmp(argv[i], "--stream"))) {
1727 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001728 }
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001729 else if ((!strcmp(argv[i], "-walker")) ||
1730 (!strcmp(argv[i], "--walker"))) {
1731 walker++;
1732 noout++;
1733 }
Daniel Veillard81273902003-09-30 00:43:48 +00001734#endif /* LIBXML_READER_ENABLED */
1735#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001736 else if ((!strcmp(argv[i], "-sax1")) ||
1737 (!strcmp(argv[i], "--sax1"))) {
1738 sax1++;
1739 }
Daniel Veillard81273902003-09-30 00:43:48 +00001740#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001741 else if ((!strcmp(argv[i], "-chkregister")) ||
1742 (!strcmp(argv[i], "--chkregister"))) {
1743 chkregister++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001744#ifdef LIBXML_SCHEMAS_ENABLED
1745 } else if ((!strcmp(argv[i], "-relaxng")) ||
1746 (!strcmp(argv[i], "--relaxng"))) {
1747 i++;
1748 relaxng = argv[i];
1749 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001750 options |= XML_PARSE_NOENT;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001751 } else if ((!strcmp(argv[i], "-schema")) ||
1752 (!strcmp(argv[i], "--schema"))) {
1753 i++;
1754 schema = argv[i];
1755 noent++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001756#endif
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001757 } else if ((!strcmp(argv[i], "-nonet")) ||
1758 (!strcmp(argv[i], "--nonet"))) {
Daniel Veillard61b93382003-11-03 14:28:31 +00001759 options |= XML_PARSE_NONET;
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001760#ifdef LIBXML_PATTERN_ENABLED
1761 } else if ((!strcmp(argv[i], "-pattern")) ||
1762 (!strcmp(argv[i], "--pattern"))) {
1763 i++;
1764 pattern = argv[i];
1765#endif
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001766 } else {
1767 fprintf(stderr, "Unknown option %s\n", argv[i]);
1768 usage(argv[0]);
1769 return(1);
1770 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001771 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001772
1773#ifdef LIBXML_CATALOG_ENABLED
1774 if (nocatalogs == 0) {
1775 if (catalogs) {
1776 const char *catal;
1777
1778 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00001779 if (catal != NULL) {
1780 xmlLoadCatalogs(catal);
1781 } else {
1782 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
1783 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001784 }
1785 }
1786#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001787
Daniel Veillard81273902003-09-30 00:43:48 +00001788#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001789 if (sax1)
1790 xmlSAXDefaultVersion(1);
1791 else
1792 xmlSAXDefaultVersion(2);
Daniel Veillard81273902003-09-30 00:43:48 +00001793#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00001794
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001795 if (chkregister) {
1796 xmlRegisterNodeDefault(registerNode);
1797 xmlDeregisterNodeDefault(deregisterNode);
1798 }
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001799
1800 indent = getenv("XMLLINT_INDENT");
1801 if(indent != NULL) {
1802 xmlTreeIndentString = indent;
1803 }
1804
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001805
Daniel Veillardd9bad132001-07-23 19:39:43 +00001806 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00001807 if (loaddtd != 0)
1808 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
1809 if (dtdattrs)
1810 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001811 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillard4432df22003-09-28 18:58:27 +00001812#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001813 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard4432df22003-09-28 18:58:27 +00001814#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001815 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001816 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001817 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001818 xmlGenericError(xmlGenericErrorContext,
1819 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
1820 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001821 "<html><head><title>%s output</title></head>\n",
1822 argv[0]);
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001823 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001824 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
1825 argv[0]);
1826 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001827
1828#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard81273902003-09-30 00:43:48 +00001829 if ((relaxng != NULL)
1830#ifdef LIBXML_READER_ENABLED
1831 && (stream == 0)
1832#endif /* LIBXML_READER_ENABLED */
1833 ) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001834 xmlRelaxNGParserCtxtPtr ctxt;
1835
Daniel Veillardce192eb2003-04-16 15:58:05 +00001836 /* forces loading the DTDs */
1837 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001838 options |= XML_PARSE_DTDLOAD;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001839 if (timing) {
1840 startTimer();
1841 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001842 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
1843 xmlRelaxNGSetParserErrors(ctxt,
1844 (xmlRelaxNGValidityErrorFunc) fprintf,
1845 (xmlRelaxNGValidityWarningFunc) fprintf,
1846 stderr);
1847 relaxngschemas = xmlRelaxNGParse(ctxt);
Daniel Veillardce192eb2003-04-16 15:58:05 +00001848 if (relaxngschemas == NULL) {
1849 xmlGenericError(xmlGenericErrorContext,
1850 "Relax-NG schema %s failed to compile\n", relaxng);
Daniel Veillard05d987b2003-10-08 11:54:57 +00001851 progresult = 5;
Daniel Veillardce192eb2003-04-16 15:58:05 +00001852 relaxng = NULL;
1853 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001854 xmlRelaxNGFreeParserCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001855 if (timing) {
1856 endTimer("Compiling the schemas");
1857 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001858 } else if ((schema != NULL) && (stream == 0)) {
1859 xmlSchemaParserCtxtPtr ctxt;
1860
1861 if (timing) {
1862 startTimer();
1863 }
1864 ctxt = xmlSchemaNewParserCtxt(schema);
1865 xmlSchemaSetParserErrors(ctxt,
1866 (xmlSchemaValidityErrorFunc) fprintf,
1867 (xmlSchemaValidityWarningFunc) fprintf,
1868 stderr);
1869 wxschemas = xmlSchemaParse(ctxt);
1870 if (wxschemas == NULL) {
1871 xmlGenericError(xmlGenericErrorContext,
1872 "WXS schema %s failed to compile\n", schema);
Daniel Veillard05d987b2003-10-08 11:54:57 +00001873 progresult = 5;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001874 schema = NULL;
1875 }
1876 xmlSchemaFreeParserCtxt(ctxt);
1877 if (timing) {
1878 endTimer("Compiling the schemas");
1879 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001880 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001881#endif /* LIBXML_SCHEMAS_ENABLED */
1882#ifdef LIBXML_PATTERN_ENABLED
1883 if (pattern != NULL) {
Daniel Veillardffa7b7e2003-12-05 16:10:21 +00001884 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001885 if (patternc == NULL) {
1886 xmlGenericError(xmlGenericErrorContext,
1887 "Pattern %s failed to compile\n", pattern);
1888 progresult = 7;
1889 pattern = NULL;
1890 }
1891 }
1892#endif /* LIBXML_PATTERN_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001893 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00001894 if ((!strcmp(argv[i], "-encode")) ||
1895 (!strcmp(argv[i], "--encode"))) {
1896 i++;
1897 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001898 } else if ((!strcmp(argv[i], "-o")) ||
1899 (!strcmp(argv[i], "-output")) ||
1900 (!strcmp(argv[i], "--output"))) {
1901 i++;
1902 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00001903 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001904#ifdef LIBXML_VALID_ENABLED
Daniel Veillardcd429612000-10-11 15:57:05 +00001905 if ((!strcmp(argv[i], "-dtdvalid")) ||
1906 (!strcmp(argv[i], "--dtdvalid"))) {
1907 i++;
1908 continue;
1909 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00001910 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
1911 (!strcmp(argv[i], "--dtdvalidfpi"))) {
1912 i++;
1913 continue;
1914 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001915#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00001916 if ((!strcmp(argv[i], "-relaxng")) ||
1917 (!strcmp(argv[i], "--relaxng"))) {
1918 i++;
1919 continue;
1920 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001921 if ((!strcmp(argv[i], "-schema")) ||
1922 (!strcmp(argv[i], "--schema"))) {
1923 i++;
1924 continue;
1925 }
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001926#ifdef LIBXML_PATTERN_ENABLED
1927 if ((!strcmp(argv[i], "-pattern")) ||
1928 (!strcmp(argv[i], "--pattern"))) {
1929 i++;
1930 continue;
1931 }
1932#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00001933 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00001934 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001935 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001936 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001937 if (repeat) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001938 xmlParserCtxtPtr ctxt = NULL;
1939
1940 for (acount = 0;acount < repeat;acount++) {
Daniel Veillard81273902003-09-30 00:43:48 +00001941#ifdef LIBXML_READER_ENABLED
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001942 if (stream != 0) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001943 streamFile(argv[i]);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001944 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00001945#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001946 if (ctxt == NULL)
1947 ctxt = xmlNewParserCtxt();
1948 parseAndPrintFile(argv[i], ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00001949#ifdef LIBXML_READER_ENABLED
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001950 }
Daniel Veillard81273902003-09-30 00:43:48 +00001951#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001952 }
1953 if (ctxt != NULL)
1954 xmlFreeParserCtxt(ctxt);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001955 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00001956#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001957 if (stream != 0)
1958 streamFile(argv[i]);
1959 else
Daniel Veillard81273902003-09-30 00:43:48 +00001960#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001961 parseAndPrintFile(argv[i], NULL);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001962 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001963 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00001964 if ((timing) && (repeat)) {
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001965 endTimer("%d iterations", repeat);
Daniel Veillarda7866932001-12-04 13:14:44 +00001966 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001967 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001968 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001969 if (generate)
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001970 parseAndPrintFile(NULL, NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001971 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001972 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001973 }
Daniel Veillard845cce42002-01-09 11:51:37 +00001974 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001975 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001976 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001977#ifdef LIBXML_SCHEMAS_ENABLED
1978 if (relaxngschemas != NULL)
1979 xmlRelaxNGFree(relaxngschemas);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001980 if (wxschemas != NULL)
1981 xmlSchemaFree(wxschemas);
Daniel Veillard71531f32003-02-05 13:19:53 +00001982 xmlRelaxNGCleanupTypes();
1983#endif
Daniel Veillardb3de70c2003-12-02 22:32:15 +00001984#ifdef LIBXML_PATTERN_ENABLED
1985 if (patternc != NULL)
1986 xmlFreePattern(patternc);
1987#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001988 xmlCleanupParser();
1989 xmlMemoryDump();
1990
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001991 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001992}
Daniel Veillard88a172f2000-08-04 18:23:10 +00001993