blob: 81c2ea58f0ce12925490e92dc1a4b5c3c2dcd18d [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 Veillarde59494f2003-01-04 16:35:29 +0000602 xmlChar *name, *value;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000603
Daniel Veillarde59494f2003-01-04 16:35:29 +0000604 name = xmlTextReaderName(reader);
605 if (name == NULL)
606 name = xmlStrdup(BAD_CAST "--");
Daniel Veillard99737f52003-03-22 14:55:50 +0000607
Daniel Veillarde59494f2003-01-04 16:35:29 +0000608 value = xmlTextReaderValue(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 xmlFree(name);
617 if (value == NULL)
618 printf("\n");
619 else {
620 printf(" %s\n", value);
621 xmlFree(value);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000622 }
623}
624
625static void streamFile(char *filename) {
626 xmlTextReaderPtr reader;
627 int ret;
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000628#ifdef HAVE_SYS_MMAN_H
629 int fd = -1;
630 struct stat info;
631 const char *base = NULL;
632 xmlParserInputBufferPtr input = NULL;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000633
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000634 if (memory) {
635 if (stat(filename, &info) < 0)
636 return;
637 if ((fd = open(filename, O_RDONLY)) < 0)
638 return;
639 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
640 if (base == (void *) MAP_FAILED)
641 return;
642
Daniel Veillard53350552003-09-18 13:35:51 +0000643 input = xmlParserInputBufferCreateStatic((char *) base, info.st_size,
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000644 XML_CHAR_ENCODING_NONE);
645 reader = xmlNewTextReader(input, filename);
646 } else
647#endif
648 reader = xmlNewTextReaderFilename(filename);
649
Daniel Veillard7704fb12003-01-03 16:19:51 +0000650 if (reader != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +0000651#ifdef LIBXML_VALID_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +0000652 if (valid)
653 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +0000654 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000655#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce192eb2003-04-16 15:58:05 +0000656 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000657#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +0000658 if (relaxng != NULL) {
Daniel Veillard81514ba2003-09-16 23:17:26 +0000659 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000660 startTimer();
661 }
662 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
663 if (ret < 0) {
664 xmlGenericError(xmlGenericErrorContext,
665 "Relax-NG schema %s failed to compile\n", relaxng);
Daniel Veillard05d987b2003-10-08 11:54:57 +0000666 progresult = 5;
Daniel Veillardce192eb2003-04-16 15:58:05 +0000667 relaxng = NULL;
668 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000669 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000670 endTimer("Compiling the schemas");
671 }
672 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000673#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000674
675 /*
676 * Process all nodes in sequence
677 */
Daniel Veillard81514ba2003-09-16 23:17:26 +0000678 if ((timing) && (!repeat)) {
Daniel Veillardce192eb2003-04-16 15:58:05 +0000679 startTimer();
680 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000681 ret = xmlTextReaderRead(reader);
682 while (ret == 1) {
683 if (debug)
684 processNode(reader);
685 ret = xmlTextReaderRead(reader);
686 }
Daniel Veillard81514ba2003-09-16 23:17:26 +0000687 if ((timing) && (!repeat)) {
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000688#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +0000689 if ((valid) || (relaxng != NULL))
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000690#else
Daniel Veillard4432df22003-09-28 18:58:27 +0000691#ifdef LIBXML_VALID_ENABLED
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000692 if (valid)
Daniel Veillardce192eb2003-04-16 15:58:05 +0000693 endTimer("Parsing and validating");
694 else
Daniel Veillard4432df22003-09-28 18:58:27 +0000695#endif /* LIBXML_VALID_ENABLED */
696#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +0000697 endTimer("Parsing");
698 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000699
Daniel Veillard4432df22003-09-28 18:58:27 +0000700#ifdef LIBXML_VALID_ENABLED
Daniel Veillardf6bad792003-04-11 19:38:54 +0000701 if (valid) {
702 if (xmlTextReaderIsValid(reader) != 1) {
703 xmlGenericError(xmlGenericErrorContext,
704 "Document %s does not validate\n", filename);
705 progresult = 3;
706 }
707 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000708#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000709#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +0000710 if (relaxng != NULL) {
711 if (xmlTextReaderIsValid(reader) != 1) {
712 printf("%s fails to validate\n", filename);
713 progresult = 3;
714 } else {
715 printf("%s validates\n", filename);
716 }
717 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000718#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000719 /*
720 * Done, cleanup and status
721 */
722 xmlFreeTextReader(reader);
723 if (ret != 0) {
724 printf("%s : failed to parse\n", filename);
Daniel Veillardf6bad792003-04-11 19:38:54 +0000725 progresult = 1;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000726 }
727 } else {
728 fprintf(stderr, "Unable to open %s\n", filename);
Daniel Veillardf6bad792003-04-11 19:38:54 +0000729 progresult = 1;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000730 }
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +0000731#ifdef HAVE_SYS_MMAN_H
732 if (memory) {
733 xmlFreeParserInputBuffer(input);
734 munmap((char *) base, info.st_size);
735 close(fd);
736 }
737#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000738}
Daniel Veillard81273902003-09-30 00:43:48 +0000739#endif /* LIBXML_READER_ENABLED */
Daniel Veillard7704fb12003-01-03 16:19:51 +0000740
741/************************************************************************
742 * *
743 * Tree Test processing *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000744 * *
745 ************************************************************************/
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000746static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
Daniel Veillard652327a2003-09-29 18:02:38 +0000747 xmlDocPtr doc = NULL;
748#ifdef LIBXML_TREE_ENABLED
749 xmlDocPtr tmp;
750#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000751
Daniel Veillard48b2f892001-02-25 16:11:03 +0000752 if ((timing) && (!repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +0000753 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000754
755
Daniel Veillard652327a2003-09-29 18:02:38 +0000756#ifdef LIBXML_TREE_ENABLED
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000757 if (filename == NULL) {
758 if (generate) {
759 xmlNodePtr n;
760
761 doc = xmlNewDoc(BAD_CAST "1.0");
762 n = xmlNewNode(NULL, BAD_CAST "info");
763 xmlNodeSetContent(n, BAD_CAST "abc");
764 xmlDocSetRootElement(doc, n);
765 }
766 }
Daniel Veillard652327a2003-09-29 18:02:38 +0000767#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000768#ifdef LIBXML_HTML_ENABLED
Daniel Veillard73b013f2003-09-30 12:36:01 +0000769#ifdef LIBXML_PUSH_ENABLED
William M. Brack78637da2003-07-31 14:47:38 +0000770 else if ((html) && (push)) {
771 FILE *f;
772
773 f = fopen(filename, "r");
774 if (f != NULL) {
775 int res, size = 3;
776 char chars[4096];
777 htmlParserCtxtPtr ctxt;
778
779 /* if (repeat) */
780 size = 4096;
781 res = fread(chars, 1, 4, f);
782 if (res > 0) {
783 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
784 chars, res, filename, 0);
785 while ((res = fread(chars, 1, size, f)) > 0) {
786 htmlParseChunk(ctxt, chars, res, 0);
787 }
788 htmlParseChunk(ctxt, chars, 0, 1);
789 doc = ctxt->myDoc;
790 htmlFreeParserCtxt(ctxt);
791 }
792 fclose(f);
793 }
794 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000795#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000796 else if (html) {
Daniel Veillard9475a352003-09-26 12:47:50 +0000797 doc = htmlReadFile(filename, NULL, options);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000798 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000799#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000800 else {
Daniel Veillard73b013f2003-09-30 12:36:01 +0000801#ifdef LIBXML_PUSH_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000802 /*
803 * build an XML tree from a string;
804 */
805 if (push) {
806 FILE *f;
807
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000808 /* '-' Usually means stdin -<sven@zen.org> */
809 if ((filename[0] == '-') && (filename[1] == 0)) {
810 f = stdin;
811 } else {
812 f = fopen(filename, "r");
813 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000814 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +0000815 int ret;
Daniel Veillarda880b122003-04-21 21:36:41 +0000816 int res, size = 1024;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000817 char chars[1024];
818 xmlParserCtxtPtr ctxt;
819
Daniel Veillarda880b122003-04-21 21:36:41 +0000820 /* if (repeat) size = 1024; */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000821 res = fread(chars, 1, 4, f);
822 if (res > 0) {
823 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
824 chars, res, filename);
825 while ((res = fread(chars, 1, size, f)) > 0) {
826 xmlParseChunk(ctxt, chars, res, 0);
827 }
828 xmlParseChunk(ctxt, chars, 0, 1);
829 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +0000830 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000831 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +0000832 if (!ret) {
833 xmlFreeDoc(doc);
834 doc = NULL;
835 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000836 }
837 }
Daniel Veillard73b013f2003-09-30 12:36:01 +0000838 } else
839#endif /* LIBXML_PUSH_ENABLED */
840 if (testIO) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000841 if ((filename[0] == '-') && (filename[1] == 0)) {
Daniel Veillard60942de2003-09-25 21:05:58 +0000842 doc = xmlReadFd(0, NULL, NULL, options);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000843 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000844 FILE *f;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000845
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000846 f = fopen(filename, "r");
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000847 if (f != NULL) {
848 if (rectxt == NULL)
849 doc = xmlReadIO((xmlInputReadCallback) myRead,
850 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +0000851 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000852 else
853 doc = xmlCtxtReadIO(rectxt,
854 (xmlInputReadCallback) myRead,
855 (xmlInputCloseCallback) myClose, f,
Daniel Veillard60942de2003-09-25 21:05:58 +0000856 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000857 } else
Daniel Veillard5e873c42000-04-12 13:27:38 +0000858 doc = NULL;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000859 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000860 } else if (htmlout) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000861 xmlParserCtxtPtr ctxt;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000862
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000863 if (rectxt == NULL)
864 ctxt = xmlNewParserCtxt();
865 else
866 ctxt = rectxt;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000867 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000868 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000869 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000870 ctxt->sax->error = xmlHTMLError;
871 ctxt->sax->warning = xmlHTMLWarning;
872 ctxt->vctxt.error = xmlHTMLValidityError;
873 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000874
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000875 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000876
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000877 if (rectxt == NULL)
878 xmlFreeParserCtxt(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000879 }
Daniel Veillard46e370e2000-07-21 20:32:03 +0000880#ifdef HAVE_SYS_MMAN_H
881 } else if (memory) {
882 int fd;
883 struct stat info;
884 const char *base;
885 if (stat(filename, &info) < 0)
886 return;
887 if ((fd = open(filename, O_RDONLY)) < 0)
888 return;
889 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +0000890 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +0000891 return;
892
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000893 if (rectxt == NULL)
Daniel Veillard60942de2003-09-25 21:05:58 +0000894 doc = xmlReadMemory((char *) base, info.st_size,
895 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000896 else
Daniel Veillard60942de2003-09-25 21:05:58 +0000897 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
898 filename, NULL, options);
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000899
Daniel Veillard46e370e2000-07-21 20:32:03 +0000900 munmap((char *) base, info.st_size);
901#endif
Daniel Veillard4432df22003-09-28 18:58:27 +0000902#ifdef LIBXML_VALID_ENABLED
Daniel Veillardea7751d2002-12-20 00:16:24 +0000903 } else if (valid) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000904 xmlParserCtxtPtr ctxt = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000905
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000906 if (rectxt == NULL)
907 ctxt = xmlNewParserCtxt();
908 else
909 ctxt = rectxt;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000910 if (ctxt == NULL) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000911 doc = NULL;
Daniel Veillardea7751d2002-12-20 00:16:24 +0000912 } else {
Daniel Veillard16fa96c2003-09-23 21:50:54 +0000913 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
914
915 if (ctxt->valid == 0)
916 progresult = 4;
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000917 if (rectxt == NULL)
918 xmlFreeParserCtxt(ctxt);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000919 }
Daniel Veillard4432df22003-09-28 18:58:27 +0000920#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardea7751d2002-12-20 00:16:24 +0000921 } else {
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000922 if (rectxt != NULL)
923 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
924 else
925 doc = xmlReadFile(filename, NULL, options);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000926 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000927 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000928
Daniel Veillard88a172f2000-08-04 18:23:10 +0000929 /*
930 * If we don't have a document we might as well give up. Do we
931 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000932 if (doc == NULL) {
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000933 progresult = 1;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000934 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000935 }
936
Daniel Veillard48b2f892001-02-25 16:11:03 +0000937 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000938 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000939 }
940
Daniel Veillard29e43992001-12-13 22:21:58 +0000941 /*
942 * Remove DOCTYPE nodes
943 */
944 if (dropdtd) {
945 xmlDtdPtr dtd;
946
947 dtd = xmlGetIntSubset(doc);
948 if (dtd != NULL) {
949 xmlUnlinkNode((xmlNodePtr)dtd);
950 xmlFreeDtd(dtd);
951 }
952 }
953
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000954#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +0000955 if (xinclude) {
956 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000957 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000958 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000959 xmlXIncludeProcess(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +0000960 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000961 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000962 }
963 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000964#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +0000965
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000966#ifdef LIBXML_DEBUG_ENABLED
967 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000968 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000969 */
970 if (shell)
971 xmlShell(doc, filename, xmlShellReadline, stdout);
972#endif
973
Daniel Veillard652327a2003-09-29 18:02:38 +0000974#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000975 /*
976 * test intermediate copy if needed.
977 */
978 if (copy) {
979 tmp = doc;
980 doc = xmlCopyDoc(doc, 1);
981 xmlFreeDoc(tmp);
982 }
Daniel Veillard652327a2003-09-29 18:02:38 +0000983#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000984
Daniel Veillard4432df22003-09-28 18:58:27 +0000985#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000986 if ((insert) && (!html)) {
987 const xmlChar* list[256];
988 int nb, i;
989 xmlNodePtr node;
990
991 if (doc->children != NULL) {
992 node = doc->children;
993 while ((node != NULL) && (node->last == NULL)) node = node->next;
994 if (node != NULL) {
995 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
996 if (nb < 0) {
997 printf("could not get valid list of elements\n");
998 } else if (nb == 0) {
MDT 2001 John Fleckbbb9e432001-09-24 03:08:43 +0000999 printf("No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001000 } else {
MDT 2001 John Fleckbbb9e432001-09-24 03:08:43 +00001001 printf("%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001002 nb);
1003 for (i = 0;i < nb;i++) {
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001004 printf("%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001005 }
1006 }
1007 }
1008 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001009 }else
1010#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001011#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001012 if (noout == 0) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001013 /*
1014 * print it.
1015 */
1016#ifdef LIBXML_DEBUG_ENABLED
1017 if (!debug) {
1018#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +00001019 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001020 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001021 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001022#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001023 if (memory) {
1024 xmlChar *result;
1025 int len;
1026
1027 if (encoding != NULL) {
Daniel Veillardd536f702001-11-08 17:32:47 +00001028 if ( format ) {
1029 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
1030 } else {
1031 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
1032 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001033 } else {
Daniel Veillard90493a92001-08-14 14:12:47 +00001034 if (format)
1035 xmlDocDumpFormatMemory(doc, &result, &len, 1);
1036 else
1037 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001038 }
1039 if (result == NULL) {
1040 fprintf(stderr, "Failed to save\n");
1041 } else {
1042 write(1, result, len);
1043 xmlFree(result);
1044 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +00001045 } else
1046#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001047 if (compress) {
1048 xmlSaveFile(output ? output : "-", doc);
1049 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001050 else if (encoding != NULL) {
1051 if ( format ) {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001052 xmlSaveFormatFileEnc(output ? output : "-", doc, encoding, 1);
1053 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001054 else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001055 xmlSaveFileEnc(output ? output : "-", doc, encoding);
Daniel Veillardd536f702001-11-08 17:32:47 +00001056 }
1057 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001058 else if (format) {
1059 xmlSaveFormatFile(output ? output : "-", doc, 1);
1060 }
1061 else {
1062 FILE *out;
1063 if (output == NULL)
1064 out = stdout;
1065 else {
1066 out = fopen(output,"wb");
1067 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001068 if (out != NULL) {
Daniel Veillard828ce832003-10-08 19:19:10 +00001069 if (xmlDocDump(out, doc) < 0)
1070 progresult = 6;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001071
Daniel Veillard05d987b2003-10-08 11:54:57 +00001072 if (output != NULL)
1073 fclose(out);
1074 } else {
1075 fprintf(stderr, "failed to open %s\n", output);
1076 progresult = 6;
1077 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001078 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001079 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001080 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001081 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001082#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001083 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001084 FILE *out;
1085 if (output == NULL)
1086 out = stdout;
1087 else {
1088 out = fopen(output,"wb");
1089 }
Daniel Veillard05d987b2003-10-08 11:54:57 +00001090 if (out != NULL) {
1091 xmlDebugDumpDocument(out, doc);
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001092
Daniel Veillard05d987b2003-10-08 11:54:57 +00001093 if (output != NULL)
1094 fclose(out);
1095 } else {
1096 fprintf(stderr, "failed to open %s\n", output);
1097 progresult = 6;
1098 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001099 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001100#endif
1101 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001102#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001103
Daniel Veillard4432df22003-09-28 18:58:27 +00001104#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001105 /*
1106 * A posteriori validation test
1107 */
Daniel Veillard66f68e72003-08-18 16:39:51 +00001108 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
Daniel Veillardcd429612000-10-11 15:57:05 +00001109 xmlDtdPtr dtd;
1110
Daniel Veillard48b2f892001-02-25 16:11:03 +00001111 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001112 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001113 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00001114 if (dtdvalid != NULL)
1115 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
1116 else
1117 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001118 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001119 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001120 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001121 if (dtd == NULL) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001122 if (dtdvalid != NULL)
1123 xmlGenericError(xmlGenericErrorContext,
1124 "Could not parse DTD %s\n", dtdvalid);
1125 else
1126 xmlGenericError(xmlGenericErrorContext,
1127 "Could not parse DTD %s\n", dtdvalidfpi);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001128 progresult = 2;
Daniel Veillardcd429612000-10-11 15:57:05 +00001129 } else {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001130 xmlValidCtxtPtr cvp;
1131
1132 if ((cvp = xmlNewValidCtxt()) == NULL) {
1133 xmlGenericError(xmlGenericErrorContext,
1134 "Couldn't allocate validation context\n");
1135 exit(-1);
1136 }
1137 cvp->userData = (void *) stderr;
1138 cvp->error = (xmlValidityErrorFunc) fprintf;
1139 cvp->warning = (xmlValidityWarningFunc) fprintf;
1140
Daniel Veillard48b2f892001-02-25 16:11:03 +00001141 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001142 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001143 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001144 if (!xmlValidateDtd(cvp, doc, dtd)) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001145 if (dtdvalid != NULL)
1146 xmlGenericError(xmlGenericErrorContext,
1147 "Document %s does not validate against %s\n",
1148 filename, dtdvalid);
1149 else
1150 xmlGenericError(xmlGenericErrorContext,
1151 "Document %s does not validate against %s\n",
1152 filename, dtdvalidfpi);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001153 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +00001154 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001155 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001156 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001157 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001158 xmlFreeValidCtxt(cvp);
Daniel Veillardcd429612000-10-11 15:57:05 +00001159 xmlFreeDtd(dtd);
1160 }
1161 } else if (postvalid) {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001162 xmlValidCtxtPtr cvp;
1163
1164 if ((cvp = xmlNewValidCtxt()) == NULL) {
1165 xmlGenericError(xmlGenericErrorContext,
1166 "Couldn't allocate validation context\n");
1167 exit(-1);
1168 }
1169
Daniel Veillard48b2f892001-02-25 16:11:03 +00001170 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001171 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001172 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001173 cvp->userData = (void *) stderr;
1174 cvp->error = (xmlValidityErrorFunc) fprintf;
1175 cvp->warning = (xmlValidityWarningFunc) fprintf;
1176 if (!xmlValidateDocument(cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001177 xmlGenericError(xmlGenericErrorContext,
1178 "Document %s does not validate\n", filename);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001179 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +00001180 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001181 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001182 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001183 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001184 xmlFreeValidCtxt(cvp);
Daniel Veillard4432df22003-09-28 18:58:27 +00001185 }
1186#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00001187#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard4432df22003-09-28 18:58:27 +00001188 if (relaxngschemas != NULL) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001189 xmlRelaxNGValidCtxtPtr ctxt;
1190 int ret;
1191
Daniel Veillard42f12e92003-03-07 18:32:59 +00001192 if ((timing) && (!repeat)) {
1193 startTimer();
1194 }
1195
Daniel Veillard71531f32003-02-05 13:19:53 +00001196 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
1197 xmlRelaxNGSetValidErrors(ctxt,
1198 (xmlRelaxNGValidityErrorFunc) fprintf,
1199 (xmlRelaxNGValidityWarningFunc) fprintf,
1200 stderr);
1201 ret = xmlRelaxNGValidateDoc(ctxt, doc);
1202 if (ret == 0) {
1203 printf("%s validates\n", filename);
1204 } else if (ret > 0) {
1205 printf("%s fails to validate\n", filename);
1206 } else {
1207 printf("%s validation generated an internal error\n",
1208 filename);
1209 }
1210 xmlRelaxNGFreeValidCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001211 if ((timing) && (!repeat)) {
1212 endTimer("Validating");
1213 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001214 } else if (wxschemas != NULL) {
1215 xmlSchemaValidCtxtPtr ctxt;
1216 int ret;
1217
1218 if ((timing) && (!repeat)) {
1219 startTimer();
1220 }
1221
1222 ctxt = xmlSchemaNewValidCtxt(wxschemas);
1223 xmlSchemaSetValidErrors(ctxt,
1224 (xmlSchemaValidityErrorFunc) fprintf,
1225 (xmlSchemaValidityWarningFunc) fprintf,
1226 stderr);
1227 ret = xmlSchemaValidateDoc(ctxt, doc);
1228 if (ret == 0) {
1229 printf("%s validates\n", filename);
1230 } else if (ret > 0) {
1231 printf("%s fails to validate\n", filename);
1232 } else {
1233 printf("%s validation generated an internal error\n",
1234 filename);
1235 }
1236 xmlSchemaFreeValidCtxt(ctxt);
1237 if ((timing) && (!repeat)) {
1238 endTimer("Validating");
1239 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001240 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001241#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001242
1243#ifdef LIBXML_DEBUG_ENABLED
1244 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001245 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001246#endif
1247
1248 /*
1249 * free it.
1250 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00001251 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001252 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001253 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001254 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001255 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001256 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001257 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001258}
1259
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001260/************************************************************************
1261 * *
1262 * Usage and Main *
1263 * *
1264 ************************************************************************/
1265
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001266static void showVersion(const char *name) {
1267 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
1268 fprintf(stderr, " compiled with: ");
Daniel Veillard4432df22003-09-28 18:58:27 +00001269#ifdef LIBXML_VALID_ENABLED
1270 fprintf(stderr, "DTDValid ");
1271#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001272#ifdef LIBXML_FTP_ENABLED
1273 fprintf(stderr, "FTP ");
1274#endif
1275#ifdef LIBXML_HTTP_ENABLED
1276 fprintf(stderr, "HTTP ");
1277#endif
1278#ifdef LIBXML_HTML_ENABLED
1279 fprintf(stderr, "HTML ");
1280#endif
1281#ifdef LIBXML_C14N_ENABLED
1282 fprintf(stderr, "C14N ");
1283#endif
1284#ifdef LIBXML_CATALOG_ENABLED
1285 fprintf(stderr, "Catalog ");
1286#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001287#ifdef LIBXML_XPATH_ENABLED
1288 fprintf(stderr, "XPath ");
1289#endif
1290#ifdef LIBXML_XPTR_ENABLED
1291 fprintf(stderr, "XPointer ");
1292#endif
1293#ifdef LIBXML_XINCLUDE_ENABLED
1294 fprintf(stderr, "XInclude ");
1295#endif
1296#ifdef LIBXML_ICONV_ENABLED
1297 fprintf(stderr, "Iconv ");
1298#endif
1299#ifdef DEBUG_MEMORY_LOCATION
1300 fprintf(stderr, "MemDebug ");
1301#endif
1302#ifdef LIBXML_UNICODE_ENABLED
1303 fprintf(stderr, "Unicode ");
1304#endif
1305#ifdef LIBXML_REGEXP_ENABLED
1306 fprintf(stderr, "Regexps ");
1307#endif
1308#ifdef LIBXML_AUTOMATA_ENABLED
1309 fprintf(stderr, "Automata ");
1310#endif
1311#ifdef LIBXML_SCHEMAS_ENABLED
1312 fprintf(stderr, "Schemas ");
1313#endif
1314 fprintf(stderr, "\n");
1315}
1316
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001317static void usage(const char *name) {
1318 printf("Usage : %s [options] XMLfiles ...\n", name);
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001319#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001320 printf("\tParse the XML files and output the result of the parsing\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001321#else
1322 printf("\tParse the XML files\n");
1323#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001324 printf("\t--version : display the version of the XML library used\n");
1325#ifdef LIBXML_DEBUG_ENABLED
1326 printf("\t--debug : dump a debug tree of the in-memory document\n");
1327 printf("\t--shell : run a navigating shell\n");
1328 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00001329#else
Daniel Veillard81273902003-09-30 00:43:48 +00001330#ifdef LIBXML_READER_ENABLED
Daniel Veillard8326e732003-01-07 00:19:07 +00001331 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001332#endif /* LIBXML_READER_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001333#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001334#ifdef LIBXML_TREE_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001335 printf("\t--copy : used to test the internal copy implementation\n");
Daniel Veillard652327a2003-09-29 18:02:38 +00001336#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001337 printf("\t--recover : output what was parsable on broken XML documents\n");
1338 printf("\t--noent : substitute entity references by their value\n");
1339 printf("\t--noout : don't output the result tree\n");
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001340 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001341 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001342 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001343#ifdef LIBXML_VALID_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001344 printf("\t--valid : validate the document in addition to std well-formed check\n");
1345 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
1346 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
Daniel Veillard66f68e72003-08-18 16:39:51 +00001347 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
Daniel Veillard4432df22003-09-28 18:58:27 +00001348#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001349 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001350 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001351 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
1352 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001353#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001354#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001355 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001356#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001357#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001358#ifdef LIBXML_HTML_ENABLED
1359 printf("\t--html : use the HTML parser\n");
1360#endif
Daniel Veillard73b013f2003-09-30 12:36:01 +00001361#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001362 printf("\t--push : use the push mode of the parser\n");
Daniel Veillard73b013f2003-09-30 12:36:01 +00001363#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001364#ifdef HAVE_SYS_MMAN_H
1365 printf("\t--memory : parse from memory\n");
1366#endif
1367 printf("\t--nowarning : do not emit warnings from parser/validator\n");
1368 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001369 printf("\t--nocdata : replace cdata section with text nodes\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001370#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001371 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001372 printf("\t--encode encoding : output in the given encoding\n");
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001373 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
1374#endif /* LIBXML_OUTPUT_ENABLED */
1375 printf("\t--nsclean : remove redundant namespace declarations\n");
1376 printf("\t--testIO : test user I/O support\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001377#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00001378 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
1379 printf("\t otherwise XML Catalogs starting from \n");
Daniel Veillard3be27512003-01-26 19:49:04 +00001380 printf("\t " XML_XML_DEFAULT_CATALOG " are activated by default\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001381 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001382#endif
1383 printf("\t--auto : generate a small doc on the fly\n");
1384#ifdef LIBXML_XINCLUDE_ENABLED
1385 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001386#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001387 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00001388 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard81273902003-09-30 00:43:48 +00001389#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001390 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard81273902003-09-30 00:43:48 +00001391#endif /* LIBXML_READER_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001392 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillardef4d3bc2003-02-07 12:38:22 +00001393#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard71531f32003-02-05 13:19:53 +00001394 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001395 printf("\t--schema schema : do validation against the WXS schema\n");
Daniel Veillard71531f32003-02-05 13:19:53 +00001396#endif
Daniel Veillarda42f25f2002-01-25 14:15:40 +00001397 printf("\nLibxml project home page: http://xmlsoft.org/\n");
1398 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001399}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001400
1401static void registerNode(xmlNodePtr node)
1402{
1403 node->_private = malloc(sizeof(long));
1404 *(long*)node->_private = (long) 0x81726354;
1405}
1406
1407static void deregisterNode(xmlNodePtr node)
1408{
1409 assert(node->_private != NULL);
1410 assert(*(long*)node->_private == (long) 0x81726354);
1411 free(node->_private);
1412}
1413
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001414int
1415main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001416 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001417 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00001418 int version = 0;
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001419 const char* indent;
1420
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001421 if (argc <= 1) {
1422 usage(argv[0]);
1423 return(1);
1424 }
Daniel Veillardbe803962000-06-28 23:40:59 +00001425 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001426 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001427 if (!strcmp(argv[i], "-"))
1428 break;
1429
1430 if (argv[i][0] != '-')
1431 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001432 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
1433 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00001434 else
1435#ifdef LIBXML_DEBUG_ENABLED
1436 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001437 (!strcmp(argv[i], "--shell"))) {
1438 shell++;
1439 noout = 1;
1440 } else
1441#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00001442#ifdef LIBXML_TREE_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001443 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
1444 copy++;
Daniel Veillard652327a2003-09-29 18:02:38 +00001445 else
1446#endif /* LIBXML_TREE_ENABLED */
1447 if ((!strcmp(argv[i], "-recover")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001448 (!strcmp(argv[i], "--recover"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001449 recovery++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001450 options |= XML_PARSE_RECOVER;
1451 } else if ((!strcmp(argv[i], "-noent")) ||
1452 (!strcmp(argv[i], "--noent"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001453 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001454 options |= XML_PARSE_NOENT;
Daniel Veillarddca8cc72003-09-26 13:53:14 +00001455 } else if ((!strcmp(argv[i], "-nsclean")) ||
1456 (!strcmp(argv[i], "--nsclean"))) {
1457 options |= XML_PARSE_NSCLEAN;
1458 } else if ((!strcmp(argv[i], "-nocdata")) ||
1459 (!strcmp(argv[i], "--nocdata"))) {
1460 options |= XML_PARSE_NOCDATA;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001461 } else if ((!strcmp(argv[i], "-nodict")) ||
1462 (!strcmp(argv[i], "--nodict"))) {
1463 options |= XML_PARSE_NODICT;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001464 } else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00001465 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001466 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00001467 version = 1;
1468 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001469 (!strcmp(argv[i], "--noout")))
1470 noout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001471#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001472 else if ((!strcmp(argv[i], "-o")) ||
1473 (!strcmp(argv[i], "-output")) ||
1474 (!strcmp(argv[i], "--output"))) {
1475 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00001476 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001477 }
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001478#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001479 else if ((!strcmp(argv[i], "-htmlout")) ||
1480 (!strcmp(argv[i], "--htmlout")))
1481 htmlout++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001482 else if ((!strcmp(argv[i], "-nowrap")) ||
1483 (!strcmp(argv[i], "--nowrap")))
1484 nowrap++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001485#ifdef LIBXML_HTML_ENABLED
1486 else if ((!strcmp(argv[i], "-html")) ||
1487 (!strcmp(argv[i], "--html"))) {
1488 html++;
1489 }
1490#endif /* LIBXML_HTML_ENABLED */
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001491 else if ((!strcmp(argv[i], "-loaddtd")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001492 (!strcmp(argv[i], "--loaddtd"))) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001493 loaddtd++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001494 options |= XML_PARSE_DTDLOAD;
1495 } else if ((!strcmp(argv[i], "-dtdattr")) ||
Daniel Veillard48da9102001-08-07 01:10:10 +00001496 (!strcmp(argv[i], "--dtdattr"))) {
1497 loaddtd++;
1498 dtdattrs++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001499 options |= XML_PARSE_DTDATTR;
Daniel Veillard4432df22003-09-28 18:58:27 +00001500 }
1501#ifdef LIBXML_VALID_ENABLED
1502 else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001503 (!strcmp(argv[i], "--valid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001504 valid++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001505 options |= XML_PARSE_DTDVALID;
1506 } else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001507 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001508 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001509 loaddtd++;
1510 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00001511 (!strcmp(argv[i], "--dtdvalid"))) {
1512 i++;
1513 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001514 loaddtd++;
Daniel Veillard66f68e72003-08-18 16:39:51 +00001515 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
1516 (!strcmp(argv[i], "--dtdvalidfpi"))) {
1517 i++;
1518 dtdvalidfpi = argv[i];
1519 loaddtd++;
Daniel Veillardcd429612000-10-11 15:57:05 +00001520 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001521#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard29e43992001-12-13 22:21:58 +00001522 else if ((!strcmp(argv[i], "-dropdtd")) ||
1523 (!strcmp(argv[i], "--dropdtd")))
1524 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001525 else if ((!strcmp(argv[i], "-insert")) ||
1526 (!strcmp(argv[i], "--insert")))
1527 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001528 else if ((!strcmp(argv[i], "-timing")) ||
1529 (!strcmp(argv[i], "--timing")))
1530 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001531 else if ((!strcmp(argv[i], "-auto")) ||
1532 (!strcmp(argv[i], "--auto")))
1533 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001534 else if ((!strcmp(argv[i], "-repeat")) ||
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001535 (!strcmp(argv[i], "--repeat"))) {
1536 if (repeat)
1537 repeat *= 10;
1538 else
1539 repeat = 100;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001540 }
1541#ifdef LIBXML_PUSH_ENABLED
1542 else if ((!strcmp(argv[i], "-push")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001543 (!strcmp(argv[i], "--push")))
1544 push++;
Daniel Veillard73b013f2003-09-30 12:36:01 +00001545#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillard46e370e2000-07-21 20:32:03 +00001546#ifdef HAVE_SYS_MMAN_H
1547 else if ((!strcmp(argv[i], "-memory")) ||
1548 (!strcmp(argv[i], "--memory")))
1549 memory++;
1550#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00001551 else if ((!strcmp(argv[i], "-testIO")) ||
1552 (!strcmp(argv[i], "--testIO")))
1553 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001554#ifdef LIBXML_XINCLUDE_ENABLED
1555 else if ((!strcmp(argv[i], "-xinclude")) ||
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001556 (!strcmp(argv[i], "--xinclude"))) {
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001557 xinclude++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001558 /* options |= XML_PARSE_XINCLUDE; */
1559 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001560#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001561#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001562#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001563 else if ((!strcmp(argv[i], "-compress")) ||
1564 (!strcmp(argv[i], "--compress"))) {
1565 compress++;
1566 xmlSetCompressMode(9);
1567 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001568#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001569#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001570 else if ((!strcmp(argv[i], "-nowarning")) ||
1571 (!strcmp(argv[i], "--nowarning"))) {
1572 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001573 xmlPedanticParserDefault(0);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001574 options |= XML_PARSE_NOWARNING;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001575 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001576 else if ((!strcmp(argv[i], "-pedantic")) ||
1577 (!strcmp(argv[i], "--pedantic"))) {
1578 xmlGetWarningsDefaultValue = 1;
1579 xmlPedanticParserDefault(1);
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001580 options |= XML_PARSE_PEDANTIC;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001581 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001582#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001583 else if ((!strcmp(argv[i], "-debugent")) ||
1584 (!strcmp(argv[i], "--debugent"))) {
1585 debugent++;
1586 xmlParserDebugEntities = 1;
1587 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001588#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00001589#ifdef LIBXML_CATALOG_ENABLED
1590 else if ((!strcmp(argv[i], "-catalogs")) ||
1591 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001592 catalogs++;
1593 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
1594 (!strcmp(argv[i], "--nocatalogs"))) {
1595 nocatalogs++;
Daniel Veillard81418e32001-05-22 15:08:55 +00001596 }
1597#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00001598 else if ((!strcmp(argv[i], "-encode")) ||
1599 (!strcmp(argv[i], "--encode"))) {
1600 i++;
1601 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001602 /*
1603 * OK it's for testing purposes
1604 */
1605 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00001606 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001607 else if ((!strcmp(argv[i], "-noblanks")) ||
1608 (!strcmp(argv[i], "--noblanks"))) {
1609 noblanks++;
1610 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00001611 }
1612 else if ((!strcmp(argv[i], "-format")) ||
1613 (!strcmp(argv[i], "--format"))) {
1614 noblanks++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001615#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard90493a92001-08-14 14:12:47 +00001616 format++;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001617#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard90493a92001-08-14 14:12:47 +00001618 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001619 }
Daniel Veillard81273902003-09-30 00:43:48 +00001620#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001621 else if ((!strcmp(argv[i], "-stream")) ||
1622 (!strcmp(argv[i], "--stream"))) {
1623 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001624 }
Daniel Veillard81273902003-09-30 00:43:48 +00001625#endif /* LIBXML_READER_ENABLED */
1626#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001627 else if ((!strcmp(argv[i], "-sax1")) ||
1628 (!strcmp(argv[i], "--sax1"))) {
1629 sax1++;
1630 }
Daniel Veillard81273902003-09-30 00:43:48 +00001631#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001632 else if ((!strcmp(argv[i], "-chkregister")) ||
1633 (!strcmp(argv[i], "--chkregister"))) {
1634 chkregister++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001635#ifdef LIBXML_SCHEMAS_ENABLED
1636 } else if ((!strcmp(argv[i], "-relaxng")) ||
1637 (!strcmp(argv[i], "--relaxng"))) {
1638 i++;
1639 relaxng = argv[i];
1640 noent++;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001641 options |= XML_PARSE_NOENT;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001642 } else if ((!strcmp(argv[i], "-schema")) ||
1643 (!strcmp(argv[i], "--schema"))) {
1644 i++;
1645 schema = argv[i];
1646 noent++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001647#endif
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001648 } else if ((!strcmp(argv[i], "-nonet")) ||
1649 (!strcmp(argv[i], "--nonet"))) {
1650 xmlSetExternalEntityLoader(xmlNoNetExternalEntityLoader);
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001651 } else {
1652 fprintf(stderr, "Unknown option %s\n", argv[i]);
1653 usage(argv[0]);
1654 return(1);
1655 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001656 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001657
1658#ifdef LIBXML_CATALOG_ENABLED
1659 if (nocatalogs == 0) {
1660 if (catalogs) {
1661 const char *catal;
1662
1663 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00001664 if (catal != NULL) {
1665 xmlLoadCatalogs(catal);
1666 } else {
1667 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
1668 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001669 }
1670 }
1671#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001672
Daniel Veillard81273902003-09-30 00:43:48 +00001673#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard07cb8222003-09-10 10:51:05 +00001674 if (sax1)
1675 xmlSAXDefaultVersion(1);
1676 else
1677 xmlSAXDefaultVersion(2);
Daniel Veillard81273902003-09-30 00:43:48 +00001678#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard07cb8222003-09-10 10:51:05 +00001679
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001680 if (chkregister) {
1681 xmlRegisterNodeDefault(registerNode);
1682 xmlDeregisterNodeDefault(deregisterNode);
1683 }
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001684
1685 indent = getenv("XMLLINT_INDENT");
1686 if(indent != NULL) {
1687 xmlTreeIndentString = indent;
1688 }
1689
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001690
Daniel Veillardd9bad132001-07-23 19:39:43 +00001691 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00001692 if (loaddtd != 0)
1693 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
1694 if (dtdattrs)
1695 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001696 if (noent != 0) xmlSubstituteEntitiesDefault(1);
Daniel Veillard4432df22003-09-28 18:58:27 +00001697#ifdef LIBXML_VALID_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001698 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
Daniel Veillard4432df22003-09-28 18:58:27 +00001699#endif /* LIBXML_VALID_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001700 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001701 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001702 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001703 xmlGenericError(xmlGenericErrorContext,
1704 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
1705 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001706 "<html><head><title>%s output</title></head>\n",
1707 argv[0]);
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001708 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001709 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
1710 argv[0]);
1711 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001712
1713#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard81273902003-09-30 00:43:48 +00001714 if ((relaxng != NULL)
1715#ifdef LIBXML_READER_ENABLED
1716 && (stream == 0)
1717#endif /* LIBXML_READER_ENABLED */
1718 ) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001719 xmlRelaxNGParserCtxtPtr ctxt;
1720
Daniel Veillardce192eb2003-04-16 15:58:05 +00001721 /* forces loading the DTDs */
1722 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +00001723 options |= XML_PARSE_DTDLOAD;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001724 if (timing) {
1725 startTimer();
1726 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001727 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
1728 xmlRelaxNGSetParserErrors(ctxt,
1729 (xmlRelaxNGValidityErrorFunc) fprintf,
1730 (xmlRelaxNGValidityWarningFunc) fprintf,
1731 stderr);
1732 relaxngschemas = xmlRelaxNGParse(ctxt);
Daniel Veillardce192eb2003-04-16 15:58:05 +00001733 if (relaxngschemas == NULL) {
1734 xmlGenericError(xmlGenericErrorContext,
1735 "Relax-NG schema %s failed to compile\n", relaxng);
Daniel Veillard05d987b2003-10-08 11:54:57 +00001736 progresult = 5;
Daniel Veillardce192eb2003-04-16 15:58:05 +00001737 relaxng = NULL;
1738 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001739 xmlRelaxNGFreeParserCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001740 if (timing) {
1741 endTimer("Compiling the schemas");
1742 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001743 } else if ((schema != NULL) && (stream == 0)) {
1744 xmlSchemaParserCtxtPtr ctxt;
1745
1746 if (timing) {
1747 startTimer();
1748 }
1749 ctxt = xmlSchemaNewParserCtxt(schema);
1750 xmlSchemaSetParserErrors(ctxt,
1751 (xmlSchemaValidityErrorFunc) fprintf,
1752 (xmlSchemaValidityWarningFunc) fprintf,
1753 stderr);
1754 wxschemas = xmlSchemaParse(ctxt);
1755 if (wxschemas == NULL) {
1756 xmlGenericError(xmlGenericErrorContext,
1757 "WXS schema %s failed to compile\n", schema);
Daniel Veillard05d987b2003-10-08 11:54:57 +00001758 progresult = 5;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001759 schema = NULL;
1760 }
1761 xmlSchemaFreeParserCtxt(ctxt);
1762 if (timing) {
1763 endTimer("Compiling the schemas");
1764 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001765 }
1766#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001767 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00001768 if ((!strcmp(argv[i], "-encode")) ||
1769 (!strcmp(argv[i], "--encode"))) {
1770 i++;
1771 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001772 } else if ((!strcmp(argv[i], "-o")) ||
1773 (!strcmp(argv[i], "-output")) ||
1774 (!strcmp(argv[i], "--output"))) {
1775 i++;
1776 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00001777 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001778#ifdef LIBXML_VALID_ENABLED
Daniel Veillardcd429612000-10-11 15:57:05 +00001779 if ((!strcmp(argv[i], "-dtdvalid")) ||
1780 (!strcmp(argv[i], "--dtdvalid"))) {
1781 i++;
1782 continue;
1783 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00001784 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
1785 (!strcmp(argv[i], "--dtdvalidfpi"))) {
1786 i++;
1787 continue;
1788 }
Daniel Veillard4432df22003-09-28 18:58:27 +00001789#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard71531f32003-02-05 13:19:53 +00001790 if ((!strcmp(argv[i], "-relaxng")) ||
1791 (!strcmp(argv[i], "--relaxng"))) {
1792 i++;
1793 continue;
1794 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001795 if ((!strcmp(argv[i], "-schema")) ||
1796 (!strcmp(argv[i], "--schema"))) {
1797 i++;
1798 continue;
1799 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001800 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00001801 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001802 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001803 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001804 if (repeat) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001805 xmlParserCtxtPtr ctxt = NULL;
1806
1807 for (acount = 0;acount < repeat;acount++) {
Daniel Veillard81273902003-09-30 00:43:48 +00001808#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001809 if (stream != 0)
1810 streamFile(argv[i]);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001811 else {
Daniel Veillard81273902003-09-30 00:43:48 +00001812#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001813 if (ctxt == NULL)
1814 ctxt = xmlNewParserCtxt();
1815 parseAndPrintFile(argv[i], ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00001816#ifdef LIBXML_READER_ENABLED
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001817 }
Daniel Veillard81273902003-09-30 00:43:48 +00001818#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001819 }
1820 if (ctxt != NULL)
1821 xmlFreeParserCtxt(ctxt);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001822 } else {
Daniel Veillard81273902003-09-30 00:43:48 +00001823#ifdef LIBXML_READER_ENABLED
Daniel Veillard7704fb12003-01-03 16:19:51 +00001824 if (stream != 0)
1825 streamFile(argv[i]);
1826 else
Daniel Veillard81273902003-09-30 00:43:48 +00001827#endif /* LIBXML_READER_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001828 parseAndPrintFile(argv[i], NULL);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001829 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001830 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00001831 if ((timing) && (repeat)) {
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001832 endTimer("%d iterations", repeat);
Daniel Veillarda7866932001-12-04 13:14:44 +00001833 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001834 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001835 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001836 if (generate)
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001837 parseAndPrintFile(NULL, NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001838 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001839 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001840 }
Daniel Veillard845cce42002-01-09 11:51:37 +00001841 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001842 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001843 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001844#ifdef LIBXML_SCHEMAS_ENABLED
1845 if (relaxngschemas != NULL)
1846 xmlRelaxNGFree(relaxngschemas);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001847 if (wxschemas != NULL)
1848 xmlSchemaFree(wxschemas);
Daniel Veillard71531f32003-02-05 13:19:53 +00001849 xmlRelaxNGCleanupTypes();
1850#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001851 xmlCleanupParser();
1852 xmlMemoryDump();
1853
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001854 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001855}
Daniel Veillard88a172f2000-08-04 18:23:10 +00001856