blob: f8556d06618137f0e7ef6e198952953837c1d6e6 [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 Veillard4432df22003-09-28 18:58:27 +0000131#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000132static int htmlout = 0;
Daniel Veillard73b013f2003-09-30 12:36:01 +0000133#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000134static int push = 0;
Daniel Veillard73b013f2003-09-30 12:36:01 +0000135#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +0000136#ifdef HAVE_SYS_MMAN_H
137static int memory = 0;
138#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +0000139static int testIO = 0;
Daniel Veillardbe803962000-06-28 23:40:59 +0000140static char *encoding = NULL;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000141#ifdef LIBXML_XINCLUDE_ENABLED
142static int xinclude = 0;
143#endif
Daniel Veillard48da9102001-08-07 01:10:10 +0000144static int dtdattrs = 0;
Daniel Veillard10ea86c2001-06-20 13:55:33 +0000145static int loaddtd = 0;
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000146static int progresult = 0;
Daniel Veillard48b2f892001-02-25 16:11:03 +0000147static int timing = 0;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000148static int generate = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000149static int dropdtd = 0;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000150#ifdef LIBXML_CATALOG_ENABLED
151static int catalogs = 0;
152static int nocatalogs = 0;
153#endif
Daniel Veillard81273902003-09-30 00:43:48 +0000154#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000155static int stream = 0;
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000156static int walker = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000157#endif /* LIBXML_READER_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000158static int chkregister = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000159#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +0000160static int sax1 = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000161#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000162static int options = 0;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000163
Daniel Veillard01db67c2001-12-18 07:09:59 +0000164/*
165 * Internal timing routines to remove the necessity to have unix-specific
166 * function calls
167 */
168
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000169#ifndef HAVE_GETTIMEOFDAY
170#ifdef HAVE_SYS_TIMEB_H
171#ifdef HAVE_SYS_TIME_H
172#ifdef HAVE_FTIME
173
Daniel Veillard01c13b52002-12-10 15:19:08 +0000174static int
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000175my_gettimeofday(struct timeval *tvp, void *tzp)
176{
177 struct timeb timebuffer;
178
179 ftime(&timebuffer);
180 if (tvp) {
181 tvp->tv_sec = timebuffer.time;
182 tvp->tv_usec = timebuffer.millitm * 1000L;
183 }
184 return (0);
185}
186#define HAVE_GETTIMEOFDAY 1
187#define gettimeofday my_gettimeofday
188
189#endif /* HAVE_FTIME */
190#endif /* HAVE_SYS_TIME_H */
191#endif /* HAVE_SYS_TIMEB_H */
192#endif /* !HAVE_GETTIMEOFDAY */
193
Daniel Veillard01db67c2001-12-18 07:09:59 +0000194#if defined(HAVE_GETTIMEOFDAY)
195static struct timeval begin, end;
196
197/*
198 * startTimer: call where you want to start timing
199 */
200static void
201startTimer(void)
202{
203 gettimeofday(&begin, NULL);
204}
205
206/*
207 * endTimer: call where you want to stop timing and to print out a
208 * message about the timing performed; format is a printf
209 * type argument
210 */
211static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000212endTimer(const char *fmt, ...)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000213{
214 long msec;
215 va_list ap;
216
217 gettimeofday(&end, NULL);
218 msec = end.tv_sec - begin.tv_sec;
219 msec *= 1000;
220 msec += (end.tv_usec - begin.tv_usec) / 1000;
221
222#ifndef HAVE_STDARG_H
223#error "endTimer required stdarg functions"
224#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000225 va_start(ap, fmt);
226 vfprintf(stderr, fmt, ap);
Daniel Veillard01db67c2001-12-18 07:09:59 +0000227 va_end(ap);
228
229 fprintf(stderr, " took %ld ms\n", msec);
230}
231#elif defined(HAVE_TIME_H)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000232/*
233 * No gettimeofday function, so we have to make do with calling clock.
234 * This is obviously less accurate, but there's little we can do about
235 * that.
236 */
Daniel Veillard90bc3712002-03-07 15:12:58 +0000237#ifndef CLOCKS_PER_SEC
238#define CLOCKS_PER_SEC 100
239#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +0000240
241static clock_t begin, end;
242static void
243startTimer(void)
244{
245 begin = clock();
246}
247static void
248endTimer(const char *fmt, ...)
249{
250 long msec;
251 va_list ap;
252
253 end = clock();
254 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
255
256#ifndef HAVE_STDARG_H
257#error "endTimer required stdarg functions"
258#endif
259 va_start(ap, fmt);
260 vfprintf(stderr, fmt, ap);
261 va_end(ap);
262 fprintf(stderr, " took %ld ms\n", msec);
263}
264#else
265
266/*
267 * We don't have a gettimeofday or time.h, so we just don't do timing
268 */
269static void
270startTimer(void)
271{
272 /*
273 * Do nothing
274 */
275}
276static void
277endTimer(char *format, ...)
278{
279 /*
280 * We cannot do anything because we don't have a timing function
281 */
282#ifdef HAVE_STDARG_H
283 va_start(ap, format);
284 vfprintf(stderr, format, ap);
285 va_end(ap);
286 fprintf(stderr, " was not timed\n", msec);
287#else
288 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
289 * this ?!
290 */
291#endif
292}
293#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000294/************************************************************************
295 * *
296 * HTML ouput *
297 * *
298 ************************************************************************/
299char buffer[50000];
300
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000301static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000302xmlHTMLEncodeSend(void) {
303 char *result;
304
305 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
306 if (result) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000307 xmlGenericError(xmlGenericErrorContext, "%s", result);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000308 xmlFree(result);
309 }
310 buffer[0] = 0;
311}
312
313/**
314 * xmlHTMLPrintFileInfo:
315 * @input: an xmlParserInputPtr input
316 *
317 * Displays the associated file and line informations for the current input
318 */
319
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000320static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000321xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000322 int len;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000323 xmlGenericError(xmlGenericErrorContext, "<p>");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000324
325 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000326 if (input != NULL) {
327 if (input->filename) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000328 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000329 input->line);
330 } else {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000331 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000332 }
333 }
334 xmlHTMLEncodeSend();
335}
336
337/**
338 * xmlHTMLPrintFileContext:
339 * @input: an xmlParserInputPtr input
340 *
341 * Displays current context within the input content for error tracking
342 */
343
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000344static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000345xmlHTMLPrintFileContext(xmlParserInputPtr input) {
346 const xmlChar *cur, *base;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000347 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000348 int n;
349
350 if (input == NULL) return;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000351 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000352 cur = input->cur;
353 base = input->base;
354 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
355 cur--;
356 }
357 n = 0;
358 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
359 cur--;
360 if ((*cur == '\n') || (*cur == '\r')) cur++;
361 base = cur;
362 n = 0;
363 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000364 len = strlen(buffer);
365 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
366 (unsigned char) *cur++);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000367 n++;
368 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000369 len = strlen(buffer);
370 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000371 cur = input->cur;
372 while ((*cur == '\n') || (*cur == '\r'))
373 cur--;
374 n = 0;
375 while ((cur != base) && (n++ < 80)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000376 len = strlen(buffer);
377 snprintf(&buffer[len], sizeof(buffer) - len, " ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000378 base++;
379 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000380 len = strlen(buffer);
381 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000382 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000383 xmlGenericError(xmlGenericErrorContext, "</pre>");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000384}
385
386/**
387 * xmlHTMLError:
388 * @ctx: an XML parser context
389 * @msg: the message to display/transmit
390 * @...: extra parameters for the message display
391 *
392 * Display and format an error messages, gives file, line, position and
393 * extra parameters.
394 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000395static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000396xmlHTMLError(void *ctx, const char *msg, ...)
397{
398 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
399 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000400 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000401 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000402
403 buffer[0] = 0;
404 input = ctxt->input;
405 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000406 input = ctxt->inputTab[ctxt->inputNr - 2];
407 }
408
409 xmlHTMLPrintFileInfo(input);
410
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000411 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000412 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000413 len = strlen(buffer);
414 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000415 va_end(args);
416 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000417 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000418
419 xmlHTMLPrintFileContext(input);
420 xmlHTMLEncodeSend();
421}
422
423/**
424 * xmlHTMLWarning:
425 * @ctx: an XML parser context
426 * @msg: the message to display/transmit
427 * @...: extra parameters for the message display
428 *
429 * Display and format a warning messages, gives file, line, position and
430 * extra parameters.
431 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000432static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000433xmlHTMLWarning(void *ctx, const char *msg, ...)
434{
435 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
436 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000437 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000438 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000439
440 buffer[0] = 0;
441 input = ctxt->input;
442 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000443 input = ctxt->inputTab[ctxt->inputNr - 2];
444 }
445
446
447 xmlHTMLPrintFileInfo(input);
448
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000449 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000450 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000451 len = strlen(buffer);
452 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000453 va_end(args);
454 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000455 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000456
457 xmlHTMLPrintFileContext(input);
458 xmlHTMLEncodeSend();
459}
460
461/**
462 * xmlHTMLValidityError:
463 * @ctx: an XML parser context
464 * @msg: the message to display/transmit
465 * @...: extra parameters for the message display
466 *
467 * Display and format an validity error messages, gives file,
468 * line, position and extra parameters.
469 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000470static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000471xmlHTMLValidityError(void *ctx, const char *msg, ...)
472{
473 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
474 xmlParserInputPtr input;
475 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000476 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000477
478 buffer[0] = 0;
479 input = ctxt->input;
480 if ((input->filename == NULL) && (ctxt->inputNr > 1))
481 input = ctxt->inputTab[ctxt->inputNr - 2];
482
483 xmlHTMLPrintFileInfo(input);
484
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000485 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000486 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000487 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000488 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000489 va_end(args);
490 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000491 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000492
493 xmlHTMLPrintFileContext(input);
494 xmlHTMLEncodeSend();
495}
496
497/**
498 * xmlHTMLValidityWarning:
499 * @ctx: an XML parser context
500 * @msg: the message to display/transmit
501 * @...: extra parameters for the message display
502 *
503 * Display and format a validity warning messages, gives file, line,
504 * position and extra parameters.
505 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000506static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000507xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
508{
509 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
510 xmlParserInputPtr input;
511 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000512 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000513
514 buffer[0] = 0;
515 input = ctxt->input;
516 if ((input->filename == NULL) && (ctxt->inputNr > 1))
517 input = ctxt->inputTab[ctxt->inputNr - 2];
518
519 xmlHTMLPrintFileInfo(input);
520
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000521 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000522 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000523 len = strlen(buffer);
524 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000525 va_end(args);
526 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000527 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000528
529 xmlHTMLPrintFileContext(input);
530 xmlHTMLEncodeSend();
531}
532
533/************************************************************************
534 * *
535 * Shell Interface *
536 * *
537 ************************************************************************/
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000538#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000539/**
540 * xmlShellReadline:
541 * @prompt: the prompt value
542 *
543 * Read a string
544 *
545 * Returns a pointer to it or NULL on EOF the caller is expected to
546 * free the returned string.
547 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000548static char *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000549xmlShellReadline(char *prompt) {
550#ifdef HAVE_LIBREADLINE
551 char *line_read;
552
553 /* Get a line from the user. */
554 line_read = readline (prompt);
555
556 /* If the line has any text in it, save it on the history. */
557 if (line_read && *line_read)
558 add_history (line_read);
559
560 return (line_read);
561#else
562 char line_read[501];
Daniel Veillard29e43992001-12-13 22:21:58 +0000563 char *ret;
564 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000565
566 if (prompt != NULL)
567 fprintf(stdout, "%s", prompt);
568 if (!fgets(line_read, 500, stdin))
569 return(NULL);
570 line_read[500] = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000571 len = strlen(line_read);
572 ret = (char *) malloc(len + 1);
573 if (ret != NULL) {
574 memcpy (ret, line_read, len + 1);
575 }
576 return(ret);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000577#endif
578}
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000579#endif /* LIBXML_DEBUG_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000580
581/************************************************************************
582 * *
Daniel Veillard5e873c42000-04-12 13:27:38 +0000583 * I/O Interfaces *
584 * *
585 ************************************************************************/
586
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000587static int myRead(FILE *f, char * buf, int len) {
588 return(fread(buf, 1, len, f));
Daniel Veillard5e873c42000-04-12 13:27:38 +0000589}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000590static void myClose(FILE *f) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000591 if (f != stdin) {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000592 fclose(f);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000593 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000594}
595
Daniel Veillard81273902003-09-30 00:43:48 +0000596#ifdef LIBXML_READER_ENABLED
Daniel Veillard5e873c42000-04-12 13:27:38 +0000597/************************************************************************
598 * *
Daniel Veillard7704fb12003-01-03 16:19:51 +0000599 * Stream Test processing *
600 * *
601 ************************************************************************/
Daniel Veillard7704fb12003-01-03 16:19:51 +0000602static void processNode(xmlTextReaderPtr reader) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000603 const xmlChar *name, *value;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000604
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000605 name = xmlTextReaderConstName(reader);
Daniel Veillarde59494f2003-01-04 16:35:29 +0000606 if (name == NULL)
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000607 name = BAD_CAST "--";
Daniel Veillard99737f52003-03-22 14:55:50 +0000608
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000609 value = xmlTextReaderConstValue(reader);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000610
Daniel Veillard99737f52003-03-22 14:55:50 +0000611 printf("%d %d %s %d %d",
Daniel Veillarde59494f2003-01-04 16:35:29 +0000612 xmlTextReaderDepth(reader),
613 xmlTextReaderNodeType(reader),
614 name,
Daniel Veillard99737f52003-03-22 14:55:50 +0000615 xmlTextReaderIsEmptyElement(reader),
616 xmlTextReaderHasValue(reader));
Daniel Veillarde59494f2003-01-04 16:35:29 +0000617 if (value == NULL)
618 printf("\n");
619 else {
620 printf(" %s\n", value);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000621 }
622}
623
624static void streamFile(char *filename) {
625 xmlTextReaderPtr reader;
626 int ret;
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000627#ifdef HAVE_SYS_MMAN_H
628 int fd = -1;
629 struct stat info;
630 const char *base = NULL;
631 xmlParserInputBufferPtr input = NULL;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000632
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000633 if (memory) {
634 if (stat(filename, &info) < 0)
635 return;
636 if ((fd = open(filename, O_RDONLY)) < 0)
637 return;
638 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
639 if (base == (void *) MAP_FAILED)
640 return;
641
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000642 reader = xmlReaderForMemory(base, info.st_size, filename,
643 NULL, options);
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000644 } else
645#endif
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000646 reader = xmlReaderForFile(filename, NULL, options);
647
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000648
Daniel Veillard7704fb12003-01-03 16:19:51 +0000649 if (reader != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +0000650#ifdef LIBXML_VALID_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000651 if (valid)
652 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +0000653 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000654#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce192eb2003-04-16 15:58:05 +0000655 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000656#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +0000657 if (relaxng != NULL) {
Daniel Veillard81514ba2003-09-16 23:17:26 +0000658 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000659 startTimer();
660 }
661 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
662 if (ret < 0) {
663 xmlGenericError(xmlGenericErrorContext,
664 "Relax-NG schema %s failed to compile\n", relaxng);
Daniel Veillard05d987b2003-10-08 11:54:57 +0000665 progresult = 5;
Daniel Veillardce192eb2003-04-16 15:58:05 +0000666 relaxng = NULL;
667 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000668 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000669 endTimer("Compiling the schemas");
670 }
671 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000672#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000673
674 /*
675 * Process all nodes in sequence
676 */
Daniel Veillard81514ba2003-09-16 23:17:26 +0000677 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000678 startTimer();
679 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000680 ret = xmlTextReaderRead(reader);
681 while (ret == 1) {
682 if (debug)
683 processNode(reader);
684 ret = xmlTextReaderRead(reader);
685 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000686 if ((timing) && (!repeat)) {
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000687#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +0000688 if ((valid) || (relaxng != NULL))
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000689#else
Daniel Veillard4432df22003-09-28 18:58:27 +0000690#ifdef LIBXML_VALID_ENABLED
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000691 if (valid)
Daniel Veillardce192eb2003-04-16 15:58:05 +0000692 endTimer("Parsing and validating");
693 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000694#endif /* LIBXML_VALID_ENABLED */
695#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +0000696 endTimer("Parsing");
697 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000698
Daniel Veillard4432df22003-09-28 18:58:27 +0000699#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf6bad792003-04-11 19:38:54 +0000700 if (valid) {
701 if (xmlTextReaderIsValid(reader) != 1) {
702 xmlGenericError(xmlGenericErrorContext,
703 "Document %s does not validate\n", filename);
704 progresult = 3;
705 }
706 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000707#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000708#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +0000709 if (relaxng != NULL) {
710 if (xmlTextReaderIsValid(reader) != 1) {
711 printf("%s fails to validate\n", filename);
712 progresult = 3;
713 } else {
714 printf("%s validates\n", filename);
715 }
716 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000717#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000718 /*
719 * Done, cleanup and status
720 */
721 xmlFreeTextReader(reader);
722 if (ret != 0) {
723 printf("%s : failed to parse\n", filename);
Daniel Veillardf6bad792003-04-11 19:38:54 +0000724 progresult = 1;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000725 }
726 } else {
727 fprintf(stderr, "Unable to open %s\n", filename);
Daniel Veillardf6bad792003-04-11 19:38:54 +0000728 progresult = 1;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000729 }
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000730#ifdef HAVE_SYS_MMAN_H
731 if (memory) {
732 xmlFreeParserInputBuffer(input);
733 munmap((char *) base, info.st_size);
734 close(fd);
735 }
736#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000737}
Daniel Veillard7899c5c2003-11-03 12:31:38 +0000738
739static void walkDoc(xmlDocPtr doc) {
740 xmlTextReaderPtr reader;
741 int ret;
742
743 reader = xmlReaderWalker(doc);
744 if (reader != NULL) {
745 if ((timing) && (!repeat)) {
746 startTimer();
747 }
748 ret = xmlTextReaderRead(reader);
749 while (ret == 1) {
750 if (debug)
751 processNode(reader);
752 ret = xmlTextReaderRead(reader);
753 }
754 if ((timing) && (!repeat)) {
755 endTimer("walking through the doc");
756 }
757 xmlFreeTextReader(reader);
758 if (ret != 0) {
759 printf("failed to walk through the doc\n");
760 progresult = 1;
761 }
762 } else {
763 fprintf(stderr, "Failed to crate a reader from the document\n");
764 progresult = 1;
765 }
766}
Daniel Veillard81273902003-09-30 00:43:48 +0000767#endif /* LIBXML_READER_ENABLED */
Daniel Veillard7704fb12003-01-03 16:19:51 +0000768
769/************************************************************************
770 * *
771 * Tree Test processing *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000772 * *
773 ************************************************************************/
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000774static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
Daniel Veillard652327a2003-09-29 18:02:38 +0000775 xmlDocPtr doc = NULL;
776#ifdef LIBXML_TREE_ENABLED
777 xmlDocPtr tmp;
778#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000779
Daniel Veillard48b2f892001-02-25 16:11:03 +0000780 if ((timing) && (!repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +0000781 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000782
783
Daniel Veillard652327a2003-09-29 18:02:38 +0000784#ifdef LIBXML_TREE_ENABLED
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000785 if (filename == NULL) {
786 if (generate) {
787 xmlNodePtr n;
788
789 doc = xmlNewDoc(BAD_CAST "1.0");
790 n = xmlNewNode(NULL, BAD_CAST "info");
791 xmlNodeSetContent(n, BAD_CAST "abc");
792 xmlDocSetRootElement(doc, n);
793 }
794 }
Daniel Veillard652327a2003-09-29 18:02:38 +0000795#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000796#ifdef LIBXML_HTML_ENABLED
Daniel Veillard73b013f2003-09-30 12:36:01 +0000797#ifdef LIBXML_PUSH_ENABLED
William M. Brack78637da2003-07-31 14:47:38 +0000798 else if ((html) && (push)) {
799 FILE *f;
800
801 f = fopen(filename, "r");
802 if (f != NULL) {
803 int res, size = 3;
804 char chars[4096];
805 htmlParserCtxtPtr ctxt;
806
807 /* if (repeat) */
808 size = 4096;
809 res = fread(chars, 1, 4, f);
810 if (res > 0) {
811 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
William M. Brack1d75c8a2003-10-27 13:48:16 +0000812 chars, res, filename, XML_CHAR_ENCODING_NONE);
William M. Brack78637da2003-07-31 14:47:38 +0000813 while ((res = fread(chars, 1, size, f)) > 0) {
814 htmlParseChunk(ctxt, chars, res, 0);
815 }
816 htmlParseChunk(ctxt, chars, 0, 1);
817 doc = ctxt->myDoc;
818 htmlFreeParserCtxt(ctxt);
819 }
820 fclose(f);
821 }
822 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000823#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000824 else if (html) {
Daniel Veillard9475a352003-09-26 12:47:50 +0000825 doc = htmlReadFile(filename, NULL, options);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000826 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000827#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000828 else {
Daniel Veillard73b013f2003-09-30 12:36:01 +0000829#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000830 /*
831 * build an XML tree from a string;
832 */
833 if (push) {
834 FILE *f;
835
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000836 /* '-' Usually means stdin -<sven@zen.org> */
837 if ((filename[0] == '-') && (filename[1] == 0)) {
838 f = stdin;
839 } else {
840 f = fopen(filename, "r");
841 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000842 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +0000843 int ret;
Daniel Veillarda880b122003-04-21 21:36:41 +0000844 int res, size = 1024;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000845 char chars[1024];
846 xmlParserCtxtPtr ctxt;
847
Daniel Veillarda880b122003-04-21 21:36:41 +0000848 /* if (repeat) size = 1024; */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000849 res = fread(chars, 1, 4, f);
850 if (res > 0) {
851 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
852 chars, res, filename);
853 while ((res = fread(chars, 1, size, f)) > 0) {
854 xmlParseChunk(ctxt, chars, res, 0);
855 }
856 xmlParseChunk(ctxt, chars, 0, 1);
857 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +0000858 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000859 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +0000860 if (!ret) {
861 xmlFreeDoc(doc);
862 doc = NULL;
863 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000864 }
865 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000866 } else
867#endif /* LIBXML_PUSH_ENABLED */
868 if (testIO) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000869 if ((filename[0] == '-') && (filename[1] == 0)) {
Daniel Veillard60942de2003-09-25 21:05:58 +0000870 doc = xmlReadFd(0, NULL, NULL, options);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000871 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000872 FILE *f;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000873
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000874 f = fopen(filename, "r");
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000875 if (f != NULL) {
876 if (rectxt == NULL)
877 doc = xmlReadIO((xmlInputReadCallback) myRead,
878 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +0000879 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000880 else
881 doc = xmlCtxtReadIO(rectxt,
882 (xmlInputReadCallback) myRead,
883 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +0000884 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000885 } else
Daniel Veillard5e873c42000-04-12 13:27:38 +0000886 doc = NULL;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000887 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000888 } else if (htmlout) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000889 xmlParserCtxtPtr ctxt;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000890
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000891 if (rectxt == NULL)
892 ctxt = xmlNewParserCtxt();
893 else
894 ctxt = rectxt;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000895 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000896 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000897 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000898 ctxt->sax->error = xmlHTMLError;
899 ctxt->sax->warning = xmlHTMLWarning;
900 ctxt->vctxt.error = xmlHTMLValidityError;
901 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000902
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000903 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000904
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000905 if (rectxt == NULL)
906 xmlFreeParserCtxt(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000907 }
Daniel Veillard46e370e2000-07-21 20:32:03 +0000908#ifdef HAVE_SYS_MMAN_H
909 } else if (memory) {
910 int fd;
911 struct stat info;
912 const char *base;
913 if (stat(filename, &info) < 0)
914 return;
915 if ((fd = open(filename, O_RDONLY)) < 0)
916 return;
917 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +0000918 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +0000919 return;
920
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000921 if (rectxt == NULL)
Daniel Veillard60942de2003-09-25 21:05:58 +0000922 doc = xmlReadMemory((char *) base, info.st_size,
923 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000924 else
Daniel Veillard60942de2003-09-25 21:05:58 +0000925 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
926 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000927
Daniel Veillard46e370e2000-07-21 20:32:03 +0000928 munmap((char *) base, info.st_size);
929#endif
Daniel Veillard4432df22003-09-28 18:58:27 +0000930#ifdef LIBXML_VALID_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +0000931 } else if (valid) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000932 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000933
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000934 if (rectxt == NULL)
935 ctxt = xmlNewParserCtxt();
936 else
937 ctxt = rectxt;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000938 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000939 doc = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000940 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000941 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
942
943 if (ctxt->valid == 0)
944 progresult = 4;
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000945 if (rectxt == NULL)
946 xmlFreeParserCtxt(ctxt);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000947 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000948#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +0000949 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000950 if (rectxt != NULL)
951 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
952 else
953 doc = xmlReadFile(filename, NULL, options);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000954 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000955 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000956
Daniel Veillard88a172f2000-08-04 18:23:10 +0000957 /*
958 * If we don't have a document we might as well give up. Do we
959 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000960 if (doc == NULL) {
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000961 progresult = 1;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000962 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000963 }
964
Daniel Veillard48b2f892001-02-25 16:11:03 +0000965 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000966 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000967 }
968
Daniel Veillard29e43992001-12-13 22:21:58 +0000969 /*
970 * Remove DOCTYPE nodes
971 */
972 if (dropdtd) {
973 xmlDtdPtr dtd;
974
975 dtd = xmlGetIntSubset(doc);
976 if (dtd != NULL) {
977 xmlUnlinkNode((xmlNodePtr)dtd);
978 xmlFreeDtd(dtd);
979 }
980 }
981
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000982#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +0000983 if (xinclude) {
984 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000985 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000986 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000987 xmlXIncludeProcess(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +0000988 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000989 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000990 }
991 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000992#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +0000993
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000994#ifdef LIBXML_DEBUG_ENABLED
995 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000996 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000997 */
998 if (shell)
999 xmlShell(doc, filename, xmlShellReadline, stdout);
1000#endif
1001
Daniel Veillard652327a2003-09-29 18:02:38 +00001002#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001003 /*
1004 * test intermediate copy if needed.
1005 */
1006 if (copy) {
1007 tmp = doc;
1008 doc = xmlCopyDoc(doc, 1);
1009 xmlFreeDoc(tmp);
1010 }
Daniel Veillard652327a2003-09-29 18:02:38 +00001011#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001012
Daniel Veillard4432df22003-09-28 18:58:27 +00001013#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001014 if ((insert) && (!html)) {
1015 const xmlChar* list[256];
1016 int nb, i;
1017 xmlNodePtr node;
1018
1019 if (doc->children != NULL) {
1020 node = doc->children;
1021 while ((node != NULL) && (node->last == NULL)) node = node->next;
1022 if (node != NULL) {
1023 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
1024 if (nb < 0) {
1025 printf("could not get valid list of elements\n");
1026 } else if (nb == 0) {
MDT 2001 John Fleckbbb9e432001-09-24 03:08:43 +00001027 printf("No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001028 } else {
MDT 2001 John Fleckbbb9e432001-09-24 03:08:43 +00001029 printf("%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001030 nb);
1031 for (i = 0;i < nb;i++) {
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001032 printf("%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001033 }
1034 }
1035 }
1036 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001037 }else
1038#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001039#ifdef LIBXML_READER_ENABLED
1040 if (walker) {
1041 walkDoc(doc);
1042 }
1043#endif /* LIBXML_READER_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001044#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001045 if (noout == 0) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001046 /*
1047 * print it.
1048 */
1049#ifdef LIBXML_DEBUG_ENABLED
1050 if (!debug) {
1051#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00001052 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001053 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001054 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001055#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001056 if (memory) {
1057 xmlChar *result;
1058 int len;
1059
1060 if (encoding != NULL) {
Daniel Veillardd536f702001-11-08 17:32:47 +00001061 if ( format ) {
1062 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
1063 } else {
1064 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
1065 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001066 } else {
Daniel Veillard90493a92001-08-14 14:12:47 +00001067 if (format)
1068 xmlDocDumpFormatMemory(doc, &result, &len, 1);
1069 else
1070 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001071 }
1072 if (result == NULL) {
1073 fprintf(stderr, "Failed to save\n");
1074 } else {
1075 write(1, result, len);
1076 xmlFree(result);
1077 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001078 } else
1079#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001080 if (compress) {
1081 xmlSaveFile(output ? output : "-", doc);
1082 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001083 else if (encoding != NULL) {
1084 if ( format ) {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001085 xmlSaveFormatFileEnc(output ? output : "-", doc, encoding, 1);
1086 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001087 else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001088 xmlSaveFileEnc(output ? output : "-", doc, encoding);
Daniel Veillardd536f702001-11-08 17:32:47 +00001089 }
1090 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001091 else if (format) {
1092 xmlSaveFormatFile(output ? output : "-", doc, 1);
1093 }
1094 else {
1095 FILE *out;
1096 if (output == NULL)
1097 out = stdout;
1098 else {
1099 out = fopen(output,"wb");
1100 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001101 if (out != NULL) {
Daniel Veillard828ce832003-10-08 19:19:10 +00001102 if (xmlDocDump(out, doc) < 0)
1103 progresult = 6;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001104
Daniel Veillard05d987b2003-10-08 11:54:57 +00001105 if (output != NULL)
1106 fclose(out);
1107 } else {
1108 fprintf(stderr, "failed to open %s\n", output);
1109 progresult = 6;
1110 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001111 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001112 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001113 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001114 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001115#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001116 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001117 FILE *out;
1118 if (output == NULL)
1119 out = stdout;
1120 else {
1121 out = fopen(output,"wb");
1122 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001123 if (out != NULL) {
1124 xmlDebugDumpDocument(out, doc);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001125
Daniel Veillard05d987b2003-10-08 11:54:57 +00001126 if (output != NULL)
1127 fclose(out);
1128 } else {
1129 fprintf(stderr, "failed to open %s\n", output);
1130 progresult = 6;
1131 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001132 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001133#endif
1134 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001135#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001136
Daniel Veillard4432df22003-09-28 18:58:27 +00001137#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001138 /*
1139 * A posteriori validation test
1140 */
Daniel Veillard66f68e72003-08-18 16:39:51 +00001141 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
Daniel Veillardcd429612000-10-11 15:57:05 +00001142 xmlDtdPtr dtd;
1143
Daniel Veillard48b2f892001-02-25 16:11:03 +00001144 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001145 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001146 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00001147 if (dtdvalid != NULL)
1148 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
1149 else
1150 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001151 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001152 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001153 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001154 if (dtd == NULL) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001155 if (dtdvalid != NULL)
1156 xmlGenericError(xmlGenericErrorContext,
1157 "Could not parse DTD %s\n", dtdvalid);
1158 else
1159 xmlGenericError(xmlGenericErrorContext,
1160 "Could not parse DTD %s\n", dtdvalidfpi);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001161 progresult = 2;
Daniel Veillardcd429612000-10-11 15:57:05 +00001162 } else {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001163 xmlValidCtxtPtr cvp;
1164
1165 if ((cvp = xmlNewValidCtxt()) == NULL) {
1166 xmlGenericError(xmlGenericErrorContext,
1167 "Couldn't allocate validation context\n");
1168 exit(-1);
1169 }
1170 cvp->userData = (void *) stderr;
1171 cvp->error = (xmlValidityErrorFunc) fprintf;
1172 cvp->warning = (xmlValidityWarningFunc) fprintf;
1173
Daniel Veillard48b2f892001-02-25 16:11:03 +00001174 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001175 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001176 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001177 if (!xmlValidateDtd(cvp, doc, dtd)) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001178 if (dtdvalid != NULL)
1179 xmlGenericError(xmlGenericErrorContext,
1180 "Document %s does not validate against %s\n",
1181 filename, dtdvalid);
1182 else
1183 xmlGenericError(xmlGenericErrorContext,
1184 "Document %s does not validate against %s\n",
1185 filename, dtdvalidfpi);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001186 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +00001187 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001188 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001189 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001190 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001191 xmlFreeValidCtxt(cvp);
Daniel Veillardcd429612000-10-11 15:57:05 +00001192 xmlFreeDtd(dtd);
1193 }
1194 } else if (postvalid) {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001195 xmlValidCtxtPtr cvp;
1196
1197 if ((cvp = xmlNewValidCtxt()) == NULL) {
1198 xmlGenericError(xmlGenericErrorContext,
1199 "Couldn't allocate validation context\n");
1200 exit(-1);
1201 }
1202
Daniel Veillard48b2f892001-02-25 16:11:03 +00001203 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001204 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001205 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001206 cvp->userData = (void *) stderr;
1207 cvp->error = (xmlValidityErrorFunc) fprintf;
1208 cvp->warning = (xmlValidityWarningFunc) fprintf;
1209 if (!xmlValidateDocument(cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001210 xmlGenericError(xmlGenericErrorContext,
1211 "Document %s does not validate\n", filename);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001212 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +00001213 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001214 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001215 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001216 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001217 xmlFreeValidCtxt(cvp);
Daniel Veillard4432df22003-09-28 18:58:27 +00001218 }
1219#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00001220#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001221 if (relaxngschemas != NULL) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001222 xmlRelaxNGValidCtxtPtr ctxt;
1223 int ret;
1224
Daniel Veillard42f12e92003-03-07 18:32:59 +00001225 if ((timing) && (!repeat)) {
1226 startTimer();
1227 }
1228
Daniel Veillard71531f32003-02-05 13:19:53 +00001229 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
1230 xmlRelaxNGSetValidErrors(ctxt,
1231 (xmlRelaxNGValidityErrorFunc) fprintf,
1232 (xmlRelaxNGValidityWarningFunc) fprintf,
1233 stderr);
1234 ret = xmlRelaxNGValidateDoc(ctxt, doc);
1235 if (ret == 0) {
1236 printf("%s validates\n", filename);
1237 } else if (ret > 0) {
1238 printf("%s fails to validate\n", filename);
1239 } else {
1240 printf("%s validation generated an internal error\n",
1241 filename);
1242 }
1243 xmlRelaxNGFreeValidCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001244 if ((timing) && (!repeat)) {
1245 endTimer("Validating");
1246 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001247 } else if (wxschemas != NULL) {
1248 xmlSchemaValidCtxtPtr ctxt;
1249 int ret;
1250
1251 if ((timing) && (!repeat)) {
1252 startTimer();
1253 }
1254
1255 ctxt = xmlSchemaNewValidCtxt(wxschemas);
1256 xmlSchemaSetValidErrors(ctxt,
1257 (xmlSchemaValidityErrorFunc) fprintf,
1258 (xmlSchemaValidityWarningFunc) fprintf,
1259 stderr);
1260 ret = xmlSchemaValidateDoc(ctxt, doc);
1261 if (ret == 0) {
1262 printf("%s validates\n", filename);
1263 } else if (ret > 0) {
1264 printf("%s fails to validate\n", filename);
1265 } else {
1266 printf("%s validation generated an internal error\n",
1267 filename);
1268 }
1269 xmlSchemaFreeValidCtxt(ctxt);
1270 if ((timing) && (!repeat)) {
1271 endTimer("Validating");
1272 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001273 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001274#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001275
1276#ifdef LIBXML_DEBUG_ENABLED
1277 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001278 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001279#endif
1280
1281 /*
1282 * free it.
1283 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00001284 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001285 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001286 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001287 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001288 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001289 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001290 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001291}
1292
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001293/************************************************************************
1294 * *
1295 * Usage and Main *
1296 * *
1297 ************************************************************************/
1298
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001299static void showVersion(const char *name) {
1300 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
1301 fprintf(stderr, " compiled with: ");
Daniel Veillard4432df22003-09-28 18:58:27 +00001302#ifdef LIBXML_VALID_ENABLED
1303 fprintf(stderr, "DTDValid ");
1304#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001305#ifdef LIBXML_FTP_ENABLED
1306 fprintf(stderr, "FTP ");
1307#endif
1308#ifdef LIBXML_HTTP_ENABLED
1309 fprintf(stderr, "HTTP ");
1310#endif
1311#ifdef LIBXML_HTML_ENABLED
1312 fprintf(stderr, "HTML ");
1313#endif
1314#ifdef LIBXML_C14N_ENABLED
1315 fprintf(stderr, "C14N ");
1316#endif
1317#ifdef LIBXML_CATALOG_ENABLED
1318 fprintf(stderr, "Catalog ");
1319#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001320#ifdef LIBXML_XPATH_ENABLED
1321 fprintf(stderr, "XPath ");
1322#endif
1323#ifdef LIBXML_XPTR_ENABLED
1324 fprintf(stderr, "XPointer ");
1325#endif
1326#ifdef LIBXML_XINCLUDE_ENABLED
1327 fprintf(stderr, "XInclude ");
1328#endif
1329#ifdef LIBXML_ICONV_ENABLED
1330 fprintf(stderr, "Iconv ");
1331#endif
1332#ifdef DEBUG_MEMORY_LOCATION
1333 fprintf(stderr, "MemDebug ");
1334#endif
1335#ifdef LIBXML_UNICODE_ENABLED
1336 fprintf(stderr, "Unicode ");
1337#endif
1338#ifdef LIBXML_REGEXP_ENABLED
1339 fprintf(stderr, "Regexps ");
1340#endif
1341#ifdef LIBXML_AUTOMATA_ENABLED
1342 fprintf(stderr, "Automata ");
1343#endif
1344#ifdef LIBXML_SCHEMAS_ENABLED
1345 fprintf(stderr, "Schemas ");
1346#endif
1347 fprintf(stderr, "\n");
1348}
1349
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001350static void usage(const char *name) {
1351 printf("Usage : %s [options] XMLfiles ...\n", name);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001352#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001353 printf("\tParse the XML files and output the result of the parsing\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001354#else
1355 printf("\tParse the XML files\n");
1356#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001357 printf("\t--version : display the version of the XML library used\n");
1358#ifdef LIBXML_DEBUG_ENABLED
1359 printf("\t--debug : dump a debug tree of the in-memory document\n");
1360 printf("\t--shell : run a navigating shell\n");
1361 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00001362#else
Daniel Veillard81273902003-09-30 00:43:48 +00001363#ifdef LIBXML_READER_ENABLED
Daniel Veillard8326e732003-01-07 00:19:07 +00001364 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001365#endif /* LIBXML_READER_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001366#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001367#ifdef LIBXML_TREE_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001368 printf("\t--copy : used to test the internal copy implementation\n");
Daniel Veillard652327a2003-09-29 18:02:38 +00001369#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001370 printf("\t--recover : output what was parsable on broken XML documents\n");
1371 printf("\t--noent : substitute entity references by their value\n");
1372 printf("\t--noout : don't output the result tree\n");
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001373 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001374 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001375 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001376#ifdef LIBXML_VALID_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001377 printf("\t--valid : validate the document in addition to std well-formed check\n");
1378 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
1379 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
Daniel Veillard66f68e72003-08-18 16:39:51 +00001380 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001381#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001382 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001383 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001384 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
1385 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001386#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001387#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001388 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001389#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001390#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001391#ifdef LIBXML_HTML_ENABLED
1392 printf("\t--html : use the HTML parser\n");
1393#endif
Daniel Veillard73b013f2003-09-30 12:36:01 +00001394#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001395 printf("\t--push : use the push mode of the parser\n");
Daniel Veillard73b013f2003-09-30 12:36:01 +00001396#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001397#ifdef HAVE_SYS_MMAN_H
1398 printf("\t--memory : parse from memory\n");
1399#endif
1400 printf("\t--nowarning : do not emit warnings from parser/validator\n");
1401 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001402 printf("\t--nocdata : replace cdata section with text nodes\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001403#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001404 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001405 printf("\t--encode encoding : output in the given encoding\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001406 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
1407#endif /* LIBXML_OUTPUT_ENABLED */
1408 printf("\t--nsclean : remove redundant namespace declarations\n");
1409 printf("\t--testIO : test user I/O support\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001410#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00001411 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
1412 printf("\t otherwise XML Catalogs starting from \n");
Daniel Veillard3be27512003-01-26 19:49:04 +00001413 printf("\t " XML_XML_DEFAULT_CATALOG " are activated by default\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001414 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001415#endif
1416 printf("\t--auto : generate a small doc on the fly\n");
1417#ifdef LIBXML_XINCLUDE_ENABLED
1418 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001419#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001420 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00001421 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard81273902003-09-30 00:43:48 +00001422#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001423 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001424 printf("\t--walker : create a reader and walk though the resulting doc\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001425#endif /* LIBXML_READER_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001426 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillardef4d3bc2003-02-07 12:38:22 +00001427#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard71531f32003-02-05 13:19:53 +00001428 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001429 printf("\t--schema schema : do validation against the WXS schema\n");
Daniel Veillard71531f32003-02-05 13:19:53 +00001430#endif
Daniel Veillarda42f25f2002-01-25 14:15:40 +00001431 printf("\nLibxml project home page: http://xmlsoft.org/\n");
1432 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001433}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001434
1435static void registerNode(xmlNodePtr node)
1436{
1437 node->_private = malloc(sizeof(long));
1438 *(long*)node->_private = (long) 0x81726354;
1439}
1440
1441static void deregisterNode(xmlNodePtr node)
1442{
1443 assert(node->_private != NULL);
1444 assert(*(long*)node->_private == (long) 0x81726354);
1445 free(node->_private);
1446}
1447
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001448int
1449main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001450 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001451 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00001452 int version = 0;
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001453 const char* indent;
1454
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001455 if (argc <= 1) {
1456 usage(argv[0]);
1457 return(1);
1458 }
Daniel Veillardbe803962000-06-28 23:40:59 +00001459 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001460 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001461 if (!strcmp(argv[i], "-"))
1462 break;
1463
1464 if (argv[i][0] != '-')
1465 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001466 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
1467 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00001468 else
1469#ifdef LIBXML_DEBUG_ENABLED
1470 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001471 (!strcmp(argv[i], "--shell"))) {
1472 shell++;
1473 noout = 1;
1474 } else
1475#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001476#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001477 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
1478 copy++;
Daniel Veillard652327a2003-09-29 18:02:38 +00001479 else
1480#endif /* LIBXML_TREE_ENABLED */
1481 if ((!strcmp(argv[i], "-recover")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001482 (!strcmp(argv[i], "--recover"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001483 recovery++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001484 options |= XML_PARSE_RECOVER;
1485 } else if ((!strcmp(argv[i], "-noent")) ||
1486 (!strcmp(argv[i], "--noent"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001487 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001488 options |= XML_PARSE_NOENT;
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001489 } else if ((!strcmp(argv[i], "-nsclean")) ||
1490 (!strcmp(argv[i], "--nsclean"))) {
1491 options |= XML_PARSE_NSCLEAN;
1492 } else if ((!strcmp(argv[i], "-nocdata")) ||
1493 (!strcmp(argv[i], "--nocdata"))) {
1494 options |= XML_PARSE_NOCDATA;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001495 } else if ((!strcmp(argv[i], "-nodict")) ||
1496 (!strcmp(argv[i], "--nodict"))) {
1497 options |= XML_PARSE_NODICT;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001498 } else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00001499 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001500 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00001501 version = 1;
1502 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001503 (!strcmp(argv[i], "--noout")))
1504 noout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001505#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001506 else if ((!strcmp(argv[i], "-o")) ||
1507 (!strcmp(argv[i], "-output")) ||
1508 (!strcmp(argv[i], "--output"))) {
1509 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00001510 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001511 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001512#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001513 else if ((!strcmp(argv[i], "-htmlout")) ||
1514 (!strcmp(argv[i], "--htmlout")))
1515 htmlout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001516 else if ((!strcmp(argv[i], "-nowrap")) ||
1517 (!strcmp(argv[i], "--nowrap")))
1518 nowrap++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001519#ifdef LIBXML_HTML_ENABLED
1520 else if ((!strcmp(argv[i], "-html")) ||
1521 (!strcmp(argv[i], "--html"))) {
1522 html++;
1523 }
1524#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001525 else if ((!strcmp(argv[i], "-loaddtd")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001526 (!strcmp(argv[i], "--loaddtd"))) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001527 loaddtd++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001528 options |= XML_PARSE_DTDLOAD;
1529 } else if ((!strcmp(argv[i], "-dtdattr")) ||
Daniel Veillard48da9102001-08-07 01:10:10 +00001530 (!strcmp(argv[i], "--dtdattr"))) {
1531 loaddtd++;
1532 dtdattrs++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001533 options |= XML_PARSE_DTDATTR;
Daniel Veillard4432df22003-09-28 18:58:27 +00001534 }
1535#ifdef LIBXML_VALID_ENABLED
1536 else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001537 (!strcmp(argv[i], "--valid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001538 valid++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001539 options |= XML_PARSE_DTDVALID;
1540 } else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001541 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001542 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001543 loaddtd++;
1544 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00001545 (!strcmp(argv[i], "--dtdvalid"))) {
1546 i++;
1547 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001548 loaddtd++;
Daniel Veillard66f68e72003-08-18 16:39:51 +00001549 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
1550 (!strcmp(argv[i], "--dtdvalidfpi"))) {
1551 i++;
1552 dtdvalidfpi = argv[i];
1553 loaddtd++;
Daniel Veillardcd429612000-10-11 15:57:05 +00001554 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001555#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard29e43992001-12-13 22:21:58 +00001556 else if ((!strcmp(argv[i], "-dropdtd")) ||
1557 (!strcmp(argv[i], "--dropdtd")))
1558 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001559 else if ((!strcmp(argv[i], "-insert")) ||
1560 (!strcmp(argv[i], "--insert")))
1561 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001562 else if ((!strcmp(argv[i], "-timing")) ||
1563 (!strcmp(argv[i], "--timing")))
1564 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001565 else if ((!strcmp(argv[i], "-auto")) ||
1566 (!strcmp(argv[i], "--auto")))
1567 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001568 else if ((!strcmp(argv[i], "-repeat")) ||
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001569 (!strcmp(argv[i], "--repeat"))) {
1570 if (repeat)
1571 repeat *= 10;
1572 else
1573 repeat = 100;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001574 }
1575#ifdef LIBXML_PUSH_ENABLED
1576 else if ((!strcmp(argv[i], "-push")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001577 (!strcmp(argv[i], "--push")))
1578 push++;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001579#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +00001580#ifdef HAVE_SYS_MMAN_H
1581 else if ((!strcmp(argv[i], "-memory")) ||
1582 (!strcmp(argv[i], "--memory")))
1583 memory++;
1584#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00001585 else if ((!strcmp(argv[i], "-testIO")) ||
1586 (!strcmp(argv[i], "--testIO")))
1587 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001588#ifdef LIBXML_XINCLUDE_ENABLED
1589 else if ((!strcmp(argv[i], "-xinclude")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001590 (!strcmp(argv[i], "--xinclude"))) {
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001591 xinclude++;
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001592 options |= XML_PARSE_XINCLUDE;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001593 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001594#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001595#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001596#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001597 else if ((!strcmp(argv[i], "-compress")) ||
1598 (!strcmp(argv[i], "--compress"))) {
1599 compress++;
1600 xmlSetCompressMode(9);
1601 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001602#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001603#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001604 else if ((!strcmp(argv[i], "-nowarning")) ||
1605 (!strcmp(argv[i], "--nowarning"))) {
1606 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001607 xmlPedanticParserDefault(0);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001608 options |= XML_PARSE_NOWARNING;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001609 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001610 else if ((!strcmp(argv[i], "-pedantic")) ||
1611 (!strcmp(argv[i], "--pedantic"))) {
1612 xmlGetWarningsDefaultValue = 1;
1613 xmlPedanticParserDefault(1);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001614 options |= XML_PARSE_PEDANTIC;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001615 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001616#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001617 else if ((!strcmp(argv[i], "-debugent")) ||
1618 (!strcmp(argv[i], "--debugent"))) {
1619 debugent++;
1620 xmlParserDebugEntities = 1;
1621 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001622#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00001623#ifdef LIBXML_CATALOG_ENABLED
1624 else if ((!strcmp(argv[i], "-catalogs")) ||
1625 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001626 catalogs++;
1627 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
1628 (!strcmp(argv[i], "--nocatalogs"))) {
1629 nocatalogs++;
Daniel Veillard81418e32001-05-22 15:08:55 +00001630 }
1631#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00001632 else if ((!strcmp(argv[i], "-encode")) ||
1633 (!strcmp(argv[i], "--encode"))) {
1634 i++;
1635 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001636 /*
1637 * OK it's for testing purposes
1638 */
1639 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00001640 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001641 else if ((!strcmp(argv[i], "-noblanks")) ||
1642 (!strcmp(argv[i], "--noblanks"))) {
1643 noblanks++;
1644 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00001645 }
1646 else if ((!strcmp(argv[i], "-format")) ||
1647 (!strcmp(argv[i], "--format"))) {
1648 noblanks++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001649#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001650 format++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001651#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard90493a92001-08-14 14:12:47 +00001652 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001653 }
Daniel Veillard81273902003-09-30 00:43:48 +00001654#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001655 else if ((!strcmp(argv[i], "-stream")) ||
1656 (!strcmp(argv[i], "--stream"))) {
1657 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001658 }
Daniel Veillard7899c5c2003-11-03 12:31:38 +00001659 else if ((!strcmp(argv[i], "-walker")) ||
1660 (!strcmp(argv[i], "--walker"))) {
1661 walker++;
1662 noout++;
1663 }
Daniel Veillard81273902003-09-30 00:43:48 +00001664#endif /* LIBXML_READER_ENABLED */
1665#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001666 else if ((!strcmp(argv[i], "-sax1")) ||
1667 (!strcmp(argv[i], "--sax1"))) {
1668 sax1++;
1669 }
Daniel Veillard81273902003-09-30 00:43:48 +00001670#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001671 else if ((!strcmp(argv[i], "-chkregister")) ||
1672 (!strcmp(argv[i], "--chkregister"))) {
1673 chkregister++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001674#ifdef LIBXML_SCHEMAS_ENABLED
1675 } else if ((!strcmp(argv[i], "-relaxng")) ||
1676 (!strcmp(argv[i], "--relaxng"))) {
1677 i++;
1678 relaxng = argv[i];
1679 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001680 options |= XML_PARSE_NOENT;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001681 } else if ((!strcmp(argv[i], "-schema")) ||
1682 (!strcmp(argv[i], "--schema"))) {
1683 i++;
1684 schema = argv[i];
1685 noent++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001686#endif
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001687 } else if ((!strcmp(argv[i], "-nonet")) ||
1688 (!strcmp(argv[i], "--nonet"))) {
1689 xmlSetExternalEntityLoader(xmlNoNetExternalEntityLoader);
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001690 } else {
1691 fprintf(stderr, "Unknown option %s\n", argv[i]);
1692 usage(argv[0]);
1693 return(1);
1694 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001695 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001696
1697#ifdef LIBXML_CATALOG_ENABLED
1698 if (nocatalogs == 0) {
1699 if (catalogs) {
1700 const char *catal;
1701
1702 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00001703 if (catal != NULL) {
1704 xmlLoadCatalogs(catal);
1705 } else {
1706 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
1707 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001708 }
1709 }
1710#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001711
Daniel Veillard81273902003-09-30 00:43:48 +00001712#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001713 if (sax1)
1714 xmlSAXDefaultVersion(1);
1715 else
1716 xmlSAXDefaultVersion(2);
Daniel Veillard81273902003-09-30 00:43:48 +00001717#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00001718
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001719 if (chkregister) {
1720 xmlRegisterNodeDefault(registerNode);
1721 xmlDeregisterNodeDefault(deregisterNode);
1722 }
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001723
1724 indent = getenv("XMLLINT_INDENT");
1725 if(indent != NULL) {
1726 xmlTreeIndentString = indent;
1727 }
1728
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001729
Daniel Veillardd9bad132001-07-23 19:39:43 +00001730 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00001731 if (loaddtd != 0)
1732 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
1733 if (dtdattrs)
1734 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001735 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillard4432df22003-09-28 18:58:27 +00001736#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001737 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard4432df22003-09-28 18:58:27 +00001738#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001739 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001740 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001741 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001742 xmlGenericError(xmlGenericErrorContext,
1743 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
1744 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001745 "<html><head><title>%s output</title></head>\n",
1746 argv[0]);
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001747 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001748 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
1749 argv[0]);
1750 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001751
1752#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard81273902003-09-30 00:43:48 +00001753 if ((relaxng != NULL)
1754#ifdef LIBXML_READER_ENABLED
1755 && (stream == 0)
1756#endif /* LIBXML_READER_ENABLED */
1757 ) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001758 xmlRelaxNGParserCtxtPtr ctxt;
1759
Daniel Veillardce192eb2003-04-16 15:58:05 +00001760 /* forces loading the DTDs */
1761 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001762 options |= XML_PARSE_DTDLOAD;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001763 if (timing) {
1764 startTimer();
1765 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001766 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
1767 xmlRelaxNGSetParserErrors(ctxt,
1768 (xmlRelaxNGValidityErrorFunc) fprintf,
1769 (xmlRelaxNGValidityWarningFunc) fprintf,
1770 stderr);
1771 relaxngschemas = xmlRelaxNGParse(ctxt);
Daniel Veillardce192eb2003-04-16 15:58:05 +00001772 if (relaxngschemas == NULL) {
1773 xmlGenericError(xmlGenericErrorContext,
1774 "Relax-NG schema %s failed to compile\n", relaxng);
Daniel Veillard05d987b2003-10-08 11:54:57 +00001775 progresult = 5;
Daniel Veillardce192eb2003-04-16 15:58:05 +00001776 relaxng = NULL;
1777 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001778 xmlRelaxNGFreeParserCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001779 if (timing) {
1780 endTimer("Compiling the schemas");
1781 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001782 } else if ((schema != NULL) && (stream == 0)) {
1783 xmlSchemaParserCtxtPtr ctxt;
1784
1785 if (timing) {
1786 startTimer();
1787 }
1788 ctxt = xmlSchemaNewParserCtxt(schema);
1789 xmlSchemaSetParserErrors(ctxt,
1790 (xmlSchemaValidityErrorFunc) fprintf,
1791 (xmlSchemaValidityWarningFunc) fprintf,
1792 stderr);
1793 wxschemas = xmlSchemaParse(ctxt);
1794 if (wxschemas == NULL) {
1795 xmlGenericError(xmlGenericErrorContext,
1796 "WXS schema %s failed to compile\n", schema);
Daniel Veillard05d987b2003-10-08 11:54:57 +00001797 progresult = 5;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001798 schema = NULL;
1799 }
1800 xmlSchemaFreeParserCtxt(ctxt);
1801 if (timing) {
1802 endTimer("Compiling the schemas");
1803 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001804 }
1805#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001806 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00001807 if ((!strcmp(argv[i], "-encode")) ||
1808 (!strcmp(argv[i], "--encode"))) {
1809 i++;
1810 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001811 } else if ((!strcmp(argv[i], "-o")) ||
1812 (!strcmp(argv[i], "-output")) ||
1813 (!strcmp(argv[i], "--output"))) {
1814 i++;
1815 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00001816 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001817#ifdef LIBXML_VALID_ENABLED
Daniel Veillardcd429612000-10-11 15:57:05 +00001818 if ((!strcmp(argv[i], "-dtdvalid")) ||
1819 (!strcmp(argv[i], "--dtdvalid"))) {
1820 i++;
1821 continue;
1822 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00001823 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
1824 (!strcmp(argv[i], "--dtdvalidfpi"))) {
1825 i++;
1826 continue;
1827 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001828#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00001829 if ((!strcmp(argv[i], "-relaxng")) ||
1830 (!strcmp(argv[i], "--relaxng"))) {
1831 i++;
1832 continue;
1833 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001834 if ((!strcmp(argv[i], "-schema")) ||
1835 (!strcmp(argv[i], "--schema"))) {
1836 i++;
1837 continue;
1838 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001839 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00001840 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001841 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001842 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001843 if (repeat) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001844 xmlParserCtxtPtr ctxt = NULL;
1845
1846 for (acount = 0;acount < repeat;acount++) {
Daniel Veillard81273902003-09-30 00:43:48 +00001847#ifdef LIBXML_READER_ENABLED
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001848 if (stream != 0) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001849 streamFile(argv[i]);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001850 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00001851#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001852 if (ctxt == NULL)
1853 ctxt = xmlNewParserCtxt();
1854 parseAndPrintFile(argv[i], ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00001855#ifdef LIBXML_READER_ENABLED
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001856 }
Daniel Veillard81273902003-09-30 00:43:48 +00001857#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001858 }
1859 if (ctxt != NULL)
1860 xmlFreeParserCtxt(ctxt);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001861 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00001862#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001863 if (stream != 0)
1864 streamFile(argv[i]);
1865 else
Daniel Veillard81273902003-09-30 00:43:48 +00001866#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001867 parseAndPrintFile(argv[i], NULL);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001868 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001869 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00001870 if ((timing) && (repeat)) {
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001871 endTimer("%d iterations", repeat);
Daniel Veillarda7866932001-12-04 13:14:44 +00001872 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001873 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001874 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001875 if (generate)
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001876 parseAndPrintFile(NULL, NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001877 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001878 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001879 }
Daniel Veillard845cce42002-01-09 11:51:37 +00001880 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001881 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001882 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001883#ifdef LIBXML_SCHEMAS_ENABLED
1884 if (relaxngschemas != NULL)
1885 xmlRelaxNGFree(relaxngschemas);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001886 if (wxschemas != NULL)
1887 xmlSchemaFree(wxschemas);
Daniel Veillard71531f32003-02-05 13:19:53 +00001888 xmlRelaxNGCleanupTypes();
1889#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001890 xmlCleanupParser();
1891 xmlMemoryDump();
1892
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001893 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001894}
Daniel Veillard88a172f2000-08-04 18:23:10 +00001895