blob: d0d2ed2016455fe585bff1e02321f013085f726e [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 Veillard81273902003-09-30 00:43:48 +0000156#endif /* LIBXML_READER_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000157static int chkregister = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000158#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +0000159static int sax1 = 0;
Daniel Veillard81273902003-09-30 00:43:48 +0000160#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000161static int options = 0;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000162
Daniel Veillard01db67c2001-12-18 07:09:59 +0000163/*
164 * Internal timing routines to remove the necessity to have unix-specific
165 * function calls
166 */
167
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000168#ifndef HAVE_GETTIMEOFDAY
169#ifdef HAVE_SYS_TIMEB_H
170#ifdef HAVE_SYS_TIME_H
171#ifdef HAVE_FTIME
172
Daniel Veillard01c13b52002-12-10 15:19:08 +0000173static int
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000174my_gettimeofday(struct timeval *tvp, void *tzp)
175{
176 struct timeb timebuffer;
177
178 ftime(&timebuffer);
179 if (tvp) {
180 tvp->tv_sec = timebuffer.time;
181 tvp->tv_usec = timebuffer.millitm * 1000L;
182 }
183 return (0);
184}
185#define HAVE_GETTIMEOFDAY 1
186#define gettimeofday my_gettimeofday
187
188#endif /* HAVE_FTIME */
189#endif /* HAVE_SYS_TIME_H */
190#endif /* HAVE_SYS_TIMEB_H */
191#endif /* !HAVE_GETTIMEOFDAY */
192
Daniel Veillard01db67c2001-12-18 07:09:59 +0000193#if defined(HAVE_GETTIMEOFDAY)
194static struct timeval begin, end;
195
196/*
197 * startTimer: call where you want to start timing
198 */
199static void
200startTimer(void)
201{
202 gettimeofday(&begin, NULL);
203}
204
205/*
206 * endTimer: call where you want to stop timing and to print out a
207 * message about the timing performed; format is a printf
208 * type argument
209 */
210static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000211endTimer(const char *fmt, ...)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000212{
213 long msec;
214 va_list ap;
215
216 gettimeofday(&end, NULL);
217 msec = end.tv_sec - begin.tv_sec;
218 msec *= 1000;
219 msec += (end.tv_usec - begin.tv_usec) / 1000;
220
221#ifndef HAVE_STDARG_H
222#error "endTimer required stdarg functions"
223#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000224 va_start(ap, fmt);
225 vfprintf(stderr, fmt, ap);
Daniel Veillard01db67c2001-12-18 07:09:59 +0000226 va_end(ap);
227
228 fprintf(stderr, " took %ld ms\n", msec);
229}
230#elif defined(HAVE_TIME_H)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000231/*
232 * No gettimeofday function, so we have to make do with calling clock.
233 * This is obviously less accurate, but there's little we can do about
234 * that.
235 */
Daniel Veillard90bc3712002-03-07 15:12:58 +0000236#ifndef CLOCKS_PER_SEC
237#define CLOCKS_PER_SEC 100
238#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +0000239
240static clock_t begin, end;
241static void
242startTimer(void)
243{
244 begin = clock();
245}
246static void
247endTimer(const char *fmt, ...)
248{
249 long msec;
250 va_list ap;
251
252 end = clock();
253 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
254
255#ifndef HAVE_STDARG_H
256#error "endTimer required stdarg functions"
257#endif
258 va_start(ap, fmt);
259 vfprintf(stderr, fmt, ap);
260 va_end(ap);
261 fprintf(stderr, " took %ld ms\n", msec);
262}
263#else
264
265/*
266 * We don't have a gettimeofday or time.h, so we just don't do timing
267 */
268static void
269startTimer(void)
270{
271 /*
272 * Do nothing
273 */
274}
275static void
276endTimer(char *format, ...)
277{
278 /*
279 * We cannot do anything because we don't have a timing function
280 */
281#ifdef HAVE_STDARG_H
282 va_start(ap, format);
283 vfprintf(stderr, format, ap);
284 va_end(ap);
285 fprintf(stderr, " was not timed\n", msec);
286#else
287 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
288 * this ?!
289 */
290#endif
291}
292#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000293/************************************************************************
294 * *
295 * HTML ouput *
296 * *
297 ************************************************************************/
298char buffer[50000];
299
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000300static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000301xmlHTMLEncodeSend(void) {
302 char *result;
303
304 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
305 if (result) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000306 xmlGenericError(xmlGenericErrorContext, "%s", result);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000307 xmlFree(result);
308 }
309 buffer[0] = 0;
310}
311
312/**
313 * xmlHTMLPrintFileInfo:
314 * @input: an xmlParserInputPtr input
315 *
316 * Displays the associated file and line informations for the current input
317 */
318
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000319static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000320xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000321 int len;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000322 xmlGenericError(xmlGenericErrorContext, "<p>");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000323
324 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000325 if (input != NULL) {
326 if (input->filename) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000327 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000328 input->line);
329 } else {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000330 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000331 }
332 }
333 xmlHTMLEncodeSend();
334}
335
336/**
337 * xmlHTMLPrintFileContext:
338 * @input: an xmlParserInputPtr input
339 *
340 * Displays current context within the input content for error tracking
341 */
342
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000343static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000344xmlHTMLPrintFileContext(xmlParserInputPtr input) {
345 const xmlChar *cur, *base;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000346 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000347 int n;
348
349 if (input == NULL) return;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000350 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000351 cur = input->cur;
352 base = input->base;
353 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
354 cur--;
355 }
356 n = 0;
357 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
358 cur--;
359 if ((*cur == '\n') || (*cur == '\r')) cur++;
360 base = cur;
361 n = 0;
362 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000363 len = strlen(buffer);
364 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
365 (unsigned char) *cur++);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000366 n++;
367 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000368 len = strlen(buffer);
369 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000370 cur = input->cur;
371 while ((*cur == '\n') || (*cur == '\r'))
372 cur--;
373 n = 0;
374 while ((cur != base) && (n++ < 80)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000375 len = strlen(buffer);
376 snprintf(&buffer[len], sizeof(buffer) - len, " ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000377 base++;
378 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000379 len = strlen(buffer);
380 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000381 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000382 xmlGenericError(xmlGenericErrorContext, "</pre>");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000383}
384
385/**
386 * xmlHTMLError:
387 * @ctx: an XML parser context
388 * @msg: the message to display/transmit
389 * @...: extra parameters for the message display
390 *
391 * Display and format an error messages, gives file, line, position and
392 * extra parameters.
393 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000394static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000395xmlHTMLError(void *ctx, const char *msg, ...)
396{
397 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
398 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000399 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000400 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000401
402 buffer[0] = 0;
403 input = ctxt->input;
404 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000405 input = ctxt->inputTab[ctxt->inputNr - 2];
406 }
407
408 xmlHTMLPrintFileInfo(input);
409
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000410 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000411 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000412 len = strlen(buffer);
413 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000414 va_end(args);
415 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000416 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000417
418 xmlHTMLPrintFileContext(input);
419 xmlHTMLEncodeSend();
420}
421
422/**
423 * xmlHTMLWarning:
424 * @ctx: an XML parser context
425 * @msg: the message to display/transmit
426 * @...: extra parameters for the message display
427 *
428 * Display and format a warning messages, gives file, line, position and
429 * extra parameters.
430 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000431static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000432xmlHTMLWarning(void *ctx, const char *msg, ...)
433{
434 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
435 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000436 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000437 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000438
439 buffer[0] = 0;
440 input = ctxt->input;
441 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000442 input = ctxt->inputTab[ctxt->inputNr - 2];
443 }
444
445
446 xmlHTMLPrintFileInfo(input);
447
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000448 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000449 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000450 len = strlen(buffer);
451 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000452 va_end(args);
453 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000454 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000455
456 xmlHTMLPrintFileContext(input);
457 xmlHTMLEncodeSend();
458}
459
460/**
461 * xmlHTMLValidityError:
462 * @ctx: an XML parser context
463 * @msg: the message to display/transmit
464 * @...: extra parameters for the message display
465 *
466 * Display and format an validity error messages, gives file,
467 * line, position and extra parameters.
468 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000469static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000470xmlHTMLValidityError(void *ctx, const char *msg, ...)
471{
472 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
473 xmlParserInputPtr input;
474 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000475 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000476
477 buffer[0] = 0;
478 input = ctxt->input;
479 if ((input->filename == NULL) && (ctxt->inputNr > 1))
480 input = ctxt->inputTab[ctxt->inputNr - 2];
481
482 xmlHTMLPrintFileInfo(input);
483
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000484 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000485 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000486 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000487 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000488 va_end(args);
489 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000490 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000491
492 xmlHTMLPrintFileContext(input);
493 xmlHTMLEncodeSend();
494}
495
496/**
497 * xmlHTMLValidityWarning:
498 * @ctx: an XML parser context
499 * @msg: the message to display/transmit
500 * @...: extra parameters for the message display
501 *
502 * Display and format a validity warning messages, gives file, line,
503 * position and extra parameters.
504 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000505static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000506xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
507{
508 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
509 xmlParserInputPtr input;
510 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000511 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000512
513 buffer[0] = 0;
514 input = ctxt->input;
515 if ((input->filename == NULL) && (ctxt->inputNr > 1))
516 input = ctxt->inputTab[ctxt->inputNr - 2];
517
518 xmlHTMLPrintFileInfo(input);
519
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000520 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000521 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000522 len = strlen(buffer);
523 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000524 va_end(args);
525 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000526 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000527
528 xmlHTMLPrintFileContext(input);
529 xmlHTMLEncodeSend();
530}
531
532/************************************************************************
533 * *
534 * Shell Interface *
535 * *
536 ************************************************************************/
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000537#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000538/**
539 * xmlShellReadline:
540 * @prompt: the prompt value
541 *
542 * Read a string
543 *
544 * Returns a pointer to it or NULL on EOF the caller is expected to
545 * free the returned string.
546 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000547static char *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000548xmlShellReadline(char *prompt) {
549#ifdef HAVE_LIBREADLINE
550 char *line_read;
551
552 /* Get a line from the user. */
553 line_read = readline (prompt);
554
555 /* If the line has any text in it, save it on the history. */
556 if (line_read && *line_read)
557 add_history (line_read);
558
559 return (line_read);
560#else
561 char line_read[501];
Daniel Veillard29e43992001-12-13 22:21:58 +0000562 char *ret;
563 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000564
565 if (prompt != NULL)
566 fprintf(stdout, "%s", prompt);
567 if (!fgets(line_read, 500, stdin))
568 return(NULL);
569 line_read[500] = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000570 len = strlen(line_read);
571 ret = (char *) malloc(len + 1);
572 if (ret != NULL) {
573 memcpy (ret, line_read, len + 1);
574 }
575 return(ret);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000576#endif
577}
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000578#endif /* LIBXML_DEBUG_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000579
580/************************************************************************
581 * *
Daniel Veillard5e873c42000-04-12 13:27:38 +0000582 * I/O Interfaces *
583 * *
584 ************************************************************************/
585
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000586static int myRead(FILE *f, char * buf, int len) {
587 return(fread(buf, 1, len, f));
Daniel Veillard5e873c42000-04-12 13:27:38 +0000588}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000589static void myClose(FILE *f) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000590 if (f != stdin) {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000591 fclose(f);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000592 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000593}
594
Daniel Veillard81273902003-09-30 00:43:48 +0000595#ifdef LIBXML_READER_ENABLED
Daniel Veillard5e873c42000-04-12 13:27:38 +0000596/************************************************************************
597 * *
Daniel Veillard7704fb12003-01-03 16:19:51 +0000598 * Stream Test processing *
599 * *
600 ************************************************************************/
Daniel Veillard7704fb12003-01-03 16:19:51 +0000601static void processNode(xmlTextReaderPtr reader) {
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000602 const xmlChar *name, *value;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000603
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000604 name = xmlTextReaderConstName(reader);
Daniel Veillarde59494f2003-01-04 16:35:29 +0000605 if (name == NULL)
606 name = xmlStrdup(BAD_CAST "--");
Daniel Veillard99737f52003-03-22 14:55:50 +0000607
Daniel Veillard198c1bf2003-10-20 17:07:41 +0000608 value = xmlTextReaderConstValue(reader);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000609
Daniel Veillard99737f52003-03-22 14:55:50 +0000610 printf("%d %d %s %d %d",
Daniel Veillarde59494f2003-01-04 16:35:29 +0000611 xmlTextReaderDepth(reader),
612 xmlTextReaderNodeType(reader),
613 name,
Daniel Veillard99737f52003-03-22 14:55:50 +0000614 xmlTextReaderIsEmptyElement(reader),
615 xmlTextReaderHasValue(reader));
Daniel Veillarde59494f2003-01-04 16:35:29 +0000616 if (value == NULL)
617 printf("\n");
618 else {
619 printf(" %s\n", value);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000620 }
621}
622
623static void streamFile(char *filename) {
624 xmlTextReaderPtr reader;
625 int ret;
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000626#ifdef HAVE_SYS_MMAN_H
627 int fd = -1;
628 struct stat info;
629 const char *base = NULL;
630 xmlParserInputBufferPtr input = NULL;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000631
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000632 if (memory) {
633 if (stat(filename, &info) < 0)
634 return;
635 if ((fd = open(filename, O_RDONLY)) < 0)
636 return;
637 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
638 if (base == (void *) MAP_FAILED)
639 return;
640
Daniel Veillard53350552003-09-18 13:35:51 +0000641 input = xmlParserInputBufferCreateStatic((char *) base, info.st_size,
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000642 XML_CHAR_ENCODING_NONE);
643 reader = xmlNewTextReader(input, filename);
644 } else
645#endif
646 reader = xmlNewTextReaderFilename(filename);
647
Daniel Veillard7704fb12003-01-03 16:19:51 +0000648 if (reader != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +0000649#ifdef LIBXML_VALID_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000650 if (valid)
651 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +0000652 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000653#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce192eb2003-04-16 15:58:05 +0000654 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000655#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +0000656 if (relaxng != NULL) {
Daniel Veillard81514ba2003-09-16 23:17:26 +0000657 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000658 startTimer();
659 }
660 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
661 if (ret < 0) {
662 xmlGenericError(xmlGenericErrorContext,
663 "Relax-NG schema %s failed to compile\n", relaxng);
Daniel Veillard05d987b2003-10-08 11:54:57 +0000664 progresult = 5;
Daniel Veillardce192eb2003-04-16 15:58:05 +0000665 relaxng = NULL;
666 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000667 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000668 endTimer("Compiling the schemas");
669 }
670 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000671#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000672
673 /*
674 * Process all nodes in sequence
675 */
Daniel Veillard81514ba2003-09-16 23:17:26 +0000676 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000677 startTimer();
678 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000679 ret = xmlTextReaderRead(reader);
680 while (ret == 1) {
681 if (debug)
682 processNode(reader);
683 ret = xmlTextReaderRead(reader);
684 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000685 if ((timing) && (!repeat)) {
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000686#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +0000687 if ((valid) || (relaxng != NULL))
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000688#else
Daniel Veillard4432df22003-09-28 18:58:27 +0000689#ifdef LIBXML_VALID_ENABLED
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000690 if (valid)
Daniel Veillardce192eb2003-04-16 15:58:05 +0000691 endTimer("Parsing and validating");
692 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000693#endif /* LIBXML_VALID_ENABLED */
694#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +0000695 endTimer("Parsing");
696 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000697
Daniel Veillard4432df22003-09-28 18:58:27 +0000698#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf6bad792003-04-11 19:38:54 +0000699 if (valid) {
700 if (xmlTextReaderIsValid(reader) != 1) {
701 xmlGenericError(xmlGenericErrorContext,
702 "Document %s does not validate\n", filename);
703 progresult = 3;
704 }
705 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000706#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000707#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +0000708 if (relaxng != NULL) {
709 if (xmlTextReaderIsValid(reader) != 1) {
710 printf("%s fails to validate\n", filename);
711 progresult = 3;
712 } else {
713 printf("%s validates\n", filename);
714 }
715 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000716#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000717 /*
718 * Done, cleanup and status
719 */
720 xmlFreeTextReader(reader);
721 if (ret != 0) {
722 printf("%s : failed to parse\n", filename);
Daniel Veillardf6bad792003-04-11 19:38:54 +0000723 progresult = 1;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000724 }
725 } else {
726 fprintf(stderr, "Unable to open %s\n", filename);
Daniel Veillardf6bad792003-04-11 19:38:54 +0000727 progresult = 1;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000728 }
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000729#ifdef HAVE_SYS_MMAN_H
730 if (memory) {
731 xmlFreeParserInputBuffer(input);
732 munmap((char *) base, info.st_size);
733 close(fd);
734 }
735#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000736}
Daniel Veillard81273902003-09-30 00:43:48 +0000737#endif /* LIBXML_READER_ENABLED */
Daniel Veillard7704fb12003-01-03 16:19:51 +0000738
739/************************************************************************
740 * *
741 * Tree Test processing *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000742 * *
743 ************************************************************************/
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000744static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
Daniel Veillard652327a2003-09-29 18:02:38 +0000745 xmlDocPtr doc = NULL;
746#ifdef LIBXML_TREE_ENABLED
747 xmlDocPtr tmp;
748#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000749
Daniel Veillard48b2f892001-02-25 16:11:03 +0000750 if ((timing) && (!repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +0000751 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000752
753
Daniel Veillard652327a2003-09-29 18:02:38 +0000754#ifdef LIBXML_TREE_ENABLED
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000755 if (filename == NULL) {
756 if (generate) {
757 xmlNodePtr n;
758
759 doc = xmlNewDoc(BAD_CAST "1.0");
760 n = xmlNewNode(NULL, BAD_CAST "info");
761 xmlNodeSetContent(n, BAD_CAST "abc");
762 xmlDocSetRootElement(doc, n);
763 }
764 }
Daniel Veillard652327a2003-09-29 18:02:38 +0000765#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000766#ifdef LIBXML_HTML_ENABLED
Daniel Veillard73b013f2003-09-30 12:36:01 +0000767#ifdef LIBXML_PUSH_ENABLED
William M. Brack78637da2003-07-31 14:47:38 +0000768 else if ((html) && (push)) {
769 FILE *f;
770
771 f = fopen(filename, "r");
772 if (f != NULL) {
773 int res, size = 3;
774 char chars[4096];
775 htmlParserCtxtPtr ctxt;
776
777 /* if (repeat) */
778 size = 4096;
779 res = fread(chars, 1, 4, f);
780 if (res > 0) {
781 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
782 chars, res, filename, 0);
783 while ((res = fread(chars, 1, size, f)) > 0) {
784 htmlParseChunk(ctxt, chars, res, 0);
785 }
786 htmlParseChunk(ctxt, chars, 0, 1);
787 doc = ctxt->myDoc;
788 htmlFreeParserCtxt(ctxt);
789 }
790 fclose(f);
791 }
792 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000793#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000794 else if (html) {
Daniel Veillard9475a352003-09-26 12:47:50 +0000795 doc = htmlReadFile(filename, NULL, options);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000796 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000797#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000798 else {
Daniel Veillard73b013f2003-09-30 12:36:01 +0000799#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000800 /*
801 * build an XML tree from a string;
802 */
803 if (push) {
804 FILE *f;
805
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000806 /* '-' Usually means stdin -<sven@zen.org> */
807 if ((filename[0] == '-') && (filename[1] == 0)) {
808 f = stdin;
809 } else {
810 f = fopen(filename, "r");
811 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000812 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +0000813 int ret;
Daniel Veillarda880b122003-04-21 21:36:41 +0000814 int res, size = 1024;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000815 char chars[1024];
816 xmlParserCtxtPtr ctxt;
817
Daniel Veillarda880b122003-04-21 21:36:41 +0000818 /* if (repeat) size = 1024; */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000819 res = fread(chars, 1, 4, f);
820 if (res > 0) {
821 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
822 chars, res, filename);
823 while ((res = fread(chars, 1, size, f)) > 0) {
824 xmlParseChunk(ctxt, chars, res, 0);
825 }
826 xmlParseChunk(ctxt, chars, 0, 1);
827 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +0000828 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000829 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +0000830 if (!ret) {
831 xmlFreeDoc(doc);
832 doc = NULL;
833 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000834 }
835 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000836 } else
837#endif /* LIBXML_PUSH_ENABLED */
838 if (testIO) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000839 if ((filename[0] == '-') && (filename[1] == 0)) {
Daniel Veillard60942de2003-09-25 21:05:58 +0000840 doc = xmlReadFd(0, NULL, NULL, options);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000841 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000842 FILE *f;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000843
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000844 f = fopen(filename, "r");
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000845 if (f != NULL) {
846 if (rectxt == NULL)
847 doc = xmlReadIO((xmlInputReadCallback) myRead,
848 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +0000849 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000850 else
851 doc = xmlCtxtReadIO(rectxt,
852 (xmlInputReadCallback) myRead,
853 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +0000854 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000855 } else
Daniel Veillard5e873c42000-04-12 13:27:38 +0000856 doc = NULL;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000857 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000858 } else if (htmlout) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000859 xmlParserCtxtPtr ctxt;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000860
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000861 if (rectxt == NULL)
862 ctxt = xmlNewParserCtxt();
863 else
864 ctxt = rectxt;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000865 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000866 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000867 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000868 ctxt->sax->error = xmlHTMLError;
869 ctxt->sax->warning = xmlHTMLWarning;
870 ctxt->vctxt.error = xmlHTMLValidityError;
871 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000872
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000873 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000874
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000875 if (rectxt == NULL)
876 xmlFreeParserCtxt(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000877 }
Daniel Veillard46e370e2000-07-21 20:32:03 +0000878#ifdef HAVE_SYS_MMAN_H
879 } else if (memory) {
880 int fd;
881 struct stat info;
882 const char *base;
883 if (stat(filename, &info) < 0)
884 return;
885 if ((fd = open(filename, O_RDONLY)) < 0)
886 return;
887 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +0000888 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +0000889 return;
890
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000891 if (rectxt == NULL)
Daniel Veillard60942de2003-09-25 21:05:58 +0000892 doc = xmlReadMemory((char *) base, info.st_size,
893 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000894 else
Daniel Veillard60942de2003-09-25 21:05:58 +0000895 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
896 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000897
Daniel Veillard46e370e2000-07-21 20:32:03 +0000898 munmap((char *) base, info.st_size);
899#endif
Daniel Veillard4432df22003-09-28 18:58:27 +0000900#ifdef LIBXML_VALID_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +0000901 } else if (valid) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000902 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000903
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000904 if (rectxt == NULL)
905 ctxt = xmlNewParserCtxt();
906 else
907 ctxt = rectxt;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000908 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000909 doc = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000910 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000911 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
912
913 if (ctxt->valid == 0)
914 progresult = 4;
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000915 if (rectxt == NULL)
916 xmlFreeParserCtxt(ctxt);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000917 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000918#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +0000919 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000920 if (rectxt != NULL)
921 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
922 else
923 doc = xmlReadFile(filename, NULL, options);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000924 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000925 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000926
Daniel Veillard88a172f2000-08-04 18:23:10 +0000927 /*
928 * If we don't have a document we might as well give up. Do we
929 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000930 if (doc == NULL) {
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000931 progresult = 1;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000932 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000933 }
934
Daniel Veillard48b2f892001-02-25 16:11:03 +0000935 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000936 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000937 }
938
Daniel Veillard29e43992001-12-13 22:21:58 +0000939 /*
940 * Remove DOCTYPE nodes
941 */
942 if (dropdtd) {
943 xmlDtdPtr dtd;
944
945 dtd = xmlGetIntSubset(doc);
946 if (dtd != NULL) {
947 xmlUnlinkNode((xmlNodePtr)dtd);
948 xmlFreeDtd(dtd);
949 }
950 }
951
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000952#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +0000953 if (xinclude) {
954 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000955 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000956 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000957 xmlXIncludeProcess(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +0000958 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000959 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000960 }
961 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000962#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +0000963
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000964#ifdef LIBXML_DEBUG_ENABLED
965 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000966 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000967 */
968 if (shell)
969 xmlShell(doc, filename, xmlShellReadline, stdout);
970#endif
971
Daniel Veillard652327a2003-09-29 18:02:38 +0000972#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000973 /*
974 * test intermediate copy if needed.
975 */
976 if (copy) {
977 tmp = doc;
978 doc = xmlCopyDoc(doc, 1);
979 xmlFreeDoc(tmp);
980 }
Daniel Veillard652327a2003-09-29 18:02:38 +0000981#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000982
Daniel Veillard4432df22003-09-28 18:58:27 +0000983#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000984 if ((insert) && (!html)) {
985 const xmlChar* list[256];
986 int nb, i;
987 xmlNodePtr node;
988
989 if (doc->children != NULL) {
990 node = doc->children;
991 while ((node != NULL) && (node->last == NULL)) node = node->next;
992 if (node != NULL) {
993 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
994 if (nb < 0) {
995 printf("could not get valid list of elements\n");
996 } else if (nb == 0) {
MDT 2001 John Fleckbbb9e432001-09-24 03:08:43 +0000997 printf("No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000998 } else {
MDT 2001 John Fleckbbb9e432001-09-24 03:08:43 +0000999 printf("%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001000 nb);
1001 for (i = 0;i < nb;i++) {
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001002 printf("%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001003 }
1004 }
1005 }
1006 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001007 }else
1008#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001009#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001010 if (noout == 0) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001011 /*
1012 * print it.
1013 */
1014#ifdef LIBXML_DEBUG_ENABLED
1015 if (!debug) {
1016#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00001017 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001018 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001019 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001020#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001021 if (memory) {
1022 xmlChar *result;
1023 int len;
1024
1025 if (encoding != NULL) {
Daniel Veillardd536f702001-11-08 17:32:47 +00001026 if ( format ) {
1027 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
1028 } else {
1029 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
1030 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001031 } else {
Daniel Veillard90493a92001-08-14 14:12:47 +00001032 if (format)
1033 xmlDocDumpFormatMemory(doc, &result, &len, 1);
1034 else
1035 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001036 }
1037 if (result == NULL) {
1038 fprintf(stderr, "Failed to save\n");
1039 } else {
1040 write(1, result, len);
1041 xmlFree(result);
1042 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001043 } else
1044#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001045 if (compress) {
1046 xmlSaveFile(output ? output : "-", doc);
1047 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001048 else if (encoding != NULL) {
1049 if ( format ) {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001050 xmlSaveFormatFileEnc(output ? output : "-", doc, encoding, 1);
1051 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001052 else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001053 xmlSaveFileEnc(output ? output : "-", doc, encoding);
Daniel Veillardd536f702001-11-08 17:32:47 +00001054 }
1055 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001056 else if (format) {
1057 xmlSaveFormatFile(output ? output : "-", doc, 1);
1058 }
1059 else {
1060 FILE *out;
1061 if (output == NULL)
1062 out = stdout;
1063 else {
1064 out = fopen(output,"wb");
1065 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001066 if (out != NULL) {
Daniel Veillard828ce832003-10-08 19:19:10 +00001067 if (xmlDocDump(out, doc) < 0)
1068 progresult = 6;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001069
Daniel Veillard05d987b2003-10-08 11:54:57 +00001070 if (output != NULL)
1071 fclose(out);
1072 } else {
1073 fprintf(stderr, "failed to open %s\n", output);
1074 progresult = 6;
1075 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001076 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001077 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001078 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001079 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001080#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001081 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001082 FILE *out;
1083 if (output == NULL)
1084 out = stdout;
1085 else {
1086 out = fopen(output,"wb");
1087 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001088 if (out != NULL) {
1089 xmlDebugDumpDocument(out, doc);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001090
Daniel Veillard05d987b2003-10-08 11:54:57 +00001091 if (output != NULL)
1092 fclose(out);
1093 } else {
1094 fprintf(stderr, "failed to open %s\n", output);
1095 progresult = 6;
1096 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001097 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001098#endif
1099 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001100#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001101
Daniel Veillard4432df22003-09-28 18:58:27 +00001102#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001103 /*
1104 * A posteriori validation test
1105 */
Daniel Veillard66f68e72003-08-18 16:39:51 +00001106 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
Daniel Veillardcd429612000-10-11 15:57:05 +00001107 xmlDtdPtr dtd;
1108
Daniel Veillard48b2f892001-02-25 16:11:03 +00001109 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001110 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001111 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00001112 if (dtdvalid != NULL)
1113 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
1114 else
1115 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001116 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001117 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001118 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001119 if (dtd == NULL) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001120 if (dtdvalid != NULL)
1121 xmlGenericError(xmlGenericErrorContext,
1122 "Could not parse DTD %s\n", dtdvalid);
1123 else
1124 xmlGenericError(xmlGenericErrorContext,
1125 "Could not parse DTD %s\n", dtdvalidfpi);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001126 progresult = 2;
Daniel Veillardcd429612000-10-11 15:57:05 +00001127 } else {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001128 xmlValidCtxtPtr cvp;
1129
1130 if ((cvp = xmlNewValidCtxt()) == NULL) {
1131 xmlGenericError(xmlGenericErrorContext,
1132 "Couldn't allocate validation context\n");
1133 exit(-1);
1134 }
1135 cvp->userData = (void *) stderr;
1136 cvp->error = (xmlValidityErrorFunc) fprintf;
1137 cvp->warning = (xmlValidityWarningFunc) fprintf;
1138
Daniel Veillard48b2f892001-02-25 16:11:03 +00001139 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001140 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001141 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001142 if (!xmlValidateDtd(cvp, doc, dtd)) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001143 if (dtdvalid != NULL)
1144 xmlGenericError(xmlGenericErrorContext,
1145 "Document %s does not validate against %s\n",
1146 filename, dtdvalid);
1147 else
1148 xmlGenericError(xmlGenericErrorContext,
1149 "Document %s does not validate against %s\n",
1150 filename, dtdvalidfpi);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001151 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +00001152 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001153 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001154 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001155 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001156 xmlFreeValidCtxt(cvp);
Daniel Veillardcd429612000-10-11 15:57:05 +00001157 xmlFreeDtd(dtd);
1158 }
1159 } else if (postvalid) {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001160 xmlValidCtxtPtr cvp;
1161
1162 if ((cvp = xmlNewValidCtxt()) == NULL) {
1163 xmlGenericError(xmlGenericErrorContext,
1164 "Couldn't allocate validation context\n");
1165 exit(-1);
1166 }
1167
Daniel Veillard48b2f892001-02-25 16:11:03 +00001168 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001169 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001170 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001171 cvp->userData = (void *) stderr;
1172 cvp->error = (xmlValidityErrorFunc) fprintf;
1173 cvp->warning = (xmlValidityWarningFunc) fprintf;
1174 if (!xmlValidateDocument(cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001175 xmlGenericError(xmlGenericErrorContext,
1176 "Document %s does not validate\n", filename);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001177 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +00001178 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001179 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001180 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001181 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001182 xmlFreeValidCtxt(cvp);
Daniel Veillard4432df22003-09-28 18:58:27 +00001183 }
1184#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00001185#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001186 if (relaxngschemas != NULL) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001187 xmlRelaxNGValidCtxtPtr ctxt;
1188 int ret;
1189
Daniel Veillard42f12e92003-03-07 18:32:59 +00001190 if ((timing) && (!repeat)) {
1191 startTimer();
1192 }
1193
Daniel Veillard71531f32003-02-05 13:19:53 +00001194 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
1195 xmlRelaxNGSetValidErrors(ctxt,
1196 (xmlRelaxNGValidityErrorFunc) fprintf,
1197 (xmlRelaxNGValidityWarningFunc) fprintf,
1198 stderr);
1199 ret = xmlRelaxNGValidateDoc(ctxt, doc);
1200 if (ret == 0) {
1201 printf("%s validates\n", filename);
1202 } else if (ret > 0) {
1203 printf("%s fails to validate\n", filename);
1204 } else {
1205 printf("%s validation generated an internal error\n",
1206 filename);
1207 }
1208 xmlRelaxNGFreeValidCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001209 if ((timing) && (!repeat)) {
1210 endTimer("Validating");
1211 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001212 } else if (wxschemas != NULL) {
1213 xmlSchemaValidCtxtPtr ctxt;
1214 int ret;
1215
1216 if ((timing) && (!repeat)) {
1217 startTimer();
1218 }
1219
1220 ctxt = xmlSchemaNewValidCtxt(wxschemas);
1221 xmlSchemaSetValidErrors(ctxt,
1222 (xmlSchemaValidityErrorFunc) fprintf,
1223 (xmlSchemaValidityWarningFunc) fprintf,
1224 stderr);
1225 ret = xmlSchemaValidateDoc(ctxt, doc);
1226 if (ret == 0) {
1227 printf("%s validates\n", filename);
1228 } else if (ret > 0) {
1229 printf("%s fails to validate\n", filename);
1230 } else {
1231 printf("%s validation generated an internal error\n",
1232 filename);
1233 }
1234 xmlSchemaFreeValidCtxt(ctxt);
1235 if ((timing) && (!repeat)) {
1236 endTimer("Validating");
1237 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001238 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001239#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001240
1241#ifdef LIBXML_DEBUG_ENABLED
1242 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001243 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001244#endif
1245
1246 /*
1247 * free it.
1248 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00001249 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001250 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001251 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001252 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001253 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001254 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001255 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001256}
1257
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001258/************************************************************************
1259 * *
1260 * Usage and Main *
1261 * *
1262 ************************************************************************/
1263
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001264static void showVersion(const char *name) {
1265 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
1266 fprintf(stderr, " compiled with: ");
Daniel Veillard4432df22003-09-28 18:58:27 +00001267#ifdef LIBXML_VALID_ENABLED
1268 fprintf(stderr, "DTDValid ");
1269#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001270#ifdef LIBXML_FTP_ENABLED
1271 fprintf(stderr, "FTP ");
1272#endif
1273#ifdef LIBXML_HTTP_ENABLED
1274 fprintf(stderr, "HTTP ");
1275#endif
1276#ifdef LIBXML_HTML_ENABLED
1277 fprintf(stderr, "HTML ");
1278#endif
1279#ifdef LIBXML_C14N_ENABLED
1280 fprintf(stderr, "C14N ");
1281#endif
1282#ifdef LIBXML_CATALOG_ENABLED
1283 fprintf(stderr, "Catalog ");
1284#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001285#ifdef LIBXML_XPATH_ENABLED
1286 fprintf(stderr, "XPath ");
1287#endif
1288#ifdef LIBXML_XPTR_ENABLED
1289 fprintf(stderr, "XPointer ");
1290#endif
1291#ifdef LIBXML_XINCLUDE_ENABLED
1292 fprintf(stderr, "XInclude ");
1293#endif
1294#ifdef LIBXML_ICONV_ENABLED
1295 fprintf(stderr, "Iconv ");
1296#endif
1297#ifdef DEBUG_MEMORY_LOCATION
1298 fprintf(stderr, "MemDebug ");
1299#endif
1300#ifdef LIBXML_UNICODE_ENABLED
1301 fprintf(stderr, "Unicode ");
1302#endif
1303#ifdef LIBXML_REGEXP_ENABLED
1304 fprintf(stderr, "Regexps ");
1305#endif
1306#ifdef LIBXML_AUTOMATA_ENABLED
1307 fprintf(stderr, "Automata ");
1308#endif
1309#ifdef LIBXML_SCHEMAS_ENABLED
1310 fprintf(stderr, "Schemas ");
1311#endif
1312 fprintf(stderr, "\n");
1313}
1314
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001315static void usage(const char *name) {
1316 printf("Usage : %s [options] XMLfiles ...\n", name);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001317#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001318 printf("\tParse the XML files and output the result of the parsing\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001319#else
1320 printf("\tParse the XML files\n");
1321#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001322 printf("\t--version : display the version of the XML library used\n");
1323#ifdef LIBXML_DEBUG_ENABLED
1324 printf("\t--debug : dump a debug tree of the in-memory document\n");
1325 printf("\t--shell : run a navigating shell\n");
1326 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00001327#else
Daniel Veillard81273902003-09-30 00:43:48 +00001328#ifdef LIBXML_READER_ENABLED
Daniel Veillard8326e732003-01-07 00:19:07 +00001329 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001330#endif /* LIBXML_READER_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001331#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001332#ifdef LIBXML_TREE_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001333 printf("\t--copy : used to test the internal copy implementation\n");
Daniel Veillard652327a2003-09-29 18:02:38 +00001334#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001335 printf("\t--recover : output what was parsable on broken XML documents\n");
1336 printf("\t--noent : substitute entity references by their value\n");
1337 printf("\t--noout : don't output the result tree\n");
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001338 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001339 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001340 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001341#ifdef LIBXML_VALID_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001342 printf("\t--valid : validate the document in addition to std well-formed check\n");
1343 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
1344 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
Daniel Veillard66f68e72003-08-18 16:39:51 +00001345 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001346#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001347 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001348 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001349 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
1350 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001351#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001352#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001353 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001354#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001355#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001356#ifdef LIBXML_HTML_ENABLED
1357 printf("\t--html : use the HTML parser\n");
1358#endif
Daniel Veillard73b013f2003-09-30 12:36:01 +00001359#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001360 printf("\t--push : use the push mode of the parser\n");
Daniel Veillard73b013f2003-09-30 12:36:01 +00001361#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001362#ifdef HAVE_SYS_MMAN_H
1363 printf("\t--memory : parse from memory\n");
1364#endif
1365 printf("\t--nowarning : do not emit warnings from parser/validator\n");
1366 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001367 printf("\t--nocdata : replace cdata section with text nodes\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001368#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001369 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001370 printf("\t--encode encoding : output in the given encoding\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001371 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
1372#endif /* LIBXML_OUTPUT_ENABLED */
1373 printf("\t--nsclean : remove redundant namespace declarations\n");
1374 printf("\t--testIO : test user I/O support\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001375#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00001376 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
1377 printf("\t otherwise XML Catalogs starting from \n");
Daniel Veillard3be27512003-01-26 19:49:04 +00001378 printf("\t " XML_XML_DEFAULT_CATALOG " are activated by default\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001379 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001380#endif
1381 printf("\t--auto : generate a small doc on the fly\n");
1382#ifdef LIBXML_XINCLUDE_ENABLED
1383 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001384#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001385 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00001386 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard81273902003-09-30 00:43:48 +00001387#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001388 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001389#endif /* LIBXML_READER_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001390 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillardef4d3bc2003-02-07 12:38:22 +00001391#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard71531f32003-02-05 13:19:53 +00001392 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001393 printf("\t--schema schema : do validation against the WXS schema\n");
Daniel Veillard71531f32003-02-05 13:19:53 +00001394#endif
Daniel Veillarda42f25f2002-01-25 14:15:40 +00001395 printf("\nLibxml project home page: http://xmlsoft.org/\n");
1396 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001397}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001398
1399static void registerNode(xmlNodePtr node)
1400{
1401 node->_private = malloc(sizeof(long));
1402 *(long*)node->_private = (long) 0x81726354;
1403}
1404
1405static void deregisterNode(xmlNodePtr node)
1406{
1407 assert(node->_private != NULL);
1408 assert(*(long*)node->_private == (long) 0x81726354);
1409 free(node->_private);
1410}
1411
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001412int
1413main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001414 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001415 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00001416 int version = 0;
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001417 const char* indent;
1418
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001419 if (argc <= 1) {
1420 usage(argv[0]);
1421 return(1);
1422 }
Daniel Veillardbe803962000-06-28 23:40:59 +00001423 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001424 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001425 if (!strcmp(argv[i], "-"))
1426 break;
1427
1428 if (argv[i][0] != '-')
1429 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001430 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
1431 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00001432 else
1433#ifdef LIBXML_DEBUG_ENABLED
1434 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001435 (!strcmp(argv[i], "--shell"))) {
1436 shell++;
1437 noout = 1;
1438 } else
1439#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001440#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001441 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
1442 copy++;
Daniel Veillard652327a2003-09-29 18:02:38 +00001443 else
1444#endif /* LIBXML_TREE_ENABLED */
1445 if ((!strcmp(argv[i], "-recover")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001446 (!strcmp(argv[i], "--recover"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001447 recovery++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001448 options |= XML_PARSE_RECOVER;
1449 } else if ((!strcmp(argv[i], "-noent")) ||
1450 (!strcmp(argv[i], "--noent"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001451 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001452 options |= XML_PARSE_NOENT;
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001453 } else if ((!strcmp(argv[i], "-nsclean")) ||
1454 (!strcmp(argv[i], "--nsclean"))) {
1455 options |= XML_PARSE_NSCLEAN;
1456 } else if ((!strcmp(argv[i], "-nocdata")) ||
1457 (!strcmp(argv[i], "--nocdata"))) {
1458 options |= XML_PARSE_NOCDATA;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001459 } else if ((!strcmp(argv[i], "-nodict")) ||
1460 (!strcmp(argv[i], "--nodict"))) {
1461 options |= XML_PARSE_NODICT;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001462 } else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00001463 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001464 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00001465 version = 1;
1466 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001467 (!strcmp(argv[i], "--noout")))
1468 noout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001469#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001470 else if ((!strcmp(argv[i], "-o")) ||
1471 (!strcmp(argv[i], "-output")) ||
1472 (!strcmp(argv[i], "--output"))) {
1473 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00001474 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001475 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001476#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001477 else if ((!strcmp(argv[i], "-htmlout")) ||
1478 (!strcmp(argv[i], "--htmlout")))
1479 htmlout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001480 else if ((!strcmp(argv[i], "-nowrap")) ||
1481 (!strcmp(argv[i], "--nowrap")))
1482 nowrap++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001483#ifdef LIBXML_HTML_ENABLED
1484 else if ((!strcmp(argv[i], "-html")) ||
1485 (!strcmp(argv[i], "--html"))) {
1486 html++;
1487 }
1488#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001489 else if ((!strcmp(argv[i], "-loaddtd")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001490 (!strcmp(argv[i], "--loaddtd"))) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001491 loaddtd++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001492 options |= XML_PARSE_DTDLOAD;
1493 } else if ((!strcmp(argv[i], "-dtdattr")) ||
Daniel Veillard48da9102001-08-07 01:10:10 +00001494 (!strcmp(argv[i], "--dtdattr"))) {
1495 loaddtd++;
1496 dtdattrs++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001497 options |= XML_PARSE_DTDATTR;
Daniel Veillard4432df22003-09-28 18:58:27 +00001498 }
1499#ifdef LIBXML_VALID_ENABLED
1500 else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001501 (!strcmp(argv[i], "--valid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001502 valid++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001503 options |= XML_PARSE_DTDVALID;
1504 } else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001505 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001506 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001507 loaddtd++;
1508 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00001509 (!strcmp(argv[i], "--dtdvalid"))) {
1510 i++;
1511 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001512 loaddtd++;
Daniel Veillard66f68e72003-08-18 16:39:51 +00001513 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
1514 (!strcmp(argv[i], "--dtdvalidfpi"))) {
1515 i++;
1516 dtdvalidfpi = argv[i];
1517 loaddtd++;
Daniel Veillardcd429612000-10-11 15:57:05 +00001518 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001519#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard29e43992001-12-13 22:21:58 +00001520 else if ((!strcmp(argv[i], "-dropdtd")) ||
1521 (!strcmp(argv[i], "--dropdtd")))
1522 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001523 else if ((!strcmp(argv[i], "-insert")) ||
1524 (!strcmp(argv[i], "--insert")))
1525 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001526 else if ((!strcmp(argv[i], "-timing")) ||
1527 (!strcmp(argv[i], "--timing")))
1528 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001529 else if ((!strcmp(argv[i], "-auto")) ||
1530 (!strcmp(argv[i], "--auto")))
1531 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001532 else if ((!strcmp(argv[i], "-repeat")) ||
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001533 (!strcmp(argv[i], "--repeat"))) {
1534 if (repeat)
1535 repeat *= 10;
1536 else
1537 repeat = 100;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001538 }
1539#ifdef LIBXML_PUSH_ENABLED
1540 else if ((!strcmp(argv[i], "-push")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001541 (!strcmp(argv[i], "--push")))
1542 push++;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001543#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +00001544#ifdef HAVE_SYS_MMAN_H
1545 else if ((!strcmp(argv[i], "-memory")) ||
1546 (!strcmp(argv[i], "--memory")))
1547 memory++;
1548#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00001549 else if ((!strcmp(argv[i], "-testIO")) ||
1550 (!strcmp(argv[i], "--testIO")))
1551 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001552#ifdef LIBXML_XINCLUDE_ENABLED
1553 else if ((!strcmp(argv[i], "-xinclude")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001554 (!strcmp(argv[i], "--xinclude"))) {
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001555 xinclude++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001556 /* options |= XML_PARSE_XINCLUDE; */
1557 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001558#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001559#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001560#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001561 else if ((!strcmp(argv[i], "-compress")) ||
1562 (!strcmp(argv[i], "--compress"))) {
1563 compress++;
1564 xmlSetCompressMode(9);
1565 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001566#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001567#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001568 else if ((!strcmp(argv[i], "-nowarning")) ||
1569 (!strcmp(argv[i], "--nowarning"))) {
1570 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001571 xmlPedanticParserDefault(0);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001572 options |= XML_PARSE_NOWARNING;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001573 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001574 else if ((!strcmp(argv[i], "-pedantic")) ||
1575 (!strcmp(argv[i], "--pedantic"))) {
1576 xmlGetWarningsDefaultValue = 1;
1577 xmlPedanticParserDefault(1);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001578 options |= XML_PARSE_PEDANTIC;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001579 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001580#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001581 else if ((!strcmp(argv[i], "-debugent")) ||
1582 (!strcmp(argv[i], "--debugent"))) {
1583 debugent++;
1584 xmlParserDebugEntities = 1;
1585 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001586#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00001587#ifdef LIBXML_CATALOG_ENABLED
1588 else if ((!strcmp(argv[i], "-catalogs")) ||
1589 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001590 catalogs++;
1591 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
1592 (!strcmp(argv[i], "--nocatalogs"))) {
1593 nocatalogs++;
Daniel Veillard81418e32001-05-22 15:08:55 +00001594 }
1595#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00001596 else if ((!strcmp(argv[i], "-encode")) ||
1597 (!strcmp(argv[i], "--encode"))) {
1598 i++;
1599 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001600 /*
1601 * OK it's for testing purposes
1602 */
1603 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00001604 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001605 else if ((!strcmp(argv[i], "-noblanks")) ||
1606 (!strcmp(argv[i], "--noblanks"))) {
1607 noblanks++;
1608 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00001609 }
1610 else if ((!strcmp(argv[i], "-format")) ||
1611 (!strcmp(argv[i], "--format"))) {
1612 noblanks++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001613#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001614 format++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001615#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard90493a92001-08-14 14:12:47 +00001616 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001617 }
Daniel Veillard81273902003-09-30 00:43:48 +00001618#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001619 else if ((!strcmp(argv[i], "-stream")) ||
1620 (!strcmp(argv[i], "--stream"))) {
1621 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001622 }
Daniel Veillard81273902003-09-30 00:43:48 +00001623#endif /* LIBXML_READER_ENABLED */
1624#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001625 else if ((!strcmp(argv[i], "-sax1")) ||
1626 (!strcmp(argv[i], "--sax1"))) {
1627 sax1++;
1628 }
Daniel Veillard81273902003-09-30 00:43:48 +00001629#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001630 else if ((!strcmp(argv[i], "-chkregister")) ||
1631 (!strcmp(argv[i], "--chkregister"))) {
1632 chkregister++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001633#ifdef LIBXML_SCHEMAS_ENABLED
1634 } else if ((!strcmp(argv[i], "-relaxng")) ||
1635 (!strcmp(argv[i], "--relaxng"))) {
1636 i++;
1637 relaxng = argv[i];
1638 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001639 options |= XML_PARSE_NOENT;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001640 } else if ((!strcmp(argv[i], "-schema")) ||
1641 (!strcmp(argv[i], "--schema"))) {
1642 i++;
1643 schema = argv[i];
1644 noent++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001645#endif
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001646 } else if ((!strcmp(argv[i], "-nonet")) ||
1647 (!strcmp(argv[i], "--nonet"))) {
1648 xmlSetExternalEntityLoader(xmlNoNetExternalEntityLoader);
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001649 } else {
1650 fprintf(stderr, "Unknown option %s\n", argv[i]);
1651 usage(argv[0]);
1652 return(1);
1653 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001654 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001655
1656#ifdef LIBXML_CATALOG_ENABLED
1657 if (nocatalogs == 0) {
1658 if (catalogs) {
1659 const char *catal;
1660
1661 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00001662 if (catal != NULL) {
1663 xmlLoadCatalogs(catal);
1664 } else {
1665 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
1666 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001667 }
1668 }
1669#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001670
Daniel Veillard81273902003-09-30 00:43:48 +00001671#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001672 if (sax1)
1673 xmlSAXDefaultVersion(1);
1674 else
1675 xmlSAXDefaultVersion(2);
Daniel Veillard81273902003-09-30 00:43:48 +00001676#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00001677
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001678 if (chkregister) {
1679 xmlRegisterNodeDefault(registerNode);
1680 xmlDeregisterNodeDefault(deregisterNode);
1681 }
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001682
1683 indent = getenv("XMLLINT_INDENT");
1684 if(indent != NULL) {
1685 xmlTreeIndentString = indent;
1686 }
1687
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001688
Daniel Veillardd9bad132001-07-23 19:39:43 +00001689 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00001690 if (loaddtd != 0)
1691 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
1692 if (dtdattrs)
1693 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001694 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillard4432df22003-09-28 18:58:27 +00001695#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001696 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard4432df22003-09-28 18:58:27 +00001697#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001698 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001699 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001700 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001701 xmlGenericError(xmlGenericErrorContext,
1702 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
1703 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001704 "<html><head><title>%s output</title></head>\n",
1705 argv[0]);
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001706 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001707 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
1708 argv[0]);
1709 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001710
1711#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard81273902003-09-30 00:43:48 +00001712 if ((relaxng != NULL)
1713#ifdef LIBXML_READER_ENABLED
1714 && (stream == 0)
1715#endif /* LIBXML_READER_ENABLED */
1716 ) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001717 xmlRelaxNGParserCtxtPtr ctxt;
1718
Daniel Veillardce192eb2003-04-16 15:58:05 +00001719 /* forces loading the DTDs */
1720 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001721 options |= XML_PARSE_DTDLOAD;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001722 if (timing) {
1723 startTimer();
1724 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001725 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
1726 xmlRelaxNGSetParserErrors(ctxt,
1727 (xmlRelaxNGValidityErrorFunc) fprintf,
1728 (xmlRelaxNGValidityWarningFunc) fprintf,
1729 stderr);
1730 relaxngschemas = xmlRelaxNGParse(ctxt);
Daniel Veillardce192eb2003-04-16 15:58:05 +00001731 if (relaxngschemas == NULL) {
1732 xmlGenericError(xmlGenericErrorContext,
1733 "Relax-NG schema %s failed to compile\n", relaxng);
Daniel Veillard05d987b2003-10-08 11:54:57 +00001734 progresult = 5;
Daniel Veillardce192eb2003-04-16 15:58:05 +00001735 relaxng = NULL;
1736 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001737 xmlRelaxNGFreeParserCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001738 if (timing) {
1739 endTimer("Compiling the schemas");
1740 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001741 } else if ((schema != NULL) && (stream == 0)) {
1742 xmlSchemaParserCtxtPtr ctxt;
1743
1744 if (timing) {
1745 startTimer();
1746 }
1747 ctxt = xmlSchemaNewParserCtxt(schema);
1748 xmlSchemaSetParserErrors(ctxt,
1749 (xmlSchemaValidityErrorFunc) fprintf,
1750 (xmlSchemaValidityWarningFunc) fprintf,
1751 stderr);
1752 wxschemas = xmlSchemaParse(ctxt);
1753 if (wxschemas == NULL) {
1754 xmlGenericError(xmlGenericErrorContext,
1755 "WXS schema %s failed to compile\n", schema);
Daniel Veillard05d987b2003-10-08 11:54:57 +00001756 progresult = 5;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001757 schema = NULL;
1758 }
1759 xmlSchemaFreeParserCtxt(ctxt);
1760 if (timing) {
1761 endTimer("Compiling the schemas");
1762 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001763 }
1764#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001765 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00001766 if ((!strcmp(argv[i], "-encode")) ||
1767 (!strcmp(argv[i], "--encode"))) {
1768 i++;
1769 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001770 } else if ((!strcmp(argv[i], "-o")) ||
1771 (!strcmp(argv[i], "-output")) ||
1772 (!strcmp(argv[i], "--output"))) {
1773 i++;
1774 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00001775 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001776#ifdef LIBXML_VALID_ENABLED
Daniel Veillardcd429612000-10-11 15:57:05 +00001777 if ((!strcmp(argv[i], "-dtdvalid")) ||
1778 (!strcmp(argv[i], "--dtdvalid"))) {
1779 i++;
1780 continue;
1781 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00001782 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
1783 (!strcmp(argv[i], "--dtdvalidfpi"))) {
1784 i++;
1785 continue;
1786 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001787#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00001788 if ((!strcmp(argv[i], "-relaxng")) ||
1789 (!strcmp(argv[i], "--relaxng"))) {
1790 i++;
1791 continue;
1792 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001793 if ((!strcmp(argv[i], "-schema")) ||
1794 (!strcmp(argv[i], "--schema"))) {
1795 i++;
1796 continue;
1797 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001798 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00001799 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001800 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001801 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001802 if (repeat) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001803 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001804#ifdef LIBXML_READER_ENABLED
1805 xmlTextReaderPtr reader = NULL;
1806#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001807
1808 for (acount = 0;acount < repeat;acount++) {
Daniel Veillard81273902003-09-30 00:43:48 +00001809#ifdef LIBXML_READER_ENABLED
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001810 if (stream != 0) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001811 streamFile(argv[i]);
Daniel Veillard198c1bf2003-10-20 17:07:41 +00001812 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00001813#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001814 if (ctxt == NULL)
1815 ctxt = xmlNewParserCtxt();
1816 parseAndPrintFile(argv[i], ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00001817#ifdef LIBXML_READER_ENABLED
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001818 }
Daniel Veillard81273902003-09-30 00:43:48 +00001819#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001820 }
1821 if (ctxt != NULL)
1822 xmlFreeParserCtxt(ctxt);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001823 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00001824#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001825 if (stream != 0)
1826 streamFile(argv[i]);
1827 else
Daniel Veillard81273902003-09-30 00:43:48 +00001828#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001829 parseAndPrintFile(argv[i], NULL);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001830 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001831 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00001832 if ((timing) && (repeat)) {
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001833 endTimer("%d iterations", repeat);
Daniel Veillarda7866932001-12-04 13:14:44 +00001834 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001835 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001836 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001837 if (generate)
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001838 parseAndPrintFile(NULL, NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001839 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001840 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001841 }
Daniel Veillard845cce42002-01-09 11:51:37 +00001842 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001843 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001844 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001845#ifdef LIBXML_SCHEMAS_ENABLED
1846 if (relaxngschemas != NULL)
1847 xmlRelaxNGFree(relaxngschemas);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001848 if (wxschemas != NULL)
1849 xmlSchemaFree(wxschemas);
Daniel Veillard71531f32003-02-05 13:19:53 +00001850 xmlRelaxNGCleanupTypes();
1851#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001852 xmlCleanupParser();
1853 xmlMemoryDump();
1854
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001855 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001856}
Daniel Veillard88a172f2000-08-04 18:23:10 +00001857