blob: 1151bf71b34e75c64e930dec0f94997aabaa2a9f [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 Veillardce8b83b2000-04-05 18:38:42 +0000102static int copy = 0;
103static int recovery = 0;
104static int noent = 0;
105static int noout = 0;
106static int nowrap = 0;
107static int valid = 0;
108static int postvalid = 0;
Daniel Veillardcd429612000-10-11 15:57:05 +0000109static char * dtdvalid = NULL;
Daniel Veillard66f68e72003-08-18 16:39:51 +0000110static char * dtdvalidfpi = NULL;
Daniel Veillard71531f32003-02-05 13:19:53 +0000111#ifdef LIBXML_SCHEMAS_ENABLED
112static char * relaxng = NULL;
113static xmlRelaxNGPtr relaxngschemas = NULL;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +0000114static char * schema = NULL;
115static xmlSchemaPtr wxschemas = NULL;
Daniel Veillard71531f32003-02-05 13:19:53 +0000116#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000117static int repeat = 0;
118static int insert = 0;
119static int compress = 0;
120static int html = 0;
121static int htmlout = 0;
122static int push = 0;
Daniel Veillard46e370e2000-07-21 20:32:03 +0000123#ifdef HAVE_SYS_MMAN_H
124static int memory = 0;
125#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000126static int noblanks = 0;
Daniel Veillard90493a92001-08-14 14:12:47 +0000127static int format = 0;
Daniel Veillard5e873c42000-04-12 13:27:38 +0000128static int testIO = 0;
Daniel Veillardbe803962000-06-28 23:40:59 +0000129static char *encoding = NULL;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000130#ifdef LIBXML_XINCLUDE_ENABLED
131static int xinclude = 0;
132#endif
Daniel Veillard48da9102001-08-07 01:10:10 +0000133static int dtdattrs = 0;
Daniel Veillard10ea86c2001-06-20 13:55:33 +0000134static int loaddtd = 0;
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000135static int progresult = 0;
Daniel Veillard48b2f892001-02-25 16:11:03 +0000136static int timing = 0;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000137static int generate = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000138static int dropdtd = 0;
Daniel Veillarde2940dd2001-08-22 00:06:49 +0000139#ifdef LIBXML_CATALOG_ENABLED
140static int catalogs = 0;
141static int nocatalogs = 0;
142#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000143static int stream = 0;
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000144static int chkregister = 0;
Daniel Veillard07cb8222003-09-10 10:51:05 +0000145static int sax1 = 0;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +0000146static const char *output = NULL;
147
Daniel Veillard01db67c2001-12-18 07:09:59 +0000148/*
149 * Internal timing routines to remove the necessity to have unix-specific
150 * function calls
151 */
152
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000153#ifndef HAVE_GETTIMEOFDAY
154#ifdef HAVE_SYS_TIMEB_H
155#ifdef HAVE_SYS_TIME_H
156#ifdef HAVE_FTIME
157
Daniel Veillard01c13b52002-12-10 15:19:08 +0000158static int
Daniel Veillard8c1ae602002-03-07 11:21:00 +0000159my_gettimeofday(struct timeval *tvp, void *tzp)
160{
161 struct timeb timebuffer;
162
163 ftime(&timebuffer);
164 if (tvp) {
165 tvp->tv_sec = timebuffer.time;
166 tvp->tv_usec = timebuffer.millitm * 1000L;
167 }
168 return (0);
169}
170#define HAVE_GETTIMEOFDAY 1
171#define gettimeofday my_gettimeofday
172
173#endif /* HAVE_FTIME */
174#endif /* HAVE_SYS_TIME_H */
175#endif /* HAVE_SYS_TIMEB_H */
176#endif /* !HAVE_GETTIMEOFDAY */
177
Daniel Veillard01db67c2001-12-18 07:09:59 +0000178#if defined(HAVE_GETTIMEOFDAY)
179static struct timeval begin, end;
180
181/*
182 * startTimer: call where you want to start timing
183 */
184static void
185startTimer(void)
186{
187 gettimeofday(&begin, NULL);
188}
189
190/*
191 * endTimer: call where you want to stop timing and to print out a
192 * message about the timing performed; format is a printf
193 * type argument
194 */
195static void
Daniel Veillard118aed72002-09-24 14:13:13 +0000196endTimer(const char *fmt, ...)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000197{
198 long msec;
199 va_list ap;
200
201 gettimeofday(&end, NULL);
202 msec = end.tv_sec - begin.tv_sec;
203 msec *= 1000;
204 msec += (end.tv_usec - begin.tv_usec) / 1000;
205
206#ifndef HAVE_STDARG_H
207#error "endTimer required stdarg functions"
208#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000209 va_start(ap, fmt);
210 vfprintf(stderr, fmt, ap);
Daniel Veillard01db67c2001-12-18 07:09:59 +0000211 va_end(ap);
212
213 fprintf(stderr, " took %ld ms\n", msec);
214}
215#elif defined(HAVE_TIME_H)
Daniel Veillard01db67c2001-12-18 07:09:59 +0000216/*
217 * No gettimeofday function, so we have to make do with calling clock.
218 * This is obviously less accurate, but there's little we can do about
219 * that.
220 */
Daniel Veillard90bc3712002-03-07 15:12:58 +0000221#ifndef CLOCKS_PER_SEC
222#define CLOCKS_PER_SEC 100
223#endif
Daniel Veillard01db67c2001-12-18 07:09:59 +0000224
225static clock_t begin, end;
226static void
227startTimer(void)
228{
229 begin = clock();
230}
231static void
232endTimer(const char *fmt, ...)
233{
234 long msec;
235 va_list ap;
236
237 end = clock();
238 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
239
240#ifndef HAVE_STDARG_H
241#error "endTimer required stdarg functions"
242#endif
243 va_start(ap, fmt);
244 vfprintf(stderr, fmt, ap);
245 va_end(ap);
246 fprintf(stderr, " took %ld ms\n", msec);
247}
248#else
249
250/*
251 * We don't have a gettimeofday or time.h, so we just don't do timing
252 */
253static void
254startTimer(void)
255{
256 /*
257 * Do nothing
258 */
259}
260static void
261endTimer(char *format, ...)
262{
263 /*
264 * We cannot do anything because we don't have a timing function
265 */
266#ifdef HAVE_STDARG_H
267 va_start(ap, format);
268 vfprintf(stderr, format, ap);
269 va_end(ap);
270 fprintf(stderr, " was not timed\n", msec);
271#else
272 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
273 * this ?!
274 */
275#endif
276}
277#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000278/************************************************************************
279 * *
280 * HTML ouput *
281 * *
282 ************************************************************************/
283char buffer[50000];
284
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000285static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000286xmlHTMLEncodeSend(void) {
287 char *result;
288
289 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
290 if (result) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000291 xmlGenericError(xmlGenericErrorContext, "%s", result);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000292 xmlFree(result);
293 }
294 buffer[0] = 0;
295}
296
297/**
298 * xmlHTMLPrintFileInfo:
299 * @input: an xmlParserInputPtr input
300 *
301 * Displays the associated file and line informations for the current input
302 */
303
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000304static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000305xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000306 int len;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000307 xmlGenericError(xmlGenericErrorContext, "<p>");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000308
309 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000310 if (input != NULL) {
311 if (input->filename) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000312 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000313 input->line);
314 } else {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000315 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000316 }
317 }
318 xmlHTMLEncodeSend();
319}
320
321/**
322 * xmlHTMLPrintFileContext:
323 * @input: an xmlParserInputPtr input
324 *
325 * Displays current context within the input content for error tracking
326 */
327
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000328static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000329xmlHTMLPrintFileContext(xmlParserInputPtr input) {
330 const xmlChar *cur, *base;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000331 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000332 int n;
333
334 if (input == NULL) return;
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000335 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000336 cur = input->cur;
337 base = input->base;
338 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
339 cur--;
340 }
341 n = 0;
342 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
343 cur--;
344 if ((*cur == '\n') || (*cur == '\r')) cur++;
345 base = cur;
346 n = 0;
347 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000348 len = strlen(buffer);
349 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
350 (unsigned char) *cur++);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000351 n++;
352 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000353 len = strlen(buffer);
354 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000355 cur = input->cur;
356 while ((*cur == '\n') || (*cur == '\r'))
357 cur--;
358 n = 0;
359 while ((cur != base) && (n++ < 80)) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000360 len = strlen(buffer);
361 snprintf(&buffer[len], sizeof(buffer) - len, " ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000362 base++;
363 }
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000364 len = strlen(buffer);
365 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000366 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000367 xmlGenericError(xmlGenericErrorContext, "</pre>");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000368}
369
370/**
371 * xmlHTMLError:
372 * @ctx: an XML parser context
373 * @msg: the message to display/transmit
374 * @...: extra parameters for the message display
375 *
376 * Display and format an error messages, gives file, line, position and
377 * extra parameters.
378 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000379static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000380xmlHTMLError(void *ctx, const char *msg, ...)
381{
382 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
383 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000384 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000385 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000386
387 buffer[0] = 0;
388 input = ctxt->input;
389 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000390 input = ctxt->inputTab[ctxt->inputNr - 2];
391 }
392
393 xmlHTMLPrintFileInfo(input);
394
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000395 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000396 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000397 len = strlen(buffer);
398 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000399 va_end(args);
400 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000401 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000402
403 xmlHTMLPrintFileContext(input);
404 xmlHTMLEncodeSend();
405}
406
407/**
408 * xmlHTMLWarning:
409 * @ctx: an XML parser context
410 * @msg: the message to display/transmit
411 * @...: extra parameters for the message display
412 *
413 * Display and format a warning messages, gives file, line, position and
414 * extra parameters.
415 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000416static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000417xmlHTMLWarning(void *ctx, const char *msg, ...)
418{
419 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
420 xmlParserInputPtr input;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000421 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000422 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000423
424 buffer[0] = 0;
425 input = ctxt->input;
426 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000427 input = ctxt->inputTab[ctxt->inputNr - 2];
428 }
429
430
431 xmlHTMLPrintFileInfo(input);
432
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000433 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000434 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000435 len = strlen(buffer);
436 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000437 va_end(args);
438 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000439 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000440
441 xmlHTMLPrintFileContext(input);
442 xmlHTMLEncodeSend();
443}
444
445/**
446 * xmlHTMLValidityError:
447 * @ctx: an XML parser context
448 * @msg: the message to display/transmit
449 * @...: extra parameters for the message display
450 *
451 * Display and format an validity error messages, gives file,
452 * line, position and extra parameters.
453 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000454static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000455xmlHTMLValidityError(void *ctx, const char *msg, ...)
456{
457 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
458 xmlParserInputPtr input;
459 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000460 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000461
462 buffer[0] = 0;
463 input = ctxt->input;
464 if ((input->filename == NULL) && (ctxt->inputNr > 1))
465 input = ctxt->inputTab[ctxt->inputNr - 2];
466
467 xmlHTMLPrintFileInfo(input);
468
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000469 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000470 len = strlen(buffer);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000471 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000472 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000473 va_end(args);
474 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000475 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000476
477 xmlHTMLPrintFileContext(input);
478 xmlHTMLEncodeSend();
479}
480
481/**
482 * xmlHTMLValidityWarning:
483 * @ctx: an XML parser context
484 * @msg: the message to display/transmit
485 * @...: extra parameters for the message display
486 *
487 * Display and format a validity warning messages, gives file, line,
488 * position and extra parameters.
489 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000490static void
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000491xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
492{
493 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
494 xmlParserInputPtr input;
495 va_list args;
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000496 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000497
498 buffer[0] = 0;
499 input = ctxt->input;
500 if ((input->filename == NULL) && (ctxt->inputNr > 1))
501 input = ctxt->inputTab[ctxt->inputNr - 2];
502
503 xmlHTMLPrintFileInfo(input);
504
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000505 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000506 va_start(args, msg);
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000507 len = strlen(buffer);
508 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000509 va_end(args);
510 xmlHTMLEncodeSend();
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +0000511 xmlGenericError(xmlGenericErrorContext, "</p>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000512
513 xmlHTMLPrintFileContext(input);
514 xmlHTMLEncodeSend();
515}
516
517/************************************************************************
518 * *
519 * Shell Interface *
520 * *
521 ************************************************************************/
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000522#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000523/**
524 * xmlShellReadline:
525 * @prompt: the prompt value
526 *
527 * Read a string
528 *
529 * Returns a pointer to it or NULL on EOF the caller is expected to
530 * free the returned string.
531 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000532static char *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000533xmlShellReadline(char *prompt) {
534#ifdef HAVE_LIBREADLINE
535 char *line_read;
536
537 /* Get a line from the user. */
538 line_read = readline (prompt);
539
540 /* If the line has any text in it, save it on the history. */
541 if (line_read && *line_read)
542 add_history (line_read);
543
544 return (line_read);
545#else
546 char line_read[501];
Daniel Veillard29e43992001-12-13 22:21:58 +0000547 char *ret;
548 int len;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000549
550 if (prompt != NULL)
551 fprintf(stdout, "%s", prompt);
552 if (!fgets(line_read, 500, stdin))
553 return(NULL);
554 line_read[500] = 0;
Daniel Veillard29e43992001-12-13 22:21:58 +0000555 len = strlen(line_read);
556 ret = (char *) malloc(len + 1);
557 if (ret != NULL) {
558 memcpy (ret, line_read, len + 1);
559 }
560 return(ret);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000561#endif
562}
Daniel Veillard56ada1d2003-01-07 11:17:25 +0000563#endif /* LIBXML_DEBUG_ENABLED */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000564
565/************************************************************************
566 * *
Daniel Veillard5e873c42000-04-12 13:27:38 +0000567 * I/O Interfaces *
568 * *
569 ************************************************************************/
570
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000571static int myRead(FILE *f, char * buf, int len) {
572 return(fread(buf, 1, len, f));
Daniel Veillard5e873c42000-04-12 13:27:38 +0000573}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000574static void myClose(FILE *f) {
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000575 if (f != stdin) {
Daniel Veillard5e873c42000-04-12 13:27:38 +0000576 fclose(f);
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000577 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000578}
579
580/************************************************************************
581 * *
Daniel Veillard7704fb12003-01-03 16:19:51 +0000582 * Stream Test processing *
583 * *
584 ************************************************************************/
Daniel Veillard7704fb12003-01-03 16:19:51 +0000585static void processNode(xmlTextReaderPtr reader) {
Daniel Veillarde59494f2003-01-04 16:35:29 +0000586 xmlChar *name, *value;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000587
Daniel Veillarde59494f2003-01-04 16:35:29 +0000588 name = xmlTextReaderName(reader);
589 if (name == NULL)
590 name = xmlStrdup(BAD_CAST "--");
Daniel Veillard99737f52003-03-22 14:55:50 +0000591
Daniel Veillarde59494f2003-01-04 16:35:29 +0000592 value = xmlTextReaderValue(reader);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000593
Daniel Veillard99737f52003-03-22 14:55:50 +0000594 printf("%d %d %s %d %d",
Daniel Veillarde59494f2003-01-04 16:35:29 +0000595 xmlTextReaderDepth(reader),
596 xmlTextReaderNodeType(reader),
597 name,
Daniel Veillard99737f52003-03-22 14:55:50 +0000598 xmlTextReaderIsEmptyElement(reader),
599 xmlTextReaderHasValue(reader));
Daniel Veillarde59494f2003-01-04 16:35:29 +0000600 xmlFree(name);
601 if (value == NULL)
602 printf("\n");
603 else {
604 printf(" %s\n", value);
605 xmlFree(value);
Daniel Veillard7704fb12003-01-03 16:19:51 +0000606 }
607}
608
609static void streamFile(char *filename) {
610 xmlTextReaderPtr reader;
611 int ret;
612
Daniel Veillard7704fb12003-01-03 16:19:51 +0000613 reader = xmlNewTextReaderFilename(filename);
614 if (reader != NULL) {
615 if (valid)
616 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +0000617 else
618 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000619#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +0000620 if (relaxng != NULL) {
621 if (timing) {
622 startTimer();
623 }
624 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
625 if (ret < 0) {
626 xmlGenericError(xmlGenericErrorContext,
627 "Relax-NG schema %s failed to compile\n", relaxng);
628 relaxng = NULL;
629 }
630 if (timing) {
631 endTimer("Compiling the schemas");
632 }
633 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000634#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000635
636 /*
637 * Process all nodes in sequence
638 */
Daniel Veillardce192eb2003-04-16 15:58:05 +0000639 if (timing) {
640 startTimer();
641 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000642 ret = xmlTextReaderRead(reader);
643 while (ret == 1) {
644 if (debug)
645 processNode(reader);
646 ret = xmlTextReaderRead(reader);
647 }
Daniel Veillardce192eb2003-04-16 15:58:05 +0000648 if (timing) {
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000649#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +0000650 if ((valid) || (relaxng != NULL))
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000651#else
652 if (valid)
653#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +0000654 endTimer("Parsing and validating");
655 else
656 endTimer("Parsing");
657 }
Daniel Veillard7704fb12003-01-03 16:19:51 +0000658
Daniel Veillardf6bad792003-04-11 19:38:54 +0000659 if (valid) {
660 if (xmlTextReaderIsValid(reader) != 1) {
661 xmlGenericError(xmlGenericErrorContext,
662 "Document %s does not validate\n", filename);
663 progresult = 3;
664 }
665 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000666#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardf4e55762003-04-15 23:32:22 +0000667 if (relaxng != NULL) {
668 if (xmlTextReaderIsValid(reader) != 1) {
669 printf("%s fails to validate\n", filename);
670 progresult = 3;
671 } else {
672 printf("%s validates\n", filename);
673 }
674 }
Daniel Veillard37fc84d2003-05-09 19:38:15 +0000675#endif
Daniel Veillard7704fb12003-01-03 16:19:51 +0000676 /*
677 * Done, cleanup and status
678 */
679 xmlFreeTextReader(reader);
680 if (ret != 0) {
681 printf("%s : failed to parse\n", filename);
Daniel Veillardf6bad792003-04-11 19:38:54 +0000682 progresult = 1;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000683 }
684 } else {
685 fprintf(stderr, "Unable to open %s\n", filename);
Daniel Veillardf6bad792003-04-11 19:38:54 +0000686 progresult = 1;
Daniel Veillard7704fb12003-01-03 16:19:51 +0000687 }
688}
689
690/************************************************************************
691 * *
692 * Tree Test processing *
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000693 * *
694 ************************************************************************/
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000695static void parseAndPrintFile(char *filename) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000696 xmlDocPtr doc = NULL, tmp;
697
Daniel Veillard48b2f892001-02-25 16:11:03 +0000698 if ((timing) && (!repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +0000699 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000700
701
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000702 if (filename == NULL) {
703 if (generate) {
704 xmlNodePtr n;
705
706 doc = xmlNewDoc(BAD_CAST "1.0");
707 n = xmlNewNode(NULL, BAD_CAST "info");
708 xmlNodeSetContent(n, BAD_CAST "abc");
709 xmlDocSetRootElement(doc, n);
710 }
711 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000712#ifdef LIBXML_HTML_ENABLED
William M. Brack78637da2003-07-31 14:47:38 +0000713 else if ((html) && (push)) {
714 FILE *f;
715
716 f = fopen(filename, "r");
717 if (f != NULL) {
718 int res, size = 3;
719 char chars[4096];
720 htmlParserCtxtPtr ctxt;
721
722 /* if (repeat) */
723 size = 4096;
724 res = fread(chars, 1, 4, f);
725 if (res > 0) {
726 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
727 chars, res, filename, 0);
728 while ((res = fread(chars, 1, size, f)) > 0) {
729 htmlParseChunk(ctxt, chars, res, 0);
730 }
731 htmlParseChunk(ctxt, chars, 0, 1);
732 doc = ctxt->myDoc;
733 htmlFreeParserCtxt(ctxt);
734 }
735 fclose(f);
736 }
737 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000738 else if (html) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000739 doc = htmlParseFile(filename, NULL);
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000740 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000741#endif /* LIBXML_HTML_ENABLED */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +0000742 else {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000743 /*
744 * build an XML tree from a string;
745 */
746 if (push) {
747 FILE *f;
748
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000749 /* '-' Usually means stdin -<sven@zen.org> */
750 if ((filename[0] == '-') && (filename[1] == 0)) {
751 f = stdin;
752 } else {
753 f = fopen(filename, "r");
754 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000755 if (f != NULL) {
Daniel Veillarde715dd22000-08-29 18:29:38 +0000756 int ret;
Daniel Veillarda880b122003-04-21 21:36:41 +0000757 int res, size = 1024;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000758 char chars[1024];
759 xmlParserCtxtPtr ctxt;
760
Daniel Veillarda880b122003-04-21 21:36:41 +0000761 /* if (repeat) size = 1024; */
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000762 res = fread(chars, 1, 4, f);
763 if (res > 0) {
764 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
765 chars, res, filename);
766 while ((res = fread(chars, 1, size, f)) > 0) {
767 xmlParseChunk(ctxt, chars, res, 0);
768 }
769 xmlParseChunk(ctxt, chars, 0, 1);
770 doc = ctxt->myDoc;
Daniel Veillarde715dd22000-08-29 18:29:38 +0000771 ret = ctxt->wellFormed;
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000772 xmlFreeParserCtxt(ctxt);
Daniel Veillarde715dd22000-08-29 18:29:38 +0000773 if (!ret) {
774 xmlFreeDoc(doc);
775 doc = NULL;
776 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000777 }
778 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000779 } else if (testIO) {
780 int ret;
781 FILE *f;
782
Daniel Veillard4a6845d2001-01-03 13:32:39 +0000783 /* '-' Usually means stdin -<sven@zen.org> */
784 if ((filename[0] == '-') && (filename[1] == 0)) {
785 f = stdin;
786 } else {
787 f = fopen(filename, "r");
788 }
Daniel Veillard5e873c42000-04-12 13:27:38 +0000789 if (f != NULL) {
790 xmlParserCtxtPtr ctxt;
791
792 ctxt = xmlCreateIOParserCtxt(NULL, NULL,
793 (xmlInputReadCallback) myRead,
794 (xmlInputCloseCallback) myClose,
795 f, XML_CHAR_ENCODING_NONE);
796 xmlParseDocument(ctxt);
797
798 ret = ctxt->wellFormed;
799 doc = ctxt->myDoc;
800 xmlFreeParserCtxt(ctxt);
801 if (!ret) {
802 xmlFreeDoc(doc);
803 doc = NULL;
804 }
805 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000806 } else if (recovery) {
807 doc = xmlRecoverFile(filename);
808 } else if (htmlout) {
809 int ret;
810 xmlParserCtxtPtr ctxt;
811 xmlSAXHandler silent, *old;
812
813 ctxt = xmlCreateFileParserCtxt(filename);
Daniel Veillard88a172f2000-08-04 18:23:10 +0000814
815 if (ctxt == NULL) {
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000816 /* If xmlCreateFileParserCtxt() return NULL something
Daniel Veillard88a172f2000-08-04 18:23:10 +0000817 strange happened so we don't want to do anything. Do
818 we want to print an error message here?
819 <sven@zen.org> */
Daniel Veillard7ebb1ee2000-08-04 18:24:45 +0000820 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000821 } else {
822 memcpy(&silent, ctxt->sax, sizeof(silent));
823 old = ctxt->sax;
824 silent.error = xmlHTMLError;
825 if (xmlGetWarningsDefaultValue)
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000826 silent.warning = xmlHTMLWarning;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000827 else
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000828 silent.warning = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000829 silent.fatalError = xmlHTMLError;
830 ctxt->sax = &silent;
831 ctxt->vctxt.error = xmlHTMLValidityError;
832 if (xmlGetWarningsDefaultValue)
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000833 ctxt->vctxt.warning = xmlHTMLValidityWarning;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000834 else
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000835 ctxt->vctxt.warning = NULL;
836
Daniel Veillard88a172f2000-08-04 18:23:10 +0000837 xmlParseDocument(ctxt);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000838
Daniel Veillard88a172f2000-08-04 18:23:10 +0000839 ret = ctxt->wellFormed;
840 doc = ctxt->myDoc;
841 ctxt->sax = old;
842 xmlFreeParserCtxt(ctxt);
843 if (!ret) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000844 xmlFreeDoc(doc);
845 doc = NULL;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000846 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000847 }
Daniel Veillard46e370e2000-07-21 20:32:03 +0000848#ifdef HAVE_SYS_MMAN_H
849 } else if (memory) {
850 int fd;
851 struct stat info;
852 const char *base;
853 if (stat(filename, &info) < 0)
854 return;
855 if ((fd = open(filename, O_RDONLY)) < 0)
856 return;
857 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
Daniel Veillard29579362000-08-14 17:57:48 +0000858 if (base == (void *) MAP_FAILED)
Daniel Veillard46e370e2000-07-21 20:32:03 +0000859 return;
860
861 doc = xmlParseMemory((char *) base, info.st_size);
862 munmap((char *) base, info.st_size);
863#endif
Daniel Veillardea7751d2002-12-20 00:16:24 +0000864 } else if (valid) {
865 int ret;
866 xmlParserCtxtPtr ctxt;
867
868 ctxt = xmlCreateFileParserCtxt(filename);
869
870 if (ctxt == NULL) {
871 doc = NULL;
872 } else {
873 xmlParseDocument(ctxt);
874 if (ctxt->valid == 0)
875 progresult = 4;
876 ret = ctxt->wellFormed;
877 doc = ctxt->myDoc;
878 xmlFreeParserCtxt(ctxt);
879 if (!ret) {
880 xmlFreeDoc(doc);
881 doc = NULL;
882 }
883 }
884 } else {
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000885 doc = xmlParseFile(filename);
Daniel Veillardea7751d2002-12-20 00:16:24 +0000886 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000887 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000888
Daniel Veillard88a172f2000-08-04 18:23:10 +0000889 /*
890 * If we don't have a document we might as well give up. Do we
891 * want an error message here? <sven@zen.org> */
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000892 if (doc == NULL) {
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000893 progresult = 1;
Daniel Veillard88a172f2000-08-04 18:23:10 +0000894 return;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000895 }
896
Daniel Veillard48b2f892001-02-25 16:11:03 +0000897 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000898 endTimer("Parsing");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000899 }
900
Daniel Veillard29e43992001-12-13 22:21:58 +0000901 /*
902 * Remove DOCTYPE nodes
903 */
904 if (dropdtd) {
905 xmlDtdPtr dtd;
906
907 dtd = xmlGetIntSubset(doc);
908 if (dtd != NULL) {
909 xmlUnlinkNode((xmlNodePtr)dtd);
910 xmlFreeDtd(dtd);
911 }
912 }
913
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000914#ifdef LIBXML_XINCLUDE_ENABLED
Daniel Veillard48b2f892001-02-25 16:11:03 +0000915 if (xinclude) {
916 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000917 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000918 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000919 xmlXIncludeProcess(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +0000920 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000921 endTimer("Xinclude processing");
Daniel Veillard48b2f892001-02-25 16:11:03 +0000922 }
923 }
Daniel Veillard9e8bfae2000-11-06 16:43:11 +0000924#endif
Daniel Veillard88a172f2000-08-04 18:23:10 +0000925
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000926#ifdef LIBXML_DEBUG_ENABLED
927 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000928 * shell interaction
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000929 */
930 if (shell)
931 xmlShell(doc, filename, xmlShellReadline, stdout);
932#endif
933
934 /*
935 * test intermediate copy if needed.
936 */
937 if (copy) {
938 tmp = doc;
939 doc = xmlCopyDoc(doc, 1);
940 xmlFreeDoc(tmp);
941 }
942
943 if ((insert) && (!html)) {
944 const xmlChar* list[256];
945 int nb, i;
946 xmlNodePtr node;
947
948 if (doc->children != NULL) {
949 node = doc->children;
950 while ((node != NULL) && (node->last == NULL)) node = node->next;
951 if (node != NULL) {
952 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
953 if (nb < 0) {
954 printf("could not get valid list of elements\n");
955 } else if (nb == 0) {
MDT 2001 John Fleckbbb9e432001-09-24 03:08:43 +0000956 printf("No element can be inserted under root\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000957 } else {
MDT 2001 John Fleckbbb9e432001-09-24 03:08:43 +0000958 printf("%d element types can be inserted under root:\n",
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000959 nb);
960 for (i = 0;i < nb;i++) {
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000961 printf("%s\n", (char *) list[i]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +0000962 }
963 }
964 }
965 }
966 }else if (noout == 0) {
967 /*
968 * print it.
969 */
970#ifdef LIBXML_DEBUG_ENABLED
971 if (!debug) {
972#endif
Daniel Veillard48b2f892001-02-25 16:11:03 +0000973 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +0000974 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +0000975 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +0000976#ifdef HAVE_SYS_MMAN_H
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000977 if (memory) {
978 xmlChar *result;
979 int len;
980
981 if (encoding != NULL) {
Daniel Veillardd536f702001-11-08 17:32:47 +0000982 if ( format ) {
983 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
984 } else {
985 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
986 }
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000987 } else {
Daniel Veillard90493a92001-08-14 14:12:47 +0000988 if (format)
989 xmlDocDumpFormatMemory(doc, &result, &len, 1);
990 else
991 xmlDocDumpMemory(doc, &result, &len);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000992 }
993 if (result == NULL) {
994 fprintf(stderr, "Failed to save\n");
995 } else {
996 write(1, result, len);
997 xmlFree(result);
998 }
Daniel Veillard3b2c2612001-04-04 00:09:00 +0000999 } else
1000#endif /* HAVE_SYS_MMAN_H */
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001001 if (compress) {
1002 xmlSaveFile(output ? output : "-", doc);
1003 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001004 else if (encoding != NULL) {
1005 if ( format ) {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001006 xmlSaveFormatFileEnc(output ? output : "-", doc, encoding, 1);
1007 }
Daniel Veillardd536f702001-11-08 17:32:47 +00001008 else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001009 xmlSaveFileEnc(output ? output : "-", doc, encoding);
Daniel Veillardd536f702001-11-08 17:32:47 +00001010 }
1011 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001012 else if (format) {
1013 xmlSaveFormatFile(output ? output : "-", doc, 1);
1014 }
1015 else {
1016 FILE *out;
1017 if (output == NULL)
1018 out = stdout;
1019 else {
1020 out = fopen(output,"wb");
1021 }
1022 xmlDocDump(out, doc);
1023
1024 if (output)
1025 fclose(out);
1026 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001027 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001028 endTimer("Saving");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001029 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001030#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001031 } else {
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001032 FILE *out;
1033 if (output == NULL)
1034 out = stdout;
1035 else {
1036 out = fopen(output,"wb");
1037 }
1038 xmlDebugDumpDocument(out, doc);
1039
1040 if (output)
1041 fclose(out);
Daniel Veillarda6d8eb62000-12-27 10:46:47 +00001042 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001043#endif
1044 }
1045
1046 /*
1047 * A posteriori validation test
1048 */
Daniel Veillard66f68e72003-08-18 16:39:51 +00001049 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
Daniel Veillardcd429612000-10-11 15:57:05 +00001050 xmlDtdPtr dtd;
1051
Daniel Veillard48b2f892001-02-25 16:11:03 +00001052 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001053 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001054 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00001055 if (dtdvalid != NULL)
1056 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
1057 else
1058 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001059 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001060 endTimer("Parsing DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001061 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001062 if (dtd == NULL) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001063 if (dtdvalid != NULL)
1064 xmlGenericError(xmlGenericErrorContext,
1065 "Could not parse DTD %s\n", dtdvalid);
1066 else
1067 xmlGenericError(xmlGenericErrorContext,
1068 "Could not parse DTD %s\n", dtdvalidfpi);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001069 progresult = 2;
Daniel Veillardcd429612000-10-11 15:57:05 +00001070 } else {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001071 xmlValidCtxtPtr cvp;
1072
1073 if ((cvp = xmlNewValidCtxt()) == NULL) {
1074 xmlGenericError(xmlGenericErrorContext,
1075 "Couldn't allocate validation context\n");
1076 exit(-1);
1077 }
1078 cvp->userData = (void *) stderr;
1079 cvp->error = (xmlValidityErrorFunc) fprintf;
1080 cvp->warning = (xmlValidityWarningFunc) fprintf;
1081
Daniel Veillard48b2f892001-02-25 16:11:03 +00001082 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001083 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001084 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001085 if (!xmlValidateDtd(cvp, doc, dtd)) {
Daniel Veillard66f68e72003-08-18 16:39:51 +00001086 if (dtdvalid != NULL)
1087 xmlGenericError(xmlGenericErrorContext,
1088 "Document %s does not validate against %s\n",
1089 filename, dtdvalid);
1090 else
1091 xmlGenericError(xmlGenericErrorContext,
1092 "Document %s does not validate against %s\n",
1093 filename, dtdvalidfpi);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001094 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +00001095 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001096 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001097 endTimer("Validating against DTD");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001098 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001099 xmlFreeValidCtxt(cvp);
Daniel Veillardcd429612000-10-11 15:57:05 +00001100 xmlFreeDtd(dtd);
1101 }
1102 } else if (postvalid) {
Daniel Veillarda37aab82003-06-09 09:10:36 +00001103 xmlValidCtxtPtr cvp;
1104
1105 if ((cvp = xmlNewValidCtxt()) == NULL) {
1106 xmlGenericError(xmlGenericErrorContext,
1107 "Couldn't allocate validation context\n");
1108 exit(-1);
1109 }
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 Veillarda37aab82003-06-09 09:10:36 +00001114 cvp->userData = (void *) stderr;
1115 cvp->error = (xmlValidityErrorFunc) fprintf;
1116 cvp->warning = (xmlValidityWarningFunc) fprintf;
1117 if (!xmlValidateDocument(cvp, doc)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001118 xmlGenericError(xmlGenericErrorContext,
1119 "Document %s does not validate\n", filename);
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001120 progresult = 3;
Daniel Veillardcd429612000-10-11 15:57:05 +00001121 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001122 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001123 endTimer("Validating");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001124 }
Daniel Veillarda37aab82003-06-09 09:10:36 +00001125 xmlFreeValidCtxt(cvp);
Daniel Veillard71531f32003-02-05 13:19:53 +00001126#ifdef LIBXML_SCHEMAS_ENABLED
1127 } else if (relaxngschemas != NULL) {
1128 xmlRelaxNGValidCtxtPtr ctxt;
1129 int ret;
1130
Daniel Veillard42f12e92003-03-07 18:32:59 +00001131 if ((timing) && (!repeat)) {
1132 startTimer();
1133 }
1134
Daniel Veillard71531f32003-02-05 13:19:53 +00001135 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
1136 xmlRelaxNGSetValidErrors(ctxt,
1137 (xmlRelaxNGValidityErrorFunc) fprintf,
1138 (xmlRelaxNGValidityWarningFunc) fprintf,
1139 stderr);
1140 ret = xmlRelaxNGValidateDoc(ctxt, doc);
1141 if (ret == 0) {
1142 printf("%s validates\n", filename);
1143 } else if (ret > 0) {
1144 printf("%s fails to validate\n", filename);
1145 } else {
1146 printf("%s validation generated an internal error\n",
1147 filename);
1148 }
1149 xmlRelaxNGFreeValidCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001150 if ((timing) && (!repeat)) {
1151 endTimer("Validating");
1152 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001153 } else if (wxschemas != NULL) {
1154 xmlSchemaValidCtxtPtr ctxt;
1155 int ret;
1156
1157 if ((timing) && (!repeat)) {
1158 startTimer();
1159 }
1160
1161 ctxt = xmlSchemaNewValidCtxt(wxschemas);
1162 xmlSchemaSetValidErrors(ctxt,
1163 (xmlSchemaValidityErrorFunc) fprintf,
1164 (xmlSchemaValidityWarningFunc) fprintf,
1165 stderr);
1166 ret = xmlSchemaValidateDoc(ctxt, doc);
1167 if (ret == 0) {
1168 printf("%s validates\n", filename);
1169 } else if (ret > 0) {
1170 printf("%s fails to validate\n", filename);
1171 } else {
1172 printf("%s validation generated an internal error\n",
1173 filename);
1174 }
1175 xmlSchemaFreeValidCtxt(ctxt);
1176 if ((timing) && (!repeat)) {
1177 endTimer("Validating");
1178 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001179#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001180 }
1181
1182#ifdef LIBXML_DEBUG_ENABLED
1183 if ((debugent) && (!html))
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001184 xmlDebugDumpEntities(stderr, doc);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001185#endif
1186
1187 /*
1188 * free it.
1189 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00001190 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001191 startTimer();
Daniel Veillard48b2f892001-02-25 16:11:03 +00001192 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001193 xmlFreeDoc(doc);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001194 if ((timing) && (!repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001195 endTimer("Freeing");
Daniel Veillard48b2f892001-02-25 16:11:03 +00001196 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001197}
1198
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001199/************************************************************************
1200 * *
1201 * Usage and Main *
1202 * *
1203 ************************************************************************/
1204
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001205static void showVersion(const char *name) {
1206 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
1207 fprintf(stderr, " compiled with: ");
1208#ifdef LIBXML_FTP_ENABLED
1209 fprintf(stderr, "FTP ");
1210#endif
1211#ifdef LIBXML_HTTP_ENABLED
1212 fprintf(stderr, "HTTP ");
1213#endif
1214#ifdef LIBXML_HTML_ENABLED
1215 fprintf(stderr, "HTML ");
1216#endif
1217#ifdef LIBXML_C14N_ENABLED
1218 fprintf(stderr, "C14N ");
1219#endif
1220#ifdef LIBXML_CATALOG_ENABLED
1221 fprintf(stderr, "Catalog ");
1222#endif
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001223#ifdef LIBXML_XPATH_ENABLED
1224 fprintf(stderr, "XPath ");
1225#endif
1226#ifdef LIBXML_XPTR_ENABLED
1227 fprintf(stderr, "XPointer ");
1228#endif
1229#ifdef LIBXML_XINCLUDE_ENABLED
1230 fprintf(stderr, "XInclude ");
1231#endif
1232#ifdef LIBXML_ICONV_ENABLED
1233 fprintf(stderr, "Iconv ");
1234#endif
1235#ifdef DEBUG_MEMORY_LOCATION
1236 fprintf(stderr, "MemDebug ");
1237#endif
1238#ifdef LIBXML_UNICODE_ENABLED
1239 fprintf(stderr, "Unicode ");
1240#endif
1241#ifdef LIBXML_REGEXP_ENABLED
1242 fprintf(stderr, "Regexps ");
1243#endif
1244#ifdef LIBXML_AUTOMATA_ENABLED
1245 fprintf(stderr, "Automata ");
1246#endif
1247#ifdef LIBXML_SCHEMAS_ENABLED
1248 fprintf(stderr, "Schemas ");
1249#endif
1250 fprintf(stderr, "\n");
1251}
1252
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001253static void usage(const char *name) {
1254 printf("Usage : %s [options] XMLfiles ...\n", name);
1255 printf("\tParse the XML files and output the result of the parsing\n");
1256 printf("\t--version : display the version of the XML library used\n");
1257#ifdef LIBXML_DEBUG_ENABLED
1258 printf("\t--debug : dump a debug tree of the in-memory document\n");
1259 printf("\t--shell : run a navigating shell\n");
1260 printf("\t--debugent : debug the entities defined in the document\n");
Daniel Veillard8326e732003-01-07 00:19:07 +00001261#else
1262 printf("\t--debug : dump the nodes content when using --stream\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001263#endif
1264 printf("\t--copy : used to test the internal copy implementation\n");
1265 printf("\t--recover : output what was parsable on broken XML documents\n");
1266 printf("\t--noent : substitute entity references by their value\n");
1267 printf("\t--noout : don't output the result tree\n");
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001268 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001269 printf("\t--htmlout : output results as HTML\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001270 printf("\t--nowrap : do not put HTML doc wrapper\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001271 printf("\t--valid : validate the document in addition to std well-formed check\n");
1272 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
1273 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
Daniel Veillard66f68e72003-08-18 16:39:51 +00001274 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001275 printf("\t--timing : print some timings\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001276 printf("\t--output file or -o file: save to a given file\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001277 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
1278 printf("\t--insert : ad-hoc test for valid insertions\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001279#ifdef HAVE_ZLIB_H
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001280 printf("\t--compress : turn on gzip compression of output\n");
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001281#endif
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001282#ifdef LIBXML_HTML_ENABLED
1283 printf("\t--html : use the HTML parser\n");
1284#endif
1285 printf("\t--push : use the push mode of the parser\n");
1286#ifdef HAVE_SYS_MMAN_H
1287 printf("\t--memory : parse from memory\n");
1288#endif
1289 printf("\t--nowarning : do not emit warnings from parser/validator\n");
1290 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
Daniel Veillard90493a92001-08-14 14:12:47 +00001291 printf("\t--format : reformat/reindent the input\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001292 printf("\t--testIO : test user I/O support\n");
1293 printf("\t--encode encoding : output in the given encoding\n");
1294#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardbd9b0e82001-11-26 10:32:08 +00001295 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
1296 printf("\t otherwise XML Catalogs starting from \n");
Daniel Veillard3be27512003-01-26 19:49:04 +00001297 printf("\t " XML_XML_DEFAULT_CATALOG " are activated by default\n");
Daniel Veillard05c13a22001-09-09 08:38:09 +00001298 printf("\t--nocatalogs: deactivate all catalogs\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001299#endif
1300 printf("\t--auto : generate a small doc on the fly\n");
1301#ifdef LIBXML_XINCLUDE_ENABLED
1302 printf("\t--xinclude : do XInclude processing\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001303#endif
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001304 printf("\t--loaddtd : fetch external DTD\n");
Daniel Veillard48da9102001-08-07 01:10:10 +00001305 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
Daniel Veillard29e43992001-12-13 22:21:58 +00001306 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
Daniel Veillard7704fb12003-01-03 16:19:51 +00001307 printf("\t--stream : use the streaming interface to process very large files\n");
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001308 printf("\t--chkregister : verify the node registration code\n");
Daniel Veillardef4d3bc2003-02-07 12:38:22 +00001309#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillard71531f32003-02-05 13:19:53 +00001310 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001311 printf("\t--schema schema : do validation against the WXS schema\n");
Daniel Veillard71531f32003-02-05 13:19:53 +00001312#endif
Daniel Veillarda42f25f2002-01-25 14:15:40 +00001313 printf("\nLibxml project home page: http://xmlsoft.org/\n");
1314 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001315}
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001316
1317static void registerNode(xmlNodePtr node)
1318{
1319 node->_private = malloc(sizeof(long));
1320 *(long*)node->_private = (long) 0x81726354;
1321}
1322
1323static void deregisterNode(xmlNodePtr node)
1324{
1325 assert(node->_private != NULL);
1326 assert(*(long*)node->_private == (long) 0x81726354);
1327 free(node->_private);
1328}
1329
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001330int
1331main(int argc, char **argv) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001332 int i, acount;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001333 int files = 0;
Daniel Veillard845cce42002-01-09 11:51:37 +00001334 int version = 0;
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001335 const char* indent;
1336
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001337 if (argc <= 1) {
1338 usage(argv[0]);
1339 return(1);
1340 }
Daniel Veillardbe803962000-06-28 23:40:59 +00001341 LIBXML_TEST_VERSION
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001342 for (i = 1; i < argc ; i++) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001343 if (!strcmp(argv[i], "-"))
1344 break;
1345
1346 if (argv[i][0] != '-')
1347 continue;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001348 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
1349 debug++;
Daniel Veillard56ada1d2003-01-07 11:17:25 +00001350 else
1351#ifdef LIBXML_DEBUG_ENABLED
1352 if ((!strcmp(argv[i], "-shell")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001353 (!strcmp(argv[i], "--shell"))) {
1354 shell++;
1355 noout = 1;
1356 } else
1357#endif
1358 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
1359 copy++;
1360 else if ((!strcmp(argv[i], "-recover")) ||
1361 (!strcmp(argv[i], "--recover")))
1362 recovery++;
1363 else if ((!strcmp(argv[i], "-noent")) ||
1364 (!strcmp(argv[i], "--noent")))
1365 noent++;
Daniel Veillard4ec885a2001-06-17 10:31:07 +00001366 else if ((!strcmp(argv[i], "-version")) ||
Daniel Veillard845cce42002-01-09 11:51:37 +00001367 (!strcmp(argv[i], "--version"))) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00001368 showVersion(argv[0]);
Daniel Veillard845cce42002-01-09 11:51:37 +00001369 version = 1;
1370 } else if ((!strcmp(argv[i], "-noout")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001371 (!strcmp(argv[i], "--noout")))
1372 noout++;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001373 else if ((!strcmp(argv[i], "-o")) ||
1374 (!strcmp(argv[i], "-output")) ||
1375 (!strcmp(argv[i], "--output"))) {
1376 i++;
Daniel Veillard6e4f1c02002-04-09 09:55:20 +00001377 output = argv[i];
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001378 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001379 else if ((!strcmp(argv[i], "-htmlout")) ||
1380 (!strcmp(argv[i], "--htmlout")))
1381 htmlout++;
1382#ifdef LIBXML_HTML_ENABLED
1383 else if ((!strcmp(argv[i], "-html")) ||
1384 (!strcmp(argv[i], "--html"))) {
1385 html++;
1386 }
1387#endif /* LIBXML_HTML_ENABLED */
1388 else if ((!strcmp(argv[i], "-nowrap")) ||
1389 (!strcmp(argv[i], "--nowrap")))
1390 nowrap++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001391 else if ((!strcmp(argv[i], "-loaddtd")) ||
1392 (!strcmp(argv[i], "--loaddtd")))
1393 loaddtd++;
Daniel Veillard48da9102001-08-07 01:10:10 +00001394 else if ((!strcmp(argv[i], "-dtdattr")) ||
1395 (!strcmp(argv[i], "--dtdattr"))) {
1396 loaddtd++;
1397 dtdattrs++;
1398 } else if ((!strcmp(argv[i], "-valid")) ||
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001399 (!strcmp(argv[i], "--valid")))
1400 valid++;
1401 else if ((!strcmp(argv[i], "-postvalid")) ||
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001402 (!strcmp(argv[i], "--postvalid"))) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001403 postvalid++;
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001404 loaddtd++;
1405 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
Daniel Veillardcd429612000-10-11 15:57:05 +00001406 (!strcmp(argv[i], "--dtdvalid"))) {
1407 i++;
1408 dtdvalid = argv[i];
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001409 loaddtd++;
Daniel Veillard66f68e72003-08-18 16:39:51 +00001410 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
1411 (!strcmp(argv[i], "--dtdvalidfpi"))) {
1412 i++;
1413 dtdvalidfpi = argv[i];
1414 loaddtd++;
Daniel Veillardcd429612000-10-11 15:57:05 +00001415 }
Daniel Veillard29e43992001-12-13 22:21:58 +00001416 else if ((!strcmp(argv[i], "-dropdtd")) ||
1417 (!strcmp(argv[i], "--dropdtd")))
1418 dropdtd++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001419 else if ((!strcmp(argv[i], "-insert")) ||
1420 (!strcmp(argv[i], "--insert")))
1421 insert++;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001422 else if ((!strcmp(argv[i], "-timing")) ||
1423 (!strcmp(argv[i], "--timing")))
1424 timing++;
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001425 else if ((!strcmp(argv[i], "-auto")) ||
1426 (!strcmp(argv[i], "--auto")))
1427 generate++;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001428 else if ((!strcmp(argv[i], "-repeat")) ||
1429 (!strcmp(argv[i], "--repeat")))
1430 repeat++;
1431 else if ((!strcmp(argv[i], "-push")) ||
1432 (!strcmp(argv[i], "--push")))
1433 push++;
Daniel Veillard46e370e2000-07-21 20:32:03 +00001434#ifdef HAVE_SYS_MMAN_H
1435 else if ((!strcmp(argv[i], "-memory")) ||
1436 (!strcmp(argv[i], "--memory")))
1437 memory++;
1438#endif
Daniel Veillard5e873c42000-04-12 13:27:38 +00001439 else if ((!strcmp(argv[i], "-testIO")) ||
1440 (!strcmp(argv[i], "--testIO")))
1441 testIO++;
Daniel Veillard9e8bfae2000-11-06 16:43:11 +00001442#ifdef LIBXML_XINCLUDE_ENABLED
1443 else if ((!strcmp(argv[i], "-xinclude")) ||
1444 (!strcmp(argv[i], "--xinclude")))
1445 xinclude++;
1446#endif
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001447#ifdef HAVE_ZLIB_H
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001448 else if ((!strcmp(argv[i], "-compress")) ||
1449 (!strcmp(argv[i], "--compress"))) {
1450 compress++;
1451 xmlSetCompressMode(9);
1452 }
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001453#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001454 else if ((!strcmp(argv[i], "-nowarning")) ||
1455 (!strcmp(argv[i], "--nowarning"))) {
1456 xmlGetWarningsDefaultValue = 0;
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001457 xmlPedanticParserDefault(0);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001458 }
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001459 else if ((!strcmp(argv[i], "-pedantic")) ||
1460 (!strcmp(argv[i], "--pedantic"))) {
1461 xmlGetWarningsDefaultValue = 1;
1462 xmlPedanticParserDefault(1);
1463 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001464#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001465 else if ((!strcmp(argv[i], "-debugent")) ||
1466 (!strcmp(argv[i], "--debugent"))) {
1467 debugent++;
1468 xmlParserDebugEntities = 1;
1469 }
Daniel Veillard64c20ed2000-09-22 16:07:02 +00001470#endif
Daniel Veillard81418e32001-05-22 15:08:55 +00001471#ifdef LIBXML_CATALOG_ENABLED
1472 else if ((!strcmp(argv[i], "-catalogs")) ||
1473 (!strcmp(argv[i], "--catalogs"))) {
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001474 catalogs++;
1475 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
1476 (!strcmp(argv[i], "--nocatalogs"))) {
1477 nocatalogs++;
Daniel Veillard81418e32001-05-22 15:08:55 +00001478 }
1479#endif
Daniel Veillardbe803962000-06-28 23:40:59 +00001480 else if ((!strcmp(argv[i], "-encode")) ||
1481 (!strcmp(argv[i], "--encode"))) {
1482 i++;
1483 encoding = argv[i];
Daniel Veillardf0cc7cc2000-08-26 21:40:43 +00001484 /*
1485 * OK it's for testing purposes
1486 */
1487 xmlAddEncodingAlias("UTF-8", "DVEnc");
Daniel Veillardbe803962000-06-28 23:40:59 +00001488 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001489 else if ((!strcmp(argv[i], "-noblanks")) ||
1490 (!strcmp(argv[i], "--noblanks"))) {
1491 noblanks++;
1492 xmlKeepBlanksDefault(0);
Daniel Veillard90493a92001-08-14 14:12:47 +00001493 }
1494 else if ((!strcmp(argv[i], "-format")) ||
1495 (!strcmp(argv[i], "--format"))) {
1496 noblanks++;
1497 format++;
1498 xmlKeepBlanksDefault(0);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001499 }
1500 else if ((!strcmp(argv[i], "-stream")) ||
1501 (!strcmp(argv[i], "--stream"))) {
1502 stream++;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001503 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001504 else if ((!strcmp(argv[i], "-sax1")) ||
1505 (!strcmp(argv[i], "--sax1"))) {
1506 sax1++;
1507 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001508 else if ((!strcmp(argv[i], "-chkregister")) ||
1509 (!strcmp(argv[i], "--chkregister"))) {
1510 chkregister++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001511#ifdef LIBXML_SCHEMAS_ENABLED
1512 } else if ((!strcmp(argv[i], "-relaxng")) ||
1513 (!strcmp(argv[i], "--relaxng"))) {
1514 i++;
1515 relaxng = argv[i];
1516 noent++;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001517 } else if ((!strcmp(argv[i], "-schema")) ||
1518 (!strcmp(argv[i], "--schema"))) {
1519 i++;
1520 schema = argv[i];
1521 noent++;
Daniel Veillard71531f32003-02-05 13:19:53 +00001522#endif
Daniel Veillarde8b09e42003-05-13 22:14:13 +00001523 } else if ((!strcmp(argv[i], "-nonet")) ||
1524 (!strcmp(argv[i], "--nonet"))) {
1525 xmlSetExternalEntityLoader(xmlNoNetExternalEntityLoader);
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001526 } else {
1527 fprintf(stderr, "Unknown option %s\n", argv[i]);
1528 usage(argv[0]);
1529 return(1);
1530 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001531 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001532
1533#ifdef LIBXML_CATALOG_ENABLED
1534 if (nocatalogs == 0) {
1535 if (catalogs) {
1536 const char *catal;
1537
1538 catal = getenv("SGML_CATALOG_FILES");
Daniel Veillard6c5f9d12001-08-25 13:33:14 +00001539 if (catal != NULL) {
1540 xmlLoadCatalogs(catal);
1541 } else {
1542 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
1543 }
Daniel Veillarde2940dd2001-08-22 00:06:49 +00001544 }
1545 }
1546#endif
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001547
Daniel Veillard07cb8222003-09-10 10:51:05 +00001548 if (sax1)
1549 xmlSAXDefaultVersion(1);
1550 else
1551 xmlSAXDefaultVersion(2);
1552
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001553 if (chkregister) {
1554 xmlRegisterNodeDefault(registerNode);
1555 xmlDeregisterNodeDefault(deregisterNode);
1556 }
Aleksey Sanin693c9bc2003-03-09 22:36:52 +00001557
1558 indent = getenv("XMLLINT_INDENT");
1559 if(indent != NULL) {
1560 xmlTreeIndentString = indent;
1561 }
1562
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001563
Daniel Veillardd9bad132001-07-23 19:39:43 +00001564 xmlLineNumbersDefault(1);
Daniel Veillard48da9102001-08-07 01:10:10 +00001565 if (loaddtd != 0)
1566 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
1567 if (dtdattrs)
1568 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001569 if (noent != 0) xmlSubstituteEntitiesDefault(1);
1570 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
1571 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001572 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001573 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001574 xmlGenericError(xmlGenericErrorContext,
1575 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
1576 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001577 "<html><head><title>%s output</title></head>\n",
1578 argv[0]);
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001579 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001580 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
1581 argv[0]);
1582 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001583
1584#ifdef LIBXML_SCHEMAS_ENABLED
Daniel Veillardce192eb2003-04-16 15:58:05 +00001585 if ((relaxng != NULL) && (stream == 0)) {
Daniel Veillard71531f32003-02-05 13:19:53 +00001586 xmlRelaxNGParserCtxtPtr ctxt;
1587
Daniel Veillardce192eb2003-04-16 15:58:05 +00001588 /* forces loading the DTDs */
1589 xmlLoadExtDtdDefaultValue |= 1;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001590 if (timing) {
1591 startTimer();
1592 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001593 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
1594 xmlRelaxNGSetParserErrors(ctxt,
1595 (xmlRelaxNGValidityErrorFunc) fprintf,
1596 (xmlRelaxNGValidityWarningFunc) fprintf,
1597 stderr);
1598 relaxngschemas = xmlRelaxNGParse(ctxt);
Daniel Veillardce192eb2003-04-16 15:58:05 +00001599 if (relaxngschemas == NULL) {
1600 xmlGenericError(xmlGenericErrorContext,
1601 "Relax-NG schema %s failed to compile\n", relaxng);
1602 relaxng = NULL;
1603 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001604 xmlRelaxNGFreeParserCtxt(ctxt);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001605 if (timing) {
1606 endTimer("Compiling the schemas");
1607 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001608 } else if ((schema != NULL) && (stream == 0)) {
1609 xmlSchemaParserCtxtPtr ctxt;
1610
1611 if (timing) {
1612 startTimer();
1613 }
1614 ctxt = xmlSchemaNewParserCtxt(schema);
1615 xmlSchemaSetParserErrors(ctxt,
1616 (xmlSchemaValidityErrorFunc) fprintf,
1617 (xmlSchemaValidityWarningFunc) fprintf,
1618 stderr);
1619 wxschemas = xmlSchemaParse(ctxt);
1620 if (wxschemas == NULL) {
1621 xmlGenericError(xmlGenericErrorContext,
1622 "WXS schema %s failed to compile\n", schema);
1623 schema = NULL;
1624 }
1625 xmlSchemaFreeParserCtxt(ctxt);
1626 if (timing) {
1627 endTimer("Compiling the schemas");
1628 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001629 }
1630#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001631 for (i = 1; i < argc ; i++) {
Daniel Veillardbe803962000-06-28 23:40:59 +00001632 if ((!strcmp(argv[i], "-encode")) ||
1633 (!strcmp(argv[i], "--encode"))) {
1634 i++;
1635 continue;
Daniel Veillard1df3dfc2001-12-18 11:14:16 +00001636 } else if ((!strcmp(argv[i], "-o")) ||
1637 (!strcmp(argv[i], "-output")) ||
1638 (!strcmp(argv[i], "--output"))) {
1639 i++;
1640 continue;
Daniel Veillardbe803962000-06-28 23:40:59 +00001641 }
Daniel Veillardcd429612000-10-11 15:57:05 +00001642 if ((!strcmp(argv[i], "-dtdvalid")) ||
1643 (!strcmp(argv[i], "--dtdvalid"))) {
1644 i++;
1645 continue;
1646 }
Daniel Veillard66f68e72003-08-18 16:39:51 +00001647 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
1648 (!strcmp(argv[i], "--dtdvalidfpi"))) {
1649 i++;
1650 continue;
1651 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001652 if ((!strcmp(argv[i], "-relaxng")) ||
1653 (!strcmp(argv[i], "--relaxng"))) {
1654 i++;
1655 continue;
1656 }
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001657 if ((!strcmp(argv[i], "-schema")) ||
1658 (!strcmp(argv[i], "--schema"))) {
1659 i++;
1660 continue;
1661 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001662 if ((timing) && (repeat))
Daniel Veillard01db67c2001-12-18 07:09:59 +00001663 startTimer();
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001664 /* Remember file names. "-" means stdin. <sven@zen.org> */
Daniel Veillard4a6845d2001-01-03 13:32:39 +00001665 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001666 if (repeat) {
Daniel Veillard7704fb12003-01-03 16:19:51 +00001667 for (acount = 0;acount < 100 * repeat;acount++)
1668 if (stream != 0)
1669 streamFile(argv[i]);
1670 else
1671 parseAndPrintFile(argv[i]);
1672 } else {
1673 if (stream != 0)
1674 streamFile(argv[i]);
1675 else
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001676 parseAndPrintFile(argv[i]);
Daniel Veillard7704fb12003-01-03 16:19:51 +00001677 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001678 files ++;
Daniel Veillarda7866932001-12-04 13:14:44 +00001679 if ((timing) && (repeat)) {
Daniel Veillard01db67c2001-12-18 07:09:59 +00001680 endTimer("100 iterations");
Daniel Veillarda7866932001-12-04 13:14:44 +00001681 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001682 }
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001683 }
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001684 if (generate)
1685 parseAndPrintFile(NULL);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001686 if ((htmlout) && (!nowrap)) {
Daniel Veillardd6d7f7b2000-10-25 19:56:55 +00001687 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001688 }
Daniel Veillard845cce42002-01-09 11:51:37 +00001689 if ((files == 0) && (!generate) && (version == 0)) {
Daniel Veillard10ea86c2001-06-20 13:55:33 +00001690 usage(argv[0]);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001691 }
Daniel Veillard71531f32003-02-05 13:19:53 +00001692#ifdef LIBXML_SCHEMAS_ENABLED
1693 if (relaxngschemas != NULL)
1694 xmlRelaxNGFree(relaxngschemas);
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001695 if (wxschemas != NULL)
1696 xmlSchemaFree(wxschemas);
Daniel Veillard71531f32003-02-05 13:19:53 +00001697 xmlRelaxNGCleanupTypes();
1698#endif
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001699 xmlCleanupParser();
1700 xmlMemoryDump();
1701
Daniel Veillardf7cd4812001-02-23 18:44:52 +00001702 return(progresult);
Daniel Veillardce8b83b2000-04-05 18:38:42 +00001703}
Daniel Veillard88a172f2000-08-04 18:23:10 +00001704