blob: 92b1c3e8fd9e18d4f2c8baf4deb5ad90b9e5b02c [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 Veillardce8b83b2000-04-05 18:38:42 +000092
Daniel Veillard3be27512003-01-26 19:49:04 +000093#ifndef XML_XML_DEFAULT_CATALOG
94#define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog"
95#endif
96
Daniel Veillardce8b83b2000-04-05 18:38:42 +000097#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +000098static int shell = 0;
99static int debugent = 0;
100#endif
Daniel Veillard8326e732003-01-07 00:19:07 +0000101static int debug = 0;
Daniel Veillard652327a2003-09-29 18:02:38 +0000102#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000103static int copy = 0;
Daniel Veillard652327a2003-09-29 18:02:38 +0000104#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000105static int recovery = 0;
106static int noent = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000107static int noblanks = 0;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000108static int noout = 0;
109static int nowrap = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000110#ifdef LIBXML_OUTPUT_ENABLED
111static int format = 0;
112static const char *output = NULL;
113static int compress = 0;
114#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4432df22003-09-28 18:58:27 +0000115#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000116static int valid = 0;
117static int postvalid = 0;
Daniel Veillardcd429612000-10-11 15:57:05 +0000118static char * dtdvalid = NULL;
Daniel Veillard66f68e72003-08-18 16:39:51 +0000119static char * dtdvalidfpi = NULL;
Daniel Veillard4432df22003-09-28 18:58:27 +0000120#endif
Daniel Veillard71531f32003-02-05 13:19:53 +0000121#ifdef LIBXML_SCHEMAS_ENABLED
122static char * relaxng = NULL;
123static xmlRelaxNGPtr relaxngschemas = NULL;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +0000124static char * schema = NULL;
125static xmlSchemaPtr wxschemas = NULL;
Daniel Veillard71531f32003-02-05 13:19:53 +0000126#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000127static int repeat = 0;
128static int insert = 0;
Daniel Veillard4432df22003-09-28 18:58:27 +0000129#ifdef LIBXML_HTML_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000130static int html = 0;
Daniel Veillard42fd4122003-11-04 08:47:48 +0000131static int xmlout = 0;
Daniel Veillard4432df22003-09-28 18:58:27 +0000132#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000133static int htmlout = 0;
Daniel Veillard73b013f2003-09-30 12:36:01 +0000134#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000135static int push = 0;
Daniel Veillard73b013f2003-09-30 12:36:01 +0000136#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +0000137#ifdef HAVE_SYS_MMAN_H
138static int memory = 0;
139#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +0000140static int testIO = 0;
Daniel Veillardbe803962000-06-28 23:40:59 +0000141static char *encoding = NULL;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000142#ifdef LIBXML_XINCLUDE_ENABLED
143static int xinclude = 0;
144#endif
Daniel Veillard48da9102001-08-07 01:10:10 +0000145static int dtdattrs = 0;
Daniel Veillard10ea86c2001-06-20 13:55:33 +0000146static int loaddtd = 0;
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000147static int progresult = 0;
Daniel Veillard48b2f892001-02-25 16:11:03 +0000148static int timing = 0;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000149static int generate = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000150static int dropdtd = 0;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000151#ifdef LIBXML_CATALOG_ENABLED
152static int catalogs = 0;
153static int nocatalogs = 0;
154#endif
Daniel Veillard81273902003-09-30 00:43:48 +0000155#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000156static int stream = 0;
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000157static int walker = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000158#endif /* LIBXML_READER_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000159static int chkregister = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000160#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +0000161static int sax1 = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000162#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000163static int options = 0;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000164
Daniel Veillard01db67c2001-12-18 07:09:59 +0000165/*
166 * Internal timing routines to remove the necessity to have unix-specific
167 * function calls
168 */
169
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000170#ifndef HAVE_GETTIMEOFDAY
171#ifdef HAVE_SYS_TIMEB_H
172#ifdef HAVE_SYS_TIME_H
173#ifdef HAVE_FTIME
174
Daniel Veillard01c13b52002-12-10 15:19:08 +0000175static int
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000176my_gettimeofday(struct timeval *tvp, void *tzp)
177{
178 struct timeb timebuffer;
179
180 ftime(&timebuffer);
181 if (tvp) {
182 tvp->tv_sec = timebuffer.time;
183 tvp->tv_usec = timebuffer.millitm * 1000L;
184 }
185 return (0);
186}
187#define HAVE_GETTIMEOFDAY 1
188#define gettimeofday my_gettimeofday
189
190#endif /* HAVE_FTIME */
191#endif /* HAVE_SYS_TIME_H */
192#endif /* HAVE_SYS_TIMEB_H */
193#endif /* !HAVE_GETTIMEOFDAY */
194
Daniel Veillard01db67c2001-12-18 07:09:59 +0000195#if defined(HAVE_GETTIMEOFDAY)
196static struct timeval begin, end;
197
198/*
199 * startTimer: call where you want to start timing
200 */
201static void
202startTimer(void)
203{
204 gettimeofday(&begin, NULL);
205}
206
207/*
208 * endTimer: call where you want to stop timing and to print out a
209 * message about the timing performed; format is a printf
210 * type argument
211 */
212static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000213endTimer(const char *fmt, ...)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000214{
215 long msec;
216 va_list ap;
217
218 gettimeofday(&end, NULL);
219 msec = end.tv_sec - begin.tv_sec;
220 msec *= 1000;
221 msec += (end.tv_usec - begin.tv_usec) / 1000;
222
223#ifndef HAVE_STDARG_H
224#error "endTimer required stdarg functions"
225#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000226 va_start(ap, fmt);
227 vfprintf(stderr, fmt, ap);
Daniel Veillard01db67c2001-12-18 07:09:59 +0000228 va_end(ap);
229
230 fprintf(stderr, " took %ld ms\n", msec);
231}
232#elif defined(HAVE_TIME_H)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000233/*
234 * No gettimeofday function, so we have to make do with calling clock.
235 * This is obviously less accurate, but there's little we can do about
236 * that.
237 */
Daniel Veillard90bc3712002-03-07 15:12:58 +0000238#ifndef CLOCKS_PER_SEC
239#define CLOCKS_PER_SEC 100
240#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +0000241
242static clock_t begin, end;
243static void
244startTimer(void)
245{
246 begin = clock();
247}
248static void
249endTimer(const char *fmt, ...)
250{
251 long msec;
252 va_list ap;
253
254 end = clock();
255 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
256
257#ifndef HAVE_STDARG_H
258#error "endTimer required stdarg functions"
259#endif
260 va_start(ap, fmt);
261 vfprintf(stderr, fmt, ap);
262 va_end(ap);
263 fprintf(stderr, " took %ld ms\n", msec);
264}
265#else
266
267/*
268 * We don't have a gettimeofday or time.h, so we just don't do timing
269 */
270static void
271startTimer(void)
272{
273 /*
274 * Do nothing
275 */
276}
277static void
278endTimer(char *format, ...)
279{
280 /*
281 * We cannot do anything because we don't have a timing function
282 */
283#ifdef HAVE_STDARG_H
284 va_start(ap, format);
285 vfprintf(stderr, format, ap);
286 va_end(ap);
287 fprintf(stderr, " was not timed\n", msec);
288#else
289 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
290 * this ?!
291 */
292#endif
293}
294#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000295/************************************************************************
296 * *
297 * HTML ouput *
298 * *
299 ************************************************************************/
300char buffer[50000];
301
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000302static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000303xmlHTMLEncodeSend(void) {
304 char *result;
305
306 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
307 if (result) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000308 xmlGenericError(xmlGenericErrorContext, "%s", result);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000309 xmlFree(result);
310 }
311 buffer[0] = 0;
312}
313
314/**
315 * xmlHTMLPrintFileInfo:
316 * @input: an xmlParserInputPtr input
317 *
318 * Displays the associated file and line informations for the current input
319 */
320
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000321static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000322xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000323 int len;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000324 xmlGenericError(xmlGenericErrorContext, "<p>");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000325
326 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000327 if (input != NULL) {
328 if (input->filename) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000329 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000330 input->line);
331 } else {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000332 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000333 }
334 }
335 xmlHTMLEncodeSend();
336}
337
338/**
339 * xmlHTMLPrintFileContext:
340 * @input: an xmlParserInputPtr input
341 *
342 * Displays current context within the input content for error tracking
343 */
344
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000345static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000346xmlHTMLPrintFileContext(xmlParserInputPtr input) {
347 const xmlChar *cur, *base;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000348 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000349 int n;
350
351 if (input == NULL) return;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000352 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000353 cur = input->cur;
354 base = input->base;
355 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
356 cur--;
357 }
358 n = 0;
359 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
360 cur--;
361 if ((*cur == '\n') || (*cur == '\r')) cur++;
362 base = cur;
363 n = 0;
364 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000365 len = strlen(buffer);
366 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
367 (unsigned char) *cur++);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000368 n++;
369 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000370 len = strlen(buffer);
371 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000372 cur = input->cur;
373 while ((*cur == '\n') || (*cur == '\r'))
374 cur--;
375 n = 0;
376 while ((cur != base) && (n++ < 80)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000377 len = strlen(buffer);
378 snprintf(&buffer[len], sizeof(buffer) - len, " ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000379 base++;
380 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000381 len = strlen(buffer);
382 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000383 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000384 xmlGenericError(xmlGenericErrorContext, "</pre>");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000385}
386
387/**
388 * xmlHTMLError:
389 * @ctx: an XML parser context
390 * @msg: the message to display/transmit
391 * @...: extra parameters for the message display
392 *
393 * Display and format an error messages, gives file, line, position and
394 * extra parameters.
395 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000396static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000397xmlHTMLError(void *ctx, const char *msg, ...)
398{
399 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
400 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000401 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000402 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000403
404 buffer[0] = 0;
405 input = ctxt->input;
406 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000407 input = ctxt->inputTab[ctxt->inputNr - 2];
408 }
409
410 xmlHTMLPrintFileInfo(input);
411
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000412 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000413 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000414 len = strlen(buffer);
415 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000416 va_end(args);
417 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000418 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000419
420 xmlHTMLPrintFileContext(input);
421 xmlHTMLEncodeSend();
422}
423
424/**
425 * xmlHTMLWarning:
426 * @ctx: an XML parser context
427 * @msg: the message to display/transmit
428 * @...: extra parameters for the message display
429 *
430 * Display and format a warning messages, gives file, line, position and
431 * extra parameters.
432 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000433static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000434xmlHTMLWarning(void *ctx, const char *msg, ...)
435{
436 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
437 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000438 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000439 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000440
441 buffer[0] = 0;
442 input = ctxt->input;
443 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000444 input = ctxt->inputTab[ctxt->inputNr - 2];
445 }
446
447
448 xmlHTMLPrintFileInfo(input);
449
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000450 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000451 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000452 len = strlen(buffer);
453 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000454 va_end(args);
455 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000456 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000457
458 xmlHTMLPrintFileContext(input);
459 xmlHTMLEncodeSend();
460}
461
462/**
463 * xmlHTMLValidityError:
464 * @ctx: an XML parser context
465 * @msg: the message to display/transmit
466 * @...: extra parameters for the message display
467 *
468 * Display and format an validity error messages, gives file,
469 * line, position and extra parameters.
470 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000471static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000472xmlHTMLValidityError(void *ctx, const char *msg, ...)
473{
474 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
475 xmlParserInputPtr input;
476 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000477 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000478
479 buffer[0] = 0;
480 input = ctxt->input;
481 if ((input->filename == NULL) && (ctxt->inputNr > 1))
482 input = ctxt->inputTab[ctxt->inputNr - 2];
483
484 xmlHTMLPrintFileInfo(input);
485
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000486 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000487 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000488 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000489 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000490 va_end(args);
491 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000492 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000493
494 xmlHTMLPrintFileContext(input);
495 xmlHTMLEncodeSend();
496}
497
498/**
499 * xmlHTMLValidityWarning:
500 * @ctx: an XML parser context
501 * @msg: the message to display/transmit
502 * @...: extra parameters for the message display
503 *
504 * Display and format a validity warning messages, gives file, line,
505 * position and extra parameters.
506 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000507static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000508xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
509{
510 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
511 xmlParserInputPtr input;
512 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000513 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000514
515 buffer[0] = 0;
516 input = ctxt->input;
517 if ((input->filename == NULL) && (ctxt->inputNr > 1))
518 input = ctxt->inputTab[ctxt->inputNr - 2];
519
520 xmlHTMLPrintFileInfo(input);
521
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000522 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000523 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000524 len = strlen(buffer);
525 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000526 va_end(args);
527 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000528 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000529
530 xmlHTMLPrintFileContext(input);
531 xmlHTMLEncodeSend();
532}
533
534/************************************************************************
535 * *
536 * Shell Interface *
537 * *
538 ************************************************************************/
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000539#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000540/**
541 * xmlShellReadline:
542 * @prompt: the prompt value
543 *
544 * Read a string
545 *
546 * Returns a pointer to it or NULL on EOF the caller is expected to
547 * free the returned string.
548 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000549static char *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000550xmlShellReadline(char *prompt) {
551#ifdef HAVE_LIBREADLINE
552 char *line_read;
553
554 /* Get a line from the user. */
555 line_read = readline (prompt);
556
557 /* If the line has any text in it, save it on the history. */
558 if (line_read && *line_read)
559 add_history (line_read);
560
561 return (line_read);
562#else
563 char line_read[501];
Daniel Veillard29e43992001-12-13 22:21:58 +0000564 char *ret;
565 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000566
567 if (prompt != NULL)
568 fprintf(stdout, "%s", prompt);
569 if (!fgets(line_read, 500, stdin))
570 return(NULL);
571 line_read[500] = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000572 len = strlen(line_read);
573 ret = (char *) malloc(len + 1);
574 if (ret != NULL) {
575 memcpy (ret, line_read, len + 1);
576 }
577 return(ret);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000578#endif
579}
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000580#endif /* LIBXML_DEBUG_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000581
582/************************************************************************
583 * *
Daniel Veillard5e873c42000-04-12 13:27:38 +0000584 * I/O Interfaces *
585 * *
586 ************************************************************************/
587
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000588static int myRead(FILE *f, char * buf, int len) {
589 return(fread(buf, 1, len, f));
Daniel Veillard5e873c42000-04-12 13:27:38 +0000590}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000591static void myClose(FILE *f) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000592 if (f != stdin) {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000593 fclose(f);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000594 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000595}
596
Daniel Veillard81273902003-09-30 00:43:48 +0000597#ifdef LIBXML_READER_ENABLED
Daniel Veillard5e873c42000-04-12 13:27:38 +0000598/************************************************************************
599 * *
Daniel Veillard7704fb12003-01-03 16:19:51 +0000600 * Stream Test processing *
601 * *
602 ************************************************************************/
Daniel Veillard7704fb12003-01-03 16:19:51 +0000603static void processNode(xmlTextReaderPtr reader) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000604 const xmlChar *name, *value;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000605
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000606 name = xmlTextReaderConstName(reader);
Daniel Veillarde59494f2003-01-04 16:35:29 +0000607 if (name == NULL)
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000608 name = BAD_CAST "--";
Daniel Veillard99737f52003-03-22 14:55:50 +0000609
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000610 value = xmlTextReaderConstValue(reader);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000611
Daniel Veillard99737f52003-03-22 14:55:50 +0000612 printf("%d %d %s %d %d",
Daniel Veillarde59494f2003-01-04 16:35:29 +0000613 xmlTextReaderDepth(reader),
614 xmlTextReaderNodeType(reader),
615 name,
Daniel Veillard99737f52003-03-22 14:55:50 +0000616 xmlTextReaderIsEmptyElement(reader),
617 xmlTextReaderHasValue(reader));
Daniel Veillarde59494f2003-01-04 16:35:29 +0000618 if (value == NULL)
619 printf("\n");
620 else {
621 printf(" %s\n", value);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000622 }
623}
624
625static void streamFile(char *filename) {
626 xmlTextReaderPtr reader;
627 int ret;
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000628#ifdef HAVE_SYS_MMAN_H
629 int fd = -1;
630 struct stat info;
631 const char *base = NULL;
632 xmlParserInputBufferPtr input = NULL;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000633
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000634 if (memory) {
635 if (stat(filename, &info) < 0)
636 return;
637 if ((fd = open(filename, O_RDONLY)) < 0)
638 return;
639 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
640 if (base == (void *) MAP_FAILED)
641 return;
642
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000643 reader = xmlReaderForMemory(base, info.st_size, filename,
644 NULL, options);
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000645 } else
646#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000647 reader = xmlReaderForFile(filename, NULL, options);
648
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000649
Daniel Veillard7704fb12003-01-03 16:19:51 +0000650 if (reader != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +0000651#ifdef LIBXML_VALID_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000652 if (valid)
653 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +0000654 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000655#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce192eb2003-04-16 15:58:05 +0000656 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000657#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +0000658 if (relaxng != NULL) {
Daniel Veillard81514ba2003-09-16 23:17:26 +0000659 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000660 startTimer();
661 }
662 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
663 if (ret < 0) {
664 xmlGenericError(xmlGenericErrorContext,
665 "Relax-NG schema %s failed to compile\n", relaxng);
Daniel Veillard05d987b2003-10-08 11:54:57 +0000666 progresult = 5;
Daniel Veillardce192eb2003-04-16 15:58:05 +0000667 relaxng = NULL;
668 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000669 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000670 endTimer("Compiling the schemas");
671 }
672 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000673#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000674
675 /*
676 * Process all nodes in sequence
677 */
Daniel Veillard81514ba2003-09-16 23:17:26 +0000678 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000679 startTimer();
680 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000681 ret = xmlTextReaderRead(reader);
682 while (ret == 1) {
683 if (debug)
684 processNode(reader);
685 ret = xmlTextReaderRead(reader);
686 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000687 if ((timing) && (!repeat)) {
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000688#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +0000689 if ((valid) || (relaxng != NULL))
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000690#else
Daniel Veillard4432df22003-09-28 18:58:27 +0000691#ifdef LIBXML_VALID_ENABLED
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000692 if (valid)
Daniel Veillardce192eb2003-04-16 15:58:05 +0000693 endTimer("Parsing and validating");
694 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000695#endif /* LIBXML_VALID_ENABLED */
696#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +0000697 endTimer("Parsing");
698 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000699
Daniel Veillard4432df22003-09-28 18:58:27 +0000700#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf6bad792003-04-11 19:38:54 +0000701 if (valid) {
702 if (xmlTextReaderIsValid(reader) != 1) {
703 xmlGenericError(xmlGenericErrorContext,
704 "Document %s does not validate\n", filename);
705 progresult = 3;
706 }
707 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000708#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000709#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +0000710 if (relaxng != NULL) {
711 if (xmlTextReaderIsValid(reader) != 1) {
712 printf("%s fails to validate\n", filename);
713 progresult = 3;
714 } else {
715 printf("%s validates\n", filename);
716 }
717 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000718#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000719 /*
720 * Done, cleanup and status
721 */
722 xmlFreeTextReader(reader);
723 if (ret != 0) {
724 printf("%s : failed to parse\n", filename);
Daniel Veillardf6bad792003-04-11 19:38:54 +0000725 progresult = 1;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000726 }
727 } else {
728 fprintf(stderr, "Unable to open %s\n", filename);
Daniel Veillardf6bad792003-04-11 19:38:54 +0000729 progresult = 1;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000730 }
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000731#ifdef HAVE_SYS_MMAN_H
732 if (memory) {
733 xmlFreeParserInputBuffer(input);
734 munmap((char *) base, info.st_size);
735 close(fd);
736 }
737#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000738}
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000739
740static void walkDoc(xmlDocPtr doc) {
741 xmlTextReaderPtr reader;
742 int ret;
743
744 reader = xmlReaderWalker(doc);
745 if (reader != NULL) {
746 if ((timing) && (!repeat)) {
747 startTimer();
748 }
749 ret = xmlTextReaderRead(reader);
750 while (ret == 1) {
751 if (debug)
752 processNode(reader);
753 ret = xmlTextReaderRead(reader);
754 }
755 if ((timing) && (!repeat)) {
756 endTimer("walking through the doc");
757 }
758 xmlFreeTextReader(reader);
759 if (ret != 0) {
760 printf("failed to walk through the doc\n");
761 progresult = 1;
762 }
763 } else {
764 fprintf(stderr, "Failed to crate a reader from the document\n");
765 progresult = 1;
766 }
767}
Daniel Veillard81273902003-09-30 00:43:48 +0000768#endif /* LIBXML_READER_ENABLED */
Daniel Veillard7704fb12003-01-03 16:19:51 +0000769
770/************************************************************************
771 * *
772 * Tree Test processing *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000773 * *
774 ************************************************************************/
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000775static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
Daniel Veillard652327a2003-09-29 18:02:38 +0000776 xmlDocPtr doc = NULL;
777#ifdef LIBXML_TREE_ENABLED
778 xmlDocPtr tmp;
779#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000780
Daniel Veillard48b2f892001-02-25 16:11:03 +0000781 if ((timing) && (!repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +0000782 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000783
784
Daniel Veillard652327a2003-09-29 18:02:38 +0000785#ifdef LIBXML_TREE_ENABLED
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000786 if (filename == NULL) {
787 if (generate) {
788 xmlNodePtr n;
789
790 doc = xmlNewDoc(BAD_CAST "1.0");
791 n = xmlNewNode(NULL, BAD_CAST "info");
792 xmlNodeSetContent(n, BAD_CAST "abc");
793 xmlDocSetRootElement(doc, n);
794 }
795 }
Daniel Veillard652327a2003-09-29 18:02:38 +0000796#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000797#ifdef LIBXML_HTML_ENABLED
Daniel Veillard73b013f2003-09-30 12:36:01 +0000798#ifdef LIBXML_PUSH_ENABLED
William M. Brack78637da2003-07-31 14:47:38 +0000799 else if ((html) && (push)) {
800 FILE *f;
801
802 f = fopen(filename, "r");
803 if (f != NULL) {
804 int res, size = 3;
805 char chars[4096];
806 htmlParserCtxtPtr ctxt;
807
808 /* if (repeat) */
809 size = 4096;
810 res = fread(chars, 1, 4, f);
811 if (res > 0) {
812 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
William M. Brack1d75c8a2003-10-27 13:48:16 +0000813 chars, res, filename, XML_CHAR_ENCODING_NONE);
William M. Brack78637da2003-07-31 14:47:38 +0000814 while ((res = fread(chars, 1, size, f)) > 0) {
815 htmlParseChunk(ctxt, chars, res, 0);
816 }
817 htmlParseChunk(ctxt, chars, 0, 1);
818 doc = ctxt->myDoc;
819 htmlFreeParserCtxt(ctxt);
820 }
821 fclose(f);
822 }
823 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000824#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000825 else if (html) {
Daniel Veillard9475a352003-09-26 12:47:50 +0000826 doc = htmlReadFile(filename, NULL, options);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000827 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000828#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000829 else {
Daniel Veillard73b013f2003-09-30 12:36:01 +0000830#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000831 /*
832 * build an XML tree from a string;
833 */
834 if (push) {
835 FILE *f;
836
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000837 /* '-' Usually means stdin -<sven@zen.org> */
838 if ((filename[0] == '-') && (filename[1] == 0)) {
839 f = stdin;
840 } else {
841 f = fopen(filename, "r");
842 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000843 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +0000844 int ret;
Daniel Veillarda880b122003-04-21 21:36:41 +0000845 int res, size = 1024;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000846 char chars[1024];
847 xmlParserCtxtPtr ctxt;
848
Daniel Veillarda880b122003-04-21 21:36:41 +0000849 /* if (repeat) size = 1024; */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000850 res = fread(chars, 1, 4, f);
851 if (res > 0) {
852 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
853 chars, res, filename);
854 while ((res = fread(chars, 1, size, f)) > 0) {
855 xmlParseChunk(ctxt, chars, res, 0);
856 }
857 xmlParseChunk(ctxt, chars, 0, 1);
858 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +0000859 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000860 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +0000861 if (!ret) {
862 xmlFreeDoc(doc);
863 doc = NULL;
864 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000865 }
866 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000867 } else
868#endif /* LIBXML_PUSH_ENABLED */
869 if (testIO) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000870 if ((filename[0] == '-') && (filename[1] == 0)) {
Daniel Veillard60942de2003-09-25 21:05:58 +0000871 doc = xmlReadFd(0, NULL, NULL, options);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000872 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000873 FILE *f;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000874
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000875 f = fopen(filename, "r");
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000876 if (f != NULL) {
877 if (rectxt == NULL)
878 doc = xmlReadIO((xmlInputReadCallback) myRead,
879 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +0000880 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000881 else
882 doc = xmlCtxtReadIO(rectxt,
883 (xmlInputReadCallback) myRead,
884 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +0000885 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000886 } else
Daniel Veillard5e873c42000-04-12 13:27:38 +0000887 doc = NULL;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000888 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000889 } else if (htmlout) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000890 xmlParserCtxtPtr ctxt;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000891
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000892 if (rectxt == NULL)
893 ctxt = xmlNewParserCtxt();
894 else
895 ctxt = rectxt;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000896 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000897 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000898 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000899 ctxt->sax->error = xmlHTMLError;
900 ctxt->sax->warning = xmlHTMLWarning;
901 ctxt->vctxt.error = xmlHTMLValidityError;
902 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000903
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000904 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000905
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000906 if (rectxt == NULL)
907 xmlFreeParserCtxt(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000908 }
Daniel Veillard46e370e2000-07-21 20:32:03 +0000909#ifdef HAVE_SYS_MMAN_H
910 } else if (memory) {
911 int fd;
912 struct stat info;
913 const char *base;
914 if (stat(filename, &info) < 0)
915 return;
916 if ((fd = open(filename, O_RDONLY)) < 0)
917 return;
918 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +0000919 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +0000920 return;
921
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000922 if (rectxt == NULL)
Daniel Veillard60942de2003-09-25 21:05:58 +0000923 doc = xmlReadMemory((char *) base, info.st_size,
924 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000925 else
Daniel Veillard60942de2003-09-25 21:05:58 +0000926 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
927 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000928
Daniel Veillard46e370e2000-07-21 20:32:03 +0000929 munmap((char *) base, info.st_size);
930#endif
Daniel Veillard4432df22003-09-28 18:58:27 +0000931#ifdef LIBXML_VALID_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +0000932 } else if (valid) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000933 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000934
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000935 if (rectxt == NULL)
936 ctxt = xmlNewParserCtxt();
937 else
938 ctxt = rectxt;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000939 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000940 doc = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000941 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000942 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
943
944 if (ctxt->valid == 0)
945 progresult = 4;
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000946 if (rectxt == NULL)
947 xmlFreeParserCtxt(ctxt);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000948 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000949#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +0000950 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000951 if (rectxt != NULL)
952 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
953 else
954 doc = xmlReadFile(filename, NULL, options);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000955 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000956 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000957
Daniel Veillard88a172f2000-08-04 18:23:10 +0000958 /*
959 * If we don't have a document we might as well give up. Do we
960 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000961 if (doc == NULL) {
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000962 progresult = 1;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000963 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000964 }
965
Daniel Veillard48b2f892001-02-25 16:11:03 +0000966 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000967 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000968 }
969
Daniel Veillard29e43992001-12-13 22:21:58 +0000970 /*
971 * Remove DOCTYPE nodes
972 */
973 if (dropdtd) {
974 xmlDtdPtr dtd;
975
976 dtd = xmlGetIntSubset(doc);
977 if (dtd != NULL) {
978 xmlUnlinkNode((xmlNodePtr)dtd);
979 xmlFreeDtd(dtd);
980 }
981 }
982
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000983#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +0000984 if (xinclude) {
985 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000986 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000987 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000988 xmlXIncludeProcess(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +0000989 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000990 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000991 }
992 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000993#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +0000994
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000995#ifdef LIBXML_DEBUG_ENABLED
996 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000997 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000998 */
999 if (shell)
1000 xmlShell(doc, filename, xmlShellReadline, stdout);
1001#endif
1002
Daniel Veillard652327a2003-09-29 18:02:38 +00001003#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001004 /*
1005 * test intermediate copy if needed.
1006 */
1007 if (copy) {
1008 tmp = doc;
1009 doc = xmlCopyDoc(doc, 1);
1010 xmlFreeDoc(tmp);
1011 }
Daniel Veillard652327a2003-09-29 18:02:38 +00001012#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001013
Daniel Veillard4432df22003-09-28 18:58:27 +00001014#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001015 if ((insert) && (!html)) {
1016 const xmlChar* list[256];
1017 int nb, i;
1018 xmlNodePtr node;
1019
1020 if (doc->children != NULL) {
1021 node = doc->children;
1022 while ((node != NULL) && (node->last == NULL)) node = node->next;
1023 if (node != NULL) {
1024 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
1025 if (nb < 0) {
1026 printf("could not get valid list of elements\n");
1027 } else if (nb == 0) {
MDT 2001 John Fleckbbb9e432001-09-24 03:08:43 +00001028 printf("No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001029 } else {
MDT 2001 John Fleckbbb9e432001-09-24 03:08:43 +00001030 printf("%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001031 nb);
1032 for (i = 0;i < nb;i++) {
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001033 printf("%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001034 }
1035 }
1036 }
1037 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001038 }else
1039#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001040#ifdef LIBXML_READER_ENABLED
1041 if (walker) {
1042 walkDoc(doc);
1043 }
1044#endif /* LIBXML_READER_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001045#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001046 if (noout == 0) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001047 /*
1048 * print it.
1049 */
1050#ifdef LIBXML_DEBUG_ENABLED
1051 if (!debug) {
1052#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00001053 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001054 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001055 }
Daniel Veillard42fd4122003-11-04 08:47:48 +00001056#ifdef LIBXML_VALID_ENABLED
1057 if ((html) && (!xmlout)) {
1058 if (compress) {
1059 htmlSaveFile(output ? output : "-", doc);
1060 }
1061 else if (encoding != NULL) {
1062 if ( format ) {
1063 htmlSaveFileFormat(output ? output : "-", doc, encoding, 1);
1064 }
1065 else {
1066 htmlSaveFileFormat(output ? output : "-", doc, encoding, 0);
1067 }
1068 }
1069 else if (format) {
1070 htmlSaveFileFormat(output ? output : "-", doc, NULL, 1);
1071 }
1072 else {
1073 FILE *out;
1074 if (output == NULL)
1075 out = stdout;
1076 else {
1077 out = fopen(output,"wb");
1078 }
1079 if (out != NULL) {
1080 if (htmlDocDump(out, doc) < 0)
1081 progresult = 6;
1082
1083 if (output != NULL)
1084 fclose(out);
1085 } else {
1086 fprintf(stderr, "failed to open %s\n", output);
1087 progresult = 6;
1088 }
1089 }
1090 if ((timing) && (!repeat)) {
1091 endTimer("Saving");
1092 }
1093 } else
1094#endif
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001095#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001096 if (memory) {
1097 xmlChar *result;
1098 int len;
1099
1100 if (encoding != NULL) {
Daniel Veillardd536f702001-11-08 17:32:47 +00001101 if ( format ) {
1102 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
1103 } else {
1104 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
1105 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001106 } else {
Daniel Veillard90493a92001-08-14 14:12:47 +00001107 if (format)
1108 xmlDocDumpFormatMemory(doc, &result, &len, 1);
1109 else
1110 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001111 }
1112 if (result == NULL) {
1113 fprintf(stderr, "Failed to save\n");
1114 } else {
1115 write(1, result, len);
1116 xmlFree(result);
1117 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001118 } else
1119#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001120 if (compress) {
1121 xmlSaveFile(output ? output : "-", doc);
1122 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001123 else if (encoding != NULL) {
1124 if ( format ) {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001125 xmlSaveFormatFileEnc(output ? output : "-", doc, encoding, 1);
1126 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001127 else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001128 xmlSaveFileEnc(output ? output : "-", doc, encoding);
Daniel Veillardd536f702001-11-08 17:32:47 +00001129 }
1130 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001131 else if (format) {
1132 xmlSaveFormatFile(output ? output : "-", doc, 1);
1133 }
1134 else {
1135 FILE *out;
1136 if (output == NULL)
1137 out = stdout;
1138 else {
1139 out = fopen(output,"wb");
1140 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001141 if (out != NULL) {
Daniel Veillard828ce832003-10-08 19:19:10 +00001142 if (xmlDocDump(out, doc) < 0)
1143 progresult = 6;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001144
Daniel Veillard05d987b2003-10-08 11:54:57 +00001145 if (output != NULL)
1146 fclose(out);
1147 } else {
1148 fprintf(stderr, "failed to open %s\n", output);
1149 progresult = 6;
1150 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001151 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001152 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001153 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001154 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001155#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001156 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001157 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) {
1164 xmlDebugDumpDocument(out, doc);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001165
Daniel Veillard05d987b2003-10-08 11:54:57 +00001166 if (output != NULL)
1167 fclose(out);
1168 } else {
1169 fprintf(stderr, "failed to open %s\n", output);
1170 progresult = 6;
1171 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001172 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001173#endif
1174 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001175#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001176
Daniel Veillard4432df22003-09-28 18:58:27 +00001177#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001178 /*
1179 * A posteriori validation test
1180 */
Daniel Veillard66f68e72003-08-18 16:39:51 +00001181 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
Daniel Veillardcd429612000-10-11 15:57:05 +00001182 xmlDtdPtr dtd;
1183
Daniel Veillard48b2f892001-02-25 16:11:03 +00001184 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001185 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001186 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00001187 if (dtdvalid != NULL)
1188 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
1189 else
1190 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001191 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001192 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001193 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001194 if (dtd == NULL) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001195 if (dtdvalid != NULL)
1196 xmlGenericError(xmlGenericErrorContext,
1197 "Could not parse DTD %s\n", dtdvalid);
1198 else
1199 xmlGenericError(xmlGenericErrorContext,
1200 "Could not parse DTD %s\n", dtdvalidfpi);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001201 progresult = 2;
Daniel Veillardcd429612000-10-11 15:57:05 +00001202 } else {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001203 xmlValidCtxtPtr cvp;
1204
1205 if ((cvp = xmlNewValidCtxt()) == NULL) {
1206 xmlGenericError(xmlGenericErrorContext,
1207 "Couldn't allocate validation context\n");
1208 exit(-1);
1209 }
1210 cvp->userData = (void *) stderr;
1211 cvp->error = (xmlValidityErrorFunc) fprintf;
1212 cvp->warning = (xmlValidityWarningFunc) fprintf;
1213
Daniel Veillard48b2f892001-02-25 16:11:03 +00001214 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001215 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001216 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001217 if (!xmlValidateDtd(cvp, doc, dtd)) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001218 if (dtdvalid != NULL)
1219 xmlGenericError(xmlGenericErrorContext,
1220 "Document %s does not validate against %s\n",
1221 filename, dtdvalid);
1222 else
1223 xmlGenericError(xmlGenericErrorContext,
1224 "Document %s does not validate against %s\n",
1225 filename, dtdvalidfpi);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001226 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +00001227 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001228 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001229 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001230 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001231 xmlFreeValidCtxt(cvp);
Daniel Veillardcd429612000-10-11 15:57:05 +00001232 xmlFreeDtd(dtd);
1233 }
1234 } else if (postvalid) {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001235 xmlValidCtxtPtr cvp;
1236
1237 if ((cvp = xmlNewValidCtxt()) == NULL) {
1238 xmlGenericError(xmlGenericErrorContext,
1239 "Couldn't allocate validation context\n");
1240 exit(-1);
1241 }
1242
Daniel Veillard48b2f892001-02-25 16:11:03 +00001243 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001244 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001245 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001246 cvp->userData = (void *) stderr;
1247 cvp->error = (xmlValidityErrorFunc) fprintf;
1248 cvp->warning = (xmlValidityWarningFunc) fprintf;
1249 if (!xmlValidateDocument(cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001250 xmlGenericError(xmlGenericErrorContext,
1251 "Document %s does not validate\n", filename);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001252 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +00001253 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001254 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001255 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001256 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001257 xmlFreeValidCtxt(cvp);
Daniel Veillard4432df22003-09-28 18:58:27 +00001258 }
1259#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00001260#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001261 if (relaxngschemas != NULL) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001262 xmlRelaxNGValidCtxtPtr ctxt;
1263 int ret;
1264
Daniel Veillard42f12e92003-03-07 18:32:59 +00001265 if ((timing) && (!repeat)) {
1266 startTimer();
1267 }
1268
Daniel Veillard71531f32003-02-05 13:19:53 +00001269 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
1270 xmlRelaxNGSetValidErrors(ctxt,
1271 (xmlRelaxNGValidityErrorFunc) fprintf,
1272 (xmlRelaxNGValidityWarningFunc) fprintf,
1273 stderr);
1274 ret = xmlRelaxNGValidateDoc(ctxt, doc);
1275 if (ret == 0) {
1276 printf("%s validates\n", filename);
1277 } else if (ret > 0) {
1278 printf("%s fails to validate\n", filename);
1279 } else {
1280 printf("%s validation generated an internal error\n",
1281 filename);
1282 }
1283 xmlRelaxNGFreeValidCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001284 if ((timing) && (!repeat)) {
1285 endTimer("Validating");
1286 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001287 } else if (wxschemas != NULL) {
1288 xmlSchemaValidCtxtPtr ctxt;
1289 int ret;
1290
1291 if ((timing) && (!repeat)) {
1292 startTimer();
1293 }
1294
1295 ctxt = xmlSchemaNewValidCtxt(wxschemas);
1296 xmlSchemaSetValidErrors(ctxt,
1297 (xmlSchemaValidityErrorFunc) fprintf,
1298 (xmlSchemaValidityWarningFunc) fprintf,
1299 stderr);
1300 ret = xmlSchemaValidateDoc(ctxt, doc);
1301 if (ret == 0) {
1302 printf("%s validates\n", filename);
1303 } else if (ret > 0) {
1304 printf("%s fails to validate\n", filename);
1305 } else {
1306 printf("%s validation generated an internal error\n",
1307 filename);
1308 }
1309 xmlSchemaFreeValidCtxt(ctxt);
1310 if ((timing) && (!repeat)) {
1311 endTimer("Validating");
1312 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001313 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001314#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001315
1316#ifdef LIBXML_DEBUG_ENABLED
1317 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001318 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001319#endif
1320
1321 /*
1322 * free it.
1323 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00001324 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001325 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001326 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001327 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001328 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001329 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001330 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001331}
1332
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001333/************************************************************************
1334 * *
1335 * Usage and Main *
1336 * *
1337 ************************************************************************/
1338
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001339static void showVersion(const char *name) {
1340 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
1341 fprintf(stderr, " compiled with: ");
Daniel Veillard4432df22003-09-28 18:58:27 +00001342#ifdef LIBXML_VALID_ENABLED
1343 fprintf(stderr, "DTDValid ");
1344#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001345#ifdef LIBXML_FTP_ENABLED
1346 fprintf(stderr, "FTP ");
1347#endif
1348#ifdef LIBXML_HTTP_ENABLED
1349 fprintf(stderr, "HTTP ");
1350#endif
1351#ifdef LIBXML_HTML_ENABLED
1352 fprintf(stderr, "HTML ");
1353#endif
1354#ifdef LIBXML_C14N_ENABLED
1355 fprintf(stderr, "C14N ");
1356#endif
1357#ifdef LIBXML_CATALOG_ENABLED
1358 fprintf(stderr, "Catalog ");
1359#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001360#ifdef LIBXML_XPATH_ENABLED
1361 fprintf(stderr, "XPath ");
1362#endif
1363#ifdef LIBXML_XPTR_ENABLED
1364 fprintf(stderr, "XPointer ");
1365#endif
1366#ifdef LIBXML_XINCLUDE_ENABLED
1367 fprintf(stderr, "XInclude ");
1368#endif
1369#ifdef LIBXML_ICONV_ENABLED
1370 fprintf(stderr, "Iconv ");
1371#endif
1372#ifdef DEBUG_MEMORY_LOCATION
1373 fprintf(stderr, "MemDebug ");
1374#endif
1375#ifdef LIBXML_UNICODE_ENABLED
1376 fprintf(stderr, "Unicode ");
1377#endif
1378#ifdef LIBXML_REGEXP_ENABLED
1379 fprintf(stderr, "Regexps ");
1380#endif
1381#ifdef LIBXML_AUTOMATA_ENABLED
1382 fprintf(stderr, "Automata ");
1383#endif
1384#ifdef LIBXML_SCHEMAS_ENABLED
1385 fprintf(stderr, "Schemas ");
1386#endif
1387 fprintf(stderr, "\n");
1388}
1389
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001390static void usage(const char *name) {
1391 printf("Usage : %s [options] XMLfiles ...\n", name);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001392#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001393 printf("\tParse the XML files and output the result of the parsing\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001394#else
1395 printf("\tParse the XML files\n");
1396#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001397 printf("\t--version : display the version of the XML library used\n");
1398#ifdef LIBXML_DEBUG_ENABLED
1399 printf("\t--debug : dump a debug tree of the in-memory document\n");
1400 printf("\t--shell : run a navigating shell\n");
1401 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00001402#else
Daniel Veillard81273902003-09-30 00:43:48 +00001403#ifdef LIBXML_READER_ENABLED
Daniel Veillard8326e732003-01-07 00:19:07 +00001404 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001405#endif /* LIBXML_READER_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001406#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001407#ifdef LIBXML_TREE_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001408 printf("\t--copy : used to test the internal copy implementation\n");
Daniel Veillard652327a2003-09-29 18:02:38 +00001409#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001410 printf("\t--recover : output what was parsable on broken XML documents\n");
1411 printf("\t--noent : substitute entity references by their value\n");
1412 printf("\t--noout : don't output the result tree\n");
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001413 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001414 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001415 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001416#ifdef LIBXML_VALID_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001417 printf("\t--valid : validate the document in addition to std well-formed check\n");
1418 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
1419 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
Daniel Veillard66f68e72003-08-18 16:39:51 +00001420 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001421#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001422 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001423 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001424 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
1425 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001426#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001427#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001428 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001429#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001430#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001431#ifdef LIBXML_HTML_ENABLED
1432 printf("\t--html : use the HTML parser\n");
Daniel Veillard42fd4122003-11-04 08:47:48 +00001433 printf("\t--xmlout : force to use the XML serializer when using --html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001434#endif
Daniel Veillard73b013f2003-09-30 12:36:01 +00001435#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001436 printf("\t--push : use the push mode of the parser\n");
Daniel Veillard73b013f2003-09-30 12:36:01 +00001437#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001438#ifdef HAVE_SYS_MMAN_H
1439 printf("\t--memory : parse from memory\n");
1440#endif
1441 printf("\t--nowarning : do not emit warnings from parser/validator\n");
1442 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001443 printf("\t--nocdata : replace cdata section with text nodes\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001444#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001445 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001446 printf("\t--encode encoding : output in the given encoding\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001447 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
1448#endif /* LIBXML_OUTPUT_ENABLED */
1449 printf("\t--nsclean : remove redundant namespace declarations\n");
1450 printf("\t--testIO : test user I/O support\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001451#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00001452 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
1453 printf("\t otherwise XML Catalogs starting from \n");
Daniel Veillard3be27512003-01-26 19:49:04 +00001454 printf("\t " XML_XML_DEFAULT_CATALOG " are activated by default\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001455 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001456#endif
1457 printf("\t--auto : generate a small doc on the fly\n");
1458#ifdef LIBXML_XINCLUDE_ENABLED
1459 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001460#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001461 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00001462 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard81273902003-09-30 00:43:48 +00001463#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001464 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001465 printf("\t--walker : create a reader and walk though the resulting doc\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001466#endif /* LIBXML_READER_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001467 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillardef4d3bc2003-02-07 12:38:22 +00001468#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard71531f32003-02-05 13:19:53 +00001469 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001470 printf("\t--schema schema : do validation against the WXS schema\n");
Daniel Veillard71531f32003-02-05 13:19:53 +00001471#endif
Daniel Veillarda42f25f2002-01-25 14:15:40 +00001472 printf("\nLibxml project home page: http://xmlsoft.org/\n");
1473 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001474}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001475
1476static void registerNode(xmlNodePtr node)
1477{
1478 node->_private = malloc(sizeof(long));
1479 *(long*)node->_private = (long) 0x81726354;
1480}
1481
1482static void deregisterNode(xmlNodePtr node)
1483{
1484 assert(node->_private != NULL);
1485 assert(*(long*)node->_private == (long) 0x81726354);
1486 free(node->_private);
1487}
1488
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001489int
1490main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001491 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001492 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00001493 int version = 0;
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001494 const char* indent;
1495
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001496 if (argc <= 1) {
1497 usage(argv[0]);
1498 return(1);
1499 }
Daniel Veillardbe803962000-06-28 23:40:59 +00001500 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001501 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001502 if (!strcmp(argv[i], "-"))
1503 break;
1504
1505 if (argv[i][0] != '-')
1506 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001507 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
1508 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00001509 else
1510#ifdef LIBXML_DEBUG_ENABLED
1511 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001512 (!strcmp(argv[i], "--shell"))) {
1513 shell++;
1514 noout = 1;
1515 } else
1516#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001517#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001518 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
1519 copy++;
Daniel Veillard652327a2003-09-29 18:02:38 +00001520 else
1521#endif /* LIBXML_TREE_ENABLED */
1522 if ((!strcmp(argv[i], "-recover")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001523 (!strcmp(argv[i], "--recover"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001524 recovery++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001525 options |= XML_PARSE_RECOVER;
1526 } else if ((!strcmp(argv[i], "-noent")) ||
1527 (!strcmp(argv[i], "--noent"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001528 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001529 options |= XML_PARSE_NOENT;
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001530 } else if ((!strcmp(argv[i], "-nsclean")) ||
1531 (!strcmp(argv[i], "--nsclean"))) {
1532 options |= XML_PARSE_NSCLEAN;
1533 } else if ((!strcmp(argv[i], "-nocdata")) ||
1534 (!strcmp(argv[i], "--nocdata"))) {
1535 options |= XML_PARSE_NOCDATA;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001536 } else if ((!strcmp(argv[i], "-nodict")) ||
1537 (!strcmp(argv[i], "--nodict"))) {
1538 options |= XML_PARSE_NODICT;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001539 } else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00001540 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001541 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00001542 version = 1;
1543 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001544 (!strcmp(argv[i], "--noout")))
1545 noout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001546#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001547 else if ((!strcmp(argv[i], "-o")) ||
1548 (!strcmp(argv[i], "-output")) ||
1549 (!strcmp(argv[i], "--output"))) {
1550 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00001551 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001552 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001553#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001554 else if ((!strcmp(argv[i], "-htmlout")) ||
1555 (!strcmp(argv[i], "--htmlout")))
1556 htmlout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001557 else if ((!strcmp(argv[i], "-nowrap")) ||
1558 (!strcmp(argv[i], "--nowrap")))
1559 nowrap++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001560#ifdef LIBXML_HTML_ENABLED
1561 else if ((!strcmp(argv[i], "-html")) ||
1562 (!strcmp(argv[i], "--html"))) {
1563 html++;
1564 }
Daniel Veillard42fd4122003-11-04 08:47:48 +00001565 else if ((!strcmp(argv[i], "-xmlout")) ||
1566 (!strcmp(argv[i], "--xmlout"))) {
1567 xmlout++;
1568 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001569#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001570 else if ((!strcmp(argv[i], "-loaddtd")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001571 (!strcmp(argv[i], "--loaddtd"))) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001572 loaddtd++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001573 options |= XML_PARSE_DTDLOAD;
1574 } else if ((!strcmp(argv[i], "-dtdattr")) ||
Daniel Veillard48da9102001-08-07 01:10:10 +00001575 (!strcmp(argv[i], "--dtdattr"))) {
1576 loaddtd++;
1577 dtdattrs++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001578 options |= XML_PARSE_DTDATTR;
Daniel Veillard4432df22003-09-28 18:58:27 +00001579 }
1580#ifdef LIBXML_VALID_ENABLED
1581 else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001582 (!strcmp(argv[i], "--valid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001583 valid++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001584 options |= XML_PARSE_DTDVALID;
1585 } else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001586 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001587 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001588 loaddtd++;
1589 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00001590 (!strcmp(argv[i], "--dtdvalid"))) {
1591 i++;
1592 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001593 loaddtd++;
Daniel Veillard66f68e72003-08-18 16:39:51 +00001594 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
1595 (!strcmp(argv[i], "--dtdvalidfpi"))) {
1596 i++;
1597 dtdvalidfpi = argv[i];
1598 loaddtd++;
Daniel Veillardcd429612000-10-11 15:57:05 +00001599 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001600#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard29e43992001-12-13 22:21:58 +00001601 else if ((!strcmp(argv[i], "-dropdtd")) ||
1602 (!strcmp(argv[i], "--dropdtd")))
1603 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001604 else if ((!strcmp(argv[i], "-insert")) ||
1605 (!strcmp(argv[i], "--insert")))
1606 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001607 else if ((!strcmp(argv[i], "-timing")) ||
1608 (!strcmp(argv[i], "--timing")))
1609 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001610 else if ((!strcmp(argv[i], "-auto")) ||
1611 (!strcmp(argv[i], "--auto")))
1612 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001613 else if ((!strcmp(argv[i], "-repeat")) ||
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001614 (!strcmp(argv[i], "--repeat"))) {
1615 if (repeat)
1616 repeat *= 10;
1617 else
1618 repeat = 100;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001619 }
1620#ifdef LIBXML_PUSH_ENABLED
1621 else if ((!strcmp(argv[i], "-push")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001622 (!strcmp(argv[i], "--push")))
1623 push++;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001624#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +00001625#ifdef HAVE_SYS_MMAN_H
1626 else if ((!strcmp(argv[i], "-memory")) ||
1627 (!strcmp(argv[i], "--memory")))
1628 memory++;
1629#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00001630 else if ((!strcmp(argv[i], "-testIO")) ||
1631 (!strcmp(argv[i], "--testIO")))
1632 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001633#ifdef LIBXML_XINCLUDE_ENABLED
1634 else if ((!strcmp(argv[i], "-xinclude")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001635 (!strcmp(argv[i], "--xinclude"))) {
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001636 xinclude++;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001637 options |= XML_PARSE_XINCLUDE;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001638 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001639#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001640#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001641#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001642 else if ((!strcmp(argv[i], "-compress")) ||
1643 (!strcmp(argv[i], "--compress"))) {
1644 compress++;
1645 xmlSetCompressMode(9);
1646 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001647#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001648#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001649 else if ((!strcmp(argv[i], "-nowarning")) ||
1650 (!strcmp(argv[i], "--nowarning"))) {
1651 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001652 xmlPedanticParserDefault(0);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001653 options |= XML_PARSE_NOWARNING;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001654 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001655 else if ((!strcmp(argv[i], "-pedantic")) ||
1656 (!strcmp(argv[i], "--pedantic"))) {
1657 xmlGetWarningsDefaultValue = 1;
1658 xmlPedanticParserDefault(1);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001659 options |= XML_PARSE_PEDANTIC;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001660 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001661#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001662 else if ((!strcmp(argv[i], "-debugent")) ||
1663 (!strcmp(argv[i], "--debugent"))) {
1664 debugent++;
1665 xmlParserDebugEntities = 1;
1666 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001667#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00001668#ifdef LIBXML_CATALOG_ENABLED
1669 else if ((!strcmp(argv[i], "-catalogs")) ||
1670 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001671 catalogs++;
1672 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
1673 (!strcmp(argv[i], "--nocatalogs"))) {
1674 nocatalogs++;
Daniel Veillard81418e32001-05-22 15:08:55 +00001675 }
1676#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00001677 else if ((!strcmp(argv[i], "-encode")) ||
1678 (!strcmp(argv[i], "--encode"))) {
1679 i++;
1680 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001681 /*
1682 * OK it's for testing purposes
1683 */
1684 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00001685 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001686 else if ((!strcmp(argv[i], "-noblanks")) ||
1687 (!strcmp(argv[i], "--noblanks"))) {
1688 noblanks++;
1689 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00001690 }
1691 else if ((!strcmp(argv[i], "-format")) ||
1692 (!strcmp(argv[i], "--format"))) {
1693 noblanks++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001694#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001695 format++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001696#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard90493a92001-08-14 14:12:47 +00001697 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001698 }
Daniel Veillard81273902003-09-30 00:43:48 +00001699#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001700 else if ((!strcmp(argv[i], "-stream")) ||
1701 (!strcmp(argv[i], "--stream"))) {
1702 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001703 }
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001704 else if ((!strcmp(argv[i], "-walker")) ||
1705 (!strcmp(argv[i], "--walker"))) {
1706 walker++;
1707 noout++;
1708 }
Daniel Veillard81273902003-09-30 00:43:48 +00001709#endif /* LIBXML_READER_ENABLED */
1710#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001711 else if ((!strcmp(argv[i], "-sax1")) ||
1712 (!strcmp(argv[i], "--sax1"))) {
1713 sax1++;
1714 }
Daniel Veillard81273902003-09-30 00:43:48 +00001715#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001716 else if ((!strcmp(argv[i], "-chkregister")) ||
1717 (!strcmp(argv[i], "--chkregister"))) {
1718 chkregister++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001719#ifdef LIBXML_SCHEMAS_ENABLED
1720 } else if ((!strcmp(argv[i], "-relaxng")) ||
1721 (!strcmp(argv[i], "--relaxng"))) {
1722 i++;
1723 relaxng = argv[i];
1724 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001725 options |= XML_PARSE_NOENT;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001726 } else if ((!strcmp(argv[i], "-schema")) ||
1727 (!strcmp(argv[i], "--schema"))) {
1728 i++;
1729 schema = argv[i];
1730 noent++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001731#endif
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001732 } else if ((!strcmp(argv[i], "-nonet")) ||
1733 (!strcmp(argv[i], "--nonet"))) {
Daniel Veillard61b93382003-11-03 14:28:31 +00001734 options |= XML_PARSE_NONET;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001735 } else {
1736 fprintf(stderr, "Unknown option %s\n", argv[i]);
1737 usage(argv[0]);
1738 return(1);
1739 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001740 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001741
1742#ifdef LIBXML_CATALOG_ENABLED
1743 if (nocatalogs == 0) {
1744 if (catalogs) {
1745 const char *catal;
1746
1747 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00001748 if (catal != NULL) {
1749 xmlLoadCatalogs(catal);
1750 } else {
1751 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
1752 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001753 }
1754 }
1755#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001756
Daniel Veillard81273902003-09-30 00:43:48 +00001757#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001758 if (sax1)
1759 xmlSAXDefaultVersion(1);
1760 else
1761 xmlSAXDefaultVersion(2);
Daniel Veillard81273902003-09-30 00:43:48 +00001762#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00001763
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001764 if (chkregister) {
1765 xmlRegisterNodeDefault(registerNode);
1766 xmlDeregisterNodeDefault(deregisterNode);
1767 }
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001768
1769 indent = getenv("XMLLINT_INDENT");
1770 if(indent != NULL) {
1771 xmlTreeIndentString = indent;
1772 }
1773
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001774
Daniel Veillardd9bad132001-07-23 19:39:43 +00001775 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00001776 if (loaddtd != 0)
1777 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
1778 if (dtdattrs)
1779 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001780 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillard4432df22003-09-28 18:58:27 +00001781#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001782 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard4432df22003-09-28 18:58:27 +00001783#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001784 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001785 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001786 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001787 xmlGenericError(xmlGenericErrorContext,
1788 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
1789 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001790 "<html><head><title>%s output</title></head>\n",
1791 argv[0]);
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001792 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001793 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
1794 argv[0]);
1795 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001796
1797#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard81273902003-09-30 00:43:48 +00001798 if ((relaxng != NULL)
1799#ifdef LIBXML_READER_ENABLED
1800 && (stream == 0)
1801#endif /* LIBXML_READER_ENABLED */
1802 ) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001803 xmlRelaxNGParserCtxtPtr ctxt;
1804
Daniel Veillardce192eb2003-04-16 15:58:05 +00001805 /* forces loading the DTDs */
1806 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001807 options |= XML_PARSE_DTDLOAD;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001808 if (timing) {
1809 startTimer();
1810 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001811 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
1812 xmlRelaxNGSetParserErrors(ctxt,
1813 (xmlRelaxNGValidityErrorFunc) fprintf,
1814 (xmlRelaxNGValidityWarningFunc) fprintf,
1815 stderr);
1816 relaxngschemas = xmlRelaxNGParse(ctxt);
Daniel Veillardce192eb2003-04-16 15:58:05 +00001817 if (relaxngschemas == NULL) {
1818 xmlGenericError(xmlGenericErrorContext,
1819 "Relax-NG schema %s failed to compile\n", relaxng);
Daniel Veillard05d987b2003-10-08 11:54:57 +00001820 progresult = 5;
Daniel Veillardce192eb2003-04-16 15:58:05 +00001821 relaxng = NULL;
1822 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001823 xmlRelaxNGFreeParserCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001824 if (timing) {
1825 endTimer("Compiling the schemas");
1826 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001827 } else if ((schema != NULL) && (stream == 0)) {
1828 xmlSchemaParserCtxtPtr ctxt;
1829
1830 if (timing) {
1831 startTimer();
1832 }
1833 ctxt = xmlSchemaNewParserCtxt(schema);
1834 xmlSchemaSetParserErrors(ctxt,
1835 (xmlSchemaValidityErrorFunc) fprintf,
1836 (xmlSchemaValidityWarningFunc) fprintf,
1837 stderr);
1838 wxschemas = xmlSchemaParse(ctxt);
1839 if (wxschemas == NULL) {
1840 xmlGenericError(xmlGenericErrorContext,
1841 "WXS schema %s failed to compile\n", schema);
Daniel Veillard05d987b2003-10-08 11:54:57 +00001842 progresult = 5;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001843 schema = NULL;
1844 }
1845 xmlSchemaFreeParserCtxt(ctxt);
1846 if (timing) {
1847 endTimer("Compiling the schemas");
1848 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001849 }
1850#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001851 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00001852 if ((!strcmp(argv[i], "-encode")) ||
1853 (!strcmp(argv[i], "--encode"))) {
1854 i++;
1855 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001856 } else if ((!strcmp(argv[i], "-o")) ||
1857 (!strcmp(argv[i], "-output")) ||
1858 (!strcmp(argv[i], "--output"))) {
1859 i++;
1860 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00001861 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001862#ifdef LIBXML_VALID_ENABLED
Daniel Veillardcd429612000-10-11 15:57:05 +00001863 if ((!strcmp(argv[i], "-dtdvalid")) ||
1864 (!strcmp(argv[i], "--dtdvalid"))) {
1865 i++;
1866 continue;
1867 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00001868 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
1869 (!strcmp(argv[i], "--dtdvalidfpi"))) {
1870 i++;
1871 continue;
1872 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001873#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00001874 if ((!strcmp(argv[i], "-relaxng")) ||
1875 (!strcmp(argv[i], "--relaxng"))) {
1876 i++;
1877 continue;
1878 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001879 if ((!strcmp(argv[i], "-schema")) ||
1880 (!strcmp(argv[i], "--schema"))) {
1881 i++;
1882 continue;
1883 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001884 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00001885 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001886 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001887 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001888 if (repeat) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001889 xmlParserCtxtPtr ctxt = NULL;
1890
1891 for (acount = 0;acount < repeat;acount++) {
Daniel Veillard81273902003-09-30 00:43:48 +00001892#ifdef LIBXML_READER_ENABLED
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001893 if (stream != 0) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001894 streamFile(argv[i]);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001895 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00001896#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001897 if (ctxt == NULL)
1898 ctxt = xmlNewParserCtxt();
1899 parseAndPrintFile(argv[i], ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00001900#ifdef LIBXML_READER_ENABLED
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001901 }
Daniel Veillard81273902003-09-30 00:43:48 +00001902#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001903 }
1904 if (ctxt != NULL)
1905 xmlFreeParserCtxt(ctxt);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001906 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00001907#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001908 if (stream != 0)
1909 streamFile(argv[i]);
1910 else
Daniel Veillard81273902003-09-30 00:43:48 +00001911#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001912 parseAndPrintFile(argv[i], NULL);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001913 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001914 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00001915 if ((timing) && (repeat)) {
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001916 endTimer("%d iterations", repeat);
Daniel Veillarda7866932001-12-04 13:14:44 +00001917 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001918 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001919 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001920 if (generate)
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001921 parseAndPrintFile(NULL, NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001922 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001923 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001924 }
Daniel Veillard845cce42002-01-09 11:51:37 +00001925 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001926 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001927 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001928#ifdef LIBXML_SCHEMAS_ENABLED
1929 if (relaxngschemas != NULL)
1930 xmlRelaxNGFree(relaxngschemas);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001931 if (wxschemas != NULL)
1932 xmlSchemaFree(wxschemas);
Daniel Veillard71531f32003-02-05 13:19:53 +00001933 xmlRelaxNGCleanupTypes();
1934#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001935 xmlCleanupParser();
1936 xmlMemoryDump();
1937
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001938 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001939}
Daniel Veillard88a172f2000-08-04 18:23:10 +00001940